11import base64
2+ from datetime import datetime , timedelta
23from socket import gethostname
34
4- from OpenSSL import crypto
5+ from cryptography import x509
6+ from cryptography .hazmat .primitives import hashes , serialization
7+ from cryptography .hazmat .primitives .asymmetric import rsa
8+ from cryptography .x509 .oid import NameOID
59
610from tests .integration import basetest
711
@@ -15,28 +19,40 @@ def setUp(self):
1519 self .certificate = self ._create_self_signed_cert ()
1620
1721 def _create_self_signed_cert (self ):
22+ # Generate a private key
23+ private_key = rsa .generate_private_key (
24+ public_exponent = 65537 ,
25+ key_size = 2048 ,
26+ )
27+
28+ # Create a self-signed certificate
29+ subject = issuer = x509 .Name ([
30+ x509 .NameAttribute (NameOID .COUNTRY_NAME , "NL" ),
31+ x509 .NameAttribute (NameOID .STATE_OR_PROVINCE_NAME , "Rotterdam" ),
32+ x509 .NameAttribute (NameOID .LOCALITY_NAME , "Rotterdam" ),
33+ x509 .NameAttribute (NameOID .ORGANIZATION_NAME , "Mendix" ),
34+ x509 .NameAttribute (NameOID .ORGANIZATIONAL_UNIT_NAME , "Mendix" ),
35+ x509 .NameAttribute (NameOID .COMMON_NAME , gethostname ()),
36+ ])
37+ cert = x509 .CertificateBuilder ().subject_name (
38+ subject
39+ ).issuer_name (
40+ issuer
41+ ).public_key (
42+ private_key .public_key ()
43+ ).serial_number (
44+ 1000
45+ ).not_valid_before (
46+ datetime .utcnow ()
47+ ).not_valid_after (
48+ datetime .utcnow () + timedelta (days = 365 * 10 )
49+ ).add_extension (
50+ x509 .BasicConstraints (ca = True , path_length = None ), critical = True ,
51+ ).sign (private_key , hashes .SHA256 ())
52+
53+ cert_pem = cert .public_bytes (serialization .Encoding .PEM )
1854
19- # Create a key pair
20- k = crypto .PKey ()
21- k .generate_key (crypto .TYPE_RSA , 1024 )
22-
23- # Create a self-signed cert
24- cert = crypto .X509 ()
25- cert .get_subject ().C = "NL"
26- cert .get_subject ().ST = "Rotterdam"
27- cert .get_subject ().L = "Rotterdam"
28- cert .get_subject ().O = "Mendix" # noqa: E741
29- cert .get_subject ().OU = "Mendix"
30- cert .get_subject ().CN = gethostname ()
31- cert .set_serial_number (1000 )
32- cert .gmtime_adj_notBefore (0 )
33- cert .gmtime_adj_notAfter (10 * 365 * 24 * 60 * 60 )
34- cert .set_issuer (cert .get_subject ())
35- cert .set_pubkey (k )
36- cert .sign (k , "sha1" )
37-
38- # Return a .PEM certificate
39- return crypto .dump_certificate (crypto .FILETYPE_PEM , cert )
55+ return cert_pem
4056
4157 def test_certificate_authorities (self ):
4258 self .stage_container (
0 commit comments