|
| 1 | +import datetime |
| 2 | +import os |
| 3 | + |
| 4 | +from cryptography import x509 |
| 5 | +from cryptography.hazmat import backends |
| 6 | +from cryptography.hazmat.primitives import hashes |
| 7 | +from cryptography.hazmat.primitives import serialization |
| 8 | +from cryptography.hazmat.primitives.asymmetric import rsa |
| 9 | +from cryptography.x509 import oid |
| 10 | + |
| 11 | + |
| 12 | +def _new_cert(issuer=None, is_issuer=False, serial_number=None, **subject): |
| 13 | + backend = backends.default_backend() |
| 14 | + private_key = rsa.generate_private_key( |
| 15 | + public_exponent=65537, key_size=4096, backend=backend |
| 16 | + ) |
| 17 | + public_key = private_key.public_key() |
| 18 | + subject = x509.Name( |
| 19 | + [ |
| 20 | + x509.NameAttribute(getattr(oid.NameOID, key.upper()), value) |
| 21 | + for key, value in subject.items() |
| 22 | + ] |
| 23 | + ) |
| 24 | + builder = ( |
| 25 | + x509.CertificateBuilder() |
| 26 | + .subject_name(subject) |
| 27 | + .public_key(public_key) |
| 28 | + .serial_number(serial_number or int.from_bytes(os.urandom(8), "big")) |
| 29 | + ) |
| 30 | + if issuer: |
| 31 | + issuer_cert, signing_key = issuer |
| 32 | + builder = ( |
| 33 | + builder.issuer_name(issuer_cert.subject) |
| 34 | + .not_valid_before(issuer_cert.not_valid_before) |
| 35 | + .not_valid_after(issuer_cert.not_valid_after) |
| 36 | + ) |
| 37 | + aki_ext = x509.AuthorityKeyIdentifier( |
| 38 | + key_identifier=issuer_cert.extensions.get_extension_for_class( |
| 39 | + x509.SubjectKeyIdentifier |
| 40 | + ).value.digest, |
| 41 | + authority_cert_issuer=[x509.DirectoryName(issuer_cert.subject)], |
| 42 | + authority_cert_serial_number=issuer_cert.serial_number, |
| 43 | + ) |
| 44 | + else: |
| 45 | + signing_key = private_key |
| 46 | + builder = ( |
| 47 | + builder.issuer_name(subject) |
| 48 | + .not_valid_before( |
| 49 | + datetime.datetime.today() - datetime.timedelta(days=1) |
| 50 | + ) |
| 51 | + .not_valid_after( |
| 52 | + datetime.datetime.today() + datetime.timedelta(weeks=1000) |
| 53 | + ) |
| 54 | + ) |
| 55 | + aki_ext = x509.AuthorityKeyIdentifier.from_issuer_public_key( |
| 56 | + public_key |
| 57 | + ) |
| 58 | + if is_issuer: |
| 59 | + builder = ( |
| 60 | + builder.add_extension( |
| 61 | + x509.BasicConstraints(ca=True, path_length=None), |
| 62 | + critical=False, |
| 63 | + ) |
| 64 | + .add_extension( |
| 65 | + x509.SubjectKeyIdentifier.from_public_key(public_key), |
| 66 | + critical=False, |
| 67 | + ) |
| 68 | + .add_extension( |
| 69 | + aki_ext, |
| 70 | + critical=False, |
| 71 | + ) |
| 72 | + ) |
| 73 | + else: |
| 74 | + builder = ( |
| 75 | + builder.add_extension( |
| 76 | + x509.KeyUsage( |
| 77 | + digital_signature=True, |
| 78 | + content_commitment=False, |
| 79 | + key_encipherment=True, |
| 80 | + data_encipherment=False, |
| 81 | + key_agreement=False, |
| 82 | + key_cert_sign=False, |
| 83 | + crl_sign=False, |
| 84 | + encipher_only=False, |
| 85 | + decipher_only=False, |
| 86 | + ), |
| 87 | + critical=False, |
| 88 | + ) |
| 89 | + .add_extension( |
| 90 | + x509.BasicConstraints(ca=False, path_length=None), |
| 91 | + critical=False, |
| 92 | + ) |
| 93 | + .add_extension( |
| 94 | + x509.ExtendedKeyUsage([oid.ExtendedKeyUsageOID.SERVER_AUTH]), |
| 95 | + critical=False, |
| 96 | + ) |
| 97 | + .add_extension( |
| 98 | + x509.SubjectAlternativeName([x509.DNSName("localhost")]), |
| 99 | + critical=False, |
| 100 | + ) |
| 101 | + .add_extension( |
| 102 | + x509.SubjectKeyIdentifier.from_public_key(public_key), |
| 103 | + critical=False, |
| 104 | + ) |
| 105 | + .add_extension( |
| 106 | + aki_ext, |
| 107 | + critical=False, |
| 108 | + ) |
| 109 | + ) |
| 110 | + certificate = builder.sign( |
| 111 | + private_key=signing_key, |
| 112 | + algorithm=hashes.SHA256(), |
| 113 | + backend=backend, |
| 114 | + ) |
| 115 | + return certificate, private_key |
| 116 | + |
| 117 | + |
| 118 | +def _write_cert(path, cert_key_pair, password=None): |
| 119 | + certificate, private_key = cert_key_pair |
| 120 | + if password: |
| 121 | + encryption = serialization.BestAvailableEncryption(password) |
| 122 | + else: |
| 123 | + encryption = serialization.NoEncryption() |
| 124 | + with open(path + ".key.pem", "wb") as f: |
| 125 | + f.write( |
| 126 | + private_key.private_bytes( |
| 127 | + encoding=serialization.Encoding.PEM, |
| 128 | + format=serialization.PrivateFormat.TraditionalOpenSSL, |
| 129 | + encryption_algorithm=encryption, |
| 130 | + ) |
| 131 | + ) |
| 132 | + with open(path + ".cert.pem", "wb") as f: |
| 133 | + f.write( |
| 134 | + certificate.public_bytes( |
| 135 | + encoding=serialization.Encoding.PEM, |
| 136 | + ) |
| 137 | + ) |
| 138 | + |
| 139 | + |
| 140 | +def new_ca(path, **subject): |
| 141 | + cert_key_pair = _new_cert(is_issuer=True, **subject) |
| 142 | + _write_cert(path, cert_key_pair) |
| 143 | + return cert_key_pair |
| 144 | + |
| 145 | + |
| 146 | +def new_cert( |
| 147 | + path, ca_cert_key_pair, password=None, is_issuer=False, **subject |
| 148 | +): |
| 149 | + cert_key_pair = _new_cert( |
| 150 | + issuer=ca_cert_key_pair, is_issuer=is_issuer, **subject |
| 151 | + ) |
| 152 | + _write_cert(path, cert_key_pair, password) |
| 153 | + return cert_key_pair |
| 154 | + |
| 155 | + |
| 156 | +def new_crl(path, issuer, cert): |
| 157 | + issuer_cert, signing_key = issuer |
| 158 | + revoked_cert = ( |
| 159 | + x509.RevokedCertificateBuilder() |
| 160 | + .serial_number(cert[0].serial_number) |
| 161 | + .revocation_date(datetime.datetime.today()) |
| 162 | + .build() |
| 163 | + ) |
| 164 | + builder = ( |
| 165 | + x509.CertificateRevocationListBuilder() |
| 166 | + .issuer_name(issuer_cert.subject) |
| 167 | + .last_update(datetime.datetime.today()) |
| 168 | + .next_update(datetime.datetime.today() + datetime.timedelta(days=1)) |
| 169 | + .add_revoked_certificate(revoked_cert) |
| 170 | + ) |
| 171 | + crl = builder.sign(private_key=signing_key, algorithm=hashes.SHA256()) |
| 172 | + with open(path + ".crl.pem", "wb") as f: |
| 173 | + f.write(crl.public_bytes(encoding=serialization.Encoding.PEM)) |
| 174 | + |
| 175 | + |
| 176 | +def main(): |
| 177 | + ca = new_ca( |
| 178 | + "ca", |
| 179 | + country_name="CA", |
| 180 | + state_or_province_name="Ontario", |
| 181 | + locality_name="Toronto", |
| 182 | + organization_name="MagicStack Inc.", |
| 183 | + organizational_unit_name="asyncpg tests", |
| 184 | + common_name="asyncpg test root ca", |
| 185 | + email_address="[email protected]", |
| 186 | + ) |
| 187 | + server = new_cert( |
| 188 | + "server", |
| 189 | + ca, |
| 190 | + country_name="CA", |
| 191 | + state_or_province_name="Ontario", |
| 192 | + organization_name="MagicStack Inc.", |
| 193 | + organizational_unit_name="asyncpg tests", |
| 194 | + common_name="localhost", |
| 195 | + email_address="[email protected]", |
| 196 | + serial_number=4096, |
| 197 | + ) |
| 198 | + new_crl('server', ca, server) |
| 199 | + |
| 200 | + |
| 201 | +if __name__ == "__main__": |
| 202 | + main() |
0 commit comments