-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
298 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"recommendations": [ | ||
"ms-python.python", | ||
"ms-python.vscode-pylance", | ||
"ms-python.black-formatter" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
# | ||
# Copyright Certy Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
from __future__ import annotations | ||
|
||
import datetime | ||
|
||
from cryptography import x509 | ||
from cryptography.hazmat.primitives import serialization | ||
|
||
from certy import Credential | ||
|
||
|
||
class CertificateRevocationList(object): | ||
"""CertificateRevocationList is a builder for X.509 CRLs.""" | ||
|
||
def __init__( | ||
self, | ||
issuer: Credential | None = None, | ||
revoked_certificates: list[Credential] | None = None, | ||
this_update: datetime.datetime | None = None, | ||
next_update: datetime.datetime | None = None, | ||
): | ||
self._issuer = issuer | ||
self._revoked_certificates = revoked_certificates or [] | ||
self._this_update = this_update | ||
self._next_update = next_update | ||
|
||
# Generated attributes | ||
self._crl: x509.CertificateRevocationList | None = None | ||
|
||
def __repr__(self) -> str: | ||
return f"CertificateRevocationList(issuer={self._issuer}, revoked_certificates={self._revoked_certificates}, this_update={self._this_update}, next_update={self._next_update})" | ||
|
||
# Setter methods | ||
|
||
def issuer(self, issuer: Credential) -> CertificateRevocationList: | ||
"""Set the issuer of the CRL.""" | ||
self._issuer = issuer | ||
return self | ||
|
||
def this_update(self, this_update: datetime.datetime) -> CertificateRevocationList: | ||
"""Set the thisUpdate field of the CRL.""" | ||
self._this_update = this_update | ||
return self | ||
|
||
def next_update(self, next_update: datetime.datetime) -> CertificateRevocationList: | ||
"""Set the nextUpdate field of the CRL.""" | ||
self._next_update = next_update | ||
return self | ||
|
||
def add(self, certificate: Credential) -> CertificateRevocationList: | ||
"""Add a certificate to the CRL.""" | ||
self._revoked_certificates.append(certificate) | ||
return self | ||
|
||
# Builder methods | ||
|
||
def generate(self) -> CertificateRevocationList: | ||
"""Generate the CRL.""" | ||
|
||
if not self._issuer: | ||
if len(self._revoked_certificates) == 0: | ||
raise ValueError( | ||
"issuer not known: either set issuer or add certificates to the CRL" | ||
) | ||
if self._revoked_certificates[0]._issuer is None: | ||
raise ValueError( | ||
"cannot determine issuer from first certificate in CRL" | ||
) | ||
self._issuer = self._revoked_certificates[0]._issuer | ||
|
||
# Ensure that the issuer has a key pair. | ||
self._issuer._ensure_generated() | ||
|
||
effective_revocation_time = datetime.datetime.utcnow() | ||
if self._this_update: | ||
effective_revocation_time = self._this_update | ||
|
||
effective_expiry_time = effective_revocation_time + datetime.timedelta(days=7) | ||
if self._next_update: | ||
effective_expiry_time = self._next_update | ||
|
||
builder = ( | ||
x509.CertificateRevocationListBuilder() | ||
.issuer_name(self._issuer._certificate.subject) # type: ignore | ||
.last_update(effective_revocation_time) | ||
.next_update(effective_expiry_time) | ||
) | ||
|
||
for certificate in self._revoked_certificates: | ||
certificate._ensure_generated() | ||
builder = builder.add_revoked_certificate( | ||
x509.RevokedCertificateBuilder() | ||
.serial_number(certificate._certificate.serial_number) # type: ignore | ||
.revocation_date(effective_revocation_time) | ||
.build() | ||
) | ||
|
||
self._crl = builder.sign( | ||
private_key=self._issuer._private_key, # type: ignore | ||
algorithm=self._issuer._certificate.signature_hash_algorithm, # type: ignore | ||
) | ||
|
||
return self | ||
|
||
def get_as_pem(self) -> bytes: | ||
"""Get the CRL as PEM.""" | ||
self._ensure_generated() | ||
return self._crl.public_bytes(encoding=serialization.Encoding.PEM) # type: ignore | ||
|
||
def get_as_der(self) -> bytes: | ||
"""Get the CRL as DER.""" | ||
self._ensure_generated() | ||
return self._crl.public_bytes(encoding=serialization.Encoding.DER) # type: ignore | ||
|
||
def write_pem(self, filename: str) -> CertificateRevocationList: | ||
"""Write the CRL as PEM to a file.""" | ||
self._ensure_generated() | ||
with open(filename, "wb") as f: | ||
f.write(self.get_as_pem()) | ||
return self | ||
|
||
def write_der(self, filename: str) -> CertificateRevocationList: | ||
self._ensure_generated() | ||
with open(filename, "wb") as f: | ||
f.write(self.get_as_der()) | ||
return self | ||
|
||
# Helper methods | ||
|
||
def _ensure_generated(self) -> CertificateRevocationList: | ||
"""Ensure that the CRL has been generated.""" | ||
if not self._crl: | ||
self.generate() | ||
return self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# | ||
# Copyright Certy Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
import pytest | ||
from cryptography import x509 | ||
|
||
from certy import CertificateRevocationList, Credential | ||
|
||
|
||
@pytest.fixture | ||
def ca(): | ||
return Credential().subject("CN=ca") | ||
|
||
|
||
def test_add(ca): | ||
first_revoked = Credential().issuer(ca).subject("CN=first-revoked") | ||
second_revoked = Credential().issuer(ca).subject("CN=second-revoked") | ||
not_revoked = Credential().issuer(ca).subject("CN=not-revoked") | ||
crl = ( | ||
CertificateRevocationList() | ||
.issuer(ca) | ||
.add(first_revoked) | ||
.add(second_revoked) | ||
.get_as_der() | ||
) | ||
|
||
# Decode DER encoded certificate revocation list from string | ||
c = x509.load_der_x509_crl(crl) | ||
assert c is not None | ||
assert ( | ||
c.get_revoked_certificate_by_serial_number( | ||
first_revoked.get_certificate().serial_number | ||
) | ||
is not None | ||
) | ||
assert ( | ||
c.get_revoked_certificate_by_serial_number( | ||
second_revoked.get_certificate().serial_number | ||
) | ||
is not None | ||
) | ||
assert ( | ||
c.get_revoked_certificate_by_serial_number( | ||
not_revoked.get_certificate().serial_number | ||
) | ||
is None | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters