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