-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssl_certificate_generator.py
177 lines (142 loc) · 4.42 KB
/
ssl_certificate_generator.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import socket
import uuid
from typing import List
from OpenSSL import crypto
def create_ca_certificate(
passphrase: str,
common_name='Develop CA',
organization_name='Develop LLC',
organizational_unit_name='Default CA Deployment',
days=365,
):
"""Generate CA certificate
Chrome manual:
chrome://settings/certificates
Authorities -> import -> ca.crt
☑ Trust this certificate for identifying websites
☑ Trust this certificate for identifying email users
☑ Trust this certificate for identifying software makers
[ OK ]
"""
passphrase = passphrase.encode()
key = crypto.PKey()
key.generate_key(crypto.TYPE_RSA, 2048)
serialnumber= int(uuid.uuid4())
cert = crypto.X509()
subject = cert.get_subject()
subject.CN = common_name
subject.O = organization_name
subject.OU = organizational_unit_name
cert.set_version(2)
cert.set_issuer(subject)
cert.set_serial_number(serialnumber)
cert.gmtime_adj_notBefore(0)
cert.gmtime_adj_notAfter(days * 24 * 60 * 60)
cert.add_extensions([
crypto.X509Extension(b'basicConstraints', True, b'CA:TRUE, pathlen:0'),
])
cert.set_pubkey(key)
cert.sign(key, 'sha512')
with open('ca.key', 'wb') as fo:
fo.write(crypto.dump_privatekey(
crypto.FILETYPE_PEM, key, passphrase=passphrase
))
with open('ca.crt', 'wb') as fo:
fo.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert))
def _domain_to_record(domain: str):
try:
socket.inet_aton(domain)
except socket.error:
return f'DNS:{domain}'
else:
return f'IP:{domain}'
def create_domain_certificate(
passphrase: str,
domains: List[str],
days=365,
C='RU', ST='Moscow', L='Moscow',
O='Develop LLC', CN='Develop cert',
email_address='[email protected]',
):
"""Generate website certificate
Chrome manual:
chrome://settings/certificates
Your certificates -> import -> cert.p12
[ enter your passphrase ]
[ OK ]
Webpack dev server:
devServer: {
server: {
type: 'https',
options: {
ca: fs.readFileSync('ca.crt'),
key: fs.readFileSync('cert.key'),
cert: fs.readFileSync('cert.crt'),
passphrase: ...your passphrase...,
},
}
}
Nginx:
http {
server {
listen 443 ssl;
ssl_certificate cert.crt;
ssl_certificate_key cert.key;
}
}
"""
passphrase = passphrase.encode()
with open('ca.crt', 'rb') as fo:
ca_cert = crypto.load_certificate(
crypto.FILETYPE_PEM, fo.read())
with open('ca.key', 'rb') as fo:
ca_key = crypto.load_privatekey(
crypto.FILETYPE_PEM, fo.read(),
passphrase=passphrase)
cert_req = crypto.X509Req()
subject = cert_req.get_subject()
subject.C = C
subject.ST = ST
subject.L = L
subject.O = O
subject.CN = CN
subject.emailAddress = email_address
key = crypto.PKey()
key.generate_key(crypto.TYPE_RSA, 2048)
cert = crypto.X509()
cert.set_version(2)
cert.gmtime_adj_notBefore(0)
cert.gmtime_adj_notAfter(days * 24 * 60 * 60)
cert.set_issuer(ca_cert.get_subject())
cert.set_subject(subject)
cert.add_extensions([
crypto.X509Extension(
b'subjectAltName', False,
','.join([
_domain_to_record(domain)
for domain in domains
]).encode()
),
])
cert.set_pubkey(key)
cert.sign(ca_key, 'sha256')
with open('cert.crt', 'wb') as fo:
fo.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert))
with open('cert.key', 'wb') as fo:
fo.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, key))
# Generate importable cert for browser
pkcs = crypto.PKCS12()
pkcs.set_privatekey(key)
pkcs.set_certificate(cert)
with open('cert.p12', 'wb') as file:
file.write(pkcs.export(passphrase=passphrase))
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.connect(('8.8.8.8', 0))
develop_ip = s.getsockname()[0]
passphrase = 'develop'
create_ca_certificate(passphrase)
create_domain_certificate(passphrase, domains=[
develop_ip,
'127.0.0.1',
'develop.local'
])