-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateCert.py
More file actions
151 lines (127 loc) · 5.7 KB
/
Copy pathcreateCert.py
File metadata and controls
151 lines (127 loc) · 5.7 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
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
# Author Vysakh P Pillai
# Includes some code from Simon Davy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from datetime import datetime, timedelta
import ipaddress, socket, binascii
from string import Template
from netifaces import interfaces, ifaddresses, AF_INET
from random import randint
def generate_selfsigned_cert(hostname, ip_addresses=None, key=None, der=True):
"""Generates self signed certificate for a hostname, and optional IP addresses."""
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives.asymmetric import ec
# Generate our key
if key is None:
key = ec.generate_private_key(
ec.SECP256R1(),
default_backend()
)
# key = rsa.generate_private_key(
# public_exponent=65537,
# key_size=2048,
# backend=default_backend(),
# )
name = x509.Name([
x509.NameAttribute(NameOID.COMMON_NAME, hostname)
])
# best practice seem to be to include the hostname in the SAN, which *SHOULD* mean COMMON_NAME is ignored.
alt_names = [x509.DNSName(hostname)]
# allow addressing by IP, for when you don't have real DNS (common in most testing scenarios
if ip_addresses:
for addr in ip_addresses:
# openssl wants DNSnames for ips...
alt_names.append(x509.DNSName(addr))
# ... whereas golang's crypto/tls is stricter, and needs IPAddresses
# note: older versions of cryptography do not understand ip_address objects
alt_names.append(x509.IPAddress(ipaddress.ip_address(addr)))
san = x509.SubjectAlternativeName(alt_names)
# path_len=0 means this cert can only sign itself, not other certs.
basic_contraints = x509.BasicConstraints(ca=True, path_length=0)
now = datetime.utcnow()
cert = (
x509.CertificateBuilder()
.subject_name(name)
.issuer_name(name)
.public_key(key.public_key())
.serial_number(randint(0,2147483647))
.not_valid_before(now)
.not_valid_after(now + timedelta(days=10*365))
.add_extension(basic_contraints, False)
.add_extension(san, False)
.sign(key, hashes.SHA256(), default_backend())
)
cert_pem = cert.public_bytes(encoding=serialization.Encoding.PEM)
key_pem = key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption(),
)
if der:
cert_der = cert.public_bytes(encoding=serialization.Encoding.DER)
key_der = key.private_bytes(
encoding=serialization.Encoding.DER,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption(),
)
return cert_pem, key_pem, cert_der, key_der
else:
return cert_pem, key_pem
def make_sublist_group(lst: list, grp: int) -> list:
"""
Group list elements into sublists.
make_sublist_group([1, 2, 3, 4, 5, 6, 7], 3) = [[1, 2, 3], [4, 5, 6], 7]
"""
return [lst[i:i+grp] for i in range(0, len(lst), grp)]
def do_convension(content: bytes) -> str:
hexstr = binascii.hexlify(content).decode("UTF-8")
hexstr = hexstr.upper()
array = ["0x" + hexstr[i:i + 2] + "" for i in range(0, len(hexstr), 2)]
array = make_sublist_group(array, 10)
return sum(len(a) for a in array ), "\n".join([", ".join(e) + ", " for e in array])
#Creating a Certificate
hostname = socket.gethostname()
cert_pem,key_pem, cert_der, key_der=generate_selfsigned_cert(hostname,ip_addresses=["192.168.1.51"])
with open('certnKey.crt', 'w') as file:
file.write(cert_pem.decode("UTF-8"))
file.write(key_pem.decode("UTF-8"))
with open('cert.der', 'wb') as file:
file.write(cert_der)
with open('key.der', 'wb') as file:
file.write(key_der)
cer_length , cerArray=do_convension(cert_der)
certHeaderTemplate=Template("""
/*GENERATED FILE*/
#ifndef _CERTS_TEST_H_
#define _CERTS_TEST_H_
static const unsigned char ca_cert_der[] =
{
$caCert
};
/* size is $ceCertSize */
static const int sizeof_ca_cert_der = sizeof(ca_cert_der);
#endif /*_CERTS_TEST_H_*/
""")
with open('cert_header.h', 'w') as file:
file.write(certHeaderTemplate.substitute(caCert=cerArray,ceCertSize=cer_length))