-
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
5 changed files
with
197 additions
and
89 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,88 @@ | ||
from datetime import datetime, timedelta, timezone | ||
from typing import Self | ||
|
||
from cryptography import x509 | ||
from cryptography.hazmat.primitives.serialization import load_pem_private_key | ||
from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID | ||
|
||
from nodeman.x509 import CertificateAuthorityClient, CertificateInformation, PrivateKey, get_hash_algorithm_from_key | ||
|
||
|
||
class InternalCertificateAuthority(CertificateAuthorityClient): | ||
"""Internal CA""" | ||
|
||
KEY_USAGE = x509.KeyUsage( | ||
digital_signature=True, | ||
content_commitment=False, | ||
key_encipherment=False, | ||
data_encipherment=False, | ||
key_agreement=False, | ||
key_cert_sign=False, | ||
crl_sign=False, | ||
encipher_only=False, | ||
decipher_only=False, | ||
) | ||
|
||
EXTENDED_KEY_USAGE = x509.ExtendedKeyUsage( | ||
usages=[ | ||
ExtendedKeyUsageOID.CLIENT_AUTH, | ||
ExtendedKeyUsageOID.SERVER_AUTH, | ||
] | ||
) | ||
|
||
def __init__( | ||
self, | ||
ca_certificate: x509.Certificate, | ||
ca_private_key: PrivateKey, | ||
validity: timedelta | None = None, | ||
time_skew: timedelta | None = None, | ||
): | ||
self.ca_certificate = ca_certificate | ||
self.ca_private_key = ca_private_key | ||
self.time_skew = time_skew or timedelta(minutes=10) | ||
self.validity = validity or timedelta(minutes=10) | ||
self.signature_hash_algorithm = get_hash_algorithm_from_key(self.ca_private_key) | ||
|
||
@classmethod | ||
def load( | ||
cls, ca_certificate_file: str, ca_private_key_file: str, validity: timedelta, w: timedelta | None = None | ||
) -> Self: | ||
with open(ca_private_key_file, "rb") as fp: | ||
ca_certificate = x509.load_pem_x509_certificate(fp.read()) | ||
|
||
with open(ca_private_key_file, "rb") as fp: | ||
ca_private_key = load_pem_private_key(fp.read()) | ||
if not isinstance(ca_private_key, PrivateKey): | ||
raise ValueError("Unsupported private key algorithm") | ||
|
||
return cls(ca_certificate=ca_certificate, ca_private_key=ca_private_key, validity=validity) | ||
|
||
def sign_csr(self, csr: x509.CertificateSigningRequest, name: str) -> CertificateInformation: | ||
"""Sign CSR with CA private key""" | ||
|
||
now = datetime.now(tz=timezone.utc) | ||
|
||
builder = x509.CertificateBuilder() | ||
builder = builder.subject_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, name)])) | ||
builder = builder.issuer_name(self.ca_certificate.subject) | ||
builder = builder.not_valid_before(now - self.time_skew) | ||
builder = builder.not_valid_after(now + self.validity) | ||
builder = builder.serial_number(x509.random_serial_number()) | ||
builder = builder.public_key(csr.public_key()) | ||
|
||
builder = builder.add_extension(self.KEY_USAGE, critical=True) | ||
builder = builder.add_extension(self.EXTENDED_KEY_USAGE, critical=False) | ||
|
||
builder = builder.add_extension(x509.SubjectKeyIdentifier.from_public_key(csr.public_key()), critical=False) | ||
builder = builder.add_extension( | ||
x509.AuthorityKeyIdentifier.from_issuer_public_key(self.ca_certificate.public_key()), critical=False | ||
) | ||
builder = builder.add_extension(x509.SubjectAlternativeName([x509.DNSName(name)]), critical=False) | ||
builder = builder.add_extension( | ||
x509.BasicConstraints(ca=False, path_length=None), | ||
critical=True, | ||
) | ||
|
||
certificate = builder.sign(private_key=self.ca_private_key, algorithm=self.signature_hash_algorithm) | ||
|
||
return CertificateInformation(cert_chain=[certificate], ca_cert=self.ca_certificate) |
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 was deleted.
Oops, something went wrong.
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,61 @@ | ||
from datetime import timedelta | ||
|
||
from cryptography import x509 | ||
from cryptography.hazmat.primitives import serialization | ||
from cryptography.hazmat.primitives.asymmetric import ec, rsa | ||
from cryptography.hazmat.primitives.asymmetric.ed448 import Ed448PrivateKey | ||
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey | ||
from cryptography.x509.oid import NameOID | ||
|
||
from nodeman.internal_ca import InternalCertificateAuthority | ||
from nodeman.x509 import PrivateKey, generate_x509_csr, verify_x509_csr | ||
from tests.utils import generate_ca_certificate | ||
|
||
|
||
def _test_internal_ca(ca_private_key: PrivateKey) -> None: | ||
"""Test Internal CA""" | ||
|
||
ca_name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Internal Test CA")]) | ||
ca_certificate = generate_ca_certificate(ca_name, ca_private_key) | ||
|
||
validity = timedelta(minutes=10) | ||
ca_client = InternalCertificateAuthority( | ||
ca_certificate=ca_certificate, ca_private_key=ca_private_key, validity=validity | ||
) | ||
|
||
name = "hostname.example.com" | ||
key = ec.generate_private_key(ec.SECP256R1()) | ||
csr = generate_x509_csr(key=key, name=name) | ||
|
||
verify_x509_csr(name=name, csr=csr) | ||
|
||
res = ca_client.sign_csr(csr, name) | ||
x509_certificate_pem = "".join( | ||
[certificate.public_bytes(serialization.Encoding.PEM).decode() for certificate in res.cert_chain] | ||
) | ||
print(x509_certificate_pem) | ||
|
||
|
||
def test_internal_ca_rsa() -> None: | ||
ca_private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048) | ||
return _test_internal_ca(ca_private_key) | ||
|
||
|
||
def test_internal_ca_p256() -> None: | ||
ca_private_key = ec.generate_private_key(ec.SECP256R1()) | ||
return _test_internal_ca(ca_private_key) | ||
|
||
|
||
def test_internal_ca_p384() -> None: | ||
ca_private_key = ec.generate_private_key(ec.SECP384R1()) | ||
return _test_internal_ca(ca_private_key) | ||
|
||
|
||
def test_internal_ca_ed25519() -> None: | ||
ca_private_key = Ed25519PrivateKey.generate() | ||
return _test_internal_ca(ca_private_key) | ||
|
||
|
||
def test_internal_ca_ed448() -> None: | ||
ca_private_key = Ed448PrivateKey.generate() | ||
return _test_internal_ca(ca_private_key) |
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