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