|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from cryptography import x509 |
| 4 | +from cryptography.hazmat import backends |
| 5 | +from cryptography.hazmat.primitives import hashes |
| 6 | +from cryptography.hazmat.primitives import serialization |
| 7 | +from cryptography.hazmat.primitives.asymmetric import rsa |
| 8 | + |
| 9 | +import datetime |
| 10 | +import argparse |
| 11 | +import sys |
| 12 | +import ipaddress |
| 13 | + |
| 14 | + |
| 15 | +def get_args(): |
| 16 | + parser = argparse.ArgumentParser( |
| 17 | + prog=sys.argv[0], |
| 18 | + description='Tool to generate a x509 CA root, server and client certs') |
| 19 | + parser.add_argument('-c', '--common-name', action='store', |
| 20 | + default='pykmip.local', |
| 21 | + help='Set the common name for the server-side cert') |
| 22 | + return parser.parse_args() |
| 23 | + |
| 24 | + |
| 25 | +def create_rsa_private_key(key_size=2048, public_exponent=65537): |
| 26 | + private_key = rsa.generate_private_key( |
| 27 | + public_exponent=public_exponent, |
| 28 | + key_size=key_size, |
| 29 | + backend=backends.default_backend() |
| 30 | + ) |
| 31 | + return private_key |
| 32 | + |
| 33 | + |
| 34 | +def create_self_signed_certificate(subject_name, |
| 35 | + private_key, |
| 36 | + days_valid=36500): |
| 37 | + subject = x509.Name([ |
| 38 | + x509.NameAttribute(x509.NameOID.ORGANIZATION_NAME, u"Scality"), |
| 39 | + x509.NameAttribute(x509.NameOID.COMMON_NAME, subject_name) |
| 40 | + ]) |
| 41 | + certificate = x509.CertificateBuilder().subject_name( |
| 42 | + subject |
| 43 | + ).issuer_name( |
| 44 | + subject |
| 45 | + ).public_key( |
| 46 | + private_key.public_key() |
| 47 | + ).serial_number( |
| 48 | + x509.random_serial_number() |
| 49 | + ).not_valid_before( |
| 50 | + datetime.datetime.utcnow() |
| 51 | + ).not_valid_after( |
| 52 | + datetime.datetime.utcnow() + datetime.timedelta(days=days_valid) |
| 53 | + ).add_extension( |
| 54 | + x509.BasicConstraints(True, None), |
| 55 | + critical=True |
| 56 | + ).sign(private_key, hashes.SHA256(), backends.default_backend()) |
| 57 | + |
| 58 | + return certificate |
| 59 | + |
| 60 | + |
| 61 | +def create_certificate(subject_name, |
| 62 | + private_key, |
| 63 | + signing_certificate, |
| 64 | + signing_key, |
| 65 | + days_valid=36500, |
| 66 | + client_auth=False, |
| 67 | + alt_names=[]): |
| 68 | + subject = x509.Name([ |
| 69 | + x509.NameAttribute(x509.NameOID.ORGANIZATION_NAME, u"Scality"), |
| 70 | + x509.NameAttribute(x509.NameOID.COMMON_NAME, subject_name) |
| 71 | + ]) |
| 72 | + builder = x509.CertificateBuilder().subject_name( |
| 73 | + subject |
| 74 | + ).issuer_name( |
| 75 | + signing_certificate.subject |
| 76 | + ).public_key( |
| 77 | + private_key.public_key() |
| 78 | + ).serial_number( |
| 79 | + x509.random_serial_number() |
| 80 | + ).not_valid_before( |
| 81 | + datetime.datetime.utcnow() |
| 82 | + ).not_valid_after( |
| 83 | + datetime.datetime.utcnow() + datetime.timedelta(days=days_valid) |
| 84 | + ) |
| 85 | + |
| 86 | + if client_auth: |
| 87 | + builder = builder.add_extension( |
| 88 | + x509.ExtendedKeyUsage([x509.ExtendedKeyUsageOID.CLIENT_AUTH]), |
| 89 | + critical=True |
| 90 | + ) |
| 91 | + |
| 92 | + if alt_names: |
| 93 | + builder = builder.add_extension( |
| 94 | + x509.SubjectAlternativeName(alt_names), |
| 95 | + critical=False |
| 96 | + ) |
| 97 | + |
| 98 | + certificate = builder.sign( |
| 99 | + signing_key, |
| 100 | + hashes.SHA256(), |
| 101 | + backends.default_backend() |
| 102 | + ) |
| 103 | + return certificate |
| 104 | + |
| 105 | + |
| 106 | +def main(common_name): |
| 107 | + root_key = create_rsa_private_key() |
| 108 | + root_certificate = create_self_signed_certificate( |
| 109 | + u"Root CA", |
| 110 | + root_key |
| 111 | + ) |
| 112 | + |
| 113 | + server_key = create_rsa_private_key() |
| 114 | + server_certificate = create_certificate( |
| 115 | + common_name, |
| 116 | + server_key, |
| 117 | + root_certificate, |
| 118 | + root_key, |
| 119 | + alt_names=[ |
| 120 | + x509.DNSName(common_name), |
| 121 | + x509.DNSName('*.' + common_name), |
| 122 | + x509.DNSName('localhost'), |
| 123 | + ] |
| 124 | + + [x509.IPAddress(ipaddress.ip_address('127.0.0.' + str(ip))) for ip in range(1, 51)] |
| 125 | + ) |
| 126 | + |
| 127 | + john_doe_client_key = create_rsa_private_key() |
| 128 | + john_doe_client_certificate = create_certificate( |
| 129 | + u"John Doe", |
| 130 | + john_doe_client_key, |
| 131 | + root_certificate, |
| 132 | + root_key, |
| 133 | + client_auth=True |
| 134 | + ) |
| 135 | + |
| 136 | + with open("certs/kmip-ca.pem", "wb") as f: |
| 137 | + f.write( |
| 138 | + root_certificate.public_bytes( |
| 139 | + serialization.Encoding.PEM |
| 140 | + ) |
| 141 | + ) |
| 142 | + with open("certs/kmip-key.pem", "wb") as f: |
| 143 | + f.write(server_key.private_bytes( |
| 144 | + encoding=serialization.Encoding.PEM, |
| 145 | + format=serialization.PrivateFormat.PKCS8, |
| 146 | + encryption_algorithm=serialization.NoEncryption() |
| 147 | + )) |
| 148 | + with open("certs/kmip-cert.pem", "wb") as f: |
| 149 | + f.write( |
| 150 | + server_certificate.public_bytes( |
| 151 | + serialization.Encoding.PEM |
| 152 | + ) |
| 153 | + ) |
| 154 | + with open("certs/kmip-client-key.pem", "wb") as f: |
| 155 | + f.write(john_doe_client_key.private_bytes( |
| 156 | + encoding=serialization.Encoding.PEM, |
| 157 | + format=serialization.PrivateFormat.PKCS8, |
| 158 | + encryption_algorithm=serialization.NoEncryption() |
| 159 | + )) |
| 160 | + with open("certs/kmip-client-cert.pem", "wb") as f: |
| 161 | + f.write( |
| 162 | + john_doe_client_certificate.public_bytes( |
| 163 | + serialization.Encoding.PEM |
| 164 | + ) |
| 165 | + ) |
| 166 | + |
| 167 | + |
| 168 | +if __name__ == '__main__': |
| 169 | + args = get_args() |
| 170 | + main(args.common_name) |
0 commit comments