2018-12-31 23:25:26 +00:00
|
|
|
import datetime
|
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
from base64 import b16encode
|
|
|
|
from functools import partial
|
|
|
|
from operator import __eq__, __ne__, __lt__, __le__, __gt__, __ge__
|
|
|
|
|
|
|
|
from six import (
|
|
|
|
integer_types as _integer_types,
|
|
|
|
text_type as _text_type,
|
|
|
|
PY3 as _PY3)
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
from cryptography import x509
|
|
|
|
from cryptography.hazmat.primitives.asymmetric import dsa, rsa
|
|
|
|
from cryptography.utils import deprecated
|
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
from OpenSSL._util import (
|
|
|
|
ffi as _ffi,
|
|
|
|
lib as _lib,
|
|
|
|
exception_from_error_queue as _exception_from_error_queue,
|
|
|
|
byte_string as _byte_string,
|
|
|
|
native as _native,
|
|
|
|
UNSPECIFIED as _UNSPECIFIED,
|
|
|
|
text_to_bytes_and_warn as _text_to_bytes_and_warn,
|
2018-12-31 23:25:26 +00:00
|
|
|
make_assert as _make_assert,
|
2016-02-06 09:36:57 +00:00
|
|
|
)
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
__all__ = [
|
|
|
|
'FILETYPE_PEM',
|
|
|
|
'FILETYPE_ASN1',
|
|
|
|
'FILETYPE_TEXT',
|
|
|
|
'TYPE_RSA',
|
|
|
|
'TYPE_DSA',
|
|
|
|
'Error',
|
|
|
|
'PKey',
|
|
|
|
'get_elliptic_curves',
|
|
|
|
'get_elliptic_curve',
|
|
|
|
'X509Name',
|
|
|
|
'X509Extension',
|
|
|
|
'X509Req',
|
|
|
|
'X509',
|
|
|
|
'X509StoreFlags',
|
|
|
|
'X509Store',
|
|
|
|
'X509StoreContextError',
|
|
|
|
'X509StoreContext',
|
|
|
|
'load_certificate',
|
|
|
|
'dump_certificate',
|
|
|
|
'dump_publickey',
|
|
|
|
'dump_privatekey',
|
|
|
|
'Revoked',
|
|
|
|
'CRL',
|
|
|
|
'PKCS7',
|
|
|
|
'PKCS12',
|
|
|
|
'NetscapeSPKI',
|
|
|
|
'load_publickey',
|
|
|
|
'load_privatekey',
|
|
|
|
'dump_certificate_request',
|
|
|
|
'load_certificate_request',
|
|
|
|
'sign',
|
|
|
|
'verify',
|
|
|
|
'dump_crl',
|
|
|
|
'load_crl',
|
|
|
|
'load_pkcs7_data',
|
|
|
|
'load_pkcs12'
|
|
|
|
]
|
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
FILETYPE_PEM = _lib.SSL_FILETYPE_PEM
|
|
|
|
FILETYPE_ASN1 = _lib.SSL_FILETYPE_ASN1
|
|
|
|
|
|
|
|
# TODO This was an API mistake. OpenSSL has no such constant.
|
|
|
|
FILETYPE_TEXT = 2 ** 16 - 1
|
|
|
|
|
|
|
|
TYPE_RSA = _lib.EVP_PKEY_RSA
|
|
|
|
TYPE_DSA = _lib.EVP_PKEY_DSA
|
|
|
|
|
|
|
|
|
|
|
|
class Error(Exception):
|
|
|
|
"""
|
|
|
|
An error occurred in an `OpenSSL.crypto` API.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
_raise_current_error = partial(_exception_from_error_queue, Error)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert = _make_assert(Error)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
def _get_backend():
|
|
|
|
"""
|
|
|
|
Importing the backend from cryptography has the side effect of activating
|
|
|
|
the osrandom engine. This mutates the global state of OpenSSL in the
|
|
|
|
process and causes issues for various programs that use subinterpreters or
|
|
|
|
embed Python. By putting the import in this function we can avoid
|
|
|
|
triggering this side effect unless _get_backend is called.
|
|
|
|
"""
|
|
|
|
from cryptography.hazmat.backends.openssl.backend import backend
|
|
|
|
return backend
|
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def _untested_error(where):
|
|
|
|
"""
|
|
|
|
An OpenSSL API failed somehow. Additionally, the failure which was
|
|
|
|
encountered isn't one that's exercised by the test suite so future behavior
|
|
|
|
of pyOpenSSL is now somewhat less predictable.
|
|
|
|
"""
|
|
|
|
raise RuntimeError("Unknown %s failure" % (where,))
|
|
|
|
|
|
|
|
|
|
|
|
def _new_mem_buf(buffer=None):
|
|
|
|
"""
|
|
|
|
Allocate a new OpenSSL memory BIO.
|
|
|
|
|
|
|
|
Arrange for the garbage collector to clean it up automatically.
|
|
|
|
|
|
|
|
:param buffer: None or some bytes to use to put into the BIO so that they
|
|
|
|
can be read out.
|
|
|
|
"""
|
|
|
|
if buffer is None:
|
|
|
|
bio = _lib.BIO_new(_lib.BIO_s_mem())
|
|
|
|
free = _lib.BIO_free
|
|
|
|
else:
|
|
|
|
data = _ffi.new("char[]", buffer)
|
|
|
|
bio = _lib.BIO_new_mem_buf(data, len(buffer))
|
2018-12-31 23:25:26 +00:00
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
# Keep the memory alive as long as the bio is alive!
|
|
|
|
def free(bio, ref=data):
|
|
|
|
return _lib.BIO_free(bio)
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(bio != _ffi.NULL)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
bio = _ffi.gc(bio, free)
|
|
|
|
return bio
|
|
|
|
|
|
|
|
|
|
|
|
def _bio_to_string(bio):
|
|
|
|
"""
|
|
|
|
Copy the contents of an OpenSSL BIO object into a Python byte string.
|
|
|
|
"""
|
|
|
|
result_buffer = _ffi.new('char**')
|
|
|
|
buffer_length = _lib.BIO_get_mem_data(bio, result_buffer)
|
|
|
|
return _ffi.buffer(result_buffer[0], buffer_length)[:]
|
|
|
|
|
|
|
|
|
|
|
|
def _set_asn1_time(boundary, when):
|
|
|
|
"""
|
|
|
|
The the time value of an ASN1 time object.
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
@param boundary: An ASN1_TIME pointer (or an object safely
|
2016-02-06 09:36:57 +00:00
|
|
|
castable to that type) which will have its value set.
|
|
|
|
@param when: A string representation of the desired time value.
|
|
|
|
|
|
|
|
@raise TypeError: If C{when} is not a L{bytes} string.
|
|
|
|
@raise ValueError: If C{when} does not represent a time in the required
|
|
|
|
format.
|
|
|
|
@raise RuntimeError: If the time value cannot be set for some other
|
|
|
|
(unspecified) reason.
|
|
|
|
"""
|
|
|
|
if not isinstance(when, bytes):
|
|
|
|
raise TypeError("when must be a byte string")
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
set_result = _lib.ASN1_TIME_set_string(boundary, when)
|
2016-02-06 09:36:57 +00:00
|
|
|
if set_result == 0:
|
2018-12-31 23:25:26 +00:00
|
|
|
raise ValueError("Invalid string")
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _get_asn1_time(timestamp):
|
|
|
|
"""
|
|
|
|
Retrieve the time value of an ASN1 time object.
|
|
|
|
|
|
|
|
@param timestamp: An ASN1_GENERALIZEDTIME* (or an object safely castable to
|
|
|
|
that type) from which the time value will be retrieved.
|
|
|
|
|
|
|
|
@return: The time value from C{timestamp} as a L{bytes} string in a certain
|
|
|
|
format. Or C{None} if the object contains no time value.
|
|
|
|
"""
|
|
|
|
string_timestamp = _ffi.cast('ASN1_STRING*', timestamp)
|
|
|
|
if _lib.ASN1_STRING_length(string_timestamp) == 0:
|
|
|
|
return None
|
2018-12-31 23:25:26 +00:00
|
|
|
elif (
|
|
|
|
_lib.ASN1_STRING_type(string_timestamp) == _lib.V_ASN1_GENERALIZEDTIME
|
|
|
|
):
|
2016-02-06 09:36:57 +00:00
|
|
|
return _ffi.string(_lib.ASN1_STRING_data(string_timestamp))
|
|
|
|
else:
|
|
|
|
generalized_timestamp = _ffi.new("ASN1_GENERALIZEDTIME**")
|
|
|
|
_lib.ASN1_TIME_to_generalizedtime(timestamp, generalized_timestamp)
|
|
|
|
if generalized_timestamp[0] == _ffi.NULL:
|
|
|
|
# This may happen:
|
|
|
|
# - if timestamp was not an ASN1_TIME
|
|
|
|
# - if allocating memory for the ASN1_GENERALIZEDTIME failed
|
|
|
|
# - if a copy of the time data from timestamp cannot be made for
|
|
|
|
# the newly allocated ASN1_GENERALIZEDTIME
|
|
|
|
#
|
|
|
|
# These are difficult to test. cffi enforces the ASN1_TIME type.
|
|
|
|
# Memory allocation failures are a pain to trigger
|
|
|
|
# deterministically.
|
|
|
|
_untested_error("ASN1_TIME_to_generalizedtime")
|
|
|
|
else:
|
|
|
|
string_timestamp = _ffi.cast(
|
|
|
|
"ASN1_STRING*", generalized_timestamp[0])
|
|
|
|
string_data = _lib.ASN1_STRING_data(string_timestamp)
|
|
|
|
string_result = _ffi.string(string_data)
|
|
|
|
_lib.ASN1_GENERALIZEDTIME_free(generalized_timestamp[0])
|
|
|
|
return string_result
|
|
|
|
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
class _X509NameInvalidator(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._names = []
|
|
|
|
|
|
|
|
def add(self, name):
|
|
|
|
self._names.append(name)
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
for name in self._names:
|
|
|
|
# Breaks the object, but also prevents UAF!
|
|
|
|
del name._name
|
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
class PKey(object):
|
2018-12-31 23:25:26 +00:00
|
|
|
"""
|
|
|
|
A class representing an DSA or RSA public key or key pair.
|
|
|
|
"""
|
2016-02-06 09:36:57 +00:00
|
|
|
_only_public = False
|
|
|
|
_initialized = True
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
pkey = _lib.EVP_PKEY_new()
|
|
|
|
self._pkey = _ffi.gc(pkey, _lib.EVP_PKEY_free)
|
|
|
|
self._initialized = False
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
def to_cryptography_key(self):
|
|
|
|
"""
|
|
|
|
Export as a ``cryptography`` key.
|
|
|
|
|
|
|
|
:rtype: One of ``cryptography``'s `key interfaces`_.
|
|
|
|
|
|
|
|
.. _key interfaces: https://cryptography.io/en/latest/hazmat/\
|
|
|
|
primitives/asymmetric/rsa/#key-interfaces
|
|
|
|
|
|
|
|
.. versionadded:: 16.1.0
|
|
|
|
"""
|
|
|
|
backend = _get_backend()
|
|
|
|
if self._only_public:
|
|
|
|
return backend._evp_pkey_to_public_key(self._pkey)
|
|
|
|
else:
|
|
|
|
return backend._evp_pkey_to_private_key(self._pkey)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_cryptography_key(cls, crypto_key):
|
|
|
|
"""
|
|
|
|
Construct based on a ``cryptography`` *crypto_key*.
|
|
|
|
|
|
|
|
:param crypto_key: A ``cryptography`` key.
|
|
|
|
:type crypto_key: One of ``cryptography``'s `key interfaces`_.
|
|
|
|
|
|
|
|
:rtype: PKey
|
|
|
|
|
|
|
|
.. versionadded:: 16.1.0
|
|
|
|
"""
|
|
|
|
pkey = cls()
|
|
|
|
if not isinstance(crypto_key, (rsa.RSAPublicKey, rsa.RSAPrivateKey,
|
|
|
|
dsa.DSAPublicKey, dsa.DSAPrivateKey)):
|
|
|
|
raise TypeError("Unsupported key type")
|
|
|
|
|
|
|
|
pkey._pkey = crypto_key._evp_pkey
|
|
|
|
if isinstance(crypto_key, (rsa.RSAPublicKey, dsa.DSAPublicKey)):
|
|
|
|
pkey._only_public = True
|
|
|
|
pkey._initialized = True
|
|
|
|
return pkey
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def generate_key(self, type, bits):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Generate a key pair of the given type, with the given number of bits.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
This generates a key "into" the this object.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param type: The key type.
|
|
|
|
:type type: :py:data:`TYPE_RSA` or :py:data:`TYPE_DSA`
|
|
|
|
:param bits: The number of bits.
|
|
|
|
:type bits: :py:data:`int` ``>= 0``
|
|
|
|
:raises TypeError: If :py:data:`type` or :py:data:`bits` isn't
|
|
|
|
of the appropriate type.
|
|
|
|
:raises ValueError: If the number of bits isn't an integer of
|
|
|
|
the appropriate size.
|
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
if not isinstance(type, int):
|
|
|
|
raise TypeError("type must be an integer")
|
|
|
|
|
|
|
|
if not isinstance(bits, int):
|
|
|
|
raise TypeError("bits must be an integer")
|
|
|
|
|
|
|
|
# TODO Check error return
|
|
|
|
exponent = _lib.BN_new()
|
|
|
|
exponent = _ffi.gc(exponent, _lib.BN_free)
|
|
|
|
_lib.BN_set_word(exponent, _lib.RSA_F4)
|
|
|
|
|
|
|
|
if type == TYPE_RSA:
|
|
|
|
if bits <= 0:
|
|
|
|
raise ValueError("Invalid number of bits")
|
|
|
|
|
|
|
|
rsa = _lib.RSA_new()
|
|
|
|
|
|
|
|
result = _lib.RSA_generate_key_ex(rsa, bits, exponent, _ffi.NULL)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(result == 1)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
result = _lib.EVP_PKEY_assign_RSA(self._pkey, rsa)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(result == 1)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
elif type == TYPE_DSA:
|
2018-12-31 23:25:26 +00:00
|
|
|
dsa = _lib.DSA_new()
|
|
|
|
_openssl_assert(dsa != _ffi.NULL)
|
|
|
|
|
|
|
|
dsa = _ffi.gc(dsa, _lib.DSA_free)
|
|
|
|
res = _lib.DSA_generate_parameters_ex(
|
|
|
|
dsa, bits, _ffi.NULL, 0, _ffi.NULL, _ffi.NULL, _ffi.NULL
|
|
|
|
)
|
|
|
|
_openssl_assert(res == 1)
|
|
|
|
|
|
|
|
_openssl_assert(_lib.DSA_generate_key(dsa) == 1)
|
|
|
|
_openssl_assert(_lib.EVP_PKEY_set1_DSA(self._pkey, dsa) == 1)
|
2016-02-06 09:36:57 +00:00
|
|
|
else:
|
|
|
|
raise Error("No such key type")
|
|
|
|
|
|
|
|
self._initialized = True
|
|
|
|
|
|
|
|
def check(self):
|
|
|
|
"""
|
|
|
|
Check the consistency of an RSA private key.
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
This is the Python equivalent of OpenSSL's ``RSA_check_key``.
|
|
|
|
|
|
|
|
:return: ``True`` if key is consistent.
|
|
|
|
|
|
|
|
:raise OpenSSL.crypto.Error: if the key is inconsistent.
|
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
:raise TypeError: if the key is of a type which cannot be checked.
|
|
|
|
Only RSA keys can currently be checked.
|
|
|
|
"""
|
|
|
|
if self._only_public:
|
|
|
|
raise TypeError("public key only")
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
if _lib.EVP_PKEY_type(self.type()) != _lib.EVP_PKEY_RSA:
|
2016-02-06 09:36:57 +00:00
|
|
|
raise TypeError("key type unsupported")
|
|
|
|
|
|
|
|
rsa = _lib.EVP_PKEY_get1_RSA(self._pkey)
|
|
|
|
rsa = _ffi.gc(rsa, _lib.RSA_free)
|
|
|
|
result = _lib.RSA_check_key(rsa)
|
|
|
|
if result:
|
|
|
|
return True
|
|
|
|
_raise_current_error()
|
|
|
|
|
|
|
|
def type(self):
|
|
|
|
"""
|
|
|
|
Returns the type of the key
|
|
|
|
|
|
|
|
:return: The type of the key.
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
return _lib.EVP_PKEY_id(self._pkey)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def bits(self):
|
|
|
|
"""
|
|
|
|
Returns the number of bits of the key
|
|
|
|
|
|
|
|
:return: The number of bits of the key.
|
|
|
|
"""
|
|
|
|
return _lib.EVP_PKEY_bits(self._pkey)
|
|
|
|
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
PKeyType = deprecated(
|
|
|
|
PKey, __name__,
|
|
|
|
"PKeyType has been deprecated, use PKey instead",
|
|
|
|
DeprecationWarning
|
|
|
|
)
|
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
class _EllipticCurve(object):
|
|
|
|
"""
|
|
|
|
A representation of a supported elliptic curve.
|
|
|
|
|
|
|
|
@cvar _curves: :py:obj:`None` until an attempt is made to load the curves.
|
|
|
|
Thereafter, a :py:type:`set` containing :py:type:`_EllipticCurve`
|
|
|
|
instances each of which represents one curve supported by the system.
|
|
|
|
@type _curves: :py:type:`NoneType` or :py:type:`set`
|
|
|
|
"""
|
|
|
|
_curves = None
|
|
|
|
|
|
|
|
if _PY3:
|
|
|
|
# This only necessary on Python 3. Morever, it is broken on Python 2.
|
|
|
|
def __ne__(self, other):
|
|
|
|
"""
|
|
|
|
Implement cooperation with the right-hand side argument of ``!=``.
|
|
|
|
|
|
|
|
Python 3 seems to have dropped this cooperation in this very narrow
|
|
|
|
circumstance.
|
|
|
|
"""
|
|
|
|
if isinstance(other, _EllipticCurve):
|
|
|
|
return super(_EllipticCurve, self).__ne__(other)
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _load_elliptic_curves(cls, lib):
|
|
|
|
"""
|
|
|
|
Get the curves supported by OpenSSL.
|
|
|
|
|
|
|
|
:param lib: The OpenSSL library binding object.
|
|
|
|
|
|
|
|
:return: A :py:type:`set` of ``cls`` instances giving the names of the
|
|
|
|
elliptic curves the underlying library supports.
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
num_curves = lib.EC_get_builtin_curves(_ffi.NULL, 0)
|
|
|
|
builtin_curves = _ffi.new('EC_builtin_curve[]', num_curves)
|
|
|
|
# The return value on this call should be num_curves again. We
|
|
|
|
# could check it to make sure but if it *isn't* then.. what could
|
|
|
|
# we do? Abort the whole process, I suppose...? -exarkun
|
|
|
|
lib.EC_get_builtin_curves(builtin_curves, num_curves)
|
|
|
|
return set(
|
|
|
|
cls.from_nid(lib, c.nid)
|
|
|
|
for c in builtin_curves)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _get_elliptic_curves(cls, lib):
|
|
|
|
"""
|
|
|
|
Get, cache, and return the curves supported by OpenSSL.
|
|
|
|
|
|
|
|
:param lib: The OpenSSL library binding object.
|
|
|
|
|
|
|
|
:return: A :py:type:`set` of ``cls`` instances giving the names of the
|
|
|
|
elliptic curves the underlying library supports.
|
|
|
|
"""
|
|
|
|
if cls._curves is None:
|
|
|
|
cls._curves = cls._load_elliptic_curves(lib)
|
|
|
|
return cls._curves
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_nid(cls, lib, nid):
|
|
|
|
"""
|
|
|
|
Instantiate a new :py:class:`_EllipticCurve` associated with the given
|
|
|
|
OpenSSL NID.
|
|
|
|
|
|
|
|
:param lib: The OpenSSL library binding object.
|
|
|
|
|
|
|
|
:param nid: The OpenSSL NID the resulting curve object will represent.
|
|
|
|
This must be a curve NID (and not, for example, a hash NID) or
|
|
|
|
subsequent operations will fail in unpredictable ways.
|
|
|
|
:type nid: :py:class:`int`
|
|
|
|
|
|
|
|
:return: The curve object.
|
|
|
|
"""
|
|
|
|
return cls(lib, nid, _ffi.string(lib.OBJ_nid2sn(nid)).decode("ascii"))
|
|
|
|
|
|
|
|
def __init__(self, lib, nid, name):
|
|
|
|
"""
|
|
|
|
:param _lib: The :py:mod:`cryptography` binding instance used to
|
|
|
|
interface with OpenSSL.
|
|
|
|
|
|
|
|
:param _nid: The OpenSSL NID identifying the curve this object
|
|
|
|
represents.
|
|
|
|
:type _nid: :py:class:`int`
|
|
|
|
|
|
|
|
:param name: The OpenSSL short name identifying the curve this object
|
|
|
|
represents.
|
|
|
|
:type name: :py:class:`unicode`
|
|
|
|
"""
|
|
|
|
self._lib = lib
|
|
|
|
self._nid = nid
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Curve %r>" % (self.name,)
|
|
|
|
|
|
|
|
def _to_EC_KEY(self):
|
|
|
|
"""
|
|
|
|
Create a new OpenSSL EC_KEY structure initialized to use this curve.
|
|
|
|
|
|
|
|
The structure is automatically garbage collected when the Python object
|
|
|
|
is garbage collected.
|
|
|
|
"""
|
|
|
|
key = self._lib.EC_KEY_new_by_curve_name(self._nid)
|
|
|
|
return _ffi.gc(key, _lib.EC_KEY_free)
|
|
|
|
|
|
|
|
|
|
|
|
def get_elliptic_curves():
|
|
|
|
"""
|
|
|
|
Return a set of objects representing the elliptic curves supported in the
|
|
|
|
OpenSSL build in use.
|
|
|
|
|
|
|
|
The curve objects have a :py:class:`unicode` ``name`` attribute by which
|
|
|
|
they identify themselves.
|
|
|
|
|
|
|
|
The curve objects are useful as values for the argument accepted by
|
|
|
|
:py:meth:`Context.set_tmp_ecdh` to specify which elliptical curve should be
|
|
|
|
used for ECDHE key exchange.
|
|
|
|
"""
|
|
|
|
return _EllipticCurve._get_elliptic_curves(_lib)
|
|
|
|
|
|
|
|
|
|
|
|
def get_elliptic_curve(name):
|
|
|
|
"""
|
|
|
|
Return a single curve object selected by name.
|
|
|
|
|
|
|
|
See :py:func:`get_elliptic_curves` for information about curve objects.
|
|
|
|
|
|
|
|
:param name: The OpenSSL short name identifying the curve object to
|
|
|
|
retrieve.
|
|
|
|
:type name: :py:class:`unicode`
|
|
|
|
|
|
|
|
If the named curve is not supported then :py:class:`ValueError` is raised.
|
|
|
|
"""
|
|
|
|
for curve in get_elliptic_curves():
|
|
|
|
if curve.name == name:
|
|
|
|
return curve
|
|
|
|
raise ValueError("unknown curve name", name)
|
|
|
|
|
|
|
|
|
|
|
|
class X509Name(object):
|
2018-12-31 23:25:26 +00:00
|
|
|
"""
|
|
|
|
An X.509 Distinguished Name.
|
|
|
|
|
|
|
|
:ivar countryName: The country of the entity.
|
|
|
|
:ivar C: Alias for :py:attr:`countryName`.
|
|
|
|
|
|
|
|
:ivar stateOrProvinceName: The state or province of the entity.
|
|
|
|
:ivar ST: Alias for :py:attr:`stateOrProvinceName`.
|
|
|
|
|
|
|
|
:ivar localityName: The locality of the entity.
|
|
|
|
:ivar L: Alias for :py:attr:`localityName`.
|
|
|
|
|
|
|
|
:ivar organizationName: The organization name of the entity.
|
|
|
|
:ivar O: Alias for :py:attr:`organizationName`.
|
|
|
|
|
|
|
|
:ivar organizationalUnitName: The organizational unit of the entity.
|
|
|
|
:ivar OU: Alias for :py:attr:`organizationalUnitName`
|
|
|
|
|
|
|
|
:ivar commonName: The common name of the entity.
|
|
|
|
:ivar CN: Alias for :py:attr:`commonName`.
|
|
|
|
|
|
|
|
:ivar emailAddress: The e-mail address of the entity.
|
|
|
|
"""
|
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
def __init__(self, name):
|
|
|
|
"""
|
|
|
|
Create a new X509Name, copying the given X509Name instance.
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param name: The name to copy.
|
|
|
|
:type name: :py:class:`X509Name`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
name = _lib.X509_NAME_dup(name._name)
|
|
|
|
self._name = _ffi.gc(name, _lib.X509_NAME_free)
|
|
|
|
|
|
|
|
def __setattr__(self, name, value):
|
|
|
|
if name.startswith('_'):
|
|
|
|
return super(X509Name, self).__setattr__(name, value)
|
|
|
|
|
|
|
|
# Note: we really do not want str subclasses here, so we do not use
|
|
|
|
# isinstance.
|
|
|
|
if type(name) is not str:
|
|
|
|
raise TypeError("attribute name must be string, not '%.200s'" % (
|
2018-12-31 23:25:26 +00:00
|
|
|
type(value).__name__,))
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
nid = _lib.OBJ_txt2nid(_byte_string(name))
|
|
|
|
if nid == _lib.NID_undef:
|
|
|
|
try:
|
|
|
|
_raise_current_error()
|
|
|
|
except Error:
|
|
|
|
pass
|
|
|
|
raise AttributeError("No such attribute")
|
|
|
|
|
|
|
|
# If there's an old entry for this NID, remove it
|
|
|
|
for i in range(_lib.X509_NAME_entry_count(self._name)):
|
|
|
|
ent = _lib.X509_NAME_get_entry(self._name, i)
|
|
|
|
ent_obj = _lib.X509_NAME_ENTRY_get_object(ent)
|
|
|
|
ent_nid = _lib.OBJ_obj2nid(ent_obj)
|
|
|
|
if nid == ent_nid:
|
|
|
|
ent = _lib.X509_NAME_delete_entry(self._name, i)
|
|
|
|
_lib.X509_NAME_ENTRY_free(ent)
|
|
|
|
break
|
|
|
|
|
|
|
|
if isinstance(value, _text_type):
|
|
|
|
value = value.encode('utf-8')
|
|
|
|
|
|
|
|
add_result = _lib.X509_NAME_add_entry_by_NID(
|
|
|
|
self._name, nid, _lib.MBSTRING_UTF8, value, -1, -1, 0)
|
|
|
|
if not add_result:
|
|
|
|
_raise_current_error()
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
"""
|
|
|
|
Find attribute. An X509Name object has the following attributes:
|
|
|
|
countryName (alias C), stateOrProvince (alias ST), locality (alias L),
|
2018-12-31 23:25:26 +00:00
|
|
|
organization (alias O), organizationalUnit (alias OU), commonName
|
|
|
|
(alias CN) and more...
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
nid = _lib.OBJ_txt2nid(_byte_string(name))
|
|
|
|
if nid == _lib.NID_undef:
|
|
|
|
# This is a bit weird. OBJ_txt2nid indicated failure, but it seems
|
|
|
|
# a lower level function, a2d_ASN1_OBJECT, also feels the need to
|
|
|
|
# push something onto the error queue. If we don't clean that up
|
|
|
|
# now, someone else will bump into it later and be quite confused.
|
|
|
|
# See lp#314814.
|
|
|
|
try:
|
|
|
|
_raise_current_error()
|
|
|
|
except Error:
|
|
|
|
pass
|
|
|
|
return super(X509Name, self).__getattr__(name)
|
|
|
|
|
|
|
|
entry_index = _lib.X509_NAME_get_index_by_NID(self._name, nid, -1)
|
|
|
|
if entry_index == -1:
|
|
|
|
return None
|
|
|
|
|
|
|
|
entry = _lib.X509_NAME_get_entry(self._name, entry_index)
|
|
|
|
data = _lib.X509_NAME_ENTRY_get_data(entry)
|
|
|
|
|
|
|
|
result_buffer = _ffi.new("unsigned char**")
|
|
|
|
data_length = _lib.ASN1_STRING_to_UTF8(result_buffer, data)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(data_length >= 0)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
try:
|
2018-12-31 23:25:26 +00:00
|
|
|
result = _ffi.buffer(
|
|
|
|
result_buffer[0], data_length
|
|
|
|
)[:].decode('utf-8')
|
2016-02-06 09:36:57 +00:00
|
|
|
finally:
|
|
|
|
# XXX untested
|
|
|
|
_lib.OPENSSL_free(result_buffer[0])
|
|
|
|
return result
|
|
|
|
|
|
|
|
def _cmp(op):
|
|
|
|
def f(self, other):
|
|
|
|
if not isinstance(other, X509Name):
|
|
|
|
return NotImplemented
|
|
|
|
result = _lib.X509_NAME_cmp(self._name, other._name)
|
|
|
|
return op(result, 0)
|
|
|
|
return f
|
|
|
|
|
|
|
|
__eq__ = _cmp(__eq__)
|
|
|
|
__ne__ = _cmp(__ne__)
|
|
|
|
|
|
|
|
__lt__ = _cmp(__lt__)
|
|
|
|
__le__ = _cmp(__le__)
|
|
|
|
|
|
|
|
__gt__ = _cmp(__gt__)
|
|
|
|
__ge__ = _cmp(__ge__)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
"""
|
|
|
|
String representation of an X509Name
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
result_buffer = _ffi.new("char[]", 512)
|
2016-02-06 09:36:57 +00:00
|
|
|
format_result = _lib.X509_NAME_oneline(
|
|
|
|
self._name, result_buffer, len(result_buffer))
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(format_result != _ffi.NULL)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
return "<X509Name object '%s'>" % (
|
|
|
|
_native(_ffi.string(result_buffer)),)
|
|
|
|
|
|
|
|
def hash(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Return an integer representation of the first four bytes of the
|
|
|
|
MD5 digest of the DER representation of the name.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
This is the Python equivalent of OpenSSL's ``X509_NAME_hash``.
|
|
|
|
|
|
|
|
:return: The (integer) hash of this name.
|
|
|
|
:rtype: :py:class:`int`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
return _lib.X509_NAME_hash(self._name)
|
|
|
|
|
|
|
|
def der(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Return the DER encoding of this name.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The DER encoded form of this name.
|
|
|
|
:rtype: :py:class:`bytes`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
result_buffer = _ffi.new('unsigned char**')
|
|
|
|
encode_result = _lib.i2d_X509_NAME(self._name, result_buffer)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(encode_result >= 0)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
string_result = _ffi.buffer(result_buffer[0], encode_result)[:]
|
|
|
|
_lib.OPENSSL_free(result_buffer[0])
|
|
|
|
return string_result
|
|
|
|
|
|
|
|
def get_components(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Returns the components of this name, as a sequence of 2-tuples.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The components of this name.
|
|
|
|
:rtype: :py:class:`list` of ``name, value`` tuples.
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
result = []
|
|
|
|
for i in range(_lib.X509_NAME_entry_count(self._name)):
|
|
|
|
ent = _lib.X509_NAME_get_entry(self._name, i)
|
|
|
|
|
|
|
|
fname = _lib.X509_NAME_ENTRY_get_object(ent)
|
|
|
|
fval = _lib.X509_NAME_ENTRY_get_data(ent)
|
|
|
|
|
|
|
|
nid = _lib.OBJ_obj2nid(fname)
|
|
|
|
name = _lib.OBJ_nid2sn(nid)
|
|
|
|
|
|
|
|
result.append((
|
2018-12-31 23:25:26 +00:00
|
|
|
_ffi.string(name),
|
|
|
|
_ffi.string(
|
|
|
|
_lib.ASN1_STRING_data(fval),
|
|
|
|
_lib.ASN1_STRING_length(fval))))
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
return result
|
2018-12-31 23:25:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
X509NameType = deprecated(
|
|
|
|
X509Name, __name__,
|
|
|
|
"X509NameType has been deprecated, use X509Name instead",
|
|
|
|
DeprecationWarning
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class X509Extension(object):
|
2018-12-31 23:25:26 +00:00
|
|
|
"""
|
|
|
|
An X.509 v3 certificate extension.
|
|
|
|
"""
|
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
def __init__(self, type_name, critical, value, subject=None, issuer=None):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Initializes an X509 extension.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param type_name: The name of the type of extension_ to create.
|
|
|
|
:type type_name: :py:data:`bytes`
|
|
|
|
|
|
|
|
:param bool critical: A flag indicating whether this is a critical
|
|
|
|
extension.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
:param value: The value of the extension.
|
2018-12-31 23:25:26 +00:00
|
|
|
:type value: :py:data:`bytes`
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param subject: Optional X509 certificate to use as subject.
|
2016-02-06 09:36:57 +00:00
|
|
|
:type subject: :py:class:`X509`
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param issuer: Optional X509 certificate to use as issuer.
|
2016-02-06 09:36:57 +00:00
|
|
|
:type issuer: :py:class:`X509`
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
.. _extension: https://www.openssl.org/docs/manmaster/man5/
|
|
|
|
x509v3_config.html#STANDARD-EXTENSIONS
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
ctx = _ffi.new("X509V3_CTX*")
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
# A context is necessary for any extension which uses the r2i
|
|
|
|
# conversion method. That is, X509V3_EXT_nconf may segfault if passed
|
|
|
|
# a NULL ctx. Start off by initializing most of the fields to NULL.
|
2016-02-06 09:36:57 +00:00
|
|
|
_lib.X509V3_set_ctx(ctx, _ffi.NULL, _ffi.NULL, _ffi.NULL, _ffi.NULL, 0)
|
|
|
|
|
|
|
|
# We have no configuration database - but perhaps we should (some
|
|
|
|
# extensions may require it).
|
|
|
|
_lib.X509V3_set_ctx_nodb(ctx)
|
|
|
|
|
|
|
|
# Initialize the subject and issuer, if appropriate. ctx is a local,
|
|
|
|
# and as far as I can tell none of the X509V3_* APIs invoked here steal
|
2018-12-31 23:25:26 +00:00
|
|
|
# any references, so no need to mess with reference counts or
|
|
|
|
# duplicates.
|
2016-02-06 09:36:57 +00:00
|
|
|
if issuer is not None:
|
|
|
|
if not isinstance(issuer, X509):
|
|
|
|
raise TypeError("issuer must be an X509 instance")
|
|
|
|
ctx.issuer_cert = issuer._x509
|
|
|
|
if subject is not None:
|
|
|
|
if not isinstance(subject, X509):
|
|
|
|
raise TypeError("subject must be an X509 instance")
|
|
|
|
ctx.subject_cert = subject._x509
|
|
|
|
|
|
|
|
if critical:
|
|
|
|
# There are other OpenSSL APIs which would let us pass in critical
|
|
|
|
# separately, but they're harder to use, and since value is already
|
|
|
|
# a pile of crappy junk smuggling a ton of utterly important
|
|
|
|
# structured data, what's the point of trying to avoid nasty stuff
|
2018-12-31 23:25:26 +00:00
|
|
|
# with strings? (However, X509V3_EXT_i2d in particular seems like
|
|
|
|
# it would be a better API to invoke. I do not know where to get
|
|
|
|
# the ext_struc it desires for its last parameter, though.)
|
2016-02-06 09:36:57 +00:00
|
|
|
value = b"critical," + value
|
|
|
|
|
|
|
|
extension = _lib.X509V3_EXT_nconf(_ffi.NULL, ctx, type_name, value)
|
|
|
|
if extension == _ffi.NULL:
|
|
|
|
_raise_current_error()
|
|
|
|
self._extension = _ffi.gc(extension, _lib.X509_EXTENSION_free)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _nid(self):
|
2018-12-31 23:25:26 +00:00
|
|
|
return _lib.OBJ_obj2nid(
|
|
|
|
_lib.X509_EXTENSION_get_object(self._extension)
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
_prefixes = {
|
|
|
|
_lib.GEN_EMAIL: "email",
|
|
|
|
_lib.GEN_DNS: "DNS",
|
|
|
|
_lib.GEN_URI: "URI",
|
2018-12-31 23:25:26 +00:00
|
|
|
}
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def _subjectAltNameString(self):
|
2018-12-31 23:25:26 +00:00
|
|
|
names = _ffi.cast(
|
|
|
|
"GENERAL_NAMES*", _lib.X509V3_EXT_d2i(self._extension)
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
names = _ffi.gc(names, _lib.GENERAL_NAMES_free)
|
2016-02-06 09:36:57 +00:00
|
|
|
parts = []
|
|
|
|
for i in range(_lib.sk_GENERAL_NAME_num(names)):
|
|
|
|
name = _lib.sk_GENERAL_NAME_value(names, i)
|
|
|
|
try:
|
|
|
|
label = self._prefixes[name.type]
|
|
|
|
except KeyError:
|
|
|
|
bio = _new_mem_buf()
|
|
|
|
_lib.GENERAL_NAME_print(bio, name)
|
|
|
|
parts.append(_native(_bio_to_string(bio)))
|
|
|
|
else:
|
|
|
|
value = _native(
|
|
|
|
_ffi.buffer(name.d.ia5.data, name.d.ia5.length)[:])
|
|
|
|
parts.append(label + ":" + value)
|
|
|
|
return ", ".join(parts)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"""
|
|
|
|
:return: a nice text representation of the extension
|
|
|
|
"""
|
|
|
|
if _lib.NID_subject_alt_name == self._nid:
|
|
|
|
return self._subjectAltNameString()
|
|
|
|
|
|
|
|
bio = _new_mem_buf()
|
|
|
|
print_result = _lib.X509V3_EXT_print(bio, self._extension, 0, 0)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(print_result != 0)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
return _native(_bio_to_string(bio))
|
|
|
|
|
|
|
|
def get_critical(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Returns the critical field of this X.509 extension.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
:return: The critical field.
|
|
|
|
"""
|
|
|
|
return _lib.X509_EXTENSION_get_critical(self._extension)
|
|
|
|
|
|
|
|
def get_short_name(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Returns the short type name of this X.509 extension.
|
|
|
|
|
|
|
|
The result is a byte string such as :py:const:`b"basicConstraints"`.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
:return: The short type name.
|
2018-12-31 23:25:26 +00:00
|
|
|
:rtype: :py:data:`bytes`
|
|
|
|
|
|
|
|
.. versionadded:: 0.12
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
obj = _lib.X509_EXTENSION_get_object(self._extension)
|
|
|
|
nid = _lib.OBJ_obj2nid(obj)
|
|
|
|
return _ffi.string(_lib.OBJ_nid2sn(nid))
|
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Returns the data of the X509 extension, encoded as ASN.1.
|
|
|
|
|
|
|
|
:return: The ASN.1 encoded data of this X509 extension.
|
|
|
|
:rtype: :py:data:`bytes`
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
.. versionadded:: 0.12
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
octet_result = _lib.X509_EXTENSION_get_data(self._extension)
|
|
|
|
string_result = _ffi.cast('ASN1_STRING*', octet_result)
|
|
|
|
char_result = _lib.ASN1_STRING_data(string_result)
|
|
|
|
result_length = _lib.ASN1_STRING_length(string_result)
|
|
|
|
return _ffi.buffer(char_result, result_length)[:]
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
|
|
|
|
X509ExtensionType = deprecated(
|
|
|
|
X509Extension, __name__,
|
|
|
|
"X509ExtensionType has been deprecated, use X509Extension instead",
|
|
|
|
DeprecationWarning
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class X509Req(object):
|
2018-12-31 23:25:26 +00:00
|
|
|
"""
|
|
|
|
An X.509 certificate signing requests.
|
|
|
|
"""
|
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
def __init__(self):
|
|
|
|
req = _lib.X509_REQ_new()
|
|
|
|
self._req = _ffi.gc(req, _lib.X509_REQ_free)
|
2018-12-31 23:25:26 +00:00
|
|
|
# Default to version 0.
|
|
|
|
self.set_version(0)
|
|
|
|
|
|
|
|
def to_cryptography(self):
|
|
|
|
"""
|
|
|
|
Export as a ``cryptography`` certificate signing request.
|
|
|
|
|
|
|
|
:rtype: ``cryptography.x509.CertificateSigningRequest``
|
|
|
|
|
|
|
|
.. versionadded:: 17.1.0
|
|
|
|
"""
|
|
|
|
from cryptography.hazmat.backends.openssl.x509 import (
|
|
|
|
_CertificateSigningRequest
|
|
|
|
)
|
|
|
|
backend = _get_backend()
|
|
|
|
return _CertificateSigningRequest(backend, self._req)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
@classmethod
|
|
|
|
def from_cryptography(cls, crypto_req):
|
|
|
|
"""
|
|
|
|
Construct based on a ``cryptography`` *crypto_req*.
|
|
|
|
|
|
|
|
:param crypto_req: A ``cryptography`` X.509 certificate signing request
|
|
|
|
:type crypto_req: ``cryptography.x509.CertificateSigningRequest``
|
|
|
|
|
|
|
|
:rtype: PKey
|
|
|
|
|
|
|
|
.. versionadded:: 17.1.0
|
|
|
|
"""
|
|
|
|
if not isinstance(crypto_req, x509.CertificateSigningRequest):
|
|
|
|
raise TypeError("Must be a certificate signing request")
|
|
|
|
|
|
|
|
req = cls()
|
|
|
|
req._req = crypto_req._x509_req
|
|
|
|
return req
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def set_pubkey(self, pkey):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Set the public key of the certificate signing request.
|
|
|
|
|
|
|
|
:param pkey: The public key to use.
|
|
|
|
:type pkey: :py:class:`PKey`
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
set_result = _lib.X509_REQ_set_pubkey(self._req, pkey._pkey)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(set_result == 1)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def get_pubkey(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Get the public key of the certificate signing request.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The public key.
|
|
|
|
:rtype: :py:class:`PKey`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
pkey = PKey.__new__(PKey)
|
|
|
|
pkey._pkey = _lib.X509_REQ_get_pubkey(self._req)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(pkey._pkey != _ffi.NULL)
|
2016-02-06 09:36:57 +00:00
|
|
|
pkey._pkey = _ffi.gc(pkey._pkey, _lib.EVP_PKEY_free)
|
|
|
|
pkey._only_public = True
|
|
|
|
return pkey
|
|
|
|
|
|
|
|
def set_version(self, version):
|
|
|
|
"""
|
|
|
|
Set the version subfield (RFC 2459, section 4.1.2.1) of the certificate
|
|
|
|
request.
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param int version: The version number.
|
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
set_result = _lib.X509_REQ_set_version(self._req, version)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(set_result == 1)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def get_version(self):
|
|
|
|
"""
|
|
|
|
Get the version subfield (RFC 2459, section 4.1.2.1) of the certificate
|
|
|
|
request.
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The value of the version subfield.
|
|
|
|
:rtype: :py:class:`int`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
return _lib.X509_REQ_get_version(self._req)
|
|
|
|
|
|
|
|
def get_subject(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Return the subject of this certificate signing request.
|
|
|
|
|
|
|
|
This creates a new :class:`X509Name` that wraps the underlying subject
|
|
|
|
name field on the certificate signing request. Modifying it will modify
|
|
|
|
the underlying signing request, and will have the effect of modifying
|
|
|
|
any other :class:`X509Name` that refers to this subject.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The subject of this certificate signing request.
|
|
|
|
:rtype: :class:`X509Name`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
name = X509Name.__new__(X509Name)
|
|
|
|
name._name = _lib.X509_REQ_get_subject_name(self._req)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(name._name != _ffi.NULL)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
# The name is owned by the X509Req structure. As long as the X509Name
|
|
|
|
# Python object is alive, keep the X509Req Python object alive.
|
|
|
|
name._owner = self
|
|
|
|
|
|
|
|
return name
|
|
|
|
|
|
|
|
def add_extensions(self, extensions):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Add extensions to the certificate signing request.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param extensions: The X.509 extensions to add.
|
|
|
|
:type extensions: iterable of :py:class:`X509Extension`
|
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
stack = _lib.sk_X509_EXTENSION_new_null()
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(stack != _ffi.NULL)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
stack = _ffi.gc(stack, _lib.sk_X509_EXTENSION_free)
|
|
|
|
|
|
|
|
for ext in extensions:
|
|
|
|
if not isinstance(ext, X509Extension):
|
|
|
|
raise ValueError("One of the elements is not an X509Extension")
|
|
|
|
|
|
|
|
# TODO push can fail (here and elsewhere)
|
|
|
|
_lib.sk_X509_EXTENSION_push(stack, ext._extension)
|
|
|
|
|
|
|
|
add_result = _lib.X509_REQ_add_extensions(self._req, stack)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(add_result == 1)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def get_extensions(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Get X.509 extensions in the certificate signing request.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The X.509 extensions in this request.
|
|
|
|
:rtype: :py:class:`list` of :py:class:`X509Extension` objects.
|
|
|
|
|
|
|
|
.. versionadded:: 0.15
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
exts = []
|
|
|
|
native_exts_obj = _lib.X509_REQ_get_extensions(self._req)
|
|
|
|
for i in range(_lib.sk_X509_EXTENSION_num(native_exts_obj)):
|
|
|
|
ext = X509Extension.__new__(X509Extension)
|
|
|
|
ext._extension = _lib.sk_X509_EXTENSION_value(native_exts_obj, i)
|
|
|
|
exts.append(ext)
|
|
|
|
return exts
|
|
|
|
|
|
|
|
def sign(self, pkey, digest):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Sign the certificate signing request with this key and digest type.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param pkey: The key pair to sign with.
|
|
|
|
:type pkey: :py:class:`PKey`
|
|
|
|
:param digest: The name of the message digest to use for the signature,
|
|
|
|
e.g. :py:data:`b"sha256"`.
|
|
|
|
:type digest: :py:class:`bytes`
|
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
if pkey._only_public:
|
|
|
|
raise ValueError("Key has only public part")
|
|
|
|
|
|
|
|
if not pkey._initialized:
|
|
|
|
raise ValueError("Key is uninitialized")
|
|
|
|
|
|
|
|
digest_obj = _lib.EVP_get_digestbyname(_byte_string(digest))
|
|
|
|
if digest_obj == _ffi.NULL:
|
|
|
|
raise ValueError("No such digest method")
|
|
|
|
|
|
|
|
sign_result = _lib.X509_REQ_sign(self._req, pkey._pkey, digest_obj)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(sign_result > 0)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def verify(self, pkey):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Verifies the signature on this certificate signing request.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param PKey key: A public key.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: ``True`` if the signature is correct.
|
|
|
|
:rtype: bool
|
|
|
|
|
|
|
|
:raises OpenSSL.crypto.Error: If the signature is invalid or there is a
|
2016-02-06 09:36:57 +00:00
|
|
|
problem verifying the signature.
|
|
|
|
"""
|
|
|
|
if not isinstance(pkey, PKey):
|
|
|
|
raise TypeError("pkey must be a PKey instance")
|
|
|
|
|
|
|
|
result = _lib.X509_REQ_verify(self._req, pkey._pkey)
|
|
|
|
if result <= 0:
|
|
|
|
_raise_current_error()
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
X509ReqType = deprecated(
|
|
|
|
X509Req, __name__,
|
|
|
|
"X509ReqType has been deprecated, use X509Req instead",
|
|
|
|
DeprecationWarning
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class X509(object):
|
2018-12-31 23:25:26 +00:00
|
|
|
"""
|
|
|
|
An X.509 certificate.
|
|
|
|
"""
|
2016-02-06 09:36:57 +00:00
|
|
|
def __init__(self):
|
|
|
|
x509 = _lib.X509_new()
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(x509 != _ffi.NULL)
|
2016-02-06 09:36:57 +00:00
|
|
|
self._x509 = _ffi.gc(x509, _lib.X509_free)
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
self._issuer_invalidator = _X509NameInvalidator()
|
|
|
|
self._subject_invalidator = _X509NameInvalidator()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _from_raw_x509_ptr(cls, x509):
|
|
|
|
cert = cls.__new__(cls)
|
|
|
|
cert._x509 = _ffi.gc(x509, _lib.X509_free)
|
|
|
|
cert._issuer_invalidator = _X509NameInvalidator()
|
|
|
|
cert._subject_invalidator = _X509NameInvalidator()
|
|
|
|
return cert
|
|
|
|
|
|
|
|
def to_cryptography(self):
|
|
|
|
"""
|
|
|
|
Export as a ``cryptography`` certificate.
|
|
|
|
|
|
|
|
:rtype: ``cryptography.x509.Certificate``
|
|
|
|
|
|
|
|
.. versionadded:: 17.1.0
|
|
|
|
"""
|
|
|
|
from cryptography.hazmat.backends.openssl.x509 import _Certificate
|
|
|
|
backend = _get_backend()
|
|
|
|
return _Certificate(backend, self._x509)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_cryptography(cls, crypto_cert):
|
|
|
|
"""
|
|
|
|
Construct based on a ``cryptography`` *crypto_cert*.
|
|
|
|
|
|
|
|
:param crypto_key: A ``cryptography`` X.509 certificate.
|
|
|
|
:type crypto_key: ``cryptography.x509.Certificate``
|
|
|
|
|
|
|
|
:rtype: PKey
|
|
|
|
|
|
|
|
.. versionadded:: 17.1.0
|
|
|
|
"""
|
|
|
|
if not isinstance(crypto_cert, x509.Certificate):
|
|
|
|
raise TypeError("Must be a certificate")
|
|
|
|
|
|
|
|
cert = cls()
|
|
|
|
cert._x509 = crypto_cert._x509
|
|
|
|
return cert
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def set_version(self, version):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Set the version number of the certificate.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param version: The version number of the certificate.
|
2016-02-06 09:36:57 +00:00
|
|
|
:type version: :py:class:`int`
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
if not isinstance(version, int):
|
|
|
|
raise TypeError("version must be an integer")
|
|
|
|
|
|
|
|
_lib.X509_set_version(self._x509, version)
|
|
|
|
|
|
|
|
def get_version(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Return the version number of the certificate.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The version number of the certificate.
|
|
|
|
:rtype: :py:class:`int`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
return _lib.X509_get_version(self._x509)
|
|
|
|
|
|
|
|
def get_pubkey(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Get the public key of the certificate.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The public key.
|
|
|
|
:rtype: :py:class:`PKey`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
pkey = PKey.__new__(PKey)
|
|
|
|
pkey._pkey = _lib.X509_get_pubkey(self._x509)
|
|
|
|
if pkey._pkey == _ffi.NULL:
|
|
|
|
_raise_current_error()
|
|
|
|
pkey._pkey = _ffi.gc(pkey._pkey, _lib.EVP_PKEY_free)
|
|
|
|
pkey._only_public = True
|
|
|
|
return pkey
|
|
|
|
|
|
|
|
def set_pubkey(self, pkey):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Set the public key of the certificate.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param pkey: The public key.
|
|
|
|
:type pkey: :py:class:`PKey`
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: :py:data:`None`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
if not isinstance(pkey, PKey):
|
|
|
|
raise TypeError("pkey must be a PKey instance")
|
|
|
|
|
|
|
|
set_result = _lib.X509_set_pubkey(self._x509, pkey._pkey)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(set_result == 1)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def sign(self, pkey, digest):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Sign the certificate with this key and digest type.
|
|
|
|
|
|
|
|
:param pkey: The key to sign with.
|
|
|
|
:type pkey: :py:class:`PKey`
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param digest: The name of the message digest to use.
|
|
|
|
:type digest: :py:class:`bytes`
|
|
|
|
|
|
|
|
:return: :py:data:`None`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
if not isinstance(pkey, PKey):
|
|
|
|
raise TypeError("pkey must be a PKey instance")
|
|
|
|
|
|
|
|
if pkey._only_public:
|
|
|
|
raise ValueError("Key only has public part")
|
|
|
|
|
|
|
|
if not pkey._initialized:
|
|
|
|
raise ValueError("Key is uninitialized")
|
|
|
|
|
|
|
|
evp_md = _lib.EVP_get_digestbyname(_byte_string(digest))
|
|
|
|
if evp_md == _ffi.NULL:
|
|
|
|
raise ValueError("No such digest method")
|
|
|
|
|
|
|
|
sign_result = _lib.X509_sign(self._x509, pkey._pkey, evp_md)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(sign_result > 0)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def get_signature_algorithm(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Return the signature algorithm used in the certificate.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The name of the algorithm.
|
|
|
|
:rtype: :py:class:`bytes`
|
|
|
|
|
|
|
|
:raises ValueError: If the signature algorithm is undefined.
|
|
|
|
|
|
|
|
.. versionadded:: 0.13
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
algor = _lib.X509_get0_tbs_sigalg(self._x509)
|
|
|
|
nid = _lib.OBJ_obj2nid(algor.algorithm)
|
2016-02-06 09:36:57 +00:00
|
|
|
if nid == _lib.NID_undef:
|
|
|
|
raise ValueError("Undefined signature algorithm")
|
|
|
|
return _ffi.string(_lib.OBJ_nid2ln(nid))
|
|
|
|
|
|
|
|
def digest(self, digest_name):
|
|
|
|
"""
|
|
|
|
Return the digest of the X509 object.
|
|
|
|
|
|
|
|
:param digest_name: The name of the digest algorithm to use.
|
|
|
|
:type digest_name: :py:class:`bytes`
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The digest of the object, formatted as
|
|
|
|
:py:const:`b":"`-delimited hex pairs.
|
|
|
|
:rtype: :py:class:`bytes`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
digest = _lib.EVP_get_digestbyname(_byte_string(digest_name))
|
|
|
|
if digest == _ffi.NULL:
|
|
|
|
raise ValueError("No such digest method")
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
result_buffer = _ffi.new("unsigned char[]", _lib.EVP_MAX_MD_SIZE)
|
2016-02-06 09:36:57 +00:00
|
|
|
result_length = _ffi.new("unsigned int[]", 1)
|
|
|
|
result_length[0] = len(result_buffer)
|
|
|
|
|
|
|
|
digest_result = _lib.X509_digest(
|
|
|
|
self._x509, digest, result_buffer, result_length)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(digest_result == 1)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
return b":".join([
|
2018-12-31 23:25:26 +00:00
|
|
|
b16encode(ch).upper() for ch
|
|
|
|
in _ffi.buffer(result_buffer, result_length[0])])
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def subject_name_hash(self):
|
|
|
|
"""
|
|
|
|
Return the hash of the X509 subject.
|
|
|
|
|
|
|
|
:return: The hash of the subject.
|
2018-12-31 23:25:26 +00:00
|
|
|
:rtype: :py:class:`bytes`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
return _lib.X509_subject_name_hash(self._x509)
|
|
|
|
|
|
|
|
def set_serial_number(self, serial):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Set the serial number of the certificate.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param serial: The new serial number.
|
2016-02-06 09:36:57 +00:00
|
|
|
:type serial: :py:class:`int`
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: :py:data`None`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
if not isinstance(serial, _integer_types):
|
|
|
|
raise TypeError("serial must be an integer")
|
|
|
|
|
|
|
|
hex_serial = hex(serial)[2:]
|
|
|
|
if not isinstance(hex_serial, bytes):
|
|
|
|
hex_serial = hex_serial.encode('ascii')
|
|
|
|
|
|
|
|
bignum_serial = _ffi.new("BIGNUM**")
|
|
|
|
|
|
|
|
# BN_hex2bn stores the result in &bignum. Unless it doesn't feel like
|
2018-12-31 23:25:26 +00:00
|
|
|
# it. If bignum is still NULL after this call, then the return value
|
|
|
|
# is actually the result. I hope. -exarkun
|
2016-02-06 09:36:57 +00:00
|
|
|
small_serial = _lib.BN_hex2bn(bignum_serial, hex_serial)
|
|
|
|
|
|
|
|
if bignum_serial[0] == _ffi.NULL:
|
|
|
|
set_result = _lib.ASN1_INTEGER_set(
|
|
|
|
_lib.X509_get_serialNumber(self._x509), small_serial)
|
|
|
|
if set_result:
|
|
|
|
# TODO Not tested
|
|
|
|
_raise_current_error()
|
|
|
|
else:
|
|
|
|
asn1_serial = _lib.BN_to_ASN1_INTEGER(bignum_serial[0], _ffi.NULL)
|
|
|
|
_lib.BN_free(bignum_serial[0])
|
|
|
|
if asn1_serial == _ffi.NULL:
|
|
|
|
# TODO Not tested
|
|
|
|
_raise_current_error()
|
|
|
|
asn1_serial = _ffi.gc(asn1_serial, _lib.ASN1_INTEGER_free)
|
|
|
|
set_result = _lib.X509_set_serialNumber(self._x509, asn1_serial)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(set_result == 1)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def get_serial_number(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Return the serial number of this certificate.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The serial number.
|
|
|
|
:rtype: int
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
asn1_serial = _lib.X509_get_serialNumber(self._x509)
|
|
|
|
bignum_serial = _lib.ASN1_INTEGER_to_BN(asn1_serial, _ffi.NULL)
|
|
|
|
try:
|
|
|
|
hex_serial = _lib.BN_bn2hex(bignum_serial)
|
|
|
|
try:
|
|
|
|
hexstring_serial = _ffi.string(hex_serial)
|
|
|
|
serial = int(hexstring_serial, 16)
|
|
|
|
return serial
|
|
|
|
finally:
|
|
|
|
_lib.OPENSSL_free(hex_serial)
|
|
|
|
finally:
|
|
|
|
_lib.BN_free(bignum_serial)
|
|
|
|
|
|
|
|
def gmtime_adj_notAfter(self, amount):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Adjust the time stamp on which the certificate stops being valid.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param int amount: The number of seconds by which to adjust the
|
|
|
|
timestamp.
|
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
if not isinstance(amount, int):
|
|
|
|
raise TypeError("amount must be an integer")
|
|
|
|
|
|
|
|
notAfter = _lib.X509_get_notAfter(self._x509)
|
|
|
|
_lib.X509_gmtime_adj(notAfter, amount)
|
|
|
|
|
|
|
|
def gmtime_adj_notBefore(self, amount):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Adjust the timestamp on which the certificate starts being valid.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param amount: The number of seconds by which to adjust the timestamp.
|
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
if not isinstance(amount, int):
|
|
|
|
raise TypeError("amount must be an integer")
|
|
|
|
|
|
|
|
notBefore = _lib.X509_get_notBefore(self._x509)
|
|
|
|
_lib.X509_gmtime_adj(notBefore, amount)
|
|
|
|
|
|
|
|
def has_expired(self):
|
|
|
|
"""
|
|
|
|
Check whether the certificate has expired.
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: ``True`` if the certificate has expired, ``False`` otherwise.
|
|
|
|
:rtype: bool
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
time_string = _native(self.get_notAfter())
|
|
|
|
not_after = datetime.datetime.strptime(time_string, "%Y%m%d%H%M%SZ")
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
return not_after < datetime.datetime.utcnow()
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def _get_boundary_time(self, which):
|
|
|
|
return _get_asn1_time(which(self._x509))
|
|
|
|
|
|
|
|
def get_notBefore(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Get the timestamp at which the certificate starts being valid.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
The timestamp is formatted as an ASN.1 TIME::
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
YYYYMMDDhhmmssZ
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: A timestamp string, or ``None`` if there is none.
|
|
|
|
:rtype: bytes or NoneType
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
return self._get_boundary_time(_lib.X509_get_notBefore)
|
|
|
|
|
|
|
|
def _set_boundary_time(self, which, when):
|
|
|
|
return _set_asn1_time(which(self._x509), when)
|
|
|
|
|
|
|
|
def set_notBefore(self, when):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Set the timestamp at which the certificate starts being valid.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
The timestamp is formatted as an ASN.1 TIME::
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
YYYYMMDDhhmmssZ
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param bytes when: A timestamp string.
|
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
return self._set_boundary_time(_lib.X509_get_notBefore, when)
|
|
|
|
|
|
|
|
def get_notAfter(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Get the timestamp at which the certificate stops being valid.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
The timestamp is formatted as an ASN.1 TIME::
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
YYYYMMDDhhmmssZ
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: A timestamp string, or ``None`` if there is none.
|
|
|
|
:rtype: bytes or NoneType
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
return self._get_boundary_time(_lib.X509_get_notAfter)
|
|
|
|
|
|
|
|
def set_notAfter(self, when):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Set the timestamp at which the certificate stops being valid.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
The timestamp is formatted as an ASN.1 TIME::
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
YYYYMMDDhhmmssZ
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param bytes when: A timestamp string.
|
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
return self._set_boundary_time(_lib.X509_get_notAfter, when)
|
|
|
|
|
|
|
|
def _get_name(self, which):
|
|
|
|
name = X509Name.__new__(X509Name)
|
|
|
|
name._name = which(self._x509)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(name._name != _ffi.NULL)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
# The name is owned by the X509 structure. As long as the X509Name
|
|
|
|
# Python object is alive, keep the X509 Python object alive.
|
|
|
|
name._owner = self
|
|
|
|
|
|
|
|
return name
|
|
|
|
|
|
|
|
def _set_name(self, which, name):
|
|
|
|
if not isinstance(name, X509Name):
|
|
|
|
raise TypeError("name must be an X509Name")
|
|
|
|
set_result = which(self._x509, name._name)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(set_result == 1)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def get_issuer(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Return the issuer of this certificate.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
This creates a new :class:`X509Name` that wraps the underlying issuer
|
|
|
|
name field on the certificate. Modifying it will modify the underlying
|
|
|
|
certificate, and will have the effect of modifying any other
|
|
|
|
:class:`X509Name` that refers to this issuer.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The issuer of this certificate.
|
|
|
|
:rtype: :class:`X509Name`
|
|
|
|
"""
|
|
|
|
name = self._get_name(_lib.X509_get_issuer_name)
|
|
|
|
self._issuer_invalidator.add(name)
|
|
|
|
return name
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def set_issuer(self, issuer):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Set the issuer of this certificate.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param issuer: The issuer.
|
2016-02-06 09:36:57 +00:00
|
|
|
:type issuer: :py:class:`X509Name`
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
self._set_name(_lib.X509_set_issuer_name, issuer)
|
|
|
|
self._issuer_invalidator.clear()
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def get_subject(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Return the subject of this certificate.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
This creates a new :class:`X509Name` that wraps the underlying subject
|
|
|
|
name field on the certificate. Modifying it will modify the underlying
|
|
|
|
certificate, and will have the effect of modifying any other
|
|
|
|
:class:`X509Name` that refers to this subject.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The subject of this certificate.
|
|
|
|
:rtype: :class:`X509Name`
|
|
|
|
"""
|
|
|
|
name = self._get_name(_lib.X509_get_subject_name)
|
|
|
|
self._subject_invalidator.add(name)
|
|
|
|
return name
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def set_subject(self, subject):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Set the subject of this certificate.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param subject: The subject.
|
2016-02-06 09:36:57 +00:00
|
|
|
:type subject: :py:class:`X509Name`
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: ``None``
|
|
|
|
"""
|
|
|
|
self._set_name(_lib.X509_set_subject_name, subject)
|
|
|
|
self._subject_invalidator.clear()
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def get_extension_count(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Get the number of extensions on this certificate.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The number of extensions.
|
|
|
|
:rtype: :py:class:`int`
|
|
|
|
|
|
|
|
.. versionadded:: 0.12
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
return _lib.X509_get_ext_count(self._x509)
|
|
|
|
|
|
|
|
def add_extensions(self, extensions):
|
|
|
|
"""
|
|
|
|
Add extensions to the certificate.
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param extensions: The extensions to add.
|
|
|
|
:type extensions: An iterable of :py:class:`X509Extension` objects.
|
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
for ext in extensions:
|
|
|
|
if not isinstance(ext, X509Extension):
|
|
|
|
raise ValueError("One of the elements is not an X509Extension")
|
|
|
|
|
|
|
|
add_result = _lib.X509_add_ext(self._x509, ext._extension, -1)
|
|
|
|
if not add_result:
|
|
|
|
_raise_current_error()
|
|
|
|
|
|
|
|
def get_extension(self, index):
|
|
|
|
"""
|
|
|
|
Get a specific extension of the certificate by index.
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
Extensions on a certificate are kept in order. The index
|
|
|
|
parameter selects which extension will be returned.
|
|
|
|
|
|
|
|
:param int index: The index of the extension to retrieve.
|
|
|
|
:return: The extension at the specified index.
|
|
|
|
:rtype: :py:class:`X509Extension`
|
|
|
|
:raises IndexError: If the extension index was out of bounds.
|
|
|
|
|
|
|
|
.. versionadded:: 0.12
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
ext = X509Extension.__new__(X509Extension)
|
|
|
|
ext._extension = _lib.X509_get_ext(self._x509, index)
|
|
|
|
if ext._extension == _ffi.NULL:
|
|
|
|
raise IndexError("extension index out of bounds")
|
|
|
|
|
|
|
|
extension = _lib.X509_EXTENSION_dup(ext._extension)
|
|
|
|
ext._extension = _ffi.gc(extension, _lib.X509_EXTENSION_free)
|
|
|
|
return ext
|
|
|
|
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
X509Type = deprecated(
|
|
|
|
X509, __name__,
|
|
|
|
"X509Type has been deprecated, use X509 instead",
|
|
|
|
DeprecationWarning
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class X509StoreFlags(object):
|
|
|
|
"""
|
|
|
|
Flags for X509 verification, used to change the behavior of
|
|
|
|
:class:`X509Store`.
|
|
|
|
|
|
|
|
See `OpenSSL Verification Flags`_ for details.
|
|
|
|
|
|
|
|
.. _OpenSSL Verification Flags:
|
|
|
|
https://www.openssl.org/docs/manmaster/man3/X509_VERIFY_PARAM_set_flags.html
|
|
|
|
"""
|
|
|
|
CRL_CHECK = _lib.X509_V_FLAG_CRL_CHECK
|
|
|
|
CRL_CHECK_ALL = _lib.X509_V_FLAG_CRL_CHECK_ALL
|
|
|
|
IGNORE_CRITICAL = _lib.X509_V_FLAG_IGNORE_CRITICAL
|
|
|
|
X509_STRICT = _lib.X509_V_FLAG_X509_STRICT
|
|
|
|
ALLOW_PROXY_CERTS = _lib.X509_V_FLAG_ALLOW_PROXY_CERTS
|
|
|
|
POLICY_CHECK = _lib.X509_V_FLAG_POLICY_CHECK
|
|
|
|
EXPLICIT_POLICY = _lib.X509_V_FLAG_EXPLICIT_POLICY
|
|
|
|
INHIBIT_MAP = _lib.X509_V_FLAG_INHIBIT_MAP
|
|
|
|
NOTIFY_POLICY = _lib.X509_V_FLAG_NOTIFY_POLICY
|
|
|
|
CHECK_SS_SIGNATURE = _lib.X509_V_FLAG_CHECK_SS_SIGNATURE
|
|
|
|
CB_ISSUER_CHECK = _lib.X509_V_FLAG_CB_ISSUER_CHECK
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class X509Store(object):
|
2018-12-31 23:25:26 +00:00
|
|
|
"""
|
|
|
|
An X.509 store.
|
|
|
|
|
|
|
|
An X.509 store is used to describe a context in which to verify a
|
|
|
|
certificate. A description of a context may include a set of certificates
|
|
|
|
to trust, a set of certificate revocation lists, verification flags and
|
|
|
|
more.
|
|
|
|
|
|
|
|
An X.509 store, being only a description, cannot be used by itself to
|
|
|
|
verify a certificate. To carry out the actual verification process, see
|
|
|
|
:class:`X509StoreContext`.
|
|
|
|
"""
|
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
def __init__(self):
|
|
|
|
store = _lib.X509_STORE_new()
|
|
|
|
self._store = _ffi.gc(store, _lib.X509_STORE_free)
|
|
|
|
|
|
|
|
def add_cert(self, cert):
|
2018-12-31 23:25:26 +00:00
|
|
|
"""
|
|
|
|
Adds a trusted certificate to this store.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
Adding a certificate with this method adds this certificate as a
|
|
|
|
*trusted* certificate.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param X509 cert: The certificate to add to this store.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:raises TypeError: If the certificate is not an :class:`X509`.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:raises OpenSSL.crypto.Error: If OpenSSL was unhappy with your
|
|
|
|
certificate.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: ``None`` if the certificate was added successfully.
|
|
|
|
"""
|
|
|
|
if not isinstance(cert, X509):
|
|
|
|
raise TypeError()
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(_lib.X509_STORE_add_cert(self._store, cert._x509) != 0)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
def add_crl(self, crl):
|
|
|
|
"""
|
|
|
|
Add a certificate revocation list to this store.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
The certificate revocation lists added to a store will only be used if
|
|
|
|
the associated flags are configured to check certificate revocation
|
|
|
|
lists.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
.. versionadded:: 16.1.0
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param CRL crl: The certificate revocation list to add to this store.
|
|
|
|
:return: ``None`` if the certificate revocation list was added
|
|
|
|
successfully.
|
|
|
|
"""
|
|
|
|
_openssl_assert(_lib.X509_STORE_add_crl(self._store, crl._crl) != 0)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
def set_flags(self, flags):
|
|
|
|
"""
|
|
|
|
Set verification flags to this store.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
Verification flags can be combined by oring them together.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
.. note::
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
Setting a verification flag sometimes requires clients to add
|
|
|
|
additional information to the store, otherwise a suitable error will
|
|
|
|
be raised.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
For example, in setting flags to enable CRL checking a
|
|
|
|
suitable CRL must be added to the store otherwise an error will be
|
|
|
|
raised.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
.. versionadded:: 16.1.0
|
|
|
|
|
|
|
|
:param int flags: The verification flags to set on this store.
|
|
|
|
See :class:`X509StoreFlags` for available constants.
|
|
|
|
:return: ``None`` if the verification flags were successfully set.
|
|
|
|
"""
|
|
|
|
_openssl_assert(_lib.X509_STORE_set_flags(self._store, flags) != 0)
|
|
|
|
|
|
|
|
def set_time(self, vfy_time):
|
|
|
|
"""
|
|
|
|
Set the time against which the certificates are verified.
|
|
|
|
|
|
|
|
Normally the current time is used.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
For example, you can determine if a certificate was valid at a given
|
|
|
|
time.
|
|
|
|
|
|
|
|
.. versionadded:: 17.0.0
|
|
|
|
|
|
|
|
:param datetime vfy_time: The verification time to set on this store.
|
|
|
|
:return: ``None`` if the verification time was successfully set.
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
param = _lib.X509_VERIFY_PARAM_new()
|
|
|
|
param = _ffi.gc(param, _lib.X509_VERIFY_PARAM_free)
|
|
|
|
|
|
|
|
_lib.X509_VERIFY_PARAM_set_time(param, int(vfy_time.strftime('%s')))
|
|
|
|
_openssl_assert(_lib.X509_STORE_set1_param(self._store, param) != 0)
|
|
|
|
|
|
|
|
|
|
|
|
X509StoreType = deprecated(
|
|
|
|
X509Store, __name__,
|
|
|
|
"X509StoreType has been deprecated, use X509Store instead",
|
|
|
|
DeprecationWarning
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class X509StoreContextError(Exception):
|
|
|
|
"""
|
|
|
|
An exception raised when an error occurred while verifying a certificate
|
|
|
|
using `OpenSSL.X509StoreContext.verify_certificate`.
|
|
|
|
|
|
|
|
:ivar certificate: The certificate which caused verificate failure.
|
|
|
|
:type certificate: :class:`X509`
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, message, certificate):
|
|
|
|
super(X509StoreContextError, self).__init__(message)
|
|
|
|
self.certificate = certificate
|
|
|
|
|
|
|
|
|
|
|
|
class X509StoreContext(object):
|
|
|
|
"""
|
|
|
|
An X.509 store context.
|
|
|
|
|
|
|
|
An X.509 store context is used to carry out the actual verification process
|
|
|
|
of a certificate in a described context. For describing such a context, see
|
|
|
|
:class:`X509Store`.
|
|
|
|
|
|
|
|
:ivar _store_ctx: The underlying X509_STORE_CTX structure used by this
|
|
|
|
instance. It is dynamically allocated and automatically garbage
|
|
|
|
collected.
|
|
|
|
:ivar _store: See the ``store`` ``__init__`` parameter.
|
|
|
|
:ivar _cert: See the ``certificate`` ``__init__`` parameter.
|
|
|
|
:param X509Store store: The certificates which will be trusted for the
|
|
|
|
purposes of any verifications.
|
|
|
|
:param X509 certificate: The certificate to be verified.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, store, certificate):
|
2016-02-06 09:36:57 +00:00
|
|
|
store_ctx = _lib.X509_STORE_CTX_new()
|
|
|
|
self._store_ctx = _ffi.gc(store_ctx, _lib.X509_STORE_CTX_free)
|
|
|
|
self._store = store
|
|
|
|
self._cert = certificate
|
|
|
|
# Make the store context available for use after instantiating this
|
|
|
|
# class by initializing it now. Per testing, subsequent calls to
|
2018-12-31 23:25:26 +00:00
|
|
|
# :meth:`_init` have no adverse affect.
|
2016-02-06 09:36:57 +00:00
|
|
|
self._init()
|
|
|
|
|
|
|
|
def _init(self):
|
|
|
|
"""
|
|
|
|
Set up the store context for a subsequent verification operation.
|
2018-12-31 23:25:26 +00:00
|
|
|
|
|
|
|
Calling this method more than once without first calling
|
|
|
|
:meth:`_cleanup` will leak memory.
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
ret = _lib.X509_STORE_CTX_init(
|
|
|
|
self._store_ctx, self._store._store, self._cert._x509, _ffi.NULL
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
if ret <= 0:
|
|
|
|
_raise_current_error()
|
|
|
|
|
|
|
|
def _cleanup(self):
|
|
|
|
"""
|
|
|
|
Internally cleans up the store context.
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
The store context can then be reused with a new call to :meth:`_init`.
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
_lib.X509_STORE_CTX_cleanup(self._store_ctx)
|
|
|
|
|
|
|
|
def _exception_from_context(self):
|
|
|
|
"""
|
|
|
|
Convert an OpenSSL native context error failure into a Python
|
|
|
|
exception.
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
When a call to native OpenSSL X509_verify_cert fails, additional
|
|
|
|
information about the failure can be obtained from the store context.
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
errors = [
|
|
|
|
_lib.X509_STORE_CTX_get_error(self._store_ctx),
|
|
|
|
_lib.X509_STORE_CTX_get_error_depth(self._store_ctx),
|
|
|
|
_native(_ffi.string(_lib.X509_verify_cert_error_string(
|
2018-12-31 23:25:26 +00:00
|
|
|
_lib.X509_STORE_CTX_get_error(self._store_ctx)))),
|
2016-02-06 09:36:57 +00:00
|
|
|
]
|
|
|
|
# A context error should always be associated with a certificate, so we
|
|
|
|
# expect this call to never return :class:`None`.
|
|
|
|
_x509 = _lib.X509_STORE_CTX_get_current_cert(self._store_ctx)
|
|
|
|
_cert = _lib.X509_dup(_x509)
|
2018-12-31 23:25:26 +00:00
|
|
|
pycert = X509._from_raw_x509_ptr(_cert)
|
2016-02-06 09:36:57 +00:00
|
|
|
return X509StoreContextError(errors, pycert)
|
|
|
|
|
|
|
|
def set_store(self, store):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Set the context's X.509 store.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
.. versionadded:: 0.15
|
|
|
|
|
|
|
|
:param X509Store store: The store description which will be used for
|
|
|
|
the purposes of any *future* verifications.
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
self._store = store
|
|
|
|
|
|
|
|
def verify_certificate(self):
|
|
|
|
"""
|
|
|
|
Verify a certificate in a context.
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
.. versionadded:: 0.15
|
|
|
|
|
|
|
|
:raises X509StoreContextError: If an error occurred when validating a
|
|
|
|
certificate in the context. Sets ``certificate`` attribute to
|
|
|
|
indicate which certificate caused the error.
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
# Always re-initialize the store context in case
|
2018-12-31 23:25:26 +00:00
|
|
|
# :meth:`verify_certificate` is called multiple times.
|
|
|
|
#
|
|
|
|
# :meth:`_init` is called in :meth:`__init__` so _cleanup is called
|
|
|
|
# before _init to ensure memory is not leaked.
|
|
|
|
self._cleanup()
|
2016-02-06 09:36:57 +00:00
|
|
|
self._init()
|
|
|
|
ret = _lib.X509_verify_cert(self._store_ctx)
|
|
|
|
self._cleanup()
|
|
|
|
if ret <= 0:
|
|
|
|
raise self._exception_from_context()
|
|
|
|
|
|
|
|
|
|
|
|
def load_certificate(type, buffer):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Load a certificate (X509) from the string *buffer* encoded with the
|
|
|
|
type *type*.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
:param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1)
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param bytes buffer: The buffer the certificate is stored in
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
:return: The X509 object
|
|
|
|
"""
|
|
|
|
if isinstance(buffer, _text_type):
|
|
|
|
buffer = buffer.encode("ascii")
|
|
|
|
|
|
|
|
bio = _new_mem_buf(buffer)
|
|
|
|
|
|
|
|
if type == FILETYPE_PEM:
|
|
|
|
x509 = _lib.PEM_read_bio_X509(bio, _ffi.NULL, _ffi.NULL, _ffi.NULL)
|
|
|
|
elif type == FILETYPE_ASN1:
|
2018-12-31 23:25:26 +00:00
|
|
|
x509 = _lib.d2i_X509_bio(bio, _ffi.NULL)
|
2016-02-06 09:36:57 +00:00
|
|
|
else:
|
|
|
|
raise ValueError(
|
|
|
|
"type argument must be FILETYPE_PEM or FILETYPE_ASN1")
|
|
|
|
|
|
|
|
if x509 == _ffi.NULL:
|
|
|
|
_raise_current_error()
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
return X509._from_raw_x509_ptr(x509)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def dump_certificate(type, cert):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Dump the certificate *cert* into a buffer string encoded with the type
|
|
|
|
*type*.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
:param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1, or
|
|
|
|
FILETYPE_TEXT)
|
|
|
|
:param cert: The certificate to dump
|
|
|
|
:return: The buffer with the dumped certificate in
|
|
|
|
"""
|
|
|
|
bio = _new_mem_buf()
|
|
|
|
|
|
|
|
if type == FILETYPE_PEM:
|
|
|
|
result_code = _lib.PEM_write_bio_X509(bio, cert._x509)
|
|
|
|
elif type == FILETYPE_ASN1:
|
|
|
|
result_code = _lib.i2d_X509_bio(bio, cert._x509)
|
|
|
|
elif type == FILETYPE_TEXT:
|
|
|
|
result_code = _lib.X509_print_ex(bio, cert._x509, 0, 0)
|
|
|
|
else:
|
|
|
|
raise ValueError(
|
|
|
|
"type argument must be FILETYPE_PEM, FILETYPE_ASN1, or "
|
|
|
|
"FILETYPE_TEXT")
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
assert result_code == 1
|
2016-02-06 09:36:57 +00:00
|
|
|
return _bio_to_string(bio)
|
|
|
|
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
def dump_publickey(type, pkey):
|
|
|
|
"""
|
|
|
|
Dump a public key to a buffer.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param type: The file type (one of :data:`FILETYPE_PEM` or
|
|
|
|
:data:`FILETYPE_ASN1`).
|
|
|
|
:param PKey pkey: The public key to dump
|
|
|
|
:return: The buffer with the dumped key in it.
|
|
|
|
:rtype: bytes
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
bio = _new_mem_buf()
|
|
|
|
if type == FILETYPE_PEM:
|
|
|
|
write_bio = _lib.PEM_write_bio_PUBKEY
|
|
|
|
elif type == FILETYPE_ASN1:
|
|
|
|
write_bio = _lib.i2d_PUBKEY_bio
|
|
|
|
else:
|
|
|
|
raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1")
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
result_code = write_bio(bio, pkey._pkey)
|
|
|
|
if result_code != 1: # pragma: no cover
|
|
|
|
_raise_current_error()
|
|
|
|
|
|
|
|
return _bio_to_string(bio)
|
|
|
|
|
|
|
|
|
|
|
|
def dump_privatekey(type, pkey, cipher=None, passphrase=None):
|
|
|
|
"""
|
|
|
|
Dump the private key *pkey* into a buffer string encoded with the type
|
|
|
|
*type*. Optionally (if *type* is :const:`FILETYPE_PEM`) encrypting it
|
|
|
|
using *cipher* and *passphrase*.
|
|
|
|
|
|
|
|
:param type: The file type (one of :const:`FILETYPE_PEM`,
|
|
|
|
:const:`FILETYPE_ASN1`, or :const:`FILETYPE_TEXT`)
|
|
|
|
:param PKey pkey: The PKey to dump
|
|
|
|
:param cipher: (optional) if encrypted PEM format, the cipher to use
|
2016-02-06 09:36:57 +00:00
|
|
|
:param passphrase: (optional) if encrypted PEM format, this can be either
|
2018-12-31 23:25:26 +00:00
|
|
|
the passphrase to use, or a callback for providing the passphrase.
|
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
:return: The buffer with the dumped key in
|
2018-12-31 23:25:26 +00:00
|
|
|
:rtype: bytes
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
bio = _new_mem_buf()
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
if not isinstance(pkey, PKey):
|
|
|
|
raise TypeError("pkey must be a PKey")
|
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
if cipher is not None:
|
|
|
|
if passphrase is None:
|
|
|
|
raise TypeError(
|
|
|
|
"if a value is given for cipher "
|
|
|
|
"one must also be given for passphrase")
|
|
|
|
cipher_obj = _lib.EVP_get_cipherbyname(_byte_string(cipher))
|
|
|
|
if cipher_obj == _ffi.NULL:
|
|
|
|
raise ValueError("Invalid cipher name")
|
|
|
|
else:
|
|
|
|
cipher_obj = _ffi.NULL
|
|
|
|
|
|
|
|
helper = _PassphraseHelper(type, passphrase)
|
|
|
|
if type == FILETYPE_PEM:
|
|
|
|
result_code = _lib.PEM_write_bio_PrivateKey(
|
|
|
|
bio, pkey._pkey, cipher_obj, _ffi.NULL, 0,
|
|
|
|
helper.callback, helper.callback_args)
|
|
|
|
helper.raise_if_problem()
|
|
|
|
elif type == FILETYPE_ASN1:
|
|
|
|
result_code = _lib.i2d_PrivateKey_bio(bio, pkey._pkey)
|
|
|
|
elif type == FILETYPE_TEXT:
|
2018-12-31 23:25:26 +00:00
|
|
|
if _lib.EVP_PKEY_id(pkey._pkey) != _lib.EVP_PKEY_RSA:
|
|
|
|
raise TypeError("Only RSA keys are supported for FILETYPE_TEXT")
|
|
|
|
|
|
|
|
rsa = _ffi.gc(
|
|
|
|
_lib.EVP_PKEY_get1_RSA(pkey._pkey),
|
|
|
|
_lib.RSA_free
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
result_code = _lib.RSA_print(bio, rsa, 0)
|
|
|
|
else:
|
|
|
|
raise ValueError(
|
|
|
|
"type argument must be FILETYPE_PEM, FILETYPE_ASN1, or "
|
|
|
|
"FILETYPE_TEXT")
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(result_code != 0)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
return _bio_to_string(bio)
|
|
|
|
|
|
|
|
|
|
|
|
class Revoked(object):
|
2018-12-31 23:25:26 +00:00
|
|
|
"""
|
|
|
|
A certificate revocation.
|
|
|
|
"""
|
2016-02-06 09:36:57 +00:00
|
|
|
# http://www.openssl.org/docs/apps/x509v3_config.html#CRL_distribution_points_
|
|
|
|
# which differs from crl_reasons of crypto/x509v3/v3_enum.c that matches
|
|
|
|
# OCSP_crl_reason_str. We use the latter, just like the command line
|
|
|
|
# program.
|
|
|
|
_crl_reasons = [
|
|
|
|
b"unspecified",
|
|
|
|
b"keyCompromise",
|
|
|
|
b"CACompromise",
|
|
|
|
b"affiliationChanged",
|
|
|
|
b"superseded",
|
|
|
|
b"cessationOfOperation",
|
|
|
|
b"certificateHold",
|
|
|
|
# b"removeFromCRL",
|
2018-12-31 23:25:26 +00:00
|
|
|
]
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
revoked = _lib.X509_REVOKED_new()
|
|
|
|
self._revoked = _ffi.gc(revoked, _lib.X509_REVOKED_free)
|
|
|
|
|
|
|
|
def set_serial(self, hex_str):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Set the serial number.
|
|
|
|
|
|
|
|
The serial number is formatted as a hexadecimal number encoded in
|
|
|
|
ASCII.
|
|
|
|
|
|
|
|
:param bytes hex_str: The new serial number.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
bignum_serial = _ffi.gc(_lib.BN_new(), _lib.BN_free)
|
|
|
|
bignum_ptr = _ffi.new("BIGNUM**")
|
|
|
|
bignum_ptr[0] = bignum_serial
|
|
|
|
bn_result = _lib.BN_hex2bn(bignum_ptr, hex_str)
|
|
|
|
if not bn_result:
|
|
|
|
raise ValueError("bad hex string")
|
|
|
|
|
|
|
|
asn1_serial = _ffi.gc(
|
|
|
|
_lib.BN_to_ASN1_INTEGER(bignum_serial, _ffi.NULL),
|
|
|
|
_lib.ASN1_INTEGER_free)
|
|
|
|
_lib.X509_REVOKED_set_serialNumber(self._revoked, asn1_serial)
|
|
|
|
|
|
|
|
def get_serial(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Get the serial number.
|
|
|
|
|
|
|
|
The serial number is formatted as a hexadecimal number encoded in
|
|
|
|
ASCII.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The serial number.
|
|
|
|
:rtype: bytes
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
bio = _new_mem_buf()
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
asn1_int = _lib.X509_REVOKED_get0_serialNumber(self._revoked)
|
|
|
|
_openssl_assert(asn1_int != _ffi.NULL)
|
|
|
|
result = _lib.i2a_ASN1_INTEGER(bio, asn1_int)
|
|
|
|
_openssl_assert(result >= 0)
|
2016-02-06 09:36:57 +00:00
|
|
|
return _bio_to_string(bio)
|
|
|
|
|
|
|
|
def _delete_reason(self):
|
2018-12-31 23:25:26 +00:00
|
|
|
for i in range(_lib.X509_REVOKED_get_ext_count(self._revoked)):
|
|
|
|
ext = _lib.X509_REVOKED_get_ext(self._revoked, i)
|
|
|
|
obj = _lib.X509_EXTENSION_get_object(ext)
|
|
|
|
if _lib.OBJ_obj2nid(obj) == _lib.NID_crl_reason:
|
2016-02-06 09:36:57 +00:00
|
|
|
_lib.X509_EXTENSION_free(ext)
|
2018-12-31 23:25:26 +00:00
|
|
|
_lib.X509_REVOKED_delete_ext(self._revoked, i)
|
2016-02-06 09:36:57 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
def set_reason(self, reason):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Set the reason of this revocation.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
If :data:`reason` is ``None``, delete the reason instead.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
:param reason: The reason string.
|
2018-12-31 23:25:26 +00:00
|
|
|
:type reason: :class:`bytes` or :class:`NoneType`
|
|
|
|
|
|
|
|
:return: ``None``
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
:meth:`all_reasons`, which gives you a list of all supported
|
|
|
|
reasons which you might pass to this method.
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
if reason is None:
|
|
|
|
self._delete_reason()
|
|
|
|
elif not isinstance(reason, bytes):
|
|
|
|
raise TypeError("reason must be None or a byte string")
|
|
|
|
else:
|
|
|
|
reason = reason.lower().replace(b' ', b'')
|
|
|
|
reason_code = [r.lower() for r in self._crl_reasons].index(reason)
|
|
|
|
|
|
|
|
new_reason_ext = _lib.ASN1_ENUMERATED_new()
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(new_reason_ext != _ffi.NULL)
|
2016-02-06 09:36:57 +00:00
|
|
|
new_reason_ext = _ffi.gc(new_reason_ext, _lib.ASN1_ENUMERATED_free)
|
|
|
|
|
|
|
|
set_result = _lib.ASN1_ENUMERATED_set(new_reason_ext, reason_code)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(set_result != _ffi.NULL)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
self._delete_reason()
|
|
|
|
add_result = _lib.X509_REVOKED_add1_ext_i2d(
|
|
|
|
self._revoked, _lib.NID_crl_reason, new_reason_ext, 0, 0)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(add_result == 1)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def get_reason(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Get the reason of this revocation.
|
|
|
|
|
|
|
|
:return: The reason, or ``None`` if there is none.
|
|
|
|
:rtype: bytes or NoneType
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
:meth:`all_reasons`, which gives you a list of all supported
|
|
|
|
reasons this method might return.
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
for i in range(_lib.X509_REVOKED_get_ext_count(self._revoked)):
|
|
|
|
ext = _lib.X509_REVOKED_get_ext(self._revoked, i)
|
|
|
|
obj = _lib.X509_EXTENSION_get_object(ext)
|
|
|
|
if _lib.OBJ_obj2nid(obj) == _lib.NID_crl_reason:
|
2016-02-06 09:36:57 +00:00
|
|
|
bio = _new_mem_buf()
|
|
|
|
|
|
|
|
print_result = _lib.X509V3_EXT_print(bio, ext, 0, 0)
|
|
|
|
if not print_result:
|
2018-12-31 23:25:26 +00:00
|
|
|
print_result = _lib.M_ASN1_OCTET_STRING_print(
|
|
|
|
bio, _lib.X509_EXTENSION_get_data(ext)
|
|
|
|
)
|
|
|
|
_openssl_assert(print_result != 0)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
return _bio_to_string(bio)
|
|
|
|
|
|
|
|
def all_reasons(self):
|
|
|
|
"""
|
|
|
|
Return a list of all the supported reason strings.
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
This list is a copy; modifying it does not change the supported reason
|
|
|
|
strings.
|
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
:return: A list of reason strings.
|
2018-12-31 23:25:26 +00:00
|
|
|
:rtype: :class:`list` of :class:`bytes`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
return self._crl_reasons[:]
|
|
|
|
|
|
|
|
def set_rev_date(self, when):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Set the revocation timestamp.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param bytes when: The timestamp of the revocation,
|
|
|
|
as ASN.1 TIME.
|
|
|
|
:return: ``None``
|
|
|
|
"""
|
|
|
|
dt = _lib.X509_REVOKED_get0_revocationDate(self._revoked)
|
|
|
|
return _set_asn1_time(dt, when)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
def get_rev_date(self):
|
|
|
|
"""
|
|
|
|
Get the revocation timestamp.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The timestamp of the revocation, as ASN.1 TIME.
|
|
|
|
:rtype: bytes
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
dt = _lib.X509_REVOKED_get0_revocationDate(self._revoked)
|
|
|
|
return _get_asn1_time(dt)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
class CRL(object):
|
|
|
|
"""
|
|
|
|
A certificate revocation list.
|
|
|
|
"""
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
def __init__(self):
|
|
|
|
crl = _lib.X509_CRL_new()
|
|
|
|
self._crl = _ffi.gc(crl, _lib.X509_CRL_free)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
def to_cryptography(self):
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Export as a ``cryptography`` CRL.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:rtype: ``cryptography.x509.CertificateRevocationList``
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
.. versionadded:: 17.1.0
|
|
|
|
"""
|
|
|
|
from cryptography.hazmat.backends.openssl.x509 import (
|
|
|
|
_CertificateRevocationList
|
|
|
|
)
|
|
|
|
backend = _get_backend()
|
|
|
|
return _CertificateRevocationList(backend, self._crl)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
@classmethod
|
|
|
|
def from_cryptography(cls, crypto_crl):
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Construct based on a ``cryptography`` *crypto_crl*.
|
|
|
|
|
|
|
|
:param crypto_crl: A ``cryptography`` certificate revocation list
|
|
|
|
:type crypto_crl: ``cryptography.x509.CertificateRevocationList``
|
|
|
|
|
|
|
|
:rtype: CRL
|
|
|
|
|
|
|
|
.. versionadded:: 17.1.0
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
if not isinstance(crypto_crl, x509.CertificateRevocationList):
|
|
|
|
raise TypeError("Must be a certificate revocation list")
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
crl = cls()
|
|
|
|
crl._crl = crypto_crl._x509_crl
|
|
|
|
return crl
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def get_revoked(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Return the revocations in this certificate revocation list.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
These revocations will be provided by value, not by reference.
|
|
|
|
That means it's okay to mutate them: it won't affect this CRL.
|
|
|
|
|
|
|
|
:return: The revocations in this CRL.
|
|
|
|
:rtype: :class:`tuple` of :class:`Revocation`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
results = []
|
2018-12-31 23:25:26 +00:00
|
|
|
revoked_stack = _lib.X509_CRL_get_REVOKED(self._crl)
|
2016-02-06 09:36:57 +00:00
|
|
|
for i in range(_lib.sk_X509_REVOKED_num(revoked_stack)):
|
|
|
|
revoked = _lib.sk_X509_REVOKED_value(revoked_stack, i)
|
2018-12-31 23:25:26 +00:00
|
|
|
revoked_copy = _lib.Cryptography_X509_REVOKED_dup(revoked)
|
2016-02-06 09:36:57 +00:00
|
|
|
pyrev = Revoked.__new__(Revoked)
|
|
|
|
pyrev._revoked = _ffi.gc(revoked_copy, _lib.X509_REVOKED_free)
|
|
|
|
results.append(pyrev)
|
|
|
|
if results:
|
|
|
|
return tuple(results)
|
|
|
|
|
|
|
|
def add_revoked(self, revoked):
|
|
|
|
"""
|
|
|
|
Add a revoked (by value not reference) to the CRL structure
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
This revocation will be added by value, not by reference. That
|
|
|
|
means it's okay to mutate it after adding: it won't affect
|
|
|
|
this CRL.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param Revoked revoked: The new revocation.
|
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
copy = _lib.Cryptography_X509_REVOKED_dup(revoked._revoked)
|
|
|
|
_openssl_assert(copy != _ffi.NULL)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
add_result = _lib.X509_CRL_add0_revoked(self._crl, copy)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(add_result != 0)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
def get_issuer(self):
|
|
|
|
"""
|
|
|
|
Get the CRL's issuer.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
.. versionadded:: 16.1.0
|
|
|
|
|
|
|
|
:rtype: X509Name
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
_issuer = _lib.X509_NAME_dup(_lib.X509_CRL_get_issuer(self._crl))
|
|
|
|
_openssl_assert(_issuer != _ffi.NULL)
|
|
|
|
_issuer = _ffi.gc(_issuer, _lib.X509_NAME_free)
|
|
|
|
issuer = X509Name.__new__(X509Name)
|
|
|
|
issuer._name = _issuer
|
|
|
|
return issuer
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
def set_version(self, version):
|
|
|
|
"""
|
|
|
|
Set the CRL version.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
.. versionadded:: 16.1.0
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param int version: The version of the CRL.
|
|
|
|
:return: ``None``
|
|
|
|
"""
|
|
|
|
_openssl_assert(_lib.X509_CRL_set_version(self._crl, version) != 0)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
def _set_boundary_time(self, which, when):
|
|
|
|
return _set_asn1_time(which(self._crl), when)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
def set_lastUpdate(self, when):
|
|
|
|
"""
|
|
|
|
Set when the CRL was last updated.
|
|
|
|
|
|
|
|
The timestamp is formatted as an ASN.1 TIME::
|
|
|
|
|
|
|
|
YYYYMMDDhhmmssZ
|
|
|
|
|
|
|
|
.. versionadded:: 16.1.0
|
|
|
|
|
|
|
|
:param bytes when: A timestamp string.
|
|
|
|
:return: ``None``
|
|
|
|
"""
|
|
|
|
return self._set_boundary_time(_lib.X509_CRL_get_lastUpdate, when)
|
|
|
|
|
|
|
|
def set_nextUpdate(self, when):
|
|
|
|
"""
|
|
|
|
Set when the CRL will next be udpated.
|
|
|
|
|
|
|
|
The timestamp is formatted as an ASN.1 TIME::
|
|
|
|
|
|
|
|
YYYYMMDDhhmmssZ
|
|
|
|
|
|
|
|
.. versionadded:: 16.1.0
|
|
|
|
|
|
|
|
:param bytes when: A timestamp string.
|
|
|
|
:return: ``None``
|
|
|
|
"""
|
|
|
|
return self._set_boundary_time(_lib.X509_CRL_get_nextUpdate, when)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
def sign(self, issuer_cert, issuer_key, digest):
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Sign the CRL.
|
|
|
|
|
|
|
|
Signing a CRL enables clients to associate the CRL itself with an
|
|
|
|
issuer. Before a CRL is meaningful to other OpenSSL functions, it must
|
|
|
|
be signed by an issuer.
|
|
|
|
|
|
|
|
This method implicitly sets the issuer's name based on the issuer
|
|
|
|
certificate and private key used to sign the CRL.
|
|
|
|
|
|
|
|
.. versionadded:: 16.1.0
|
|
|
|
|
|
|
|
:param X509 issuer_cert: The issuer's certificate.
|
|
|
|
:param PKey issuer_key: The issuer's private key.
|
|
|
|
:param bytes digest: The digest method to sign the CRL with.
|
|
|
|
"""
|
|
|
|
digest_obj = _lib.EVP_get_digestbyname(digest)
|
|
|
|
_openssl_assert(digest_obj != _ffi.NULL)
|
|
|
|
_lib.X509_CRL_set_issuer_name(
|
|
|
|
self._crl, _lib.X509_get_subject_name(issuer_cert._x509))
|
|
|
|
_lib.X509_CRL_sort(self._crl)
|
|
|
|
result = _lib.X509_CRL_sign(self._crl, issuer_key._pkey, digest_obj)
|
|
|
|
_openssl_assert(result != 0)
|
|
|
|
|
|
|
|
def export(self, cert, key, type=FILETYPE_PEM, days=100,
|
|
|
|
digest=_UNSPECIFIED):
|
|
|
|
"""
|
|
|
|
Export the CRL as a string.
|
|
|
|
|
|
|
|
:param X509 cert: The certificate used to sign the CRL.
|
|
|
|
:param PKey key: The key used to sign the CRL.
|
|
|
|
:param int type: The export format, either :data:`FILETYPE_PEM`,
|
|
|
|
:data:`FILETYPE_ASN1`, or :data:`FILETYPE_TEXT`.
|
|
|
|
:param int days: The number of days until the next update of this CRL.
|
|
|
|
:param bytes digest: The name of the message digest to use (eg
|
|
|
|
``b"sha2566"``).
|
|
|
|
:rtype: bytes
|
|
|
|
"""
|
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
if not isinstance(cert, X509):
|
|
|
|
raise TypeError("cert must be an X509 instance")
|
|
|
|
if not isinstance(key, PKey):
|
|
|
|
raise TypeError("key must be a PKey instance")
|
|
|
|
if not isinstance(type, int):
|
|
|
|
raise TypeError("type must be an integer")
|
|
|
|
|
|
|
|
if digest is _UNSPECIFIED:
|
2018-12-31 23:25:26 +00:00
|
|
|
raise TypeError("digest must be provided")
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
digest_obj = _lib.EVP_get_digestbyname(digest)
|
|
|
|
if digest_obj == _ffi.NULL:
|
|
|
|
raise ValueError("No such digest method")
|
|
|
|
|
|
|
|
bio = _lib.BIO_new(_lib.BIO_s_mem())
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(bio != _ffi.NULL)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
# A scratch time object to give different values to different CRL
|
|
|
|
# fields
|
2016-02-06 09:36:57 +00:00
|
|
|
sometime = _lib.ASN1_TIME_new()
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(sometime != _ffi.NULL)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
_lib.X509_gmtime_adj(sometime, 0)
|
|
|
|
_lib.X509_CRL_set_lastUpdate(self._crl, sometime)
|
|
|
|
|
|
|
|
_lib.X509_gmtime_adj(sometime, days * 24 * 60 * 60)
|
|
|
|
_lib.X509_CRL_set_nextUpdate(self._crl, sometime)
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
_lib.X509_CRL_set_issuer_name(
|
|
|
|
self._crl, _lib.X509_get_subject_name(cert._x509)
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
sign_result = _lib.X509_CRL_sign(self._crl, key._pkey, digest_obj)
|
|
|
|
if not sign_result:
|
|
|
|
_raise_current_error()
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
return dump_crl(type, self)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
CRLType = deprecated(
|
|
|
|
CRL, __name__,
|
|
|
|
"CRLType has been deprecated, use CRL instead",
|
|
|
|
DeprecationWarning
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PKCS7(object):
|
|
|
|
def type_is_signed(self):
|
|
|
|
"""
|
|
|
|
Check if this NID_pkcs7_signed object
|
|
|
|
|
|
|
|
:return: True if the PKCS7 is of type signed
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
return bool(_lib.PKCS7_type_is_signed(self._pkcs7))
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def type_is_enveloped(self):
|
|
|
|
"""
|
|
|
|
Check if this NID_pkcs7_enveloped object
|
|
|
|
|
|
|
|
:returns: True if the PKCS7 is of type enveloped
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
return bool(_lib.PKCS7_type_is_enveloped(self._pkcs7))
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def type_is_signedAndEnveloped(self):
|
|
|
|
"""
|
|
|
|
Check if this NID_pkcs7_signedAndEnveloped object
|
|
|
|
|
|
|
|
:returns: True if the PKCS7 is of type signedAndEnveloped
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
return bool(_lib.PKCS7_type_is_signedAndEnveloped(self._pkcs7))
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def type_is_data(self):
|
|
|
|
"""
|
|
|
|
Check if this NID_pkcs7_data object
|
|
|
|
|
|
|
|
:return: True if the PKCS7 is of type data
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
return bool(_lib.PKCS7_type_is_data(self._pkcs7))
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def get_type_name(self):
|
|
|
|
"""
|
|
|
|
Returns the type name of the PKCS7 structure
|
|
|
|
|
|
|
|
:return: A string with the typename
|
|
|
|
"""
|
|
|
|
nid = _lib.OBJ_obj2nid(self._pkcs7.type)
|
|
|
|
string_type = _lib.OBJ_nid2sn(nid)
|
|
|
|
return _ffi.string(string_type)
|
|
|
|
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
PKCS7Type = deprecated(
|
|
|
|
PKCS7, __name__,
|
|
|
|
"PKCS7Type has been deprecated, use PKCS7 instead",
|
|
|
|
DeprecationWarning
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PKCS12(object):
|
2018-12-31 23:25:26 +00:00
|
|
|
"""
|
|
|
|
A PKCS #12 archive.
|
|
|
|
"""
|
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
def __init__(self):
|
|
|
|
self._pkey = None
|
|
|
|
self._cert = None
|
|
|
|
self._cacerts = None
|
|
|
|
self._friendlyname = None
|
|
|
|
|
|
|
|
def get_certificate(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Get the certificate in the PKCS #12 structure.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The certificate, or :py:const:`None` if there is none.
|
|
|
|
:rtype: :py:class:`X509` or :py:const:`None`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
return self._cert
|
|
|
|
|
|
|
|
def set_certificate(self, cert):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Set the certificate in the PKCS #12 structure.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param cert: The new certificate, or :py:const:`None` to unset it.
|
|
|
|
:type cert: :py:class:`X509` or :py:const:`None`
|
|
|
|
|
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
if not isinstance(cert, X509):
|
|
|
|
raise TypeError("cert must be an X509 instance")
|
|
|
|
self._cert = cert
|
|
|
|
|
|
|
|
def get_privatekey(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Get the private key in the PKCS #12 structure.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The private key, or :py:const:`None` if there is none.
|
|
|
|
:rtype: :py:class:`PKey`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
return self._pkey
|
|
|
|
|
|
|
|
def set_privatekey(self, pkey):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Set the certificate portion of the PKCS #12 structure.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param pkey: The new private key, or :py:const:`None` to unset it.
|
|
|
|
:type pkey: :py:class:`PKey` or :py:const:`None`
|
|
|
|
|
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
if not isinstance(pkey, PKey):
|
|
|
|
raise TypeError("pkey must be a PKey instance")
|
|
|
|
self._pkey = pkey
|
|
|
|
|
|
|
|
def get_ca_certificates(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Get the CA certificates in the PKCS #12 structure.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: A tuple with the CA certificates in the chain, or
|
|
|
|
:py:const:`None` if there are none.
|
|
|
|
:rtype: :py:class:`tuple` of :py:class:`X509` or :py:const:`None`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
if self._cacerts is not None:
|
|
|
|
return tuple(self._cacerts)
|
|
|
|
|
|
|
|
def set_ca_certificates(self, cacerts):
|
|
|
|
"""
|
|
|
|
Replace or set the CA certificates within the PKCS12 object.
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param cacerts: The new CA certificates, or :py:const:`None` to unset
|
|
|
|
them.
|
|
|
|
:type cacerts: An iterable of :py:class:`X509` or :py:const:`None`
|
|
|
|
|
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
if cacerts is None:
|
|
|
|
self._cacerts = None
|
|
|
|
else:
|
|
|
|
cacerts = list(cacerts)
|
|
|
|
for cert in cacerts:
|
|
|
|
if not isinstance(cert, X509):
|
2018-12-31 23:25:26 +00:00
|
|
|
raise TypeError(
|
|
|
|
"iterable must only contain X509 instances"
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
self._cacerts = cacerts
|
|
|
|
|
|
|
|
def set_friendlyname(self, name):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Set the friendly name in the PKCS #12 structure.
|
|
|
|
|
|
|
|
:param name: The new friendly name, or :py:const:`None` to unset.
|
|
|
|
:type name: :py:class:`bytes` or :py:const:`None`
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
if name is None:
|
|
|
|
self._friendlyname = None
|
|
|
|
elif not isinstance(name, bytes):
|
2018-12-31 23:25:26 +00:00
|
|
|
raise TypeError(
|
|
|
|
"name must be a byte string or None (not %r)" % (name,)
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
self._friendlyname = name
|
|
|
|
|
|
|
|
def get_friendlyname(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Get the friendly name in the PKCS# 12 structure.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:returns: The friendly name, or :py:const:`None` if there is none.
|
|
|
|
:rtype: :py:class:`bytes` or :py:const:`None`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
return self._friendlyname
|
|
|
|
|
|
|
|
def export(self, passphrase=None, iter=2048, maciter=1):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Dump a PKCS12 object as a string.
|
|
|
|
|
|
|
|
For more information, see the :c:func:`PKCS12_create` man page.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param passphrase: The passphrase used to encrypt the structure. Unlike
|
|
|
|
some other passphrase arguments, this *must* be a string, not a
|
|
|
|
callback.
|
2016-02-06 09:36:57 +00:00
|
|
|
:type passphrase: :py:data:`bytes`
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param iter: Number of times to repeat the encryption step.
|
2016-02-06 09:36:57 +00:00
|
|
|
:type iter: :py:data:`int`
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param maciter: Number of times to repeat the MAC step.
|
2016-02-06 09:36:57 +00:00
|
|
|
:type maciter: :py:data:`int`
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The string representation of the PKCS #12 structure.
|
|
|
|
:rtype:
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
passphrase = _text_to_bytes_and_warn("passphrase", passphrase)
|
|
|
|
|
|
|
|
if self._cacerts is None:
|
|
|
|
cacerts = _ffi.NULL
|
|
|
|
else:
|
|
|
|
cacerts = _lib.sk_X509_new_null()
|
|
|
|
cacerts = _ffi.gc(cacerts, _lib.sk_X509_free)
|
|
|
|
for cert in self._cacerts:
|
|
|
|
_lib.sk_X509_push(cacerts, cert._x509)
|
|
|
|
|
|
|
|
if passphrase is None:
|
|
|
|
passphrase = _ffi.NULL
|
|
|
|
|
|
|
|
friendlyname = self._friendlyname
|
|
|
|
if friendlyname is None:
|
|
|
|
friendlyname = _ffi.NULL
|
|
|
|
|
|
|
|
if self._pkey is None:
|
|
|
|
pkey = _ffi.NULL
|
|
|
|
else:
|
|
|
|
pkey = self._pkey._pkey
|
|
|
|
|
|
|
|
if self._cert is None:
|
|
|
|
cert = _ffi.NULL
|
|
|
|
else:
|
|
|
|
cert = self._cert._x509
|
|
|
|
|
|
|
|
pkcs12 = _lib.PKCS12_create(
|
|
|
|
passphrase, friendlyname, pkey, cert, cacerts,
|
|
|
|
_lib.NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
|
|
|
|
_lib.NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
|
|
|
|
iter, maciter, 0)
|
|
|
|
if pkcs12 == _ffi.NULL:
|
|
|
|
_raise_current_error()
|
|
|
|
pkcs12 = _ffi.gc(pkcs12, _lib.PKCS12_free)
|
|
|
|
|
|
|
|
bio = _new_mem_buf()
|
|
|
|
_lib.i2d_PKCS12_bio(bio, pkcs12)
|
|
|
|
return _bio_to_string(bio)
|
|
|
|
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
PKCS12Type = deprecated(
|
|
|
|
PKCS12, __name__,
|
|
|
|
"PKCS12Type has been deprecated, use PKCS12 instead",
|
|
|
|
DeprecationWarning
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NetscapeSPKI(object):
|
2018-12-31 23:25:26 +00:00
|
|
|
"""
|
|
|
|
A Netscape SPKI object.
|
|
|
|
"""
|
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
def __init__(self):
|
|
|
|
spki = _lib.NETSCAPE_SPKI_new()
|
|
|
|
self._spki = _ffi.gc(spki, _lib.NETSCAPE_SPKI_free)
|
|
|
|
|
|
|
|
def sign(self, pkey, digest):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Sign the certificate request with this key and digest type.
|
|
|
|
|
|
|
|
:param pkey: The private key to sign with.
|
|
|
|
:type pkey: :py:class:`PKey`
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param digest: The message digest to use.
|
|
|
|
:type digest: :py:class:`bytes`
|
|
|
|
|
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
if pkey._only_public:
|
|
|
|
raise ValueError("Key has only public part")
|
|
|
|
|
|
|
|
if not pkey._initialized:
|
|
|
|
raise ValueError("Key is uninitialized")
|
|
|
|
|
|
|
|
digest_obj = _lib.EVP_get_digestbyname(_byte_string(digest))
|
|
|
|
if digest_obj == _ffi.NULL:
|
|
|
|
raise ValueError("No such digest method")
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
sign_result = _lib.NETSCAPE_SPKI_sign(
|
|
|
|
self._spki, pkey._pkey, digest_obj
|
|
|
|
)
|
|
|
|
_openssl_assert(sign_result > 0)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def verify(self, key):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Verifies a signature on a certificate request.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param PKey key: The public key that signature is supposedly from.
|
|
|
|
|
|
|
|
:return: ``True`` if the signature is correct.
|
|
|
|
:rtype: bool
|
|
|
|
|
|
|
|
:raises OpenSSL.crypto.Error: If the signature is invalid, or there was
|
|
|
|
a problem verifying the signature.
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
answer = _lib.NETSCAPE_SPKI_verify(self._spki, key._pkey)
|
|
|
|
if answer <= 0:
|
|
|
|
_raise_current_error()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def b64_encode(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Generate a base64 encoded representation of this SPKI object.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The base64 encoded string.
|
|
|
|
:rtype: :py:class:`bytes`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
encoded = _lib.NETSCAPE_SPKI_b64_encode(self._spki)
|
|
|
|
result = _ffi.string(encoded)
|
2018-12-31 23:25:26 +00:00
|
|
|
_lib.OPENSSL_free(encoded)
|
2016-02-06 09:36:57 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
def get_pubkey(self):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Get the public key of this certificate.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: The public key.
|
|
|
|
:rtype: :py:class:`PKey`
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
pkey = PKey.__new__(PKey)
|
|
|
|
pkey._pkey = _lib.NETSCAPE_SPKI_get_pubkey(self._spki)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(pkey._pkey != _ffi.NULL)
|
2016-02-06 09:36:57 +00:00
|
|
|
pkey._pkey = _ffi.gc(pkey._pkey, _lib.EVP_PKEY_free)
|
|
|
|
pkey._only_public = True
|
|
|
|
return pkey
|
|
|
|
|
|
|
|
def set_pubkey(self, pkey):
|
|
|
|
"""
|
|
|
|
Set the public key of the certificate
|
|
|
|
|
|
|
|
:param pkey: The public key
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: ``None``
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
set_result = _lib.NETSCAPE_SPKI_set_pubkey(self._spki, pkey._pkey)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(set_result == 1)
|
|
|
|
|
|
|
|
|
|
|
|
NetscapeSPKIType = deprecated(
|
|
|
|
NetscapeSPKI, __name__,
|
|
|
|
"NetscapeSPKIType has been deprecated, use NetscapeSPKI instead",
|
|
|
|
DeprecationWarning
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class _PassphraseHelper(object):
|
|
|
|
def __init__(self, type, passphrase, more_args=False, truncate=False):
|
|
|
|
if type != FILETYPE_PEM and passphrase is not None:
|
2018-12-31 23:25:26 +00:00
|
|
|
raise ValueError(
|
|
|
|
"only FILETYPE_PEM key format supports encryption"
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
self._passphrase = passphrase
|
|
|
|
self._more_args = more_args
|
|
|
|
self._truncate = truncate
|
|
|
|
self._problems = []
|
|
|
|
|
|
|
|
@property
|
|
|
|
def callback(self):
|
|
|
|
if self._passphrase is None:
|
|
|
|
return _ffi.NULL
|
|
|
|
elif isinstance(self._passphrase, bytes):
|
|
|
|
return _ffi.NULL
|
|
|
|
elif callable(self._passphrase):
|
|
|
|
return _ffi.callback("pem_password_cb", self._read_passphrase)
|
|
|
|
else:
|
2018-12-31 23:25:26 +00:00
|
|
|
raise TypeError(
|
|
|
|
"Last argument must be a byte string or a callable."
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def callback_args(self):
|
|
|
|
if self._passphrase is None:
|
|
|
|
return _ffi.NULL
|
|
|
|
elif isinstance(self._passphrase, bytes):
|
|
|
|
return self._passphrase
|
|
|
|
elif callable(self._passphrase):
|
|
|
|
return _ffi.NULL
|
|
|
|
else:
|
2018-12-31 23:25:26 +00:00
|
|
|
raise TypeError(
|
|
|
|
"Last argument must be a byte string or a callable."
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def raise_if_problem(self, exceptionType=Error):
|
|
|
|
if self._problems:
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
# Flush the OpenSSL error queue
|
|
|
|
try:
|
|
|
|
_exception_from_error_queue(exceptionType)
|
|
|
|
except exceptionType:
|
|
|
|
pass
|
|
|
|
|
|
|
|
raise self._problems.pop(0)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def _read_passphrase(self, buf, size, rwflag, userdata):
|
|
|
|
try:
|
|
|
|
if self._more_args:
|
|
|
|
result = self._passphrase(size, rwflag, userdata)
|
|
|
|
else:
|
|
|
|
result = self._passphrase(rwflag)
|
|
|
|
if not isinstance(result, bytes):
|
|
|
|
raise ValueError("String expected")
|
|
|
|
if len(result) > size:
|
|
|
|
if self._truncate:
|
|
|
|
result = result[:size]
|
|
|
|
else:
|
2018-12-31 23:25:26 +00:00
|
|
|
raise ValueError(
|
|
|
|
"passphrase returned by callback is too long"
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
for i in range(len(result)):
|
|
|
|
buf[i] = result[i:i + 1]
|
|
|
|
return len(result)
|
|
|
|
except Exception as e:
|
|
|
|
self._problems.append(e)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
def load_publickey(type, buffer):
|
|
|
|
"""
|
|
|
|
Load a public key from a buffer.
|
|
|
|
|
|
|
|
:param type: The file type (one of :data:`FILETYPE_PEM`,
|
|
|
|
:data:`FILETYPE_ASN1`).
|
|
|
|
:param buffer: The buffer the key is stored in.
|
|
|
|
:type buffer: A Python string object, either unicode or bytestring.
|
|
|
|
:return: The PKey object.
|
|
|
|
:rtype: :class:`PKey`
|
|
|
|
"""
|
|
|
|
if isinstance(buffer, _text_type):
|
|
|
|
buffer = buffer.encode("ascii")
|
|
|
|
|
|
|
|
bio = _new_mem_buf(buffer)
|
|
|
|
|
|
|
|
if type == FILETYPE_PEM:
|
|
|
|
evp_pkey = _lib.PEM_read_bio_PUBKEY(
|
|
|
|
bio, _ffi.NULL, _ffi.NULL, _ffi.NULL)
|
|
|
|
elif type == FILETYPE_ASN1:
|
|
|
|
evp_pkey = _lib.d2i_PUBKEY_bio(bio, _ffi.NULL)
|
|
|
|
else:
|
|
|
|
raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1")
|
|
|
|
|
|
|
|
if evp_pkey == _ffi.NULL:
|
|
|
|
_raise_current_error()
|
|
|
|
|
|
|
|
pkey = PKey.__new__(PKey)
|
|
|
|
pkey._pkey = _ffi.gc(evp_pkey, _lib.EVP_PKEY_free)
|
|
|
|
pkey._only_public = True
|
|
|
|
return pkey
|
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
def load_privatekey(type, buffer, passphrase=None):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Load a private key (PKey) from the string *buffer* encoded with the type
|
|
|
|
*type*.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
:param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1)
|
|
|
|
:param buffer: The buffer the key is stored in
|
|
|
|
:param passphrase: (optional) if encrypted PEM format, this can be
|
|
|
|
either the passphrase to use, or a callback for
|
|
|
|
providing the passphrase.
|
|
|
|
|
|
|
|
:return: The PKey object
|
|
|
|
"""
|
|
|
|
if isinstance(buffer, _text_type):
|
|
|
|
buffer = buffer.encode("ascii")
|
|
|
|
|
|
|
|
bio = _new_mem_buf(buffer)
|
|
|
|
|
|
|
|
helper = _PassphraseHelper(type, passphrase)
|
|
|
|
if type == FILETYPE_PEM:
|
|
|
|
evp_pkey = _lib.PEM_read_bio_PrivateKey(
|
|
|
|
bio, _ffi.NULL, helper.callback, helper.callback_args)
|
|
|
|
helper.raise_if_problem()
|
|
|
|
elif type == FILETYPE_ASN1:
|
|
|
|
evp_pkey = _lib.d2i_PrivateKey_bio(bio, _ffi.NULL)
|
|
|
|
else:
|
|
|
|
raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1")
|
|
|
|
|
|
|
|
if evp_pkey == _ffi.NULL:
|
|
|
|
_raise_current_error()
|
|
|
|
|
|
|
|
pkey = PKey.__new__(PKey)
|
|
|
|
pkey._pkey = _ffi.gc(evp_pkey, _lib.EVP_PKEY_free)
|
|
|
|
return pkey
|
|
|
|
|
|
|
|
|
|
|
|
def dump_certificate_request(type, req):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Dump the certificate request *req* into a buffer string encoded with the
|
|
|
|
type *type*.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
:param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1)
|
|
|
|
:param req: The certificate request to dump
|
|
|
|
:return: The buffer with the dumped certificate request in
|
|
|
|
"""
|
|
|
|
bio = _new_mem_buf()
|
|
|
|
|
|
|
|
if type == FILETYPE_PEM:
|
|
|
|
result_code = _lib.PEM_write_bio_X509_REQ(bio, req._req)
|
|
|
|
elif type == FILETYPE_ASN1:
|
|
|
|
result_code = _lib.i2d_X509_REQ_bio(bio, req._req)
|
|
|
|
elif type == FILETYPE_TEXT:
|
|
|
|
result_code = _lib.X509_REQ_print_ex(bio, req._req, 0, 0)
|
|
|
|
else:
|
2018-12-31 23:25:26 +00:00
|
|
|
raise ValueError(
|
|
|
|
"type argument must be FILETYPE_PEM, FILETYPE_ASN1, or "
|
|
|
|
"FILETYPE_TEXT"
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(result_code != 0)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
return _bio_to_string(bio)
|
|
|
|
|
|
|
|
|
|
|
|
def load_certificate_request(type, buffer):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Load a certificate request (X509Req) from the string *buffer* encoded with
|
|
|
|
the type *type*.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
:param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1)
|
|
|
|
:param buffer: The buffer the certificate request is stored in
|
|
|
|
:return: The X509Req object
|
|
|
|
"""
|
|
|
|
if isinstance(buffer, _text_type):
|
|
|
|
buffer = buffer.encode("ascii")
|
|
|
|
|
|
|
|
bio = _new_mem_buf(buffer)
|
|
|
|
|
|
|
|
if type == FILETYPE_PEM:
|
|
|
|
req = _lib.PEM_read_bio_X509_REQ(bio, _ffi.NULL, _ffi.NULL, _ffi.NULL)
|
|
|
|
elif type == FILETYPE_ASN1:
|
|
|
|
req = _lib.d2i_X509_REQ_bio(bio, _ffi.NULL)
|
|
|
|
else:
|
|
|
|
raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1")
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(req != _ffi.NULL)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
x509req = X509Req.__new__(X509Req)
|
|
|
|
x509req._req = _ffi.gc(req, _lib.X509_REQ_free)
|
|
|
|
return x509req
|
|
|
|
|
|
|
|
|
|
|
|
def sign(pkey, data, digest):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Sign a data string using the given key and message digest.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param pkey: PKey to sign with
|
2016-02-06 09:36:57 +00:00
|
|
|
:param data: data to be signed
|
|
|
|
:param digest: message digest to use
|
|
|
|
:return: signature
|
2018-12-31 23:25:26 +00:00
|
|
|
|
|
|
|
.. versionadded:: 0.11
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
data = _text_to_bytes_and_warn("data", data)
|
|
|
|
|
|
|
|
digest_obj = _lib.EVP_get_digestbyname(_byte_string(digest))
|
|
|
|
if digest_obj == _ffi.NULL:
|
|
|
|
raise ValueError("No such digest method")
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
md_ctx = _lib.Cryptography_EVP_MD_CTX_new()
|
|
|
|
md_ctx = _ffi.gc(md_ctx, _lib.Cryptography_EVP_MD_CTX_free)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
_lib.EVP_SignInit(md_ctx, digest_obj)
|
|
|
|
_lib.EVP_SignUpdate(md_ctx, data, len(data))
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
length = _lib.EVP_PKEY_size(pkey._pkey)
|
|
|
|
_openssl_assert(length > 0)
|
|
|
|
signature_buffer = _ffi.new("unsigned char[]", length)
|
|
|
|
signature_length = _ffi.new("unsigned int *")
|
2016-02-06 09:36:57 +00:00
|
|
|
final_result = _lib.EVP_SignFinal(
|
|
|
|
md_ctx, signature_buffer, signature_length, pkey._pkey)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(final_result == 1)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
return _ffi.buffer(signature_buffer, signature_length[0])[:]
|
|
|
|
|
|
|
|
|
|
|
|
def verify(cert, signature, data, digest):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Verify the signature for a data string.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
:param cert: signing certificate (X509 object) corresponding to the
|
|
|
|
private key which generated the signature.
|
2016-02-06 09:36:57 +00:00
|
|
|
:param signature: signature returned by sign function
|
|
|
|
:param data: data to be verified
|
|
|
|
:param digest: message digest to use
|
2018-12-31 23:25:26 +00:00
|
|
|
:return: ``None`` if the signature is correct, raise exception otherwise.
|
|
|
|
|
|
|
|
.. versionadded:: 0.11
|
2016-02-06 09:36:57 +00:00
|
|
|
"""
|
|
|
|
data = _text_to_bytes_and_warn("data", data)
|
|
|
|
|
|
|
|
digest_obj = _lib.EVP_get_digestbyname(_byte_string(digest))
|
|
|
|
if digest_obj == _ffi.NULL:
|
|
|
|
raise ValueError("No such digest method")
|
|
|
|
|
|
|
|
pkey = _lib.X509_get_pubkey(cert._x509)
|
2018-12-31 23:25:26 +00:00
|
|
|
_openssl_assert(pkey != _ffi.NULL)
|
2016-02-06 09:36:57 +00:00
|
|
|
pkey = _ffi.gc(pkey, _lib.EVP_PKEY_free)
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
md_ctx = _lib.Cryptography_EVP_MD_CTX_new()
|
|
|
|
md_ctx = _ffi.gc(md_ctx, _lib.Cryptography_EVP_MD_CTX_free)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
_lib.EVP_VerifyInit(md_ctx, digest_obj)
|
|
|
|
_lib.EVP_VerifyUpdate(md_ctx, data, len(data))
|
2018-12-31 23:25:26 +00:00
|
|
|
verify_result = _lib.EVP_VerifyFinal(
|
|
|
|
md_ctx, signature, len(signature), pkey
|
|
|
|
)
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
if verify_result != 1:
|
|
|
|
_raise_current_error()
|
|
|
|
|
|
|
|
|
2018-12-31 23:25:26 +00:00
|
|
|
def dump_crl(type, crl):
|
|
|
|
"""
|
|
|
|
Dump a certificate revocation list to a buffer.
|
|
|
|
|
|
|
|
:param type: The file type (one of ``FILETYPE_PEM``, ``FILETYPE_ASN1``, or
|
|
|
|
``FILETYPE_TEXT``).
|
|
|
|
:param CRL crl: The CRL to dump.
|
|
|
|
|
|
|
|
:return: The buffer with the CRL.
|
|
|
|
:rtype: bytes
|
|
|
|
"""
|
|
|
|
bio = _new_mem_buf()
|
|
|
|
|
|
|
|
if type == FILETYPE_PEM:
|
|
|
|
ret = _lib.PEM_write_bio_X509_CRL(bio, crl._crl)
|
|
|
|
elif type == FILETYPE_ASN1:
|
|
|
|
ret = _lib.i2d_X509_CRL_bio(bio, crl._crl)
|
|
|
|
elif type == FILETYPE_TEXT:
|
|
|
|
ret = _lib.X509_CRL_print(bio, crl._crl)
|
|
|
|
else:
|
|
|
|
raise ValueError(
|
|
|
|
"type argument must be FILETYPE_PEM, FILETYPE_ASN1, or "
|
|
|
|
"FILETYPE_TEXT")
|
|
|
|
|
|
|
|
assert ret == 1
|
|
|
|
return _bio_to_string(bio)
|
|
|
|
|
|
|
|
|
2016-02-06 09:36:57 +00:00
|
|
|
def load_crl(type, buffer):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Load Certificate Revocation List (CRL) data from a string *buffer*.
|
|
|
|
*buffer* encoded with the type *type*.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
:param type: The file type (one of FILETYPE_PEM, FILETYPE_ASN1)
|
|
|
|
:param buffer: The buffer the CRL is stored in
|
|
|
|
|
|
|
|
:return: The PKey object
|
|
|
|
"""
|
|
|
|
if isinstance(buffer, _text_type):
|
|
|
|
buffer = buffer.encode("ascii")
|
|
|
|
|
|
|
|
bio = _new_mem_buf(buffer)
|
|
|
|
|
|
|
|
if type == FILETYPE_PEM:
|
|
|
|
crl = _lib.PEM_read_bio_X509_CRL(bio, _ffi.NULL, _ffi.NULL, _ffi.NULL)
|
|
|
|
elif type == FILETYPE_ASN1:
|
|
|
|
crl = _lib.d2i_X509_CRL_bio(bio, _ffi.NULL)
|
|
|
|
else:
|
|
|
|
raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1")
|
|
|
|
|
|
|
|
if crl == _ffi.NULL:
|
|
|
|
_raise_current_error()
|
|
|
|
|
|
|
|
result = CRL.__new__(CRL)
|
2018-12-31 23:25:26 +00:00
|
|
|
result._crl = _ffi.gc(crl, _lib.X509_CRL_free)
|
2016-02-06 09:36:57 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def load_pkcs7_data(type, buffer):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Load pkcs7 data from the string *buffer* encoded with the type
|
|
|
|
*type*.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
:param type: The file type (one of FILETYPE_PEM or FILETYPE_ASN1)
|
|
|
|
:param buffer: The buffer with the pkcs7 data.
|
|
|
|
:return: The PKCS7 object
|
|
|
|
"""
|
|
|
|
if isinstance(buffer, _text_type):
|
|
|
|
buffer = buffer.encode("ascii")
|
|
|
|
|
|
|
|
bio = _new_mem_buf(buffer)
|
|
|
|
|
|
|
|
if type == FILETYPE_PEM:
|
|
|
|
pkcs7 = _lib.PEM_read_bio_PKCS7(bio, _ffi.NULL, _ffi.NULL, _ffi.NULL)
|
|
|
|
elif type == FILETYPE_ASN1:
|
|
|
|
pkcs7 = _lib.d2i_PKCS7_bio(bio, _ffi.NULL)
|
|
|
|
else:
|
|
|
|
raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1")
|
|
|
|
|
|
|
|
if pkcs7 == _ffi.NULL:
|
|
|
|
_raise_current_error()
|
|
|
|
|
|
|
|
pypkcs7 = PKCS7.__new__(PKCS7)
|
|
|
|
pypkcs7._pkcs7 = _ffi.gc(pkcs7, _lib.PKCS7_free)
|
|
|
|
return pypkcs7
|
|
|
|
|
|
|
|
|
|
|
|
def load_pkcs12(buffer, passphrase=None):
|
|
|
|
"""
|
2018-12-31 23:25:26 +00:00
|
|
|
Load pkcs12 data from the string *buffer*. If the pkcs12 structure is
|
|
|
|
encrypted, a *passphrase* must be included. The MAC is always
|
|
|
|
checked and thus required.
|
|
|
|
|
|
|
|
See also the man page for the C function :py:func:`PKCS12_parse`.
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
:param buffer: The buffer the certificate is stored in
|
|
|
|
:param passphrase: (Optional) The password to decrypt the PKCS12 lump
|
|
|
|
:returns: The PKCS12 object
|
|
|
|
"""
|
|
|
|
passphrase = _text_to_bytes_and_warn("passphrase", passphrase)
|
|
|
|
|
|
|
|
if isinstance(buffer, _text_type):
|
|
|
|
buffer = buffer.encode("ascii")
|
|
|
|
|
|
|
|
bio = _new_mem_buf(buffer)
|
|
|
|
|
|
|
|
# Use null passphrase if passphrase is None or empty string. With PKCS#12
|
|
|
|
# password based encryption no password and a zero length password are two
|
|
|
|
# different things, but OpenSSL implementation will try both to figure out
|
|
|
|
# which one works.
|
|
|
|
if not passphrase:
|
|
|
|
passphrase = _ffi.NULL
|
|
|
|
|
|
|
|
p12 = _lib.d2i_PKCS12_bio(bio, _ffi.NULL)
|
|
|
|
if p12 == _ffi.NULL:
|
|
|
|
_raise_current_error()
|
|
|
|
p12 = _ffi.gc(p12, _lib.PKCS12_free)
|
|
|
|
|
|
|
|
pkey = _ffi.new("EVP_PKEY**")
|
|
|
|
cert = _ffi.new("X509**")
|
|
|
|
cacerts = _ffi.new("Cryptography_STACK_OF_X509**")
|
|
|
|
|
|
|
|
parse_result = _lib.PKCS12_parse(p12, passphrase, pkey, cert, cacerts)
|
|
|
|
if not parse_result:
|
|
|
|
_raise_current_error()
|
|
|
|
|
|
|
|
cacerts = _ffi.gc(cacerts[0], _lib.sk_X509_free)
|
|
|
|
|
|
|
|
# openssl 1.0.0 sometimes leaves an X509_check_private_key error in the
|
|
|
|
# queue for no particular reason. This error isn't interesting to anyone
|
|
|
|
# outside this function. It's not even interesting to us. Get rid of it.
|
|
|
|
try:
|
|
|
|
_raise_current_error()
|
|
|
|
except Error:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if pkey[0] == _ffi.NULL:
|
|
|
|
pykey = None
|
|
|
|
else:
|
|
|
|
pykey = PKey.__new__(PKey)
|
|
|
|
pykey._pkey = _ffi.gc(pkey[0], _lib.EVP_PKEY_free)
|
|
|
|
|
|
|
|
if cert[0] == _ffi.NULL:
|
|
|
|
pycert = None
|
|
|
|
friendlyname = None
|
|
|
|
else:
|
2018-12-31 23:25:26 +00:00
|
|
|
pycert = X509._from_raw_x509_ptr(cert[0])
|
2016-02-06 09:36:57 +00:00
|
|
|
|
|
|
|
friendlyname_length = _ffi.new("int*")
|
2018-12-31 23:25:26 +00:00
|
|
|
friendlyname_buffer = _lib.X509_alias_get0(
|
|
|
|
cert[0], friendlyname_length
|
|
|
|
)
|
|
|
|
friendlyname = _ffi.buffer(
|
|
|
|
friendlyname_buffer, friendlyname_length[0]
|
|
|
|
)[:]
|
2016-02-06 09:36:57 +00:00
|
|
|
if friendlyname_buffer == _ffi.NULL:
|
|
|
|
friendlyname = None
|
|
|
|
|
|
|
|
pycacerts = []
|
|
|
|
for i in range(_lib.sk_X509_num(cacerts)):
|
2018-12-31 23:25:26 +00:00
|
|
|
x509 = _lib.sk_X509_value(cacerts, i)
|
|
|
|
pycacert = X509._from_raw_x509_ptr(x509)
|
2016-02-06 09:36:57 +00:00
|
|
|
pycacerts.append(pycacert)
|
|
|
|
if not pycacerts:
|
|
|
|
pycacerts = None
|
|
|
|
|
|
|
|
pkcs12 = PKCS12.__new__(PKCS12)
|
|
|
|
pkcs12._pkey = pykey
|
|
|
|
pkcs12._cert = pycert
|
|
|
|
pkcs12._cacerts = pycacerts
|
|
|
|
pkcs12._friendlyname = friendlyname
|
|
|
|
return pkcs12
|
|
|
|
|
|
|
|
|
|
|
|
# There are no direct unit tests for this initialization. It is tested
|
|
|
|
# indirectly since it is necessary for functions like dump_privatekey when
|
|
|
|
# using encryption.
|
|
|
|
#
|
|
|
|
# Thus OpenSSL.test.test_crypto.FunctionTests.test_dump_privatekey_passphrase
|
|
|
|
# and some other similar tests may fail without this (though they may not if
|
|
|
|
# the Python runtime has already done some initialization of the underlying
|
|
|
|
# OpenSSL library (and is linked against the same one that cryptography is
|
|
|
|
# using)).
|
|
|
|
_lib.OpenSSL_add_all_algorithms()
|
|
|
|
|
|
|
|
# This is similar but exercised mainly by exception_from_error_queue. It calls
|
|
|
|
# both ERR_load_crypto_strings() and ERR_load_SSL_strings().
|
|
|
|
_lib.SSL_load_error_strings()
|
2018-12-31 23:25:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Set the default string mask to match OpenSSL upstream (since 2005) and
|
|
|
|
# RFC5280 recommendations.
|
|
|
|
_lib.ASN1_STRING_set_default_mask_asc(b'utf8only')
|