diff options
author | Alex Gaynor <alex.gaynor@gmail.com> | 2015-12-27 10:38:39 -0500 |
---|---|---|
committer | Alex Gaynor <alex.gaynor@gmail.com> | 2015-12-27 10:38:39 -0500 |
commit | 98f3df9c6d70c53599d3b256e56a8059ddadb0eb (patch) | |
tree | 26eb0bdc040c82b9e67c2f60a0f7a072c601cf5b /src | |
parent | 28077b621390965fbe1bca3409691974c894251d (diff) | |
parent | 3a03d7ba193b07d1146a6ff7615721598f05bfe0 (diff) | |
download | cryptography-98f3df9c6d70c53599d3b256e56a8059ddadb0eb.tar.gz cryptography-98f3df9c6d70c53599d3b256e56a8059ddadb0eb.tar.bz2 cryptography-98f3df9c6d70c53599d3b256e56a8059ddadb0eb.zip |
Merge pull request #2582 from reaperhulk/revokedcertbuilder-extensions
RevokedCertificateBuilder extensions
Diffstat (limited to 'src')
-rw-r--r-- | src/cryptography/hazmat/backends/openssl/backend.py | 57 | ||||
-rw-r--r-- | src/cryptography/x509/base.py | 15 |
2 files changed, 67 insertions, 5 deletions
diff --git a/src/cryptography/hazmat/backends/openssl/backend.py b/src/cryptography/hazmat/backends/openssl/backend.py index c0c9ebe2..109993f2 100644 --- a/src/cryptography/hazmat/backends/openssl/backend.py +++ b/src/cryptography/hazmat/backends/openssl/backend.py @@ -37,8 +37,9 @@ from cryptography.hazmat.backends.openssl.rsa import ( _RSAPrivateKey, _RSAPublicKey ) from cryptography.hazmat.backends.openssl.x509 import ( - _Certificate, _CertificateRevocationList, _CertificateSigningRequest, - _DISTPOINT_TYPE_FULLNAME, _DISTPOINT_TYPE_RELATIVENAME, _RevokedCertificate + _CRL_ENTRY_REASON_ENUM_TO_CODE, _Certificate, _CertificateRevocationList, + _CertificateSigningRequest, _DISTPOINT_TYPE_FULLNAME, + _DISTPOINT_TYPE_RELATIVENAME, _RevokedCertificate ) from cryptography.hazmat.bindings._openssl import ffi as _ffi from cryptography.hazmat.bindings.openssl import binding @@ -53,7 +54,7 @@ from cryptography.hazmat.primitives.ciphers.algorithms import ( from cryptography.hazmat.primitives.ciphers.modes import ( CBC, CFB, CFB8, CTR, ECB, GCM, OFB ) -from cryptography.x509.oid import ExtensionOID, NameOID +from cryptography.x509.oid import CRLEntryExtensionOID, ExtensionOID, NameOID _MemoryBIO = collections.namedtuple("_MemoryBIO", ["bio", "char_ptr"]) @@ -164,6 +165,40 @@ def _encode_crl_number(backend, crl_number): return pp, r +def _encode_crl_reason(backend, crl_reason): + asn1enum = backend._lib.ASN1_ENUMERATED_new() + backend.openssl_assert(asn1enum != backend._ffi.NULL) + asn1enum = backend._ffi.gc(asn1enum, backend._lib.ASN1_ENUMERATED_free) + res = backend._lib.ASN1_ENUMERATED_set( + asn1enum, _CRL_ENTRY_REASON_ENUM_TO_CODE[crl_reason.reason] + ) + backend.openssl_assert(res == 1) + pp = backend._ffi.new('unsigned char **') + r = backend._lib.i2d_ASN1_ENUMERATED(asn1enum, pp) + backend.openssl_assert(r > 0) + pp = backend._ffi.gc( + pp, lambda pointer: backend._lib.OPENSSL_free(pointer[0]) + ) + return pp, r + + +def _encode_invalidity_date(backend, invalidity_date): + time = backend._lib.ASN1_GENERALIZEDTIME_set( + backend._ffi.NULL, calendar.timegm( + invalidity_date.invalidity_date.timetuple() + ) + ) + backend.openssl_assert(time != backend._ffi.NULL) + time = backend._ffi.gc(time, backend._lib.ASN1_GENERALIZEDTIME_free) + pp = backend._ffi.new('unsigned char **') + r = backend._lib.i2d_ASN1_GENERALIZEDTIME(time, pp) + backend.openssl_assert(r > 0) + pp = backend._ffi.gc( + pp, lambda pointer: backend._lib.OPENSSL_free(pointer[0]) + ) + return pp, r + + def _encode_certificate_policies(backend, certificate_policies): cp = backend._lib.sk_POLICYINFO_new_null() backend.openssl_assert(cp != backend._ffi.NULL) @@ -645,6 +680,12 @@ _CRL_EXTENSION_ENCODE_HANDLERS = { ExtensionOID.CRL_NUMBER: _encode_crl_number, } +_CRL_ENTRY_EXTENSION_ENCODE_HANDLERS = { + CRLEntryExtensionOID.CERTIFICATE_ISSUER: _encode_alt_name, + CRLEntryExtensionOID.CRL_REASON: _encode_crl_reason, + CRLEntryExtensionOID.INVALIDITY_DATE: _encode_invalidity_date, +} + class _PasswordUserdata(object): def __init__(self, password): @@ -1505,7 +1546,6 @@ class Backend(object): next_update = self._ffi.gc(next_update, self._lib.ASN1_TIME_free) res = self._lib.X509_CRL_set_nextUpdate(x509_crl, next_update) self.openssl_assert(res == 1) - # TODO: support revoked certificates # Add extensions. self._create_x509_extensions( @@ -1583,7 +1623,14 @@ class Backend(object): calendar.timegm(builder._revocation_date.timetuple()) ) self.openssl_assert(res != self._ffi.NULL) - # TODO: add crl entry extensions + # add CRL entry extensions + self._create_x509_extensions( + extensions=builder._extensions, + handlers=_CRL_ENTRY_EXTENSION_ENCODE_HANDLERS, + x509_obj=x509_revoked, + add_func=self._lib.X509_REVOKED_add_ext, + gc=True + ) return _RevokedCertificate(self, None, x509_revoked) def load_pem_private_key(self, data, password): diff --git a/src/cryptography/x509/base.py b/src/cryptography/x509/base.py index bc927e87..55e965f7 100644 --- a/src/cryptography/x509/base.py +++ b/src/cryptography/x509/base.py @@ -650,6 +650,21 @@ class RevokedCertificateBuilder(object): self._serial_number, time, self._extensions ) + def add_extension(self, extension, critical): + if not isinstance(extension, ExtensionType): + raise TypeError("extension must be an ExtensionType") + + extension = Extension(extension.oid, critical, extension) + + # TODO: This is quadratic in the number of extensions + for e in self._extensions: + if e.oid == extension.oid: + raise ValueError('This extension has already been set.') + return RevokedCertificateBuilder( + self._serial_number, self._revocation_date, + self._extensions + [extension] + ) + def build(self, backend): if self._serial_number is None: raise ValueError("A revoked certificate must have a serial number") |