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