Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1 | from time import time |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2 | from base64 import b16encode |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 3 | from functools import partial |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 4 | from operator import __eq__, __ne__, __lt__, __le__, __gt__, __ge__ |
| 5 | |
| 6 | from six import ( |
| 7 | integer_types as _integer_types, |
| 8 | text_type as _text_type) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 9 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 10 | from OpenSSL._util import ( |
| 11 | ffi as _ffi, |
| 12 | lib as _lib, |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 13 | exception_from_error_queue as _exception_from_error_queue, |
| 14 | byte_string as _byte_string, |
| 15 | native as _native) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 16 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 17 | FILETYPE_PEM = _lib.SSL_FILETYPE_PEM |
| 18 | FILETYPE_ASN1 = _lib.SSL_FILETYPE_ASN1 |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 19 | |
| 20 | # TODO This was an API mistake. OpenSSL has no such constant. |
| 21 | FILETYPE_TEXT = 2 ** 16 - 1 |
| 22 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 23 | TYPE_RSA = _lib.EVP_PKEY_RSA |
| 24 | TYPE_DSA = _lib.EVP_PKEY_DSA |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 25 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 26 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 27 | class Error(Exception): |
Jean-Paul Calderone | 511cde0 | 2013-12-29 10:31:13 -0500 | [diff] [blame] | 28 | """ |
| 29 | An error occurred in an `OpenSSL.crypto` API. |
| 30 | """ |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 31 | |
| 32 | |
| 33 | _raise_current_error = partial(_exception_from_error_queue, Error) |
| 34 | |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 35 | def _untested_error(where): |
| 36 | """ |
| 37 | An OpenSSL API failed somehow. Additionally, the failure which was |
| 38 | encountered isn't one that's exercised by the test suite so future behavior |
| 39 | of pyOpenSSL is now somewhat less predictable. |
| 40 | """ |
| 41 | raise RuntimeError("Unknown %s failure" % (where,)) |
| 42 | |
| 43 | |
| 44 | |
| 45 | def _new_mem_buf(buffer=None): |
| 46 | """ |
| 47 | Allocate a new OpenSSL memory BIO. |
| 48 | |
| 49 | Arrange for the garbage collector to clean it up automatically. |
| 50 | |
| 51 | :param buffer: None or some bytes to use to put into the BIO so that they |
| 52 | can be read out. |
| 53 | """ |
| 54 | if buffer is None: |
| 55 | bio = _lib.BIO_new(_lib.BIO_s_mem()) |
| 56 | free = _lib.BIO_free |
| 57 | else: |
| 58 | data = _ffi.new("char[]", buffer) |
| 59 | bio = _lib.BIO_new_mem_buf(data, len(buffer)) |
| 60 | # Keep the memory alive as long as the bio is alive! |
| 61 | def free(bio, ref=data): |
| 62 | return _lib.BIO_free(bio) |
| 63 | |
| 64 | if bio == _ffi.NULL: |
| 65 | # TODO: This is untested. |
| 66 | _raise_current_error() |
| 67 | |
| 68 | bio = _ffi.gc(bio, free) |
| 69 | return bio |
| 70 | |
| 71 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 72 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 73 | def _bio_to_string(bio): |
| 74 | """ |
| 75 | Copy the contents of an OpenSSL BIO object into a Python byte string. |
| 76 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 77 | result_buffer = _ffi.new('char**') |
| 78 | buffer_length = _lib.BIO_get_mem_data(bio, result_buffer) |
| 79 | return _ffi.buffer(result_buffer[0], buffer_length)[:] |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 80 | |
| 81 | |
| 82 | |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 83 | def _set_asn1_time(boundary, when): |
Jean-Paul Calderone | e728e87 | 2013-12-29 10:37:15 -0500 | [diff] [blame] | 84 | """ |
| 85 | The the time value of an ASN1 time object. |
| 86 | |
| 87 | @param boundary: An ASN1_GENERALIZEDTIME pointer (or an object safely |
| 88 | castable to that type) which will have its value set. |
| 89 | @param when: A string representation of the desired time value. |
| 90 | |
| 91 | @raise TypeError: If C{when} is not a L{bytes} string. |
| 92 | @raise ValueError: If C{when} does not represent a time in the required |
| 93 | format. |
| 94 | @raise RuntimeError: If the time value cannot be set for some other |
| 95 | (unspecified) reason. |
| 96 | """ |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 97 | if not isinstance(when, bytes): |
| 98 | raise TypeError("when must be a byte string") |
| 99 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 100 | set_result = _lib.ASN1_GENERALIZEDTIME_set_string( |
| 101 | _ffi.cast('ASN1_GENERALIZEDTIME*', boundary), when) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 102 | if set_result == 0: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 103 | dummy = _ffi.gc(_lib.ASN1_STRING_new(), _lib.ASN1_STRING_free) |
| 104 | _lib.ASN1_STRING_set(dummy, when, len(when)) |
| 105 | check_result = _lib.ASN1_GENERALIZEDTIME_check( |
| 106 | _ffi.cast('ASN1_GENERALIZEDTIME*', dummy)) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 107 | if not check_result: |
| 108 | raise ValueError("Invalid string") |
| 109 | else: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 110 | _untested_error() |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 111 | |
| 112 | |
| 113 | |
| 114 | def _get_asn1_time(timestamp): |
Jean-Paul Calderone | e728e87 | 2013-12-29 10:37:15 -0500 | [diff] [blame] | 115 | """ |
| 116 | Retrieve the time value of an ASN1 time object. |
| 117 | |
| 118 | @param timestamp: An ASN1_GENERALIZEDTIME* (or an object safely castable to |
| 119 | that type) from which the time value will be retrieved. |
| 120 | |
| 121 | @return: The time value from C{timestamp} as a L{bytes} string in a certain |
| 122 | format. Or C{None} if the object contains no time value. |
| 123 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 124 | string_timestamp = _ffi.cast('ASN1_STRING*', timestamp) |
| 125 | if _lib.ASN1_STRING_length(string_timestamp) == 0: |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 126 | return None |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 127 | elif _lib.ASN1_STRING_type(string_timestamp) == _lib.V_ASN1_GENERALIZEDTIME: |
| 128 | return _ffi.string(_lib.ASN1_STRING_data(string_timestamp)) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 129 | else: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 130 | generalized_timestamp = _ffi.new("ASN1_GENERALIZEDTIME**") |
| 131 | _lib.ASN1_TIME_to_generalizedtime(timestamp, generalized_timestamp) |
| 132 | if generalized_timestamp[0] == _ffi.NULL: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 133 | # This may happen: |
| 134 | # - if timestamp was not an ASN1_TIME |
| 135 | # - if allocating memory for the ASN1_GENERALIZEDTIME failed |
| 136 | # - if a copy of the time data from timestamp cannot be made for |
| 137 | # the newly allocated ASN1_GENERALIZEDTIME |
| 138 | # |
| 139 | # These are difficult to test. cffi enforces the ASN1_TIME type. |
| 140 | # Memory allocation failures are a pain to trigger |
| 141 | # deterministically. |
| 142 | _untested_error("ASN1_TIME_to_generalizedtime") |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 143 | else: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 144 | string_timestamp = _ffi.cast( |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 145 | "ASN1_STRING*", generalized_timestamp[0]) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 146 | string_data = _lib.ASN1_STRING_data(string_timestamp) |
| 147 | string_result = _ffi.string(string_data) |
| 148 | _lib.ASN1_GENERALIZEDTIME_free(generalized_timestamp[0]) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 149 | return string_result |
| 150 | |
| 151 | |
| 152 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 153 | class PKey(object): |
Jean-Paul Calderone | edafced | 2013-02-19 11:48:38 -0800 | [diff] [blame] | 154 | _only_public = False |
Jean-Paul Calderone | 09e3bdc | 2013-02-19 12:15:28 -0800 | [diff] [blame] | 155 | _initialized = True |
Jean-Paul Calderone | edafced | 2013-02-19 11:48:38 -0800 | [diff] [blame] | 156 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 157 | def __init__(self): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 158 | pkey = _lib.EVP_PKEY_new() |
| 159 | self._pkey = _ffi.gc(pkey, _lib.EVP_PKEY_free) |
Jean-Paul Calderone | 09e3bdc | 2013-02-19 12:15:28 -0800 | [diff] [blame] | 160 | self._initialized = False |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 161 | |
| 162 | |
| 163 | def generate_key(self, type, bits): |
| 164 | """ |
| 165 | Generate a key of a given type, with a given number of a bits |
| 166 | |
| 167 | :param type: The key type (TYPE_RSA or TYPE_DSA) |
| 168 | :param bits: The number of bits |
| 169 | |
| 170 | :return: None |
| 171 | """ |
| 172 | if not isinstance(type, int): |
| 173 | raise TypeError("type must be an integer") |
| 174 | |
| 175 | if not isinstance(bits, int): |
| 176 | raise TypeError("bits must be an integer") |
| 177 | |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 178 | # TODO Check error return |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 179 | exponent = _lib.BN_new() |
| 180 | exponent = _ffi.gc(exponent, _lib.BN_free) |
| 181 | _lib.BN_set_word(exponent, _lib.RSA_F4) |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 182 | |
| 183 | if type == TYPE_RSA: |
| 184 | if bits <= 0: |
| 185 | raise ValueError("Invalid number of bits") |
| 186 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 187 | rsa = _lib.RSA_new() |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 188 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 189 | result = _lib.RSA_generate_key_ex(rsa, bits, exponent, _ffi.NULL) |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 190 | if result == 0: |
| 191 | # TODO: The test for this case is commented out. Different |
| 192 | # builds of OpenSSL appear to have different failure modes that |
| 193 | # make it hard to test. Visual inspection of the OpenSSL |
| 194 | # source reveals that a return value of 0 signals an error. |
| 195 | # Manual testing on a particular build of OpenSSL suggests that |
| 196 | # this is probably the appropriate way to handle those errors. |
| 197 | _raise_current_error() |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 198 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 199 | result = _lib.EVP_PKEY_assign_RSA(self._pkey, rsa) |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 200 | if not result: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 201 | # TODO: It appears as though this can fail if an engine is in |
| 202 | # use which does not support RSA. |
| 203 | _raise_current_error() |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 204 | |
| 205 | elif type == TYPE_DSA: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 206 | dsa = _lib.DSA_generate_parameters( |
| 207 | bits, _ffi.NULL, 0, _ffi.NULL, _ffi.NULL, _ffi.NULL, _ffi.NULL) |
| 208 | if dsa == _ffi.NULL: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 209 | # TODO: This is untested. |
| 210 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 211 | if not _lib.DSA_generate_key(dsa): |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 212 | # TODO: This is untested. |
| 213 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 214 | if not _lib.EVP_PKEY_assign_DSA(self._pkey, dsa): |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 215 | # TODO: This is untested. |
| 216 | _raise_current_error() |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 217 | else: |
| 218 | raise Error("No such key type") |
| 219 | |
Jean-Paul Calderone | 09e3bdc | 2013-02-19 12:15:28 -0800 | [diff] [blame] | 220 | self._initialized = True |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 221 | |
| 222 | |
| 223 | def check(self): |
| 224 | """ |
| 225 | Check the consistency of an RSA private key. |
| 226 | |
| 227 | :return: True if key is consistent. |
| 228 | :raise Error: if the key is inconsistent. |
| 229 | :raise TypeError: if the key is of a type which cannot be checked. |
| 230 | Only RSA keys can currently be checked. |
| 231 | """ |
Jean-Paul Calderone | c86fcaf | 2013-02-20 12:38:33 -0800 | [diff] [blame] | 232 | if self._only_public: |
| 233 | raise TypeError("public key only") |
| 234 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 235 | if _lib.EVP_PKEY_type(self._pkey.type) != _lib.EVP_PKEY_RSA: |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 236 | raise TypeError("key type unsupported") |
| 237 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 238 | rsa = _lib.EVP_PKEY_get1_RSA(self._pkey) |
| 239 | rsa = _ffi.gc(rsa, _lib.RSA_free) |
| 240 | result = _lib.RSA_check_key(rsa) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 241 | if result: |
| 242 | return True |
| 243 | _raise_current_error() |
| 244 | |
| 245 | |
Jean-Paul Calderone | c86fcaf | 2013-02-20 12:38:33 -0800 | [diff] [blame] | 246 | def type(self): |
| 247 | """ |
| 248 | Returns the type of the key |
| 249 | |
| 250 | :return: The type of the key. |
| 251 | """ |
| 252 | return self._pkey.type |
| 253 | |
| 254 | |
| 255 | def bits(self): |
| 256 | """ |
| 257 | Returns the number of bits of the key |
| 258 | |
| 259 | :return: The number of bits of the key. |
| 260 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 261 | return _lib.EVP_PKEY_bits(self._pkey) |
Jean-Paul Calderone | c86fcaf | 2013-02-20 12:38:33 -0800 | [diff] [blame] | 262 | PKeyType = PKey |
| 263 | |
| 264 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 265 | |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 266 | class _EllipticCurve(object): |
| 267 | """ |
Jean-Paul Calderone | aaf516d | 2014-04-19 09:10:45 -0400 | [diff] [blame] | 268 | A representation of a supported elliptic curve. |
Jean-Paul Calderone | 73945e3 | 2014-04-30 18:18:01 -0400 | [diff] [blame^] | 269 | |
| 270 | @cvar _curves: :py:obj:`None` until an attempt is made to load the curves. |
| 271 | Thereafter, a :py:type:`set` containing :py:type:`_EllipticCurve` |
| 272 | instances each of which represents one curve supported by the system. |
| 273 | @type _curves: :py:type:`NoneType` or :py:type:`set` |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 274 | """ |
Jean-Paul Calderone | 73945e3 | 2014-04-30 18:18:01 -0400 | [diff] [blame^] | 275 | _curves = None |
| 276 | |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 277 | @classmethod |
Jean-Paul Calderone | 73945e3 | 2014-04-30 18:18:01 -0400 | [diff] [blame^] | 278 | def _load_elliptic_curves(cls, lib): |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 279 | """ |
Jean-Paul Calderone | 73945e3 | 2014-04-30 18:18:01 -0400 | [diff] [blame^] | 280 | Get the curves supported by OpenSSL. |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 281 | |
| 282 | :param lib: The OpenSSL library binding object. |
Jean-Paul Calderone | 73945e3 | 2014-04-30 18:18:01 -0400 | [diff] [blame^] | 283 | |
| 284 | :return: A :py:type:`set` of ``cls`` instances giving the names of the |
| 285 | elliptic curves the underlying library supports. |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 286 | """ |
| 287 | if lib.Cryptography_HAS_EC: |
| 288 | num_curves = lib.EC_get_builtin_curves(_ffi.NULL, 0) |
| 289 | builtin_curves = _ffi.new('EC_builtin_curve[]', num_curves) |
| 290 | # The return value on this call should be num_curves again. We could |
| 291 | # check it to make sure but if it *isn't* then.. what could we do? |
| 292 | # Abort the whole process, I suppose...? -exarkun |
| 293 | lib.EC_get_builtin_curves(builtin_curves, num_curves) |
| 294 | return set( |
| 295 | cls.from_nid(lib, c.nid) |
| 296 | for c in builtin_curves) |
Jean-Paul Calderone | 73945e3 | 2014-04-30 18:18:01 -0400 | [diff] [blame^] | 297 | return set() |
| 298 | |
| 299 | |
| 300 | @classmethod |
| 301 | def _get_elliptic_curves(cls, lib): |
| 302 | """ |
| 303 | Get, cache, and return the curves supported by OpenSSL. |
| 304 | |
| 305 | :param lib: The OpenSSL library binding object. |
| 306 | |
| 307 | :return: A :py:type:`set` of ``cls`` instances giving the names of the |
| 308 | elliptic curves the underlying library supports. |
| 309 | """ |
| 310 | if cls._curves is None: |
| 311 | cls._curves = cls._load_elliptic_curves(lib) |
| 312 | return cls._curves |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 313 | |
| 314 | |
| 315 | @classmethod |
| 316 | def from_nid(cls, lib, nid): |
Jean-Paul Calderone | aaf516d | 2014-04-19 09:10:45 -0400 | [diff] [blame] | 317 | """ |
| 318 | Instantiate a new :py:class:`_EllipticCurve` associated with the given |
| 319 | OpenSSL NID. |
| 320 | |
| 321 | :param lib: The OpenSSL library binding object. |
| 322 | |
| 323 | :param nid: The OpenSSL NID the resulting curve object will represent. |
| 324 | This must be a curve NID (and not, for example, a hash NID) or |
| 325 | subsequent operations will fail in unpredictable ways. |
| 326 | :type nid: :py:class:`int` |
| 327 | |
| 328 | :return: The curve object. |
| 329 | """ |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 330 | return cls(lib, nid, _ffi.string(lib.OBJ_nid2sn(nid)).decode("ascii")) |
| 331 | |
| 332 | |
| 333 | def __init__(self, lib, nid, name): |
Jean-Paul Calderone | aaf516d | 2014-04-19 09:10:45 -0400 | [diff] [blame] | 334 | """ |
| 335 | :param _lib: The :py:mod:`cryptography` binding instance used to |
| 336 | interface with OpenSSL. |
| 337 | |
| 338 | :param _nid: The OpenSSL NID identifying the curve this object |
| 339 | represents. |
| 340 | :type _nid: :py:class:`int` |
| 341 | |
| 342 | :param name: The OpenSSL short name identifying the curve this object |
| 343 | represents. |
| 344 | :type name: :py:class:`unicode` |
| 345 | """ |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 346 | self._lib = lib |
| 347 | self._nid = nid |
| 348 | self.name = name |
| 349 | |
| 350 | |
| 351 | def __repr__(self): |
| 352 | return "<Curve %r>" % (self.name,) |
| 353 | |
| 354 | |
| 355 | def _to_EC_KEY(self): |
Jean-Paul Calderone | aaf516d | 2014-04-19 09:10:45 -0400 | [diff] [blame] | 356 | """ |
| 357 | Create a new OpenSSL EC_KEY structure initialized to use this curve. |
| 358 | |
| 359 | The structure is automatically garbage collected when the Python object |
| 360 | is garbage collected. |
| 361 | """ |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 362 | key = self._lib.EC_KEY_new_by_curve_name(self._nid) |
| 363 | return _ffi.gc(key, _lib.EC_KEY_free) |
| 364 | |
| 365 | |
| 366 | |
| 367 | def get_elliptic_curves(): |
Jean-Paul Calderone | aaf516d | 2014-04-19 09:10:45 -0400 | [diff] [blame] | 368 | """ |
| 369 | Return a set of objects representing the elliptic curves supported in the |
| 370 | OpenSSL build in use. |
| 371 | |
| 372 | The curve objects have a :py:class:`unicode` ``name`` attribute by which |
| 373 | they identify themselves. |
| 374 | |
| 375 | 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] | 376 | :py:meth:`Context.set_tmp_ecdh` to specify which elliptical curve should be |
| 377 | used for ECDHE key exchange. |
Jean-Paul Calderone | aaf516d | 2014-04-19 09:10:45 -0400 | [diff] [blame] | 378 | """ |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 379 | return _EllipticCurve._get_elliptic_curves(_lib) |
| 380 | |
| 381 | |
| 382 | |
| 383 | def get_elliptic_curve(name): |
Jean-Paul Calderone | aaf516d | 2014-04-19 09:10:45 -0400 | [diff] [blame] | 384 | """ |
| 385 | Return a single curve object selected by name. |
| 386 | |
| 387 | See :py:func:`get_elliptic_curves` for information about curve objects. |
| 388 | |
Jean-Paul Calderone | d5839e2 | 2014-04-19 09:26:44 -0400 | [diff] [blame] | 389 | :param name: The OpenSSL short name identifying the curve object to |
| 390 | retrieve. |
| 391 | :type name: :py:class:`unicode` |
| 392 | |
Jean-Paul Calderone | aaf516d | 2014-04-19 09:10:45 -0400 | [diff] [blame] | 393 | If the named curve is not supported then :py:class:`ValueError` is raised. |
| 394 | """ |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 395 | for curve in get_elliptic_curves(): |
| 396 | if curve.name == name: |
| 397 | return curve |
| 398 | raise ValueError("unknown curve name", name) |
| 399 | |
| 400 | |
| 401 | |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 402 | class X509Name(object): |
| 403 | def __init__(self, name): |
| 404 | """ |
| 405 | Create a new X509Name, copying the given X509Name instance. |
| 406 | |
| 407 | :param name: An X509Name object to copy |
| 408 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 409 | name = _lib.X509_NAME_dup(name._name) |
| 410 | self._name = _ffi.gc(name, _lib.X509_NAME_free) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 411 | |
| 412 | |
| 413 | def __setattr__(self, name, value): |
| 414 | if name.startswith('_'): |
| 415 | return super(X509Name, self).__setattr__(name, value) |
| 416 | |
Jean-Paul Calderone | ff363be | 2013-03-03 10:21:23 -0800 | [diff] [blame] | 417 | # Note: we really do not want str subclasses here, so we do not use |
| 418 | # isinstance. |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 419 | if type(name) is not str: |
| 420 | raise TypeError("attribute name must be string, not '%.200s'" % ( |
| 421 | type(value).__name__,)) |
| 422 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 423 | nid = _lib.OBJ_txt2nid(_byte_string(name)) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 424 | if nid == _lib.NID_undef: |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 425 | try: |
| 426 | _raise_current_error() |
| 427 | except Error: |
| 428 | pass |
| 429 | raise AttributeError("No such attribute") |
| 430 | |
| 431 | # If there's an old entry for this NID, remove it |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 432 | for i in range(_lib.X509_NAME_entry_count(self._name)): |
| 433 | ent = _lib.X509_NAME_get_entry(self._name, i) |
| 434 | ent_obj = _lib.X509_NAME_ENTRY_get_object(ent) |
| 435 | ent_nid = _lib.OBJ_obj2nid(ent_obj) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 436 | if nid == ent_nid: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 437 | ent = _lib.X509_NAME_delete_entry(self._name, i) |
| 438 | _lib.X509_NAME_ENTRY_free(ent) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 439 | break |
| 440 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 441 | if isinstance(value, _text_type): |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 442 | value = value.encode('utf-8') |
| 443 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 444 | add_result = _lib.X509_NAME_add_entry_by_NID( |
| 445 | self._name, nid, _lib.MBSTRING_UTF8, value, -1, -1, 0) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 446 | if not add_result: |
Jean-Paul Calderone | 5300d6a | 2013-12-29 16:36:50 -0500 | [diff] [blame] | 447 | _raise_current_error() |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 448 | |
| 449 | |
| 450 | def __getattr__(self, name): |
| 451 | """ |
| 452 | Find attribute. An X509Name object has the following attributes: |
| 453 | countryName (alias C), stateOrProvince (alias ST), locality (alias L), |
| 454 | organization (alias O), organizationalUnit (alias OU), commonName (alias |
| 455 | CN) and more... |
| 456 | """ |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 457 | nid = _lib.OBJ_txt2nid(_byte_string(name)) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 458 | if nid == _lib.NID_undef: |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 459 | # This is a bit weird. OBJ_txt2nid indicated failure, but it seems |
| 460 | # a lower level function, a2d_ASN1_OBJECT, also feels the need to |
| 461 | # push something onto the error queue. If we don't clean that up |
| 462 | # now, someone else will bump into it later and be quite confused. |
| 463 | # See lp#314814. |
| 464 | try: |
| 465 | _raise_current_error() |
| 466 | except Error: |
| 467 | pass |
| 468 | return super(X509Name, self).__getattr__(name) |
| 469 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 470 | 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] | 471 | if entry_index == -1: |
| 472 | return None |
| 473 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 474 | entry = _lib.X509_NAME_get_entry(self._name, entry_index) |
| 475 | data = _lib.X509_NAME_ENTRY_get_data(entry) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 476 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 477 | result_buffer = _ffi.new("unsigned char**") |
| 478 | data_length = _lib.ASN1_STRING_to_UTF8(result_buffer, data) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 479 | if data_length < 0: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 480 | # TODO: This is untested. |
| 481 | _raise_current_error() |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 482 | |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 483 | try: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 484 | result = _ffi.buffer(result_buffer[0], data_length)[:].decode('utf-8') |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 485 | finally: |
| 486 | # XXX untested |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 487 | _lib.OPENSSL_free(result_buffer[0]) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 488 | return result |
| 489 | |
| 490 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 491 | def _cmp(op): |
| 492 | def f(self, other): |
| 493 | if not isinstance(other, X509Name): |
| 494 | return NotImplemented |
| 495 | result = _lib.X509_NAME_cmp(self._name, other._name) |
| 496 | return op(result, 0) |
| 497 | return f |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 498 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 499 | __eq__ = _cmp(__eq__) |
| 500 | __ne__ = _cmp(__ne__) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 501 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 502 | __lt__ = _cmp(__lt__) |
| 503 | __le__ = _cmp(__le__) |
| 504 | |
| 505 | __gt__ = _cmp(__gt__) |
| 506 | __ge__ = _cmp(__ge__) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 507 | |
| 508 | def __repr__(self): |
| 509 | """ |
| 510 | String representation of an X509Name |
| 511 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 512 | result_buffer = _ffi.new("char[]", 512); |
| 513 | format_result = _lib.X509_NAME_oneline( |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 514 | self._name, result_buffer, len(result_buffer)) |
| 515 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 516 | if format_result == _ffi.NULL: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 517 | # TODO: This is untested. |
| 518 | _raise_current_error() |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 519 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 520 | return "<X509Name object '%s'>" % ( |
| 521 | _native(_ffi.string(result_buffer)),) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 522 | |
| 523 | |
| 524 | def hash(self): |
| 525 | """ |
| 526 | Return the hash value of this name |
| 527 | |
| 528 | :return: None |
| 529 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 530 | return _lib.X509_NAME_hash(self._name) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 531 | |
| 532 | |
| 533 | def der(self): |
| 534 | """ |
| 535 | Return the DER encoding of this name |
| 536 | |
| 537 | :return: A :py:class:`bytes` instance giving the DER encoded form of |
| 538 | this name. |
| 539 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 540 | result_buffer = _ffi.new('unsigned char**') |
| 541 | encode_result = _lib.i2d_X509_NAME(self._name, result_buffer) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 542 | if encode_result < 0: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 543 | # TODO: This is untested. |
| 544 | _raise_current_error() |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 545 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 546 | string_result = _ffi.buffer(result_buffer[0], encode_result)[:] |
| 547 | _lib.OPENSSL_free(result_buffer[0]) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 548 | return string_result |
| 549 | |
| 550 | |
| 551 | def get_components(self): |
| 552 | """ |
| 553 | Returns the split-up components of this name. |
| 554 | |
| 555 | :return: List of tuples (name, value). |
| 556 | """ |
| 557 | result = [] |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 558 | for i in range(_lib.X509_NAME_entry_count(self._name)): |
| 559 | ent = _lib.X509_NAME_get_entry(self._name, i) |
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 | fname = _lib.X509_NAME_ENTRY_get_object(ent) |
| 562 | fval = _lib.X509_NAME_ENTRY_get_data(ent) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 563 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 564 | nid = _lib.OBJ_obj2nid(fname) |
| 565 | name = _lib.OBJ_nid2sn(nid) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 566 | |
| 567 | result.append(( |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 568 | _ffi.string(name), |
| 569 | _ffi.string( |
| 570 | _lib.ASN1_STRING_data(fval), |
| 571 | _lib.ASN1_STRING_length(fval)))) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 572 | |
| 573 | return result |
| 574 | X509NameType = X509Name |
| 575 | |
| 576 | |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 577 | class X509Extension(object): |
| 578 | def __init__(self, type_name, critical, value, subject=None, issuer=None): |
| 579 | """ |
| 580 | :param typename: The name of the extension to create. |
| 581 | :type typename: :py:data:`str` |
| 582 | |
| 583 | :param critical: A flag indicating whether this is a critical extension. |
| 584 | |
| 585 | :param value: The value of the extension. |
| 586 | :type value: :py:data:`str` |
| 587 | |
| 588 | :param subject: Optional X509 cert to use as subject. |
| 589 | :type subject: :py:class:`X509` |
| 590 | |
| 591 | :param issuer: Optional X509 cert to use as issuer. |
| 592 | :type issuer: :py:class:`X509` |
| 593 | |
| 594 | :return: The X509Extension object |
| 595 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 596 | ctx = _ffi.new("X509V3_CTX*") |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 597 | |
| 598 | # A context is necessary for any extension which uses the r2i conversion |
| 599 | # method. That is, X509V3_EXT_nconf may segfault if passed a NULL ctx. |
| 600 | # Start off by initializing most of the fields to NULL. |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 601 | _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] | 602 | |
| 603 | # We have no configuration database - but perhaps we should (some |
| 604 | # extensions may require it). |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 605 | _lib.X509V3_set_ctx_nodb(ctx) |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 606 | |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 607 | # Initialize the subject and issuer, if appropriate. ctx is a local, |
| 608 | # and as far as I can tell none of the X509V3_* APIs invoked here steal |
| 609 | # any references, so no need to mess with reference counts or duplicates. |
| 610 | if issuer is not None: |
| 611 | if not isinstance(issuer, X509): |
| 612 | raise TypeError("issuer must be an X509 instance") |
| 613 | ctx.issuer_cert = issuer._x509 |
| 614 | if subject is not None: |
| 615 | if not isinstance(subject, X509): |
| 616 | raise TypeError("subject must be an X509 instance") |
| 617 | ctx.subject_cert = subject._x509 |
| 618 | |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 619 | if critical: |
| 620 | # There are other OpenSSL APIs which would let us pass in critical |
| 621 | # separately, but they're harder to use, and since value is already |
| 622 | # a pile of crappy junk smuggling a ton of utterly important |
| 623 | # structured data, what's the point of trying to avoid nasty stuff |
| 624 | # with strings? (However, X509V3_EXT_i2d in particular seems like it |
| 625 | # would be a better API to invoke. I do not know where to get the |
| 626 | # ext_struc it desires for its last parameter, though.) |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 627 | value = b"critical," + value |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 628 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 629 | extension = _lib.X509V3_EXT_nconf(_ffi.NULL, ctx, type_name, value) |
| 630 | if extension == _ffi.NULL: |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 631 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 632 | self._extension = _ffi.gc(extension, _lib.X509_EXTENSION_free) |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 633 | |
| 634 | |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 635 | @property |
| 636 | def _nid(self): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 637 | return _lib.OBJ_obj2nid(self._extension.object) |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 638 | |
| 639 | _prefixes = { |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 640 | _lib.GEN_EMAIL: "email", |
| 641 | _lib.GEN_DNS: "DNS", |
| 642 | _lib.GEN_URI: "URI", |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | def _subjectAltNameString(self): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 646 | method = _lib.X509V3_EXT_get(self._extension) |
| 647 | if method == _ffi.NULL: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 648 | # TODO: This is untested. |
| 649 | _raise_current_error() |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 650 | payload = self._extension.value.data |
| 651 | length = self._extension.value.length |
| 652 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 653 | payloadptr = _ffi.new("unsigned char**") |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 654 | payloadptr[0] = payload |
| 655 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 656 | if method.it != _ffi.NULL: |
| 657 | ptr = _lib.ASN1_ITEM_ptr(method.it) |
| 658 | data = _lib.ASN1_item_d2i(_ffi.NULL, payloadptr, length, ptr) |
| 659 | names = _ffi.cast("GENERAL_NAMES*", data) |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 660 | else: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 661 | names = _ffi.cast( |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 662 | "GENERAL_NAMES*", |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 663 | method.d2i(_ffi.NULL, payloadptr, length)) |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 664 | |
| 665 | parts = [] |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 666 | for i in range(_lib.sk_GENERAL_NAME_num(names)): |
| 667 | name = _lib.sk_GENERAL_NAME_value(names, i) |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 668 | try: |
| 669 | label = self._prefixes[name.type] |
| 670 | except KeyError: |
| 671 | bio = _new_mem_buf() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 672 | _lib.GENERAL_NAME_print(bio, name) |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 673 | parts.append(_native(_bio_to_string(bio))) |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 674 | else: |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 675 | value = _native( |
| 676 | _ffi.buffer(name.d.ia5.data, name.d.ia5.length)[:]) |
| 677 | parts.append(label + ":" + value) |
| 678 | return ", ".join(parts) |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 679 | |
| 680 | |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 681 | def __str__(self): |
| 682 | """ |
| 683 | :return: a nice text representation of the extension |
| 684 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 685 | if _lib.NID_subject_alt_name == self._nid: |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 686 | return self._subjectAltNameString() |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 687 | |
Jean-Paul Calderone | ed0c57b | 2013-10-06 08:31:40 -0400 | [diff] [blame] | 688 | bio = _new_mem_buf() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 689 | print_result = _lib.X509V3_EXT_print(bio, self._extension, 0, 0) |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 690 | if not print_result: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 691 | # TODO: This is untested. |
| 692 | _raise_current_error() |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 693 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 694 | return _native(_bio_to_string(bio)) |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 695 | |
| 696 | |
| 697 | def get_critical(self): |
| 698 | """ |
| 699 | Returns the critical field of the X509Extension |
| 700 | |
| 701 | :return: The critical field. |
| 702 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 703 | return _lib.X509_EXTENSION_get_critical(self._extension) |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 704 | |
| 705 | |
| 706 | def get_short_name(self): |
| 707 | """ |
| 708 | Returns the short version of the type name of the X509Extension |
| 709 | |
| 710 | :return: The short type name. |
| 711 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 712 | obj = _lib.X509_EXTENSION_get_object(self._extension) |
| 713 | nid = _lib.OBJ_obj2nid(obj) |
| 714 | return _ffi.string(_lib.OBJ_nid2sn(nid)) |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 715 | |
| 716 | |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 717 | def get_data(self): |
| 718 | """ |
| 719 | Returns the data of the X509Extension |
| 720 | |
| 721 | :return: A :py:data:`str` giving the X509Extension's ASN.1 encoded data. |
| 722 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 723 | octet_result = _lib.X509_EXTENSION_get_data(self._extension) |
| 724 | string_result = _ffi.cast('ASN1_STRING*', octet_result) |
| 725 | char_result = _lib.ASN1_STRING_data(string_result) |
| 726 | result_length = _lib.ASN1_STRING_length(string_result) |
| 727 | return _ffi.buffer(char_result, result_length)[:] |
Jean-Paul Calderone | d418a9c | 2013-02-20 16:24:55 -0800 | [diff] [blame] | 728 | |
| 729 | X509ExtensionType = X509Extension |
| 730 | |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 731 | |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 732 | class X509Req(object): |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 733 | def __init__(self): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 734 | req = _lib.X509_REQ_new() |
| 735 | self._req = _ffi.gc(req, _lib.X509_REQ_free) |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 736 | |
| 737 | |
| 738 | def set_pubkey(self, pkey): |
| 739 | """ |
| 740 | Set the public key of the certificate request |
| 741 | |
| 742 | :param pkey: The public key to use |
| 743 | :return: None |
| 744 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 745 | set_result = _lib.X509_REQ_set_pubkey(self._req, pkey._pkey) |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 746 | if not set_result: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 747 | # TODO: This is untested. |
| 748 | _raise_current_error() |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 749 | |
| 750 | |
| 751 | def get_pubkey(self): |
| 752 | """ |
| 753 | Get the public key from the certificate request |
| 754 | |
| 755 | :return: The public key |
| 756 | """ |
| 757 | pkey = PKey.__new__(PKey) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 758 | pkey._pkey = _lib.X509_REQ_get_pubkey(self._req) |
| 759 | if pkey._pkey == _ffi.NULL: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 760 | # TODO: This is untested. |
| 761 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 762 | pkey._pkey = _ffi.gc(pkey._pkey, _lib.EVP_PKEY_free) |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 763 | pkey._only_public = True |
| 764 | return pkey |
| 765 | |
| 766 | |
| 767 | def set_version(self, version): |
| 768 | """ |
| 769 | Set the version subfield (RFC 2459, section 4.1.2.1) of the certificate |
| 770 | request. |
| 771 | |
| 772 | :param version: The version number |
| 773 | :return: None |
| 774 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 775 | set_result = _lib.X509_REQ_set_version(self._req, version) |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 776 | if not set_result: |
| 777 | _raise_current_error() |
| 778 | |
| 779 | |
| 780 | def get_version(self): |
| 781 | """ |
| 782 | Get the version subfield (RFC 2459, section 4.1.2.1) of the certificate |
| 783 | request. |
| 784 | |
| 785 | :return: an integer giving the value of the version subfield |
| 786 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 787 | return _lib.X509_REQ_get_version(self._req) |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 788 | |
| 789 | |
| 790 | def get_subject(self): |
| 791 | """ |
| 792 | Create an X509Name object for the subject of the certificate request |
| 793 | |
| 794 | :return: An X509Name object |
| 795 | """ |
| 796 | name = X509Name.__new__(X509Name) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 797 | name._name = _lib.X509_REQ_get_subject_name(self._req) |
| 798 | if name._name == _ffi.NULL: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 799 | # TODO: This is untested. |
| 800 | _raise_current_error() |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 801 | |
| 802 | # The name is owned by the X509Req structure. As long as the X509Name |
| 803 | # Python object is alive, keep the X509Req Python object alive. |
| 804 | name._owner = self |
| 805 | |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 806 | return name |
| 807 | |
| 808 | |
| 809 | def add_extensions(self, extensions): |
| 810 | """ |
| 811 | Add extensions to the request. |
| 812 | |
| 813 | :param extensions: a sequence of X509Extension objects |
| 814 | :return: None |
| 815 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 816 | stack = _lib.sk_X509_EXTENSION_new_null() |
| 817 | if stack == _ffi.NULL: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 818 | # TODO: This is untested. |
| 819 | _raise_current_error() |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 820 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 821 | stack = _ffi.gc(stack, _lib.sk_X509_EXTENSION_free) |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 822 | |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 823 | for ext in extensions: |
| 824 | if not isinstance(ext, X509Extension): |
Jean-Paul Calderone | c2154b7 | 2013-02-20 14:29:37 -0800 | [diff] [blame] | 825 | raise ValueError("One of the elements is not an X509Extension") |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 826 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 827 | # TODO push can fail (here and elsewhere) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 828 | _lib.sk_X509_EXTENSION_push(stack, ext._extension) |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 829 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 830 | add_result = _lib.X509_REQ_add_extensions(self._req, stack) |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 831 | if not add_result: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 832 | # TODO: This is untested. |
| 833 | _raise_current_error() |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 834 | |
| 835 | |
Stephen Holsapple | adfd39d | 2014-01-28 17:58:31 -0800 | [diff] [blame] | 836 | def get_extensions(self): |
Stephen Holsapple | 7fbdf64 | 2014-03-01 20:05:47 -0800 | [diff] [blame] | 837 | """ |
| 838 | Get extensions to the request. |
Stephen Holsapple | adfd39d | 2014-01-28 17:58:31 -0800 | [diff] [blame] | 839 | |
Jean-Paul Calderone | da3e6c6 | 2014-03-02 08:06:11 -0500 | [diff] [blame] | 840 | :return: A :py:class:`list` of :py:class:`X509Extension` objects. |
Stephen Holsapple | 7fbdf64 | 2014-03-01 20:05:47 -0800 | [diff] [blame] | 841 | """ |
| 842 | exts = [] |
Jean-Paul Calderone | 9479d73 | 2014-03-02 08:04:54 -0500 | [diff] [blame] | 843 | native_exts_obj = _lib.X509_REQ_get_extensions(self._req) |
Jean-Paul Calderone | b7a79b4 | 2014-03-02 08:06:47 -0500 | [diff] [blame] | 844 | for i in range(_lib.sk_X509_EXTENSION_num(native_exts_obj)): |
Stephen Holsapple | 7fbdf64 | 2014-03-01 20:05:47 -0800 | [diff] [blame] | 845 | ext = X509Extension.__new__(X509Extension) |
Jean-Paul Calderone | 9479d73 | 2014-03-02 08:04:54 -0500 | [diff] [blame] | 846 | ext._extension = _lib.sk_X509_EXTENSION_value(native_exts_obj, i) |
Stephen Holsapple | 7fbdf64 | 2014-03-01 20:05:47 -0800 | [diff] [blame] | 847 | exts.append(ext) |
| 848 | return exts |
Stephen Holsapple | adfd39d | 2014-01-28 17:58:31 -0800 | [diff] [blame] | 849 | |
| 850 | |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 851 | def sign(self, pkey, digest): |
| 852 | """ |
| 853 | Sign the certificate request using the supplied key and digest |
| 854 | |
| 855 | :param pkey: The key to sign with |
| 856 | :param digest: The message digest to use |
| 857 | :return: None |
| 858 | """ |
| 859 | if pkey._only_public: |
| 860 | raise ValueError("Key has only public part") |
| 861 | |
| 862 | if not pkey._initialized: |
| 863 | raise ValueError("Key is uninitialized") |
| 864 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 865 | digest_obj = _lib.EVP_get_digestbyname(_byte_string(digest)) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 866 | if digest_obj == _ffi.NULL: |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 867 | raise ValueError("No such digest method") |
| 868 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 869 | sign_result = _lib.X509_REQ_sign(self._req, pkey._pkey, digest_obj) |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 870 | if not sign_result: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 871 | # TODO: This is untested. |
| 872 | _raise_current_error() |
Jean-Paul Calderone | 4328d47 | 2013-02-20 14:28:46 -0800 | [diff] [blame] | 873 | |
| 874 | |
Jean-Paul Calderone | 5565f0f | 2013-03-06 11:10:20 -0800 | [diff] [blame] | 875 | def verify(self, pkey): |
| 876 | """ |
| 877 | Verifies a certificate request using the supplied public key |
| 878 | |
| 879 | :param key: a public key |
| 880 | :return: True if the signature is correct. |
| 881 | |
| 882 | :raise OpenSSL.crypto.Error: If the signature is invalid or there is a |
| 883 | problem verifying the signature. |
| 884 | """ |
| 885 | if not isinstance(pkey, PKey): |
| 886 | raise TypeError("pkey must be a PKey instance") |
| 887 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 888 | result = _lib.X509_REQ_verify(self._req, pkey._pkey) |
Jean-Paul Calderone | 5565f0f | 2013-03-06 11:10:20 -0800 | [diff] [blame] | 889 | if result <= 0: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 890 | _raise_current_error() |
Jean-Paul Calderone | 5565f0f | 2013-03-06 11:10:20 -0800 | [diff] [blame] | 891 | |
| 892 | return result |
| 893 | |
| 894 | |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 895 | X509ReqType = X509Req |
| 896 | |
| 897 | |
| 898 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 899 | class X509(object): |
| 900 | def __init__(self): |
| 901 | # TODO Allocation failure? And why not __new__ instead of __init__? |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 902 | x509 = _lib.X509_new() |
| 903 | self._x509 = _ffi.gc(x509, _lib.X509_free) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 904 | |
| 905 | |
| 906 | def set_version(self, version): |
| 907 | """ |
| 908 | Set version number of the certificate |
| 909 | |
| 910 | :param version: The version number |
| 911 | :type version: :py:class:`int` |
| 912 | |
| 913 | :return: None |
| 914 | """ |
| 915 | if not isinstance(version, int): |
| 916 | raise TypeError("version must be an integer") |
| 917 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 918 | _lib.X509_set_version(self._x509, version) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 919 | |
| 920 | |
| 921 | def get_version(self): |
| 922 | """ |
| 923 | Return version number of the certificate |
| 924 | |
| 925 | :return: Version number as a Python integer |
| 926 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 927 | return _lib.X509_get_version(self._x509) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 928 | |
| 929 | |
Jean-Paul Calderone | edafced | 2013-02-19 11:48:38 -0800 | [diff] [blame] | 930 | def get_pubkey(self): |
| 931 | """ |
| 932 | Get the public key of the certificate |
| 933 | |
| 934 | :return: The public key |
| 935 | """ |
| 936 | pkey = PKey.__new__(PKey) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 937 | pkey._pkey = _lib.X509_get_pubkey(self._x509) |
| 938 | if pkey._pkey == _ffi.NULL: |
Jean-Paul Calderone | edafced | 2013-02-19 11:48:38 -0800 | [diff] [blame] | 939 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 940 | pkey._pkey = _ffi.gc(pkey._pkey, _lib.EVP_PKEY_free) |
Jean-Paul Calderone | edafced | 2013-02-19 11:48:38 -0800 | [diff] [blame] | 941 | pkey._only_public = True |
| 942 | return pkey |
| 943 | |
| 944 | |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 945 | def set_pubkey(self, pkey): |
| 946 | """ |
| 947 | Set the public key of the certificate |
| 948 | |
| 949 | :param pkey: The public key |
| 950 | |
| 951 | :return: None |
| 952 | """ |
| 953 | if not isinstance(pkey, PKey): |
| 954 | raise TypeError("pkey must be a PKey instance") |
| 955 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 956 | set_result = _lib.X509_set_pubkey(self._x509, pkey._pkey) |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 957 | if not set_result: |
| 958 | _raise_current_error() |
| 959 | |
| 960 | |
| 961 | def sign(self, pkey, digest): |
| 962 | """ |
| 963 | Sign the certificate using the supplied key and digest |
| 964 | |
| 965 | :param pkey: The key to sign with |
| 966 | :param digest: The message digest to use |
| 967 | :return: None |
| 968 | """ |
| 969 | if not isinstance(pkey, PKey): |
| 970 | raise TypeError("pkey must be a PKey instance") |
| 971 | |
Jean-Paul Calderone | edafced | 2013-02-19 11:48:38 -0800 | [diff] [blame] | 972 | if pkey._only_public: |
| 973 | raise ValueError("Key only has public part") |
| 974 | |
Jean-Paul Calderone | 09e3bdc | 2013-02-19 12:15:28 -0800 | [diff] [blame] | 975 | if not pkey._initialized: |
| 976 | raise ValueError("Key is uninitialized") |
| 977 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 978 | evp_md = _lib.EVP_get_digestbyname(_byte_string(digest)) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 979 | if evp_md == _ffi.NULL: |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 980 | raise ValueError("No such digest method") |
| 981 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 982 | sign_result = _lib.X509_sign(self._x509, pkey._pkey, evp_md) |
Jean-Paul Calderone | 3e29ccf | 2013-02-19 11:32:46 -0800 | [diff] [blame] | 983 | if not sign_result: |
| 984 | _raise_current_error() |
| 985 | |
| 986 | |
Jean-Paul Calderone | e4aa3fa | 2013-02-19 12:12:53 -0800 | [diff] [blame] | 987 | def get_signature_algorithm(self): |
| 988 | """ |
| 989 | Retrieve the signature algorithm used in the certificate |
| 990 | |
| 991 | :return: A byte string giving the name of the signature algorithm used in |
| 992 | the certificate. |
| 993 | :raise ValueError: If the signature algorithm is undefined. |
| 994 | """ |
| 995 | alg = self._x509.cert_info.signature.algorithm |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 996 | nid = _lib.OBJ_obj2nid(alg) |
| 997 | if nid == _lib.NID_undef: |
Jean-Paul Calderone | e4aa3fa | 2013-02-19 12:12:53 -0800 | [diff] [blame] | 998 | raise ValueError("Undefined signature algorithm") |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 999 | return _ffi.string(_lib.OBJ_nid2ln(nid)) |
Jean-Paul Calderone | e4aa3fa | 2013-02-19 12:12:53 -0800 | [diff] [blame] | 1000 | |
| 1001 | |
Jean-Paul Calderone | b407872 | 2013-02-19 12:01:55 -0800 | [diff] [blame] | 1002 | def digest(self, digest_name): |
| 1003 | """ |
| 1004 | Return the digest of the X509 object. |
| 1005 | |
| 1006 | :param digest_name: The name of the digest algorithm to use. |
| 1007 | :type digest_name: :py:class:`bytes` |
| 1008 | |
| 1009 | :return: The digest of the object |
| 1010 | """ |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1011 | digest = _lib.EVP_get_digestbyname(_byte_string(digest_name)) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1012 | if digest == _ffi.NULL: |
Jean-Paul Calderone | b407872 | 2013-02-19 12:01:55 -0800 | [diff] [blame] | 1013 | raise ValueError("No such digest method") |
| 1014 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1015 | result_buffer = _ffi.new("char[]", _lib.EVP_MAX_MD_SIZE) |
| 1016 | result_length = _ffi.new("unsigned int[]", 1) |
Jean-Paul Calderone | b407872 | 2013-02-19 12:01:55 -0800 | [diff] [blame] | 1017 | result_length[0] = len(result_buffer) |
| 1018 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1019 | digest_result = _lib.X509_digest( |
Jean-Paul Calderone | b407872 | 2013-02-19 12:01:55 -0800 | [diff] [blame] | 1020 | self._x509, digest, result_buffer, result_length) |
| 1021 | |
| 1022 | if not digest_result: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 1023 | # TODO: This is untested. |
| 1024 | _raise_current_error() |
Jean-Paul Calderone | b407872 | 2013-02-19 12:01:55 -0800 | [diff] [blame] | 1025 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1026 | return b":".join([ |
| 1027 | b16encode(ch).upper() for ch |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1028 | in _ffi.buffer(result_buffer, result_length[0])]) |
Jean-Paul Calderone | b407872 | 2013-02-19 12:01:55 -0800 | [diff] [blame] | 1029 | |
| 1030 | |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1031 | def subject_name_hash(self): |
| 1032 | """ |
| 1033 | Return the hash of the X509 subject. |
| 1034 | |
| 1035 | :return: The hash of the subject. |
| 1036 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1037 | return _lib.X509_subject_name_hash(self._x509) |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1038 | |
| 1039 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1040 | def set_serial_number(self, serial): |
| 1041 | """ |
| 1042 | Set serial number of the certificate |
| 1043 | |
| 1044 | :param serial: The serial number |
| 1045 | :type serial: :py:class:`int` |
| 1046 | |
| 1047 | :return: None |
| 1048 | """ |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1049 | if not isinstance(serial, _integer_types): |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1050 | raise TypeError("serial must be an integer") |
| 1051 | |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1052 | hex_serial = hex(serial)[2:] |
| 1053 | if not isinstance(hex_serial, bytes): |
| 1054 | hex_serial = hex_serial.encode('ascii') |
| 1055 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1056 | bignum_serial = _ffi.new("BIGNUM**") |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1057 | |
| 1058 | # BN_hex2bn stores the result in &bignum. Unless it doesn't feel like |
| 1059 | # it. If bignum is still NULL after this call, then the return value is |
| 1060 | # actually the result. I hope. -exarkun |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1061 | small_serial = _lib.BN_hex2bn(bignum_serial, hex_serial) |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1062 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1063 | if bignum_serial[0] == _ffi.NULL: |
| 1064 | set_result = _lib.ASN1_INTEGER_set( |
| 1065 | _lib.X509_get_serialNumber(self._x509), small_serial) |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1066 | if set_result: |
| 1067 | # TODO Not tested |
| 1068 | _raise_current_error() |
| 1069 | else: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1070 | asn1_serial = _lib.BN_to_ASN1_INTEGER(bignum_serial[0], _ffi.NULL) |
| 1071 | _lib.BN_free(bignum_serial[0]) |
| 1072 | if asn1_serial == _ffi.NULL: |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1073 | # TODO Not tested |
| 1074 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1075 | asn1_serial = _ffi.gc(asn1_serial, _lib.ASN1_INTEGER_free) |
| 1076 | set_result = _lib.X509_set_serialNumber(self._x509, asn1_serial) |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1077 | if not set_result: |
| 1078 | # TODO Not tested |
| 1079 | _raise_current_error() |
| 1080 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1081 | |
| 1082 | def get_serial_number(self): |
| 1083 | """ |
| 1084 | Return serial number of the certificate |
| 1085 | |
| 1086 | :return: Serial number as a Python integer |
| 1087 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1088 | asn1_serial = _lib.X509_get_serialNumber(self._x509) |
| 1089 | bignum_serial = _lib.ASN1_INTEGER_to_BN(asn1_serial, _ffi.NULL) |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1090 | try: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1091 | hex_serial = _lib.BN_bn2hex(bignum_serial) |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1092 | try: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1093 | hexstring_serial = _ffi.string(hex_serial) |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1094 | serial = int(hexstring_serial, 16) |
| 1095 | return serial |
| 1096 | finally: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1097 | _lib.OPENSSL_free(hex_serial) |
Jean-Paul Calderone | 7813385 | 2013-02-19 10:41:46 -0800 | [diff] [blame] | 1098 | finally: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1099 | _lib.BN_free(bignum_serial) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1100 | |
| 1101 | |
| 1102 | def gmtime_adj_notAfter(self, amount): |
| 1103 | """ |
| 1104 | Adjust the time stamp for when the certificate stops being valid |
| 1105 | |
| 1106 | :param amount: The number of seconds by which to adjust the ending |
| 1107 | validity time. |
| 1108 | :type amount: :py:class:`int` |
| 1109 | |
| 1110 | :return: None |
| 1111 | """ |
| 1112 | if not isinstance(amount, int): |
| 1113 | raise TypeError("amount must be an integer") |
| 1114 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1115 | notAfter = _lib.X509_get_notAfter(self._x509) |
| 1116 | _lib.X509_gmtime_adj(notAfter, amount) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1117 | |
| 1118 | |
Jean-Paul Calderone | 662afe5 | 2013-02-20 08:41:11 -0800 | [diff] [blame] | 1119 | def gmtime_adj_notBefore(self, amount): |
| 1120 | """ |
| 1121 | Change the timestamp for when the certificate starts being valid to the current |
| 1122 | time plus an offset. |
| 1123 | |
| 1124 | :param amount: The number of seconds by which to adjust the starting validity |
| 1125 | time. |
| 1126 | :return: None |
| 1127 | """ |
| 1128 | if not isinstance(amount, int): |
| 1129 | raise TypeError("amount must be an integer") |
| 1130 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1131 | notBefore = _lib.X509_get_notBefore(self._x509) |
| 1132 | _lib.X509_gmtime_adj(notBefore, amount) |
Jean-Paul Calderone | 662afe5 | 2013-02-20 08:41:11 -0800 | [diff] [blame] | 1133 | |
| 1134 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1135 | def has_expired(self): |
| 1136 | """ |
| 1137 | Check whether the certificate has expired. |
| 1138 | |
| 1139 | :return: True if the certificate has expired, false otherwise |
| 1140 | """ |
| 1141 | now = int(time()) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1142 | notAfter = _lib.X509_get_notAfter(self._x509) |
| 1143 | return _lib.ASN1_UTCTIME_cmp_time_t( |
| 1144 | _ffi.cast('ASN1_UTCTIME*', notAfter), now) < 0 |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1145 | |
| 1146 | |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1147 | def _get_boundary_time(self, which): |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1148 | return _get_asn1_time(which(self._x509)) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1149 | |
| 1150 | |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1151 | def get_notBefore(self): |
| 1152 | """ |
| 1153 | Retrieve the time stamp for when the certificate starts being valid |
| 1154 | |
| 1155 | :return: A string giving the timestamp, in the format:: |
| 1156 | |
| 1157 | YYYYMMDDhhmmssZ |
| 1158 | YYYYMMDDhhmmss+hhmm |
| 1159 | YYYYMMDDhhmmss-hhmm |
| 1160 | |
| 1161 | or None if there is no value set. |
| 1162 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1163 | return self._get_boundary_time(_lib.X509_get_notBefore) |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1164 | |
| 1165 | |
| 1166 | def _set_boundary_time(self, which, when): |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1167 | return _set_asn1_time(which(self._x509), when) |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1168 | |
| 1169 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1170 | def set_notBefore(self, when): |
| 1171 | """ |
| 1172 | Set the time stamp for when the certificate starts being valid |
| 1173 | |
| 1174 | :param when: A string giving the timestamp, in the format: |
| 1175 | |
| 1176 | YYYYMMDDhhmmssZ |
| 1177 | YYYYMMDDhhmmss+hhmm |
| 1178 | YYYYMMDDhhmmss-hhmm |
| 1179 | :type when: :py:class:`bytes` |
| 1180 | |
| 1181 | :return: None |
| 1182 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1183 | return self._set_boundary_time(_lib.X509_get_notBefore, when) |
Jean-Paul Calderone | d7d8127 | 2013-02-19 13:16:03 -0800 | [diff] [blame] | 1184 | |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1185 | |
| 1186 | def get_notAfter(self): |
| 1187 | """ |
| 1188 | Retrieve the time stamp for when the certificate stops being valid |
| 1189 | |
| 1190 | :return: A string giving the timestamp, in the format:: |
| 1191 | |
| 1192 | YYYYMMDDhhmmssZ |
| 1193 | YYYYMMDDhhmmss+hhmm |
| 1194 | YYYYMMDDhhmmss-hhmm |
| 1195 | |
| 1196 | or None if there is no value set. |
| 1197 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1198 | return self._get_boundary_time(_lib.X509_get_notAfter) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1199 | |
| 1200 | |
| 1201 | def set_notAfter(self, when): |
| 1202 | """ |
| 1203 | Set the time stamp for when the certificate stops being valid |
| 1204 | |
| 1205 | :param when: A string giving the timestamp, in the format: |
| 1206 | |
| 1207 | YYYYMMDDhhmmssZ |
| 1208 | YYYYMMDDhhmmss+hhmm |
| 1209 | YYYYMMDDhhmmss-hhmm |
| 1210 | :type when: :py:class:`bytes` |
| 1211 | |
| 1212 | :return: None |
| 1213 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1214 | return self._set_boundary_time(_lib.X509_get_notAfter, when) |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1215 | |
| 1216 | |
| 1217 | def _get_name(self, which): |
| 1218 | name = X509Name.__new__(X509Name) |
| 1219 | name._name = which(self._x509) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1220 | if name._name == _ffi.NULL: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 1221 | # TODO: This is untested. |
| 1222 | _raise_current_error() |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 1223 | |
| 1224 | # The name is owned by the X509 structure. As long as the X509Name |
| 1225 | # Python object is alive, keep the X509 Python object alive. |
| 1226 | name._owner = self |
| 1227 | |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1228 | return name |
| 1229 | |
| 1230 | |
| 1231 | def _set_name(self, which, name): |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 1232 | if not isinstance(name, X509Name): |
| 1233 | raise TypeError("name must be an X509Name") |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1234 | set_result = which(self._x509, name._name) |
| 1235 | if not set_result: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 1236 | # TODO: This is untested. |
| 1237 | _raise_current_error() |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1238 | |
| 1239 | |
| 1240 | def get_issuer(self): |
| 1241 | """ |
| 1242 | Create an X509Name object for the issuer of the certificate |
| 1243 | |
| 1244 | :return: An X509Name object |
| 1245 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1246 | return self._get_name(_lib.X509_get_issuer_name) |
Jean-Paul Calderone | c2bd4e9 | 2013-02-20 08:12:36 -0800 | [diff] [blame] | 1247 | |
| 1248 | |
| 1249 | def set_issuer(self, issuer): |
| 1250 | """ |
| 1251 | Set the issuer of the certificate |
| 1252 | |
| 1253 | :param issuer: The issuer name |
| 1254 | :type issuer: :py:class:`X509Name` |
| 1255 | |
| 1256 | :return: None |
| 1257 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1258 | return self._set_name(_lib.X509_set_issuer_name, issuer) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 1259 | |
| 1260 | |
| 1261 | def get_subject(self): |
| 1262 | """ |
| 1263 | Create an X509Name object for the subject of the certificate |
| 1264 | |
| 1265 | :return: An X509Name object |
| 1266 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1267 | return self._get_name(_lib.X509_get_subject_name) |
Jean-Paul Calderone | a9de195 | 2013-02-19 16:58:42 -0800 | [diff] [blame] | 1268 | |
| 1269 | |
| 1270 | def set_subject(self, subject): |
| 1271 | """ |
| 1272 | Set the subject of the certificate |
| 1273 | |
| 1274 | :param subject: The subject name |
| 1275 | :type subject: :py:class:`X509Name` |
| 1276 | :return: None |
| 1277 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1278 | return self._set_name(_lib.X509_set_subject_name, subject) |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 1279 | |
| 1280 | |
| 1281 | def get_extension_count(self): |
| 1282 | """ |
| 1283 | Get the number of extensions on the certificate. |
| 1284 | |
| 1285 | :return: The number of extensions as an integer. |
| 1286 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1287 | return _lib.X509_get_ext_count(self._x509) |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 1288 | |
| 1289 | |
| 1290 | def add_extensions(self, extensions): |
| 1291 | """ |
| 1292 | Add extensions to the certificate. |
| 1293 | |
| 1294 | :param extensions: a sequence of X509Extension objects |
| 1295 | :return: None |
| 1296 | """ |
| 1297 | for ext in extensions: |
| 1298 | if not isinstance(ext, X509Extension): |
| 1299 | raise ValueError("One of the elements is not an X509Extension") |
| 1300 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1301 | add_result = _lib.X509_add_ext(self._x509, ext._extension, -1) |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 1302 | if not add_result: |
| 1303 | _raise_current_error() |
| 1304 | |
| 1305 | |
| 1306 | def get_extension(self, index): |
| 1307 | """ |
| 1308 | Get a specific extension of the certificate by index. |
| 1309 | |
| 1310 | :param index: The index of the extension to retrieve. |
| 1311 | :return: The X509Extension object at the specified index. |
| 1312 | """ |
| 1313 | ext = X509Extension.__new__(X509Extension) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1314 | ext._extension = _lib.X509_get_ext(self._x509, index) |
| 1315 | if ext._extension == _ffi.NULL: |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 1316 | raise IndexError("extension index out of bounds") |
| 1317 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1318 | extension = _lib.X509_EXTENSION_dup(ext._extension) |
| 1319 | ext._extension = _ffi.gc(extension, _lib.X509_EXTENSION_free) |
Jean-Paul Calderone | 83d22eb | 2013-02-20 12:19:43 -0800 | [diff] [blame] | 1320 | return ext |
| 1321 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1322 | X509Type = X509 |
| 1323 | |
| 1324 | |
| 1325 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1326 | class X509Store(object): |
| 1327 | def __init__(self): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1328 | store = _lib.X509_STORE_new() |
| 1329 | self._store = _ffi.gc(store, _lib.X509_STORE_free) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1330 | |
| 1331 | |
| 1332 | def add_cert(self, cert): |
| 1333 | if not isinstance(cert, X509): |
| 1334 | raise TypeError() |
| 1335 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1336 | result = _lib.X509_STORE_add_cert(self._store, cert._x509) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1337 | if not result: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 1338 | _raise_current_error() |
Jean-Paul Calderone | e6f32b8 | 2013-03-06 10:27:57 -0800 | [diff] [blame] | 1339 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1340 | |
| 1341 | X509StoreType = X509Store |
| 1342 | |
| 1343 | |
| 1344 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1345 | def load_certificate(type, buffer): |
| 1346 | """ |
| 1347 | Load a certificate from a buffer |
| 1348 | |
| 1349 | :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1) |
| 1350 | |
| 1351 | :param buffer: The buffer the certificate is stored in |
| 1352 | :type buffer: :py:class:`bytes` |
| 1353 | |
| 1354 | :return: The X509 object |
| 1355 | """ |
Jean-Paul Calderone | 6922a86 | 2014-01-18 10:38:28 -0500 | [diff] [blame] | 1356 | if isinstance(buffer, _text_type): |
| 1357 | buffer = buffer.encode("ascii") |
| 1358 | |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 1359 | bio = _new_mem_buf(buffer) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1360 | |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 1361 | if type == FILETYPE_PEM: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1362 | 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] | 1363 | elif type == FILETYPE_ASN1: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1364 | x509 = _lib.d2i_X509_bio(bio, _ffi.NULL); |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 1365 | else: |
| 1366 | raise ValueError( |
| 1367 | "type argument must be FILETYPE_PEM or FILETYPE_ASN1") |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1368 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1369 | if x509 == _ffi.NULL: |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1370 | _raise_current_error() |
| 1371 | |
| 1372 | cert = X509.__new__(X509) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1373 | cert._x509 = _ffi.gc(x509, _lib.X509_free) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1374 | return cert |
| 1375 | |
| 1376 | |
| 1377 | def dump_certificate(type, cert): |
| 1378 | """ |
| 1379 | Dump a certificate to a buffer |
| 1380 | |
Jean-Paul Calderone | a12e7d2 | 2013-04-03 08:17:34 -0400 | [diff] [blame] | 1381 | :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1, or |
| 1382 | FILETYPE_TEXT) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1383 | :param cert: The certificate to dump |
| 1384 | :return: The buffer with the dumped certificate in |
| 1385 | """ |
Jean-Paul Calderone | 0c73aff | 2013-03-02 07:45:12 -0800 | [diff] [blame] | 1386 | bio = _new_mem_buf() |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 1387 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1388 | if type == FILETYPE_PEM: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1389 | result_code = _lib.PEM_write_bio_X509(bio, cert._x509) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1390 | elif type == FILETYPE_ASN1: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1391 | result_code = _lib.i2d_X509_bio(bio, cert._x509) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1392 | elif type == FILETYPE_TEXT: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1393 | result_code = _lib.X509_print_ex(bio, cert._x509, 0, 0) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1394 | else: |
| 1395 | raise ValueError( |
| 1396 | "type argument must be FILETYPE_PEM, FILETYPE_ASN1, or " |
| 1397 | "FILETYPE_TEXT") |
| 1398 | |
| 1399 | return _bio_to_string(bio) |
| 1400 | |
| 1401 | |
| 1402 | |
| 1403 | def dump_privatekey(type, pkey, cipher=None, passphrase=None): |
| 1404 | """ |
| 1405 | Dump a private key to a buffer |
| 1406 | |
Jean-Paul Calderone | e66fde2 | 2013-04-03 08:35:08 -0400 | [diff] [blame] | 1407 | :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1, or |
| 1408 | FILETYPE_TEXT) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1409 | :param pkey: The PKey to dump |
| 1410 | :param cipher: (optional) if encrypted PEM format, the cipher to |
| 1411 | use |
| 1412 | :param passphrase: (optional) if encrypted PEM format, this can be either |
| 1413 | the passphrase to use, or a callback for providing the |
| 1414 | passphrase. |
| 1415 | :return: The buffer with the dumped key in |
| 1416 | :rtype: :py:data:`str` |
| 1417 | """ |
Jean-Paul Calderone | ef9a3dc | 2013-03-02 16:33:32 -0800 | [diff] [blame] | 1418 | bio = _new_mem_buf() |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 1419 | |
| 1420 | if cipher is not None: |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1421 | if passphrase is None: |
| 1422 | raise TypeError( |
| 1423 | "if a value is given for cipher " |
| 1424 | "one must also be given for passphrase") |
| 1425 | cipher_obj = _lib.EVP_get_cipherbyname(_byte_string(cipher)) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1426 | if cipher_obj == _ffi.NULL: |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 1427 | raise ValueError("Invalid cipher name") |
| 1428 | else: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1429 | cipher_obj = _ffi.NULL |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1430 | |
Jean-Paul Calderone | 23478b3 | 2013-02-20 13:31:38 -0800 | [diff] [blame] | 1431 | helper = _PassphraseHelper(type, passphrase) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1432 | if type == FILETYPE_PEM: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1433 | result_code = _lib.PEM_write_bio_PrivateKey( |
| 1434 | bio, pkey._pkey, cipher_obj, _ffi.NULL, 0, |
Jean-Paul Calderone | 23478b3 | 2013-02-20 13:31:38 -0800 | [diff] [blame] | 1435 | helper.callback, helper.callback_args) |
| 1436 | helper.raise_if_problem() |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1437 | elif type == FILETYPE_ASN1: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1438 | result_code = _lib.i2d_PrivateKey_bio(bio, pkey._pkey) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1439 | elif type == FILETYPE_TEXT: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1440 | rsa = _lib.EVP_PKEY_get1_RSA(pkey._pkey) |
| 1441 | result_code = _lib.RSA_print(bio, rsa, 0) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 1442 | # TODO RSA_free(rsa)? |
| 1443 | else: |
| 1444 | raise ValueError( |
| 1445 | "type argument must be FILETYPE_PEM, FILETYPE_ASN1, or " |
| 1446 | "FILETYPE_TEXT") |
| 1447 | |
| 1448 | if result_code == 0: |
| 1449 | _raise_current_error() |
| 1450 | |
| 1451 | return _bio_to_string(bio) |
| 1452 | |
| 1453 | |
| 1454 | |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1455 | def _X509_REVOKED_dup(original): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1456 | copy = _lib.X509_REVOKED_new() |
| 1457 | if copy == _ffi.NULL: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 1458 | # TODO: This is untested. |
| 1459 | _raise_current_error() |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1460 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1461 | if original.serialNumber != _ffi.NULL: |
Jonathan Giannuzzi | b5b9322 | 2014-03-20 15:54:29 +0100 | [diff] [blame] | 1462 | _lib.ASN1_INTEGER_free(copy.serialNumber) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1463 | copy.serialNumber = _lib.ASN1_INTEGER_dup(original.serialNumber) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1464 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1465 | if original.revocationDate != _ffi.NULL: |
Jonathan Giannuzzi | b5b9322 | 2014-03-20 15:54:29 +0100 | [diff] [blame] | 1466 | _lib.ASN1_TIME_free(copy.revocationDate) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1467 | copy.revocationDate = _lib.M_ASN1_TIME_dup(original.revocationDate) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1468 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1469 | if original.extensions != _ffi.NULL: |
| 1470 | extension_stack = _lib.sk_X509_EXTENSION_new_null() |
| 1471 | for i in range(_lib.sk_X509_EXTENSION_num(original.extensions)): |
| 1472 | original_ext = _lib.sk_X509_EXTENSION_value(original.extensions, i) |
| 1473 | copy_ext = _lib.X509_EXTENSION_dup(original_ext) |
| 1474 | _lib.sk_X509_EXTENSION_push(extension_stack, copy_ext) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1475 | copy.extensions = extension_stack |
| 1476 | |
| 1477 | copy.sequence = original.sequence |
| 1478 | return copy |
| 1479 | |
| 1480 | |
| 1481 | |
| 1482 | class Revoked(object): |
| 1483 | # http://www.openssl.org/docs/apps/x509v3_config.html#CRL_distribution_points_ |
| 1484 | # which differs from crl_reasons of crypto/x509v3/v3_enum.c that matches |
| 1485 | # OCSP_crl_reason_str. We use the latter, just like the command line |
| 1486 | # program. |
| 1487 | _crl_reasons = [ |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1488 | b"unspecified", |
| 1489 | b"keyCompromise", |
| 1490 | b"CACompromise", |
| 1491 | b"affiliationChanged", |
| 1492 | b"superseded", |
| 1493 | b"cessationOfOperation", |
| 1494 | b"certificateHold", |
| 1495 | # b"removeFromCRL", |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1496 | ] |
| 1497 | |
| 1498 | def __init__(self): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1499 | revoked = _lib.X509_REVOKED_new() |
| 1500 | self._revoked = _ffi.gc(revoked, _lib.X509_REVOKED_free) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1501 | |
| 1502 | |
| 1503 | def set_serial(self, hex_str): |
| 1504 | """ |
| 1505 | Set the serial number of a revoked Revoked structure |
| 1506 | |
| 1507 | :param hex_str: The new serial number. |
| 1508 | :type hex_str: :py:data:`str` |
| 1509 | :return: None |
| 1510 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1511 | bignum_serial = _ffi.gc(_lib.BN_new(), _lib.BN_free) |
| 1512 | bignum_ptr = _ffi.new("BIGNUM**") |
Jean-Paul Calderone | fd37136 | 2013-03-01 20:53:58 -0800 | [diff] [blame] | 1513 | bignum_ptr[0] = bignum_serial |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1514 | bn_result = _lib.BN_hex2bn(bignum_ptr, hex_str) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1515 | if not bn_result: |
| 1516 | raise ValueError("bad hex string") |
| 1517 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1518 | asn1_serial = _ffi.gc( |
| 1519 | _lib.BN_to_ASN1_INTEGER(bignum_serial, _ffi.NULL), |
| 1520 | _lib.ASN1_INTEGER_free) |
| 1521 | _lib.X509_REVOKED_set_serialNumber(self._revoked, asn1_serial) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1522 | |
| 1523 | |
| 1524 | def get_serial(self): |
| 1525 | """ |
| 1526 | Return the serial number of a Revoked structure |
| 1527 | |
| 1528 | :return: The serial number as a string |
| 1529 | """ |
Jean-Paul Calderone | fd37136 | 2013-03-01 20:53:58 -0800 | [diff] [blame] | 1530 | bio = _new_mem_buf() |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1531 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1532 | result = _lib.i2a_ASN1_INTEGER(bio, self._revoked.serialNumber) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1533 | if result < 0: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 1534 | # TODO: This is untested. |
| 1535 | _raise_current_error() |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1536 | |
| 1537 | return _bio_to_string(bio) |
| 1538 | |
| 1539 | |
| 1540 | def _delete_reason(self): |
| 1541 | stack = self._revoked.extensions |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1542 | for i in range(_lib.sk_X509_EXTENSION_num(stack)): |
| 1543 | ext = _lib.sk_X509_EXTENSION_value(stack, i) |
| 1544 | if _lib.OBJ_obj2nid(ext.object) == _lib.NID_crl_reason: |
| 1545 | _lib.X509_EXTENSION_free(ext) |
| 1546 | _lib.sk_X509_EXTENSION_delete(stack, i) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1547 | break |
| 1548 | |
| 1549 | |
| 1550 | def set_reason(self, reason): |
| 1551 | """ |
| 1552 | Set the reason of a Revoked object. |
| 1553 | |
| 1554 | If :py:data:`reason` is :py:data:`None`, delete the reason instead. |
| 1555 | |
| 1556 | :param reason: The reason string. |
| 1557 | :type reason: :py:class:`str` or :py:class:`NoneType` |
| 1558 | :return: None |
| 1559 | """ |
| 1560 | if reason is None: |
| 1561 | self._delete_reason() |
| 1562 | elif not isinstance(reason, bytes): |
| 1563 | raise TypeError("reason must be None or a byte string") |
| 1564 | else: |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1565 | reason = reason.lower().replace(b' ', b'') |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1566 | reason_code = [r.lower() for r in self._crl_reasons].index(reason) |
| 1567 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1568 | new_reason_ext = _lib.ASN1_ENUMERATED_new() |
| 1569 | if new_reason_ext == _ffi.NULL: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 1570 | # TODO: This is untested. |
| 1571 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1572 | 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] | 1573 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1574 | set_result = _lib.ASN1_ENUMERATED_set(new_reason_ext, reason_code) |
| 1575 | if set_result == _ffi.NULL: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 1576 | # TODO: This is untested. |
| 1577 | _raise_current_error() |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1578 | |
| 1579 | self._delete_reason() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1580 | add_result = _lib.X509_REVOKED_add1_ext_i2d( |
| 1581 | self._revoked, _lib.NID_crl_reason, new_reason_ext, 0, 0) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1582 | |
| 1583 | if not add_result: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 1584 | # TODO: This is untested. |
| 1585 | _raise_current_error() |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1586 | |
| 1587 | |
| 1588 | def get_reason(self): |
| 1589 | """ |
| 1590 | Return the reason of a Revoked object. |
| 1591 | |
| 1592 | :return: The reason as a string |
| 1593 | """ |
| 1594 | extensions = self._revoked.extensions |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1595 | for i in range(_lib.sk_X509_EXTENSION_num(extensions)): |
| 1596 | ext = _lib.sk_X509_EXTENSION_value(extensions, i) |
| 1597 | if _lib.OBJ_obj2nid(ext.object) == _lib.NID_crl_reason: |
Jean-Paul Calderone | fd37136 | 2013-03-01 20:53:58 -0800 | [diff] [blame] | 1598 | bio = _new_mem_buf() |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1599 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1600 | print_result = _lib.X509V3_EXT_print(bio, ext, 0, 0) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1601 | if not print_result: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1602 | print_result = _lib.M_ASN1_OCTET_STRING_print(bio, ext.value) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1603 | if print_result == 0: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 1604 | # TODO: This is untested. |
| 1605 | _raise_current_error() |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1606 | |
| 1607 | return _bio_to_string(bio) |
| 1608 | |
| 1609 | |
| 1610 | def all_reasons(self): |
| 1611 | """ |
| 1612 | Return a list of all the supported reason strings. |
| 1613 | |
| 1614 | :return: A list of reason strings. |
| 1615 | """ |
| 1616 | return self._crl_reasons[:] |
| 1617 | |
| 1618 | |
| 1619 | def set_rev_date(self, when): |
| 1620 | """ |
| 1621 | Set the revocation timestamp |
| 1622 | |
| 1623 | :param when: A string giving the timestamp, in the format: |
| 1624 | |
| 1625 | YYYYMMDDhhmmssZ |
| 1626 | YYYYMMDDhhmmss+hhmm |
| 1627 | YYYYMMDDhhmmss-hhmm |
| 1628 | |
| 1629 | :return: None |
| 1630 | """ |
| 1631 | return _set_asn1_time(self._revoked.revocationDate, when) |
| 1632 | |
| 1633 | |
| 1634 | def get_rev_date(self): |
| 1635 | """ |
| 1636 | Retrieve the revocation date |
| 1637 | |
| 1638 | :return: A string giving the timestamp, in the format: |
| 1639 | |
| 1640 | YYYYMMDDhhmmssZ |
| 1641 | YYYYMMDDhhmmss+hhmm |
| 1642 | YYYYMMDDhhmmss-hhmm |
| 1643 | """ |
| 1644 | return _get_asn1_time(self._revoked.revocationDate) |
| 1645 | |
| 1646 | |
| 1647 | |
| 1648 | class CRL(object): |
| 1649 | def __init__(self): |
| 1650 | """ |
| 1651 | Create a new empty CRL object. |
| 1652 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1653 | crl = _lib.X509_CRL_new() |
| 1654 | self._crl = _ffi.gc(crl, _lib.X509_CRL_free) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1655 | |
| 1656 | |
| 1657 | def get_revoked(self): |
| 1658 | """ |
| 1659 | Return revoked portion of the CRL structure (by value not reference). |
| 1660 | |
| 1661 | :return: A tuple of Revoked objects. |
| 1662 | """ |
| 1663 | results = [] |
| 1664 | revoked_stack = self._crl.crl.revoked |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1665 | for i in range(_lib.sk_X509_REVOKED_num(revoked_stack)): |
| 1666 | revoked = _lib.sk_X509_REVOKED_value(revoked_stack, i) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1667 | revoked_copy = _X509_REVOKED_dup(revoked) |
| 1668 | pyrev = Revoked.__new__(Revoked) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1669 | pyrev._revoked = _ffi.gc(revoked_copy, _lib.X509_REVOKED_free) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1670 | results.append(pyrev) |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 1671 | if results: |
| 1672 | return tuple(results) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1673 | |
| 1674 | |
| 1675 | def add_revoked(self, revoked): |
| 1676 | """ |
| 1677 | Add a revoked (by value not reference) to the CRL structure |
| 1678 | |
| 1679 | :param revoked: The new revoked. |
| 1680 | :type revoked: :class:`X509` |
| 1681 | |
| 1682 | :return: None |
| 1683 | """ |
| 1684 | copy = _X509_REVOKED_dup(revoked._revoked) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1685 | if copy == _ffi.NULL: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 1686 | # TODO: This is untested. |
| 1687 | _raise_current_error() |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1688 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1689 | add_result = _lib.X509_CRL_add0_revoked(self._crl, copy) |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 1690 | if add_result == 0: |
| 1691 | # TODO: This is untested. |
| 1692 | _raise_current_error() |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1693 | |
| 1694 | |
| 1695 | def export(self, cert, key, type=FILETYPE_PEM, days=100): |
| 1696 | """ |
| 1697 | export a CRL as a string |
| 1698 | |
| 1699 | :param cert: Used to sign CRL. |
| 1700 | :type cert: :class:`X509` |
| 1701 | |
| 1702 | :param key: Used to sign CRL. |
| 1703 | :type key: :class:`PKey` |
| 1704 | |
| 1705 | :param type: The export format, either :py:data:`FILETYPE_PEM`, :py:data:`FILETYPE_ASN1`, or :py:data:`FILETYPE_TEXT`. |
| 1706 | |
| 1707 | :param days: The number of days until the next update of this CRL. |
| 1708 | :type days: :py:data:`int` |
| 1709 | |
| 1710 | :return: :py:data:`str` |
| 1711 | """ |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 1712 | if not isinstance(cert, X509): |
| 1713 | raise TypeError("cert must be an X509 instance") |
| 1714 | if not isinstance(key, PKey): |
| 1715 | raise TypeError("key must be a PKey instance") |
| 1716 | if not isinstance(type, int): |
| 1717 | raise TypeError("type must be an integer") |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1718 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1719 | bio = _lib.BIO_new(_lib.BIO_s_mem()) |
| 1720 | if bio == _ffi.NULL: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 1721 | # TODO: This is untested. |
| 1722 | _raise_current_error() |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 1723 | |
| 1724 | # A scratch time object to give different values to different CRL fields |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1725 | sometime = _lib.ASN1_TIME_new() |
| 1726 | if sometime == _ffi.NULL: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 1727 | # TODO: This is untested. |
| 1728 | _raise_current_error() |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 1729 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1730 | _lib.X509_gmtime_adj(sometime, 0) |
| 1731 | _lib.X509_CRL_set_lastUpdate(self._crl, sometime) |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 1732 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1733 | _lib.X509_gmtime_adj(sometime, days * 24 * 60 * 60) |
| 1734 | _lib.X509_CRL_set_nextUpdate(self._crl, sometime) |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 1735 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1736 | _lib.X509_CRL_set_issuer_name(self._crl, _lib.X509_get_subject_name(cert._x509)) |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 1737 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1738 | sign_result = _lib.X509_CRL_sign(self._crl, key._pkey, _lib.EVP_md5()) |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 1739 | if not sign_result: |
| 1740 | _raise_current_error() |
| 1741 | |
| 1742 | if type == FILETYPE_PEM: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1743 | ret = _lib.PEM_write_bio_X509_CRL(bio, self._crl) |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 1744 | elif type == FILETYPE_ASN1: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1745 | ret = _lib.i2d_X509_CRL_bio(bio, self._crl) |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 1746 | elif type == FILETYPE_TEXT: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1747 | ret = _lib.X509_CRL_print(bio, self._crl) |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 1748 | else: |
| 1749 | raise ValueError( |
| 1750 | "type argument must be FILETYPE_PEM, FILETYPE_ASN1, or FILETYPE_TEXT") |
| 1751 | |
| 1752 | if not ret: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 1753 | # TODO: This is untested. |
| 1754 | _raise_current_error() |
Jean-Paul Calderone | 85b74eb | 2013-02-21 09:15:01 -0800 | [diff] [blame] | 1755 | |
| 1756 | return _bio_to_string(bio) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 1757 | CRLType = CRL |
| 1758 | |
| 1759 | |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 1760 | |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 1761 | class PKCS7(object): |
| 1762 | def type_is_signed(self): |
| 1763 | """ |
| 1764 | Check if this NID_pkcs7_signed object |
| 1765 | |
| 1766 | :return: True if the PKCS7 is of type signed |
| 1767 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1768 | if _lib.PKCS7_type_is_signed(self._pkcs7): |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 1769 | return True |
| 1770 | return False |
| 1771 | |
| 1772 | |
| 1773 | def type_is_enveloped(self): |
| 1774 | """ |
| 1775 | Check if this NID_pkcs7_enveloped object |
| 1776 | |
| 1777 | :returns: True if the PKCS7 is of type enveloped |
| 1778 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1779 | if _lib.PKCS7_type_is_enveloped(self._pkcs7): |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 1780 | return True |
| 1781 | return False |
| 1782 | |
| 1783 | |
| 1784 | def type_is_signedAndEnveloped(self): |
| 1785 | """ |
| 1786 | Check if this NID_pkcs7_signedAndEnveloped object |
| 1787 | |
| 1788 | :returns: True if the PKCS7 is of type signedAndEnveloped |
| 1789 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1790 | if _lib.PKCS7_type_is_signedAndEnveloped(self._pkcs7): |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 1791 | return True |
| 1792 | return False |
| 1793 | |
| 1794 | |
| 1795 | def type_is_data(self): |
| 1796 | """ |
| 1797 | Check if this NID_pkcs7_data object |
| 1798 | |
| 1799 | :return: True if the PKCS7 is of type data |
| 1800 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1801 | if _lib.PKCS7_type_is_data(self._pkcs7): |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 1802 | return True |
| 1803 | return False |
| 1804 | |
| 1805 | |
| 1806 | def get_type_name(self): |
| 1807 | """ |
| 1808 | Returns the type name of the PKCS7 structure |
| 1809 | |
| 1810 | :return: A string with the typename |
| 1811 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1812 | nid = _lib.OBJ_obj2nid(self._pkcs7.type) |
| 1813 | string_type = _lib.OBJ_nid2sn(nid) |
| 1814 | return _ffi.string(string_type) |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 1815 | |
| 1816 | PKCS7Type = PKCS7 |
| 1817 | |
| 1818 | |
| 1819 | |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 1820 | class PKCS12(object): |
| 1821 | def __init__(self): |
| 1822 | self._pkey = None |
| 1823 | self._cert = None |
| 1824 | self._cacerts = None |
| 1825 | self._friendlyname = None |
| 1826 | |
| 1827 | |
| 1828 | def get_certificate(self): |
| 1829 | """ |
| 1830 | Return certificate portion of the PKCS12 structure |
| 1831 | |
| 1832 | :return: X509 object containing the certificate |
| 1833 | """ |
| 1834 | return self._cert |
| 1835 | |
| 1836 | |
| 1837 | def set_certificate(self, cert): |
| 1838 | """ |
| 1839 | Replace the certificate portion of the PKCS12 structure |
| 1840 | |
| 1841 | :param cert: The new certificate. |
| 1842 | :type cert: :py:class:`X509` or :py:data:`None` |
| 1843 | :return: None |
| 1844 | """ |
| 1845 | if not isinstance(cert, X509): |
| 1846 | raise TypeError("cert must be an X509 instance") |
| 1847 | self._cert = cert |
| 1848 | |
| 1849 | |
| 1850 | def get_privatekey(self): |
| 1851 | """ |
| 1852 | Return private key portion of the PKCS12 structure |
| 1853 | |
| 1854 | :returns: PKey object containing the private key |
| 1855 | """ |
| 1856 | return self._pkey |
| 1857 | |
| 1858 | |
| 1859 | def set_privatekey(self, pkey): |
| 1860 | """ |
| 1861 | Replace or set the certificate portion of the PKCS12 structure |
| 1862 | |
| 1863 | :param pkey: The new private key. |
| 1864 | :type pkey: :py:class:`PKey` |
| 1865 | :return: None |
| 1866 | """ |
| 1867 | if not isinstance(pkey, PKey): |
| 1868 | raise TypeError("pkey must be a PKey instance") |
| 1869 | self._pkey = pkey |
| 1870 | |
| 1871 | |
| 1872 | def get_ca_certificates(self): |
| 1873 | """ |
| 1874 | Return CA certificates within of the PKCS12 object |
| 1875 | |
| 1876 | :return: A newly created tuple containing the CA certificates in the chain, |
| 1877 | if any are present, or None if no CA certificates are present. |
| 1878 | """ |
| 1879 | if self._cacerts is not None: |
| 1880 | return tuple(self._cacerts) |
| 1881 | |
| 1882 | |
| 1883 | def set_ca_certificates(self, cacerts): |
| 1884 | """ |
| 1885 | Replace or set the CA certificates withing the PKCS12 object. |
| 1886 | |
| 1887 | :param cacerts: The new CA certificates. |
| 1888 | :type cacerts: :py:data:`None` or an iterable of :py:class:`X509` |
| 1889 | :return: None |
| 1890 | """ |
| 1891 | if cacerts is None: |
| 1892 | self._cacerts = None |
| 1893 | else: |
| 1894 | cacerts = list(cacerts) |
| 1895 | for cert in cacerts: |
| 1896 | if not isinstance(cert, X509): |
| 1897 | raise TypeError("iterable must only contain X509 instances") |
| 1898 | self._cacerts = cacerts |
| 1899 | |
| 1900 | |
| 1901 | def set_friendlyname(self, name): |
| 1902 | """ |
| 1903 | Replace or set the certificate portion of the PKCS12 structure |
| 1904 | |
| 1905 | :param name: The new friendly name. |
| 1906 | :type name: :py:class:`bytes` |
| 1907 | :return: None |
| 1908 | """ |
| 1909 | if name is None: |
| 1910 | self._friendlyname = None |
| 1911 | elif not isinstance(name, bytes): |
| 1912 | raise TypeError("name must be a byte string or None (not %r)" % (name,)) |
| 1913 | self._friendlyname = name |
| 1914 | |
| 1915 | |
| 1916 | def get_friendlyname(self): |
| 1917 | """ |
| 1918 | Return friendly name portion of the PKCS12 structure |
| 1919 | |
| 1920 | :returns: String containing the friendlyname |
| 1921 | """ |
| 1922 | return self._friendlyname |
| 1923 | |
| 1924 | |
| 1925 | def export(self, passphrase=None, iter=2048, maciter=1): |
| 1926 | """ |
| 1927 | Dump a PKCS12 object as a string. See also "man PKCS12_create". |
| 1928 | |
| 1929 | :param passphrase: used to encrypt the PKCS12 |
| 1930 | :type passphrase: :py:data:`bytes` |
| 1931 | |
| 1932 | :param iter: How many times to repeat the encryption |
| 1933 | :type iter: :py:data:`int` |
| 1934 | |
| 1935 | :param maciter: How many times to repeat the MAC |
| 1936 | :type maciter: :py:data:`int` |
| 1937 | |
| 1938 | :return: The string containing the PKCS12 |
| 1939 | """ |
| 1940 | if self._cacerts is None: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1941 | cacerts = _ffi.NULL |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 1942 | else: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1943 | cacerts = _lib.sk_X509_new_null() |
| 1944 | cacerts = _ffi.gc(cacerts, _lib.sk_X509_free) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 1945 | for cert in self._cacerts: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1946 | _lib.sk_X509_push(cacerts, cert._x509) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 1947 | |
| 1948 | if passphrase is None: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1949 | passphrase = _ffi.NULL |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 1950 | |
| 1951 | friendlyname = self._friendlyname |
| 1952 | if friendlyname is None: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1953 | friendlyname = _ffi.NULL |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 1954 | |
| 1955 | if self._pkey is None: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1956 | pkey = _ffi.NULL |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 1957 | else: |
| 1958 | pkey = self._pkey._pkey |
| 1959 | |
| 1960 | if self._cert is None: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1961 | cert = _ffi.NULL |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 1962 | else: |
| 1963 | cert = self._cert._x509 |
| 1964 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1965 | pkcs12 = _lib.PKCS12_create( |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 1966 | passphrase, friendlyname, pkey, cert, cacerts, |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1967 | _lib.NID_pbe_WithSHA1And3_Key_TripleDES_CBC, |
| 1968 | _lib.NID_pbe_WithSHA1And3_Key_TripleDES_CBC, |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 1969 | iter, maciter, 0) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1970 | if pkcs12 == _ffi.NULL: |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 1971 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1972 | pkcs12 = _ffi.gc(pkcs12, _lib.PKCS12_free) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 1973 | |
Jean-Paul Calderone | ef9a3dc | 2013-03-02 16:33:32 -0800 | [diff] [blame] | 1974 | bio = _new_mem_buf() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1975 | _lib.i2d_PKCS12_bio(bio, pkcs12) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 1976 | return _bio_to_string(bio) |
Jean-Paul Calderone | ef9a3dc | 2013-03-02 16:33:32 -0800 | [diff] [blame] | 1977 | |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 1978 | PKCS12Type = PKCS12 |
| 1979 | |
| 1980 | |
| 1981 | |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 1982 | class NetscapeSPKI(object): |
| 1983 | def __init__(self): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1984 | spki = _lib.NETSCAPE_SPKI_new() |
| 1985 | self._spki = _ffi.gc(spki, _lib.NETSCAPE_SPKI_free) |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 1986 | |
| 1987 | |
| 1988 | def sign(self, pkey, digest): |
| 1989 | """ |
| 1990 | Sign the certificate request using the supplied key and digest |
| 1991 | |
| 1992 | :param pkey: The key to sign with |
| 1993 | :param digest: The message digest to use |
| 1994 | :return: None |
| 1995 | """ |
| 1996 | if pkey._only_public: |
| 1997 | raise ValueError("Key has only public part") |
| 1998 | |
| 1999 | if not pkey._initialized: |
| 2000 | raise ValueError("Key is uninitialized") |
| 2001 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2002 | digest_obj = _lib.EVP_get_digestbyname(_byte_string(digest)) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2003 | if digest_obj == _ffi.NULL: |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2004 | raise ValueError("No such digest method") |
| 2005 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2006 | sign_result = _lib.NETSCAPE_SPKI_sign(self._spki, pkey._pkey, digest_obj) |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2007 | if not sign_result: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 2008 | # TODO: This is untested. |
| 2009 | _raise_current_error() |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2010 | |
| 2011 | |
| 2012 | def verify(self, key): |
| 2013 | """ |
| 2014 | Verifies a certificate request using the supplied public key |
| 2015 | |
| 2016 | :param key: a public key |
| 2017 | :return: True if the signature is correct. |
| 2018 | :raise OpenSSL.crypto.Error: If the signature is invalid or there is a |
| 2019 | problem verifying the signature. |
| 2020 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2021 | answer = _lib.NETSCAPE_SPKI_verify(self._spki, key._pkey) |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2022 | if answer <= 0: |
| 2023 | _raise_current_error() |
| 2024 | return True |
| 2025 | |
| 2026 | |
| 2027 | def b64_encode(self): |
| 2028 | """ |
| 2029 | Generate a base64 encoded string from an SPKI |
| 2030 | |
| 2031 | :return: The base64 encoded string |
| 2032 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2033 | encoded = _lib.NETSCAPE_SPKI_b64_encode(self._spki) |
| 2034 | result = _ffi.string(encoded) |
| 2035 | _lib.CRYPTO_free(encoded) |
Jean-Paul Calderone | 2c2e21d | 2013-03-02 16:50:35 -0800 | [diff] [blame] | 2036 | return result |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2037 | |
| 2038 | |
| 2039 | def get_pubkey(self): |
| 2040 | """ |
| 2041 | Get the public key of the certificate |
| 2042 | |
| 2043 | :return: The public key |
| 2044 | """ |
| 2045 | pkey = PKey.__new__(PKey) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2046 | pkey._pkey = _lib.NETSCAPE_SPKI_get_pubkey(self._spki) |
| 2047 | if pkey._pkey == _ffi.NULL: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 2048 | # TODO: This is untested. |
| 2049 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2050 | pkey._pkey = _ffi.gc(pkey._pkey, _lib.EVP_PKEY_free) |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2051 | pkey._only_public = True |
| 2052 | return pkey |
| 2053 | |
| 2054 | |
| 2055 | def set_pubkey(self, pkey): |
| 2056 | """ |
| 2057 | Set the public key of the certificate |
| 2058 | |
| 2059 | :param pkey: The public key |
| 2060 | :return: None |
| 2061 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2062 | set_result = _lib.NETSCAPE_SPKI_set_pubkey(self._spki, pkey._pkey) |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2063 | if not set_result: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 2064 | # TODO: This is untested. |
| 2065 | _raise_current_error() |
Jean-Paul Calderone | 3b89f47 | 2013-02-21 09:32:25 -0800 | [diff] [blame] | 2066 | NetscapeSPKIType = NetscapeSPKI |
| 2067 | |
| 2068 | |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2069 | class _PassphraseHelper(object): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 2070 | def __init__(self, type, passphrase, more_args=False, truncate=False): |
Jean-Paul Calderone | 23478b3 | 2013-02-20 13:31:38 -0800 | [diff] [blame] | 2071 | if type != FILETYPE_PEM and passphrase is not None: |
| 2072 | raise ValueError("only FILETYPE_PEM key format supports encryption") |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2073 | self._passphrase = passphrase |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 2074 | self._more_args = more_args |
| 2075 | self._truncate = truncate |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2076 | self._problems = [] |
| 2077 | |
| 2078 | |
| 2079 | @property |
| 2080 | def callback(self): |
| 2081 | if self._passphrase is None: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2082 | return _ffi.NULL |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2083 | elif isinstance(self._passphrase, bytes): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2084 | return _ffi.NULL |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2085 | elif callable(self._passphrase): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2086 | return _ffi.callback("pem_password_cb", self._read_passphrase) |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2087 | else: |
| 2088 | raise TypeError("Last argument must be string or callable") |
| 2089 | |
| 2090 | |
| 2091 | @property |
| 2092 | def callback_args(self): |
| 2093 | if self._passphrase is None: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2094 | return _ffi.NULL |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2095 | elif isinstance(self._passphrase, bytes): |
| 2096 | return self._passphrase |
| 2097 | elif callable(self._passphrase): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2098 | return _ffi.NULL |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2099 | else: |
| 2100 | raise TypeError("Last argument must be string or callable") |
| 2101 | |
| 2102 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 2103 | def raise_if_problem(self, exceptionType=Error): |
| 2104 | try: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 2105 | _exception_from_error_queue(exceptionType) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 2106 | except exceptionType as e: |
Jean-Paul Calderone | 9b4115f | 2014-01-10 14:06:04 -0500 | [diff] [blame] | 2107 | from_queue = e |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2108 | if self._problems: |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2109 | raise self._problems[0] |
Jean-Paul Calderone | 9b4115f | 2014-01-10 14:06:04 -0500 | [diff] [blame] | 2110 | return from_queue |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2111 | |
| 2112 | |
| 2113 | def _read_passphrase(self, buf, size, rwflag, userdata): |
| 2114 | try: |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 2115 | if self._more_args: |
| 2116 | result = self._passphrase(size, rwflag, userdata) |
| 2117 | else: |
| 2118 | result = self._passphrase(rwflag) |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2119 | if not isinstance(result, bytes): |
| 2120 | raise ValueError("String expected") |
| 2121 | if len(result) > size: |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 2122 | if self._truncate: |
| 2123 | result = result[:size] |
| 2124 | else: |
| 2125 | raise ValueError("passphrase returned by callback is too long") |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2126 | for i in range(len(result)): |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2127 | buf[i] = result[i:i + 1] |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2128 | return len(result) |
| 2129 | except Exception as e: |
| 2130 | self._problems.append(e) |
| 2131 | return 0 |
| 2132 | |
| 2133 | |
| 2134 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 2135 | def load_privatekey(type, buffer, passphrase=None): |
| 2136 | """ |
| 2137 | Load a private key from a buffer |
| 2138 | |
| 2139 | :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1) |
| 2140 | :param buffer: The buffer the key is stored in |
| 2141 | :param passphrase: (optional) if encrypted PEM format, this can be |
| 2142 | either the passphrase to use, or a callback for |
| 2143 | providing the passphrase. |
| 2144 | |
| 2145 | :return: The PKey object |
| 2146 | """ |
Jean-Paul Calderone | 6922a86 | 2014-01-18 10:38:28 -0500 | [diff] [blame] | 2147 | if isinstance(buffer, _text_type): |
| 2148 | buffer = buffer.encode("ascii") |
| 2149 | |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 2150 | bio = _new_mem_buf(buffer) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 2151 | |
Jean-Paul Calderone | 23478b3 | 2013-02-20 13:31:38 -0800 | [diff] [blame] | 2152 | helper = _PassphraseHelper(type, passphrase) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 2153 | if type == FILETYPE_PEM: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2154 | evp_pkey = _lib.PEM_read_bio_PrivateKey( |
| 2155 | bio, _ffi.NULL, helper.callback, helper.callback_args) |
Jean-Paul Calderone | e41f05c | 2013-02-20 13:28:16 -0800 | [diff] [blame] | 2156 | helper.raise_if_problem() |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 2157 | elif type == FILETYPE_ASN1: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2158 | evp_pkey = _lib.d2i_PrivateKey_bio(bio, _ffi.NULL) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 2159 | else: |
| 2160 | raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1") |
| 2161 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2162 | if evp_pkey == _ffi.NULL: |
Jean-Paul Calderone | 31393aa | 2013-02-20 13:22:21 -0800 | [diff] [blame] | 2163 | _raise_current_error() |
| 2164 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 2165 | pkey = PKey.__new__(PKey) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2166 | pkey._pkey = _ffi.gc(evp_pkey, _lib.EVP_PKEY_free) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 2167 | return pkey |
| 2168 | |
| 2169 | |
| 2170 | |
| 2171 | def dump_certificate_request(type, req): |
| 2172 | """ |
| 2173 | Dump a certificate request to a buffer |
| 2174 | |
| 2175 | :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1) |
| 2176 | :param req: The certificate request to dump |
| 2177 | :return: The buffer with the dumped certificate request in |
| 2178 | """ |
Jean-Paul Calderone | ef9a3dc | 2013-03-02 16:33:32 -0800 | [diff] [blame] | 2179 | bio = _new_mem_buf() |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2180 | |
| 2181 | if type == FILETYPE_PEM: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2182 | result_code = _lib.PEM_write_bio_X509_REQ(bio, req._req) |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2183 | elif type == FILETYPE_ASN1: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2184 | result_code = _lib.i2d_X509_REQ_bio(bio, req._req) |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2185 | elif type == FILETYPE_TEXT: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2186 | 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] | 2187 | else: |
Jean-Paul Calderone | c9a395f | 2013-02-20 16:59:21 -0800 | [diff] [blame] | 2188 | raise ValueError("type argument must be FILETYPE_PEM, FILETYPE_ASN1, or FILETYPE_TEXT") |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2189 | |
| 2190 | if result_code == 0: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 2191 | # TODO: This is untested. |
| 2192 | _raise_current_error() |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2193 | |
| 2194 | return _bio_to_string(bio) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 2195 | |
| 2196 | |
| 2197 | |
| 2198 | def load_certificate_request(type, buffer): |
| 2199 | """ |
| 2200 | Load a certificate request from a buffer |
| 2201 | |
| 2202 | :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1) |
| 2203 | :param buffer: The buffer the certificate request is stored in |
| 2204 | :return: The X509Req object |
| 2205 | """ |
Jean-Paul Calderone | 6922a86 | 2014-01-18 10:38:28 -0500 | [diff] [blame] | 2206 | if isinstance(buffer, _text_type): |
| 2207 | buffer = buffer.encode("ascii") |
| 2208 | |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 2209 | bio = _new_mem_buf(buffer) |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2210 | |
| 2211 | if type == FILETYPE_PEM: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2212 | 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] | 2213 | elif type == FILETYPE_ASN1: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2214 | req = _lib.d2i_X509_REQ_bio(bio, _ffi.NULL) |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2215 | else: |
Jean-Paul Calderone | 4a68b40 | 2013-12-29 16:54:58 -0500 | [diff] [blame] | 2216 | raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1") |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2217 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2218 | if req == _ffi.NULL: |
Jean-Paul Calderone | 4a68b40 | 2013-12-29 16:54:58 -0500 | [diff] [blame] | 2219 | # TODO: This is untested. |
| 2220 | _raise_current_error() |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2221 | |
| 2222 | x509req = X509Req.__new__(X509Req) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2223 | x509req._req = _ffi.gc(req, _lib.X509_REQ_free) |
Jean-Paul Calderone | 066f057 | 2013-02-20 13:43:44 -0800 | [diff] [blame] | 2224 | return x509req |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2225 | |
| 2226 | |
| 2227 | |
| 2228 | def sign(pkey, data, digest): |
| 2229 | """ |
| 2230 | Sign data with a digest |
| 2231 | |
| 2232 | :param pkey: Pkey to sign with |
| 2233 | :param data: data to be signed |
| 2234 | :param digest: message digest to use |
| 2235 | :return: signature |
| 2236 | """ |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2237 | digest_obj = _lib.EVP_get_digestbyname(_byte_string(digest)) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2238 | if digest_obj == _ffi.NULL: |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2239 | raise ValueError("No such digest method") |
| 2240 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2241 | md_ctx = _ffi.new("EVP_MD_CTX*") |
| 2242 | md_ctx = _ffi.gc(md_ctx, _lib.EVP_MD_CTX_cleanup) |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2243 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2244 | _lib.EVP_SignInit(md_ctx, digest_obj) |
| 2245 | _lib.EVP_SignUpdate(md_ctx, data, len(data)) |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2246 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2247 | signature_buffer = _ffi.new("unsigned char[]", 512) |
| 2248 | signature_length = _ffi.new("unsigned int*") |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2249 | signature_length[0] = len(signature_buffer) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2250 | final_result = _lib.EVP_SignFinal( |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2251 | md_ctx, signature_buffer, signature_length, pkey._pkey) |
| 2252 | |
| 2253 | if final_result != 1: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 2254 | # TODO: This is untested. |
| 2255 | _raise_current_error() |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2256 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2257 | return _ffi.buffer(signature_buffer, signature_length[0])[:] |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2258 | |
| 2259 | |
| 2260 | |
| 2261 | def verify(cert, signature, data, digest): |
| 2262 | """ |
| 2263 | Verify a signature |
| 2264 | |
| 2265 | :param cert: signing certificate (X509 object) |
| 2266 | :param signature: signature returned by sign function |
| 2267 | :param data: data to be verified |
| 2268 | :param digest: message digest to use |
| 2269 | :return: None if the signature is correct, raise exception otherwise |
| 2270 | """ |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2271 | digest_obj = _lib.EVP_get_digestbyname(_byte_string(digest)) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2272 | if digest_obj == _ffi.NULL: |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2273 | raise ValueError("No such digest method") |
| 2274 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2275 | pkey = _lib.X509_get_pubkey(cert._x509) |
| 2276 | if pkey == _ffi.NULL: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 2277 | # TODO: This is untested. |
| 2278 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2279 | pkey = _ffi.gc(pkey, _lib.EVP_PKEY_free) |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2280 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2281 | md_ctx = _ffi.new("EVP_MD_CTX*") |
| 2282 | md_ctx = _ffi.gc(md_ctx, _lib.EVP_MD_CTX_cleanup) |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2283 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2284 | _lib.EVP_VerifyInit(md_ctx, digest_obj) |
| 2285 | _lib.EVP_VerifyUpdate(md_ctx, data, len(data)) |
| 2286 | verify_result = _lib.EVP_VerifyFinal(md_ctx, signature, len(signature), pkey) |
Jean-Paul Calderone | 8cf4f80 | 2013-02-20 16:45:02 -0800 | [diff] [blame] | 2287 | |
| 2288 | if verify_result != 1: |
| 2289 | _raise_current_error() |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2290 | |
| 2291 | |
| 2292 | |
| 2293 | def load_crl(type, buffer): |
| 2294 | """ |
| 2295 | Load a certificate revocation list from a buffer |
| 2296 | |
| 2297 | :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1) |
| 2298 | :param buffer: The buffer the CRL is stored in |
| 2299 | |
| 2300 | :return: The PKey object |
| 2301 | """ |
Jean-Paul Calderone | 6922a86 | 2014-01-18 10:38:28 -0500 | [diff] [blame] | 2302 | if isinstance(buffer, _text_type): |
| 2303 | buffer = buffer.encode("ascii") |
| 2304 | |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 2305 | bio = _new_mem_buf(buffer) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2306 | |
| 2307 | if type == FILETYPE_PEM: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2308 | 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] | 2309 | elif type == FILETYPE_ASN1: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2310 | crl = _lib.d2i_X509_CRL_bio(bio, _ffi.NULL) |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2311 | else: |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2312 | raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1") |
| 2313 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2314 | if crl == _ffi.NULL: |
Jean-Paul Calderone | 5712298 | 2013-02-21 08:47:05 -0800 | [diff] [blame] | 2315 | _raise_current_error() |
| 2316 | |
| 2317 | result = CRL.__new__(CRL) |
| 2318 | result._crl = crl |
| 2319 | return result |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2320 | |
| 2321 | |
| 2322 | |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2323 | def load_pkcs7_data(type, buffer): |
| 2324 | """ |
| 2325 | Load pkcs7 data from a buffer |
| 2326 | |
| 2327 | :param type: The file type (one of FILETYPE_PEM or FILETYPE_ASN1) |
| 2328 | :param buffer: The buffer with the pkcs7 data. |
| 2329 | :return: The PKCS7 object |
| 2330 | """ |
Jean-Paul Calderone | 6922a86 | 2014-01-18 10:38:28 -0500 | [diff] [blame] | 2331 | if isinstance(buffer, _text_type): |
| 2332 | buffer = buffer.encode("ascii") |
| 2333 | |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 2334 | bio = _new_mem_buf(buffer) |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2335 | |
| 2336 | if type == FILETYPE_PEM: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2337 | 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] | 2338 | elif type == FILETYPE_ASN1: |
| 2339 | pass |
| 2340 | else: |
Jean-Paul Calderone | dba578b | 2013-12-29 17:00:04 -0500 | [diff] [blame] | 2341 | # TODO: This is untested. |
| 2342 | _raise_current_error() |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2343 | raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1") |
| 2344 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2345 | if pkcs7 == _ffi.NULL: |
Jean-Paul Calderone | b0f6471 | 2013-03-03 10:15:39 -0800 | [diff] [blame] | 2346 | _raise_current_error() |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2347 | |
| 2348 | pypkcs7 = PKCS7.__new__(PKCS7) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2349 | pypkcs7._pkcs7 = _ffi.gc(pkcs7, _lib.PKCS7_free) |
Jean-Paul Calderone | 4e8be1c | 2013-02-21 18:31:12 -0800 | [diff] [blame] | 2350 | return pypkcs7 |
| 2351 | |
| 2352 | |
| 2353 | |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2354 | def load_pkcs12(buffer, passphrase): |
| 2355 | """ |
| 2356 | Load a PKCS12 object from a buffer |
| 2357 | |
| 2358 | :param buffer: The buffer the certificate is stored in |
| 2359 | :param passphrase: (Optional) The password to decrypt the PKCS12 lump |
| 2360 | :returns: The PKCS12 object |
| 2361 | """ |
Jean-Paul Calderone | 6922a86 | 2014-01-18 10:38:28 -0500 | [diff] [blame] | 2362 | if isinstance(buffer, _text_type): |
| 2363 | buffer = buffer.encode("ascii") |
| 2364 | |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 2365 | bio = _new_mem_buf(buffer) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2366 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2367 | p12 = _lib.d2i_PKCS12_bio(bio, _ffi.NULL) |
| 2368 | if p12 == _ffi.NULL: |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2369 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2370 | p12 = _ffi.gc(p12, _lib.PKCS12_free) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2371 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2372 | pkey = _ffi.new("EVP_PKEY**") |
| 2373 | cert = _ffi.new("X509**") |
| 2374 | cacerts = _ffi.new("Cryptography_STACK_OF_X509**") |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2375 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2376 | parse_result = _lib.PKCS12_parse(p12, passphrase, pkey, cert, cacerts) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2377 | if not parse_result: |
| 2378 | _raise_current_error() |
| 2379 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2380 | cacerts = _ffi.gc(cacerts[0], _lib.sk_X509_free) |
Jean-Paul Calderone | ef9a3dc | 2013-03-02 16:33:32 -0800 | [diff] [blame] | 2381 | |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2382 | # openssl 1.0.0 sometimes leaves an X509_check_private_key error in the |
| 2383 | # queue for no particular reason. This error isn't interesting to anyone |
| 2384 | # outside this function. It's not even interesting to us. Get rid of it. |
| 2385 | try: |
| 2386 | _raise_current_error() |
| 2387 | except Error: |
| 2388 | pass |
| 2389 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2390 | if pkey[0] == _ffi.NULL: |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2391 | pykey = None |
| 2392 | else: |
| 2393 | pykey = PKey.__new__(PKey) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2394 | pykey._pkey = _ffi.gc(pkey[0], _lib.EVP_PKEY_free) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2395 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2396 | if cert[0] == _ffi.NULL: |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2397 | pycert = None |
| 2398 | friendlyname = None |
| 2399 | else: |
| 2400 | pycert = X509.__new__(X509) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2401 | pycert._x509 = _ffi.gc(cert[0], _lib.X509_free) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2402 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2403 | friendlyname_length = _ffi.new("int*") |
| 2404 | friendlyname_buffer = _lib.X509_alias_get0(cert[0], friendlyname_length) |
| 2405 | friendlyname = _ffi.buffer(friendlyname_buffer, friendlyname_length[0])[:] |
| 2406 | if friendlyname_buffer == _ffi.NULL: |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2407 | friendlyname = None |
| 2408 | |
| 2409 | pycacerts = [] |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2410 | for i in range(_lib.sk_X509_num(cacerts)): |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2411 | pycacert = X509.__new__(X509) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2412 | pycacert._x509 = _lib.sk_X509_value(cacerts, i) |
Jean-Paul Calderone | e5912ce | 2013-02-21 10:49:35 -0800 | [diff] [blame] | 2413 | pycacerts.append(pycacert) |
| 2414 | if not pycacerts: |
| 2415 | pycacerts = None |
| 2416 | |
| 2417 | pkcs12 = PKCS12.__new__(PKCS12) |
| 2418 | pkcs12._pkey = pykey |
| 2419 | pkcs12._cert = pycert |
| 2420 | pkcs12._cacerts = pycacerts |
| 2421 | pkcs12._friendlyname = friendlyname |
| 2422 | return pkcs12 |
Jean-Paul Calderone | 6bb4089 | 2014-01-01 12:21:34 -0500 | [diff] [blame] | 2423 | |
| 2424 | |
| 2425 | def _initialize_openssl_threads(get_ident, Lock): |
| 2426 | import _ssl |
| 2427 | return |
| 2428 | |
| 2429 | locks = list(Lock() for n in range(_lib.CRYPTO_num_locks())) |
| 2430 | |
| 2431 | def locking_function(mode, index, filename, line): |
| 2432 | if mode & _lib.CRYPTO_LOCK: |
| 2433 | locks[index].acquire() |
| 2434 | else: |
| 2435 | locks[index].release() |
| 2436 | |
| 2437 | _lib.CRYPTO_set_id_callback( |
| 2438 | _ffi.callback("unsigned long (*)(void)", get_ident)) |
| 2439 | |
| 2440 | _lib.CRYPTO_set_locking_callback( |
| 2441 | _ffi.callback( |
| 2442 | "void (*)(int, int, const char*, int)", locking_function)) |
| 2443 | |
| 2444 | |
| 2445 | try: |
| 2446 | from thread import get_ident |
| 2447 | from threading import Lock |
| 2448 | except ImportError: |
| 2449 | pass |
| 2450 | else: |
| 2451 | _initialize_openssl_threads(get_ident, Lock) |
| 2452 | del get_ident, Lock |
Jean-Paul Calderone | e324fd6 | 2014-01-11 08:00:33 -0500 | [diff] [blame] | 2453 | |
Jean-Paul Calderone | b64e2a2 | 2014-01-11 08:06:35 -0500 | [diff] [blame] | 2454 | # There are no direct unit tests for this initialization. It is tested |
| 2455 | # indirectly since it is necessary for functions like dump_privatekey when |
| 2456 | # using encryption. |
| 2457 | # |
| 2458 | # Thus OpenSSL.test.test_crypto.FunctionTests.test_dump_privatekey_passphrase |
| 2459 | # and some other similar tests may fail without this (though they may not if |
| 2460 | # the Python runtime has already done some initialization of the underlying |
| 2461 | # OpenSSL library (and is linked against the same one that cryptography is |
| 2462 | # using)). |
Jean-Paul Calderone | e324fd6 | 2014-01-11 08:00:33 -0500 | [diff] [blame] | 2463 | _lib.OpenSSL_add_all_algorithms() |
Jean-Paul Calderone | 11ed8e8 | 2014-01-18 10:21:50 -0500 | [diff] [blame] | 2464 | |
Jean-Paul Calderone | fab157b | 2014-01-18 11:21:38 -0500 | [diff] [blame] | 2465 | # This is similar but exercised mainly by exception_from_error_queue. It calls |
| 2466 | # both ERR_load_crypto_strings() and ERR_load_SSL_strings(). |
| 2467 | _lib.SSL_load_error_strings() |