|
7 | 7 |
|
8 | 8 |
|
9 | 9 | @contextmanager |
10 | | -def cert_gen(emailAddress="emailAddress", |
11 | | - commonName="commonName", |
12 | | - countryName="NT", |
13 | | - localityName="localityName", |
14 | | - stateOrProvinceName="stateOrProvinceName", |
15 | | - organizationName="organizationName", |
16 | | - organizationUnitName="organizationUnitName", |
17 | | - serialNumber=0, |
18 | | - validityStartInSeconds=0, |
19 | | - validityEndInSeconds=None) -> Tuple[str, str]: |
20 | | - if validityEndInSeconds is None: |
21 | | - validityEndInSeconds = int(timedelta(days=3650).total_seconds()) |
| 10 | +def generate_certificate_and_key(email_address="emailAddress", |
| 11 | + common_name="localhost", |
| 12 | + country_name="NT", |
| 13 | + locality_name="localityName", |
| 14 | + state_or_province_name="stateOrProvinceName", |
| 15 | + organization_name="organizationName", |
| 16 | + organization_unit_name="organizationUnitName", |
| 17 | + serial_number=0, |
| 18 | + validity_start_in_seconds=0, |
| 19 | + validity_end_in_seconds=None) -> Tuple[str, str]: |
| 20 | + if validity_end_in_seconds is None: |
| 21 | + validity_end_in_seconds = int(timedelta(days=3650).total_seconds()) |
| 22 | + |
22 | 23 | # can look at generated file using openssl: |
23 | 24 | # openssl x509 -inform pem -in selfsigned.crt -noout -text |
24 | 25 | # create a key pair |
25 | | - k = crypto.PKey() |
26 | | - k.generate_key(crypto.TYPE_RSA, 4096) |
| 26 | + private_key = create_key() |
27 | 27 |
|
28 | 28 | # create a self-signed cert |
29 | | - cert = crypto.X509() |
30 | | - cert.get_subject().C = countryName |
31 | | - cert.get_subject().ST = stateOrProvinceName |
32 | | - cert.get_subject().L = localityName |
33 | | - cert.get_subject().O = organizationName |
34 | | - cert.get_subject().OU = organizationUnitName |
35 | | - cert.get_subject().CN = commonName |
36 | | - cert.get_subject().emailAddress = emailAddress |
37 | | - cert.set_serial_number(serialNumber) |
38 | | - cert.gmtime_adj_notBefore(0) |
39 | | - cert.gmtime_adj_notAfter(validityEndInSeconds) |
40 | | - cert.set_issuer(cert.get_subject()) |
41 | | - cert.set_pubkey(k) |
42 | | - cert.sign(k, 'sha512') |
| 29 | + cert = create_self_signed_certificate(common_name, country_name, email_address, private_key, locality_name, |
| 30 | + organization_name, |
| 31 | + organization_unit_name, serial_number, state_or_province_name, |
| 32 | + validity_end_in_seconds, validity_start_in_seconds) |
43 | 33 |
|
44 | 34 | with tempfile.NamedTemporaryFile() as certificate_file: |
45 | 35 | with tempfile.NamedTemporaryFile() as key_file: |
46 | 36 | certificate_file.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert)) |
47 | 37 | certificate_file.flush() |
48 | 38 |
|
49 | | - key_file.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, k)) |
| 39 | + key_file.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, private_key)) |
50 | 40 | key_file.flush() |
51 | 41 |
|
52 | 42 | yield certificate_file.name, key_file.name |
| 43 | + |
| 44 | + |
| 45 | +def create_key(): |
| 46 | + k = crypto.PKey() |
| 47 | + k.generate_key(crypto.TYPE_RSA, 4096) |
| 48 | + return k |
| 49 | + |
| 50 | + |
| 51 | +def create_self_signed_certificate(common_name, country_name, email_address, private_key, locality_name, |
| 52 | + organization_name, |
| 53 | + organization_unit_name, serial_number, state_or_province_name, |
| 54 | + validity_end_in_seconds, validity_start_in_seconds): |
| 55 | + cert = crypto.X509() |
| 56 | + cert.get_subject().C = country_name |
| 57 | + cert.get_subject().ST = state_or_province_name |
| 58 | + cert.get_subject().L = locality_name |
| 59 | + cert.get_subject().O = organization_name |
| 60 | + cert.get_subject().OU = organization_unit_name |
| 61 | + cert.get_subject().CN = common_name |
| 62 | + cert.get_subject().emailAddress = email_address |
| 63 | + cert.set_serial_number(serial_number) |
| 64 | + cert.gmtime_adj_notBefore(validity_start_in_seconds) |
| 65 | + cert.gmtime_adj_notAfter(validity_end_in_seconds) |
| 66 | + cert.set_issuer(cert.get_subject()) |
| 67 | + cert.set_pubkey(private_key) |
| 68 | + cert.sign(private_key, 'sha512') |
| 69 | + return cert |
0 commit comments