forked from fportantier/vulpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathca-create.py
executable file
·60 lines (47 loc) · 1.97 KB
/
ca-create.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
#!/usr/bin/env python3
import datetime
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.primitives import hashes
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend()
)
public_key = private_key.public_key()
pem_private = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
)
pem_public = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
with open('/tmp/ca.key', 'wb') as out:
out.write(pem_private)
with open('/tmp/ca.pub', 'wb') as out:
out.write(pem_public)
print('Created files in /tmp/ca.key /tmp/ca.pub /tmp/ca.cert')
# Various details about who we are. For a self-signed certificate the
# subject and issuer are always the same.
subject = issuer = x509.Name([
x509.NameAttribute(NameOID.COUNTRY_NAME, "AR"),
x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "BA"),
x509.NameAttribute(NameOID.LOCALITY_NAME, "Buenos Aires"),
x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Vulpy by Securetia"),
x509.NameAttribute(NameOID.COMMON_NAME, "www.securetia.com"),
])
cert = x509.CertificateBuilder().subject_name(subject)
cert = cert.issuer_name(issuer)
cert = cert.public_key(public_key)
cert = cert.serial_number(x509.random_serial_number())
cert = cert.not_valid_before(datetime.datetime.utcnow())
cert = cert.not_valid_after(datetime.datetime.utcnow() + datetime.timedelta(days=30))
cert = cert.sign(private_key, hashes.SHA256(), default_backend())
# Write our certificate out to disk.
with open('/tmp/ca.cert', 'wb') as out:
out.write(cert.public_bytes(serialization.Encoding.PEM))