|
1 | 1 | import os |
2 | 2 | import ssl |
| 3 | +from datetime import datetime, timedelta, timezone |
3 | 4 |
|
4 | 5 | import grpc |
5 | | -from OpenSSL import crypto |
| 6 | +from cryptography import x509 |
| 7 | +from cryptography.hazmat.primitives import hashes, serialization |
| 8 | +from cryptography.hazmat.primitives.asymmetric import rsa |
| 9 | +from cryptography.x509.oid import NameOID |
6 | 10 |
|
7 | 11 |
|
8 | 12 | class Certs: |
9 | 13 | server_type = 'grpc' |
10 | 14 |
|
11 | 15 | @classmethod |
12 | 16 | def create_certificates(cls): |
13 | | - # create a key pair |
14 | | - k = crypto.PKey() |
15 | | - k.generate_key(crypto.TYPE_RSA, 4096) |
16 | | - |
17 | | - # create a self-signed cert |
18 | | - cert = crypto.X509() |
19 | | - cert.get_subject().organizationName = 'Dapr' |
20 | | - cert.get_subject().commonName = 'localhost' |
21 | | - cert.gmtime_adj_notBefore(0) |
22 | | - cert.gmtime_adj_notAfter(24 * 60 * 60) |
23 | | - cert.set_issuer(cert.get_subject()) |
24 | | - cert.set_pubkey(k) |
| 17 | + key = rsa.generate_private_key(public_exponent=65537, key_size=4096) |
| 18 | + |
| 19 | + subject = x509.Name( |
| 20 | + [ |
| 21 | + x509.NameAttribute(NameOID.ORGANIZATION_NAME, 'Dapr'), |
| 22 | + x509.NameAttribute(NameOID.COMMON_NAME, 'localhost'), |
| 23 | + ] |
| 24 | + ) |
| 25 | + now = datetime.now(timezone.utc) |
| 26 | + cert_builder = ( |
| 27 | + x509.CertificateBuilder() |
| 28 | + .subject_name(subject) |
| 29 | + .issuer_name(subject) |
| 30 | + .public_key(key.public_key()) |
| 31 | + .serial_number(x509.random_serial_number()) |
| 32 | + .not_valid_before(now) |
| 33 | + .not_valid_after(now + timedelta(days=1)) |
| 34 | + ) |
25 | 35 |
|
26 | 36 | if cls.server_type == 'http': |
27 | | - cert.add_extensions([crypto.X509Extension(b'subjectAltName', False, b'DNS:localhost')]) |
| 37 | + localhost_san = x509.SubjectAlternativeName([x509.DNSName('localhost')]) |
| 38 | + cert_builder = cert_builder.add_extension(localhost_san, critical=False) |
28 | 39 |
|
29 | | - cert.sign(k, 'sha512') |
| 40 | + cert = cert_builder.sign(key, hashes.SHA512()) |
30 | 41 |
|
31 | | - with open(cls.get_cert_path(), 'wt') as f_cert: |
32 | | - f_cert.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert).decode('utf-8')) |
| 42 | + cert_pem = cert.public_bytes(serialization.Encoding.PEM) |
| 43 | + key_pem = key.private_bytes( |
| 44 | + encoding=serialization.Encoding.PEM, |
| 45 | + format=serialization.PrivateFormat.PKCS8, |
| 46 | + encryption_algorithm=serialization.NoEncryption(), |
| 47 | + ) |
33 | 48 |
|
34 | | - with open(cls.get_pk_path(), 'wt') as f_key: |
35 | | - f_key.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, k).decode('utf-8')) |
| 49 | + with open(cls.get_cert_path(), 'wb') as f_cert: |
| 50 | + f_cert.write(cert_pem) |
| 51 | + |
| 52 | + with open(cls.get_pk_path(), 'wb') as f_key: |
| 53 | + f_key.write(key_pem) |
36 | 54 |
|
37 | 55 | @classmethod |
38 | 56 | def get_pk_path(cls): |
|
0 commit comments