blob: 880c2b315d22adfae0ab77cac64072aeec61e055 [file] [log] [blame]
Paul Kehrer5d5d28d2015-10-21 18:55:22 -05001import datetime
Paul Kehrer8d887e12015-10-24 09:09:55 -05002
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -05003from base64 import b16encode
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -05004from functools import partial
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -05005from operator import __eq__, __ne__, __lt__, __le__, __gt__, __ge__
6
7from six import (
8 integer_types as _integer_types,
Jean-Paul Calderonef22abcd2014-05-01 09:31:19 -04009 text_type as _text_type,
Alex Gaynor03737182020-07-23 20:40:46 -040010 PY2 as _PY2,
11)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -080012
Alex Gaynorbb971ae2020-08-05 01:14:16 -040013from cryptography import utils, x509
Paul Kehrer72d968b2016-07-29 15:31:04 +080014from cryptography.hazmat.primitives.asymmetric import dsa, rsa
15
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050016from OpenSSL._util import (
17 ffi as _ffi,
18 lib as _lib,
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -050019 exception_from_error_queue as _exception_from_error_queue,
20 byte_string as _byte_string,
Jean-Paul Calderone00f84eb2015-04-13 12:47:21 -040021 native as _native,
Sándor Oroszi43c97762020-09-11 17:17:31 +020022 path_string as _path_string,
Jean-Paul Calderone00f84eb2015-04-13 12:47:21 -040023 UNSPECIFIED as _UNSPECIFIED,
Jean-Paul Calderone39a8d592015-04-13 20:49:50 -040024 text_to_bytes_and_warn as _text_to_bytes_and_warn,
Alex Gaynor67903a62016-06-02 10:37:13 -070025 make_assert as _make_assert,
Jean-Paul Calderone00f84eb2015-04-13 12:47:21 -040026)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -080027
Nicolas Karolak736c6212017-11-26 14:40:28 +010028__all__ = [
Alex Gaynor03737182020-07-23 20:40:46 -040029 "FILETYPE_PEM",
30 "FILETYPE_ASN1",
31 "FILETYPE_TEXT",
32 "TYPE_RSA",
33 "TYPE_DSA",
34 "Error",
35 "PKey",
36 "get_elliptic_curves",
37 "get_elliptic_curve",
38 "X509Name",
39 "X509Extension",
40 "X509Req",
41 "X509",
42 "X509StoreFlags",
43 "X509Store",
44 "X509StoreContextError",
45 "X509StoreContext",
46 "load_certificate",
47 "dump_certificate",
48 "dump_publickey",
49 "dump_privatekey",
50 "Revoked",
51 "CRL",
52 "PKCS7",
53 "PKCS12",
54 "NetscapeSPKI",
55 "load_publickey",
56 "load_privatekey",
57 "dump_certificate_request",
58 "load_certificate_request",
59 "sign",
60 "verify",
61 "dump_crl",
62 "load_crl",
63 "load_pkcs7_data",
64 "load_pkcs12",
Nicolas Karolak736c6212017-11-26 14:40:28 +010065]
66
Jean-Paul Calderone6037d072013-12-28 18:04:00 -050067FILETYPE_PEM = _lib.SSL_FILETYPE_PEM
68FILETYPE_ASN1 = _lib.SSL_FILETYPE_ASN1
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -080069
70# TODO This was an API mistake. OpenSSL has no such constant.
71FILETYPE_TEXT = 2 ** 16 - 1
72
Jean-Paul Calderone6037d072013-12-28 18:04:00 -050073TYPE_RSA = _lib.EVP_PKEY_RSA
74TYPE_DSA = _lib.EVP_PKEY_DSA
Igr2f874f22019-01-21 21:39:37 +030075TYPE_DH = _lib.EVP_PKEY_DH
76TYPE_EC = _lib.EVP_PKEY_EC
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -080077
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -080078
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050079class Error(Exception):
Jean-Paul Calderone511cde02013-12-29 10:31:13 -050080 """
81 An error occurred in an `OpenSSL.crypto` API.
82 """
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050083
84
85_raise_current_error = partial(_exception_from_error_queue, Error)
Alex Gaynor67903a62016-06-02 10:37:13 -070086_openssl_assert = _make_assert(Error)
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050087
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070088
Paul Kehrereb633842016-10-06 11:22:01 +020089def _get_backend():
90 """
91 Importing the backend from cryptography has the side effect of activating
92 the osrandom engine. This mutates the global state of OpenSSL in the
93 process and causes issues for various programs that use subinterpreters or
94 embed Python. By putting the import in this function we can avoid
95 triggering this side effect unless _get_backend is called.
96 """
97 from cryptography.hazmat.backends.openssl.backend import backend
Alex Gaynor03737182020-07-23 20:40:46 -040098
Paul Kehrereb633842016-10-06 11:22:01 +020099 return backend
100
101
Jean-Paul Calderonedba578b2013-12-29 17:00:04 -0500102def _untested_error(where):
103 """
104 An OpenSSL API failed somehow. Additionally, the failure which was
105 encountered isn't one that's exercised by the test suite so future behavior
106 of pyOpenSSL is now somewhat less predictable.
107 """
108 raise RuntimeError("Unknown %s failure" % (where,))
109
110
Jean-Paul Calderonedba578b2013-12-29 17:00:04 -0500111def _new_mem_buf(buffer=None):
112 """
113 Allocate a new OpenSSL memory BIO.
114
115 Arrange for the garbage collector to clean it up automatically.
116
117 :param buffer: None or some bytes to use to put into the BIO so that they
118 can be read out.
119 """
120 if buffer is None:
121 bio = _lib.BIO_new(_lib.BIO_s_mem())
122 free = _lib.BIO_free
123 else:
124 data = _ffi.new("char[]", buffer)
125 bio = _lib.BIO_new_mem_buf(data, len(buffer))
Alex Gaynor5945ea82015-09-05 14:59:06 -0400126
Jean-Paul Calderonedba578b2013-12-29 17:00:04 -0500127 # Keep the memory alive as long as the bio is alive!
128 def free(bio, ref=data):
129 return _lib.BIO_free(bio)
130
Alex Gaynorfb8a2a12016-06-04 18:26:26 -0700131 _openssl_assert(bio != _ffi.NULL)
Jean-Paul Calderonedba578b2013-12-29 17:00:04 -0500132
133 bio = _ffi.gc(bio, free)
134 return bio
135
136
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -0800137def _bio_to_string(bio):
138 """
139 Copy the contents of an OpenSSL BIO object into a Python byte string.
140 """
Alex Gaynor03737182020-07-23 20:40:46 -0400141 result_buffer = _ffi.new("char**")
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500142 buffer_length = _lib.BIO_get_mem_data(bio, result_buffer)
143 return _ffi.buffer(result_buffer[0], buffer_length)[:]
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -0800144
145
Jean-Paul Calderone57122982013-02-21 08:47:05 -0800146def _set_asn1_time(boundary, when):
Jean-Paul Calderonee728e872013-12-29 10:37:15 -0500147 """
148 The the time value of an ASN1 time object.
149
Moriyoshi Koizumi80b25ef2017-06-22 00:54:20 +0900150 @param boundary: An ASN1_TIME pointer (or an object safely
Jean-Paul Calderonee728e872013-12-29 10:37:15 -0500151 castable to that type) which will have its value set.
152 @param when: A string representation of the desired time value.
153
154 @raise TypeError: If C{when} is not a L{bytes} string.
155 @raise ValueError: If C{when} does not represent a time in the required
156 format.
157 @raise RuntimeError: If the time value cannot be set for some other
158 (unspecified) reason.
159 """
Jean-Paul Calderone57122982013-02-21 08:47:05 -0800160 if not isinstance(when, bytes):
161 raise TypeError("when must be a byte string")
162
Moriyoshi Koizumi80b25ef2017-06-22 00:54:20 +0900163 set_result = _lib.ASN1_TIME_set_string(boundary, when)
Jean-Paul Calderone57122982013-02-21 08:47:05 -0800164 if set_result == 0:
Moriyoshi Koizumi80b25ef2017-06-22 00:54:20 +0900165 raise ValueError("Invalid string")
Jean-Paul Calderone57122982013-02-21 08:47:05 -0800166
Alex Gaynor510293e2016-06-02 12:07:59 -0700167
Jean-Paul Calderone57122982013-02-21 08:47:05 -0800168def _get_asn1_time(timestamp):
Jean-Paul Calderonee728e872013-12-29 10:37:15 -0500169 """
170 Retrieve the time value of an ASN1 time object.
171
172 @param timestamp: An ASN1_GENERALIZEDTIME* (or an object safely castable to
173 that type) from which the time value will be retrieved.
174
175 @return: The time value from C{timestamp} as a L{bytes} string in a certain
176 format. Or C{None} if the object contains no time value.
177 """
Alex Gaynor03737182020-07-23 20:40:46 -0400178 string_timestamp = _ffi.cast("ASN1_STRING*", timestamp)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500179 if _lib.ASN1_STRING_length(string_timestamp) == 0:
Jean-Paul Calderone57122982013-02-21 08:47:05 -0800180 return None
Alex Gaynor5945ea82015-09-05 14:59:06 -0400181 elif (
182 _lib.ASN1_STRING_type(string_timestamp) == _lib.V_ASN1_GENERALIZEDTIME
183 ):
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500184 return _ffi.string(_lib.ASN1_STRING_data(string_timestamp))
Jean-Paul Calderone57122982013-02-21 08:47:05 -0800185 else:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500186 generalized_timestamp = _ffi.new("ASN1_GENERALIZEDTIME**")
187 _lib.ASN1_TIME_to_generalizedtime(timestamp, generalized_timestamp)
188 if generalized_timestamp[0] == _ffi.NULL:
Jean-Paul Calderonedba578b2013-12-29 17:00:04 -0500189 # This may happen:
190 # - if timestamp was not an ASN1_TIME
191 # - if allocating memory for the ASN1_GENERALIZEDTIME failed
192 # - if a copy of the time data from timestamp cannot be made for
193 # the newly allocated ASN1_GENERALIZEDTIME
194 #
195 # These are difficult to test. cffi enforces the ASN1_TIME type.
196 # Memory allocation failures are a pain to trigger
197 # deterministically.
198 _untested_error("ASN1_TIME_to_generalizedtime")
Jean-Paul Calderone57122982013-02-21 08:47:05 -0800199 else:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500200 string_timestamp = _ffi.cast(
Alex Gaynor03737182020-07-23 20:40:46 -0400201 "ASN1_STRING*", generalized_timestamp[0]
202 )
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500203 string_data = _lib.ASN1_STRING_data(string_timestamp)
204 string_result = _ffi.string(string_data)
205 _lib.ASN1_GENERALIZEDTIME_free(generalized_timestamp[0])
Jean-Paul Calderone57122982013-02-21 08:47:05 -0800206 return string_result
207
208
Alex Gaynor4aa52c32017-11-20 09:04:08 -0500209class _X509NameInvalidator(object):
210 def __init__(self):
211 self._names = []
212
213 def add(self, name):
214 self._names.append(name)
215
216 def clear(self):
217 for name in self._names:
218 # Breaks the object, but also prevents UAF!
219 del name._name
220
221
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -0800222class PKey(object):
Laurens Van Houtven6e7dd432014-06-17 16:10:57 +0200223 """
224 A class representing an DSA or RSA public key or key pair.
225 """
Alex Gaynor03737182020-07-23 20:40:46 -0400226
Jean-Paul Calderoneedafced2013-02-19 11:48:38 -0800227 _only_public = False
Jean-Paul Calderone09e3bdc2013-02-19 12:15:28 -0800228 _initialized = True
Jean-Paul Calderoneedafced2013-02-19 11:48:38 -0800229
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -0800230 def __init__(self):
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500231 pkey = _lib.EVP_PKEY_new()
232 self._pkey = _ffi.gc(pkey, _lib.EVP_PKEY_free)
Jean-Paul Calderone09e3bdc2013-02-19 12:15:28 -0800233 self._initialized = False
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -0800234
Paul Kehrer72d968b2016-07-29 15:31:04 +0800235 def to_cryptography_key(self):
236 """
237 Export as a ``cryptography`` key.
238
239 :rtype: One of ``cryptography``'s `key interfaces`_.
240
241 .. _key interfaces: https://cryptography.io/en/latest/hazmat/\
242 primitives/asymmetric/rsa/#key-interfaces
243
244 .. versionadded:: 16.1.0
245 """
Paul Kehrereb633842016-10-06 11:22:01 +0200246 backend = _get_backend()
Paul Kehrer72d968b2016-07-29 15:31:04 +0800247 if self._only_public:
248 return backend._evp_pkey_to_public_key(self._pkey)
249 else:
250 return backend._evp_pkey_to_private_key(self._pkey)
251
252 @classmethod
253 def from_cryptography_key(cls, crypto_key):
254 """
255 Construct based on a ``cryptography`` *crypto_key*.
256
257 :param crypto_key: A ``cryptography`` key.
258 :type crypto_key: One of ``cryptography``'s `key interfaces`_.
259
260 :rtype: PKey
261
262 .. versionadded:: 16.1.0
263 """
264 pkey = cls()
Alex Gaynor03737182020-07-23 20:40:46 -0400265 if not isinstance(
266 crypto_key,
267 (
268 rsa.RSAPublicKey,
269 rsa.RSAPrivateKey,
270 dsa.DSAPublicKey,
271 dsa.DSAPrivateKey,
272 ),
273 ):
Paul Kehrer72d968b2016-07-29 15:31:04 +0800274 raise TypeError("Unsupported key type")
275
276 pkey._pkey = crypto_key._evp_pkey
277 if isinstance(crypto_key, (rsa.RSAPublicKey, dsa.DSAPublicKey)):
278 pkey._only_public = True
279 pkey._initialized = True
280 return pkey
281
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -0800282 def generate_key(self, type, bits):
283 """
Laurens Van Houtven90c09142015-04-23 10:52:49 -0700284 Generate a key pair of the given type, with the given number of bits.
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -0800285
Laurens Van Houtven6e7dd432014-06-17 16:10:57 +0200286 This generates a key "into" the this object.
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -0800287
Laurens Van Houtven6e7dd432014-06-17 16:10:57 +0200288 :param type: The key type.
289 :type type: :py:data:`TYPE_RSA` or :py:data:`TYPE_DSA`
290 :param bits: The number of bits.
291 :type bits: :py:data:`int` ``>= 0``
292 :raises TypeError: If :py:data:`type` or :py:data:`bits` isn't
293 of the appropriate type.
294 :raises ValueError: If the number of bits isn't an integer of
295 the appropriate size.
Dan Sully44e767a2016-06-04 18:05:27 -0700296 :return: ``None``
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -0800297 """
298 if not isinstance(type, int):
299 raise TypeError("type must be an integer")
300
301 if not isinstance(bits, int):
302 raise TypeError("bits must be an integer")
303
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -0800304 if type == TYPE_RSA:
305 if bits <= 0:
306 raise ValueError("Invalid number of bits")
307
David Benjamin179eb1d2018-06-05 17:56:07 -0400308 # TODO Check error return
309 exponent = _lib.BN_new()
310 exponent = _ffi.gc(exponent, _lib.BN_free)
311 _lib.BN_set_word(exponent, _lib.RSA_F4)
312
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500313 rsa = _lib.RSA_new()
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -0800314
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500315 result = _lib.RSA_generate_key_ex(rsa, bits, exponent, _ffi.NULL)
Alex Gaynor5bb2bd12016-07-03 10:48:32 -0400316 _openssl_assert(result == 1)
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -0800317
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500318 result = _lib.EVP_PKEY_assign_RSA(self._pkey, rsa)
Alex Gaynor5bb2bd12016-07-03 10:48:32 -0400319 _openssl_assert(result == 1)
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -0800320
321 elif type == TYPE_DSA:
Paul Kehrera0860b92016-03-09 21:39:27 -0400322 dsa = _lib.DSA_new()
Alex Gaynorfb8a2a12016-06-04 18:26:26 -0700323 _openssl_assert(dsa != _ffi.NULL)
Paul Kehrerafa5a662016-03-10 10:29:28 -0400324
325 dsa = _ffi.gc(dsa, _lib.DSA_free)
Paul Kehrera0860b92016-03-09 21:39:27 -0400326 res = _lib.DSA_generate_parameters_ex(
327 dsa, bits, _ffi.NULL, 0, _ffi.NULL, _ffi.NULL, _ffi.NULL
328 )
Alex Gaynorfb8a2a12016-06-04 18:26:26 -0700329 _openssl_assert(res == 1)
Alex Gaynor09a386e2016-07-03 09:32:44 -0400330
331 _openssl_assert(_lib.DSA_generate_key(dsa) == 1)
332 _openssl_assert(_lib.EVP_PKEY_set1_DSA(self._pkey, dsa) == 1)
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -0800333 else:
334 raise Error("No such key type")
335
Jean-Paul Calderone09e3bdc2013-02-19 12:15:28 -0800336 self._initialized = True
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -0800337
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -0800338 def check(self):
339 """
340 Check the consistency of an RSA private key.
341
Laurens Van Houtven6e7dd432014-06-17 16:10:57 +0200342 This is the Python equivalent of OpenSSL's ``RSA_check_key``.
343
Hynek Schlawack01c31672016-12-11 15:14:09 +0100344 :return: ``True`` if key is consistent.
345
346 :raise OpenSSL.crypto.Error: if the key is inconsistent.
347
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -0800348 :raise TypeError: if the key is of a type which cannot be checked.
349 Only RSA keys can currently be checked.
350 """
Jean-Paul Calderonec86fcaf2013-02-20 12:38:33 -0800351 if self._only_public:
352 raise TypeError("public key only")
353
Hynek Schlawack2a91ba32016-01-31 14:18:54 +0100354 if _lib.EVP_PKEY_type(self.type()) != _lib.EVP_PKEY_RSA:
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -0800355 raise TypeError("key type unsupported")
356
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500357 rsa = _lib.EVP_PKEY_get1_RSA(self._pkey)
358 rsa = _ffi.gc(rsa, _lib.RSA_free)
359 result = _lib.RSA_check_key(rsa)
Mrmaxmeier8cd3b172020-03-11 22:03:59 +0100360 if result == 1:
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -0800361 return True
362 _raise_current_error()
363
Jean-Paul Calderonec86fcaf2013-02-20 12:38:33 -0800364 def type(self):
365 """
366 Returns the type of the key
367
368 :return: The type of the key.
369 """
Alex Gaynor0d2aec52017-05-31 04:26:27 -0400370 return _lib.EVP_PKEY_id(self._pkey)
Jean-Paul Calderonec86fcaf2013-02-20 12:38:33 -0800371
Jean-Paul Calderonec86fcaf2013-02-20 12:38:33 -0800372 def bits(self):
373 """
374 Returns the number of bits of the key
375
376 :return: The number of bits of the key.
377 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500378 return _lib.EVP_PKEY_bits(self._pkey)
Alex Chanc6077062016-11-18 13:53:39 +0000379
380
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400381class _EllipticCurve(object):
382 """
Jean-Paul Calderoneaaf516d2014-04-19 09:10:45 -0400383 A representation of a supported elliptic curve.
Jean-Paul Calderone73945e32014-04-30 18:18:01 -0400384
385 @cvar _curves: :py:obj:`None` until an attempt is made to load the curves.
386 Thereafter, a :py:type:`set` containing :py:type:`_EllipticCurve`
387 instances each of which represents one curve supported by the system.
388 @type _curves: :py:type:`NoneType` or :py:type:`set`
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400389 """
Alex Gaynor03737182020-07-23 20:40:46 -0400390
Jean-Paul Calderone73945e32014-04-30 18:18:01 -0400391 _curves = None
392
Alex Gaynor26f1a922019-11-18 00:37:39 -0500393 if not _PY2:
Jean-Paul Calderonea5381052014-05-01 09:32:46 -0400394 # This only necessary on Python 3. Morever, it is broken on Python 2.
Jean-Paul Calderonef22abcd2014-05-01 09:31:19 -0400395 def __ne__(self, other):
Jean-Paul Calderonea5381052014-05-01 09:32:46 -0400396 """
397 Implement cooperation with the right-hand side argument of ``!=``.
398
399 Python 3 seems to have dropped this cooperation in this very narrow
400 circumstance.
401 """
Jean-Paul Calderonef22abcd2014-05-01 09:31:19 -0400402 if isinstance(other, _EllipticCurve):
403 return super(_EllipticCurve, self).__ne__(other)
404 return NotImplemented
Jean-Paul Calderone40da72d2014-05-01 09:25:17 -0400405
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400406 @classmethod
Jean-Paul Calderone73945e32014-04-30 18:18:01 -0400407 def _load_elliptic_curves(cls, lib):
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400408 """
Jean-Paul Calderone73945e32014-04-30 18:18:01 -0400409 Get the curves supported by OpenSSL.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400410
411 :param lib: The OpenSSL library binding object.
Jean-Paul Calderone73945e32014-04-30 18:18:01 -0400412
413 :return: A :py:type:`set` of ``cls`` instances giving the names of the
414 elliptic curves the underlying library supports.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400415 """
Alex Chan84902a22017-04-20 11:50:47 +0100416 num_curves = lib.EC_get_builtin_curves(_ffi.NULL, 0)
Alex Gaynor03737182020-07-23 20:40:46 -0400417 builtin_curves = _ffi.new("EC_builtin_curve[]", num_curves)
Alex Chan84902a22017-04-20 11:50:47 +0100418 # The return value on this call should be num_curves again. We
419 # could check it to make sure but if it *isn't* then.. what could
420 # we do? Abort the whole process, I suppose...? -exarkun
421 lib.EC_get_builtin_curves(builtin_curves, num_curves)
Alex Gaynor03737182020-07-23 20:40:46 -0400422 return set(cls.from_nid(lib, c.nid) for c in builtin_curves)
Jean-Paul Calderone73945e32014-04-30 18:18:01 -0400423
Jean-Paul Calderone73945e32014-04-30 18:18:01 -0400424 @classmethod
425 def _get_elliptic_curves(cls, lib):
426 """
427 Get, cache, and return the curves supported by OpenSSL.
428
429 :param lib: The OpenSSL library binding object.
430
431 :return: A :py:type:`set` of ``cls`` instances giving the names of the
432 elliptic curves the underlying library supports.
433 """
434 if cls._curves is None:
435 cls._curves = cls._load_elliptic_curves(lib)
436 return cls._curves
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400437
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400438 @classmethod
439 def from_nid(cls, lib, nid):
Jean-Paul Calderoneaaf516d2014-04-19 09:10:45 -0400440 """
441 Instantiate a new :py:class:`_EllipticCurve` associated with the given
442 OpenSSL NID.
443
444 :param lib: The OpenSSL library binding object.
445
446 :param nid: The OpenSSL NID the resulting curve object will represent.
447 This must be a curve NID (and not, for example, a hash NID) or
448 subsequent operations will fail in unpredictable ways.
449 :type nid: :py:class:`int`
450
451 :return: The curve object.
452 """
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400453 return cls(lib, nid, _ffi.string(lib.OBJ_nid2sn(nid)).decode("ascii"))
454
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400455 def __init__(self, lib, nid, name):
Jean-Paul Calderoneaaf516d2014-04-19 09:10:45 -0400456 """
457 :param _lib: The :py:mod:`cryptography` binding instance used to
458 interface with OpenSSL.
459
460 :param _nid: The OpenSSL NID identifying the curve this object
461 represents.
462 :type _nid: :py:class:`int`
463
464 :param name: The OpenSSL short name identifying the curve this object
465 represents.
466 :type name: :py:class:`unicode`
467 """
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400468 self._lib = lib
469 self._nid = nid
470 self.name = name
471
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400472 def __repr__(self):
473 return "<Curve %r>" % (self.name,)
474
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400475 def _to_EC_KEY(self):
Jean-Paul Calderoneaaf516d2014-04-19 09:10:45 -0400476 """
477 Create a new OpenSSL EC_KEY structure initialized to use this curve.
478
479 The structure is automatically garbage collected when the Python object
480 is garbage collected.
481 """
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400482 key = self._lib.EC_KEY_new_by_curve_name(self._nid)
483 return _ffi.gc(key, _lib.EC_KEY_free)
484
485
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400486def get_elliptic_curves():
Jean-Paul Calderoneaaf516d2014-04-19 09:10:45 -0400487 """
488 Return a set of objects representing the elliptic curves supported in the
489 OpenSSL build in use.
490
491 The curve objects have a :py:class:`unicode` ``name`` attribute by which
492 they identify themselves.
493
494 The curve objects are useful as values for the argument accepted by
Jean-Paul Calderone3b04e352014-04-19 09:29:10 -0400495 :py:meth:`Context.set_tmp_ecdh` to specify which elliptical curve should be
496 used for ECDHE key exchange.
Jean-Paul Calderoneaaf516d2014-04-19 09:10:45 -0400497 """
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400498 return _EllipticCurve._get_elliptic_curves(_lib)
499
500
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400501def get_elliptic_curve(name):
Jean-Paul Calderoneaaf516d2014-04-19 09:10:45 -0400502 """
503 Return a single curve object selected by name.
504
505 See :py:func:`get_elliptic_curves` for information about curve objects.
506
Jean-Paul Calderoned5839e22014-04-19 09:26:44 -0400507 :param name: The OpenSSL short name identifying the curve object to
508 retrieve.
509 :type name: :py:class:`unicode`
510
Jean-Paul Calderoneaaf516d2014-04-19 09:10:45 -0400511 If the named curve is not supported then :py:class:`ValueError` is raised.
512 """
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400513 for curve in get_elliptic_curves():
514 if curve.name == name:
515 return curve
516 raise ValueError("unknown curve name", name)
517
518
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800519class X509Name(object):
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200520 """
521 An X.509 Distinguished Name.
522
523 :ivar countryName: The country of the entity.
524 :ivar C: Alias for :py:attr:`countryName`.
525
526 :ivar stateOrProvinceName: The state or province of the entity.
527 :ivar ST: Alias for :py:attr:`stateOrProvinceName`.
528
529 :ivar localityName: The locality of the entity.
530 :ivar L: Alias for :py:attr:`localityName`.
531
532 :ivar organizationName: The organization name of the entity.
533 :ivar O: Alias for :py:attr:`organizationName`.
534
535 :ivar organizationalUnitName: The organizational unit of the entity.
536 :ivar OU: Alias for :py:attr:`organizationalUnitName`
537
538 :ivar commonName: The common name of the entity.
539 :ivar CN: Alias for :py:attr:`commonName`.
540
541 :ivar emailAddress: The e-mail address of the entity.
542 """
Alex Gaynor5945ea82015-09-05 14:59:06 -0400543
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800544 def __init__(self, name):
545 """
546 Create a new X509Name, copying the given X509Name instance.
547
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200548 :param name: The name to copy.
549 :type name: :py:class:`X509Name`
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800550 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500551 name = _lib.X509_NAME_dup(name._name)
552 self._name = _ffi.gc(name, _lib.X509_NAME_free)
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800553
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800554 def __setattr__(self, name, value):
Alex Gaynor03737182020-07-23 20:40:46 -0400555 if name.startswith("_"):
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800556 return super(X509Name, self).__setattr__(name, value)
557
Jean-Paul Calderoneff363be2013-03-03 10:21:23 -0800558 # Note: we really do not want str subclasses here, so we do not use
559 # isinstance.
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800560 if type(name) is not str:
Alex Gaynor03737182020-07-23 20:40:46 -0400561 raise TypeError(
562 "attribute name must be string, not '%.200s'"
563 % (type(value).__name__,)
564 )
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800565
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -0500566 nid = _lib.OBJ_txt2nid(_byte_string(name))
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500567 if nid == _lib.NID_undef:
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800568 try:
569 _raise_current_error()
570 except Error:
571 pass
572 raise AttributeError("No such attribute")
573
574 # If there's an old entry for this NID, remove it
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500575 for i in range(_lib.X509_NAME_entry_count(self._name)):
576 ent = _lib.X509_NAME_get_entry(self._name, i)
577 ent_obj = _lib.X509_NAME_ENTRY_get_object(ent)
578 ent_nid = _lib.OBJ_obj2nid(ent_obj)
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800579 if nid == ent_nid:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500580 ent = _lib.X509_NAME_delete_entry(self._name, i)
581 _lib.X509_NAME_ENTRY_free(ent)
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800582 break
583
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -0500584 if isinstance(value, _text_type):
Alex Gaynor03737182020-07-23 20:40:46 -0400585 value = value.encode("utf-8")
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800586
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500587 add_result = _lib.X509_NAME_add_entry_by_NID(
Alex Gaynor03737182020-07-23 20:40:46 -0400588 self._name, nid, _lib.MBSTRING_UTF8, value, -1, -1, 0
589 )
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800590 if not add_result:
Jean-Paul Calderone5300d6a2013-12-29 16:36:50 -0500591 _raise_current_error()
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800592
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800593 def __getattr__(self, name):
594 """
595 Find attribute. An X509Name object has the following attributes:
596 countryName (alias C), stateOrProvince (alias ST), locality (alias L),
Alex Gaynor5945ea82015-09-05 14:59:06 -0400597 organization (alias O), organizationalUnit (alias OU), commonName
598 (alias CN) and more...
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800599 """
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -0500600 nid = _lib.OBJ_txt2nid(_byte_string(name))
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500601 if nid == _lib.NID_undef:
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800602 # This is a bit weird. OBJ_txt2nid indicated failure, but it seems
603 # a lower level function, a2d_ASN1_OBJECT, also feels the need to
604 # push something onto the error queue. If we don't clean that up
605 # now, someone else will bump into it later and be quite confused.
606 # See lp#314814.
607 try:
608 _raise_current_error()
609 except Error:
610 pass
611 return super(X509Name, self).__getattr__(name)
612
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500613 entry_index = _lib.X509_NAME_get_index_by_NID(self._name, nid, -1)
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800614 if entry_index == -1:
615 return None
616
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500617 entry = _lib.X509_NAME_get_entry(self._name, entry_index)
618 data = _lib.X509_NAME_ENTRY_get_data(entry)
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800619
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500620 result_buffer = _ffi.new("unsigned char**")
621 data_length = _lib.ASN1_STRING_to_UTF8(result_buffer, data)
Alex Gaynor09a386e2016-07-03 09:32:44 -0400622 _openssl_assert(data_length >= 0)
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800623
Jean-Paul Calderoned899af02013-03-19 22:10:37 -0700624 try:
Alex Gaynor03737182020-07-23 20:40:46 -0400625 result = _ffi.buffer(result_buffer[0], data_length)[:].decode(
626 "utf-8"
627 )
Jean-Paul Calderoned899af02013-03-19 22:10:37 -0700628 finally:
629 # XXX untested
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500630 _lib.OPENSSL_free(result_buffer[0])
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800631 return result
632
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -0500633 def _cmp(op):
634 def f(self, other):
635 if not isinstance(other, X509Name):
636 return NotImplemented
637 result = _lib.X509_NAME_cmp(self._name, other._name)
638 return op(result, 0)
Alex Gaynor03737182020-07-23 20:40:46 -0400639
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -0500640 return f
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800641
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -0500642 __eq__ = _cmp(__eq__)
643 __ne__ = _cmp(__ne__)
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800644
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -0500645 __lt__ = _cmp(__lt__)
646 __le__ = _cmp(__le__)
647
648 __gt__ = _cmp(__gt__)
649 __ge__ = _cmp(__ge__)
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800650
651 def __repr__(self):
652 """
653 String representation of an X509Name
654 """
Alex Gaynor962ac212015-09-04 08:06:42 -0400655 result_buffer = _ffi.new("char[]", 512)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500656 format_result = _lib.X509_NAME_oneline(
Alex Gaynor03737182020-07-23 20:40:46 -0400657 self._name, result_buffer, len(result_buffer)
658 )
Alex Gaynorfb8a2a12016-06-04 18:26:26 -0700659 _openssl_assert(format_result != _ffi.NULL)
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800660
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -0500661 return "<X509Name object '%s'>" % (
Alex Gaynor03737182020-07-23 20:40:46 -0400662 _native(_ffi.string(result_buffer)),
663 )
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800664
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800665 def hash(self):
666 """
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200667 Return an integer representation of the first four bytes of the
668 MD5 digest of the DER representation of the name.
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800669
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200670 This is the Python equivalent of OpenSSL's ``X509_NAME_hash``.
671
672 :return: The (integer) hash of this name.
673 :rtype: :py:class:`int`
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800674 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500675 return _lib.X509_NAME_hash(self._name)
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800676
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800677 def der(self):
678 """
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200679 Return the DER encoding of this name.
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800680
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200681 :return: The DER encoded form of this name.
682 :rtype: :py:class:`bytes`
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800683 """
Alex Gaynor03737182020-07-23 20:40:46 -0400684 result_buffer = _ffi.new("unsigned char**")
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500685 encode_result = _lib.i2d_X509_NAME(self._name, result_buffer)
Alex Gaynor09a386e2016-07-03 09:32:44 -0400686 _openssl_assert(encode_result >= 0)
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800687
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500688 string_result = _ffi.buffer(result_buffer[0], encode_result)[:]
689 _lib.OPENSSL_free(result_buffer[0])
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800690 return string_result
691
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800692 def get_components(self):
693 """
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200694 Returns the components of this name, as a sequence of 2-tuples.
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800695
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200696 :return: The components of this name.
697 :rtype: :py:class:`list` of ``name, value`` tuples.
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800698 """
699 result = []
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500700 for i in range(_lib.X509_NAME_entry_count(self._name)):
701 ent = _lib.X509_NAME_get_entry(self._name, i)
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800702
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500703 fname = _lib.X509_NAME_ENTRY_get_object(ent)
704 fval = _lib.X509_NAME_ENTRY_get_data(ent)
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800705
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500706 nid = _lib.OBJ_obj2nid(fname)
707 name = _lib.OBJ_nid2sn(nid)
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800708
Romuald Brunet4183beb2019-01-21 19:38:33 +0100709 # ffi.string does not handle strings containing NULL bytes
710 # (which may have been generated by old, broken software)
Alex Gaynor03737182020-07-23 20:40:46 -0400711 value = _ffi.buffer(
712 _lib.ASN1_STRING_data(fval), _lib.ASN1_STRING_length(fval)
713 )[:]
Romuald Brunet4183beb2019-01-21 19:38:33 +0100714 result.append((_ffi.string(name), value))
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -0800715
716 return result
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200717
718
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -0800719class X509Extension(object):
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200720 """
721 An X.509 v3 certificate extension.
722 """
Alex Gaynor5945ea82015-09-05 14:59:06 -0400723
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -0800724 def __init__(self, type_name, critical, value, subject=None, issuer=None):
725 """
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200726 Initializes an X509 extension.
727
Hynek Schlawack8d4f9762016-03-19 08:15:03 +0100728 :param type_name: The name of the type of extension_ to create.
Alex Gaynor6f719912015-09-20 09:21:29 -0400729 :type type_name: :py:data:`bytes`
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -0800730
Alex Gaynor5945ea82015-09-05 14:59:06 -0400731 :param bool critical: A flag indicating whether this is a critical
732 extension.
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -0800733
734 :param value: The value of the extension.
Maximilian Hils0de43752015-09-18 15:26:54 +0200735 :type value: :py:data:`bytes`
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -0800736
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200737 :param subject: Optional X509 certificate to use as subject.
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -0800738 :type subject: :py:class:`X509`
739
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200740 :param issuer: Optional X509 certificate to use as issuer.
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -0800741 :type issuer: :py:class:`X509`
Hynek Schlawack8d4f9762016-03-19 08:15:03 +0100742
Alex Chan54005ce2017-03-21 08:08:17 +0000743 .. _extension: https://www.openssl.org/docs/manmaster/man5/
Hynek Schlawack8d4f9762016-03-19 08:15:03 +0100744 x509v3_config.html#STANDARD-EXTENSIONS
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -0800745 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500746 ctx = _ffi.new("X509V3_CTX*")
Jean-Paul Calderoned418a9c2013-02-20 16:24:55 -0800747
Alex Gaynor5945ea82015-09-05 14:59:06 -0400748 # A context is necessary for any extension which uses the r2i
749 # conversion method. That is, X509V3_EXT_nconf may segfault if passed
750 # a NULL ctx. Start off by initializing most of the fields to NULL.
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500751 _lib.X509V3_set_ctx(ctx, _ffi.NULL, _ffi.NULL, _ffi.NULL, _ffi.NULL, 0)
Jean-Paul Calderoned418a9c2013-02-20 16:24:55 -0800752
753 # We have no configuration database - but perhaps we should (some
754 # extensions may require it).
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500755 _lib.X509V3_set_ctx_nodb(ctx)
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -0800756
Jean-Paul Calderoned418a9c2013-02-20 16:24:55 -0800757 # Initialize the subject and issuer, if appropriate. ctx is a local,
758 # and as far as I can tell none of the X509V3_* APIs invoked here steal
Alex Gaynora738ed52015-09-05 11:17:10 -0400759 # any references, so no need to mess with reference counts or
760 # duplicates.
Jean-Paul Calderoned418a9c2013-02-20 16:24:55 -0800761 if issuer is not None:
762 if not isinstance(issuer, X509):
763 raise TypeError("issuer must be an X509 instance")
764 ctx.issuer_cert = issuer._x509
765 if subject is not None:
766 if not isinstance(subject, X509):
767 raise TypeError("subject must be an X509 instance")
768 ctx.subject_cert = subject._x509
769
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -0800770 if critical:
771 # There are other OpenSSL APIs which would let us pass in critical
772 # separately, but they're harder to use, and since value is already
773 # a pile of crappy junk smuggling a ton of utterly important
774 # structured data, what's the point of trying to avoid nasty stuff
Alex Gaynor5945ea82015-09-05 14:59:06 -0400775 # with strings? (However, X509V3_EXT_i2d in particular seems like
776 # it would be a better API to invoke. I do not know where to get
777 # the ext_struc it desires for its last parameter, though.)
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -0500778 value = b"critical," + value
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -0800779
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500780 extension = _lib.X509V3_EXT_nconf(_ffi.NULL, ctx, type_name, value)
781 if extension == _ffi.NULL:
Jean-Paul Calderoned418a9c2013-02-20 16:24:55 -0800782 _raise_current_error()
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500783 self._extension = _ffi.gc(extension, _lib.X509_EXTENSION_free)
Jean-Paul Calderoned418a9c2013-02-20 16:24:55 -0800784
Jean-Paul Calderoneed0c57b2013-10-06 08:31:40 -0400785 @property
786 def _nid(self):
Paul Kehrere8f91cc2016-03-09 21:26:29 -0400787 return _lib.OBJ_obj2nid(
788 _lib.X509_EXTENSION_get_object(self._extension)
789 )
Jean-Paul Calderoneed0c57b2013-10-06 08:31:40 -0400790
791 _prefixes = {
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -0500792 _lib.GEN_EMAIL: "email",
793 _lib.GEN_DNS: "DNS",
794 _lib.GEN_URI: "URI",
Alex Gaynora738ed52015-09-05 11:17:10 -0400795 }
Jean-Paul Calderoneed0c57b2013-10-06 08:31:40 -0400796
797 def _subjectAltNameString(self):
Alex Gaynord61c46a2017-06-29 22:51:33 -0700798 names = _ffi.cast(
799 "GENERAL_NAMES*", _lib.X509V3_EXT_d2i(self._extension)
800 )
Jean-Paul Calderoneed0c57b2013-10-06 08:31:40 -0400801
Paul Kehrerb7d79502015-05-04 07:43:51 -0500802 names = _ffi.gc(names, _lib.GENERAL_NAMES_free)
Jean-Paul Calderoneed0c57b2013-10-06 08:31:40 -0400803 parts = []
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500804 for i in range(_lib.sk_GENERAL_NAME_num(names)):
805 name = _lib.sk_GENERAL_NAME_value(names, i)
Jean-Paul Calderoneed0c57b2013-10-06 08:31:40 -0400806 try:
807 label = self._prefixes[name.type]
808 except KeyError:
809 bio = _new_mem_buf()
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500810 _lib.GENERAL_NAME_print(bio, name)
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -0500811 parts.append(_native(_bio_to_string(bio)))
Jean-Paul Calderoneed0c57b2013-10-06 08:31:40 -0400812 else:
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -0500813 value = _native(
Alex Gaynor03737182020-07-23 20:40:46 -0400814 _ffi.buffer(name.d.ia5.data, name.d.ia5.length)[:]
815 )
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -0500816 parts.append(label + ":" + value)
817 return ", ".join(parts)
Jean-Paul Calderoneed0c57b2013-10-06 08:31:40 -0400818
Jean-Paul Calderoned418a9c2013-02-20 16:24:55 -0800819 def __str__(self):
820 """
821 :return: a nice text representation of the extension
822 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500823 if _lib.NID_subject_alt_name == self._nid:
Jean-Paul Calderoneed0c57b2013-10-06 08:31:40 -0400824 return self._subjectAltNameString()
Jean-Paul Calderoned418a9c2013-02-20 16:24:55 -0800825
Jean-Paul Calderoneed0c57b2013-10-06 08:31:40 -0400826 bio = _new_mem_buf()
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500827 print_result = _lib.X509V3_EXT_print(bio, self._extension, 0, 0)
Alex Gaynor09a386e2016-07-03 09:32:44 -0400828 _openssl_assert(print_result != 0)
Jean-Paul Calderoned418a9c2013-02-20 16:24:55 -0800829
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -0500830 return _native(_bio_to_string(bio))
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -0800831
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -0800832 def get_critical(self):
833 """
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200834 Returns the critical field of this X.509 extension.
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -0800835
836 :return: The critical field.
837 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500838 return _lib.X509_EXTENSION_get_critical(self._extension)
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -0800839
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -0800840 def get_short_name(self):
841 """
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200842 Returns the short type name of this X.509 extension.
843
844 The result is a byte string such as :py:const:`b"basicConstraints"`.
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -0800845
846 :return: The short type name.
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200847 :rtype: :py:data:`bytes`
848
849 .. versionadded:: 0.12
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -0800850 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500851 obj = _lib.X509_EXTENSION_get_object(self._extension)
852 nid = _lib.OBJ_obj2nid(obj)
853 return _ffi.string(_lib.OBJ_nid2sn(nid))
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -0800854
Jean-Paul Calderoned418a9c2013-02-20 16:24:55 -0800855 def get_data(self):
856 """
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200857 Returns the data of the X509 extension, encoded as ASN.1.
Jean-Paul Calderoned418a9c2013-02-20 16:24:55 -0800858
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200859 :return: The ASN.1 encoded data of this X509 extension.
860 :rtype: :py:data:`bytes`
861
862 .. versionadded:: 0.12
Jean-Paul Calderoned418a9c2013-02-20 16:24:55 -0800863 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500864 octet_result = _lib.X509_EXTENSION_get_data(self._extension)
Alex Gaynor03737182020-07-23 20:40:46 -0400865 string_result = _ffi.cast("ASN1_STRING*", octet_result)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500866 char_result = _lib.ASN1_STRING_data(string_result)
867 result_length = _lib.ASN1_STRING_length(string_result)
868 return _ffi.buffer(char_result, result_length)[:]
Jean-Paul Calderoned418a9c2013-02-20 16:24:55 -0800869
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200870
Jean-Paul Calderone066f0572013-02-20 13:43:44 -0800871class X509Req(object):
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200872 """
873 An X.509 certificate signing requests.
874 """
Alex Gaynora738ed52015-09-05 11:17:10 -0400875
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800876 def __init__(self):
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500877 req = _lib.X509_REQ_new()
878 self._req = _ffi.gc(req, _lib.X509_REQ_free)
Alex Gaynor5af32d02016-09-24 01:52:21 -0400879 # Default to version 0.
880 self.set_version(0)
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800881
Paul Kehrer41c10242017-06-29 18:24:17 -0500882 def to_cryptography(self):
883 """
884 Export as a ``cryptography`` certificate signing request.
885
886 :rtype: ``cryptography.x509.CertificateSigningRequest``
887
888 .. versionadded:: 17.1.0
889 """
890 from cryptography.hazmat.backends.openssl.x509 import (
Alex Gaynor03737182020-07-23 20:40:46 -0400891 _CertificateSigningRequest,
Paul Kehrer41c10242017-06-29 18:24:17 -0500892 )
Alex Gaynor03737182020-07-23 20:40:46 -0400893
Paul Kehrer41c10242017-06-29 18:24:17 -0500894 backend = _get_backend()
895 return _CertificateSigningRequest(backend, self._req)
896
897 @classmethod
898 def from_cryptography(cls, crypto_req):
899 """
900 Construct based on a ``cryptography`` *crypto_req*.
901
902 :param crypto_req: A ``cryptography`` X.509 certificate signing request
903 :type crypto_req: ``cryptography.x509.CertificateSigningRequest``
904
Gaurav Malhotra4121e252019-01-22 00:09:19 +0530905 :rtype: X509Req
Paul Kehrer41c10242017-06-29 18:24:17 -0500906
907 .. versionadded:: 17.1.0
908 """
909 if not isinstance(crypto_req, x509.CertificateSigningRequest):
910 raise TypeError("Must be a certificate signing request")
911
912 req = cls()
913 req._req = crypto_req._x509_req
914 return req
915
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800916 def set_pubkey(self, pkey):
917 """
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200918 Set the public key of the certificate signing request.
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800919
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200920 :param pkey: The public key to use.
921 :type pkey: :py:class:`PKey`
922
Dan Sully44e767a2016-06-04 18:05:27 -0700923 :return: ``None``
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800924 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500925 set_result = _lib.X509_REQ_set_pubkey(self._req, pkey._pkey)
Alex Gaynor09a386e2016-07-03 09:32:44 -0400926 _openssl_assert(set_result == 1)
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800927
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800928 def get_pubkey(self):
929 """
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200930 Get the public key of the certificate signing request.
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800931
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200932 :return: The public key.
933 :rtype: :py:class:`PKey`
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800934 """
935 pkey = PKey.__new__(PKey)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500936 pkey._pkey = _lib.X509_REQ_get_pubkey(self._req)
Alex Gaynorfb8a2a12016-06-04 18:26:26 -0700937 _openssl_assert(pkey._pkey != _ffi.NULL)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500938 pkey._pkey = _ffi.gc(pkey._pkey, _lib.EVP_PKEY_free)
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800939 pkey._only_public = True
940 return pkey
941
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800942 def set_version(self, version):
943 """
944 Set the version subfield (RFC 2459, section 4.1.2.1) of the certificate
945 request.
946
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200947 :param int version: The version number.
Dan Sully44e767a2016-06-04 18:05:27 -0700948 :return: ``None``
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800949 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500950 set_result = _lib.X509_REQ_set_version(self._req, version)
Alex Gaynor5bb2bd12016-07-03 10:48:32 -0400951 _openssl_assert(set_result == 1)
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800952
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800953 def get_version(self):
954 """
955 Get the version subfield (RFC 2459, section 4.1.2.1) of the certificate
956 request.
957
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200958 :return: The value of the version subfield.
959 :rtype: :py:class:`int`
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800960 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500961 return _lib.X509_REQ_get_version(self._req)
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800962
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800963 def get_subject(self):
964 """
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200965 Return the subject of this certificate signing request.
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800966
Cory Benfield881dc8d2015-12-09 08:25:14 +0000967 This creates a new :class:`X509Name` that wraps the underlying subject
968 name field on the certificate signing request. Modifying it will modify
969 the underlying signing request, and will have the effect of modifying
970 any other :class:`X509Name` that refers to this subject.
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200971
972 :return: The subject of this certificate signing request.
Cory Benfield881dc8d2015-12-09 08:25:14 +0000973 :rtype: :class:`X509Name`
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800974 """
975 name = X509Name.__new__(X509Name)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500976 name._name = _lib.X509_REQ_get_subject_name(self._req)
Alex Gaynorfb8a2a12016-06-04 18:26:26 -0700977 _openssl_assert(name._name != _ffi.NULL)
Jean-Paul Calderonef6745b32013-03-01 15:08:46 -0800978
979 # The name is owned by the X509Req structure. As long as the X509Name
980 # Python object is alive, keep the X509Req Python object alive.
981 name._owner = self
982
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800983 return name
984
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800985 def add_extensions(self, extensions):
986 """
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200987 Add extensions to the certificate signing request.
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800988
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200989 :param extensions: The X.509 extensions to add.
990 :type extensions: iterable of :py:class:`X509Extension`
Dan Sully44e767a2016-06-04 18:05:27 -0700991 :return: ``None``
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800992 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500993 stack = _lib.sk_X509_EXTENSION_new_null()
Alex Gaynorfb8a2a12016-06-04 18:26:26 -0700994 _openssl_assert(stack != _ffi.NULL)
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800995
Jean-Paul Calderone6037d072013-12-28 18:04:00 -0500996 stack = _ffi.gc(stack, _lib.sk_X509_EXTENSION_free)
Jean-Paul Calderonef6745b32013-03-01 15:08:46 -0800997
Jean-Paul Calderone4328d472013-02-20 14:28:46 -0800998 for ext in extensions:
999 if not isinstance(ext, X509Extension):
Jean-Paul Calderonec2154b72013-02-20 14:29:37 -08001000 raise ValueError("One of the elements is not an X509Extension")
Jean-Paul Calderone4328d472013-02-20 14:28:46 -08001001
Jean-Paul Calderonea63714c2013-03-05 17:02:26 -08001002 # TODO push can fail (here and elsewhere)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001003 _lib.sk_X509_EXTENSION_push(stack, ext._extension)
Jean-Paul Calderone4328d472013-02-20 14:28:46 -08001004
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001005 add_result = _lib.X509_REQ_add_extensions(self._req, stack)
Alex Gaynor09a386e2016-07-03 09:32:44 -04001006 _openssl_assert(add_result == 1)
Jean-Paul Calderone4328d472013-02-20 14:28:46 -08001007
Stephen Holsappleadfd39d2014-01-28 17:58:31 -08001008 def get_extensions(self):
Stephen Holsapple7fbdf642014-03-01 20:05:47 -08001009 """
Laurens Van Houtven3e83d242014-06-18 14:29:47 +02001010 Get X.509 extensions in the certificate signing request.
Stephen Holsappleadfd39d2014-01-28 17:58:31 -08001011
Laurens Van Houtven3e83d242014-06-18 14:29:47 +02001012 :return: The X.509 extensions in this request.
1013 :rtype: :py:class:`list` of :py:class:`X509Extension` objects.
1014
1015 .. versionadded:: 0.15
Stephen Holsapple7fbdf642014-03-01 20:05:47 -08001016 """
1017 exts = []
Jean-Paul Calderone9479d732014-03-02 08:04:54 -05001018 native_exts_obj = _lib.X509_REQ_get_extensions(self._req)
Jean-Paul Calderoneb7a79b42014-03-02 08:06:47 -05001019 for i in range(_lib.sk_X509_EXTENSION_num(native_exts_obj)):
Stephen Holsapple7fbdf642014-03-01 20:05:47 -08001020 ext = X509Extension.__new__(X509Extension)
Jean-Paul Calderone9479d732014-03-02 08:04:54 -05001021 ext._extension = _lib.sk_X509_EXTENSION_value(native_exts_obj, i)
Stephen Holsapple7fbdf642014-03-01 20:05:47 -08001022 exts.append(ext)
1023 return exts
Stephen Holsappleadfd39d2014-01-28 17:58:31 -08001024
Jean-Paul Calderone4328d472013-02-20 14:28:46 -08001025 def sign(self, pkey, digest):
1026 """
Laurens Van Houtven6f2e4262015-04-23 10:48:32 -07001027 Sign the certificate signing request with this key and digest type.
Jean-Paul Calderone4328d472013-02-20 14:28:46 -08001028
Laurens Van Houtven3e83d242014-06-18 14:29:47 +02001029 :param pkey: The key pair to sign with.
1030 :type pkey: :py:class:`PKey`
1031 :param digest: The name of the message digest to use for the signature,
Alex Gaynor239e2d32016-09-11 12:36:35 -04001032 e.g. :py:data:`b"sha256"`.
Laurens Van Houtven3e83d242014-06-18 14:29:47 +02001033 :type digest: :py:class:`bytes`
Dan Sully44e767a2016-06-04 18:05:27 -07001034 :return: ``None``
Jean-Paul Calderone4328d472013-02-20 14:28:46 -08001035 """
1036 if pkey._only_public:
1037 raise ValueError("Key has only public part")
1038
1039 if not pkey._initialized:
1040 raise ValueError("Key is uninitialized")
1041
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -05001042 digest_obj = _lib.EVP_get_digestbyname(_byte_string(digest))
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001043 if digest_obj == _ffi.NULL:
Jean-Paul Calderone4328d472013-02-20 14:28:46 -08001044 raise ValueError("No such digest method")
1045
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001046 sign_result = _lib.X509_REQ_sign(self._req, pkey._pkey, digest_obj)
Alex Gaynor09a386e2016-07-03 09:32:44 -04001047 _openssl_assert(sign_result > 0)
Jean-Paul Calderone4328d472013-02-20 14:28:46 -08001048
Jean-Paul Calderone5565f0f2013-03-06 11:10:20 -08001049 def verify(self, pkey):
1050 """
Laurens Van Houtven3e83d242014-06-18 14:29:47 +02001051 Verifies the signature on this certificate signing request.
Jean-Paul Calderone5565f0f2013-03-06 11:10:20 -08001052
Hynek Schlawack01c31672016-12-11 15:14:09 +01001053 :param PKey key: A public key.
1054
1055 :return: ``True`` if the signature is correct.
1056 :rtype: bool
1057
1058 :raises OpenSSL.crypto.Error: If the signature is invalid or there is a
Jean-Paul Calderone5565f0f2013-03-06 11:10:20 -08001059 problem verifying the signature.
1060 """
1061 if not isinstance(pkey, PKey):
1062 raise TypeError("pkey must be a PKey instance")
1063
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001064 result = _lib.X509_REQ_verify(self._req, pkey._pkey)
Jean-Paul Calderone5565f0f2013-03-06 11:10:20 -08001065 if result <= 0:
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -05001066 _raise_current_error()
Jean-Paul Calderone5565f0f2013-03-06 11:10:20 -08001067
1068 return result
1069
1070
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001071class X509(object):
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001072 """
1073 An X.509 certificate.
1074 """
Alex Gaynor03737182020-07-23 20:40:46 -04001075
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001076 def __init__(self):
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001077 x509 = _lib.X509_new()
Hynek Schlawack8a2dd772016-07-31 13:46:20 +02001078 _openssl_assert(x509 != _ffi.NULL)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001079 self._x509 = _ffi.gc(x509, _lib.X509_free)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001080
Alex Gaynor4aa52c32017-11-20 09:04:08 -05001081 self._issuer_invalidator = _X509NameInvalidator()
1082 self._subject_invalidator = _X509NameInvalidator()
1083
1084 @classmethod
1085 def _from_raw_x509_ptr(cls, x509):
1086 cert = cls.__new__(cls)
1087 cert._x509 = _ffi.gc(x509, _lib.X509_free)
1088 cert._issuer_invalidator = _X509NameInvalidator()
1089 cert._subject_invalidator = _X509NameInvalidator()
1090 return cert
1091
Alex Gaynor9939ba12017-06-25 16:28:24 -04001092 def to_cryptography(self):
1093 """
1094 Export as a ``cryptography`` certificate.
1095
1096 :rtype: ``cryptography.x509.Certificate``
1097
1098 .. versionadded:: 17.1.0
1099 """
1100 from cryptography.hazmat.backends.openssl.x509 import _Certificate
Alex Gaynor03737182020-07-23 20:40:46 -04001101
Alex Gaynor9939ba12017-06-25 16:28:24 -04001102 backend = _get_backend()
1103 return _Certificate(backend, self._x509)
1104
1105 @classmethod
1106 def from_cryptography(cls, crypto_cert):
1107 """
1108 Construct based on a ``cryptography`` *crypto_cert*.
1109
1110 :param crypto_key: A ``cryptography`` X.509 certificate.
1111 :type crypto_key: ``cryptography.x509.Certificate``
1112
Gaurav Malhotra4121e252019-01-22 00:09:19 +05301113 :rtype: X509
Alex Gaynor9939ba12017-06-25 16:28:24 -04001114
1115 .. versionadded:: 17.1.0
1116 """
1117 if not isinstance(crypto_cert, x509.Certificate):
1118 raise TypeError("Must be a certificate")
1119
1120 cert = cls()
1121 cert._x509 = crypto_cert._x509
1122 return cert
1123
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001124 def set_version(self, version):
1125 """
Cyril Stoller6f25ced2018-08-27 13:37:51 +02001126 Set the version number of the certificate. Note that the
1127 version value is zero-based, eg. a value of 0 is V1.
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001128
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001129 :param version: The version number of the certificate.
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001130 :type version: :py:class:`int`
1131
Dan Sully44e767a2016-06-04 18:05:27 -07001132 :return: ``None``
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001133 """
1134 if not isinstance(version, int):
1135 raise TypeError("version must be an integer")
1136
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001137 _lib.X509_set_version(self._x509, version)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001138
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001139 def get_version(self):
1140 """
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001141 Return the version number of the certificate.
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001142
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001143 :return: The version number of the certificate.
1144 :rtype: :py:class:`int`
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001145 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001146 return _lib.X509_get_version(self._x509)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001147
Jean-Paul Calderoneedafced2013-02-19 11:48:38 -08001148 def get_pubkey(self):
1149 """
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001150 Get the public key of the certificate.
Jean-Paul Calderoneedafced2013-02-19 11:48:38 -08001151
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001152 :return: The public key.
1153 :rtype: :py:class:`PKey`
Jean-Paul Calderoneedafced2013-02-19 11:48:38 -08001154 """
1155 pkey = PKey.__new__(PKey)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001156 pkey._pkey = _lib.X509_get_pubkey(self._x509)
1157 if pkey._pkey == _ffi.NULL:
Jean-Paul Calderoneedafced2013-02-19 11:48:38 -08001158 _raise_current_error()
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001159 pkey._pkey = _ffi.gc(pkey._pkey, _lib.EVP_PKEY_free)
Jean-Paul Calderoneedafced2013-02-19 11:48:38 -08001160 pkey._only_public = True
1161 return pkey
1162
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -08001163 def set_pubkey(self, pkey):
1164 """
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001165 Set the public key of the certificate.
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -08001166
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001167 :param pkey: The public key.
1168 :type pkey: :py:class:`PKey`
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -08001169
Laurens Van Houtven33fcf122015-04-23 10:50:08 -07001170 :return: :py:data:`None`
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -08001171 """
1172 if not isinstance(pkey, PKey):
1173 raise TypeError("pkey must be a PKey instance")
1174
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001175 set_result = _lib.X509_set_pubkey(self._x509, pkey._pkey)
Alex Gaynor7778e792016-07-03 23:38:48 -04001176 _openssl_assert(set_result == 1)
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -08001177
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -08001178 def sign(self, pkey, digest):
1179 """
Laurens Van Houtven6f2e4262015-04-23 10:48:32 -07001180 Sign the certificate with this key and digest type.
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -08001181
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001182 :param pkey: The key to sign with.
1183 :type pkey: :py:class:`PKey`
1184
1185 :param digest: The name of the message digest to use.
1186 :type digest: :py:class:`bytes`
1187
Laurens Van Houtvena367fe82015-04-23 10:49:12 -07001188 :return: :py:data:`None`
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -08001189 """
1190 if not isinstance(pkey, PKey):
1191 raise TypeError("pkey must be a PKey instance")
1192
Jean-Paul Calderoneedafced2013-02-19 11:48:38 -08001193 if pkey._only_public:
1194 raise ValueError("Key only has public part")
1195
Jean-Paul Calderone09e3bdc2013-02-19 12:15:28 -08001196 if not pkey._initialized:
1197 raise ValueError("Key is uninitialized")
1198
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -05001199 evp_md = _lib.EVP_get_digestbyname(_byte_string(digest))
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001200 if evp_md == _ffi.NULL:
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -08001201 raise ValueError("No such digest method")
1202
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001203 sign_result = _lib.X509_sign(self._x509, pkey._pkey, evp_md)
Alex Gaynor5bb2bd12016-07-03 10:48:32 -04001204 _openssl_assert(sign_result > 0)
Jean-Paul Calderone3e29ccf2013-02-19 11:32:46 -08001205
Jean-Paul Calderonee4aa3fa2013-02-19 12:12:53 -08001206 def get_signature_algorithm(self):
1207 """
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001208 Return the signature algorithm used in the certificate.
Jean-Paul Calderonee4aa3fa2013-02-19 12:12:53 -08001209
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001210 :return: The name of the algorithm.
1211 :rtype: :py:class:`bytes`
1212
1213 :raises ValueError: If the signature algorithm is undefined.
1214
Laurens Van Houtven0dd87402015-04-23 10:47:18 -07001215 .. versionadded:: 0.13
Jean-Paul Calderonee4aa3fa2013-02-19 12:12:53 -08001216 """
Alex Gaynor39ea5312016-06-02 09:12:10 -07001217 algor = _lib.X509_get0_tbs_sigalg(self._x509)
1218 nid = _lib.OBJ_obj2nid(algor.algorithm)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001219 if nid == _lib.NID_undef:
Jean-Paul Calderonee4aa3fa2013-02-19 12:12:53 -08001220 raise ValueError("Undefined signature algorithm")
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001221 return _ffi.string(_lib.OBJ_nid2ln(nid))
Jean-Paul Calderonee4aa3fa2013-02-19 12:12:53 -08001222
Jean-Paul Calderoneb4078722013-02-19 12:01:55 -08001223 def digest(self, digest_name):
1224 """
1225 Return the digest of the X509 object.
1226
1227 :param digest_name: The name of the digest algorithm to use.
1228 :type digest_name: :py:class:`bytes`
1229
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001230 :return: The digest of the object, formatted as
1231 :py:const:`b":"`-delimited hex pairs.
1232 :rtype: :py:class:`bytes`
Jean-Paul Calderoneb4078722013-02-19 12:01:55 -08001233 """
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -05001234 digest = _lib.EVP_get_digestbyname(_byte_string(digest_name))
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001235 if digest == _ffi.NULL:
Jean-Paul Calderoneb4078722013-02-19 12:01:55 -08001236 raise ValueError("No such digest method")
1237
Paul Kehrer9f9113a2016-09-20 20:10:25 -05001238 result_buffer = _ffi.new("unsigned char[]", _lib.EVP_MAX_MD_SIZE)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001239 result_length = _ffi.new("unsigned int[]", 1)
Jean-Paul Calderoneb4078722013-02-19 12:01:55 -08001240 result_length[0] = len(result_buffer)
1241
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001242 digest_result = _lib.X509_digest(
Alex Gaynor03737182020-07-23 20:40:46 -04001243 self._x509, digest, result_buffer, result_length
1244 )
Alex Gaynor09a386e2016-07-03 09:32:44 -04001245 _openssl_assert(digest_result == 1)
Jean-Paul Calderoneb4078722013-02-19 12:01:55 -08001246
Alex Gaynor03737182020-07-23 20:40:46 -04001247 return b":".join(
1248 [
1249 b16encode(ch).upper()
1250 for ch in _ffi.buffer(result_buffer, result_length[0])
1251 ]
1252 )
Jean-Paul Calderoneb4078722013-02-19 12:01:55 -08001253
Jean-Paul Calderone78133852013-02-19 10:41:46 -08001254 def subject_name_hash(self):
1255 """
1256 Return the hash of the X509 subject.
1257
1258 :return: The hash of the subject.
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001259 :rtype: :py:class:`bytes`
Jean-Paul Calderone78133852013-02-19 10:41:46 -08001260 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001261 return _lib.X509_subject_name_hash(self._x509)
Jean-Paul Calderone78133852013-02-19 10:41:46 -08001262
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001263 def set_serial_number(self, serial):
1264 """
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001265 Set the serial number of the certificate.
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001266
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001267 :param serial: The new serial number.
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001268 :type serial: :py:class:`int`
1269
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001270 :return: :py:data`None`
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001271 """
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -05001272 if not isinstance(serial, _integer_types):
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001273 raise TypeError("serial must be an integer")
1274
Jean-Paul Calderone78133852013-02-19 10:41:46 -08001275 hex_serial = hex(serial)[2:]
1276 if not isinstance(hex_serial, bytes):
Alex Gaynor03737182020-07-23 20:40:46 -04001277 hex_serial = hex_serial.encode("ascii")
Jean-Paul Calderone78133852013-02-19 10:41:46 -08001278
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001279 bignum_serial = _ffi.new("BIGNUM**")
Jean-Paul Calderone78133852013-02-19 10:41:46 -08001280
1281 # BN_hex2bn stores the result in &bignum. Unless it doesn't feel like
Alex Gaynor5945ea82015-09-05 14:59:06 -04001282 # it. If bignum is still NULL after this call, then the return value
1283 # is actually the result. I hope. -exarkun
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001284 small_serial = _lib.BN_hex2bn(bignum_serial, hex_serial)
Jean-Paul Calderone78133852013-02-19 10:41:46 -08001285
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001286 if bignum_serial[0] == _ffi.NULL:
1287 set_result = _lib.ASN1_INTEGER_set(
Alex Gaynor03737182020-07-23 20:40:46 -04001288 _lib.X509_get_serialNumber(self._x509), small_serial
1289 )
Jean-Paul Calderone78133852013-02-19 10:41:46 -08001290 if set_result:
1291 # TODO Not tested
1292 _raise_current_error()
1293 else:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001294 asn1_serial = _lib.BN_to_ASN1_INTEGER(bignum_serial[0], _ffi.NULL)
1295 _lib.BN_free(bignum_serial[0])
1296 if asn1_serial == _ffi.NULL:
Jean-Paul Calderone78133852013-02-19 10:41:46 -08001297 # TODO Not tested
1298 _raise_current_error()
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001299 asn1_serial = _ffi.gc(asn1_serial, _lib.ASN1_INTEGER_free)
1300 set_result = _lib.X509_set_serialNumber(self._x509, asn1_serial)
Alex Gaynor37726112016-07-04 09:51:32 -04001301 _openssl_assert(set_result == 1)
Jean-Paul Calderone78133852013-02-19 10:41:46 -08001302
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001303 def get_serial_number(self):
1304 """
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001305 Return the serial number of this certificate.
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001306
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001307 :return: The serial number.
Dan Sully44e767a2016-06-04 18:05:27 -07001308 :rtype: int
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001309 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001310 asn1_serial = _lib.X509_get_serialNumber(self._x509)
1311 bignum_serial = _lib.ASN1_INTEGER_to_BN(asn1_serial, _ffi.NULL)
Jean-Paul Calderone78133852013-02-19 10:41:46 -08001312 try:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001313 hex_serial = _lib.BN_bn2hex(bignum_serial)
Jean-Paul Calderone78133852013-02-19 10:41:46 -08001314 try:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001315 hexstring_serial = _ffi.string(hex_serial)
Jean-Paul Calderone78133852013-02-19 10:41:46 -08001316 serial = int(hexstring_serial, 16)
1317 return serial
1318 finally:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001319 _lib.OPENSSL_free(hex_serial)
Jean-Paul Calderone78133852013-02-19 10:41:46 -08001320 finally:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001321 _lib.BN_free(bignum_serial)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001322
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001323 def gmtime_adj_notAfter(self, amount):
1324 """
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001325 Adjust the time stamp on which the certificate stops being valid.
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001326
Dan Sully44e767a2016-06-04 18:05:27 -07001327 :param int amount: The number of seconds by which to adjust the
1328 timestamp.
1329 :return: ``None``
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001330 """
1331 if not isinstance(amount, int):
1332 raise TypeError("amount must be an integer")
1333
Rosen Penev43a23a32020-08-13 12:07:12 -07001334 notAfter = _lib.X509_getm_notAfter(self._x509)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001335 _lib.X509_gmtime_adj(notAfter, amount)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001336
Jean-Paul Calderone662afe52013-02-20 08:41:11 -08001337 def gmtime_adj_notBefore(self, amount):
1338 """
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001339 Adjust the timestamp on which the certificate starts being valid.
Jean-Paul Calderone662afe52013-02-20 08:41:11 -08001340
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001341 :param amount: The number of seconds by which to adjust the timestamp.
Dan Sully44e767a2016-06-04 18:05:27 -07001342 :return: ``None``
Jean-Paul Calderone662afe52013-02-20 08:41:11 -08001343 """
1344 if not isinstance(amount, int):
1345 raise TypeError("amount must be an integer")
1346
Rosen Penev43a23a32020-08-13 12:07:12 -07001347 notBefore = _lib.X509_getm_notBefore(self._x509)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001348 _lib.X509_gmtime_adj(notBefore, amount)
Jean-Paul Calderone662afe52013-02-20 08:41:11 -08001349
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001350 def has_expired(self):
1351 """
1352 Check whether the certificate has expired.
1353
Dan Sully44e767a2016-06-04 18:05:27 -07001354 :return: ``True`` if the certificate has expired, ``False`` otherwise.
1355 :rtype: bool
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001356 """
Paul Kehrer8d887e12015-10-24 09:09:55 -05001357 time_string = _native(self.get_notAfter())
Paul Kehrerfde45c92016-01-21 12:57:37 -06001358 not_after = datetime.datetime.strptime(time_string, "%Y%m%d%H%M%SZ")
Paul Kehrer5d5d28d2015-10-21 18:55:22 -05001359
Paul Kehrerfde45c92016-01-21 12:57:37 -06001360 return not_after < datetime.datetime.utcnow()
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001361
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001362 def _get_boundary_time(self, which):
Jean-Paul Calderone57122982013-02-21 08:47:05 -08001363 return _get_asn1_time(which(self._x509))
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001364
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001365 def get_notBefore(self):
1366 """
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001367 Get the timestamp at which the certificate starts being valid.
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001368
Paul Kehrerce98ee62017-06-21 06:59:58 -10001369 The timestamp is formatted as an ASN.1 TIME::
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001370
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001371 YYYYMMDDhhmmssZ
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001372
Dan Sully44e767a2016-06-04 18:05:27 -07001373 :return: A timestamp string, or ``None`` if there is none.
1374 :rtype: bytes or NoneType
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001375 """
Rosen Penev43a23a32020-08-13 12:07:12 -07001376 return self._get_boundary_time(_lib.X509_getm_notBefore)
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001377
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001378 def _set_boundary_time(self, which, when):
Jean-Paul Calderone57122982013-02-21 08:47:05 -08001379 return _set_asn1_time(which(self._x509), when)
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001380
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001381 def set_notBefore(self, when):
1382 """
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001383 Set the timestamp at which the certificate starts being valid.
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001384
Paul Kehrerce98ee62017-06-21 06:59:58 -10001385 The timestamp is formatted as an ASN.1 TIME::
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001386
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001387 YYYYMMDDhhmmssZ
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001388
Dan Sully44e767a2016-06-04 18:05:27 -07001389 :param bytes when: A timestamp string.
1390 :return: ``None``
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001391 """
Rosen Penev43a23a32020-08-13 12:07:12 -07001392 return self._set_boundary_time(_lib.X509_getm_notBefore, when)
Jean-Paul Calderoned7d81272013-02-19 13:16:03 -08001393
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001394 def get_notAfter(self):
1395 """
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001396 Get the timestamp at which the certificate stops being valid.
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001397
Paul Kehrerce98ee62017-06-21 06:59:58 -10001398 The timestamp is formatted as an ASN.1 TIME::
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001399
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001400 YYYYMMDDhhmmssZ
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001401
Dan Sully44e767a2016-06-04 18:05:27 -07001402 :return: A timestamp string, or ``None`` if there is none.
1403 :rtype: bytes or NoneType
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001404 """
Rosen Penev43a23a32020-08-13 12:07:12 -07001405 return self._get_boundary_time(_lib.X509_getm_notAfter)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001406
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001407 def set_notAfter(self, when):
1408 """
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001409 Set the timestamp at which the certificate stops being valid.
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001410
Paul Kehrerce98ee62017-06-21 06:59:58 -10001411 The timestamp is formatted as an ASN.1 TIME::
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001412
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001413 YYYYMMDDhhmmssZ
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001414
Dan Sully44e767a2016-06-04 18:05:27 -07001415 :param bytes when: A timestamp string.
1416 :return: ``None``
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001417 """
Rosen Penev43a23a32020-08-13 12:07:12 -07001418 return self._set_boundary_time(_lib.X509_getm_notAfter, when)
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001419
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001420 def _get_name(self, which):
1421 name = X509Name.__new__(X509Name)
1422 name._name = which(self._x509)
Alex Gaynoradd5b072016-06-04 21:04:00 -07001423 _openssl_assert(name._name != _ffi.NULL)
Jean-Paul Calderonef6745b32013-03-01 15:08:46 -08001424
1425 # The name is owned by the X509 structure. As long as the X509Name
1426 # Python object is alive, keep the X509 Python object alive.
1427 name._owner = self
1428
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001429 return name
1430
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001431 def _set_name(self, which, name):
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -08001432 if not isinstance(name, X509Name):
1433 raise TypeError("name must be an X509Name")
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001434 set_result = which(self._x509, name._name)
Alex Gaynor09a386e2016-07-03 09:32:44 -04001435 _openssl_assert(set_result == 1)
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001436
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001437 def get_issuer(self):
1438 """
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001439 Return the issuer of this certificate.
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001440
Cory Benfielde6bcce82015-12-09 08:40:03 +00001441 This creates a new :class:`X509Name` that wraps the underlying issuer
1442 name field on the certificate. Modifying it will modify the underlying
1443 certificate, and will have the effect of modifying any other
1444 :class:`X509Name` that refers to this issuer.
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001445
1446 :return: The issuer of this certificate.
Cory Benfielde6bcce82015-12-09 08:40:03 +00001447 :rtype: :class:`X509Name`
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001448 """
Alex Gaynor4aa52c32017-11-20 09:04:08 -05001449 name = self._get_name(_lib.X509_get_issuer_name)
1450 self._issuer_invalidator.add(name)
1451 return name
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001452
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001453 def set_issuer(self, issuer):
1454 """
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001455 Set the issuer of this certificate.
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001456
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001457 :param issuer: The issuer.
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001458 :type issuer: :py:class:`X509Name`
1459
Dan Sully44e767a2016-06-04 18:05:27 -07001460 :return: ``None``
Jean-Paul Calderonec2bd4e92013-02-20 08:12:36 -08001461 """
Alex Gaynor4aa52c32017-11-20 09:04:08 -05001462 self._set_name(_lib.X509_set_issuer_name, issuer)
1463 self._issuer_invalidator.clear()
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -08001464
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -08001465 def get_subject(self):
1466 """
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001467 Return the subject of this certificate.
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -08001468
Cory Benfielde6bcce82015-12-09 08:40:03 +00001469 This creates a new :class:`X509Name` that wraps the underlying subject
1470 name field on the certificate. Modifying it will modify the underlying
1471 certificate, and will have the effect of modifying any other
1472 :class:`X509Name` that refers to this subject.
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001473
1474 :return: The subject of this certificate.
Cory Benfielde6bcce82015-12-09 08:40:03 +00001475 :rtype: :class:`X509Name`
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -08001476 """
Alex Gaynor4aa52c32017-11-20 09:04:08 -05001477 name = self._get_name(_lib.X509_get_subject_name)
1478 self._subject_invalidator.add(name)
1479 return name
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -08001480
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -08001481 def set_subject(self, subject):
1482 """
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001483 Set the subject of this certificate.
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -08001484
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001485 :param subject: The subject.
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -08001486 :type subject: :py:class:`X509Name`
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001487
Dan Sully44e767a2016-06-04 18:05:27 -07001488 :return: ``None``
Jean-Paul Calderonea9de1952013-02-19 16:58:42 -08001489 """
Alex Gaynor4aa52c32017-11-20 09:04:08 -05001490 self._set_name(_lib.X509_set_subject_name, subject)
1491 self._subject_invalidator.clear()
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -08001492
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -08001493 def get_extension_count(self):
1494 """
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001495 Get the number of extensions on this certificate.
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -08001496
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001497 :return: The number of extensions.
1498 :rtype: :py:class:`int`
1499
1500 .. versionadded:: 0.12
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -08001501 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001502 return _lib.X509_get_ext_count(self._x509)
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -08001503
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -08001504 def add_extensions(self, extensions):
1505 """
1506 Add extensions to the certificate.
1507
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001508 :param extensions: The extensions to add.
1509 :type extensions: An iterable of :py:class:`X509Extension` objects.
Dan Sully44e767a2016-06-04 18:05:27 -07001510 :return: ``None``
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -08001511 """
1512 for ext in extensions:
1513 if not isinstance(ext, X509Extension):
1514 raise ValueError("One of the elements is not an X509Extension")
1515
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001516 add_result = _lib.X509_add_ext(self._x509, ext._extension, -1)
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -08001517 if not add_result:
1518 _raise_current_error()
1519
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -08001520 def get_extension(self, index):
1521 """
1522 Get a specific extension of the certificate by index.
1523
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +02001524 Extensions on a certificate are kept in order. The index
1525 parameter selects which extension will be returned.
1526
1527 :param int index: The index of the extension to retrieve.
1528 :return: The extension at the specified index.
1529 :rtype: :py:class:`X509Extension`
1530 :raises IndexError: If the extension index was out of bounds.
1531
1532 .. versionadded:: 0.12
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -08001533 """
1534 ext = X509Extension.__new__(X509Extension)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001535 ext._extension = _lib.X509_get_ext(self._x509, index)
1536 if ext._extension == _ffi.NULL:
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -08001537 raise IndexError("extension index out of bounds")
1538
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001539 extension = _lib.X509_EXTENSION_dup(ext._extension)
1540 ext._extension = _ffi.gc(extension, _lib.X509_EXTENSION_free)
Jean-Paul Calderone83d22eb2013-02-20 12:19:43 -08001541 return ext
1542
Laurens Van Houtvenef5c83d2014-06-17 15:32:27 +02001543
Dan Sully44e767a2016-06-04 18:05:27 -07001544class X509StoreFlags(object):
1545 """
1546 Flags for X509 verification, used to change the behavior of
1547 :class:`X509Store`.
1548
1549 See `OpenSSL Verification Flags`_ for details.
1550
1551 .. _OpenSSL Verification Flags:
Alex Chan54005ce2017-03-21 08:08:17 +00001552 https://www.openssl.org/docs/manmaster/man3/X509_VERIFY_PARAM_set_flags.html
Dan Sully44e767a2016-06-04 18:05:27 -07001553 """
Alex Gaynor03737182020-07-23 20:40:46 -04001554
Dan Sully44e767a2016-06-04 18:05:27 -07001555 CRL_CHECK = _lib.X509_V_FLAG_CRL_CHECK
1556 CRL_CHECK_ALL = _lib.X509_V_FLAG_CRL_CHECK_ALL
1557 IGNORE_CRITICAL = _lib.X509_V_FLAG_IGNORE_CRITICAL
1558 X509_STRICT = _lib.X509_V_FLAG_X509_STRICT
1559 ALLOW_PROXY_CERTS = _lib.X509_V_FLAG_ALLOW_PROXY_CERTS
1560 POLICY_CHECK = _lib.X509_V_FLAG_POLICY_CHECK
1561 EXPLICIT_POLICY = _lib.X509_V_FLAG_EXPLICIT_POLICY
1562 INHIBIT_MAP = _lib.X509_V_FLAG_INHIBIT_MAP
1563 NOTIFY_POLICY = _lib.X509_V_FLAG_NOTIFY_POLICY
1564 CHECK_SS_SIGNATURE = _lib.X509_V_FLAG_CHECK_SS_SIGNATURE
1565 CB_ISSUER_CHECK = _lib.X509_V_FLAG_CB_ISSUER_CHECK
1566
1567
Jean-Paul Calderonea63714c2013-03-05 17:02:26 -08001568class X509Store(object):
Laurens Van Houtvenef5c83d2014-06-17 15:32:27 +02001569 """
Dan Sully44e767a2016-06-04 18:05:27 -07001570 An X.509 store.
1571
1572 An X.509 store is used to describe a context in which to verify a
1573 certificate. A description of a context may include a set of certificates
1574 to trust, a set of certificate revocation lists, verification flags and
1575 more.
1576
1577 An X.509 store, being only a description, cannot be used by itself to
1578 verify a certificate. To carry out the actual verification process, see
1579 :class:`X509StoreContext`.
Laurens Van Houtvenef5c83d2014-06-17 15:32:27 +02001580 """
Alex Gaynora738ed52015-09-05 11:17:10 -04001581
Jean-Paul Calderonea63714c2013-03-05 17:02:26 -08001582 def __init__(self):
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001583 store = _lib.X509_STORE_new()
1584 self._store = _ffi.gc(store, _lib.X509_STORE_free)
Jean-Paul Calderonea63714c2013-03-05 17:02:26 -08001585
Jean-Paul Calderonea63714c2013-03-05 17:02:26 -08001586 def add_cert(self, cert):
Laurens Van Houtvenef5c83d2014-06-17 15:32:27 +02001587 """
Dan Sully44e767a2016-06-04 18:05:27 -07001588 Adds a trusted certificate to this store.
Laurens Van Houtvenef5c83d2014-06-17 15:32:27 +02001589
Dan Sully44e767a2016-06-04 18:05:27 -07001590 Adding a certificate with this method adds this certificate as a
1591 *trusted* certificate.
Laurens Van Houtvenef5c83d2014-06-17 15:32:27 +02001592
1593 :param X509 cert: The certificate to add to this store.
Hynek Schlawack01c31672016-12-11 15:14:09 +01001594
Dan Sully44e767a2016-06-04 18:05:27 -07001595 :raises TypeError: If the certificate is not an :class:`X509`.
Hynek Schlawack01c31672016-12-11 15:14:09 +01001596
1597 :raises OpenSSL.crypto.Error: If OpenSSL was unhappy with your
1598 certificate.
1599
Dan Sully44e767a2016-06-04 18:05:27 -07001600 :return: ``None`` if the certificate was added successfully.
Laurens Van Houtvenef5c83d2014-06-17 15:32:27 +02001601 """
Jean-Paul Calderonea63714c2013-03-05 17:02:26 -08001602 if not isinstance(cert, X509):
1603 raise TypeError()
1604
Paul Kehrer0e6c5532018-08-23 10:52:15 -05001605 # As of OpenSSL 1.1.0i adding the same cert to the store more than
1606 # once doesn't cause an error. Accordingly, this code now silences
1607 # the error for OpenSSL < 1.1.0i as well.
1608 if _lib.X509_STORE_add_cert(self._store, cert._x509) == 0:
1609 code = _lib.ERR_peek_error()
1610 err_reason = _lib.ERR_GET_REASON(code)
1611 _openssl_assert(
1612 err_reason == _lib.X509_R_CERT_ALREADY_IN_HASH_TABLE
1613 )
1614 _lib.ERR_clear_error()
Dan Sully44e767a2016-06-04 18:05:27 -07001615
1616 def add_crl(self, crl):
1617 """
1618 Add a certificate revocation list to this store.
1619
1620 The certificate revocation lists added to a store will only be used if
1621 the associated flags are configured to check certificate revocation
1622 lists.
1623
1624 .. versionadded:: 16.1.0
1625
1626 :param CRL crl: The certificate revocation list to add to this store.
1627 :return: ``None`` if the certificate revocation list was added
1628 successfully.
1629 """
1630 _openssl_assert(_lib.X509_STORE_add_crl(self._store, crl._crl) != 0)
1631
1632 def set_flags(self, flags):
1633 """
1634 Set verification flags to this store.
1635
1636 Verification flags can be combined by oring them together.
1637
1638 .. note::
1639
1640 Setting a verification flag sometimes requires clients to add
1641 additional information to the store, otherwise a suitable error will
1642 be raised.
1643
1644 For example, in setting flags to enable CRL checking a
1645 suitable CRL must be added to the store otherwise an error will be
1646 raised.
1647
1648 .. versionadded:: 16.1.0
1649
1650 :param int flags: The verification flags to set on this store.
1651 See :class:`X509StoreFlags` for available constants.
1652 :return: ``None`` if the verification flags were successfully set.
1653 """
1654 _openssl_assert(_lib.X509_STORE_set_flags(self._store, flags) != 0)
Jean-Paul Calderonee6f32b82013-03-06 10:27:57 -08001655
Thomas Sileoe15e60a2016-11-22 18:13:30 +01001656 def set_time(self, vfy_time):
1657 """
1658 Set the time against which the certificates are verified.
1659
1660 Normally the current time is used.
1661
1662 .. note::
1663
1664 For example, you can determine if a certificate was valid at a given
1665 time.
1666
Hynek Schlawackf6c96af2017-04-20 12:34:58 +02001667 .. versionadded:: 17.0.0
Thomas Sileoe15e60a2016-11-22 18:13:30 +01001668
1669 :param datetime vfy_time: The verification time to set on this store.
1670 :return: ``None`` if the verification time was successfully set.
1671 """
1672 param = _lib.X509_VERIFY_PARAM_new()
1673 param = _ffi.gc(param, _lib.X509_VERIFY_PARAM_free)
1674
Alex Gaynor03737182020-07-23 20:40:46 -04001675 _lib.X509_VERIFY_PARAM_set_time(param, int(vfy_time.strftime("%s")))
Thomas Sileoe15e60a2016-11-22 18:13:30 +01001676 _openssl_assert(_lib.X509_STORE_set1_param(self._store, param) != 0)
1677
Sándor Oroszi43c97762020-09-11 17:17:31 +02001678 def load_locations(self, cafile, capath=None):
1679 """
1680 Let X509Store know where we can find trusted certificates for the
1681 certificate chain. Note that the certificates have to be in PEM
1682 format.
1683
1684 If *capath* is passed, it must be a directory prepared using the
1685 ``c_rehash`` tool included with OpenSSL. Either, but not both, of
1686 *cafile* or *capath* may be ``None``.
1687
1688 .. note::
1689
1690 Both *cafile* and *capath* may be set simultaneously.
1691
1692 Call this method multiple times to add more than one location.
1693 For example, CA certificates, and certificate revocation list bundles
1694 may be passed in *cafile* in subsequent calls to this method.
1695
1696 .. versionadded:: 20.0
1697
1698 :param cafile: In which file we can find the certificates (``bytes`` or
1699 ``unicode``).
1700 :param capath: In which directory we can find the certificates
1701 (``bytes`` or ``unicode``).
1702
1703 :return: ``None`` if the locations were set successfully.
1704
1705 :raises OpenSSL.crypto.Error: If both *cafile* and *capath* is ``None``
1706 or the locations could not be set for any reason.
1707
1708 """
1709 if cafile is None:
1710 cafile = _ffi.NULL
1711 else:
1712 cafile = _path_string(cafile)
1713
1714 if capath is None:
1715 capath = _ffi.NULL
1716 else:
1717 capath = _path_string(capath)
1718
1719 load_result = _lib.X509_STORE_load_locations(
1720 self._store, cafile, capath
1721 )
1722 if not load_result:
1723 _raise_current_error()
1724
Jean-Paul Calderonea63714c2013-03-05 17:02:26 -08001725
Stephen Holsapple08ffaa62015-01-30 17:18:40 -08001726class X509StoreContextError(Exception):
1727 """
Stephen Holsapple8ad4a192015-06-09 22:51:43 -07001728 An exception raised when an error occurred while verifying a certificate
1729 using `OpenSSL.X509StoreContext.verify_certificate`.
Stephen Holsapple08ffaa62015-01-30 17:18:40 -08001730
Jean-Paul Calderonefeb17432015-03-15 15:49:45 -04001731 :ivar certificate: The certificate which caused verificate failure.
Stephen Holsapple8ad4a192015-06-09 22:51:43 -07001732 :type certificate: :class:`X509`
Stephen Holsapple08ffaa62015-01-30 17:18:40 -08001733 """
Alex Gaynora738ed52015-09-05 11:17:10 -04001734
Stephen Holsapple08ffaa62015-01-30 17:18:40 -08001735 def __init__(self, message, certificate):
1736 super(X509StoreContextError, self).__init__(message)
1737 self.certificate = certificate
1738
Jean-Paul Calderonea63714c2013-03-05 17:02:26 -08001739
Stephen Holsapple0d9815f2014-08-27 19:36:53 -07001740class X509StoreContext(object):
1741 """
1742 An X.509 store context.
1743
Dan Sully44e767a2016-06-04 18:05:27 -07001744 An X.509 store context is used to carry out the actual verification process
1745 of a certificate in a described context. For describing such a context, see
1746 :class:`X509Store`.
Stephen Holsapple0d9815f2014-08-27 19:36:53 -07001747
Jean-Paul Calderoneb7b7fb92015-01-18 15:37:10 -05001748 :ivar _store_ctx: The underlying X509_STORE_CTX structure used by this
1749 instance. It is dynamically allocated and automatically garbage
1750 collected.
Jean-Paul Calderone64b6b842015-03-15 16:08:02 -04001751 :ivar _store: See the ``store`` ``__init__`` parameter.
Jean-Paul Calderone64b6b842015-03-15 16:08:02 -04001752 :ivar _cert: See the ``certificate`` ``__init__`` parameter.
Stephen Holsapple8ad4a192015-06-09 22:51:43 -07001753 :param X509Store store: The certificates which will be trusted for the
1754 purposes of any verifications.
Stephen Holsapple8ad4a192015-06-09 22:51:43 -07001755 :param X509 certificate: The certificate to be verified.
Stephen Holsapple0d9815f2014-08-27 19:36:53 -07001756 """
1757
Jean-Paul Calderoneb7b7fb92015-01-18 15:37:10 -05001758 def __init__(self, store, certificate):
Stephen Holsapple0d9815f2014-08-27 19:36:53 -07001759 store_ctx = _lib.X509_STORE_CTX_new()
1760 self._store_ctx = _ffi.gc(store_ctx, _lib.X509_STORE_CTX_free)
1761 self._store = store
Jean-Paul Calderoneb7b7fb92015-01-18 15:37:10 -05001762 self._cert = certificate
Shane Harvey33c54992020-08-05 16:48:51 -07001763 self._chain = _ffi.NULL
Stephen Holsapple46a09252015-02-12 14:45:43 -08001764 # Make the store context available for use after instantiating this
1765 # class by initializing it now. Per testing, subsequent calls to
Dan Sully44e767a2016-06-04 18:05:27 -07001766 # :meth:`_init` have no adverse affect.
Stephen Holsapple46a09252015-02-12 14:45:43 -08001767 self._init()
Jean-Paul Calderoneb7b7fb92015-01-18 15:37:10 -05001768
Stephen Holsapple0d9815f2014-08-27 19:36:53 -07001769 def _init(self):
1770 """
1771 Set up the store context for a subsequent verification operation.
Jeremy Cline58193f12017-09-13 21:14:53 -04001772
1773 Calling this method more than once without first calling
1774 :meth:`_cleanup` will leak memory.
Stephen Holsapple0d9815f2014-08-27 19:36:53 -07001775 """
Alex Gaynor5945ea82015-09-05 14:59:06 -04001776 ret = _lib.X509_STORE_CTX_init(
Shane Harvey33c54992020-08-05 16:48:51 -07001777 self._store_ctx, self._store._store, self._cert._x509, self._chain
Alex Gaynor5945ea82015-09-05 14:59:06 -04001778 )
Stephen Holsapple0d9815f2014-08-27 19:36:53 -07001779 if ret <= 0:
1780 _raise_current_error()
1781
1782 def _cleanup(self):
1783 """
1784 Internally cleans up the store context.
1785
Dan Sully44e767a2016-06-04 18:05:27 -07001786 The store context can then be reused with a new call to :meth:`_init`.
Stephen Holsapple0d9815f2014-08-27 19:36:53 -07001787 """
1788 _lib.X509_STORE_CTX_cleanup(self._store_ctx)
1789
Stephen Holsapple08ffaa62015-01-30 17:18:40 -08001790 def _exception_from_context(self):
1791 """
1792 Convert an OpenSSL native context error failure into a Python
1793 exception.
1794
Alex Gaynor5945ea82015-09-05 14:59:06 -04001795 When a call to native OpenSSL X509_verify_cert fails, additional
1796 information about the failure can be obtained from the store context.
Stephen Holsapple08ffaa62015-01-30 17:18:40 -08001797 """
1798 errors = [
1799 _lib.X509_STORE_CTX_get_error(self._store_ctx),
1800 _lib.X509_STORE_CTX_get_error_depth(self._store_ctx),
Alex Gaynor03737182020-07-23 20:40:46 -04001801 _native(
1802 _ffi.string(
1803 _lib.X509_verify_cert_error_string(
1804 _lib.X509_STORE_CTX_get_error(self._store_ctx)
1805 )
1806 )
1807 ),
Stephen Holsapple08ffaa62015-01-30 17:18:40 -08001808 ]
Stephen Holsapple1f713eb2015-02-09 19:19:44 -08001809 # A context error should always be associated with a certificate, so we
1810 # expect this call to never return :class:`None`.
Stephen Holsapple08ffaa62015-01-30 17:18:40 -08001811 _x509 = _lib.X509_STORE_CTX_get_current_cert(self._store_ctx)
Stephen Holsapple1f713eb2015-02-09 19:19:44 -08001812 _cert = _lib.X509_dup(_x509)
Alex Gaynor4aa52c32017-11-20 09:04:08 -05001813 pycert = X509._from_raw_x509_ptr(_cert)
Stephen Holsapple08ffaa62015-01-30 17:18:40 -08001814 return X509StoreContextError(errors, pycert)
1815
Stephen Holsapple46a09252015-02-12 14:45:43 -08001816 def set_store(self, store):
1817 """
Dan Sully44e767a2016-06-04 18:05:27 -07001818 Set the context's X.509 store.
Stephen Holsapple46a09252015-02-12 14:45:43 -08001819
Stephen Holsapple8ad4a192015-06-09 22:51:43 -07001820 .. versionadded:: 0.15
1821
Dan Sully44e767a2016-06-04 18:05:27 -07001822 :param X509Store store: The store description which will be used for
1823 the purposes of any *future* verifications.
Stephen Holsapple46a09252015-02-12 14:45:43 -08001824 """
1825 self._store = store
1826
Stephen Holsapple08ffaa62015-01-30 17:18:40 -08001827 def verify_certificate(self):
1828 """
1829 Verify a certificate in a context.
1830
Stephen Holsapple8ad4a192015-06-09 22:51:43 -07001831 .. versionadded:: 0.15
1832
Alex Gaynorca87ff62015-09-04 23:31:03 -04001833 :raises X509StoreContextError: If an error occurred when validating a
Alex Gaynor5945ea82015-09-05 14:59:06 -04001834 certificate in the context. Sets ``certificate`` attribute to
1835 indicate which certificate caused the error.
Stephen Holsapple08ffaa62015-01-30 17:18:40 -08001836 """
Stephen Holsapple46a09252015-02-12 14:45:43 -08001837 # Always re-initialize the store context in case
Dan Sully44e767a2016-06-04 18:05:27 -07001838 # :meth:`verify_certificate` is called multiple times.
Jeremy Cline58193f12017-09-13 21:14:53 -04001839 #
1840 # :meth:`_init` is called in :meth:`__init__` so _cleanup is called
1841 # before _init to ensure memory is not leaked.
1842 self._cleanup()
Stephen Holsapple08ffaa62015-01-30 17:18:40 -08001843 self._init()
1844 ret = _lib.X509_verify_cert(self._store_ctx)
1845 self._cleanup()
1846 if ret <= 0:
1847 raise self._exception_from_context()
1848
Shane Harvey33c54992020-08-05 16:48:51 -07001849 def get_verified_chain(self):
1850 """
1851 Verify a certificate in a context and return the complete validated
1852 chain.
1853
1854 :raises X509StoreContextError: If an error occurred when validating a
1855 certificate in the context. Sets ``certificate`` attribute to
1856 indicate which certificate caused the error.
1857
1858 .. versionadded:: 20.0
1859 """
1860 # Always re-initialize the store context in case
1861 # :meth:`verify_certificate` is called multiple times.
1862 #
1863 # :meth:`_init` is called in :meth:`__init__` so _cleanup is called
1864 # before _init to ensure memory is not leaked.
1865 self._cleanup()
1866 self._init()
1867 ret = _lib.X509_verify_cert(self._store_ctx)
1868 if ret <= 0:
1869 self._cleanup()
1870 raise self._exception_from_context()
1871
1872 # Note: X509_STORE_CTX_get1_chain returns a deep copy of the chain.
1873 cert_stack = _lib.X509_STORE_CTX_get1_chain(self._store_ctx)
1874 _openssl_assert(cert_stack != _ffi.NULL)
1875
1876 result = []
1877 for i in range(_lib.sk_X509_num(cert_stack)):
1878 cert = _lib.sk_X509_value(cert_stack, i)
1879 _openssl_assert(cert != _ffi.NULL)
1880 pycert = X509._from_raw_x509_ptr(cert)
1881 result.append(pycert)
1882
1883 # Free the stack but not the members which are freed by the X509 class.
1884 _lib.sk_X509_free(cert_stack)
1885 self._cleanup()
1886 return result
1887
Stephen Holsapple08ffaa62015-01-30 17:18:40 -08001888
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001889def load_certificate(type, buffer):
1890 """
Alex Chand072cae2018-02-15 09:57:59 +00001891 Load a certificate (X509) from the string *buffer* encoded with the
1892 type *type*.
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001893
1894 :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1)
1895
Dan Sully44e767a2016-06-04 18:05:27 -07001896 :param bytes buffer: The buffer the certificate is stored in
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001897
1898 :return: The X509 object
1899 """
Jean-Paul Calderone6922a862014-01-18 10:38:28 -05001900 if isinstance(buffer, _text_type):
1901 buffer = buffer.encode("ascii")
1902
Jean-Paul Calderonef6745b32013-03-01 15:08:46 -08001903 bio = _new_mem_buf(buffer)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001904
Jean-Paul Calderonef6745b32013-03-01 15:08:46 -08001905 if type == FILETYPE_PEM:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001906 x509 = _lib.PEM_read_bio_X509(bio, _ffi.NULL, _ffi.NULL, _ffi.NULL)
Jean-Paul Calderonef6745b32013-03-01 15:08:46 -08001907 elif type == FILETYPE_ASN1:
Alex Gaynor962ac212015-09-04 08:06:42 -04001908 x509 = _lib.d2i_X509_bio(bio, _ffi.NULL)
Jean-Paul Calderonef6745b32013-03-01 15:08:46 -08001909 else:
Alex Gaynor03737182020-07-23 20:40:46 -04001910 raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1")
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001911
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001912 if x509 == _ffi.NULL:
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001913 _raise_current_error()
1914
Alex Gaynor4aa52c32017-11-20 09:04:08 -05001915 return X509._from_raw_x509_ptr(x509)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001916
1917
1918def dump_certificate(type, cert):
1919 """
Alex Chand072cae2018-02-15 09:57:59 +00001920 Dump the certificate *cert* into a buffer string encoded with the type
1921 *type*.
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001922
Jean-Paul Calderonea12e7d22013-04-03 08:17:34 -04001923 :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1, or
1924 FILETYPE_TEXT)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001925 :param cert: The certificate to dump
1926 :return: The buffer with the dumped certificate in
1927 """
Jean-Paul Calderone0c73aff2013-03-02 07:45:12 -08001928 bio = _new_mem_buf()
Jean-Paul Calderone066f0572013-02-20 13:43:44 -08001929
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001930 if type == FILETYPE_PEM:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001931 result_code = _lib.PEM_write_bio_X509(bio, cert._x509)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001932 elif type == FILETYPE_ASN1:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001933 result_code = _lib.i2d_X509_bio(bio, cert._x509)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001934 elif type == FILETYPE_TEXT:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001935 result_code = _lib.X509_print_ex(bio, cert._x509, 0, 0)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001936 else:
1937 raise ValueError(
1938 "type argument must be FILETYPE_PEM, FILETYPE_ASN1, or "
Alex Gaynor03737182020-07-23 20:40:46 -04001939 "FILETYPE_TEXT"
1940 )
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001941
Adrián Chaves98c57be2020-03-31 16:14:50 +02001942 _openssl_assert(result_code == 1)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001943 return _bio_to_string(bio)
1944
1945
Cory Benfield6492f7c2015-10-27 16:57:58 +09001946def dump_publickey(type, pkey):
1947 """
Cory Benfield11c10192015-10-27 17:23:03 +09001948 Dump a public key to a buffer.
Cory Benfield6492f7c2015-10-27 16:57:58 +09001949
Cory Benfield9c590b92015-10-28 14:55:05 +09001950 :param type: The file type (one of :data:`FILETYPE_PEM` or
Cory Benfielde813cec2015-10-28 08:57:08 +09001951 :data:`FILETYPE_ASN1`).
Cory Benfield2b6bb802015-10-28 22:19:31 +09001952 :param PKey pkey: The public key to dump
Cory Benfield6492f7c2015-10-27 16:57:58 +09001953 :return: The buffer with the dumped key in it.
Cory Benfield11c10192015-10-27 17:23:03 +09001954 :rtype: bytes
Cory Benfield6492f7c2015-10-27 16:57:58 +09001955 """
1956 bio = _new_mem_buf()
1957 if type == FILETYPE_PEM:
1958 write_bio = _lib.PEM_write_bio_PUBKEY
1959 elif type == FILETYPE_ASN1:
1960 write_bio = _lib.i2d_PUBKEY_bio
1961 else:
1962 raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1")
1963
1964 result_code = write_bio(bio, pkey._pkey)
Cory Benfield1e9c7ab2015-10-28 08:58:31 +09001965 if result_code != 1: # pragma: no cover
Cory Benfield6492f7c2015-10-27 16:57:58 +09001966 _raise_current_error()
1967
1968 return _bio_to_string(bio)
1969
1970
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001971def dump_privatekey(type, pkey, cipher=None, passphrase=None):
1972 """
Hynek Schlawack11e43ad2016-07-03 14:40:20 +02001973 Dump the private key *pkey* into a buffer string encoded with the type
1974 *type*. Optionally (if *type* is :const:`FILETYPE_PEM`) encrypting it
1975 using *cipher* and *passphrase*.
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001976
Hynek Schlawack11e43ad2016-07-03 14:40:20 +02001977 :param type: The file type (one of :const:`FILETYPE_PEM`,
1978 :const:`FILETYPE_ASN1`, or :const:`FILETYPE_TEXT`)
1979 :param PKey pkey: The PKey to dump
1980 :param cipher: (optional) if encrypted PEM format, the cipher to use
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001981 :param passphrase: (optional) if encrypted PEM format, this can be either
Hynek Schlawack11e43ad2016-07-03 14:40:20 +02001982 the passphrase to use, or a callback for providing the passphrase.
1983
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001984 :return: The buffer with the dumped key in
Dan Sully44e767a2016-06-04 18:05:27 -07001985 :rtype: bytes
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08001986 """
Jean-Paul Calderoneef9a3dc2013-03-02 16:33:32 -08001987 bio = _new_mem_buf()
Jean-Paul Calderone066f0572013-02-20 13:43:44 -08001988
Paul Kehrercded9932017-06-29 18:43:42 -05001989 if not isinstance(pkey, PKey):
1990 raise TypeError("pkey must be a PKey")
1991
Jean-Paul Calderone066f0572013-02-20 13:43:44 -08001992 if cipher is not None:
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -05001993 if passphrase is None:
1994 raise TypeError(
1995 "if a value is given for cipher "
Alex Gaynor03737182020-07-23 20:40:46 -04001996 "one must also be given for passphrase"
1997 )
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -05001998 cipher_obj = _lib.EVP_get_cipherbyname(_byte_string(cipher))
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05001999 if cipher_obj == _ffi.NULL:
Jean-Paul Calderone066f0572013-02-20 13:43:44 -08002000 raise ValueError("Invalid cipher name")
2001 else:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002002 cipher_obj = _ffi.NULL
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002003
Jean-Paul Calderone23478b32013-02-20 13:31:38 -08002004 helper = _PassphraseHelper(type, passphrase)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002005 if type == FILETYPE_PEM:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002006 result_code = _lib.PEM_write_bio_PrivateKey(
Alex Gaynor03737182020-07-23 20:40:46 -04002007 bio,
2008 pkey._pkey,
2009 cipher_obj,
2010 _ffi.NULL,
2011 0,
2012 helper.callback,
2013 helper.callback_args,
2014 )
Jean-Paul Calderone23478b32013-02-20 13:31:38 -08002015 helper.raise_if_problem()
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002016 elif type == FILETYPE_ASN1:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002017 result_code = _lib.i2d_PrivateKey_bio(bio, pkey._pkey)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002018 elif type == FILETYPE_TEXT:
Paul Kehrercded9932017-06-29 18:43:42 -05002019 if _lib.EVP_PKEY_id(pkey._pkey) != _lib.EVP_PKEY_RSA:
2020 raise TypeError("Only RSA keys are supported for FILETYPE_TEXT")
2021
Alex Gaynor03737182020-07-23 20:40:46 -04002022 rsa = _ffi.gc(_lib.EVP_PKEY_get1_RSA(pkey._pkey), _lib.RSA_free)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002023 result_code = _lib.RSA_print(bio, rsa, 0)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002024 else:
2025 raise ValueError(
2026 "type argument must be FILETYPE_PEM, FILETYPE_ASN1, or "
Alex Gaynor03737182020-07-23 20:40:46 -04002027 "FILETYPE_TEXT"
2028 )
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002029
Hynek Schlawack11e43ad2016-07-03 14:40:20 +02002030 _openssl_assert(result_code != 0)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002031
2032 return _bio_to_string(bio)
2033
2034
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002035class Revoked(object):
Laurens Van Houtvend92f55c2014-06-19 17:08:41 +02002036 """
2037 A certificate revocation.
2038 """
Alex Gaynor03737182020-07-23 20:40:46 -04002039
Cyril Stoller37e60222018-08-27 13:38:10 +02002040 # https://www.openssl.org/docs/manmaster/man5/x509v3_config.html#CRL-distribution-points
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002041 # which differs from crl_reasons of crypto/x509v3/v3_enum.c that matches
2042 # OCSP_crl_reason_str. We use the latter, just like the command line
2043 # program.
2044 _crl_reasons = [
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -05002045 b"unspecified",
2046 b"keyCompromise",
2047 b"CACompromise",
2048 b"affiliationChanged",
2049 b"superseded",
2050 b"cessationOfOperation",
2051 b"certificateHold",
2052 # b"removeFromCRL",
Alex Gaynorca87ff62015-09-04 23:31:03 -04002053 ]
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002054
2055 def __init__(self):
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002056 revoked = _lib.X509_REVOKED_new()
2057 self._revoked = _ffi.gc(revoked, _lib.X509_REVOKED_free)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002058
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002059 def set_serial(self, hex_str):
2060 """
Laurens Van Houtvend92f55c2014-06-19 17:08:41 +02002061 Set the serial number.
2062
2063 The serial number is formatted as a hexadecimal number encoded in
2064 ASCII.
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002065
Dan Sully44e767a2016-06-04 18:05:27 -07002066 :param bytes hex_str: The new serial number.
Laurens Van Houtvend92f55c2014-06-19 17:08:41 +02002067
Dan Sully44e767a2016-06-04 18:05:27 -07002068 :return: ``None``
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002069 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002070 bignum_serial = _ffi.gc(_lib.BN_new(), _lib.BN_free)
2071 bignum_ptr = _ffi.new("BIGNUM**")
Jean-Paul Calderonefd371362013-03-01 20:53:58 -08002072 bignum_ptr[0] = bignum_serial
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002073 bn_result = _lib.BN_hex2bn(bignum_ptr, hex_str)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002074 if not bn_result:
2075 raise ValueError("bad hex string")
2076
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002077 asn1_serial = _ffi.gc(
2078 _lib.BN_to_ASN1_INTEGER(bignum_serial, _ffi.NULL),
Alex Gaynor03737182020-07-23 20:40:46 -04002079 _lib.ASN1_INTEGER_free,
2080 )
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002081 _lib.X509_REVOKED_set_serialNumber(self._revoked, asn1_serial)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002082
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002083 def get_serial(self):
2084 """
Laurens Van Houtvend92f55c2014-06-19 17:08:41 +02002085 Get the serial number.
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002086
Laurens Van Houtvend92f55c2014-06-19 17:08:41 +02002087 The serial number is formatted as a hexadecimal number encoded in
2088 ASCII.
2089
2090 :return: The serial number.
Dan Sully44e767a2016-06-04 18:05:27 -07002091 :rtype: bytes
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002092 """
Jean-Paul Calderonefd371362013-03-01 20:53:58 -08002093 bio = _new_mem_buf()
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002094
Alex Gaynor67903a62016-06-02 10:37:13 -07002095 asn1_int = _lib.X509_REVOKED_get0_serialNumber(self._revoked)
2096 _openssl_assert(asn1_int != _ffi.NULL)
2097 result = _lib.i2a_ASN1_INTEGER(bio, asn1_int)
2098 _openssl_assert(result >= 0)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002099 return _bio_to_string(bio)
2100
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002101 def _delete_reason(self):
Alex Gaynor67903a62016-06-02 10:37:13 -07002102 for i in range(_lib.X509_REVOKED_get_ext_count(self._revoked)):
2103 ext = _lib.X509_REVOKED_get_ext(self._revoked, i)
Paul Kehrere8f91cc2016-03-09 21:26:29 -04002104 obj = _lib.X509_EXTENSION_get_object(ext)
2105 if _lib.OBJ_obj2nid(obj) == _lib.NID_crl_reason:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002106 _lib.X509_EXTENSION_free(ext)
Alex Gaynor67903a62016-06-02 10:37:13 -07002107 _lib.X509_REVOKED_delete_ext(self._revoked, i)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002108 break
2109
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002110 def set_reason(self, reason):
2111 """
Laurens Van Houtvend92f55c2014-06-19 17:08:41 +02002112 Set the reason of this revocation.
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002113
Dan Sully44e767a2016-06-04 18:05:27 -07002114 If :data:`reason` is ``None``, delete the reason instead.
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002115
2116 :param reason: The reason string.
Dan Sully44e767a2016-06-04 18:05:27 -07002117 :type reason: :class:`bytes` or :class:`NoneType`
Laurens Van Houtvend92f55c2014-06-19 17:08:41 +02002118
Dan Sully44e767a2016-06-04 18:05:27 -07002119 :return: ``None``
Laurens Van Houtvend92f55c2014-06-19 17:08:41 +02002120
2121 .. seealso::
2122
Dan Sully44e767a2016-06-04 18:05:27 -07002123 :meth:`all_reasons`, which gives you a list of all supported
Laurens Van Houtvend92f55c2014-06-19 17:08:41 +02002124 reasons which you might pass to this method.
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002125 """
2126 if reason is None:
2127 self._delete_reason()
2128 elif not isinstance(reason, bytes):
2129 raise TypeError("reason must be None or a byte string")
2130 else:
Alex Gaynor03737182020-07-23 20:40:46 -04002131 reason = reason.lower().replace(b" ", b"")
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002132 reason_code = [r.lower() for r in self._crl_reasons].index(reason)
2133
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002134 new_reason_ext = _lib.ASN1_ENUMERATED_new()
Alex Gaynoradd5b072016-06-04 21:04:00 -07002135 _openssl_assert(new_reason_ext != _ffi.NULL)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002136 new_reason_ext = _ffi.gc(new_reason_ext, _lib.ASN1_ENUMERATED_free)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002137
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002138 set_result = _lib.ASN1_ENUMERATED_set(new_reason_ext, reason_code)
Alex Gaynoradd5b072016-06-04 21:04:00 -07002139 _openssl_assert(set_result != _ffi.NULL)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002140
2141 self._delete_reason()
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002142 add_result = _lib.X509_REVOKED_add1_ext_i2d(
Alex Gaynor03737182020-07-23 20:40:46 -04002143 self._revoked, _lib.NID_crl_reason, new_reason_ext, 0, 0
2144 )
Alex Gaynor09a386e2016-07-03 09:32:44 -04002145 _openssl_assert(add_result == 1)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002146
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002147 def get_reason(self):
2148 """
Alex Gaynor80262fb2016-04-22 07:53:42 -04002149 Get the reason of this revocation.
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002150
Dan Sully44e767a2016-06-04 18:05:27 -07002151 :return: The reason, or ``None`` if there is none.
2152 :rtype: bytes or NoneType
Laurens Van Houtvend92f55c2014-06-19 17:08:41 +02002153
2154 .. seealso::
2155
Dan Sully44e767a2016-06-04 18:05:27 -07002156 :meth:`all_reasons`, which gives you a list of all supported
Laurens Van Houtvend92f55c2014-06-19 17:08:41 +02002157 reasons this method might return.
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002158 """
Alex Gaynor67903a62016-06-02 10:37:13 -07002159 for i in range(_lib.X509_REVOKED_get_ext_count(self._revoked)):
2160 ext = _lib.X509_REVOKED_get_ext(self._revoked, i)
Paul Kehrere8f91cc2016-03-09 21:26:29 -04002161 obj = _lib.X509_EXTENSION_get_object(ext)
2162 if _lib.OBJ_obj2nid(obj) == _lib.NID_crl_reason:
Jean-Paul Calderonefd371362013-03-01 20:53:58 -08002163 bio = _new_mem_buf()
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002164
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002165 print_result = _lib.X509V3_EXT_print(bio, ext, 0, 0)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002166 if not print_result:
Alex Gaynor5945ea82015-09-05 14:59:06 -04002167 print_result = _lib.M_ASN1_OCTET_STRING_print(
Paul Kehrere8f91cc2016-03-09 21:26:29 -04002168 bio, _lib.X509_EXTENSION_get_data(ext)
Alex Gaynor5945ea82015-09-05 14:59:06 -04002169 )
Alex Gaynor09a386e2016-07-03 09:32:44 -04002170 _openssl_assert(print_result != 0)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002171
2172 return _bio_to_string(bio)
2173
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002174 def all_reasons(self):
2175 """
2176 Return a list of all the supported reason strings.
2177
Laurens Van Houtvend92f55c2014-06-19 17:08:41 +02002178 This list is a copy; modifying it does not change the supported reason
2179 strings.
2180
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002181 :return: A list of reason strings.
Dan Sully44e767a2016-06-04 18:05:27 -07002182 :rtype: :class:`list` of :class:`bytes`
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002183 """
2184 return self._crl_reasons[:]
2185
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002186 def set_rev_date(self, when):
2187 """
Laurens Van Houtvend92f55c2014-06-19 17:08:41 +02002188 Set the revocation timestamp.
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002189
Dan Sully44e767a2016-06-04 18:05:27 -07002190 :param bytes when: The timestamp of the revocation,
Paul Kehrerce98ee62017-06-21 06:59:58 -10002191 as ASN.1 TIME.
Dan Sully44e767a2016-06-04 18:05:27 -07002192 :return: ``None``
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002193 """
Alex Gaynor67903a62016-06-02 10:37:13 -07002194 dt = _lib.X509_REVOKED_get0_revocationDate(self._revoked)
2195 return _set_asn1_time(dt, when)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002196
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002197 def get_rev_date(self):
2198 """
Laurens Van Houtvend92f55c2014-06-19 17:08:41 +02002199 Get the revocation timestamp.
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002200
Paul Kehrerce98ee62017-06-21 06:59:58 -10002201 :return: The timestamp of the revocation, as ASN.1 TIME.
Dan Sully44e767a2016-06-04 18:05:27 -07002202 :rtype: bytes
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002203 """
Alex Gaynor67903a62016-06-02 10:37:13 -07002204 dt = _lib.X509_REVOKED_get0_revocationDate(self._revoked)
2205 return _get_asn1_time(dt)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002206
2207
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002208class CRL(object):
Laurens Van Houtvencb32e852014-06-19 17:36:28 +02002209 """
2210 A certificate revocation list.
2211 """
Alex Gaynora738ed52015-09-05 11:17:10 -04002212
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002213 def __init__(self):
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002214 crl = _lib.X509_CRL_new()
2215 self._crl = _ffi.gc(crl, _lib.X509_CRL_free)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002216
Paul Kehrer41c10242017-06-29 18:24:17 -05002217 def to_cryptography(self):
2218 """
2219 Export as a ``cryptography`` CRL.
2220
2221 :rtype: ``cryptography.x509.CertificateRevocationList``
2222
2223 .. versionadded:: 17.1.0
2224 """
2225 from cryptography.hazmat.backends.openssl.x509 import (
Alex Gaynor03737182020-07-23 20:40:46 -04002226 _CertificateRevocationList,
Paul Kehrer41c10242017-06-29 18:24:17 -05002227 )
Alex Gaynor03737182020-07-23 20:40:46 -04002228
Paul Kehrer41c10242017-06-29 18:24:17 -05002229 backend = _get_backend()
2230 return _CertificateRevocationList(backend, self._crl)
2231
2232 @classmethod
2233 def from_cryptography(cls, crypto_crl):
2234 """
2235 Construct based on a ``cryptography`` *crypto_crl*.
2236
2237 :param crypto_crl: A ``cryptography`` certificate revocation list
2238 :type crypto_crl: ``cryptography.x509.CertificateRevocationList``
2239
2240 :rtype: CRL
2241
2242 .. versionadded:: 17.1.0
2243 """
2244 if not isinstance(crypto_crl, x509.CertificateRevocationList):
2245 raise TypeError("Must be a certificate revocation list")
2246
2247 crl = cls()
2248 crl._crl = crypto_crl._x509_crl
2249 return crl
2250
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002251 def get_revoked(self):
2252 """
Laurens Van Houtvencb32e852014-06-19 17:36:28 +02002253 Return the revocations in this certificate revocation list.
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002254
Laurens Van Houtvencb32e852014-06-19 17:36:28 +02002255 These revocations will be provided by value, not by reference.
2256 That means it's okay to mutate them: it won't affect this CRL.
2257
2258 :return: The revocations in this CRL.
Dan Sully44e767a2016-06-04 18:05:27 -07002259 :rtype: :class:`tuple` of :class:`Revocation`
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002260 """
2261 results = []
Alex Gaynor67903a62016-06-02 10:37:13 -07002262 revoked_stack = _lib.X509_CRL_get_REVOKED(self._crl)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002263 for i in range(_lib.sk_X509_REVOKED_num(revoked_stack)):
2264 revoked = _lib.sk_X509_REVOKED_value(revoked_stack, i)
Paul Kehrer2fe23b02016-03-09 22:02:15 -04002265 revoked_copy = _lib.Cryptography_X509_REVOKED_dup(revoked)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002266 pyrev = Revoked.__new__(Revoked)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002267 pyrev._revoked = _ffi.gc(revoked_copy, _lib.X509_REVOKED_free)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002268 results.append(pyrev)
Jean-Paul Calderone85b74eb2013-02-21 09:15:01 -08002269 if results:
2270 return tuple(results)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002271
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002272 def add_revoked(self, revoked):
2273 """
2274 Add a revoked (by value not reference) to the CRL structure
2275
Laurens Van Houtvencb32e852014-06-19 17:36:28 +02002276 This revocation will be added by value, not by reference. That
2277 means it's okay to mutate it after adding: it won't affect
2278 this CRL.
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002279
Dan Sully44e767a2016-06-04 18:05:27 -07002280 :param Revoked revoked: The new revocation.
2281 :return: ``None``
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002282 """
Paul Kehrer8dddb1a2016-03-09 21:48:04 -04002283 copy = _lib.Cryptography_X509_REVOKED_dup(revoked._revoked)
Alex Gaynoradd5b072016-06-04 21:04:00 -07002284 _openssl_assert(copy != _ffi.NULL)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002285
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002286 add_result = _lib.X509_CRL_add0_revoked(self._crl, copy)
Alex Gaynor09a386e2016-07-03 09:32:44 -04002287 _openssl_assert(add_result != 0)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002288
Dan Sully44e767a2016-06-04 18:05:27 -07002289 def get_issuer(self):
2290 """
2291 Get the CRL's issuer.
2292
2293 .. versionadded:: 16.1.0
2294
2295 :rtype: X509Name
2296 """
2297 _issuer = _lib.X509_NAME_dup(_lib.X509_CRL_get_issuer(self._crl))
2298 _openssl_assert(_issuer != _ffi.NULL)
2299 _issuer = _ffi.gc(_issuer, _lib.X509_NAME_free)
2300 issuer = X509Name.__new__(X509Name)
2301 issuer._name = _issuer
2302 return issuer
2303
2304 def set_version(self, version):
2305 """
2306 Set the CRL version.
2307
2308 .. versionadded:: 16.1.0
2309
2310 :param int version: The version of the CRL.
2311 :return: ``None``
2312 """
2313 _openssl_assert(_lib.X509_CRL_set_version(self._crl, version) != 0)
2314
2315 def _set_boundary_time(self, which, when):
2316 return _set_asn1_time(which(self._crl), when)
2317
2318 def set_lastUpdate(self, when):
2319 """
2320 Set when the CRL was last updated.
2321
Paul Kehrerce98ee62017-06-21 06:59:58 -10002322 The timestamp is formatted as an ASN.1 TIME::
Dan Sully44e767a2016-06-04 18:05:27 -07002323
2324 YYYYMMDDhhmmssZ
Dan Sully44e767a2016-06-04 18:05:27 -07002325
2326 .. versionadded:: 16.1.0
2327
2328 :param bytes when: A timestamp string.
2329 :return: ``None``
2330 """
2331 return self._set_boundary_time(_lib.X509_CRL_get_lastUpdate, when)
2332
2333 def set_nextUpdate(self, when):
2334 """
2335 Set when the CRL will next be udpated.
2336
Paul Kehrerce98ee62017-06-21 06:59:58 -10002337 The timestamp is formatted as an ASN.1 TIME::
Dan Sully44e767a2016-06-04 18:05:27 -07002338
2339 YYYYMMDDhhmmssZ
Dan Sully44e767a2016-06-04 18:05:27 -07002340
2341 .. versionadded:: 16.1.0
2342
2343 :param bytes when: A timestamp string.
2344 :return: ``None``
2345 """
2346 return self._set_boundary_time(_lib.X509_CRL_get_nextUpdate, when)
2347
2348 def sign(self, issuer_cert, issuer_key, digest):
2349 """
2350 Sign the CRL.
2351
2352 Signing a CRL enables clients to associate the CRL itself with an
2353 issuer. Before a CRL is meaningful to other OpenSSL functions, it must
2354 be signed by an issuer.
2355
2356 This method implicitly sets the issuer's name based on the issuer
2357 certificate and private key used to sign the CRL.
2358
2359 .. versionadded:: 16.1.0
2360
2361 :param X509 issuer_cert: The issuer's certificate.
2362 :param PKey issuer_key: The issuer's private key.
2363 :param bytes digest: The digest method to sign the CRL with.
2364 """
2365 digest_obj = _lib.EVP_get_digestbyname(digest)
2366 _openssl_assert(digest_obj != _ffi.NULL)
2367 _lib.X509_CRL_set_issuer_name(
Alex Gaynor03737182020-07-23 20:40:46 -04002368 self._crl, _lib.X509_get_subject_name(issuer_cert._x509)
2369 )
Dan Sully44e767a2016-06-04 18:05:27 -07002370 _lib.X509_CRL_sort(self._crl)
2371 result = _lib.X509_CRL_sign(self._crl, issuer_key._pkey, digest_obj)
2372 _openssl_assert(result != 0)
2373
Alex Gaynor03737182020-07-23 20:40:46 -04002374 def export(
2375 self, cert, key, type=FILETYPE_PEM, days=100, digest=_UNSPECIFIED
2376 ):
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002377 """
Dan Sully44e767a2016-06-04 18:05:27 -07002378 Export the CRL as a string.
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002379
Dan Sully44e767a2016-06-04 18:05:27 -07002380 :param X509 cert: The certificate used to sign the CRL.
2381 :param PKey key: The key used to sign the CRL.
2382 :param int type: The export format, either :data:`FILETYPE_PEM`,
2383 :data:`FILETYPE_ASN1`, or :data:`FILETYPE_TEXT`.
Jean-Paul Calderonedf514012015-04-13 21:45:18 -04002384 :param int days: The number of days until the next update of this CRL.
Jean-Paul Calderonecce22d02015-04-13 13:56:09 -04002385 :param bytes digest: The name of the message digest to use (eg
Wayne Werner80dcf382019-01-30 15:03:16 -06002386 ``b"sha256"``).
Dan Sully44e767a2016-06-04 18:05:27 -07002387 :rtype: bytes
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002388 """
Dan Sully44e767a2016-06-04 18:05:27 -07002389
Jean-Paul Calderone85b74eb2013-02-21 09:15:01 -08002390 if not isinstance(cert, X509):
2391 raise TypeError("cert must be an X509 instance")
2392 if not isinstance(key, PKey):
2393 raise TypeError("key must be a PKey instance")
2394 if not isinstance(type, int):
2395 raise TypeError("type must be an integer")
Jean-Paul Calderone57122982013-02-21 08:47:05 -08002396
Jean-Paul Calderone00f84eb2015-04-13 12:47:21 -04002397 if digest is _UNSPECIFIED:
Alex Gaynor173e4ba2017-06-30 08:01:12 -07002398 raise TypeError("digest must be provided")
Jean-Paul Calderone60432792015-04-13 12:26:07 -04002399
Jean-Paul Calderonecce22d02015-04-13 13:56:09 -04002400 digest_obj = _lib.EVP_get_digestbyname(digest)
Bulat Gaifullin2923dc02014-09-21 22:36:48 +04002401 if digest_obj == _ffi.NULL:
2402 raise ValueError("No such digest method")
2403
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002404 bio = _lib.BIO_new(_lib.BIO_s_mem())
Alex Gaynoradd5b072016-06-04 21:04:00 -07002405 _openssl_assert(bio != _ffi.NULL)
Jean-Paul Calderone85b74eb2013-02-21 09:15:01 -08002406
Alex Gaynora738ed52015-09-05 11:17:10 -04002407 # A scratch time object to give different values to different CRL
2408 # fields
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002409 sometime = _lib.ASN1_TIME_new()
Alex Gaynoradd5b072016-06-04 21:04:00 -07002410 _openssl_assert(sometime != _ffi.NULL)
Jean-Paul Calderone85b74eb2013-02-21 09:15:01 -08002411
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002412 _lib.X509_gmtime_adj(sometime, 0)
2413 _lib.X509_CRL_set_lastUpdate(self._crl, sometime)
Jean-Paul Calderone85b74eb2013-02-21 09:15:01 -08002414
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002415 _lib.X509_gmtime_adj(sometime, days * 24 * 60 * 60)
2416 _lib.X509_CRL_set_nextUpdate(self._crl, sometime)
Jean-Paul Calderone85b74eb2013-02-21 09:15:01 -08002417
Alex Gaynor5945ea82015-09-05 14:59:06 -04002418 _lib.X509_CRL_set_issuer_name(
2419 self._crl, _lib.X509_get_subject_name(cert._x509)
2420 )
Jean-Paul Calderone85b74eb2013-02-21 09:15:01 -08002421
Bulat Gaifullin2923dc02014-09-21 22:36:48 +04002422 sign_result = _lib.X509_CRL_sign(self._crl, key._pkey, digest_obj)
Jean-Paul Calderone85b74eb2013-02-21 09:15:01 -08002423 if not sign_result:
2424 _raise_current_error()
2425
Dominic Chenf05b2122015-10-13 16:32:35 +00002426 return dump_crl(type, self)
Jean-Paul Calderone85b74eb2013-02-21 09:15:01 -08002427
Jean-Paul Calderone85b74eb2013-02-21 09:15:01 -08002428
Jean-Paul Calderone4e8be1c2013-02-21 18:31:12 -08002429class PKCS7(object):
2430 def type_is_signed(self):
2431 """
2432 Check if this NID_pkcs7_signed object
2433
2434 :return: True if the PKCS7 is of type signed
2435 """
Alex Gaynor3aeead92016-07-31 11:31:59 -04002436 return bool(_lib.PKCS7_type_is_signed(self._pkcs7))
Jean-Paul Calderone4e8be1c2013-02-21 18:31:12 -08002437
Jean-Paul Calderone4e8be1c2013-02-21 18:31:12 -08002438 def type_is_enveloped(self):
2439 """
2440 Check if this NID_pkcs7_enveloped object
2441
2442 :returns: True if the PKCS7 is of type enveloped
2443 """
Alex Gaynor3aeead92016-07-31 11:31:59 -04002444 return bool(_lib.PKCS7_type_is_enveloped(self._pkcs7))
Jean-Paul Calderone4e8be1c2013-02-21 18:31:12 -08002445
Jean-Paul Calderone4e8be1c2013-02-21 18:31:12 -08002446 def type_is_signedAndEnveloped(self):
2447 """
2448 Check if this NID_pkcs7_signedAndEnveloped object
2449
2450 :returns: True if the PKCS7 is of type signedAndEnveloped
2451 """
Alex Gaynor3aeead92016-07-31 11:31:59 -04002452 return bool(_lib.PKCS7_type_is_signedAndEnveloped(self._pkcs7))
Jean-Paul Calderone4e8be1c2013-02-21 18:31:12 -08002453
Jean-Paul Calderone4e8be1c2013-02-21 18:31:12 -08002454 def type_is_data(self):
2455 """
2456 Check if this NID_pkcs7_data object
2457
2458 :return: True if the PKCS7 is of type data
2459 """
Alex Gaynor3aeead92016-07-31 11:31:59 -04002460 return bool(_lib.PKCS7_type_is_data(self._pkcs7))
Jean-Paul Calderone4e8be1c2013-02-21 18:31:12 -08002461
Jean-Paul Calderone4e8be1c2013-02-21 18:31:12 -08002462 def get_type_name(self):
2463 """
2464 Returns the type name of the PKCS7 structure
2465
2466 :return: A string with the typename
2467 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002468 nid = _lib.OBJ_obj2nid(self._pkcs7.type)
2469 string_type = _lib.OBJ_nid2sn(nid)
2470 return _ffi.string(string_type)
Jean-Paul Calderone4e8be1c2013-02-21 18:31:12 -08002471
Alex Chanc6077062016-11-18 13:53:39 +00002472
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002473class PKCS12(object):
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002474 """
2475 A PKCS #12 archive.
2476 """
Alex Gaynora738ed52015-09-05 11:17:10 -04002477
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002478 def __init__(self):
2479 self._pkey = None
2480 self._cert = None
2481 self._cacerts = None
2482 self._friendlyname = None
2483
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002484 def get_certificate(self):
2485 """
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002486 Get the certificate in the PKCS #12 structure.
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002487
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002488 :return: The certificate, or :py:const:`None` if there is none.
2489 :rtype: :py:class:`X509` or :py:const:`None`
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002490 """
2491 return self._cert
2492
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002493 def set_certificate(self, cert):
2494 """
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002495 Set the certificate in the PKCS #12 structure.
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002496
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002497 :param cert: The new certificate, or :py:const:`None` to unset it.
Laurens Van Houtvena7904582014-06-19 12:33:04 +02002498 :type cert: :py:class:`X509` or :py:const:`None`
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002499
Dan Sully44e767a2016-06-04 18:05:27 -07002500 :return: ``None``
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002501 """
2502 if not isinstance(cert, X509):
2503 raise TypeError("cert must be an X509 instance")
2504 self._cert = cert
2505
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002506 def get_privatekey(self):
2507 """
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002508 Get the private key in the PKCS #12 structure.
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002509
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002510 :return: The private key, or :py:const:`None` if there is none.
2511 :rtype: :py:class:`PKey`
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002512 """
2513 return self._pkey
2514
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002515 def set_privatekey(self, pkey):
2516 """
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002517 Set the certificate portion of the PKCS #12 structure.
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002518
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002519 :param pkey: The new private key, or :py:const:`None` to unset it.
2520 :type pkey: :py:class:`PKey` or :py:const:`None`
2521
Dan Sully44e767a2016-06-04 18:05:27 -07002522 :return: ``None``
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002523 """
2524 if not isinstance(pkey, PKey):
2525 raise TypeError("pkey must be a PKey instance")
2526 self._pkey = pkey
2527
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002528 def get_ca_certificates(self):
2529 """
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002530 Get the CA certificates in the PKCS #12 structure.
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002531
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002532 :return: A tuple with the CA certificates in the chain, or
2533 :py:const:`None` if there are none.
2534 :rtype: :py:class:`tuple` of :py:class:`X509` or :py:const:`None`
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002535 """
2536 if self._cacerts is not None:
2537 return tuple(self._cacerts)
2538
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002539 def set_ca_certificates(self, cacerts):
2540 """
Alex Gaynor3b0ee972014-11-15 09:17:33 -08002541 Replace or set the CA certificates within the PKCS12 object.
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002542
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002543 :param cacerts: The new CA certificates, or :py:const:`None` to unset
2544 them.
Laurens Van Houtvena7904582014-06-19 12:33:04 +02002545 :type cacerts: An iterable of :py:class:`X509` or :py:const:`None`
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002546
Dan Sully44e767a2016-06-04 18:05:27 -07002547 :return: ``None``
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002548 """
2549 if cacerts is None:
2550 self._cacerts = None
2551 else:
2552 cacerts = list(cacerts)
2553 for cert in cacerts:
2554 if not isinstance(cert, X509):
Alex Gaynor5945ea82015-09-05 14:59:06 -04002555 raise TypeError(
2556 "iterable must only contain X509 instances"
2557 )
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002558 self._cacerts = cacerts
2559
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002560 def set_friendlyname(self, name):
2561 """
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002562 Set the friendly name in the PKCS #12 structure.
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002563
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002564 :param name: The new friendly name, or :py:const:`None` to unset.
Laurens Van Houtvena7904582014-06-19 12:33:04 +02002565 :type name: :py:class:`bytes` or :py:const:`None`
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002566
Dan Sully44e767a2016-06-04 18:05:27 -07002567 :return: ``None``
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002568 """
2569 if name is None:
2570 self._friendlyname = None
2571 elif not isinstance(name, bytes):
Alex Gaynor5945ea82015-09-05 14:59:06 -04002572 raise TypeError(
2573 "name must be a byte string or None (not %r)" % (name,)
2574 )
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002575 self._friendlyname = name
2576
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002577 def get_friendlyname(self):
2578 """
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002579 Get the friendly name in the PKCS# 12 structure.
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002580
Laurens Van Houtvena7904582014-06-19 12:33:04 +02002581 :returns: The friendly name, or :py:const:`None` if there is none.
2582 :rtype: :py:class:`bytes` or :py:const:`None`
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002583 """
2584 return self._friendlyname
2585
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002586 def export(self, passphrase=None, iter=2048, maciter=1):
2587 """
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002588 Dump a PKCS12 object as a string.
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002589
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002590 For more information, see the :c:func:`PKCS12_create` man page.
2591
2592 :param passphrase: The passphrase used to encrypt the structure. Unlike
2593 some other passphrase arguments, this *must* be a string, not a
2594 callback.
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002595 :type passphrase: :py:data:`bytes`
2596
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002597 :param iter: Number of times to repeat the encryption step.
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002598 :type iter: :py:data:`int`
2599
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002600 :param maciter: Number of times to repeat the MAC step.
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002601 :type maciter: :py:data:`int`
2602
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002603 :return: The string representation of the PKCS #12 structure.
2604 :rtype:
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002605 """
Jean-Paul Calderone39a8d592015-04-13 20:49:50 -04002606 passphrase = _text_to_bytes_and_warn("passphrase", passphrase)
Abraham Martine82326c2015-02-04 10:18:10 +00002607
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002608 if self._cacerts is None:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002609 cacerts = _ffi.NULL
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002610 else:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002611 cacerts = _lib.sk_X509_new_null()
2612 cacerts = _ffi.gc(cacerts, _lib.sk_X509_free)
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002613 for cert in self._cacerts:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002614 _lib.sk_X509_push(cacerts, cert._x509)
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002615
2616 if passphrase is None:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002617 passphrase = _ffi.NULL
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002618
2619 friendlyname = self._friendlyname
2620 if friendlyname is None:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002621 friendlyname = _ffi.NULL
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002622
2623 if self._pkey is None:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002624 pkey = _ffi.NULL
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002625 else:
2626 pkey = self._pkey._pkey
2627
2628 if self._cert is None:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002629 cert = _ffi.NULL
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002630 else:
2631 cert = self._cert._x509
2632
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002633 pkcs12 = _lib.PKCS12_create(
Alex Gaynor03737182020-07-23 20:40:46 -04002634 passphrase,
2635 friendlyname,
2636 pkey,
2637 cert,
2638 cacerts,
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002639 _lib.NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
2640 _lib.NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
Alex Gaynor03737182020-07-23 20:40:46 -04002641 iter,
2642 maciter,
2643 0,
2644 )
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002645 if pkcs12 == _ffi.NULL:
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002646 _raise_current_error()
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002647 pkcs12 = _ffi.gc(pkcs12, _lib.PKCS12_free)
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002648
Jean-Paul Calderoneef9a3dc2013-03-02 16:33:32 -08002649 bio = _new_mem_buf()
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002650 _lib.i2d_PKCS12_bio(bio, pkcs12)
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08002651 return _bio_to_string(bio)
Jean-Paul Calderoneef9a3dc2013-03-02 16:33:32 -08002652
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +02002653
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002654class NetscapeSPKI(object):
Laurens Van Houtven59152b52014-06-19 16:42:30 +02002655 """
2656 A Netscape SPKI object.
2657 """
Alex Gaynora738ed52015-09-05 11:17:10 -04002658
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002659 def __init__(self):
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002660 spki = _lib.NETSCAPE_SPKI_new()
2661 self._spki = _ffi.gc(spki, _lib.NETSCAPE_SPKI_free)
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002662
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002663 def sign(self, pkey, digest):
2664 """
Laurens Van Houtven59152b52014-06-19 16:42:30 +02002665 Sign the certificate request with this key and digest type.
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002666
Laurens Van Houtven59152b52014-06-19 16:42:30 +02002667 :param pkey: The private key to sign with.
2668 :type pkey: :py:class:`PKey`
2669
2670 :param digest: The message digest to use.
2671 :type digest: :py:class:`bytes`
2672
Dan Sully44e767a2016-06-04 18:05:27 -07002673 :return: ``None``
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002674 """
2675 if pkey._only_public:
2676 raise ValueError("Key has only public part")
2677
2678 if not pkey._initialized:
2679 raise ValueError("Key is uninitialized")
2680
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -05002681 digest_obj = _lib.EVP_get_digestbyname(_byte_string(digest))
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002682 if digest_obj == _ffi.NULL:
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002683 raise ValueError("No such digest method")
2684
Alex Gaynor5945ea82015-09-05 14:59:06 -04002685 sign_result = _lib.NETSCAPE_SPKI_sign(
2686 self._spki, pkey._pkey, digest_obj
2687 )
Alex Gaynor09a386e2016-07-03 09:32:44 -04002688 _openssl_assert(sign_result > 0)
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002689
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002690 def verify(self, key):
2691 """
Laurens Van Houtven59152b52014-06-19 16:42:30 +02002692 Verifies a signature on a certificate request.
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002693
Hynek Schlawack01c31672016-12-11 15:14:09 +01002694 :param PKey key: The public key that signature is supposedly from.
Laurens Van Houtven59152b52014-06-19 16:42:30 +02002695
Hynek Schlawack01c31672016-12-11 15:14:09 +01002696 :return: ``True`` if the signature is correct.
2697 :rtype: bool
Laurens Van Houtven59152b52014-06-19 16:42:30 +02002698
Hynek Schlawack01c31672016-12-11 15:14:09 +01002699 :raises OpenSSL.crypto.Error: If the signature is invalid, or there was
2700 a problem verifying the signature.
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002701 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002702 answer = _lib.NETSCAPE_SPKI_verify(self._spki, key._pkey)
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002703 if answer <= 0:
2704 _raise_current_error()
2705 return True
2706
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002707 def b64_encode(self):
2708 """
Laurens Van Houtven59152b52014-06-19 16:42:30 +02002709 Generate a base64 encoded representation of this SPKI object.
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002710
Laurens Van Houtven59152b52014-06-19 16:42:30 +02002711 :return: The base64 encoded string.
2712 :rtype: :py:class:`bytes`
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002713 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002714 encoded = _lib.NETSCAPE_SPKI_b64_encode(self._spki)
2715 result = _ffi.string(encoded)
Paul Kehrer0dcacf72016-03-17 19:25:39 -04002716 _lib.OPENSSL_free(encoded)
Jean-Paul Calderone2c2e21d2013-03-02 16:50:35 -08002717 return result
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002718
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002719 def get_pubkey(self):
2720 """
Laurens Van Houtven59152b52014-06-19 16:42:30 +02002721 Get the public key of this certificate.
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002722
Laurens Van Houtven59152b52014-06-19 16:42:30 +02002723 :return: The public key.
2724 :rtype: :py:class:`PKey`
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002725 """
2726 pkey = PKey.__new__(PKey)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002727 pkey._pkey = _lib.NETSCAPE_SPKI_get_pubkey(self._spki)
Alex Gaynoradd5b072016-06-04 21:04:00 -07002728 _openssl_assert(pkey._pkey != _ffi.NULL)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002729 pkey._pkey = _ffi.gc(pkey._pkey, _lib.EVP_PKEY_free)
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002730 pkey._only_public = True
2731 return pkey
2732
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002733 def set_pubkey(self, pkey):
2734 """
2735 Set the public key of the certificate
2736
2737 :param pkey: The public key
Dan Sully44e767a2016-06-04 18:05:27 -07002738 :return: ``None``
Jean-Paul Calderone3b89f472013-02-21 09:32:25 -08002739 """
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002740 set_result = _lib.NETSCAPE_SPKI_set_pubkey(self._spki, pkey._pkey)
Alex Gaynor09a386e2016-07-03 09:32:44 -04002741 _openssl_assert(set_result == 1)
Laurens Van Houtven59152b52014-06-19 16:42:30 +02002742
2743
Jean-Paul Calderonee41f05c2013-02-20 13:28:16 -08002744class _PassphraseHelper(object):
Jean-Paul Calderone8a1bea52013-03-05 07:57:57 -08002745 def __init__(self, type, passphrase, more_args=False, truncate=False):
Jean-Paul Calderone23478b32013-02-20 13:31:38 -08002746 if type != FILETYPE_PEM and passphrase is not None:
Alex Gaynor5945ea82015-09-05 14:59:06 -04002747 raise ValueError(
2748 "only FILETYPE_PEM key format supports encryption"
2749 )
Jean-Paul Calderonee41f05c2013-02-20 13:28:16 -08002750 self._passphrase = passphrase
Jean-Paul Calderone8a1bea52013-03-05 07:57:57 -08002751 self._more_args = more_args
2752 self._truncate = truncate
Jean-Paul Calderonee41f05c2013-02-20 13:28:16 -08002753 self._problems = []
2754
Jean-Paul Calderonee41f05c2013-02-20 13:28:16 -08002755 @property
2756 def callback(self):
2757 if self._passphrase is None:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002758 return _ffi.NULL
Jean-Paul Calderonee41f05c2013-02-20 13:28:16 -08002759 elif isinstance(self._passphrase, bytes):
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002760 return _ffi.NULL
Jean-Paul Calderonee41f05c2013-02-20 13:28:16 -08002761 elif callable(self._passphrase):
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002762 return _ffi.callback("pem_password_cb", self._read_passphrase)
Jean-Paul Calderonee41f05c2013-02-20 13:28:16 -08002763 else:
Hynek Schlawack33675f92016-11-18 14:55:06 +01002764 raise TypeError(
2765 "Last argument must be a byte string or a callable."
2766 )
Jean-Paul Calderonee41f05c2013-02-20 13:28:16 -08002767
Jean-Paul Calderonee41f05c2013-02-20 13:28:16 -08002768 @property
2769 def callback_args(self):
2770 if self._passphrase is None:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002771 return _ffi.NULL
Jean-Paul Calderonee41f05c2013-02-20 13:28:16 -08002772 elif isinstance(self._passphrase, bytes):
2773 return self._passphrase
2774 elif callable(self._passphrase):
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002775 return _ffi.NULL
Jean-Paul Calderonee41f05c2013-02-20 13:28:16 -08002776 else:
Hynek Schlawack33675f92016-11-18 14:55:06 +01002777 raise TypeError(
2778 "Last argument must be a byte string or a callable."
2779 )
Jean-Paul Calderonee41f05c2013-02-20 13:28:16 -08002780
Jean-Paul Calderone8a1bea52013-03-05 07:57:57 -08002781 def raise_if_problem(self, exceptionType=Error):
Jean-Paul Calderonee41f05c2013-02-20 13:28:16 -08002782 if self._problems:
Greg Bowser36eb2de2017-01-24 11:38:55 -05002783
2784 # Flush the OpenSSL error queue
2785 try:
2786 _exception_from_error_queue(exceptionType)
2787 except exceptionType:
2788 pass
2789
2790 raise self._problems.pop(0)
Jean-Paul Calderonee41f05c2013-02-20 13:28:16 -08002791
Jean-Paul Calderonee41f05c2013-02-20 13:28:16 -08002792 def _read_passphrase(self, buf, size, rwflag, userdata):
2793 try:
Jean-Paul Calderone8a1bea52013-03-05 07:57:57 -08002794 if self._more_args:
2795 result = self._passphrase(size, rwflag, userdata)
2796 else:
2797 result = self._passphrase(rwflag)
Jean-Paul Calderonee41f05c2013-02-20 13:28:16 -08002798 if not isinstance(result, bytes):
2799 raise ValueError("String expected")
2800 if len(result) > size:
Jean-Paul Calderone8a1bea52013-03-05 07:57:57 -08002801 if self._truncate:
2802 result = result[:size]
2803 else:
Alex Gaynor5945ea82015-09-05 14:59:06 -04002804 raise ValueError(
2805 "passphrase returned by callback is too long"
2806 )
Jean-Paul Calderonee41f05c2013-02-20 13:28:16 -08002807 for i in range(len(result)):
Alex Gaynor03737182020-07-23 20:40:46 -04002808 buf[i] = result[i : i + 1]
Jean-Paul Calderonee41f05c2013-02-20 13:28:16 -08002809 return len(result)
2810 except Exception as e:
2811 self._problems.append(e)
2812 return 0
2813
2814
Cory Benfield6492f7c2015-10-27 16:57:58 +09002815def load_publickey(type, buffer):
2816 """
Cory Benfield11c10192015-10-27 17:23:03 +09002817 Load a public key from a buffer.
Cory Benfield6492f7c2015-10-27 16:57:58 +09002818
Cory Benfield9c590b92015-10-28 14:55:05 +09002819 :param type: The file type (one of :data:`FILETYPE_PEM`,
Cory Benfielde813cec2015-10-28 08:57:08 +09002820 :data:`FILETYPE_ASN1`).
Cory Benfieldc9c30a22015-10-28 17:39:20 +09002821 :param buffer: The buffer the key is stored in.
2822 :type buffer: A Python string object, either unicode or bytestring.
2823 :return: The PKey object.
2824 :rtype: :class:`PKey`
Cory Benfield6492f7c2015-10-27 16:57:58 +09002825 """
2826 if isinstance(buffer, _text_type):
2827 buffer = buffer.encode("ascii")
2828
2829 bio = _new_mem_buf(buffer)
2830
2831 if type == FILETYPE_PEM:
2832 evp_pkey = _lib.PEM_read_bio_PUBKEY(
Alex Gaynor03737182020-07-23 20:40:46 -04002833 bio, _ffi.NULL, _ffi.NULL, _ffi.NULL
2834 )
Cory Benfield6492f7c2015-10-27 16:57:58 +09002835 elif type == FILETYPE_ASN1:
2836 evp_pkey = _lib.d2i_PUBKEY_bio(bio, _ffi.NULL)
2837 else:
2838 raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1")
2839
2840 if evp_pkey == _ffi.NULL:
2841 _raise_current_error()
2842
2843 pkey = PKey.__new__(PKey)
2844 pkey._pkey = _ffi.gc(evp_pkey, _lib.EVP_PKEY_free)
Paul Kehrer32fc4e62016-06-03 15:21:44 -07002845 pkey._only_public = True
Cory Benfield6492f7c2015-10-27 16:57:58 +09002846 return pkey
2847
2848
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002849def load_privatekey(type, buffer, passphrase=None):
2850 """
Alex Chand072cae2018-02-15 09:57:59 +00002851 Load a private key (PKey) from the string *buffer* encoded with the type
2852 *type*.
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002853
2854 :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1)
2855 :param buffer: The buffer the key is stored in
2856 :param passphrase: (optional) if encrypted PEM format, this can be
2857 either the passphrase to use, or a callback for
2858 providing the passphrase.
2859
2860 :return: The PKey object
2861 """
Jean-Paul Calderone6922a862014-01-18 10:38:28 -05002862 if isinstance(buffer, _text_type):
2863 buffer = buffer.encode("ascii")
2864
Jean-Paul Calderonef6745b32013-03-01 15:08:46 -08002865 bio = _new_mem_buf(buffer)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002866
Jean-Paul Calderone23478b32013-02-20 13:31:38 -08002867 helper = _PassphraseHelper(type, passphrase)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002868 if type == FILETYPE_PEM:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002869 evp_pkey = _lib.PEM_read_bio_PrivateKey(
Alex Gaynor03737182020-07-23 20:40:46 -04002870 bio, _ffi.NULL, helper.callback, helper.callback_args
2871 )
Jean-Paul Calderonee41f05c2013-02-20 13:28:16 -08002872 helper.raise_if_problem()
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002873 elif type == FILETYPE_ASN1:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002874 evp_pkey = _lib.d2i_PrivateKey_bio(bio, _ffi.NULL)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002875 else:
2876 raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1")
2877
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002878 if evp_pkey == _ffi.NULL:
Jean-Paul Calderone31393aa2013-02-20 13:22:21 -08002879 _raise_current_error()
2880
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002881 pkey = PKey.__new__(PKey)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002882 pkey._pkey = _ffi.gc(evp_pkey, _lib.EVP_PKEY_free)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002883 return pkey
2884
2885
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002886def dump_certificate_request(type, req):
2887 """
Alex Chand072cae2018-02-15 09:57:59 +00002888 Dump the certificate request *req* into a buffer string encoded with the
2889 type *type*.
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002890
2891 :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1)
2892 :param req: The certificate request to dump
2893 :return: The buffer with the dumped certificate request in
2894 """
Jean-Paul Calderoneef9a3dc2013-03-02 16:33:32 -08002895 bio = _new_mem_buf()
Jean-Paul Calderone066f0572013-02-20 13:43:44 -08002896
2897 if type == FILETYPE_PEM:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002898 result_code = _lib.PEM_write_bio_X509_REQ(bio, req._req)
Jean-Paul Calderone066f0572013-02-20 13:43:44 -08002899 elif type == FILETYPE_ASN1:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002900 result_code = _lib.i2d_X509_REQ_bio(bio, req._req)
Jean-Paul Calderone066f0572013-02-20 13:43:44 -08002901 elif type == FILETYPE_TEXT:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002902 result_code = _lib.X509_REQ_print_ex(bio, req._req, 0, 0)
Jean-Paul Calderone066f0572013-02-20 13:43:44 -08002903 else:
Alex Gaynor5945ea82015-09-05 14:59:06 -04002904 raise ValueError(
2905 "type argument must be FILETYPE_PEM, FILETYPE_ASN1, or "
2906 "FILETYPE_TEXT"
2907 )
Jean-Paul Calderone066f0572013-02-20 13:43:44 -08002908
Alex Gaynor09a386e2016-07-03 09:32:44 -04002909 _openssl_assert(result_code != 0)
Jean-Paul Calderone066f0572013-02-20 13:43:44 -08002910
2911 return _bio_to_string(bio)
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002912
2913
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002914def load_certificate_request(type, buffer):
2915 """
Alex Chand072cae2018-02-15 09:57:59 +00002916 Load a certificate request (X509Req) from the string *buffer* encoded with
2917 the type *type*.
Jean-Paul Calderoneabfbab62013-02-09 21:25:02 -08002918
2919 :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1)
2920 :param buffer: The buffer the certificate request is stored in
2921 :return: The X509Req object
2922 """
Jean-Paul Calderone6922a862014-01-18 10:38:28 -05002923 if isinstance(buffer, _text_type):
2924 buffer = buffer.encode("ascii")
2925
Jean-Paul Calderonef6745b32013-03-01 15:08:46 -08002926 bio = _new_mem_buf(buffer)
Jean-Paul Calderone066f0572013-02-20 13:43:44 -08002927
2928 if type == FILETYPE_PEM:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002929 req = _lib.PEM_read_bio_X509_REQ(bio, _ffi.NULL, _ffi.NULL, _ffi.NULL)
Jean-Paul Calderone066f0572013-02-20 13:43:44 -08002930 elif type == FILETYPE_ASN1:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002931 req = _lib.d2i_X509_REQ_bio(bio, _ffi.NULL)
Jean-Paul Calderone066f0572013-02-20 13:43:44 -08002932 else:
Jean-Paul Calderone4a68b402013-12-29 16:54:58 -05002933 raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1")
Jean-Paul Calderone066f0572013-02-20 13:43:44 -08002934
Alex Gaynoradd5b072016-06-04 21:04:00 -07002935 _openssl_assert(req != _ffi.NULL)
Jean-Paul Calderone066f0572013-02-20 13:43:44 -08002936
2937 x509req = X509Req.__new__(X509Req)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002938 x509req._req = _ffi.gc(req, _lib.X509_REQ_free)
Jean-Paul Calderone066f0572013-02-20 13:43:44 -08002939 return x509req
Jean-Paul Calderone8cf4f802013-02-20 16:45:02 -08002940
2941
Jean-Paul Calderone8cf4f802013-02-20 16:45:02 -08002942def sign(pkey, data, digest):
2943 """
Alex Chand072cae2018-02-15 09:57:59 +00002944 Sign a data string using the given key and message digest.
Jean-Paul Calderone8cf4f802013-02-20 16:45:02 -08002945
Alex Chand072cae2018-02-15 09:57:59 +00002946 :param pkey: PKey to sign with
Jean-Paul Calderone8cf4f802013-02-20 16:45:02 -08002947 :param data: data to be signed
2948 :param digest: message digest to use
2949 :return: signature
Alex Chand072cae2018-02-15 09:57:59 +00002950
2951 .. versionadded:: 0.11
Jean-Paul Calderone8cf4f802013-02-20 16:45:02 -08002952 """
Jean-Paul Calderone39a8d592015-04-13 20:49:50 -04002953 data = _text_to_bytes_and_warn("data", data)
Abraham Martine82326c2015-02-04 10:18:10 +00002954
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -05002955 digest_obj = _lib.EVP_get_digestbyname(_byte_string(digest))
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002956 if digest_obj == _ffi.NULL:
Jean-Paul Calderone8cf4f802013-02-20 16:45:02 -08002957 raise ValueError("No such digest method")
2958
Alex Gaynor67903a62016-06-02 10:37:13 -07002959 md_ctx = _lib.Cryptography_EVP_MD_CTX_new()
Alex Gaynor1f9d4de2016-06-02 11:01:52 -07002960 md_ctx = _ffi.gc(md_ctx, _lib.Cryptography_EVP_MD_CTX_free)
Jean-Paul Calderone8cf4f802013-02-20 16:45:02 -08002961
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002962 _lib.EVP_SignInit(md_ctx, digest_obj)
2963 _lib.EVP_SignUpdate(md_ctx, data, len(data))
Jean-Paul Calderone8cf4f802013-02-20 16:45:02 -08002964
Paul Kehrer59d26252017-07-20 10:45:54 +02002965 length = _lib.EVP_PKEY_size(pkey._pkey)
2966 _openssl_assert(length > 0)
2967 signature_buffer = _ffi.new("unsigned char[]", length)
2968 signature_length = _ffi.new("unsigned int *")
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002969 final_result = _lib.EVP_SignFinal(
Alex Gaynor03737182020-07-23 20:40:46 -04002970 md_ctx, signature_buffer, signature_length, pkey._pkey
2971 )
Alex Gaynor09a386e2016-07-03 09:32:44 -04002972 _openssl_assert(final_result == 1)
Jean-Paul Calderone8cf4f802013-02-20 16:45:02 -08002973
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002974 return _ffi.buffer(signature_buffer, signature_length[0])[:]
Jean-Paul Calderone8cf4f802013-02-20 16:45:02 -08002975
2976
Jean-Paul Calderone8cf4f802013-02-20 16:45:02 -08002977def verify(cert, signature, data, digest):
2978 """
Alex Chand072cae2018-02-15 09:57:59 +00002979 Verify the signature for a data string.
Jean-Paul Calderone8cf4f802013-02-20 16:45:02 -08002980
Alex Chand072cae2018-02-15 09:57:59 +00002981 :param cert: signing certificate (X509 object) corresponding to the
2982 private key which generated the signature.
Jean-Paul Calderone8cf4f802013-02-20 16:45:02 -08002983 :param signature: signature returned by sign function
2984 :param data: data to be verified
2985 :param digest: message digest to use
Dan Sully44e767a2016-06-04 18:05:27 -07002986 :return: ``None`` if the signature is correct, raise exception otherwise.
Alex Chand072cae2018-02-15 09:57:59 +00002987
2988 .. versionadded:: 0.11
Jean-Paul Calderone8cf4f802013-02-20 16:45:02 -08002989 """
Jean-Paul Calderone39a8d592015-04-13 20:49:50 -04002990 data = _text_to_bytes_and_warn("data", data)
Abraham Martine82326c2015-02-04 10:18:10 +00002991
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -05002992 digest_obj = _lib.EVP_get_digestbyname(_byte_string(digest))
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002993 if digest_obj == _ffi.NULL:
Jean-Paul Calderone8cf4f802013-02-20 16:45:02 -08002994 raise ValueError("No such digest method")
2995
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002996 pkey = _lib.X509_get_pubkey(cert._x509)
Alex Gaynoradd5b072016-06-04 21:04:00 -07002997 _openssl_assert(pkey != _ffi.NULL)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05002998 pkey = _ffi.gc(pkey, _lib.EVP_PKEY_free)
Jean-Paul Calderone8cf4f802013-02-20 16:45:02 -08002999
Alex Gaynor67903a62016-06-02 10:37:13 -07003000 md_ctx = _lib.Cryptography_EVP_MD_CTX_new()
Alex Gaynor1f9d4de2016-06-02 11:01:52 -07003001 md_ctx = _ffi.gc(md_ctx, _lib.Cryptography_EVP_MD_CTX_free)
Jean-Paul Calderone8cf4f802013-02-20 16:45:02 -08003002
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05003003 _lib.EVP_VerifyInit(md_ctx, digest_obj)
3004 _lib.EVP_VerifyUpdate(md_ctx, data, len(data))
Alex Gaynor5945ea82015-09-05 14:59:06 -04003005 verify_result = _lib.EVP_VerifyFinal(
3006 md_ctx, signature, len(signature), pkey
3007 )
Jean-Paul Calderone8cf4f802013-02-20 16:45:02 -08003008
3009 if verify_result != 1:
3010 _raise_current_error()
Jean-Paul Calderone57122982013-02-21 08:47:05 -08003011
3012
Dominic Chenf05b2122015-10-13 16:32:35 +00003013def dump_crl(type, crl):
3014 """
3015 Dump a certificate revocation list to a buffer.
3016
3017 :param type: The file type (one of ``FILETYPE_PEM``, ``FILETYPE_ASN1``, or
3018 ``FILETYPE_TEXT``).
Hynek Schlawack0a3cd6d2015-10-21 16:39:22 +02003019 :param CRL crl: The CRL to dump.
3020
Dominic Chenf05b2122015-10-13 16:32:35 +00003021 :return: The buffer with the CRL.
Dan Sully44e767a2016-06-04 18:05:27 -07003022 :rtype: bytes
Dominic Chenf05b2122015-10-13 16:32:35 +00003023 """
3024 bio = _new_mem_buf()
3025
3026 if type == FILETYPE_PEM:
3027 ret = _lib.PEM_write_bio_X509_CRL(bio, crl._crl)
3028 elif type == FILETYPE_ASN1:
3029 ret = _lib.i2d_X509_CRL_bio(bio, crl._crl)
3030 elif type == FILETYPE_TEXT:
3031 ret = _lib.X509_CRL_print(bio, crl._crl)
3032 else:
3033 raise ValueError(
3034 "type argument must be FILETYPE_PEM, FILETYPE_ASN1, or "
Alex Gaynor03737182020-07-23 20:40:46 -04003035 "FILETYPE_TEXT"
3036 )
Dominic Chenf05b2122015-10-13 16:32:35 +00003037
Adrián Chaves98c57be2020-03-31 16:14:50 +02003038 _openssl_assert(ret == 1)
Dominic Chenf05b2122015-10-13 16:32:35 +00003039 return _bio_to_string(bio)
3040
3041
Jean-Paul Calderone57122982013-02-21 08:47:05 -08003042def load_crl(type, buffer):
3043 """
Alex Chand072cae2018-02-15 09:57:59 +00003044 Load Certificate Revocation List (CRL) data from a string *buffer*.
3045 *buffer* encoded with the type *type*.
Jean-Paul Calderone57122982013-02-21 08:47:05 -08003046
3047 :param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1)
3048 :param buffer: The buffer the CRL is stored in
3049
3050 :return: The PKey object
3051 """
Jean-Paul Calderone6922a862014-01-18 10:38:28 -05003052 if isinstance(buffer, _text_type):
3053 buffer = buffer.encode("ascii")
3054
Jean-Paul Calderonef6745b32013-03-01 15:08:46 -08003055 bio = _new_mem_buf(buffer)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08003056
3057 if type == FILETYPE_PEM:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05003058 crl = _lib.PEM_read_bio_X509_CRL(bio, _ffi.NULL, _ffi.NULL, _ffi.NULL)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08003059 elif type == FILETYPE_ASN1:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05003060 crl = _lib.d2i_X509_CRL_bio(bio, _ffi.NULL)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08003061 else:
Jean-Paul Calderone57122982013-02-21 08:47:05 -08003062 raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1")
3063
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05003064 if crl == _ffi.NULL:
Jean-Paul Calderone57122982013-02-21 08:47:05 -08003065 _raise_current_error()
3066
3067 result = CRL.__new__(CRL)
Jeremy Cline9e15eca2017-09-07 20:11:08 -04003068 result._crl = _ffi.gc(crl, _lib.X509_CRL_free)
Jean-Paul Calderone57122982013-02-21 08:47:05 -08003069 return result
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08003070
3071
Jean-Paul Calderone4e8be1c2013-02-21 18:31:12 -08003072def load_pkcs7_data(type, buffer):
3073 """
Alex Chand072cae2018-02-15 09:57:59 +00003074 Load pkcs7 data from the string *buffer* encoded with the type
3075 *type*.
Jean-Paul Calderone4e8be1c2013-02-21 18:31:12 -08003076
3077 :param type: The file type (one of FILETYPE_PEM or FILETYPE_ASN1)
3078 :param buffer: The buffer with the pkcs7 data.
3079 :return: The PKCS7 object
3080 """
Jean-Paul Calderone6922a862014-01-18 10:38:28 -05003081 if isinstance(buffer, _text_type):
3082 buffer = buffer.encode("ascii")
3083
Jean-Paul Calderonef6745b32013-03-01 15:08:46 -08003084 bio = _new_mem_buf(buffer)
Jean-Paul Calderone4e8be1c2013-02-21 18:31:12 -08003085
3086 if type == FILETYPE_PEM:
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05003087 pkcs7 = _lib.PEM_read_bio_PKCS7(bio, _ffi.NULL, _ffi.NULL, _ffi.NULL)
Jean-Paul Calderone4e8be1c2013-02-21 18:31:12 -08003088 elif type == FILETYPE_ASN1:
Alex Gaynor77acc362014-08-13 14:46:15 -07003089 pkcs7 = _lib.d2i_PKCS7_bio(bio, _ffi.NULL)
Jean-Paul Calderone4e8be1c2013-02-21 18:31:12 -08003090 else:
Jean-Paul Calderone4e8be1c2013-02-21 18:31:12 -08003091 raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1")
3092
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05003093 if pkcs7 == _ffi.NULL:
Jean-Paul Calderoneb0f64712013-03-03 10:15:39 -08003094 _raise_current_error()
Jean-Paul Calderone4e8be1c2013-02-21 18:31:12 -08003095
3096 pypkcs7 = PKCS7.__new__(PKCS7)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05003097 pypkcs7._pkcs7 = _ffi.gc(pkcs7, _lib.PKCS7_free)
Jean-Paul Calderone4e8be1c2013-02-21 18:31:12 -08003098 return pypkcs7
3099
3100
Alex Gaynorbb971ae2020-08-05 01:14:16 -04003101load_pkcs7_data = utils.deprecated(
3102 load_pkcs7_data,
3103 __name__,
3104 (
3105 "PKCS#7 support in pyOpenSSL is deprecated. You should use the APIs "
3106 "in cryptography."
3107 ),
3108 DeprecationWarning,
3109)
3110
3111
Stephen Holsapple38482622014-04-05 20:29:34 -07003112def load_pkcs12(buffer, passphrase=None):
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08003113 """
Alex Chand072cae2018-02-15 09:57:59 +00003114 Load pkcs12 data from the string *buffer*. If the pkcs12 structure is
3115 encrypted, a *passphrase* must be included. The MAC is always
3116 checked and thus required.
3117
3118 See also the man page for the C function :py:func:`PKCS12_parse`.
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08003119
3120 :param buffer: The buffer the certificate is stored in
3121 :param passphrase: (Optional) The password to decrypt the PKCS12 lump
3122 :returns: The PKCS12 object
3123 """
Jean-Paul Calderone39a8d592015-04-13 20:49:50 -04003124 passphrase = _text_to_bytes_and_warn("passphrase", passphrase)
Abraham Martine82326c2015-02-04 10:18:10 +00003125
Jean-Paul Calderone6922a862014-01-18 10:38:28 -05003126 if isinstance(buffer, _text_type):
3127 buffer = buffer.encode("ascii")
3128
Jean-Paul Calderonef6745b32013-03-01 15:08:46 -08003129 bio = _new_mem_buf(buffer)
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08003130
Stephen Holsapple38482622014-04-05 20:29:34 -07003131 # Use null passphrase if passphrase is None or empty string. With PKCS#12
3132 # password based encryption no password and a zero length password are two
3133 # different things, but OpenSSL implementation will try both to figure out
3134 # which one works.
3135 if not passphrase:
3136 passphrase = _ffi.NULL
3137
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05003138 p12 = _lib.d2i_PKCS12_bio(bio, _ffi.NULL)
3139 if p12 == _ffi.NULL:
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08003140 _raise_current_error()
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05003141 p12 = _ffi.gc(p12, _lib.PKCS12_free)
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08003142
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05003143 pkey = _ffi.new("EVP_PKEY**")
3144 cert = _ffi.new("X509**")
3145 cacerts = _ffi.new("Cryptography_STACK_OF_X509**")
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08003146
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05003147 parse_result = _lib.PKCS12_parse(p12, passphrase, pkey, cert, cacerts)
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08003148 if not parse_result:
3149 _raise_current_error()
3150
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05003151 cacerts = _ffi.gc(cacerts[0], _lib.sk_X509_free)
Jean-Paul Calderoneef9a3dc2013-03-02 16:33:32 -08003152
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08003153 # openssl 1.0.0 sometimes leaves an X509_check_private_key error in the
3154 # queue for no particular reason. This error isn't interesting to anyone
3155 # outside this function. It's not even interesting to us. Get rid of it.
3156 try:
3157 _raise_current_error()
3158 except Error:
3159 pass
3160
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05003161 if pkey[0] == _ffi.NULL:
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08003162 pykey = None
3163 else:
3164 pykey = PKey.__new__(PKey)
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05003165 pykey._pkey = _ffi.gc(pkey[0], _lib.EVP_PKEY_free)
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08003166
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05003167 if cert[0] == _ffi.NULL:
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08003168 pycert = None
3169 friendlyname = None
3170 else:
Paul Kehrere7381862017-11-30 20:55:25 +08003171 pycert = X509._from_raw_x509_ptr(cert[0])
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08003172
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05003173 friendlyname_length = _ffi.new("int*")
Alex Gaynor5945ea82015-09-05 14:59:06 -04003174 friendlyname_buffer = _lib.X509_alias_get0(
3175 cert[0], friendlyname_length
3176 )
3177 friendlyname = _ffi.buffer(
3178 friendlyname_buffer, friendlyname_length[0]
3179 )[:]
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05003180 if friendlyname_buffer == _ffi.NULL:
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08003181 friendlyname = None
3182
3183 pycacerts = []
Jean-Paul Calderone6037d072013-12-28 18:04:00 -05003184 for i in range(_lib.sk_X509_num(cacerts)):
Paul Kehrere7381862017-11-30 20:55:25 +08003185 x509 = _lib.sk_X509_value(cacerts, i)
3186 pycacert = X509._from_raw_x509_ptr(x509)
Jean-Paul Calderonee5912ce2013-02-21 10:49:35 -08003187 pycacerts.append(pycacert)
3188 if not pycacerts:
3189 pycacerts = None
3190
3191 pkcs12 = PKCS12.__new__(PKCS12)
3192 pkcs12._pkey = pykey
3193 pkcs12._cert = pycert
3194 pkcs12._cacerts = pycacerts
3195 pkcs12._friendlyname = friendlyname
3196 return pkcs12
Jean-Paul Calderone6bb40892014-01-01 12:21:34 -05003197
3198
Alex Gaynorbb971ae2020-08-05 01:14:16 -04003199load_pkcs12 = utils.deprecated(
3200 load_pkcs12,
3201 __name__,
3202 (
3203 "PKCS#12 support in pyOpenSSL is deprecated. You should use the APIs "
3204 "in cryptography."
3205 ),
3206 DeprecationWarning,
3207)
3208
3209
Jean-Paul Calderoneb64e2a22014-01-11 08:06:35 -05003210# There are no direct unit tests for this initialization. It is tested
3211# indirectly since it is necessary for functions like dump_privatekey when
3212# using encryption.
3213#
3214# Thus OpenSSL.test.test_crypto.FunctionTests.test_dump_privatekey_passphrase
3215# and some other similar tests may fail without this (though they may not if
3216# the Python runtime has already done some initialization of the underlying
3217# OpenSSL library (and is linked against the same one that cryptography is
3218# using)).
Jean-Paul Calderonee324fd62014-01-11 08:00:33 -05003219_lib.OpenSSL_add_all_algorithms()
Jean-Paul Calderone11ed8e82014-01-18 10:21:50 -05003220
Jean-Paul Calderonefab157b2014-01-18 11:21:38 -05003221# This is similar but exercised mainly by exception_from_error_queue. It calls
3222# both ERR_load_crypto_strings() and ERR_load_SSL_strings().
3223_lib.SSL_load_error_strings()
D.S. Ljungmark349e1362014-05-31 18:40:38 +02003224
3225
D.S. Ljungmark349e1362014-05-31 18:40:38 +02003226# Set the default string mask to match OpenSSL upstream (since 2005) and
3227# RFC5280 recommendations.
Alex Gaynor03737182020-07-23 20:40:46 -04003228_lib.ASN1_STRING_set_default_mask_asc(b"utf8only")