Paul Kehrer | 5d5d28d | 2015-10-21 18:55:22 -0500 | [diff] [blame] | 1 | import datetime |
Paul Kehrer | 8d887e1 | 2015-10-24 09:09:55 -0500 | [diff] [blame] | 2 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 3 | from base64 import b16encode |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 4 | from functools import partial |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 5 | from operator import __eq__, __ne__, __lt__, __le__, __gt__, __ge__ |
| 6 | |
| 7 | from six import ( |
| 8 | integer_types as _integer_types, |
Jean-Paul Calderone | f22abcd | 2014-05-01 09:31:19 -0400 | [diff] [blame] | 9 | text_type as _text_type, |
| 10 | PY3 as _PY3) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 11 | |
Alex Gaynor | 9939ba1 | 2017-06-25 16:28:24 -0400 | [diff] [blame] | 12 | from cryptography import x509 |
Paul Kehrer | 72d968b | 2016-07-29 15:31:04 +0800 | [diff] [blame] | 13 | from cryptography.hazmat.primitives.asymmetric import dsa, rsa |
Alex Gaynor | 10d3083 | 2017-06-29 15:31:39 -0700 | [diff] [blame] | 14 | from cryptography.utils import deprecated |
Paul Kehrer | 72d968b | 2016-07-29 15:31:04 +0800 | [diff] [blame] | 15 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 16 | from OpenSSL._util import ( |
| 17 | ffi as _ffi, |
| 18 | lib as _lib, |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 19 | exception_from_error_queue as _exception_from_error_queue, |
| 20 | byte_string as _byte_string, |
Jean-Paul Calderone | 00f84eb | 2015-04-13 12:47:21 -0400 | [diff] [blame] | 21 | native as _native, |
| 22 | UNSPECIFIED as _UNSPECIFIED, |
Jean-Paul Calderone | 39a8d59 | 2015-04-13 20:49:50 -0400 | [diff] [blame] | 23 | text_to_bytes_and_warn as _text_to_bytes_and_warn, |
Alex Gaynor | 67903a6 | 2016-06-02 10:37:13 -0700 | [diff] [blame] | 24 | make_assert as _make_assert, |
Jean-Paul Calderone | 00f84eb | 2015-04-13 12:47:21 -0400 | [diff] [blame] | 25 | ) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 26 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 27 | FILETYPE_PEM = _lib.SSL_FILETYPE_PEM |
| 28 | FILETYPE_ASN1 = _lib.SSL_FILETYPE_ASN1 |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 29 | |
| 30 | # TODO This was an API mistake. OpenSSL has no such constant. |
| 31 | FILETYPE_TEXT = 2 ** 16 - 1 |
| 32 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 33 | TYPE_RSA = _lib.EVP_PKEY_RSA |
| 34 | TYPE_DSA = _lib.EVP_PKEY_DSA |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 35 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 36 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 37 | class Error(Exception): |
Jean-Paul Calderone | 511cde0 | 2013-12-29 10:31:13 -0500 | [diff] [blame] | 38 | """ |
| 39 | An error occurred in an `OpenSSL.crypto` API. |
| 40 | """ |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 41 | |
| 42 | |
| 43 | _raise_current_error = partial(_exception_from_error_queue, Error) |
Alex Gaynor | 67903a6 | 2016-06-02 10:37:13 -0700 | [diff] [blame] | 44 | _openssl_assert = _make_assert(Error) |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 45 | |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 46 | |
Paul Kehrer | eb63384 | 2016-10-06 11:22:01 +0200 | [diff] [blame] | 47 | def _get_backend(): |
| 48 | """ |
| 49 | Importing the backend from cryptography has the side effect of activating |
| 50 | the osrandom engine. This mutates the global state of OpenSSL in the |
| 51 | process and causes issues for various programs that use subinterpreters or |
| 52 | embed Python. By putting the import in this function we can avoid |
| 53 | triggering this side effect unless _get_backend is called. |
| 54 | """ |
| 55 | from cryptography.hazmat.backends.openssl.backend import backend |
| 56 | return backend |
| 57 | |
| 58 | |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 59 | def _untested_error(where): |
| 60 | """ |
| 61 | An OpenSSL API failed somehow. Additionally, the failure which was |
| 62 | encountered isn't one that's exercised by the test suite so future behavior |
| 63 | of pyOpenSSL is now somewhat less predictable. |
| 64 | """ |
| 65 | raise RuntimeError("Unknown %s failure" % (where,)) |
| 66 | |
| 67 | |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 68 | def _new_mem_buf(buffer=None): |
| 69 | """ |
| 70 | Allocate a new OpenSSL memory BIO. |
| 71 | |
| 72 | Arrange for the garbage collector to clean it up automatically. |
| 73 | |
| 74 | :param buffer: None or some bytes to use to put into the BIO so that they |
| 75 | can be read out. |
| 76 | """ |
| 77 | if buffer is None: |
| 78 | bio = _lib.BIO_new(_lib.BIO_s_mem()) |
| 79 | free = _lib.BIO_free |
| 80 | else: |
| 81 | data = _ffi.new("char[]", buffer) |
| 82 | bio = _lib.BIO_new_mem_buf(data, len(buffer)) |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 83 | |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 84 | # Keep the memory alive as long as the bio is alive! |
| 85 | def free(bio, ref=data): |
| 86 | return _lib.BIO_free(bio) |
| 87 | |
Alex Gaynor | fb8a2a1 | 2016-06-04 18:26:26 -0700 | [diff] [blame] | 88 | _openssl_assert(bio != _ffi.NULL) |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 89 | |
| 90 | bio = _ffi.gc(bio, free) |
| 91 | return bio |
| 92 | |
| 93 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 94 | def _bio_to_string(bio): |
| 95 | """ |
| 96 | Copy the contents of an OpenSSL BIO object into a Python byte string. |
| 97 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 98 | result_buffer = _ffi.new('char**') |
| 99 | buffer_length = _lib.BIO_get_mem_data(bio, result_buffer) |
| 100 | return _ffi.buffer(result_buffer[0], buffer_length)[:] |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 101 | |
| 102 | |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 103 | def _set_asn1_time(boundary, when): |
Jean-Paul Calderone | e728e87 | 2013-12-29 10:37:15 -0500 | [diff] [blame] | 104 | """ |
| 105 | The the time value of an ASN1 time object. |
| 106 | |
Moriyoshi Koizumi | 80b25ef | 2017-06-22 00:54:20 +0900 | [diff] [blame] | 107 | @param boundary: An ASN1_TIME pointer (or an object safely |
Jean-Paul Calderone | e728e87 | 2013-12-29 10:37:15 -0500 | [diff] [blame] | 108 | castable to that type) which will have its value set. |
| 109 | @param when: A string representation of the desired time value. |
| 110 | |
| 111 | @raise TypeError: If C{when} is not a L{bytes} string. |
| 112 | @raise ValueError: If C{when} does not represent a time in the required |
| 113 | format. |
| 114 | @raise RuntimeError: If the time value cannot be set for some other |
| 115 | (unspecified) reason. |
| 116 | """ |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 117 | if not isinstance(when, bytes): |
| 118 | raise TypeError("when must be a byte string") |
| 119 | |
Moriyoshi Koizumi | 80b25ef | 2017-06-22 00:54:20 +0900 | [diff] [blame] | 120 | set_result = _lib.ASN1_TIME_set_string(boundary, when) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 121 | if set_result == 0: |
Moriyoshi Koizumi | 80b25ef | 2017-06-22 00:54:20 +0900 | [diff] [blame] | 122 | raise ValueError("Invalid string") |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 123 | |
Alex Gaynor | 510293e | 2016-06-02 12:07:59 -0700 | [diff] [blame] | 124 | |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 125 | def _get_asn1_time(timestamp): |
Jean-Paul Calderone | e728e87 | 2013-12-29 10:37:15 -0500 | [diff] [blame] | 126 | """ |
| 127 | Retrieve the time value of an ASN1 time object. |
| 128 | |
| 129 | @param timestamp: An ASN1_GENERALIZEDTIME* (or an object safely castable to |
| 130 | that type) from which the time value will be retrieved. |
| 131 | |
| 132 | @return: The time value from C{timestamp} as a L{bytes} string in a certain |
| 133 | format. Or C{None} if the object contains no time value. |
| 134 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 135 | string_timestamp = _ffi.cast('ASN1_STRING*', timestamp) |
| 136 | if _lib.ASN1_STRING_length(string_timestamp) == 0: |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 137 | return None |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 138 | elif ( |
| 139 | _lib.ASN1_STRING_type(string_timestamp) == _lib.V_ASN1_GENERALIZEDTIME |
| 140 | ): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 141 | return _ffi.string(_lib.ASN1_STRING_data(string_timestamp)) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 142 | else: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 143 | generalized_timestamp = _ffi.new("ASN1_GENERALIZEDTIME**") |
| 144 | _lib.ASN1_TIME_to_generalizedtime(timestamp, generalized_timestamp) |
| 145 | if generalized_timestamp[0] == _ffi.NULL: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 146 | # This may happen: |
| 147 | # - if timestamp was not an ASN1_TIME |
| 148 | # - if allocating memory for the ASN1_GENERALIZEDTIME failed |
| 149 | # - if a copy of the time data from timestamp cannot be made for |
| 150 | # the newly allocated ASN1_GENERALIZEDTIME |
| 151 | # |
| 152 | # These are difficult to test. cffi enforces the ASN1_TIME type. |
| 153 | # Memory allocation failures are a pain to trigger |
| 154 | # deterministically. |
| 155 | _untested_error("ASN1_TIME_to_generalizedtime") |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 156 | else: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 157 | string_timestamp = _ffi.cast( |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 158 | "ASN1_STRING*", generalized_timestamp[0]) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 159 | string_data = _lib.ASN1_STRING_data(string_timestamp) |
| 160 | string_result = _ffi.string(string_data) |
| 161 | _lib.ASN1_GENERALIZEDTIME_free(generalized_timestamp[0]) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 162 | return string_result |
| 163 | |
| 164 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 165 | class PKey(object): |
Laurens Van Houtven | 6e7dd43 | 2014-06-17 16:10:57 +0200 | [diff] [blame] | 166 | """ |
| 167 | A class representing an DSA or RSA public key or key pair. |
| 168 | """ |
Jean-Paul Calderone | edafced | 2013-02-19 11:48:38 -0800 | [diff] [blame] | 169 | _only_public = False |
Jean-Paul Calderone | 09e3bdc | 2013-02-19 12:15:28 -0800 | [diff] [blame] | 170 | _initialized = True |
Jean-Paul Calderone | edafced | 2013-02-19 11:48:38 -0800 | [diff] [blame] | 171 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 172 | def __init__(self): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 173 | pkey = _lib.EVP_PKEY_new() |
| 174 | self._pkey = _ffi.gc(pkey, _lib.EVP_PKEY_free) |
Jean-Paul Calderone | 09e3bdc | 2013-02-19 12:15:28 -0800 | [diff] [blame] | 175 | self._initialized = False |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 176 | |
Paul Kehrer | 72d968b | 2016-07-29 15:31:04 +0800 | [diff] [blame] | 177 | def to_cryptography_key(self): |
| 178 | """ |
| 179 | Export as a ``cryptography`` key. |
| 180 | |
| 181 | :rtype: One of ``cryptography``'s `key interfaces`_. |
| 182 | |
| 183 | .. _key interfaces: https://cryptography.io/en/latest/hazmat/\ |
| 184 | primitives/asymmetric/rsa/#key-interfaces |
| 185 | |
| 186 | .. versionadded:: 16.1.0 |
| 187 | """ |
Paul Kehrer | eb63384 | 2016-10-06 11:22:01 +0200 | [diff] [blame] | 188 | backend = _get_backend() |
Paul Kehrer | 72d968b | 2016-07-29 15:31:04 +0800 | [diff] [blame] | 189 | if self._only_public: |
| 190 | return backend._evp_pkey_to_public_key(self._pkey) |
| 191 | else: |
| 192 | return backend._evp_pkey_to_private_key(self._pkey) |
| 193 | |
| 194 | @classmethod |
| 195 | def from_cryptography_key(cls, crypto_key): |
| 196 | """ |
| 197 | Construct based on a ``cryptography`` *crypto_key*. |
| 198 | |
| 199 | :param crypto_key: A ``cryptography`` key. |
| 200 | :type crypto_key: One of ``cryptography``'s `key interfaces`_. |
| 201 | |
| 202 | :rtype: PKey |
| 203 | |
| 204 | .. versionadded:: 16.1.0 |
| 205 | """ |
| 206 | pkey = cls() |
| 207 | if not isinstance(crypto_key, (rsa.RSAPublicKey, rsa.RSAPrivateKey, |
| 208 | dsa.DSAPublicKey, dsa.DSAPrivateKey)): |
| 209 | raise TypeError("Unsupported key type") |
| 210 | |
| 211 | pkey._pkey = crypto_key._evp_pkey |
| 212 | if isinstance(crypto_key, (rsa.RSAPublicKey, dsa.DSAPublicKey)): |
| 213 | pkey._only_public = True |
| 214 | pkey._initialized = True |
| 215 | return pkey |
| 216 | |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 217 | def generate_key(self, type, bits): |
| 218 | """ |
Laurens Van Houtven | 90c0914 | 2015-04-23 10:52:49 -0700 | [diff] [blame] | 219 | Generate a key pair of the given type, with the given number of bits. |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 220 | |
Laurens Van Houtven | 6e7dd43 | 2014-06-17 16:10:57 +0200 | [diff] [blame] | 221 | This generates a key "into" the this object. |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 222 | |
Laurens Van Houtven | 6e7dd43 | 2014-06-17 16:10:57 +0200 | [diff] [blame] | 223 | :param type: The key type. |
| 224 | :type type: :py:data:`TYPE_RSA` or :py:data:`TYPE_DSA` |
| 225 | :param bits: The number of bits. |
| 226 | :type bits: :py:data:`int` ``>= 0`` |
| 227 | :raises TypeError: If :py:data:`type` or :py:data:`bits` isn't |
| 228 | of the appropriate type. |
| 229 | :raises ValueError: If the number of bits isn't an integer of |
| 230 | the appropriate size. |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 231 | :return: ``None`` |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 232 | """ |
| 233 | if not isinstance(type, int): |
| 234 | raise TypeError("type must be an integer") |
| 235 | |
| 236 | if not isinstance(bits, int): |
| 237 | raise TypeError("bits must be an integer") |
| 238 | |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 239 | # TODO Check error return |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 240 | exponent = _lib.BN_new() |
| 241 | exponent = _ffi.gc(exponent, _lib.BN_free) |
| 242 | _lib.BN_set_word(exponent, _lib.RSA_F4) |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 243 | |
| 244 | if type == TYPE_RSA: |
| 245 | if bits <= 0: |
| 246 | raise ValueError("Invalid number of bits") |
| 247 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 248 | rsa = _lib.RSA_new() |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 249 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 250 | result = _lib.RSA_generate_key_ex(rsa, bits, exponent, _ffi.NULL) |
Alex Gaynor | 5bb2bd1 | 2016-07-03 10:48:32 -0400 | [diff] [blame] | 251 | _openssl_assert(result == 1) |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 252 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 253 | result = _lib.EVP_PKEY_assign_RSA(self._pkey, rsa) |
Alex Gaynor | 5bb2bd1 | 2016-07-03 10:48:32 -0400 | [diff] [blame] | 254 | _openssl_assert(result == 1) |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 255 | |
| 256 | elif type == TYPE_DSA: |
Paul Kehrer | a0860b9 | 2016-03-09 21:39:27 -0400 | [diff] [blame] | 257 | dsa = _lib.DSA_new() |
Alex Gaynor | fb8a2a1 | 2016-06-04 18:26:26 -0700 | [diff] [blame] | 258 | _openssl_assert(dsa != _ffi.NULL) |
Paul Kehrer | afa5a66 | 2016-03-10 10:29:28 -0400 | [diff] [blame] | 259 | |
| 260 | dsa = _ffi.gc(dsa, _lib.DSA_free) |
Paul Kehrer | a0860b9 | 2016-03-09 21:39:27 -0400 | [diff] [blame] | 261 | res = _lib.DSA_generate_parameters_ex( |
| 262 | dsa, bits, _ffi.NULL, 0, _ffi.NULL, _ffi.NULL, _ffi.NULL |
| 263 | ) |
Alex Gaynor | fb8a2a1 | 2016-06-04 18:26:26 -0700 | [diff] [blame] | 264 | _openssl_assert(res == 1) |
Alex Gaynor | 09a386e | 2016-07-03 09:32:44 -0400 | [diff] [blame] | 265 | |
| 266 | _openssl_assert(_lib.DSA_generate_key(dsa) == 1) |
| 267 | _openssl_assert(_lib.EVP_PKEY_set1_DSA(self._pkey, dsa) == 1) |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 268 | else: |
| 269 | raise Error("No such key type") |
| 270 | |
Jean-Paul Calderone | 09e3bdc | 2013-02-19 12:15:28 -0800 | [diff] [blame] | 271 | self._initialized = True |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 272 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 273 | def check(self): |
| 274 | """ |
| 275 | Check the consistency of an RSA private key. |
| 276 | |
Laurens Van Houtven | 6e7dd43 | 2014-06-17 16:10:57 +0200 | [diff] [blame] | 277 | This is the Python equivalent of OpenSSL's ``RSA_check_key``. |
| 278 | |
Hynek Schlawack | 01c3167 | 2016-12-11 15:14:09 +0100 | [diff] [blame] | 279 | :return: ``True`` if key is consistent. |
| 280 | |
| 281 | :raise OpenSSL.crypto.Error: if the key is inconsistent. |
| 282 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 283 | :raise TypeError: if the key is of a type which cannot be checked. |
| 284 | Only RSA keys can currently be checked. |
| 285 | """ |
Jean-Paul Calderone | c86fcaf | 2013-02-20 12:38:33 -0800 | [diff] [blame] | 286 | if self._only_public: |
| 287 | raise TypeError("public key only") |
| 288 | |
Hynek Schlawack | 2a91ba3 | 2016-01-31 14:18:54 +0100 | [diff] [blame] | 289 | if _lib.EVP_PKEY_type(self.type()) != _lib.EVP_PKEY_RSA: |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 290 | raise TypeError("key type unsupported") |
| 291 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 292 | rsa = _lib.EVP_PKEY_get1_RSA(self._pkey) |
| 293 | rsa = _ffi.gc(rsa, _lib.RSA_free) |
| 294 | result = _lib.RSA_check_key(rsa) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 295 | if result: |
| 296 | return True |
| 297 | _raise_current_error() |
| 298 | |
Jean-Paul Calderone | c86fcaf | 2013-02-20 12:38:33 -0800 | [diff] [blame] | 299 | def type(self): |
| 300 | """ |
| 301 | Returns the type of the key |
| 302 | |
| 303 | :return: The type of the key. |
| 304 | """ |
Alex Gaynor | 0d2aec5 | 2017-05-31 04:26:27 -0400 | [diff] [blame] | 305 | return _lib.EVP_PKEY_id(self._pkey) |
Jean-Paul Calderone | c86fcaf | 2013-02-20 12:38:33 -0800 | [diff] [blame] | 306 | |
Jean-Paul Calderone | c86fcaf | 2013-02-20 12:38:33 -0800 | [diff] [blame] | 307 | def bits(self): |
| 308 | """ |
| 309 | Returns the number of bits of the key |
| 310 | |
| 311 | :return: The number of bits of the key. |
| 312 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 313 | return _lib.EVP_PKEY_bits(self._pkey) |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 314 | |
| 315 | |
Alex Gaynor | 10d3083 | 2017-06-29 15:31:39 -0700 | [diff] [blame] | 316 | PKeyType = deprecated( |
| 317 | PKey, __name__, |
| 318 | "PKeyType has been deprecated, use PKey instead", |
| 319 | DeprecationWarning |
| 320 | ) |
Jean-Paul Calderone | c86fcaf | 2013-02-20 12:38:33 -0800 | [diff] [blame] | 321 | |
| 322 | |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 323 | class _EllipticCurve(object): |
| 324 | """ |
Jean-Paul Calderone | aaf516d | 2014-04-19 09:10:45 -0400 | [diff] [blame] | 325 | A representation of a supported elliptic curve. |
Jean-Paul Calderone | 73945e3 | 2014-04-30 18:18:01 -0400 | [diff] [blame] | 326 | |
| 327 | @cvar _curves: :py:obj:`None` until an attempt is made to load the curves. |
| 328 | Thereafter, a :py:type:`set` containing :py:type:`_EllipticCurve` |
| 329 | instances each of which represents one curve supported by the system. |
| 330 | @type _curves: :py:type:`NoneType` or :py:type:`set` |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 331 | """ |
Jean-Paul Calderone | 73945e3 | 2014-04-30 18:18:01 -0400 | [diff] [blame] | 332 | _curves = None |
| 333 | |
Jean-Paul Calderone | f22abcd | 2014-05-01 09:31:19 -0400 | [diff] [blame] | 334 | if _PY3: |
Jean-Paul Calderone | a538105 | 2014-05-01 09:32:46 -0400 | [diff] [blame] | 335 | # This only necessary on Python 3. Morever, it is broken on Python 2. |
Jean-Paul Calderone | f22abcd | 2014-05-01 09:31:19 -0400 | [diff] [blame] | 336 | def __ne__(self, other): |
Jean-Paul Calderone | a538105 | 2014-05-01 09:32:46 -0400 | [diff] [blame] | 337 | """ |
| 338 | Implement cooperation with the right-hand side argument of ``!=``. |
| 339 | |
| 340 | Python 3 seems to have dropped this cooperation in this very narrow |
| 341 | circumstance. |
| 342 | """ |
Jean-Paul Calderone | f22abcd | 2014-05-01 09:31:19 -0400 | [diff] [blame] | 343 | if isinstance(other, _EllipticCurve): |
| 344 | return super(_EllipticCurve, self).__ne__(other) |
| 345 | return NotImplemented |
Jean-Paul Calderone | 40da72d | 2014-05-01 09:25:17 -0400 | [diff] [blame] | 346 | |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 347 | @classmethod |
Jean-Paul Calderone | 73945e3 | 2014-04-30 18:18:01 -0400 | [diff] [blame] | 348 | def _load_elliptic_curves(cls, lib): |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 349 | """ |
Jean-Paul Calderone | 73945e3 | 2014-04-30 18:18:01 -0400 | [diff] [blame] | 350 | Get the curves supported by OpenSSL. |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 351 | |
| 352 | :param lib: The OpenSSL library binding object. |
Jean-Paul Calderone | 73945e3 | 2014-04-30 18:18:01 -0400 | [diff] [blame] | 353 | |
| 354 | :return: A :py:type:`set` of ``cls`` instances giving the names of the |
| 355 | elliptic curves the underlying library supports. |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 356 | """ |
Alex Chan | 84902a2 | 2017-04-20 11:50:47 +0100 | [diff] [blame] | 357 | num_curves = lib.EC_get_builtin_curves(_ffi.NULL, 0) |
| 358 | builtin_curves = _ffi.new('EC_builtin_curve[]', num_curves) |
| 359 | # The return value on this call should be num_curves again. We |
| 360 | # could check it to make sure but if it *isn't* then.. what could |
| 361 | # we do? Abort the whole process, I suppose...? -exarkun |
| 362 | lib.EC_get_builtin_curves(builtin_curves, num_curves) |
| 363 | return set( |
| 364 | cls.from_nid(lib, c.nid) |
| 365 | for c in builtin_curves) |
Jean-Paul Calderone | 73945e3 | 2014-04-30 18:18:01 -0400 | [diff] [blame] | 366 | |
Jean-Paul Calderone | 73945e3 | 2014-04-30 18:18:01 -0400 | [diff] [blame] | 367 | @classmethod |
| 368 | def _get_elliptic_curves(cls, lib): |
| 369 | """ |
| 370 | Get, cache, and return the curves supported by OpenSSL. |
| 371 | |
| 372 | :param lib: The OpenSSL library binding object. |
| 373 | |
| 374 | :return: A :py:type:`set` of ``cls`` instances giving the names of the |
| 375 | elliptic curves the underlying library supports. |
| 376 | """ |
| 377 | if cls._curves is None: |
| 378 | cls._curves = cls._load_elliptic_curves(lib) |
| 379 | return cls._curves |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 380 | |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 381 | @classmethod |
| 382 | def from_nid(cls, lib, nid): |
Jean-Paul Calderone | aaf516d | 2014-04-19 09:10:45 -0400 | [diff] [blame] | 383 | """ |
| 384 | Instantiate a new :py:class:`_EllipticCurve` associated with the given |
| 385 | OpenSSL NID. |
| 386 | |
| 387 | :param lib: The OpenSSL library binding object. |
| 388 | |
| 389 | :param nid: The OpenSSL NID the resulting curve object will represent. |
| 390 | This must be a curve NID (and not, for example, a hash NID) or |
| 391 | subsequent operations will fail in unpredictable ways. |
| 392 | :type nid: :py:class:`int` |
| 393 | |
| 394 | :return: The curve object. |
| 395 | """ |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 396 | return cls(lib, nid, _ffi.string(lib.OBJ_nid2sn(nid)).decode("ascii")) |
| 397 | |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 398 | def __init__(self, lib, nid, name): |
Jean-Paul Calderone | aaf516d | 2014-04-19 09:10:45 -0400 | [diff] [blame] | 399 | """ |
| 400 | :param _lib: The :py:mod:`cryptography` binding instance used to |
| 401 | interface with OpenSSL. |
| 402 | |
| 403 | :param _nid: The OpenSSL NID identifying the curve this object |
| 404 | represents. |
| 405 | :type _nid: :py:class:`int` |
| 406 | |
| 407 | :param name: The OpenSSL short name identifying the curve this object |
| 408 | represents. |
| 409 | :type name: :py:class:`unicode` |
| 410 | """ |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 411 | self._lib = lib |
| 412 | self._nid = nid |
| 413 | self.name = name |
| 414 | |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 415 | def __repr__(self): |
| 416 | return "<Curve %r>" % (self.name,) |
| 417 | |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 418 | def _to_EC_KEY(self): |
Jean-Paul Calderone | aaf516d | 2014-04-19 09:10:45 -0400 | [diff] [blame] | 419 | """ |
| 420 | Create a new OpenSSL EC_KEY structure initialized to use this curve. |
| 421 | |
| 422 | The structure is automatically garbage collected when the Python object |
| 423 | is garbage collected. |
| 424 | """ |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 425 | key = self._lib.EC_KEY_new_by_curve_name(self._nid) |
| 426 | return _ffi.gc(key, _lib.EC_KEY_free) |
| 427 | |
| 428 | |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 429 | def get_elliptic_curves(): |
Jean-Paul Calderone | aaf516d | 2014-04-19 09:10:45 -0400 | [diff] [blame] | 430 | """ |
| 431 | Return a set of objects representing the elliptic curves supported in the |
| 432 | OpenSSL build in use. |
| 433 | |
| 434 | The curve objects have a :py:class:`unicode` ``name`` attribute by which |
| 435 | they identify themselves. |
| 436 | |
| 437 | The curve objects are useful as values for the argument accepted by |
Jean-Paul Calderone | 3b04e35 | 2014-04-19 09:29:10 -0400 | [diff] [blame] | 438 | :py:meth:`Context.set_tmp_ecdh` to specify which elliptical curve should be |
| 439 | used for ECDHE key exchange. |
Jean-Paul Calderone | aaf516d | 2014-04-19 09:10:45 -0400 | [diff] [blame] | 440 | """ |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 441 | return _EllipticCurve._get_elliptic_curves(_lib) |
| 442 | |
| 443 | |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 444 | def get_elliptic_curve(name): |
Jean-Paul Calderone | aaf516d | 2014-04-19 09:10:45 -0400 | [diff] [blame] | 445 | """ |
| 446 | Return a single curve object selected by name. |
| 447 | |
| 448 | See :py:func:`get_elliptic_curves` for information about curve objects. |
| 449 | |
Jean-Paul Calderone | d5839e2 | 2014-04-19 09:26:44 -0400 | [diff] [blame] | 450 | :param name: The OpenSSL short name identifying the curve object to |
| 451 | retrieve. |
| 452 | :type name: :py:class:`unicode` |
| 453 | |
Jean-Paul Calderone | aaf516d | 2014-04-19 09:10:45 -0400 | [diff] [blame] | 454 | If the named curve is not supported then :py:class:`ValueError` is raised. |
| 455 | """ |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 456 | for curve in get_elliptic_curves(): |
| 457 | if curve.name == name: |
| 458 | return curve |
| 459 | raise ValueError("unknown curve name", name) |
| 460 | |
| 461 | |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 462 | class X509Name(object): |
Laurens Van Houtven | 196195b | 2014-06-17 17:06:34 +0200 | [diff] [blame] | 463 | """ |
| 464 | An X.509 Distinguished Name. |
| 465 | |
| 466 | :ivar countryName: The country of the entity. |
| 467 | :ivar C: Alias for :py:attr:`countryName`. |
| 468 | |
| 469 | :ivar stateOrProvinceName: The state or province of the entity. |
| 470 | :ivar ST: Alias for :py:attr:`stateOrProvinceName`. |
| 471 | |
| 472 | :ivar localityName: The locality of the entity. |
| 473 | :ivar L: Alias for :py:attr:`localityName`. |
| 474 | |
| 475 | :ivar organizationName: The organization name of the entity. |
| 476 | :ivar O: Alias for :py:attr:`organizationName`. |
| 477 | |
| 478 | :ivar organizationalUnitName: The organizational unit of the entity. |
| 479 | :ivar OU: Alias for :py:attr:`organizationalUnitName` |
| 480 | |
| 481 | :ivar commonName: The common name of the entity. |
| 482 | :ivar CN: Alias for :py:attr:`commonName`. |
| 483 | |
| 484 | :ivar emailAddress: The e-mail address of the entity. |
| 485 | """ |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 486 | |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 487 | def __init__(self, name): |
| 488 | """ |
| 489 | Create a new X509Name, copying the given X509Name instance. |
| 490 | |
Laurens Van Houtven | 196195b | 2014-06-17 17:06:34 +0200 | [diff] [blame] | 491 | :param name: The name to copy. |
| 492 | :type name: :py:class:`X509Name` |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 493 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 494 | name = _lib.X509_NAME_dup(name._name) |
| 495 | self._name = _ffi.gc(name, _lib.X509_NAME_free) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 496 | |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 497 | def __setattr__(self, name, value): |
| 498 | if name.startswith('_'): |
| 499 | return super(X509Name, self).__setattr__(name, value) |
| 500 | |
Jean-Paul Calderone | ff363be | 2013-03-03 10:21:23 -0800 | [diff] [blame] | 501 | # Note: we really do not want str subclasses here, so we do not use |
| 502 | # isinstance. |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 503 | if type(name) is not str: |
| 504 | raise TypeError("attribute name must be string, not '%.200s'" % ( |
Alex Gaynor | a738ed5 | 2015-09-05 11:17:10 -0400 | [diff] [blame] | 505 | type(value).__name__,)) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 506 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 507 | nid = _lib.OBJ_txt2nid(_byte_string(name)) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 508 | if nid == _lib.NID_undef: |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 509 | try: |
| 510 | _raise_current_error() |
| 511 | except Error: |
| 512 | pass |
| 513 | raise AttributeError("No such attribute") |
| 514 | |
| 515 | # If there's an old entry for this NID, remove it |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 516 | for i in range(_lib.X509_NAME_entry_count(self._name)): |
| 517 | ent = _lib.X509_NAME_get_entry(self._name, i) |
| 518 | ent_obj = _lib.X509_NAME_ENTRY_get_object(ent) |
| 519 | ent_nid = _lib.OBJ_obj2nid(ent_obj) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 520 | if nid == ent_nid: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 521 | ent = _lib.X509_NAME_delete_entry(self._name, i) |
| 522 | _lib.X509_NAME_ENTRY_free(ent) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 523 | break |
| 524 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 525 | if isinstance(value, _text_type): |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 526 | value = value.encode('utf-8') |
| 527 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 528 | add_result = _lib.X509_NAME_add_entry_by_NID( |
| 529 | self._name, nid, _lib.MBSTRING_UTF8, value, -1, -1, 0) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 530 | if not add_result: |
Jean-Paul Calderone | 5300d6a | 2013-12-29 16:36:50 -0500 | [diff] [blame] | 531 | _raise_current_error() |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 532 | |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 533 | def __getattr__(self, name): |
| 534 | """ |
| 535 | Find attribute. An X509Name object has the following attributes: |
| 536 | countryName (alias C), stateOrProvince (alias ST), locality (alias L), |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 537 | organization (alias O), organizationalUnit (alias OU), commonName |
| 538 | (alias CN) and more... |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 539 | """ |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 540 | nid = _lib.OBJ_txt2nid(_byte_string(name)) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 541 | if nid == _lib.NID_undef: |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 542 | # This is a bit weird. OBJ_txt2nid indicated failure, but it seems |
| 543 | # a lower level function, a2d_ASN1_OBJECT, also feels the need to |
| 544 | # push something onto the error queue. If we don't clean that up |
| 545 | # now, someone else will bump into it later and be quite confused. |
| 546 | # See lp#314814. |
| 547 | try: |
| 548 | _raise_current_error() |
| 549 | except Error: |
| 550 | pass |
| 551 | return super(X509Name, self).__getattr__(name) |
| 552 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 553 | entry_index = _lib.X509_NAME_get_index_by_NID(self._name, nid, -1) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 554 | if entry_index == -1: |
| 555 | return None |
| 556 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 557 | entry = _lib.X509_NAME_get_entry(self._name, entry_index) |
| 558 | data = _lib.X509_NAME_ENTRY_get_data(entry) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 559 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 560 | result_buffer = _ffi.new("unsigned char**") |
| 561 | data_length = _lib.ASN1_STRING_to_UTF8(result_buffer, data) |
Alex Gaynor | 09a386e | 2016-07-03 09:32:44 -0400 | [diff] [blame] | 562 | _openssl_assert(data_length >= 0) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 563 | |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 564 | try: |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 565 | result = _ffi.buffer( |
| 566 | result_buffer[0], data_length |
| 567 | )[:].decode('utf-8') |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 568 | finally: |
| 569 | # XXX untested |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 570 | _lib.OPENSSL_free(result_buffer[0]) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 571 | return result |
| 572 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 573 | def _cmp(op): |
| 574 | def f(self, other): |
| 575 | if not isinstance(other, X509Name): |
| 576 | return NotImplemented |
| 577 | result = _lib.X509_NAME_cmp(self._name, other._name) |
| 578 | return op(result, 0) |
| 579 | return f |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 580 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 581 | __eq__ = _cmp(__eq__) |
| 582 | __ne__ = _cmp(__ne__) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 583 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 584 | __lt__ = _cmp(__lt__) |
| 585 | __le__ = _cmp(__le__) |
| 586 | |
| 587 | __gt__ = _cmp(__gt__) |
| 588 | __ge__ = _cmp(__ge__) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 589 | |
| 590 | def __repr__(self): |
| 591 | """ |
| 592 | String representation of an X509Name |
| 593 | """ |
Alex Gaynor | 962ac21 | 2015-09-04 08:06:42 -0400 | [diff] [blame] | 594 | result_buffer = _ffi.new("char[]", 512) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 595 | format_result = _lib.X509_NAME_oneline( |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 596 | self._name, result_buffer, len(result_buffer)) |
Alex Gaynor | fb8a2a1 | 2016-06-04 18:26:26 -0700 | [diff] [blame] | 597 | _openssl_assert(format_result != _ffi.NULL) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 598 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 599 | return "<X509Name object '%s'>" % ( |
| 600 | _native(_ffi.string(result_buffer)),) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 601 | |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 602 | def hash(self): |
| 603 | """ |
Laurens Van Houtven | 196195b | 2014-06-17 17:06:34 +0200 | [diff] [blame] | 604 | Return an integer representation of the first four bytes of the |
| 605 | MD5 digest of the DER representation of the name. |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 606 | |
Laurens Van Houtven | 196195b | 2014-06-17 17:06:34 +0200 | [diff] [blame] | 607 | This is the Python equivalent of OpenSSL's ``X509_NAME_hash``. |
| 608 | |
| 609 | :return: The (integer) hash of this name. |
| 610 | :rtype: :py:class:`int` |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 611 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 612 | return _lib.X509_NAME_hash(self._name) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 613 | |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 614 | def der(self): |
| 615 | """ |
Laurens Van Houtven | 196195b | 2014-06-17 17:06:34 +0200 | [diff] [blame] | 616 | Return the DER encoding of this name. |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 617 | |
Laurens Van Houtven | 196195b | 2014-06-17 17:06:34 +0200 | [diff] [blame] | 618 | :return: The DER encoded form of this name. |
| 619 | :rtype: :py:class:`bytes` |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 620 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 621 | result_buffer = _ffi.new('unsigned char**') |
| 622 | encode_result = _lib.i2d_X509_NAME(self._name, result_buffer) |
Alex Gaynor | 09a386e | 2016-07-03 09:32:44 -0400 | [diff] [blame] | 623 | _openssl_assert(encode_result >= 0) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 624 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 625 | string_result = _ffi.buffer(result_buffer[0], encode_result)[:] |
| 626 | _lib.OPENSSL_free(result_buffer[0]) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 627 | return string_result |
| 628 | |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 629 | def get_components(self): |
| 630 | """ |
Laurens Van Houtven | 196195b | 2014-06-17 17:06:34 +0200 | [diff] [blame] | 631 | Returns the components of this name, as a sequence of 2-tuples. |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 632 | |
Laurens Van Houtven | 196195b | 2014-06-17 17:06:34 +0200 | [diff] [blame] | 633 | :return: The components of this name. |
| 634 | :rtype: :py:class:`list` of ``name, value`` tuples. |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 635 | """ |
| 636 | result = [] |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 637 | for i in range(_lib.X509_NAME_entry_count(self._name)): |
| 638 | ent = _lib.X509_NAME_get_entry(self._name, i) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 639 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 640 | fname = _lib.X509_NAME_ENTRY_get_object(ent) |
| 641 | fval = _lib.X509_NAME_ENTRY_get_data(ent) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 642 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 643 | nid = _lib.OBJ_obj2nid(fname) |
| 644 | name = _lib.OBJ_nid2sn(nid) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 645 | |
| 646 | result.append(( |
Alex Gaynor | a738ed5 | 2015-09-05 11:17:10 -0400 | [diff] [blame] | 647 | _ffi.string(name), |
| 648 | _ffi.string( |
| 649 | _lib.ASN1_STRING_data(fval), |
| 650 | _lib.ASN1_STRING_length(fval)))) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 651 | |
| 652 | return result |
Laurens Van Houtven | 2650de5 | 2014-06-18 13:47:47 +0200 | [diff] [blame] | 653 | |
| 654 | |
Alex Gaynor | 10d3083 | 2017-06-29 15:31:39 -0700 | [diff] [blame] | 655 | X509NameType = deprecated( |
| 656 | X509Name, __name__, |
| 657 | "X509NameType has been deprecated, use X509Name instead", |
| 658 | DeprecationWarning |
| 659 | ) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 660 | |
| 661 | |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 662 | class X509Extension(object): |
Laurens Van Houtven | 2650de5 | 2014-06-18 13:47:47 +0200 | [diff] [blame] | 663 | """ |
| 664 | An X.509 v3 certificate extension. |
| 665 | """ |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 666 | |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 667 | def __init__(self, type_name, critical, value, subject=None, issuer=None): |
| 668 | """ |
Laurens Van Houtven | 2650de5 | 2014-06-18 13:47:47 +0200 | [diff] [blame] | 669 | Initializes an X509 extension. |
| 670 | |
Hynek Schlawack | 8d4f976 | 2016-03-19 08:15:03 +0100 | [diff] [blame] | 671 | :param type_name: The name of the type of extension_ to create. |
Alex Gaynor | 6f71991 | 2015-09-20 09:21:29 -0400 | [diff] [blame] | 672 | :type type_name: :py:data:`bytes` |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 673 | |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 674 | :param bool critical: A flag indicating whether this is a critical |
| 675 | extension. |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 676 | |
| 677 | :param value: The value of the extension. |
Maximilian Hils | 0de4375 | 2015-09-18 15:26:54 +0200 | [diff] [blame] | 678 | :type value: :py:data:`bytes` |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 679 | |
Laurens Van Houtven | 2650de5 | 2014-06-18 13:47:47 +0200 | [diff] [blame] | 680 | :param subject: Optional X509 certificate to use as subject. |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 681 | :type subject: :py:class:`X509` |
| 682 | |
Laurens Van Houtven | 2650de5 | 2014-06-18 13:47:47 +0200 | [diff] [blame] | 683 | :param issuer: Optional X509 certificate to use as issuer. |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 684 | :type issuer: :py:class:`X509` |
Hynek Schlawack | 8d4f976 | 2016-03-19 08:15:03 +0100 | [diff] [blame] | 685 | |
Alex Chan | 54005ce | 2017-03-21 08:08:17 +0000 | [diff] [blame] | 686 | .. _extension: https://www.openssl.org/docs/manmaster/man5/ |
Hynek Schlawack | 8d4f976 | 2016-03-19 08:15:03 +0100 | [diff] [blame] | 687 | x509v3_config.html#STANDARD-EXTENSIONS |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 688 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 689 | ctx = _ffi.new("X509V3_CTX*") |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 690 | |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 691 | # A context is necessary for any extension which uses the r2i |
| 692 | # conversion method. That is, X509V3_EXT_nconf may segfault if passed |
| 693 | # a NULL ctx. Start off by initializing most of the fields to NULL. |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 694 | _lib.X509V3_set_ctx(ctx, _ffi.NULL, _ffi.NULL, _ffi.NULL, _ffi.NULL, 0) |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 695 | |
| 696 | # We have no configuration database - but perhaps we should (some |
| 697 | # extensions may require it). |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 698 | _lib.X509V3_set_ctx_nodb(ctx) |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 699 | |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 700 | # Initialize the subject and issuer, if appropriate. ctx is a local, |
| 701 | # and as far as I can tell none of the X509V3_* APIs invoked here steal |
Alex Gaynor | a738ed5 | 2015-09-05 11:17:10 -0400 | [diff] [blame] | 702 | # any references, so no need to mess with reference counts or |
| 703 | # duplicates. |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 704 | if issuer is not None: |
| 705 | if not isinstance(issuer, X509): |
| 706 | raise TypeError("issuer must be an X509 instance") |
| 707 | ctx.issuer_cert = issuer._x509 |
| 708 | if subject is not None: |
| 709 | if not isinstance(subject, X509): |
| 710 | raise TypeError("subject must be an X509 instance") |
| 711 | ctx.subject_cert = subject._x509 |
| 712 | |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 713 | if critical: |
| 714 | # There are other OpenSSL APIs which would let us pass in critical |
| 715 | # separately, but they're harder to use, and since value is already |
| 716 | # a pile of crappy junk smuggling a ton of utterly important |
| 717 | # structured data, what's the point of trying to avoid nasty stuff |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 718 | # with strings? (However, X509V3_EXT_i2d in particular seems like |
| 719 | # it would be a better API to invoke. I do not know where to get |
| 720 | # the ext_struc it desires for its last parameter, though.) |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 721 | value = b"critical," + value |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 722 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 723 | extension = _lib.X509V3_EXT_nconf(_ffi.NULL, ctx, type_name, value) |
| 724 | if extension == _ffi.NULL: |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 725 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 726 | self._extension = _ffi.gc(extension, _lib.X509_EXTENSION_free) |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 727 | |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 728 | @property |
| 729 | def _nid(self): |
Paul Kehrer | e8f91cc | 2016-03-09 21:26:29 -0400 | [diff] [blame] | 730 | return _lib.OBJ_obj2nid( |
| 731 | _lib.X509_EXTENSION_get_object(self._extension) |
| 732 | ) |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 733 | |
| 734 | _prefixes = { |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 735 | _lib.GEN_EMAIL: "email", |
| 736 | _lib.GEN_DNS: "DNS", |
| 737 | _lib.GEN_URI: "URI", |
Alex Gaynor | a738ed5 | 2015-09-05 11:17:10 -0400 | [diff] [blame] | 738 | } |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 739 | |
| 740 | def _subjectAltNameString(self): |
Alex Gaynor | d61c46a | 2017-06-29 22:51:33 -0700 | [diff] [blame] | 741 | names = _ffi.cast( |
| 742 | "GENERAL_NAMES*", _lib.X509V3_EXT_d2i(self._extension) |
| 743 | ) |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 744 | |
Paul Kehrer | b7d7950 | 2015-05-04 07:43:51 -0500 | [diff] [blame] | 745 | names = _ffi.gc(names, _lib.GENERAL_NAMES_free) |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 746 | parts = [] |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 747 | for i in range(_lib.sk_GENERAL_NAME_num(names)): |
| 748 | name = _lib.sk_GENERAL_NAME_value(names, i) |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 749 | try: |
| 750 | label = self._prefixes[name.type] |
| 751 | except KeyError: |
| 752 | bio = _new_mem_buf() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 753 | _lib.GENERAL_NAME_print(bio, name) |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 754 | parts.append(_native(_bio_to_string(bio))) |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 755 | else: |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 756 | value = _native( |
| 757 | _ffi.buffer(name.d.ia5.data, name.d.ia5.length)[:]) |
| 758 | parts.append(label + ":" + value) |
| 759 | return ", ".join(parts) |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 760 | |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 761 | def __str__(self): |
| 762 | """ |
| 763 | :return: a nice text representation of the extension |
| 764 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 765 | if _lib.NID_subject_alt_name == self._nid: |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 766 | return self._subjectAltNameString() |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 767 | |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 768 | bio = _new_mem_buf() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 769 | print_result = _lib.X509V3_EXT_print(bio, self._extension, 0, 0) |
Alex Gaynor | 09a386e | 2016-07-03 09:32:44 -0400 | [diff] [blame] | 770 | _openssl_assert(print_result != 0) |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 771 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 772 | return _native(_bio_to_string(bio)) |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 773 | |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 774 | def get_critical(self): |
| 775 | """ |
Laurens Van Houtven | 2650de5 | 2014-06-18 13:47:47 +0200 | [diff] [blame] | 776 | Returns the critical field of this X.509 extension. |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 777 | |
| 778 | :return: The critical field. |
| 779 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 780 | return _lib.X509_EXTENSION_get_critical(self._extension) |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 781 | |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 782 | def get_short_name(self): |
| 783 | """ |
Laurens Van Houtven | 2650de5 | 2014-06-18 13:47:47 +0200 | [diff] [blame] | 784 | Returns the short type name of this X.509 extension. |
| 785 | |
| 786 | The result is a byte string such as :py:const:`b"basicConstraints"`. |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 787 | |
| 788 | :return: The short type name. |
Laurens Van Houtven | 2650de5 | 2014-06-18 13:47:47 +0200 | [diff] [blame] | 789 | :rtype: :py:data:`bytes` |
| 790 | |
| 791 | .. versionadded:: 0.12 |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 792 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 793 | obj = _lib.X509_EXTENSION_get_object(self._extension) |
| 794 | nid = _lib.OBJ_obj2nid(obj) |
| 795 | return _ffi.string(_lib.OBJ_nid2sn(nid)) |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 796 | |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 797 | def get_data(self): |
| 798 | """ |
Laurens Van Houtven | 2650de5 | 2014-06-18 13:47:47 +0200 | [diff] [blame] | 799 | Returns the data of the X509 extension, encoded as ASN.1. |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 800 | |
Laurens Van Houtven | 2650de5 | 2014-06-18 13:47:47 +0200 | [diff] [blame] | 801 | :return: The ASN.1 encoded data of this X509 extension. |
| 802 | :rtype: :py:data:`bytes` |
| 803 | |
| 804 | .. versionadded:: 0.12 |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 805 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 806 | octet_result = _lib.X509_EXTENSION_get_data(self._extension) |
| 807 | string_result = _ffi.cast('ASN1_STRING*', octet_result) |
| 808 | char_result = _lib.ASN1_STRING_data(string_result) |
| 809 | result_length = _lib.ASN1_STRING_length(string_result) |
| 810 | return _ffi.buffer(char_result, result_length)[:] |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 811 | |
Laurens Van Houtven | 2650de5 | 2014-06-18 13:47:47 +0200 | [diff] [blame] | 812 | |
Alex Gaynor | 10d3083 | 2017-06-29 15:31:39 -0700 | [diff] [blame] | 813 | X509ExtensionType = deprecated( |
| 814 | X509Extension, __name__, |
| 815 | "X509ExtensionType has been deprecated, use X509Extension instead", |
| 816 | DeprecationWarning |
| 817 | ) |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 818 | |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 819 | |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 820 | class X509Req(object): |
Laurens Van Houtven | 3e83d24 | 2014-06-18 14:29:47 +0200 | [diff] [blame] | 821 | """ |
| 822 | An X.509 certificate signing requests. |
| 823 | """ |
Alex Gaynor | a738ed5 | 2015-09-05 11:17:10 -0400 | [diff] [blame] | 824 | |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 825 | def __init__(self): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 826 | req = _lib.X509_REQ_new() |
| 827 | self._req = _ffi.gc(req, _lib.X509_REQ_free) |
Alex Gaynor | 5af32d0 | 2016-09-24 01:52:21 -0400 | [diff] [blame] | 828 | # Default to version 0. |
| 829 | self.set_version(0) |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 830 | |
Paul Kehrer | 41c1024 | 2017-06-29 18:24:17 -0500 | [diff] [blame] | 831 | def to_cryptography(self): |
| 832 | """ |
| 833 | Export as a ``cryptography`` certificate signing request. |
| 834 | |
| 835 | :rtype: ``cryptography.x509.CertificateSigningRequest`` |
| 836 | |
| 837 | .. versionadded:: 17.1.0 |
| 838 | """ |
| 839 | from cryptography.hazmat.backends.openssl.x509 import ( |
| 840 | _CertificateSigningRequest |
| 841 | ) |
| 842 | backend = _get_backend() |
| 843 | return _CertificateSigningRequest(backend, self._req) |
| 844 | |
| 845 | @classmethod |
| 846 | def from_cryptography(cls, crypto_req): |
| 847 | """ |
| 848 | Construct based on a ``cryptography`` *crypto_req*. |
| 849 | |
| 850 | :param crypto_req: A ``cryptography`` X.509 certificate signing request |
| 851 | :type crypto_req: ``cryptography.x509.CertificateSigningRequest`` |
| 852 | |
| 853 | :rtype: PKey |
| 854 | |
| 855 | .. versionadded:: 17.1.0 |
| 856 | """ |
| 857 | if not isinstance(crypto_req, x509.CertificateSigningRequest): |
| 858 | raise TypeError("Must be a certificate signing request") |
| 859 | |
| 860 | req = cls() |
| 861 | req._req = crypto_req._x509_req |
| 862 | return req |
| 863 | |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 864 | def set_pubkey(self, pkey): |
| 865 | """ |
Laurens Van Houtven | 3e83d24 | 2014-06-18 14:29:47 +0200 | [diff] [blame] | 866 | Set the public key of the certificate signing request. |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 867 | |
Laurens Van Houtven | 3e83d24 | 2014-06-18 14:29:47 +0200 | [diff] [blame] | 868 | :param pkey: The public key to use. |
| 869 | :type pkey: :py:class:`PKey` |
| 870 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 871 | :return: ``None`` |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 872 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 873 | set_result = _lib.X509_REQ_set_pubkey(self._req, pkey._pkey) |
Alex Gaynor | 09a386e | 2016-07-03 09:32:44 -0400 | [diff] [blame] | 874 | _openssl_assert(set_result == 1) |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 875 | |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 876 | def get_pubkey(self): |
| 877 | """ |
Laurens Van Houtven | 3e83d24 | 2014-06-18 14:29:47 +0200 | [diff] [blame] | 878 | Get the public key of the certificate signing request. |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 879 | |
Laurens Van Houtven | 3e83d24 | 2014-06-18 14:29:47 +0200 | [diff] [blame] | 880 | :return: The public key. |
| 881 | :rtype: :py:class:`PKey` |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 882 | """ |
| 883 | pkey = PKey.__new__(PKey) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 884 | pkey._pkey = _lib.X509_REQ_get_pubkey(self._req) |
Alex Gaynor | fb8a2a1 | 2016-06-04 18:26:26 -0700 | [diff] [blame] | 885 | _openssl_assert(pkey._pkey != _ffi.NULL) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 886 | pkey._pkey = _ffi.gc(pkey._pkey, _lib.EVP_PKEY_free) |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 887 | pkey._only_public = True |
| 888 | return pkey |
| 889 | |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 890 | def set_version(self, version): |
| 891 | """ |
| 892 | Set the version subfield (RFC 2459, section 4.1.2.1) of the certificate |
| 893 | request. |
| 894 | |
Laurens Van Houtven | 3e83d24 | 2014-06-18 14:29:47 +0200 | [diff] [blame] | 895 | :param int version: The version number. |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 896 | :return: ``None`` |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 897 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 898 | set_result = _lib.X509_REQ_set_version(self._req, version) |
Alex Gaynor | 5bb2bd1 | 2016-07-03 10:48:32 -0400 | [diff] [blame] | 899 | _openssl_assert(set_result == 1) |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 900 | |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 901 | def get_version(self): |
| 902 | """ |
| 903 | Get the version subfield (RFC 2459, section 4.1.2.1) of the certificate |
| 904 | request. |
| 905 | |
Laurens Van Houtven | 3e83d24 | 2014-06-18 14:29:47 +0200 | [diff] [blame] | 906 | :return: The value of the version subfield. |
| 907 | :rtype: :py:class:`int` |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 908 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 909 | return _lib.X509_REQ_get_version(self._req) |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 910 | |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 911 | def get_subject(self): |
| 912 | """ |
Laurens Van Houtven | 3e83d24 | 2014-06-18 14:29:47 +0200 | [diff] [blame] | 913 | Return the subject of this certificate signing request. |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 914 | |
Cory Benfield | 881dc8d | 2015-12-09 08:25:14 +0000 | [diff] [blame] | 915 | This creates a new :class:`X509Name` that wraps the underlying subject |
| 916 | name field on the certificate signing request. Modifying it will modify |
| 917 | the underlying signing request, and will have the effect of modifying |
| 918 | any other :class:`X509Name` that refers to this subject. |
Laurens Van Houtven | 3e83d24 | 2014-06-18 14:29:47 +0200 | [diff] [blame] | 919 | |
| 920 | :return: The subject of this certificate signing request. |
Cory Benfield | 881dc8d | 2015-12-09 08:25:14 +0000 | [diff] [blame] | 921 | :rtype: :class:`X509Name` |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 922 | """ |
| 923 | name = X509Name.__new__(X509Name) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 924 | name._name = _lib.X509_REQ_get_subject_name(self._req) |
Alex Gaynor | fb8a2a1 | 2016-06-04 18:26:26 -0700 | [diff] [blame] | 925 | _openssl_assert(name._name != _ffi.NULL) |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 926 | |
| 927 | # The name is owned by the X509Req structure. As long as the X509Name |
| 928 | # Python object is alive, keep the X509Req Python object alive. |
| 929 | name._owner = self |
| 930 | |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 931 | return name |
| 932 | |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 933 | def add_extensions(self, extensions): |
| 934 | """ |
Laurens Van Houtven | 3e83d24 | 2014-06-18 14:29:47 +0200 | [diff] [blame] | 935 | Add extensions to the certificate signing request. |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 936 | |
Laurens Van Houtven | 3e83d24 | 2014-06-18 14:29:47 +0200 | [diff] [blame] | 937 | :param extensions: The X.509 extensions to add. |
| 938 | :type extensions: iterable of :py:class:`X509Extension` |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 939 | :return: ``None`` |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 940 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 941 | stack = _lib.sk_X509_EXTENSION_new_null() |
Alex Gaynor | fb8a2a1 | 2016-06-04 18:26:26 -0700 | [diff] [blame] | 942 | _openssl_assert(stack != _ffi.NULL) |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 943 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 944 | stack = _ffi.gc(stack, _lib.sk_X509_EXTENSION_free) |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 945 | |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 946 | for ext in extensions: |
| 947 | if not isinstance(ext, X509Extension): |
Jean-Paul Calderone | c2154b7 | 2013-02-20 14:29:37 -0800 | [diff] [blame] | 948 | raise ValueError("One of the elements is not an X509Extension") |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 949 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 950 | # TODO push can fail (here and elsewhere) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 951 | _lib.sk_X509_EXTENSION_push(stack, ext._extension) |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 952 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 953 | add_result = _lib.X509_REQ_add_extensions(self._req, stack) |
Alex Gaynor | 09a386e | 2016-07-03 09:32:44 -0400 | [diff] [blame] | 954 | _openssl_assert(add_result == 1) |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 955 | |
Stephen Holsapple | adfd39d | 2014-01-28 17:58:31 -0800 | [diff] [blame] | 956 | def get_extensions(self): |
Stephen Holsapple | 7fbdf64 | 2014-03-01 20:05:47 -0800 | [diff] [blame] | 957 | """ |
Laurens Van Houtven | 3e83d24 | 2014-06-18 14:29:47 +0200 | [diff] [blame] | 958 | Get X.509 extensions in the certificate signing request. |
Stephen Holsapple | adfd39d | 2014-01-28 17:58:31 -0800 | [diff] [blame] | 959 | |
Laurens Van Houtven | 3e83d24 | 2014-06-18 14:29:47 +0200 | [diff] [blame] | 960 | :return: The X.509 extensions in this request. |
| 961 | :rtype: :py:class:`list` of :py:class:`X509Extension` objects. |
| 962 | |
| 963 | .. versionadded:: 0.15 |
Stephen Holsapple | 7fbdf64 | 2014-03-01 20:05:47 -0800 | [diff] [blame] | 964 | """ |
| 965 | exts = [] |
Jean-Paul Calderone | 9479d73 | 2014-03-02 08:04:54 -0500 | [diff] [blame] | 966 | native_exts_obj = _lib.X509_REQ_get_extensions(self._req) |
Jean-Paul Calderone | b7a79b4 | 2014-03-02 08:06:47 -0500 | [diff] [blame] | 967 | for i in range(_lib.sk_X509_EXTENSION_num(native_exts_obj)): |
Stephen Holsapple | 7fbdf64 | 2014-03-01 20:05:47 -0800 | [diff] [blame] | 968 | ext = X509Extension.__new__(X509Extension) |
Jean-Paul Calderone | 9479d73 | 2014-03-02 08:04:54 -0500 | [diff] [blame] | 969 | ext._extension = _lib.sk_X509_EXTENSION_value(native_exts_obj, i) |
Stephen Holsapple | 7fbdf64 | 2014-03-01 20:05:47 -0800 | [diff] [blame] | 970 | exts.append(ext) |
| 971 | return exts |
Stephen Holsapple | adfd39d | 2014-01-28 17:58:31 -0800 | [diff] [blame] | 972 | |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 973 | def sign(self, pkey, digest): |
| 974 | """ |
Laurens Van Houtven | 6f2e426 | 2015-04-23 10:48:32 -0700 | [diff] [blame] | 975 | Sign the certificate signing request with this key and digest type. |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 976 | |
Laurens Van Houtven | 3e83d24 | 2014-06-18 14:29:47 +0200 | [diff] [blame] | 977 | :param pkey: The key pair to sign with. |
| 978 | :type pkey: :py:class:`PKey` |
| 979 | :param digest: The name of the message digest to use for the signature, |
Alex Gaynor | 239e2d3 | 2016-09-11 12:36:35 -0400 | [diff] [blame] | 980 | e.g. :py:data:`b"sha256"`. |
Laurens Van Houtven | 3e83d24 | 2014-06-18 14:29:47 +0200 | [diff] [blame] | 981 | :type digest: :py:class:`bytes` |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 982 | :return: ``None`` |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 983 | """ |
| 984 | if pkey._only_public: |
| 985 | raise ValueError("Key has only public part") |
| 986 | |
| 987 | if not pkey._initialized: |
| 988 | raise ValueError("Key is uninitialized") |
| 989 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 990 | digest_obj = _lib.EVP_get_digestbyname(_byte_string(digest)) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 991 | if digest_obj == _ffi.NULL: |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 992 | raise ValueError("No such digest method") |
| 993 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 994 | sign_result = _lib.X509_REQ_sign(self._req, pkey._pkey, digest_obj) |
Alex Gaynor | 09a386e | 2016-07-03 09:32:44 -0400 | [diff] [blame] | 995 | _openssl_assert(sign_result > 0) |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 996 | |
Jean-Paul Calderone | 5565f0f | 2013-03-06 11:10:20 -0800 | [diff] [blame] | 997 | def verify(self, pkey): |
| 998 | """ |
Laurens Van Houtven | 3e83d24 | 2014-06-18 14:29:47 +0200 | [diff] [blame] | 999 | Verifies the signature on this certificate signing request. |
Jean-Paul Calderone | 5565f0f | 2013-03-06 11:10:20 -0800 | [diff] [blame] | 1000 | |
Hynek Schlawack | 01c3167 | 2016-12-11 15:14:09 +0100 | [diff] [blame] | 1001 | :param PKey key: A public key. |
| 1002 | |
| 1003 | :return: ``True`` if the signature is correct. |
| 1004 | :rtype: bool |
| 1005 | |
| 1006 | :raises OpenSSL.crypto.Error: If the signature is invalid or there is a |
Jean-Paul Calderone | 5565f0f | 2013-03-06 11:10:20 -0800 | [diff] [blame] | 1007 | problem verifying the signature. |
| 1008 | """ |
| 1009 | if not isinstance(pkey, PKey): |
| 1010 | raise TypeError("pkey must be a PKey instance") |
| 1011 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1012 | result = _lib.X509_REQ_verify(self._req, pkey._pkey) |
Jean-Paul Calderone | 5565f0f | 2013-03-06 11:10:20 -0800 | [diff] [blame] | 1013 | if result <= 0: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 1014 | _raise_current_error() |
Jean-Paul Calderone | 5565f0f | 2013-03-06 11:10:20 -0800 | [diff] [blame] | 1015 | |
| 1016 | return result |
| 1017 | |
| 1018 | |
Alex Gaynor | 10d3083 | 2017-06-29 15:31:39 -0700 | [diff] [blame] | 1019 | X509ReqType = deprecated( |
| 1020 | X509Req, __name__, |
| 1021 | "X509ReqType has been deprecated, use X509Req instead", |
| 1022 | DeprecationWarning |
| 1023 | ) |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 1024 | |
| 1025 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1026 | class X509(object): |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1027 | """ |
| 1028 | An X.509 certificate. |
| 1029 | """ |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1030 | def __init__(self): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1031 | x509 = _lib.X509_new() |
Hynek Schlawack | 8a2dd77 | 2016-07-31 13:46:20 +0200 | [diff] [blame] | 1032 | _openssl_assert(x509 != _ffi.NULL) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1033 | self._x509 = _ffi.gc(x509, _lib.X509_free) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1034 | |
Alex Gaynor | 9939ba1 | 2017-06-25 16:28:24 -0400 | [diff] [blame] | 1035 | def to_cryptography(self): |
| 1036 | """ |
| 1037 | Export as a ``cryptography`` certificate. |
| 1038 | |
| 1039 | :rtype: ``cryptography.x509.Certificate`` |
| 1040 | |
| 1041 | .. versionadded:: 17.1.0 |
| 1042 | """ |
| 1043 | from cryptography.hazmat.backends.openssl.x509 import _Certificate |
| 1044 | backend = _get_backend() |
| 1045 | return _Certificate(backend, self._x509) |
| 1046 | |
| 1047 | @classmethod |
| 1048 | def from_cryptography(cls, crypto_cert): |
| 1049 | """ |
| 1050 | Construct based on a ``cryptography`` *crypto_cert*. |
| 1051 | |
| 1052 | :param crypto_key: A ``cryptography`` X.509 certificate. |
| 1053 | :type crypto_key: ``cryptography.x509.Certificate`` |
| 1054 | |
| 1055 | :rtype: PKey |
| 1056 | |
| 1057 | .. versionadded:: 17.1.0 |
| 1058 | """ |
| 1059 | if not isinstance(crypto_cert, x509.Certificate): |
| 1060 | raise TypeError("Must be a certificate") |
| 1061 | |
| 1062 | cert = cls() |
| 1063 | cert._x509 = crypto_cert._x509 |
| 1064 | return cert |
| 1065 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1066 | def set_version(self, version): |
| 1067 | """ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1068 | Set the version number of the certificate. |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1069 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1070 | :param version: The version number of the certificate. |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1071 | :type version: :py:class:`int` |
| 1072 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1073 | :return: ``None`` |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1074 | """ |
| 1075 | if not isinstance(version, int): |
| 1076 | raise TypeError("version must be an integer") |
| 1077 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1078 | _lib.X509_set_version(self._x509, version) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1079 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1080 | def get_version(self): |
| 1081 | """ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1082 | Return the version number of the certificate. |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1083 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1084 | :return: The version number of the certificate. |
| 1085 | :rtype: :py:class:`int` |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1086 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1087 | return _lib.X509_get_version(self._x509) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1088 | |
Jean-Paul Calderone | edafced | 2013-02-19 11:48:38 -0800 | [diff] [blame] | 1089 | def get_pubkey(self): |
| 1090 | """ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1091 | Get the public key of the certificate. |
Jean-Paul Calderone | edafced | 2013-02-19 11:48:38 -0800 | [diff] [blame] | 1092 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1093 | :return: The public key. |
| 1094 | :rtype: :py:class:`PKey` |
Jean-Paul Calderone | edafced | 2013-02-19 11:48:38 -0800 | [diff] [blame] | 1095 | """ |
| 1096 | pkey = PKey.__new__(PKey) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1097 | pkey._pkey = _lib.X509_get_pubkey(self._x509) |
| 1098 | if pkey._pkey == _ffi.NULL: |
Jean-Paul Calderone | edafced | 2013-02-19 11:48:38 -0800 | [diff] [blame] | 1099 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1100 | pkey._pkey = _ffi.gc(pkey._pkey, _lib.EVP_PKEY_free) |
Jean-Paul Calderone | edafced | 2013-02-19 11:48:38 -0800 | [diff] [blame] | 1101 | pkey._only_public = True |
| 1102 | return pkey |
| 1103 | |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 1104 | def set_pubkey(self, pkey): |
| 1105 | """ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1106 | Set the public key of the certificate. |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 1107 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1108 | :param pkey: The public key. |
| 1109 | :type pkey: :py:class:`PKey` |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 1110 | |
Laurens Van Houtven | 33fcf12 | 2015-04-23 10:50:08 -0700 | [diff] [blame] | 1111 | :return: :py:data:`None` |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 1112 | """ |
| 1113 | if not isinstance(pkey, PKey): |
| 1114 | raise TypeError("pkey must be a PKey instance") |
| 1115 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1116 | set_result = _lib.X509_set_pubkey(self._x509, pkey._pkey) |
Alex Gaynor | 7778e79 | 2016-07-03 23:38:48 -0400 | [diff] [blame] | 1117 | _openssl_assert(set_result == 1) |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 1118 | |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 1119 | def sign(self, pkey, digest): |
| 1120 | """ |
Laurens Van Houtven | 6f2e426 | 2015-04-23 10:48:32 -0700 | [diff] [blame] | 1121 | Sign the certificate with this key and digest type. |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 1122 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1123 | :param pkey: The key to sign with. |
| 1124 | :type pkey: :py:class:`PKey` |
| 1125 | |
| 1126 | :param digest: The name of the message digest to use. |
| 1127 | :type digest: :py:class:`bytes` |
| 1128 | |
Laurens Van Houtven | a367fe8 | 2015-04-23 10:49:12 -0700 | [diff] [blame] | 1129 | :return: :py:data:`None` |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 1130 | """ |
| 1131 | if not isinstance(pkey, PKey): |
| 1132 | raise TypeError("pkey must be a PKey instance") |
| 1133 | |
Jean-Paul Calderone | edafced | 2013-02-19 11:48:38 -0800 | [diff] [blame] | 1134 | if pkey._only_public: |
| 1135 | raise ValueError("Key only has public part") |
| 1136 | |
Jean-Paul Calderone | 09e3bdc | 2013-02-19 12:15:28 -0800 | [diff] [blame] | 1137 | if not pkey._initialized: |
| 1138 | raise ValueError("Key is uninitialized") |
| 1139 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1140 | evp_md = _lib.EVP_get_digestbyname(_byte_string(digest)) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1141 | if evp_md == _ffi.NULL: |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 1142 | raise ValueError("No such digest method") |
| 1143 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1144 | sign_result = _lib.X509_sign(self._x509, pkey._pkey, evp_md) |
Alex Gaynor | 5bb2bd1 | 2016-07-03 10:48:32 -0400 | [diff] [blame] | 1145 | _openssl_assert(sign_result > 0) |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 1146 | |
Jean-Paul Calderone | e4aa3fa | 2013-02-19 12:12:53 -0800 | [diff] [blame] | 1147 | def get_signature_algorithm(self): |
| 1148 | """ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1149 | Return the signature algorithm used in the certificate. |
Jean-Paul Calderone | e4aa3fa | 2013-02-19 12:12:53 -0800 | [diff] [blame] | 1150 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1151 | :return: The name of the algorithm. |
| 1152 | :rtype: :py:class:`bytes` |
| 1153 | |
| 1154 | :raises ValueError: If the signature algorithm is undefined. |
| 1155 | |
Laurens Van Houtven | 0dd8740 | 2015-04-23 10:47:18 -0700 | [diff] [blame] | 1156 | .. versionadded:: 0.13 |
Jean-Paul Calderone | e4aa3fa | 2013-02-19 12:12:53 -0800 | [diff] [blame] | 1157 | """ |
Alex Gaynor | 39ea531 | 2016-06-02 09:12:10 -0700 | [diff] [blame] | 1158 | algor = _lib.X509_get0_tbs_sigalg(self._x509) |
| 1159 | nid = _lib.OBJ_obj2nid(algor.algorithm) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1160 | if nid == _lib.NID_undef: |
Jean-Paul Calderone | e4aa3fa | 2013-02-19 12:12:53 -0800 | [diff] [blame] | 1161 | raise ValueError("Undefined signature algorithm") |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1162 | return _ffi.string(_lib.OBJ_nid2ln(nid)) |
Jean-Paul Calderone | e4aa3fa | 2013-02-19 12:12:53 -0800 | [diff] [blame] | 1163 | |
Jean-Paul Calderone | b407872 | 2013-02-19 12:01:55 -0800 | [diff] [blame] | 1164 | def digest(self, digest_name): |
| 1165 | """ |
| 1166 | Return the digest of the X509 object. |
| 1167 | |
| 1168 | :param digest_name: The name of the digest algorithm to use. |
| 1169 | :type digest_name: :py:class:`bytes` |
| 1170 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1171 | :return: The digest of the object, formatted as |
| 1172 | :py:const:`b":"`-delimited hex pairs. |
| 1173 | :rtype: :py:class:`bytes` |
Jean-Paul Calderone | b407872 | 2013-02-19 12:01:55 -0800 | [diff] [blame] | 1174 | """ |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1175 | digest = _lib.EVP_get_digestbyname(_byte_string(digest_name)) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1176 | if digest == _ffi.NULL: |
Jean-Paul Calderone | b407872 | 2013-02-19 12:01:55 -0800 | [diff] [blame] | 1177 | raise ValueError("No such digest method") |
| 1178 | |
Paul Kehrer | 9f9113a | 2016-09-20 20:10:25 -0500 | [diff] [blame] | 1179 | result_buffer = _ffi.new("unsigned char[]", _lib.EVP_MAX_MD_SIZE) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1180 | result_length = _ffi.new("unsigned int[]", 1) |
Jean-Paul Calderone | b407872 | 2013-02-19 12:01:55 -0800 | [diff] [blame] | 1181 | result_length[0] = len(result_buffer) |
| 1182 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1183 | digest_result = _lib.X509_digest( |
Jean-Paul Calderone | b407872 | 2013-02-19 12:01:55 -0800 | [diff] [blame] | 1184 | self._x509, digest, result_buffer, result_length) |
Alex Gaynor | 09a386e | 2016-07-03 09:32:44 -0400 | [diff] [blame] | 1185 | _openssl_assert(digest_result == 1) |
Jean-Paul Calderone | b407872 | 2013-02-19 12:01:55 -0800 | [diff] [blame] | 1186 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1187 | return b":".join([ |
Alex Gaynor | a738ed5 | 2015-09-05 11:17:10 -0400 | [diff] [blame] | 1188 | b16encode(ch).upper() for ch |
| 1189 | in _ffi.buffer(result_buffer, result_length[0])]) |
Jean-Paul Calderone | b407872 | 2013-02-19 12:01:55 -0800 | [diff] [blame] | 1190 | |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1191 | def subject_name_hash(self): |
| 1192 | """ |
| 1193 | Return the hash of the X509 subject. |
| 1194 | |
| 1195 | :return: The hash of the subject. |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1196 | :rtype: :py:class:`bytes` |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1197 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1198 | return _lib.X509_subject_name_hash(self._x509) |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1199 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1200 | def set_serial_number(self, serial): |
| 1201 | """ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1202 | Set the serial number of the certificate. |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1203 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1204 | :param serial: The new serial number. |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1205 | :type serial: :py:class:`int` |
| 1206 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1207 | :return: :py:data`None` |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1208 | """ |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1209 | if not isinstance(serial, _integer_types): |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1210 | raise TypeError("serial must be an integer") |
| 1211 | |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1212 | hex_serial = hex(serial)[2:] |
| 1213 | if not isinstance(hex_serial, bytes): |
| 1214 | hex_serial = hex_serial.encode('ascii') |
| 1215 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1216 | bignum_serial = _ffi.new("BIGNUM**") |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1217 | |
| 1218 | # BN_hex2bn stores the result in &bignum. Unless it doesn't feel like |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 1219 | # it. If bignum is still NULL after this call, then the return value |
| 1220 | # is actually the result. I hope. -exarkun |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1221 | small_serial = _lib.BN_hex2bn(bignum_serial, hex_serial) |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1222 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1223 | if bignum_serial[0] == _ffi.NULL: |
| 1224 | set_result = _lib.ASN1_INTEGER_set( |
| 1225 | _lib.X509_get_serialNumber(self._x509), small_serial) |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1226 | if set_result: |
| 1227 | # TODO Not tested |
| 1228 | _raise_current_error() |
| 1229 | else: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1230 | asn1_serial = _lib.BN_to_ASN1_INTEGER(bignum_serial[0], _ffi.NULL) |
| 1231 | _lib.BN_free(bignum_serial[0]) |
| 1232 | if asn1_serial == _ffi.NULL: |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1233 | # TODO Not tested |
| 1234 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1235 | asn1_serial = _ffi.gc(asn1_serial, _lib.ASN1_INTEGER_free) |
| 1236 | set_result = _lib.X509_set_serialNumber(self._x509, asn1_serial) |
Alex Gaynor | 3772611 | 2016-07-04 09:51:32 -0400 | [diff] [blame] | 1237 | _openssl_assert(set_result == 1) |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1238 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1239 | def get_serial_number(self): |
| 1240 | """ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1241 | Return the serial number of this certificate. |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1242 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1243 | :return: The serial number. |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1244 | :rtype: int |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1245 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1246 | asn1_serial = _lib.X509_get_serialNumber(self._x509) |
| 1247 | bignum_serial = _lib.ASN1_INTEGER_to_BN(asn1_serial, _ffi.NULL) |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1248 | try: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1249 | hex_serial = _lib.BN_bn2hex(bignum_serial) |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1250 | try: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1251 | hexstring_serial = _ffi.string(hex_serial) |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1252 | serial = int(hexstring_serial, 16) |
| 1253 | return serial |
| 1254 | finally: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1255 | _lib.OPENSSL_free(hex_serial) |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1256 | finally: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1257 | _lib.BN_free(bignum_serial) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1258 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1259 | def gmtime_adj_notAfter(self, amount): |
| 1260 | """ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1261 | Adjust the time stamp on which the certificate stops being valid. |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1262 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1263 | :param int amount: The number of seconds by which to adjust the |
| 1264 | timestamp. |
| 1265 | :return: ``None`` |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1266 | """ |
| 1267 | if not isinstance(amount, int): |
| 1268 | raise TypeError("amount must be an integer") |
| 1269 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1270 | notAfter = _lib.X509_get_notAfter(self._x509) |
| 1271 | _lib.X509_gmtime_adj(notAfter, amount) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1272 | |
Jean-Paul Calderone | 662afe5 | 2013-02-20 08:41:11 -0800 | [diff] [blame] | 1273 | def gmtime_adj_notBefore(self, amount): |
| 1274 | """ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1275 | Adjust the timestamp on which the certificate starts being valid. |
Jean-Paul Calderone | 662afe5 | 2013-02-20 08:41:11 -0800 | [diff] [blame] | 1276 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1277 | :param amount: The number of seconds by which to adjust the timestamp. |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1278 | :return: ``None`` |
Jean-Paul Calderone | 662afe5 | 2013-02-20 08:41:11 -0800 | [diff] [blame] | 1279 | """ |
| 1280 | if not isinstance(amount, int): |
| 1281 | raise TypeError("amount must be an integer") |
| 1282 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1283 | notBefore = _lib.X509_get_notBefore(self._x509) |
| 1284 | _lib.X509_gmtime_adj(notBefore, amount) |
Jean-Paul Calderone | 662afe5 | 2013-02-20 08:41:11 -0800 | [diff] [blame] | 1285 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1286 | def has_expired(self): |
| 1287 | """ |
| 1288 | Check whether the certificate has expired. |
| 1289 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1290 | :return: ``True`` if the certificate has expired, ``False`` otherwise. |
| 1291 | :rtype: bool |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1292 | """ |
Paul Kehrer | 8d887e1 | 2015-10-24 09:09:55 -0500 | [diff] [blame] | 1293 | time_string = _native(self.get_notAfter()) |
Paul Kehrer | fde45c9 | 2016-01-21 12:57:37 -0600 | [diff] [blame] | 1294 | not_after = datetime.datetime.strptime(time_string, "%Y%m%d%H%M%SZ") |
Paul Kehrer | 5d5d28d | 2015-10-21 18:55:22 -0500 | [diff] [blame] | 1295 | |
Paul Kehrer | fde45c9 | 2016-01-21 12:57:37 -0600 | [diff] [blame] | 1296 | return not_after < datetime.datetime.utcnow() |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1297 | |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1298 | def _get_boundary_time(self, which): |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1299 | return _get_asn1_time(which(self._x509)) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1300 | |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1301 | def get_notBefore(self): |
| 1302 | """ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1303 | Get the timestamp at which the certificate starts being valid. |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1304 | |
Paul Kehrer | ce98ee6 | 2017-06-21 06:59:58 -1000 | [diff] [blame] | 1305 | The timestamp is formatted as an ASN.1 TIME:: |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1306 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1307 | YYYYMMDDhhmmssZ |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1308 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1309 | :return: A timestamp string, or ``None`` if there is none. |
| 1310 | :rtype: bytes or NoneType |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1311 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1312 | return self._get_boundary_time(_lib.X509_get_notBefore) |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1313 | |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1314 | def _set_boundary_time(self, which, when): |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1315 | return _set_asn1_time(which(self._x509), when) |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1316 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1317 | def set_notBefore(self, when): |
| 1318 | """ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1319 | Set the timestamp at which the certificate starts being valid. |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1320 | |
Paul Kehrer | ce98ee6 | 2017-06-21 06:59:58 -1000 | [diff] [blame] | 1321 | The timestamp is formatted as an ASN.1 TIME:: |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1322 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1323 | YYYYMMDDhhmmssZ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1324 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1325 | :param bytes when: A timestamp string. |
| 1326 | :return: ``None`` |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1327 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1328 | return self._set_boundary_time(_lib.X509_get_notBefore, when) |
Jean-Paul Calderone | d7d8127 | 2013-02-19 13:16:03 -0800 | [diff] [blame] | 1329 | |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1330 | def get_notAfter(self): |
| 1331 | """ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1332 | Get the timestamp at which the certificate stops being valid. |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1333 | |
Paul Kehrer | ce98ee6 | 2017-06-21 06:59:58 -1000 | [diff] [blame] | 1334 | The timestamp is formatted as an ASN.1 TIME:: |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1335 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1336 | YYYYMMDDhhmmssZ |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1337 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1338 | :return: A timestamp string, or ``None`` if there is none. |
| 1339 | :rtype: bytes or NoneType |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1340 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1341 | return self._get_boundary_time(_lib.X509_get_notAfter) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1342 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1343 | def set_notAfter(self, when): |
| 1344 | """ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1345 | Set the timestamp at which the certificate stops being valid. |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1346 | |
Paul Kehrer | ce98ee6 | 2017-06-21 06:59:58 -1000 | [diff] [blame] | 1347 | The timestamp is formatted as an ASN.1 TIME:: |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1348 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1349 | YYYYMMDDhhmmssZ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1350 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1351 | :param bytes when: A timestamp string. |
| 1352 | :return: ``None`` |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1353 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1354 | return self._set_boundary_time(_lib.X509_get_notAfter, when) |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1355 | |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1356 | def _get_name(self, which): |
| 1357 | name = X509Name.__new__(X509Name) |
| 1358 | name._name = which(self._x509) |
Alex Gaynor | add5b07 | 2016-06-04 21:04:00 -0700 | [diff] [blame] | 1359 | _openssl_assert(name._name != _ffi.NULL) |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 1360 | |
| 1361 | # The name is owned by the X509 structure. As long as the X509Name |
| 1362 | # Python object is alive, keep the X509 Python object alive. |
| 1363 | name._owner = self |
| 1364 | |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1365 | return name |
| 1366 | |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1367 | def _set_name(self, which, name): |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 1368 | if not isinstance(name, X509Name): |
| 1369 | raise TypeError("name must be an X509Name") |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1370 | set_result = which(self._x509, name._name) |
Alex Gaynor | 09a386e | 2016-07-03 09:32:44 -0400 | [diff] [blame] | 1371 | _openssl_assert(set_result == 1) |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1372 | |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1373 | def get_issuer(self): |
| 1374 | """ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1375 | Return the issuer of this certificate. |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1376 | |
Cory Benfield | e6bcce8 | 2015-12-09 08:40:03 +0000 | [diff] [blame] | 1377 | This creates a new :class:`X509Name` that wraps the underlying issuer |
| 1378 | name field on the certificate. Modifying it will modify the underlying |
| 1379 | certificate, and will have the effect of modifying any other |
| 1380 | :class:`X509Name` that refers to this issuer. |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1381 | |
| 1382 | :return: The issuer of this certificate. |
Cory Benfield | e6bcce8 | 2015-12-09 08:40:03 +0000 | [diff] [blame] | 1383 | :rtype: :class:`X509Name` |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1384 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1385 | return self._get_name(_lib.X509_get_issuer_name) |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1386 | |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1387 | def set_issuer(self, issuer): |
| 1388 | """ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1389 | Set the issuer of this certificate. |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1390 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1391 | :param issuer: The issuer. |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1392 | :type issuer: :py:class:`X509Name` |
| 1393 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1394 | :return: ``None`` |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1395 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1396 | return self._set_name(_lib.X509_set_issuer_name, issuer) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 1397 | |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 1398 | def get_subject(self): |
| 1399 | """ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1400 | Return the subject of this certificate. |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 1401 | |
Cory Benfield | e6bcce8 | 2015-12-09 08:40:03 +0000 | [diff] [blame] | 1402 | This creates a new :class:`X509Name` that wraps the underlying subject |
| 1403 | name field on the certificate. Modifying it will modify the underlying |
| 1404 | certificate, and will have the effect of modifying any other |
| 1405 | :class:`X509Name` that refers to this subject. |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1406 | |
| 1407 | :return: The subject of this certificate. |
Cory Benfield | e6bcce8 | 2015-12-09 08:40:03 +0000 | [diff] [blame] | 1408 | :rtype: :class:`X509Name` |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 1409 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1410 | return self._get_name(_lib.X509_get_subject_name) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 1411 | |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 1412 | def set_subject(self, subject): |
| 1413 | """ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1414 | Set the subject of this certificate. |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 1415 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1416 | :param subject: The subject. |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 1417 | :type subject: :py:class:`X509Name` |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1418 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1419 | :return: ``None`` |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 1420 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1421 | return self._set_name(_lib.X509_set_subject_name, subject) |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 1422 | |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 1423 | def get_extension_count(self): |
| 1424 | """ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1425 | Get the number of extensions on this certificate. |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 1426 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1427 | :return: The number of extensions. |
| 1428 | :rtype: :py:class:`int` |
| 1429 | |
| 1430 | .. versionadded:: 0.12 |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 1431 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1432 | return _lib.X509_get_ext_count(self._x509) |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 1433 | |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 1434 | def add_extensions(self, extensions): |
| 1435 | """ |
| 1436 | Add extensions to the certificate. |
| 1437 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1438 | :param extensions: The extensions to add. |
| 1439 | :type extensions: An iterable of :py:class:`X509Extension` objects. |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1440 | :return: ``None`` |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 1441 | """ |
| 1442 | for ext in extensions: |
| 1443 | if not isinstance(ext, X509Extension): |
| 1444 | raise ValueError("One of the elements is not an X509Extension") |
| 1445 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1446 | add_result = _lib.X509_add_ext(self._x509, ext._extension, -1) |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 1447 | if not add_result: |
| 1448 | _raise_current_error() |
| 1449 | |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 1450 | def get_extension(self, index): |
| 1451 | """ |
| 1452 | Get a specific extension of the certificate by index. |
| 1453 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 1454 | Extensions on a certificate are kept in order. The index |
| 1455 | parameter selects which extension will be returned. |
| 1456 | |
| 1457 | :param int index: The index of the extension to retrieve. |
| 1458 | :return: The extension at the specified index. |
| 1459 | :rtype: :py:class:`X509Extension` |
| 1460 | :raises IndexError: If the extension index was out of bounds. |
| 1461 | |
| 1462 | .. versionadded:: 0.12 |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 1463 | """ |
| 1464 | ext = X509Extension.__new__(X509Extension) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1465 | ext._extension = _lib.X509_get_ext(self._x509, index) |
| 1466 | if ext._extension == _ffi.NULL: |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 1467 | raise IndexError("extension index out of bounds") |
| 1468 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1469 | extension = _lib.X509_EXTENSION_dup(ext._extension) |
| 1470 | ext._extension = _ffi.gc(extension, _lib.X509_EXTENSION_free) |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 1471 | return ext |
| 1472 | |
Laurens Van Houtven | ef5c83d | 2014-06-17 15:32:27 +0200 | [diff] [blame] | 1473 | |
Alex Gaynor | 10d3083 | 2017-06-29 15:31:39 -0700 | [diff] [blame] | 1474 | X509Type = deprecated( |
| 1475 | X509, __name__, |
| 1476 | "X509Type has been deprecated, use X509 instead", |
| 1477 | DeprecationWarning |
| 1478 | ) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1479 | |
| 1480 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1481 | class X509StoreFlags(object): |
| 1482 | """ |
| 1483 | Flags for X509 verification, used to change the behavior of |
| 1484 | :class:`X509Store`. |
| 1485 | |
| 1486 | See `OpenSSL Verification Flags`_ for details. |
| 1487 | |
| 1488 | .. _OpenSSL Verification Flags: |
Alex Chan | 54005ce | 2017-03-21 08:08:17 +0000 | [diff] [blame] | 1489 | https://www.openssl.org/docs/manmaster/man3/X509_VERIFY_PARAM_set_flags.html |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1490 | """ |
| 1491 | CRL_CHECK = _lib.X509_V_FLAG_CRL_CHECK |
| 1492 | CRL_CHECK_ALL = _lib.X509_V_FLAG_CRL_CHECK_ALL |
| 1493 | IGNORE_CRITICAL = _lib.X509_V_FLAG_IGNORE_CRITICAL |
| 1494 | X509_STRICT = _lib.X509_V_FLAG_X509_STRICT |
| 1495 | ALLOW_PROXY_CERTS = _lib.X509_V_FLAG_ALLOW_PROXY_CERTS |
| 1496 | POLICY_CHECK = _lib.X509_V_FLAG_POLICY_CHECK |
| 1497 | EXPLICIT_POLICY = _lib.X509_V_FLAG_EXPLICIT_POLICY |
| 1498 | INHIBIT_MAP = _lib.X509_V_FLAG_INHIBIT_MAP |
| 1499 | NOTIFY_POLICY = _lib.X509_V_FLAG_NOTIFY_POLICY |
| 1500 | CHECK_SS_SIGNATURE = _lib.X509_V_FLAG_CHECK_SS_SIGNATURE |
| 1501 | CB_ISSUER_CHECK = _lib.X509_V_FLAG_CB_ISSUER_CHECK |
| 1502 | |
| 1503 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1504 | class X509Store(object): |
Laurens Van Houtven | ef5c83d | 2014-06-17 15:32:27 +0200 | [diff] [blame] | 1505 | """ |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1506 | An X.509 store. |
| 1507 | |
| 1508 | An X.509 store is used to describe a context in which to verify a |
| 1509 | certificate. A description of a context may include a set of certificates |
| 1510 | to trust, a set of certificate revocation lists, verification flags and |
| 1511 | more. |
| 1512 | |
| 1513 | An X.509 store, being only a description, cannot be used by itself to |
| 1514 | verify a certificate. To carry out the actual verification process, see |
| 1515 | :class:`X509StoreContext`. |
Laurens Van Houtven | ef5c83d | 2014-06-17 15:32:27 +0200 | [diff] [blame] | 1516 | """ |
Alex Gaynor | a738ed5 | 2015-09-05 11:17:10 -0400 | [diff] [blame] | 1517 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1518 | def __init__(self): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1519 | store = _lib.X509_STORE_new() |
| 1520 | self._store = _ffi.gc(store, _lib.X509_STORE_free) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1521 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1522 | def add_cert(self, cert): |
Laurens Van Houtven | ef5c83d | 2014-06-17 15:32:27 +0200 | [diff] [blame] | 1523 | """ |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1524 | Adds a trusted certificate to this store. |
Laurens Van Houtven | ef5c83d | 2014-06-17 15:32:27 +0200 | [diff] [blame] | 1525 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1526 | Adding a certificate with this method adds this certificate as a |
| 1527 | *trusted* certificate. |
Laurens Van Houtven | ef5c83d | 2014-06-17 15:32:27 +0200 | [diff] [blame] | 1528 | |
| 1529 | :param X509 cert: The certificate to add to this store. |
Hynek Schlawack | 01c3167 | 2016-12-11 15:14:09 +0100 | [diff] [blame] | 1530 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1531 | :raises TypeError: If the certificate is not an :class:`X509`. |
Hynek Schlawack | 01c3167 | 2016-12-11 15:14:09 +0100 | [diff] [blame] | 1532 | |
| 1533 | :raises OpenSSL.crypto.Error: If OpenSSL was unhappy with your |
| 1534 | certificate. |
| 1535 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1536 | :return: ``None`` if the certificate was added successfully. |
Laurens Van Houtven | ef5c83d | 2014-06-17 15:32:27 +0200 | [diff] [blame] | 1537 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1538 | if not isinstance(cert, X509): |
| 1539 | raise TypeError() |
| 1540 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1541 | _openssl_assert(_lib.X509_STORE_add_cert(self._store, cert._x509) != 0) |
| 1542 | |
| 1543 | def add_crl(self, crl): |
| 1544 | """ |
| 1545 | Add a certificate revocation list to this store. |
| 1546 | |
| 1547 | The certificate revocation lists added to a store will only be used if |
| 1548 | the associated flags are configured to check certificate revocation |
| 1549 | lists. |
| 1550 | |
| 1551 | .. versionadded:: 16.1.0 |
| 1552 | |
| 1553 | :param CRL crl: The certificate revocation list to add to this store. |
| 1554 | :return: ``None`` if the certificate revocation list was added |
| 1555 | successfully. |
| 1556 | """ |
| 1557 | _openssl_assert(_lib.X509_STORE_add_crl(self._store, crl._crl) != 0) |
| 1558 | |
| 1559 | def set_flags(self, flags): |
| 1560 | """ |
| 1561 | Set verification flags to this store. |
| 1562 | |
| 1563 | Verification flags can be combined by oring them together. |
| 1564 | |
| 1565 | .. note:: |
| 1566 | |
| 1567 | Setting a verification flag sometimes requires clients to add |
| 1568 | additional information to the store, otherwise a suitable error will |
| 1569 | be raised. |
| 1570 | |
| 1571 | For example, in setting flags to enable CRL checking a |
| 1572 | suitable CRL must be added to the store otherwise an error will be |
| 1573 | raised. |
| 1574 | |
| 1575 | .. versionadded:: 16.1.0 |
| 1576 | |
| 1577 | :param int flags: The verification flags to set on this store. |
| 1578 | See :class:`X509StoreFlags` for available constants. |
| 1579 | :return: ``None`` if the verification flags were successfully set. |
| 1580 | """ |
| 1581 | _openssl_assert(_lib.X509_STORE_set_flags(self._store, flags) != 0) |
Jean-Paul Calderone | e6f32b8 | 2013-03-06 10:27:57 -0800 | [diff] [blame] | 1582 | |
Thomas Sileo | e15e60a | 2016-11-22 18:13:30 +0100 | [diff] [blame] | 1583 | def set_time(self, vfy_time): |
| 1584 | """ |
| 1585 | Set the time against which the certificates are verified. |
| 1586 | |
| 1587 | Normally the current time is used. |
| 1588 | |
| 1589 | .. note:: |
| 1590 | |
| 1591 | For example, you can determine if a certificate was valid at a given |
| 1592 | time. |
| 1593 | |
Hynek Schlawack | f6c96af | 2017-04-20 12:34:58 +0200 | [diff] [blame] | 1594 | .. versionadded:: 17.0.0 |
Thomas Sileo | e15e60a | 2016-11-22 18:13:30 +0100 | [diff] [blame] | 1595 | |
| 1596 | :param datetime vfy_time: The verification time to set on this store. |
| 1597 | :return: ``None`` if the verification time was successfully set. |
| 1598 | """ |
| 1599 | param = _lib.X509_VERIFY_PARAM_new() |
| 1600 | param = _ffi.gc(param, _lib.X509_VERIFY_PARAM_free) |
| 1601 | |
| 1602 | _lib.X509_VERIFY_PARAM_set_time(param, int(vfy_time.strftime('%s'))) |
| 1603 | _openssl_assert(_lib.X509_STORE_set1_param(self._store, param) != 0) |
| 1604 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1605 | |
Alex Gaynor | 10d3083 | 2017-06-29 15:31:39 -0700 | [diff] [blame] | 1606 | X509StoreType = deprecated( |
| 1607 | X509Store, __name__, |
| 1608 | "X509StoreType has been deprecated, use X509Store instead", |
| 1609 | DeprecationWarning |
| 1610 | ) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1611 | |
| 1612 | |
Stephen Holsapple | 08ffaa6 | 2015-01-30 17:18:40 -0800 | [diff] [blame] | 1613 | class X509StoreContextError(Exception): |
| 1614 | """ |
Stephen Holsapple | 8ad4a19 | 2015-06-09 22:51:43 -0700 | [diff] [blame] | 1615 | An exception raised when an error occurred while verifying a certificate |
| 1616 | using `OpenSSL.X509StoreContext.verify_certificate`. |
Stephen Holsapple | 08ffaa6 | 2015-01-30 17:18:40 -0800 | [diff] [blame] | 1617 | |
Jean-Paul Calderone | feb1743 | 2015-03-15 15:49:45 -0400 | [diff] [blame] | 1618 | :ivar certificate: The certificate which caused verificate failure. |
Stephen Holsapple | 8ad4a19 | 2015-06-09 22:51:43 -0700 | [diff] [blame] | 1619 | :type certificate: :class:`X509` |
Stephen Holsapple | 08ffaa6 | 2015-01-30 17:18:40 -0800 | [diff] [blame] | 1620 | """ |
Alex Gaynor | a738ed5 | 2015-09-05 11:17:10 -0400 | [diff] [blame] | 1621 | |
Stephen Holsapple | 08ffaa6 | 2015-01-30 17:18:40 -0800 | [diff] [blame] | 1622 | def __init__(self, message, certificate): |
| 1623 | super(X509StoreContextError, self).__init__(message) |
| 1624 | self.certificate = certificate |
| 1625 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1626 | |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 1627 | class X509StoreContext(object): |
| 1628 | """ |
| 1629 | An X.509 store context. |
| 1630 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1631 | An X.509 store context is used to carry out the actual verification process |
| 1632 | of a certificate in a described context. For describing such a context, see |
| 1633 | :class:`X509Store`. |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 1634 | |
Jean-Paul Calderone | b7b7fb9 | 2015-01-18 15:37:10 -0500 | [diff] [blame] | 1635 | :ivar _store_ctx: The underlying X509_STORE_CTX structure used by this |
| 1636 | instance. It is dynamically allocated and automatically garbage |
| 1637 | collected. |
Jean-Paul Calderone | 64b6b84 | 2015-03-15 16:08:02 -0400 | [diff] [blame] | 1638 | :ivar _store: See the ``store`` ``__init__`` parameter. |
Jean-Paul Calderone | 64b6b84 | 2015-03-15 16:08:02 -0400 | [diff] [blame] | 1639 | :ivar _cert: See the ``certificate`` ``__init__`` parameter. |
Stephen Holsapple | 8ad4a19 | 2015-06-09 22:51:43 -0700 | [diff] [blame] | 1640 | :param X509Store store: The certificates which will be trusted for the |
| 1641 | purposes of any verifications. |
Stephen Holsapple | 8ad4a19 | 2015-06-09 22:51:43 -0700 | [diff] [blame] | 1642 | :param X509 certificate: The certificate to be verified. |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 1643 | """ |
| 1644 | |
Jean-Paul Calderone | b7b7fb9 | 2015-01-18 15:37:10 -0500 | [diff] [blame] | 1645 | def __init__(self, store, certificate): |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 1646 | store_ctx = _lib.X509_STORE_CTX_new() |
| 1647 | self._store_ctx = _ffi.gc(store_ctx, _lib.X509_STORE_CTX_free) |
| 1648 | self._store = store |
Jean-Paul Calderone | b7b7fb9 | 2015-01-18 15:37:10 -0500 | [diff] [blame] | 1649 | self._cert = certificate |
Stephen Holsapple | 46a0925 | 2015-02-12 14:45:43 -0800 | [diff] [blame] | 1650 | # Make the store context available for use after instantiating this |
| 1651 | # class by initializing it now. Per testing, subsequent calls to |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1652 | # :meth:`_init` have no adverse affect. |
Stephen Holsapple | 46a0925 | 2015-02-12 14:45:43 -0800 | [diff] [blame] | 1653 | self._init() |
Jean-Paul Calderone | b7b7fb9 | 2015-01-18 15:37:10 -0500 | [diff] [blame] | 1654 | |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 1655 | def _init(self): |
| 1656 | """ |
| 1657 | Set up the store context for a subsequent verification operation. |
Jeremy Cline | 58193f1 | 2017-09-13 21:14:53 -0400 | [diff] [blame] | 1658 | |
| 1659 | Calling this method more than once without first calling |
| 1660 | :meth:`_cleanup` will leak memory. |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 1661 | """ |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 1662 | ret = _lib.X509_STORE_CTX_init( |
| 1663 | self._store_ctx, self._store._store, self._cert._x509, _ffi.NULL |
| 1664 | ) |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 1665 | if ret <= 0: |
| 1666 | _raise_current_error() |
| 1667 | |
| 1668 | def _cleanup(self): |
| 1669 | """ |
| 1670 | Internally cleans up the store context. |
| 1671 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1672 | The store context can then be reused with a new call to :meth:`_init`. |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 1673 | """ |
| 1674 | _lib.X509_STORE_CTX_cleanup(self._store_ctx) |
| 1675 | |
Stephen Holsapple | 08ffaa6 | 2015-01-30 17:18:40 -0800 | [diff] [blame] | 1676 | def _exception_from_context(self): |
| 1677 | """ |
| 1678 | Convert an OpenSSL native context error failure into a Python |
| 1679 | exception. |
| 1680 | |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 1681 | When a call to native OpenSSL X509_verify_cert fails, additional |
| 1682 | information about the failure can be obtained from the store context. |
Stephen Holsapple | 08ffaa6 | 2015-01-30 17:18:40 -0800 | [diff] [blame] | 1683 | """ |
| 1684 | errors = [ |
| 1685 | _lib.X509_STORE_CTX_get_error(self._store_ctx), |
| 1686 | _lib.X509_STORE_CTX_get_error_depth(self._store_ctx), |
| 1687 | _native(_ffi.string(_lib.X509_verify_cert_error_string( |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 1688 | _lib.X509_STORE_CTX_get_error(self._store_ctx)))), |
Stephen Holsapple | 08ffaa6 | 2015-01-30 17:18:40 -0800 | [diff] [blame] | 1689 | ] |
Stephen Holsapple | 1f713eb | 2015-02-09 19:19:44 -0800 | [diff] [blame] | 1690 | # A context error should always be associated with a certificate, so we |
| 1691 | # expect this call to never return :class:`None`. |
Stephen Holsapple | 08ffaa6 | 2015-01-30 17:18:40 -0800 | [diff] [blame] | 1692 | _x509 = _lib.X509_STORE_CTX_get_current_cert(self._store_ctx) |
Stephen Holsapple | 1f713eb | 2015-02-09 19:19:44 -0800 | [diff] [blame] | 1693 | _cert = _lib.X509_dup(_x509) |
| 1694 | pycert = X509.__new__(X509) |
| 1695 | pycert._x509 = _ffi.gc(_cert, _lib.X509_free) |
Stephen Holsapple | 08ffaa6 | 2015-01-30 17:18:40 -0800 | [diff] [blame] | 1696 | return X509StoreContextError(errors, pycert) |
| 1697 | |
Stephen Holsapple | 46a0925 | 2015-02-12 14:45:43 -0800 | [diff] [blame] | 1698 | def set_store(self, store): |
| 1699 | """ |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1700 | Set the context's X.509 store. |
Stephen Holsapple | 46a0925 | 2015-02-12 14:45:43 -0800 | [diff] [blame] | 1701 | |
Stephen Holsapple | 8ad4a19 | 2015-06-09 22:51:43 -0700 | [diff] [blame] | 1702 | .. versionadded:: 0.15 |
| 1703 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1704 | :param X509Store store: The store description which will be used for |
| 1705 | the purposes of any *future* verifications. |
Stephen Holsapple | 46a0925 | 2015-02-12 14:45:43 -0800 | [diff] [blame] | 1706 | """ |
| 1707 | self._store = store |
| 1708 | |
Stephen Holsapple | 08ffaa6 | 2015-01-30 17:18:40 -0800 | [diff] [blame] | 1709 | def verify_certificate(self): |
| 1710 | """ |
| 1711 | Verify a certificate in a context. |
| 1712 | |
Stephen Holsapple | 8ad4a19 | 2015-06-09 22:51:43 -0700 | [diff] [blame] | 1713 | .. versionadded:: 0.15 |
| 1714 | |
Alex Gaynor | ca87ff6 | 2015-09-04 23:31:03 -0400 | [diff] [blame] | 1715 | :raises X509StoreContextError: If an error occurred when validating a |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 1716 | certificate in the context. Sets ``certificate`` attribute to |
| 1717 | indicate which certificate caused the error. |
Stephen Holsapple | 08ffaa6 | 2015-01-30 17:18:40 -0800 | [diff] [blame] | 1718 | """ |
Stephen Holsapple | 46a0925 | 2015-02-12 14:45:43 -0800 | [diff] [blame] | 1719 | # Always re-initialize the store context in case |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1720 | # :meth:`verify_certificate` is called multiple times. |
Jeremy Cline | 58193f1 | 2017-09-13 21:14:53 -0400 | [diff] [blame] | 1721 | # |
| 1722 | # :meth:`_init` is called in :meth:`__init__` so _cleanup is called |
| 1723 | # before _init to ensure memory is not leaked. |
| 1724 | self._cleanup() |
Stephen Holsapple | 08ffaa6 | 2015-01-30 17:18:40 -0800 | [diff] [blame] | 1725 | self._init() |
| 1726 | ret = _lib.X509_verify_cert(self._store_ctx) |
| 1727 | self._cleanup() |
| 1728 | if ret <= 0: |
| 1729 | raise self._exception_from_context() |
| 1730 | |
| 1731 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1732 | def load_certificate(type, buffer): |
| 1733 | """ |
| 1734 | Load a certificate from a buffer |
| 1735 | |
| 1736 | :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1) |
| 1737 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1738 | :param bytes buffer: The buffer the certificate is stored in |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1739 | |
| 1740 | :return: The X509 object |
| 1741 | """ |
Jean-Paul Calderone | 6922a86 | 2014-01-18 10:38:28 -0500 | [diff] [blame] | 1742 | if isinstance(buffer, _text_type): |
| 1743 | buffer = buffer.encode("ascii") |
| 1744 | |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 1745 | bio = _new_mem_buf(buffer) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1746 | |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 1747 | if type == FILETYPE_PEM: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1748 | x509 = _lib.PEM_read_bio_X509(bio, _ffi.NULL, _ffi.NULL, _ffi.NULL) |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 1749 | elif type == FILETYPE_ASN1: |
Alex Gaynor | 962ac21 | 2015-09-04 08:06:42 -0400 | [diff] [blame] | 1750 | x509 = _lib.d2i_X509_bio(bio, _ffi.NULL) |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 1751 | else: |
| 1752 | raise ValueError( |
| 1753 | "type argument must be FILETYPE_PEM or FILETYPE_ASN1") |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1754 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1755 | if x509 == _ffi.NULL: |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1756 | _raise_current_error() |
| 1757 | |
| 1758 | cert = X509.__new__(X509) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1759 | cert._x509 = _ffi.gc(x509, _lib.X509_free) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1760 | return cert |
| 1761 | |
| 1762 | |
| 1763 | def dump_certificate(type, cert): |
| 1764 | """ |
| 1765 | Dump a certificate to a buffer |
| 1766 | |
Jean-Paul Calderone | a12e7d2 | 2013-04-03 08:17:34 -0400 | [diff] [blame] | 1767 | :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1, or |
| 1768 | FILETYPE_TEXT) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1769 | :param cert: The certificate to dump |
| 1770 | :return: The buffer with the dumped certificate in |
| 1771 | """ |
Jean-Paul Calderone | 0c73aff | 2013-03-02 07:45:12 -0800 | [diff] [blame] | 1772 | bio = _new_mem_buf() |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 1773 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1774 | if type == FILETYPE_PEM: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1775 | result_code = _lib.PEM_write_bio_X509(bio, cert._x509) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1776 | elif type == FILETYPE_ASN1: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1777 | result_code = _lib.i2d_X509_bio(bio, cert._x509) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1778 | elif type == FILETYPE_TEXT: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1779 | result_code = _lib.X509_print_ex(bio, cert._x509, 0, 0) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1780 | else: |
| 1781 | raise ValueError( |
| 1782 | "type argument must be FILETYPE_PEM, FILETYPE_ASN1, or " |
| 1783 | "FILETYPE_TEXT") |
| 1784 | |
Alex Gaynor | c7a9eb5 | 2015-09-05 16:57:49 -0400 | [diff] [blame] | 1785 | assert result_code == 1 |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1786 | return _bio_to_string(bio) |
| 1787 | |
| 1788 | |
Cory Benfield | 6492f7c | 2015-10-27 16:57:58 +0900 | [diff] [blame] | 1789 | def dump_publickey(type, pkey): |
| 1790 | """ |
Cory Benfield | 11c1019 | 2015-10-27 17:23:03 +0900 | [diff] [blame] | 1791 | Dump a public key to a buffer. |
Cory Benfield | 6492f7c | 2015-10-27 16:57:58 +0900 | [diff] [blame] | 1792 | |
Cory Benfield | 9c590b9 | 2015-10-28 14:55:05 +0900 | [diff] [blame] | 1793 | :param type: The file type (one of :data:`FILETYPE_PEM` or |
Cory Benfield | e813cec | 2015-10-28 08:57:08 +0900 | [diff] [blame] | 1794 | :data:`FILETYPE_ASN1`). |
Cory Benfield | 2b6bb80 | 2015-10-28 22:19:31 +0900 | [diff] [blame] | 1795 | :param PKey pkey: The public key to dump |
Cory Benfield | 6492f7c | 2015-10-27 16:57:58 +0900 | [diff] [blame] | 1796 | :return: The buffer with the dumped key in it. |
Cory Benfield | 11c1019 | 2015-10-27 17:23:03 +0900 | [diff] [blame] | 1797 | :rtype: bytes |
Cory Benfield | 6492f7c | 2015-10-27 16:57:58 +0900 | [diff] [blame] | 1798 | """ |
| 1799 | bio = _new_mem_buf() |
| 1800 | if type == FILETYPE_PEM: |
| 1801 | write_bio = _lib.PEM_write_bio_PUBKEY |
| 1802 | elif type == FILETYPE_ASN1: |
| 1803 | write_bio = _lib.i2d_PUBKEY_bio |
| 1804 | else: |
| 1805 | raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1") |
| 1806 | |
| 1807 | result_code = write_bio(bio, pkey._pkey) |
Cory Benfield | 1e9c7ab | 2015-10-28 08:58:31 +0900 | [diff] [blame] | 1808 | if result_code != 1: # pragma: no cover |
Cory Benfield | 6492f7c | 2015-10-27 16:57:58 +0900 | [diff] [blame] | 1809 | _raise_current_error() |
| 1810 | |
| 1811 | return _bio_to_string(bio) |
| 1812 | |
| 1813 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1814 | def dump_privatekey(type, pkey, cipher=None, passphrase=None): |
| 1815 | """ |
Hynek Schlawack | 11e43ad | 2016-07-03 14:40:20 +0200 | [diff] [blame] | 1816 | Dump the private key *pkey* into a buffer string encoded with the type |
| 1817 | *type*. Optionally (if *type* is :const:`FILETYPE_PEM`) encrypting it |
| 1818 | using *cipher* and *passphrase*. |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1819 | |
Hynek Schlawack | 11e43ad | 2016-07-03 14:40:20 +0200 | [diff] [blame] | 1820 | :param type: The file type (one of :const:`FILETYPE_PEM`, |
| 1821 | :const:`FILETYPE_ASN1`, or :const:`FILETYPE_TEXT`) |
| 1822 | :param PKey pkey: The PKey to dump |
| 1823 | :param cipher: (optional) if encrypted PEM format, the cipher to use |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1824 | :param passphrase: (optional) if encrypted PEM format, this can be either |
Hynek Schlawack | 11e43ad | 2016-07-03 14:40:20 +0200 | [diff] [blame] | 1825 | the passphrase to use, or a callback for providing the passphrase. |
| 1826 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1827 | :return: The buffer with the dumped key in |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1828 | :rtype: bytes |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1829 | """ |
Jean-Paul Calderone | ef9a3dc | 2013-03-02 16:33:32 -0800 | [diff] [blame] | 1830 | bio = _new_mem_buf() |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 1831 | |
Paul Kehrer | cded993 | 2017-06-29 18:43:42 -0500 | [diff] [blame] | 1832 | if not isinstance(pkey, PKey): |
| 1833 | raise TypeError("pkey must be a PKey") |
| 1834 | |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 1835 | if cipher is not None: |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1836 | if passphrase is None: |
| 1837 | raise TypeError( |
| 1838 | "if a value is given for cipher " |
| 1839 | "one must also be given for passphrase") |
| 1840 | cipher_obj = _lib.EVP_get_cipherbyname(_byte_string(cipher)) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1841 | if cipher_obj == _ffi.NULL: |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 1842 | raise ValueError("Invalid cipher name") |
| 1843 | else: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1844 | cipher_obj = _ffi.NULL |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1845 | |
Jean-Paul Calderone | 23478b3 | 2013-02-20 13:31:38 -0800 | [diff] [blame] | 1846 | helper = _PassphraseHelper(type, passphrase) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1847 | if type == FILETYPE_PEM: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1848 | result_code = _lib.PEM_write_bio_PrivateKey( |
| 1849 | bio, pkey._pkey, cipher_obj, _ffi.NULL, 0, |
Jean-Paul Calderone | 23478b3 | 2013-02-20 13:31:38 -0800 | [diff] [blame] | 1850 | helper.callback, helper.callback_args) |
| 1851 | helper.raise_if_problem() |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1852 | elif type == FILETYPE_ASN1: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1853 | result_code = _lib.i2d_PrivateKey_bio(bio, pkey._pkey) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1854 | elif type == FILETYPE_TEXT: |
Paul Kehrer | cded993 | 2017-06-29 18:43:42 -0500 | [diff] [blame] | 1855 | if _lib.EVP_PKEY_id(pkey._pkey) != _lib.EVP_PKEY_RSA: |
| 1856 | raise TypeError("Only RSA keys are supported for FILETYPE_TEXT") |
| 1857 | |
Hynek Schlawack | 11e43ad | 2016-07-03 14:40:20 +0200 | [diff] [blame] | 1858 | rsa = _ffi.gc( |
| 1859 | _lib.EVP_PKEY_get1_RSA(pkey._pkey), |
| 1860 | _lib.RSA_free |
| 1861 | ) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1862 | result_code = _lib.RSA_print(bio, rsa, 0) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1863 | else: |
| 1864 | raise ValueError( |
| 1865 | "type argument must be FILETYPE_PEM, FILETYPE_ASN1, or " |
| 1866 | "FILETYPE_TEXT") |
| 1867 | |
Hynek Schlawack | 11e43ad | 2016-07-03 14:40:20 +0200 | [diff] [blame] | 1868 | _openssl_assert(result_code != 0) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1869 | |
| 1870 | return _bio_to_string(bio) |
| 1871 | |
| 1872 | |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1873 | class Revoked(object): |
Laurens Van Houtven | d92f55c | 2014-06-19 17:08:41 +0200 | [diff] [blame] | 1874 | """ |
| 1875 | A certificate revocation. |
| 1876 | """ |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1877 | # http://www.openssl.org/docs/apps/x509v3_config.html#CRL_distribution_points_ |
| 1878 | # which differs from crl_reasons of crypto/x509v3/v3_enum.c that matches |
| 1879 | # OCSP_crl_reason_str. We use the latter, just like the command line |
| 1880 | # program. |
| 1881 | _crl_reasons = [ |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1882 | b"unspecified", |
| 1883 | b"keyCompromise", |
| 1884 | b"CACompromise", |
| 1885 | b"affiliationChanged", |
| 1886 | b"superseded", |
| 1887 | b"cessationOfOperation", |
| 1888 | b"certificateHold", |
| 1889 | # b"removeFromCRL", |
Alex Gaynor | ca87ff6 | 2015-09-04 23:31:03 -0400 | [diff] [blame] | 1890 | ] |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1891 | |
| 1892 | def __init__(self): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1893 | revoked = _lib.X509_REVOKED_new() |
| 1894 | self._revoked = _ffi.gc(revoked, _lib.X509_REVOKED_free) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1895 | |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1896 | def set_serial(self, hex_str): |
| 1897 | """ |
Laurens Van Houtven | d92f55c | 2014-06-19 17:08:41 +0200 | [diff] [blame] | 1898 | Set the serial number. |
| 1899 | |
| 1900 | The serial number is formatted as a hexadecimal number encoded in |
| 1901 | ASCII. |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1902 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1903 | :param bytes hex_str: The new serial number. |
Laurens Van Houtven | d92f55c | 2014-06-19 17:08:41 +0200 | [diff] [blame] | 1904 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1905 | :return: ``None`` |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1906 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1907 | bignum_serial = _ffi.gc(_lib.BN_new(), _lib.BN_free) |
| 1908 | bignum_ptr = _ffi.new("BIGNUM**") |
Jean-Paul Calderone | fd37136 | 2013-03-01 20:53:58 -0800 | [diff] [blame] | 1909 | bignum_ptr[0] = bignum_serial |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1910 | bn_result = _lib.BN_hex2bn(bignum_ptr, hex_str) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1911 | if not bn_result: |
| 1912 | raise ValueError("bad hex string") |
| 1913 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1914 | asn1_serial = _ffi.gc( |
| 1915 | _lib.BN_to_ASN1_INTEGER(bignum_serial, _ffi.NULL), |
| 1916 | _lib.ASN1_INTEGER_free) |
| 1917 | _lib.X509_REVOKED_set_serialNumber(self._revoked, asn1_serial) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1918 | |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1919 | def get_serial(self): |
| 1920 | """ |
Laurens Van Houtven | d92f55c | 2014-06-19 17:08:41 +0200 | [diff] [blame] | 1921 | Get the serial number. |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1922 | |
Laurens Van Houtven | d92f55c | 2014-06-19 17:08:41 +0200 | [diff] [blame] | 1923 | The serial number is formatted as a hexadecimal number encoded in |
| 1924 | ASCII. |
| 1925 | |
| 1926 | :return: The serial number. |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1927 | :rtype: bytes |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1928 | """ |
Jean-Paul Calderone | fd37136 | 2013-03-01 20:53:58 -0800 | [diff] [blame] | 1929 | bio = _new_mem_buf() |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1930 | |
Alex Gaynor | 67903a6 | 2016-06-02 10:37:13 -0700 | [diff] [blame] | 1931 | asn1_int = _lib.X509_REVOKED_get0_serialNumber(self._revoked) |
| 1932 | _openssl_assert(asn1_int != _ffi.NULL) |
| 1933 | result = _lib.i2a_ASN1_INTEGER(bio, asn1_int) |
| 1934 | _openssl_assert(result >= 0) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1935 | return _bio_to_string(bio) |
| 1936 | |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1937 | def _delete_reason(self): |
Alex Gaynor | 67903a6 | 2016-06-02 10:37:13 -0700 | [diff] [blame] | 1938 | for i in range(_lib.X509_REVOKED_get_ext_count(self._revoked)): |
| 1939 | ext = _lib.X509_REVOKED_get_ext(self._revoked, i) |
Paul Kehrer | e8f91cc | 2016-03-09 21:26:29 -0400 | [diff] [blame] | 1940 | obj = _lib.X509_EXTENSION_get_object(ext) |
| 1941 | if _lib.OBJ_obj2nid(obj) == _lib.NID_crl_reason: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1942 | _lib.X509_EXTENSION_free(ext) |
Alex Gaynor | 67903a6 | 2016-06-02 10:37:13 -0700 | [diff] [blame] | 1943 | _lib.X509_REVOKED_delete_ext(self._revoked, i) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1944 | break |
| 1945 | |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1946 | def set_reason(self, reason): |
| 1947 | """ |
Laurens Van Houtven | d92f55c | 2014-06-19 17:08:41 +0200 | [diff] [blame] | 1948 | Set the reason of this revocation. |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1949 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1950 | If :data:`reason` is ``None``, delete the reason instead. |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1951 | |
| 1952 | :param reason: The reason string. |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1953 | :type reason: :class:`bytes` or :class:`NoneType` |
Laurens Van Houtven | d92f55c | 2014-06-19 17:08:41 +0200 | [diff] [blame] | 1954 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1955 | :return: ``None`` |
Laurens Van Houtven | d92f55c | 2014-06-19 17:08:41 +0200 | [diff] [blame] | 1956 | |
| 1957 | .. seealso:: |
| 1958 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1959 | :meth:`all_reasons`, which gives you a list of all supported |
Laurens Van Houtven | d92f55c | 2014-06-19 17:08:41 +0200 | [diff] [blame] | 1960 | reasons which you might pass to this method. |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1961 | """ |
| 1962 | if reason is None: |
| 1963 | self._delete_reason() |
| 1964 | elif not isinstance(reason, bytes): |
| 1965 | raise TypeError("reason must be None or a byte string") |
| 1966 | else: |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1967 | reason = reason.lower().replace(b' ', b'') |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1968 | reason_code = [r.lower() for r in self._crl_reasons].index(reason) |
| 1969 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1970 | new_reason_ext = _lib.ASN1_ENUMERATED_new() |
Alex Gaynor | add5b07 | 2016-06-04 21:04:00 -0700 | [diff] [blame] | 1971 | _openssl_assert(new_reason_ext != _ffi.NULL) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1972 | new_reason_ext = _ffi.gc(new_reason_ext, _lib.ASN1_ENUMERATED_free) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1973 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1974 | set_result = _lib.ASN1_ENUMERATED_set(new_reason_ext, reason_code) |
Alex Gaynor | add5b07 | 2016-06-04 21:04:00 -0700 | [diff] [blame] | 1975 | _openssl_assert(set_result != _ffi.NULL) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1976 | |
| 1977 | self._delete_reason() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1978 | add_result = _lib.X509_REVOKED_add1_ext_i2d( |
| 1979 | self._revoked, _lib.NID_crl_reason, new_reason_ext, 0, 0) |
Alex Gaynor | 09a386e | 2016-07-03 09:32:44 -0400 | [diff] [blame] | 1980 | _openssl_assert(add_result == 1) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1981 | |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1982 | def get_reason(self): |
| 1983 | """ |
Alex Gaynor | 80262fb | 2016-04-22 07:53:42 -0400 | [diff] [blame] | 1984 | Get the reason of this revocation. |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1985 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1986 | :return: The reason, or ``None`` if there is none. |
| 1987 | :rtype: bytes or NoneType |
Laurens Van Houtven | d92f55c | 2014-06-19 17:08:41 +0200 | [diff] [blame] | 1988 | |
| 1989 | .. seealso:: |
| 1990 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 1991 | :meth:`all_reasons`, which gives you a list of all supported |
Laurens Van Houtven | d92f55c | 2014-06-19 17:08:41 +0200 | [diff] [blame] | 1992 | reasons this method might return. |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1993 | """ |
Alex Gaynor | 67903a6 | 2016-06-02 10:37:13 -0700 | [diff] [blame] | 1994 | for i in range(_lib.X509_REVOKED_get_ext_count(self._revoked)): |
| 1995 | ext = _lib.X509_REVOKED_get_ext(self._revoked, i) |
Paul Kehrer | e8f91cc | 2016-03-09 21:26:29 -0400 | [diff] [blame] | 1996 | obj = _lib.X509_EXTENSION_get_object(ext) |
| 1997 | if _lib.OBJ_obj2nid(obj) == _lib.NID_crl_reason: |
Jean-Paul Calderone | fd37136 | 2013-03-01 20:53:58 -0800 | [diff] [blame] | 1998 | bio = _new_mem_buf() |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1999 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2000 | print_result = _lib.X509V3_EXT_print(bio, ext, 0, 0) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2001 | if not print_result: |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 2002 | print_result = _lib.M_ASN1_OCTET_STRING_print( |
Paul Kehrer | e8f91cc | 2016-03-09 21:26:29 -0400 | [diff] [blame] | 2003 | bio, _lib.X509_EXTENSION_get_data(ext) |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 2004 | ) |
Alex Gaynor | 09a386e | 2016-07-03 09:32:44 -0400 | [diff] [blame] | 2005 | _openssl_assert(print_result != 0) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2006 | |
| 2007 | return _bio_to_string(bio) |
| 2008 | |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2009 | def all_reasons(self): |
| 2010 | """ |
| 2011 | Return a list of all the supported reason strings. |
| 2012 | |
Laurens Van Houtven | d92f55c | 2014-06-19 17:08:41 +0200 | [diff] [blame] | 2013 | This list is a copy; modifying it does not change the supported reason |
| 2014 | strings. |
| 2015 | |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2016 | :return: A list of reason strings. |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2017 | :rtype: :class:`list` of :class:`bytes` |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2018 | """ |
| 2019 | return self._crl_reasons[:] |
| 2020 | |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2021 | def set_rev_date(self, when): |
| 2022 | """ |
Laurens Van Houtven | d92f55c | 2014-06-19 17:08:41 +0200 | [diff] [blame] | 2023 | Set the revocation timestamp. |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2024 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2025 | :param bytes when: The timestamp of the revocation, |
Paul Kehrer | ce98ee6 | 2017-06-21 06:59:58 -1000 | [diff] [blame] | 2026 | as ASN.1 TIME. |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2027 | :return: ``None`` |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2028 | """ |
Alex Gaynor | 67903a6 | 2016-06-02 10:37:13 -0700 | [diff] [blame] | 2029 | dt = _lib.X509_REVOKED_get0_revocationDate(self._revoked) |
| 2030 | return _set_asn1_time(dt, when) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2031 | |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2032 | def get_rev_date(self): |
| 2033 | """ |
Laurens Van Houtven | d92f55c | 2014-06-19 17:08:41 +0200 | [diff] [blame] | 2034 | Get the revocation timestamp. |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2035 | |
Paul Kehrer | ce98ee6 | 2017-06-21 06:59:58 -1000 | [diff] [blame] | 2036 | :return: The timestamp of the revocation, as ASN.1 TIME. |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2037 | :rtype: bytes |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2038 | """ |
Alex Gaynor | 67903a6 | 2016-06-02 10:37:13 -0700 | [diff] [blame] | 2039 | dt = _lib.X509_REVOKED_get0_revocationDate(self._revoked) |
| 2040 | return _get_asn1_time(dt) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2041 | |
| 2042 | |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2043 | class CRL(object): |
Laurens Van Houtven | cb32e85 | 2014-06-19 17:36:28 +0200 | [diff] [blame] | 2044 | """ |
| 2045 | A certificate revocation list. |
| 2046 | """ |
Alex Gaynor | a738ed5 | 2015-09-05 11:17:10 -0400 | [diff] [blame] | 2047 | |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2048 | def __init__(self): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2049 | crl = _lib.X509_CRL_new() |
| 2050 | self._crl = _ffi.gc(crl, _lib.X509_CRL_free) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2051 | |
Paul Kehrer | 41c1024 | 2017-06-29 18:24:17 -0500 | [diff] [blame] | 2052 | def to_cryptography(self): |
| 2053 | """ |
| 2054 | Export as a ``cryptography`` CRL. |
| 2055 | |
| 2056 | :rtype: ``cryptography.x509.CertificateRevocationList`` |
| 2057 | |
| 2058 | .. versionadded:: 17.1.0 |
| 2059 | """ |
| 2060 | from cryptography.hazmat.backends.openssl.x509 import ( |
| 2061 | _CertificateRevocationList |
| 2062 | ) |
| 2063 | backend = _get_backend() |
| 2064 | return _CertificateRevocationList(backend, self._crl) |
| 2065 | |
| 2066 | @classmethod |
| 2067 | def from_cryptography(cls, crypto_crl): |
| 2068 | """ |
| 2069 | Construct based on a ``cryptography`` *crypto_crl*. |
| 2070 | |
| 2071 | :param crypto_crl: A ``cryptography`` certificate revocation list |
| 2072 | :type crypto_crl: ``cryptography.x509.CertificateRevocationList`` |
| 2073 | |
| 2074 | :rtype: CRL |
| 2075 | |
| 2076 | .. versionadded:: 17.1.0 |
| 2077 | """ |
| 2078 | if not isinstance(crypto_crl, x509.CertificateRevocationList): |
| 2079 | raise TypeError("Must be a certificate revocation list") |
| 2080 | |
| 2081 | crl = cls() |
| 2082 | crl._crl = crypto_crl._x509_crl |
| 2083 | return crl |
| 2084 | |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2085 | def get_revoked(self): |
| 2086 | """ |
Laurens Van Houtven | cb32e85 | 2014-06-19 17:36:28 +0200 | [diff] [blame] | 2087 | Return the revocations in this certificate revocation list. |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2088 | |
Laurens Van Houtven | cb32e85 | 2014-06-19 17:36:28 +0200 | [diff] [blame] | 2089 | These revocations will be provided by value, not by reference. |
| 2090 | That means it's okay to mutate them: it won't affect this CRL. |
| 2091 | |
| 2092 | :return: The revocations in this CRL. |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2093 | :rtype: :class:`tuple` of :class:`Revocation` |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2094 | """ |
| 2095 | results = [] |
Alex Gaynor | 67903a6 | 2016-06-02 10:37:13 -0700 | [diff] [blame] | 2096 | revoked_stack = _lib.X509_CRL_get_REVOKED(self._crl) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2097 | for i in range(_lib.sk_X509_REVOKED_num(revoked_stack)): |
| 2098 | revoked = _lib.sk_X509_REVOKED_value(revoked_stack, i) |
Paul Kehrer | 2fe23b0 | 2016-03-09 22:02:15 -0400 | [diff] [blame] | 2099 | revoked_copy = _lib.Cryptography_X509_REVOKED_dup(revoked) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2100 | pyrev = Revoked.__new__(Revoked) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2101 | pyrev._revoked = _ffi.gc(revoked_copy, _lib.X509_REVOKED_free) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2102 | results.append(pyrev) |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 2103 | if results: |
| 2104 | return tuple(results) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2105 | |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2106 | def add_revoked(self, revoked): |
| 2107 | """ |
| 2108 | Add a revoked (by value not reference) to the CRL structure |
| 2109 | |
Laurens Van Houtven | cb32e85 | 2014-06-19 17:36:28 +0200 | [diff] [blame] | 2110 | This revocation will be added by value, not by reference. That |
| 2111 | means it's okay to mutate it after adding: it won't affect |
| 2112 | this CRL. |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2113 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2114 | :param Revoked revoked: The new revocation. |
| 2115 | :return: ``None`` |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2116 | """ |
Paul Kehrer | 8dddb1a | 2016-03-09 21:48:04 -0400 | [diff] [blame] | 2117 | copy = _lib.Cryptography_X509_REVOKED_dup(revoked._revoked) |
Alex Gaynor | add5b07 | 2016-06-04 21:04:00 -0700 | [diff] [blame] | 2118 | _openssl_assert(copy != _ffi.NULL) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2119 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2120 | add_result = _lib.X509_CRL_add0_revoked(self._crl, copy) |
Alex Gaynor | 09a386e | 2016-07-03 09:32:44 -0400 | [diff] [blame] | 2121 | _openssl_assert(add_result != 0) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2122 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2123 | def get_issuer(self): |
| 2124 | """ |
| 2125 | Get the CRL's issuer. |
| 2126 | |
| 2127 | .. versionadded:: 16.1.0 |
| 2128 | |
| 2129 | :rtype: X509Name |
| 2130 | """ |
| 2131 | _issuer = _lib.X509_NAME_dup(_lib.X509_CRL_get_issuer(self._crl)) |
| 2132 | _openssl_assert(_issuer != _ffi.NULL) |
| 2133 | _issuer = _ffi.gc(_issuer, _lib.X509_NAME_free) |
| 2134 | issuer = X509Name.__new__(X509Name) |
| 2135 | issuer._name = _issuer |
| 2136 | return issuer |
| 2137 | |
| 2138 | def set_version(self, version): |
| 2139 | """ |
| 2140 | Set the CRL version. |
| 2141 | |
| 2142 | .. versionadded:: 16.1.0 |
| 2143 | |
| 2144 | :param int version: The version of the CRL. |
| 2145 | :return: ``None`` |
| 2146 | """ |
| 2147 | _openssl_assert(_lib.X509_CRL_set_version(self._crl, version) != 0) |
| 2148 | |
| 2149 | def _set_boundary_time(self, which, when): |
| 2150 | return _set_asn1_time(which(self._crl), when) |
| 2151 | |
| 2152 | def set_lastUpdate(self, when): |
| 2153 | """ |
| 2154 | Set when the CRL was last updated. |
| 2155 | |
Paul Kehrer | ce98ee6 | 2017-06-21 06:59:58 -1000 | [diff] [blame] | 2156 | The timestamp is formatted as an ASN.1 TIME:: |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2157 | |
| 2158 | YYYYMMDDhhmmssZ |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2159 | |
| 2160 | .. versionadded:: 16.1.0 |
| 2161 | |
| 2162 | :param bytes when: A timestamp string. |
| 2163 | :return: ``None`` |
| 2164 | """ |
| 2165 | return self._set_boundary_time(_lib.X509_CRL_get_lastUpdate, when) |
| 2166 | |
| 2167 | def set_nextUpdate(self, when): |
| 2168 | """ |
| 2169 | Set when the CRL will next be udpated. |
| 2170 | |
Paul Kehrer | ce98ee6 | 2017-06-21 06:59:58 -1000 | [diff] [blame] | 2171 | The timestamp is formatted as an ASN.1 TIME:: |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2172 | |
| 2173 | YYYYMMDDhhmmssZ |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2174 | |
| 2175 | .. versionadded:: 16.1.0 |
| 2176 | |
| 2177 | :param bytes when: A timestamp string. |
| 2178 | :return: ``None`` |
| 2179 | """ |
| 2180 | return self._set_boundary_time(_lib.X509_CRL_get_nextUpdate, when) |
| 2181 | |
| 2182 | def sign(self, issuer_cert, issuer_key, digest): |
| 2183 | """ |
| 2184 | Sign the CRL. |
| 2185 | |
| 2186 | Signing a CRL enables clients to associate the CRL itself with an |
| 2187 | issuer. Before a CRL is meaningful to other OpenSSL functions, it must |
| 2188 | be signed by an issuer. |
| 2189 | |
| 2190 | This method implicitly sets the issuer's name based on the issuer |
| 2191 | certificate and private key used to sign the CRL. |
| 2192 | |
| 2193 | .. versionadded:: 16.1.0 |
| 2194 | |
| 2195 | :param X509 issuer_cert: The issuer's certificate. |
| 2196 | :param PKey issuer_key: The issuer's private key. |
| 2197 | :param bytes digest: The digest method to sign the CRL with. |
| 2198 | """ |
| 2199 | digest_obj = _lib.EVP_get_digestbyname(digest) |
| 2200 | _openssl_assert(digest_obj != _ffi.NULL) |
| 2201 | _lib.X509_CRL_set_issuer_name( |
| 2202 | self._crl, _lib.X509_get_subject_name(issuer_cert._x509)) |
| 2203 | _lib.X509_CRL_sort(self._crl) |
| 2204 | result = _lib.X509_CRL_sign(self._crl, issuer_key._pkey, digest_obj) |
| 2205 | _openssl_assert(result != 0) |
| 2206 | |
Jean-Paul Calderone | 6043279 | 2015-04-13 12:26:07 -0400 | [diff] [blame] | 2207 | def export(self, cert, key, type=FILETYPE_PEM, days=100, |
Jean-Paul Calderone | 00f84eb | 2015-04-13 12:47:21 -0400 | [diff] [blame] | 2208 | digest=_UNSPECIFIED): |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2209 | """ |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2210 | Export the CRL as a string. |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2211 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2212 | :param X509 cert: The certificate used to sign the CRL. |
| 2213 | :param PKey key: The key used to sign the CRL. |
| 2214 | :param int type: The export format, either :data:`FILETYPE_PEM`, |
| 2215 | :data:`FILETYPE_ASN1`, or :data:`FILETYPE_TEXT`. |
Jean-Paul Calderone | df51401 | 2015-04-13 21:45:18 -0400 | [diff] [blame] | 2216 | :param int days: The number of days until the next update of this CRL. |
Jean-Paul Calderone | cce22d0 | 2015-04-13 13:56:09 -0400 | [diff] [blame] | 2217 | :param bytes digest: The name of the message digest to use (eg |
Alex Gaynor | 239e2d3 | 2016-09-11 12:36:35 -0400 | [diff] [blame] | 2218 | ``b"sha2566"``). |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2219 | :rtype: bytes |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2220 | """ |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2221 | |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 2222 | if not isinstance(cert, X509): |
| 2223 | raise TypeError("cert must be an X509 instance") |
| 2224 | if not isinstance(key, PKey): |
| 2225 | raise TypeError("key must be a PKey instance") |
| 2226 | if not isinstance(type, int): |
| 2227 | raise TypeError("type must be an integer") |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2228 | |
Jean-Paul Calderone | 00f84eb | 2015-04-13 12:47:21 -0400 | [diff] [blame] | 2229 | if digest is _UNSPECIFIED: |
Alex Gaynor | 173e4ba | 2017-06-30 08:01:12 -0700 | [diff] [blame] | 2230 | raise TypeError("digest must be provided") |
Jean-Paul Calderone | 6043279 | 2015-04-13 12:26:07 -0400 | [diff] [blame] | 2231 | |
Jean-Paul Calderone | cce22d0 | 2015-04-13 13:56:09 -0400 | [diff] [blame] | 2232 | digest_obj = _lib.EVP_get_digestbyname(digest) |
Bulat Gaifullin | 2923dc0 | 2014-09-21 22:36:48 +0400 | [diff] [blame] | 2233 | if digest_obj == _ffi.NULL: |
| 2234 | raise ValueError("No such digest method") |
| 2235 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2236 | bio = _lib.BIO_new(_lib.BIO_s_mem()) |
Alex Gaynor | add5b07 | 2016-06-04 21:04:00 -0700 | [diff] [blame] | 2237 | _openssl_assert(bio != _ffi.NULL) |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 2238 | |
Alex Gaynor | a738ed5 | 2015-09-05 11:17:10 -0400 | [diff] [blame] | 2239 | # A scratch time object to give different values to different CRL |
| 2240 | # fields |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2241 | sometime = _lib.ASN1_TIME_new() |
Alex Gaynor | add5b07 | 2016-06-04 21:04:00 -0700 | [diff] [blame] | 2242 | _openssl_assert(sometime != _ffi.NULL) |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 2243 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2244 | _lib.X509_gmtime_adj(sometime, 0) |
| 2245 | _lib.X509_CRL_set_lastUpdate(self._crl, sometime) |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 2246 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2247 | _lib.X509_gmtime_adj(sometime, days * 24 * 60 * 60) |
| 2248 | _lib.X509_CRL_set_nextUpdate(self._crl, sometime) |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 2249 | |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 2250 | _lib.X509_CRL_set_issuer_name( |
| 2251 | self._crl, _lib.X509_get_subject_name(cert._x509) |
| 2252 | ) |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 2253 | |
Bulat Gaifullin | 2923dc0 | 2014-09-21 22:36:48 +0400 | [diff] [blame] | 2254 | sign_result = _lib.X509_CRL_sign(self._crl, key._pkey, digest_obj) |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 2255 | if not sign_result: |
| 2256 | _raise_current_error() |
| 2257 | |
Dominic Chen | f05b212 | 2015-10-13 16:32:35 +0000 | [diff] [blame] | 2258 | return dump_crl(type, self) |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 2259 | |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 2260 | |
Alex Gaynor | 10d3083 | 2017-06-29 15:31:39 -0700 | [diff] [blame] | 2261 | CRLType = deprecated( |
| 2262 | CRL, __name__, |
| 2263 | "CRLType has been deprecated, use CRL instead", |
| 2264 | DeprecationWarning |
| 2265 | ) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2266 | |
| 2267 | |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2268 | class PKCS7(object): |
| 2269 | def type_is_signed(self): |
| 2270 | """ |
| 2271 | Check if this NID_pkcs7_signed object |
| 2272 | |
| 2273 | :return: True if the PKCS7 is of type signed |
| 2274 | """ |
Alex Gaynor | 3aeead9 | 2016-07-31 11:31:59 -0400 | [diff] [blame] | 2275 | return bool(_lib.PKCS7_type_is_signed(self._pkcs7)) |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2276 | |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2277 | def type_is_enveloped(self): |
| 2278 | """ |
| 2279 | Check if this NID_pkcs7_enveloped object |
| 2280 | |
| 2281 | :returns: True if the PKCS7 is of type enveloped |
| 2282 | """ |
Alex Gaynor | 3aeead9 | 2016-07-31 11:31:59 -0400 | [diff] [blame] | 2283 | return bool(_lib.PKCS7_type_is_enveloped(self._pkcs7)) |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2284 | |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2285 | def type_is_signedAndEnveloped(self): |
| 2286 | """ |
| 2287 | Check if this NID_pkcs7_signedAndEnveloped object |
| 2288 | |
| 2289 | :returns: True if the PKCS7 is of type signedAndEnveloped |
| 2290 | """ |
Alex Gaynor | 3aeead9 | 2016-07-31 11:31:59 -0400 | [diff] [blame] | 2291 | return bool(_lib.PKCS7_type_is_signedAndEnveloped(self._pkcs7)) |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2292 | |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2293 | def type_is_data(self): |
| 2294 | """ |
| 2295 | Check if this NID_pkcs7_data object |
| 2296 | |
| 2297 | :return: True if the PKCS7 is of type data |
| 2298 | """ |
Alex Gaynor | 3aeead9 | 2016-07-31 11:31:59 -0400 | [diff] [blame] | 2299 | return bool(_lib.PKCS7_type_is_data(self._pkcs7)) |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2300 | |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2301 | def get_type_name(self): |
| 2302 | """ |
| 2303 | Returns the type name of the PKCS7 structure |
| 2304 | |
| 2305 | :return: A string with the typename |
| 2306 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2307 | nid = _lib.OBJ_obj2nid(self._pkcs7.type) |
| 2308 | string_type = _lib.OBJ_nid2sn(nid) |
| 2309 | return _ffi.string(string_type) |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2310 | |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 2311 | |
Alex Gaynor | 10d3083 | 2017-06-29 15:31:39 -0700 | [diff] [blame] | 2312 | PKCS7Type = deprecated( |
| 2313 | PKCS7, __name__, |
| 2314 | "PKCS7Type has been deprecated, use PKCS7 instead", |
| 2315 | DeprecationWarning |
| 2316 | ) |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2317 | |
| 2318 | |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2319 | class PKCS12(object): |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2320 | """ |
| 2321 | A PKCS #12 archive. |
| 2322 | """ |
Alex Gaynor | a738ed5 | 2015-09-05 11:17:10 -0400 | [diff] [blame] | 2323 | |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2324 | def __init__(self): |
| 2325 | self._pkey = None |
| 2326 | self._cert = None |
| 2327 | self._cacerts = None |
| 2328 | self._friendlyname = None |
| 2329 | |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2330 | def get_certificate(self): |
| 2331 | """ |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2332 | Get the certificate in the PKCS #12 structure. |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2333 | |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2334 | :return: The certificate, or :py:const:`None` if there is none. |
| 2335 | :rtype: :py:class:`X509` or :py:const:`None` |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2336 | """ |
| 2337 | return self._cert |
| 2338 | |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2339 | def set_certificate(self, cert): |
| 2340 | """ |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2341 | Set the certificate in the PKCS #12 structure. |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2342 | |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2343 | :param cert: The new certificate, or :py:const:`None` to unset it. |
Laurens Van Houtven | a790458 | 2014-06-19 12:33:04 +0200 | [diff] [blame] | 2344 | :type cert: :py:class:`X509` or :py:const:`None` |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2345 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2346 | :return: ``None`` |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2347 | """ |
| 2348 | if not isinstance(cert, X509): |
| 2349 | raise TypeError("cert must be an X509 instance") |
| 2350 | self._cert = cert |
| 2351 | |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2352 | def get_privatekey(self): |
| 2353 | """ |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2354 | Get the private key in the PKCS #12 structure. |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2355 | |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2356 | :return: The private key, or :py:const:`None` if there is none. |
| 2357 | :rtype: :py:class:`PKey` |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2358 | """ |
| 2359 | return self._pkey |
| 2360 | |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2361 | def set_privatekey(self, pkey): |
| 2362 | """ |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2363 | Set the certificate portion of the PKCS #12 structure. |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2364 | |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2365 | :param pkey: The new private key, or :py:const:`None` to unset it. |
| 2366 | :type pkey: :py:class:`PKey` or :py:const:`None` |
| 2367 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2368 | :return: ``None`` |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2369 | """ |
| 2370 | if not isinstance(pkey, PKey): |
| 2371 | raise TypeError("pkey must be a PKey instance") |
| 2372 | self._pkey = pkey |
| 2373 | |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2374 | def get_ca_certificates(self): |
| 2375 | """ |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2376 | Get the CA certificates in the PKCS #12 structure. |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2377 | |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2378 | :return: A tuple with the CA certificates in the chain, or |
| 2379 | :py:const:`None` if there are none. |
| 2380 | :rtype: :py:class:`tuple` of :py:class:`X509` or :py:const:`None` |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2381 | """ |
| 2382 | if self._cacerts is not None: |
| 2383 | return tuple(self._cacerts) |
| 2384 | |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2385 | def set_ca_certificates(self, cacerts): |
| 2386 | """ |
Alex Gaynor | 3b0ee97 | 2014-11-15 09:17:33 -0800 | [diff] [blame] | 2387 | Replace or set the CA certificates within the PKCS12 object. |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2388 | |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2389 | :param cacerts: The new CA certificates, or :py:const:`None` to unset |
| 2390 | them. |
Laurens Van Houtven | a790458 | 2014-06-19 12:33:04 +0200 | [diff] [blame] | 2391 | :type cacerts: An iterable of :py:class:`X509` or :py:const:`None` |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2392 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2393 | :return: ``None`` |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2394 | """ |
| 2395 | if cacerts is None: |
| 2396 | self._cacerts = None |
| 2397 | else: |
| 2398 | cacerts = list(cacerts) |
| 2399 | for cert in cacerts: |
| 2400 | if not isinstance(cert, X509): |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 2401 | raise TypeError( |
| 2402 | "iterable must only contain X509 instances" |
| 2403 | ) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2404 | self._cacerts = cacerts |
| 2405 | |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2406 | def set_friendlyname(self, name): |
| 2407 | """ |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2408 | Set the friendly name in the PKCS #12 structure. |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2409 | |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2410 | :param name: The new friendly name, or :py:const:`None` to unset. |
Laurens Van Houtven | a790458 | 2014-06-19 12:33:04 +0200 | [diff] [blame] | 2411 | :type name: :py:class:`bytes` or :py:const:`None` |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2412 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2413 | :return: ``None`` |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2414 | """ |
| 2415 | if name is None: |
| 2416 | self._friendlyname = None |
| 2417 | elif not isinstance(name, bytes): |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 2418 | raise TypeError( |
| 2419 | "name must be a byte string or None (not %r)" % (name,) |
| 2420 | ) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2421 | self._friendlyname = name |
| 2422 | |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2423 | def get_friendlyname(self): |
| 2424 | """ |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2425 | Get the friendly name in the PKCS# 12 structure. |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2426 | |
Laurens Van Houtven | a790458 | 2014-06-19 12:33:04 +0200 | [diff] [blame] | 2427 | :returns: The friendly name, or :py:const:`None` if there is none. |
| 2428 | :rtype: :py:class:`bytes` or :py:const:`None` |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2429 | """ |
| 2430 | return self._friendlyname |
| 2431 | |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2432 | def export(self, passphrase=None, iter=2048, maciter=1): |
| 2433 | """ |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2434 | Dump a PKCS12 object as a string. |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2435 | |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2436 | For more information, see the :c:func:`PKCS12_create` man page. |
| 2437 | |
| 2438 | :param passphrase: The passphrase used to encrypt the structure. Unlike |
| 2439 | some other passphrase arguments, this *must* be a string, not a |
| 2440 | callback. |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2441 | :type passphrase: :py:data:`bytes` |
| 2442 | |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2443 | :param iter: Number of times to repeat the encryption step. |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2444 | :type iter: :py:data:`int` |
| 2445 | |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2446 | :param maciter: Number of times to repeat the MAC step. |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2447 | :type maciter: :py:data:`int` |
| 2448 | |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2449 | :return: The string representation of the PKCS #12 structure. |
| 2450 | :rtype: |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2451 | """ |
Jean-Paul Calderone | 39a8d59 | 2015-04-13 20:49:50 -0400 | [diff] [blame] | 2452 | passphrase = _text_to_bytes_and_warn("passphrase", passphrase) |
Abraham Martin | e82326c | 2015-02-04 10:18:10 +0000 | [diff] [blame] | 2453 | |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2454 | if self._cacerts is None: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2455 | cacerts = _ffi.NULL |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2456 | else: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2457 | cacerts = _lib.sk_X509_new_null() |
| 2458 | cacerts = _ffi.gc(cacerts, _lib.sk_X509_free) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2459 | for cert in self._cacerts: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2460 | _lib.sk_X509_push(cacerts, cert._x509) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2461 | |
| 2462 | if passphrase is None: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2463 | passphrase = _ffi.NULL |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2464 | |
| 2465 | friendlyname = self._friendlyname |
| 2466 | if friendlyname is None: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2467 | friendlyname = _ffi.NULL |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2468 | |
| 2469 | if self._pkey is None: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2470 | pkey = _ffi.NULL |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2471 | else: |
| 2472 | pkey = self._pkey._pkey |
| 2473 | |
| 2474 | if self._cert is None: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2475 | cert = _ffi.NULL |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2476 | else: |
| 2477 | cert = self._cert._x509 |
| 2478 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2479 | pkcs12 = _lib.PKCS12_create( |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2480 | passphrase, friendlyname, pkey, cert, cacerts, |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2481 | _lib.NID_pbe_WithSHA1And3_Key_TripleDES_CBC, |
| 2482 | _lib.NID_pbe_WithSHA1And3_Key_TripleDES_CBC, |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2483 | iter, maciter, 0) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2484 | if pkcs12 == _ffi.NULL: |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2485 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2486 | pkcs12 = _ffi.gc(pkcs12, _lib.PKCS12_free) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2487 | |
Jean-Paul Calderone | ef9a3dc | 2013-03-02 16:33:32 -0800 | [diff] [blame] | 2488 | bio = _new_mem_buf() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2489 | _lib.i2d_PKCS12_bio(bio, pkcs12) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2490 | return _bio_to_string(bio) |
Jean-Paul Calderone | ef9a3dc | 2013-03-02 16:33:32 -0800 | [diff] [blame] | 2491 | |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 2492 | |
Alex Gaynor | 10d3083 | 2017-06-29 15:31:39 -0700 | [diff] [blame] | 2493 | PKCS12Type = deprecated( |
| 2494 | PKCS12, __name__, |
| 2495 | "PKCS12Type has been deprecated, use PKCS12 instead", |
| 2496 | DeprecationWarning |
| 2497 | ) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2498 | |
| 2499 | |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2500 | class NetscapeSPKI(object): |
Laurens Van Houtven | 59152b5 | 2014-06-19 16:42:30 +0200 | [diff] [blame] | 2501 | """ |
| 2502 | A Netscape SPKI object. |
| 2503 | """ |
Alex Gaynor | a738ed5 | 2015-09-05 11:17:10 -0400 | [diff] [blame] | 2504 | |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2505 | def __init__(self): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2506 | spki = _lib.NETSCAPE_SPKI_new() |
| 2507 | self._spki = _ffi.gc(spki, _lib.NETSCAPE_SPKI_free) |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2508 | |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2509 | def sign(self, pkey, digest): |
| 2510 | """ |
Laurens Van Houtven | 59152b5 | 2014-06-19 16:42:30 +0200 | [diff] [blame] | 2511 | Sign the certificate request with this key and digest type. |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2512 | |
Laurens Van Houtven | 59152b5 | 2014-06-19 16:42:30 +0200 | [diff] [blame] | 2513 | :param pkey: The private key to sign with. |
| 2514 | :type pkey: :py:class:`PKey` |
| 2515 | |
| 2516 | :param digest: The message digest to use. |
| 2517 | :type digest: :py:class:`bytes` |
| 2518 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2519 | :return: ``None`` |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2520 | """ |
| 2521 | if pkey._only_public: |
| 2522 | raise ValueError("Key has only public part") |
| 2523 | |
| 2524 | if not pkey._initialized: |
| 2525 | raise ValueError("Key is uninitialized") |
| 2526 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2527 | digest_obj = _lib.EVP_get_digestbyname(_byte_string(digest)) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2528 | if digest_obj == _ffi.NULL: |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2529 | raise ValueError("No such digest method") |
| 2530 | |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 2531 | sign_result = _lib.NETSCAPE_SPKI_sign( |
| 2532 | self._spki, pkey._pkey, digest_obj |
| 2533 | ) |
Alex Gaynor | 09a386e | 2016-07-03 09:32:44 -0400 | [diff] [blame] | 2534 | _openssl_assert(sign_result > 0) |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2535 | |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2536 | def verify(self, key): |
| 2537 | """ |
Laurens Van Houtven | 59152b5 | 2014-06-19 16:42:30 +0200 | [diff] [blame] | 2538 | Verifies a signature on a certificate request. |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2539 | |
Hynek Schlawack | 01c3167 | 2016-12-11 15:14:09 +0100 | [diff] [blame] | 2540 | :param PKey key: The public key that signature is supposedly from. |
Laurens Van Houtven | 59152b5 | 2014-06-19 16:42:30 +0200 | [diff] [blame] | 2541 | |
Hynek Schlawack | 01c3167 | 2016-12-11 15:14:09 +0100 | [diff] [blame] | 2542 | :return: ``True`` if the signature is correct. |
| 2543 | :rtype: bool |
Laurens Van Houtven | 59152b5 | 2014-06-19 16:42:30 +0200 | [diff] [blame] | 2544 | |
Hynek Schlawack | 01c3167 | 2016-12-11 15:14:09 +0100 | [diff] [blame] | 2545 | :raises OpenSSL.crypto.Error: If the signature is invalid, or there was |
| 2546 | a problem verifying the signature. |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2547 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2548 | answer = _lib.NETSCAPE_SPKI_verify(self._spki, key._pkey) |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2549 | if answer <= 0: |
| 2550 | _raise_current_error() |
| 2551 | return True |
| 2552 | |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2553 | def b64_encode(self): |
| 2554 | """ |
Laurens Van Houtven | 59152b5 | 2014-06-19 16:42:30 +0200 | [diff] [blame] | 2555 | Generate a base64 encoded representation of this SPKI object. |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2556 | |
Laurens Van Houtven | 59152b5 | 2014-06-19 16:42:30 +0200 | [diff] [blame] | 2557 | :return: The base64 encoded string. |
| 2558 | :rtype: :py:class:`bytes` |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2559 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2560 | encoded = _lib.NETSCAPE_SPKI_b64_encode(self._spki) |
| 2561 | result = _ffi.string(encoded) |
Paul Kehrer | 0dcacf7 | 2016-03-17 19:25:39 -0400 | [diff] [blame] | 2562 | _lib.OPENSSL_free(encoded) |
Jean-Paul Calderone | 2c2e21d | 2013-03-02 16:50:35 -0800 | [diff] [blame] | 2563 | return result |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2564 | |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2565 | def get_pubkey(self): |
| 2566 | """ |
Laurens Van Houtven | 59152b5 | 2014-06-19 16:42:30 +0200 | [diff] [blame] | 2567 | Get the public key of this certificate. |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2568 | |
Laurens Van Houtven | 59152b5 | 2014-06-19 16:42:30 +0200 | [diff] [blame] | 2569 | :return: The public key. |
| 2570 | :rtype: :py:class:`PKey` |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2571 | """ |
| 2572 | pkey = PKey.__new__(PKey) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2573 | pkey._pkey = _lib.NETSCAPE_SPKI_get_pubkey(self._spki) |
Alex Gaynor | add5b07 | 2016-06-04 21:04:00 -0700 | [diff] [blame] | 2574 | _openssl_assert(pkey._pkey != _ffi.NULL) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2575 | pkey._pkey = _ffi.gc(pkey._pkey, _lib.EVP_PKEY_free) |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2576 | pkey._only_public = True |
| 2577 | return pkey |
| 2578 | |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2579 | def set_pubkey(self, pkey): |
| 2580 | """ |
| 2581 | Set the public key of the certificate |
| 2582 | |
| 2583 | :param pkey: The public key |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2584 | :return: ``None`` |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2585 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2586 | set_result = _lib.NETSCAPE_SPKI_set_pubkey(self._spki, pkey._pkey) |
Alex Gaynor | 09a386e | 2016-07-03 09:32:44 -0400 | [diff] [blame] | 2587 | _openssl_assert(set_result == 1) |
Laurens Van Houtven | 59152b5 | 2014-06-19 16:42:30 +0200 | [diff] [blame] | 2588 | |
| 2589 | |
Alex Gaynor | 10d3083 | 2017-06-29 15:31:39 -0700 | [diff] [blame] | 2590 | NetscapeSPKIType = deprecated( |
| 2591 | NetscapeSPKI, __name__, |
| 2592 | "NetscapeSPKIType has been deprecated, use NetscapeSPKI instead", |
| 2593 | DeprecationWarning |
| 2594 | ) |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2595 | |
| 2596 | |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2597 | class _PassphraseHelper(object): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 2598 | def __init__(self, type, passphrase, more_args=False, truncate=False): |
Jean-Paul Calderone | 23478b3 | 2013-02-20 13:31:38 -0800 | [diff] [blame] | 2599 | if type != FILETYPE_PEM and passphrase is not None: |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 2600 | raise ValueError( |
| 2601 | "only FILETYPE_PEM key format supports encryption" |
| 2602 | ) |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2603 | self._passphrase = passphrase |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 2604 | self._more_args = more_args |
| 2605 | self._truncate = truncate |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2606 | self._problems = [] |
| 2607 | |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2608 | @property |
| 2609 | def callback(self): |
| 2610 | if self._passphrase is None: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2611 | return _ffi.NULL |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2612 | elif isinstance(self._passphrase, bytes): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2613 | return _ffi.NULL |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2614 | elif callable(self._passphrase): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2615 | return _ffi.callback("pem_password_cb", self._read_passphrase) |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2616 | else: |
Hynek Schlawack | 33675f9 | 2016-11-18 14:55:06 +0100 | [diff] [blame] | 2617 | raise TypeError( |
| 2618 | "Last argument must be a byte string or a callable." |
| 2619 | ) |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2620 | |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2621 | @property |
| 2622 | def callback_args(self): |
| 2623 | if self._passphrase is None: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2624 | return _ffi.NULL |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2625 | elif isinstance(self._passphrase, bytes): |
| 2626 | return self._passphrase |
| 2627 | elif callable(self._passphrase): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2628 | return _ffi.NULL |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2629 | else: |
Hynek Schlawack | 33675f9 | 2016-11-18 14:55:06 +0100 | [diff] [blame] | 2630 | raise TypeError( |
| 2631 | "Last argument must be a byte string or a callable." |
| 2632 | ) |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2633 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 2634 | def raise_if_problem(self, exceptionType=Error): |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2635 | if self._problems: |
Greg Bowser | 36eb2de | 2017-01-24 11:38:55 -0500 | [diff] [blame] | 2636 | |
| 2637 | # Flush the OpenSSL error queue |
| 2638 | try: |
| 2639 | _exception_from_error_queue(exceptionType) |
| 2640 | except exceptionType: |
| 2641 | pass |
| 2642 | |
| 2643 | raise self._problems.pop(0) |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2644 | |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2645 | def _read_passphrase(self, buf, size, rwflag, userdata): |
| 2646 | try: |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 2647 | if self._more_args: |
| 2648 | result = self._passphrase(size, rwflag, userdata) |
| 2649 | else: |
| 2650 | result = self._passphrase(rwflag) |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2651 | if not isinstance(result, bytes): |
| 2652 | raise ValueError("String expected") |
| 2653 | if len(result) > size: |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 2654 | if self._truncate: |
| 2655 | result = result[:size] |
| 2656 | else: |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 2657 | raise ValueError( |
| 2658 | "passphrase returned by callback is too long" |
| 2659 | ) |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2660 | for i in range(len(result)): |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2661 | buf[i] = result[i:i + 1] |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2662 | return len(result) |
| 2663 | except Exception as e: |
| 2664 | self._problems.append(e) |
| 2665 | return 0 |
| 2666 | |
| 2667 | |
Cory Benfield | 6492f7c | 2015-10-27 16:57:58 +0900 | [diff] [blame] | 2668 | def load_publickey(type, buffer): |
| 2669 | """ |
Cory Benfield | 11c1019 | 2015-10-27 17:23:03 +0900 | [diff] [blame] | 2670 | Load a public key from a buffer. |
Cory Benfield | 6492f7c | 2015-10-27 16:57:58 +0900 | [diff] [blame] | 2671 | |
Cory Benfield | 9c590b9 | 2015-10-28 14:55:05 +0900 | [diff] [blame] | 2672 | :param type: The file type (one of :data:`FILETYPE_PEM`, |
Cory Benfield | e813cec | 2015-10-28 08:57:08 +0900 | [diff] [blame] | 2673 | :data:`FILETYPE_ASN1`). |
Cory Benfield | c9c30a2 | 2015-10-28 17:39:20 +0900 | [diff] [blame] | 2674 | :param buffer: The buffer the key is stored in. |
| 2675 | :type buffer: A Python string object, either unicode or bytestring. |
| 2676 | :return: The PKey object. |
| 2677 | :rtype: :class:`PKey` |
Cory Benfield | 6492f7c | 2015-10-27 16:57:58 +0900 | [diff] [blame] | 2678 | """ |
| 2679 | if isinstance(buffer, _text_type): |
| 2680 | buffer = buffer.encode("ascii") |
| 2681 | |
| 2682 | bio = _new_mem_buf(buffer) |
| 2683 | |
| 2684 | if type == FILETYPE_PEM: |
| 2685 | evp_pkey = _lib.PEM_read_bio_PUBKEY( |
| 2686 | bio, _ffi.NULL, _ffi.NULL, _ffi.NULL) |
| 2687 | elif type == FILETYPE_ASN1: |
| 2688 | evp_pkey = _lib.d2i_PUBKEY_bio(bio, _ffi.NULL) |
| 2689 | else: |
| 2690 | raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1") |
| 2691 | |
| 2692 | if evp_pkey == _ffi.NULL: |
| 2693 | _raise_current_error() |
| 2694 | |
| 2695 | pkey = PKey.__new__(PKey) |
| 2696 | pkey._pkey = _ffi.gc(evp_pkey, _lib.EVP_PKEY_free) |
Paul Kehrer | 32fc4e6 | 2016-06-03 15:21:44 -0700 | [diff] [blame] | 2697 | pkey._only_public = True |
Cory Benfield | 6492f7c | 2015-10-27 16:57:58 +0900 | [diff] [blame] | 2698 | return pkey |
| 2699 | |
| 2700 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 2701 | def load_privatekey(type, buffer, passphrase=None): |
| 2702 | """ |
| 2703 | Load a private key from a buffer |
| 2704 | |
| 2705 | :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1) |
| 2706 | :param buffer: The buffer the key is stored in |
| 2707 | :param passphrase: (optional) if encrypted PEM format, this can be |
| 2708 | either the passphrase to use, or a callback for |
| 2709 | providing the passphrase. |
| 2710 | |
| 2711 | :return: The PKey object |
| 2712 | """ |
Jean-Paul Calderone | 6922a86 | 2014-01-18 10:38:28 -0500 | [diff] [blame] | 2713 | if isinstance(buffer, _text_type): |
| 2714 | buffer = buffer.encode("ascii") |
| 2715 | |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 2716 | bio = _new_mem_buf(buffer) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 2717 | |
Jean-Paul Calderone | 23478b3 | 2013-02-20 13:31:38 -0800 | [diff] [blame] | 2718 | helper = _PassphraseHelper(type, passphrase) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 2719 | if type == FILETYPE_PEM: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2720 | evp_pkey = _lib.PEM_read_bio_PrivateKey( |
| 2721 | bio, _ffi.NULL, helper.callback, helper.callback_args) |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2722 | helper.raise_if_problem() |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 2723 | elif type == FILETYPE_ASN1: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2724 | evp_pkey = _lib.d2i_PrivateKey_bio(bio, _ffi.NULL) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 2725 | else: |
| 2726 | raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1") |
| 2727 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2728 | if evp_pkey == _ffi.NULL: |
Jean-Paul Calderone | 31393aa | 2013-02-20 13:22:21 -0800 | [diff] [blame] | 2729 | _raise_current_error() |
| 2730 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 2731 | pkey = PKey.__new__(PKey) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2732 | pkey._pkey = _ffi.gc(evp_pkey, _lib.EVP_PKEY_free) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 2733 | return pkey |
| 2734 | |
| 2735 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 2736 | def dump_certificate_request(type, req): |
| 2737 | """ |
| 2738 | Dump a certificate request to a buffer |
| 2739 | |
| 2740 | :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1) |
| 2741 | :param req: The certificate request to dump |
| 2742 | :return: The buffer with the dumped certificate request in |
| 2743 | """ |
Jean-Paul Calderone | ef9a3dc | 2013-03-02 16:33:32 -0800 | [diff] [blame] | 2744 | bio = _new_mem_buf() |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2745 | |
| 2746 | if type == FILETYPE_PEM: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2747 | result_code = _lib.PEM_write_bio_X509_REQ(bio, req._req) |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2748 | elif type == FILETYPE_ASN1: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2749 | result_code = _lib.i2d_X509_REQ_bio(bio, req._req) |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2750 | elif type == FILETYPE_TEXT: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2751 | result_code = _lib.X509_REQ_print_ex(bio, req._req, 0, 0) |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2752 | else: |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 2753 | raise ValueError( |
| 2754 | "type argument must be FILETYPE_PEM, FILETYPE_ASN1, or " |
| 2755 | "FILETYPE_TEXT" |
| 2756 | ) |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2757 | |
Alex Gaynor | 09a386e | 2016-07-03 09:32:44 -0400 | [diff] [blame] | 2758 | _openssl_assert(result_code != 0) |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2759 | |
| 2760 | return _bio_to_string(bio) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 2761 | |
| 2762 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 2763 | def load_certificate_request(type, buffer): |
| 2764 | """ |
| 2765 | Load a certificate request from a buffer |
| 2766 | |
| 2767 | :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1) |
| 2768 | :param buffer: The buffer the certificate request is stored in |
| 2769 | :return: The X509Req object |
| 2770 | """ |
Jean-Paul Calderone | 6922a86 | 2014-01-18 10:38:28 -0500 | [diff] [blame] | 2771 | if isinstance(buffer, _text_type): |
| 2772 | buffer = buffer.encode("ascii") |
| 2773 | |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 2774 | bio = _new_mem_buf(buffer) |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2775 | |
| 2776 | if type == FILETYPE_PEM: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2777 | req = _lib.PEM_read_bio_X509_REQ(bio, _ffi.NULL, _ffi.NULL, _ffi.NULL) |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2778 | elif type == FILETYPE_ASN1: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2779 | req = _lib.d2i_X509_REQ_bio(bio, _ffi.NULL) |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2780 | else: |
Jean-Paul Calderone | 4a68b40 | 2013-12-29 16:54:58 -0500 | [diff] [blame] | 2781 | raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1") |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2782 | |
Alex Gaynor | add5b07 | 2016-06-04 21:04:00 -0700 | [diff] [blame] | 2783 | _openssl_assert(req != _ffi.NULL) |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2784 | |
| 2785 | x509req = X509Req.__new__(X509Req) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2786 | x509req._req = _ffi.gc(req, _lib.X509_REQ_free) |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2787 | return x509req |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2788 | |
| 2789 | |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2790 | def sign(pkey, data, digest): |
| 2791 | """ |
| 2792 | Sign data with a digest |
| 2793 | |
| 2794 | :param pkey: Pkey to sign with |
| 2795 | :param data: data to be signed |
| 2796 | :param digest: message digest to use |
| 2797 | :return: signature |
| 2798 | """ |
Jean-Paul Calderone | 39a8d59 | 2015-04-13 20:49:50 -0400 | [diff] [blame] | 2799 | data = _text_to_bytes_and_warn("data", data) |
Abraham Martin | e82326c | 2015-02-04 10:18:10 +0000 | [diff] [blame] | 2800 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2801 | digest_obj = _lib.EVP_get_digestbyname(_byte_string(digest)) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2802 | if digest_obj == _ffi.NULL: |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2803 | raise ValueError("No such digest method") |
| 2804 | |
Alex Gaynor | 67903a6 | 2016-06-02 10:37:13 -0700 | [diff] [blame] | 2805 | md_ctx = _lib.Cryptography_EVP_MD_CTX_new() |
Alex Gaynor | 1f9d4de | 2016-06-02 11:01:52 -0700 | [diff] [blame] | 2806 | md_ctx = _ffi.gc(md_ctx, _lib.Cryptography_EVP_MD_CTX_free) |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2807 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2808 | _lib.EVP_SignInit(md_ctx, digest_obj) |
| 2809 | _lib.EVP_SignUpdate(md_ctx, data, len(data)) |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2810 | |
Paul Kehrer | 59d2625 | 2017-07-20 10:45:54 +0200 | [diff] [blame] | 2811 | length = _lib.EVP_PKEY_size(pkey._pkey) |
| 2812 | _openssl_assert(length > 0) |
| 2813 | signature_buffer = _ffi.new("unsigned char[]", length) |
| 2814 | signature_length = _ffi.new("unsigned int *") |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2815 | final_result = _lib.EVP_SignFinal( |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2816 | md_ctx, signature_buffer, signature_length, pkey._pkey) |
Alex Gaynor | 09a386e | 2016-07-03 09:32:44 -0400 | [diff] [blame] | 2817 | _openssl_assert(final_result == 1) |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2818 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2819 | return _ffi.buffer(signature_buffer, signature_length[0])[:] |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2820 | |
| 2821 | |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2822 | def verify(cert, signature, data, digest): |
| 2823 | """ |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 2824 | Verify a signature. |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2825 | |
| 2826 | :param cert: signing certificate (X509 object) |
| 2827 | :param signature: signature returned by sign function |
| 2828 | :param data: data to be verified |
| 2829 | :param digest: message digest to use |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2830 | :return: ``None`` if the signature is correct, raise exception otherwise. |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2831 | """ |
Jean-Paul Calderone | 39a8d59 | 2015-04-13 20:49:50 -0400 | [diff] [blame] | 2832 | data = _text_to_bytes_and_warn("data", data) |
Abraham Martin | e82326c | 2015-02-04 10:18:10 +0000 | [diff] [blame] | 2833 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2834 | digest_obj = _lib.EVP_get_digestbyname(_byte_string(digest)) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2835 | if digest_obj == _ffi.NULL: |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2836 | raise ValueError("No such digest method") |
| 2837 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2838 | pkey = _lib.X509_get_pubkey(cert._x509) |
Alex Gaynor | add5b07 | 2016-06-04 21:04:00 -0700 | [diff] [blame] | 2839 | _openssl_assert(pkey != _ffi.NULL) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2840 | pkey = _ffi.gc(pkey, _lib.EVP_PKEY_free) |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2841 | |
Alex Gaynor | 67903a6 | 2016-06-02 10:37:13 -0700 | [diff] [blame] | 2842 | md_ctx = _lib.Cryptography_EVP_MD_CTX_new() |
Alex Gaynor | 1f9d4de | 2016-06-02 11:01:52 -0700 | [diff] [blame] | 2843 | md_ctx = _ffi.gc(md_ctx, _lib.Cryptography_EVP_MD_CTX_free) |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2844 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2845 | _lib.EVP_VerifyInit(md_ctx, digest_obj) |
| 2846 | _lib.EVP_VerifyUpdate(md_ctx, data, len(data)) |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 2847 | verify_result = _lib.EVP_VerifyFinal( |
| 2848 | md_ctx, signature, len(signature), pkey |
| 2849 | ) |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2850 | |
| 2851 | if verify_result != 1: |
| 2852 | _raise_current_error() |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2853 | |
| 2854 | |
Dominic Chen | f05b212 | 2015-10-13 16:32:35 +0000 | [diff] [blame] | 2855 | def dump_crl(type, crl): |
| 2856 | """ |
| 2857 | Dump a certificate revocation list to a buffer. |
| 2858 | |
| 2859 | :param type: The file type (one of ``FILETYPE_PEM``, ``FILETYPE_ASN1``, or |
| 2860 | ``FILETYPE_TEXT``). |
Hynek Schlawack | 0a3cd6d | 2015-10-21 16:39:22 +0200 | [diff] [blame] | 2861 | :param CRL crl: The CRL to dump. |
| 2862 | |
Dominic Chen | f05b212 | 2015-10-13 16:32:35 +0000 | [diff] [blame] | 2863 | :return: The buffer with the CRL. |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 2864 | :rtype: bytes |
Dominic Chen | f05b212 | 2015-10-13 16:32:35 +0000 | [diff] [blame] | 2865 | """ |
| 2866 | bio = _new_mem_buf() |
| 2867 | |
| 2868 | if type == FILETYPE_PEM: |
| 2869 | ret = _lib.PEM_write_bio_X509_CRL(bio, crl._crl) |
| 2870 | elif type == FILETYPE_ASN1: |
| 2871 | ret = _lib.i2d_X509_CRL_bio(bio, crl._crl) |
| 2872 | elif type == FILETYPE_TEXT: |
| 2873 | ret = _lib.X509_CRL_print(bio, crl._crl) |
| 2874 | else: |
| 2875 | raise ValueError( |
| 2876 | "type argument must be FILETYPE_PEM, FILETYPE_ASN1, or " |
| 2877 | "FILETYPE_TEXT") |
| 2878 | |
| 2879 | assert ret == 1 |
| 2880 | return _bio_to_string(bio) |
| 2881 | |
| 2882 | |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2883 | def load_crl(type, buffer): |
| 2884 | """ |
| 2885 | Load a certificate revocation list from a buffer |
| 2886 | |
| 2887 | :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1) |
| 2888 | :param buffer: The buffer the CRL is stored in |
| 2889 | |
| 2890 | :return: The PKey object |
| 2891 | """ |
Jean-Paul Calderone | 6922a86 | 2014-01-18 10:38:28 -0500 | [diff] [blame] | 2892 | if isinstance(buffer, _text_type): |
| 2893 | buffer = buffer.encode("ascii") |
| 2894 | |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 2895 | bio = _new_mem_buf(buffer) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2896 | |
| 2897 | if type == FILETYPE_PEM: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2898 | crl = _lib.PEM_read_bio_X509_CRL(bio, _ffi.NULL, _ffi.NULL, _ffi.NULL) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2899 | elif type == FILETYPE_ASN1: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2900 | crl = _lib.d2i_X509_CRL_bio(bio, _ffi.NULL) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2901 | else: |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2902 | raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1") |
| 2903 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2904 | if crl == _ffi.NULL: |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2905 | _raise_current_error() |
| 2906 | |
| 2907 | result = CRL.__new__(CRL) |
Jeremy Cline | 9e15eca | 2017-09-07 20:11:08 -0400 | [diff] [blame] | 2908 | result._crl = _ffi.gc(crl, _lib.X509_CRL_free) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2909 | return result |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2910 | |
| 2911 | |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2912 | def load_pkcs7_data(type, buffer): |
| 2913 | """ |
| 2914 | Load pkcs7 data from a buffer |
| 2915 | |
| 2916 | :param type: The file type (one of FILETYPE_PEM or FILETYPE_ASN1) |
| 2917 | :param buffer: The buffer with the pkcs7 data. |
| 2918 | :return: The PKCS7 object |
| 2919 | """ |
Jean-Paul Calderone | 6922a86 | 2014-01-18 10:38:28 -0500 | [diff] [blame] | 2920 | if isinstance(buffer, _text_type): |
| 2921 | buffer = buffer.encode("ascii") |
| 2922 | |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 2923 | bio = _new_mem_buf(buffer) |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2924 | |
| 2925 | if type == FILETYPE_PEM: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2926 | pkcs7 = _lib.PEM_read_bio_PKCS7(bio, _ffi.NULL, _ffi.NULL, _ffi.NULL) |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2927 | elif type == FILETYPE_ASN1: |
Alex Gaynor | 77acc36 | 2014-08-13 14:46:15 -0700 | [diff] [blame] | 2928 | pkcs7 = _lib.d2i_PKCS7_bio(bio, _ffi.NULL) |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2929 | else: |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2930 | raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1") |
| 2931 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2932 | if pkcs7 == _ffi.NULL: |
Jean-Paul Calderone | b0f6471 | 2013-03-03 10:15:39 -0800 | [diff] [blame] | 2933 | _raise_current_error() |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2934 | |
| 2935 | pypkcs7 = PKCS7.__new__(PKCS7) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2936 | pypkcs7._pkcs7 = _ffi.gc(pkcs7, _lib.PKCS7_free) |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2937 | return pypkcs7 |
| 2938 | |
| 2939 | |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2940 | def load_pkcs12(buffer, passphrase=None): |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2941 | """ |
| 2942 | Load a PKCS12 object from a buffer |
| 2943 | |
| 2944 | :param buffer: The buffer the certificate is stored in |
| 2945 | :param passphrase: (Optional) The password to decrypt the PKCS12 lump |
| 2946 | :returns: The PKCS12 object |
| 2947 | """ |
Jean-Paul Calderone | 39a8d59 | 2015-04-13 20:49:50 -0400 | [diff] [blame] | 2948 | passphrase = _text_to_bytes_and_warn("passphrase", passphrase) |
Abraham Martin | e82326c | 2015-02-04 10:18:10 +0000 | [diff] [blame] | 2949 | |
Jean-Paul Calderone | 6922a86 | 2014-01-18 10:38:28 -0500 | [diff] [blame] | 2950 | if isinstance(buffer, _text_type): |
| 2951 | buffer = buffer.encode("ascii") |
| 2952 | |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 2953 | bio = _new_mem_buf(buffer) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2954 | |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2955 | # Use null passphrase if passphrase is None or empty string. With PKCS#12 |
| 2956 | # password based encryption no password and a zero length password are two |
| 2957 | # different things, but OpenSSL implementation will try both to figure out |
| 2958 | # which one works. |
| 2959 | if not passphrase: |
| 2960 | passphrase = _ffi.NULL |
| 2961 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2962 | p12 = _lib.d2i_PKCS12_bio(bio, _ffi.NULL) |
| 2963 | if p12 == _ffi.NULL: |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2964 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2965 | p12 = _ffi.gc(p12, _lib.PKCS12_free) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2966 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2967 | pkey = _ffi.new("EVP_PKEY**") |
| 2968 | cert = _ffi.new("X509**") |
| 2969 | cacerts = _ffi.new("Cryptography_STACK_OF_X509**") |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2970 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2971 | parse_result = _lib.PKCS12_parse(p12, passphrase, pkey, cert, cacerts) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2972 | if not parse_result: |
| 2973 | _raise_current_error() |
| 2974 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2975 | cacerts = _ffi.gc(cacerts[0], _lib.sk_X509_free) |
Jean-Paul Calderone | ef9a3dc | 2013-03-02 16:33:32 -0800 | [diff] [blame] | 2976 | |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2977 | # openssl 1.0.0 sometimes leaves an X509_check_private_key error in the |
| 2978 | # queue for no particular reason. This error isn't interesting to anyone |
| 2979 | # outside this function. It's not even interesting to us. Get rid of it. |
| 2980 | try: |
| 2981 | _raise_current_error() |
| 2982 | except Error: |
| 2983 | pass |
| 2984 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2985 | if pkey[0] == _ffi.NULL: |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2986 | pykey = None |
| 2987 | else: |
| 2988 | pykey = PKey.__new__(PKey) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2989 | pykey._pkey = _ffi.gc(pkey[0], _lib.EVP_PKEY_free) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2990 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2991 | if cert[0] == _ffi.NULL: |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2992 | pycert = None |
| 2993 | friendlyname = None |
| 2994 | else: |
| 2995 | pycert = X509.__new__(X509) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2996 | pycert._x509 = _ffi.gc(cert[0], _lib.X509_free) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2997 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2998 | friendlyname_length = _ffi.new("int*") |
Alex Gaynor | 5945ea8 | 2015-09-05 14:59:06 -0400 | [diff] [blame] | 2999 | friendlyname_buffer = _lib.X509_alias_get0( |
| 3000 | cert[0], friendlyname_length |
| 3001 | ) |
| 3002 | friendlyname = _ffi.buffer( |
| 3003 | friendlyname_buffer, friendlyname_length[0] |
| 3004 | )[:] |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 3005 | if friendlyname_buffer == _ffi.NULL: |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 3006 | friendlyname = None |
| 3007 | |
| 3008 | pycacerts = [] |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 3009 | for i in range(_lib.sk_X509_num(cacerts)): |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 3010 | pycacert = X509.__new__(X509) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 3011 | pycacert._x509 = _lib.sk_X509_value(cacerts, i) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 3012 | pycacerts.append(pycacert) |
| 3013 | if not pycacerts: |
| 3014 | pycacerts = None |
| 3015 | |
| 3016 | pkcs12 = PKCS12.__new__(PKCS12) |
| 3017 | pkcs12._pkey = pykey |
| 3018 | pkcs12._cert = pycert |
| 3019 | pkcs12._cacerts = pycacerts |
| 3020 | pkcs12._friendlyname = friendlyname |
| 3021 | return pkcs12 |
Jean-Paul Calderone | 6bb4089 | 2014-01-01 12:21:34 -0500 | [diff] [blame] | 3022 | |
| 3023 | |
Jean-Paul Calderone | b64e2a2 | 2014-01-11 08:06:35 -0500 | [diff] [blame] | 3024 | # There are no direct unit tests for this initialization. It is tested |
| 3025 | # indirectly since it is necessary for functions like dump_privatekey when |
| 3026 | # using encryption. |
| 3027 | # |
| 3028 | # Thus OpenSSL.test.test_crypto.FunctionTests.test_dump_privatekey_passphrase |
| 3029 | # and some other similar tests may fail without this (though they may not if |
| 3030 | # the Python runtime has already done some initialization of the underlying |
| 3031 | # OpenSSL library (and is linked against the same one that cryptography is |
| 3032 | # using)). |
Jean-Paul Calderone | e324fd6 | 2014-01-11 08:00:33 -0500 | [diff] [blame] | 3033 | _lib.OpenSSL_add_all_algorithms() |
Jean-Paul Calderone | 11ed8e8 | 2014-01-18 10:21:50 -0500 | [diff] [blame] | 3034 | |
Jean-Paul Calderone | fab157b | 2014-01-18 11:21:38 -0500 | [diff] [blame] | 3035 | # This is similar but exercised mainly by exception_from_error_queue. It calls |
| 3036 | # both ERR_load_crypto_strings() and ERR_load_SSL_strings(). |
| 3037 | _lib.SSL_load_error_strings() |
D.S. Ljungmark | 349e136 | 2014-05-31 18:40:38 +0200 | [diff] [blame] | 3038 | |
| 3039 | |
D.S. Ljungmark | 349e136 | 2014-05-31 18:40:38 +0200 | [diff] [blame] | 3040 | # Set the default string mask to match OpenSSL upstream (since 2005) and |
| 3041 | # RFC5280 recommendations. |
| 3042 | _lib.ASN1_STRING_set_default_mask_asc(b'utf8only') |