-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathgenerate_selfsigned_cert.py
More file actions
54 lines (42 loc) · 1.47 KB
/
Copy pathgenerate_selfsigned_cert.py
File metadata and controls
54 lines (42 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
# SOURCE: https://stackoverflow.com/a/60804101/5909792
from OpenSSL import crypto
def cert_gen(
email_address="emailAddress",
common_name="commonName",
country_name="NT",
locality_name="localityName",
state_or_province_name="stateOrProvinceName",
organization_name="organizationName",
organization_unit_name="organizationUnitName",
serial_number=0,
validity_end_in_seconds=10 * 365 * 24 * 60 * 60,
key_file="key.pem",
cert_file="cert.pem",
) -> None:
# Create a key pair
k = crypto.PKey()
k.generate_key(crypto.TYPE_RSA, 4096)
# Create a self-signed cert
cert = crypto.X509()
cert.get_subject().C = country_name
cert.get_subject().ST = state_or_province_name
cert.get_subject().L = locality_name
cert.get_subject().O = organization_name
cert.get_subject().OU = organization_unit_name
cert.get_subject().CN = common_name
cert.get_subject().emailAddress = email_address
cert.set_serial_number(serial_number)
cert.gmtime_adj_notBefore(0)
cert.gmtime_adj_notAfter(validity_end_in_seconds)
cert.set_issuer(cert.get_subject())
cert.set_pubkey(k)
cert.sign(k, "sha512")
with open(cert_file, "wb") as f:
f.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert))
with open(key_file, "wb") as f:
f.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, k))
if __name__ == "__main__":
cert_gen()