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