|
1 | 1 | import base64 |
2 | 2 | import json |
3 | 3 | import os |
| 4 | +from datetime import datetime, timedelta |
4 | 5 | from socket import gethostname |
5 | 6 | from unittest import TestCase, mock |
6 | 7 |
|
| 8 | +from cryptography import x509 |
| 9 | +from cryptography.hazmat.primitives import hashes, serialization |
| 10 | +from cryptography.hazmat.primitives.asymmetric import rsa |
| 11 | +from cryptography.hazmat.primitives.serialization import pkcs12 |
| 12 | +from cryptography.x509.oid import NameOID |
| 13 | + |
7 | 14 | from buildpack import util |
8 | 15 | from buildpack.core import runtime, security |
9 | 16 | from lib.m2ee.version import MXVersion |
10 | | -from OpenSSL import crypto |
11 | 17 |
|
12 | 18 |
|
13 | 19 | class M2EEMock: |
@@ -87,34 +93,49 @@ def test_custom_runtime_setting_is_set(self): |
87 | 93 | ) |
88 | 94 |
|
89 | 95 |
|
90 | | -def _create_self_signed_cert(): |
91 | | - # Create a key pair |
92 | | - k = crypto.PKey() |
93 | | - k.generate_key(crypto.TYPE_RSA, 1024) |
94 | | - |
95 | | - # Create a self-signed cert |
96 | | - cert = crypto.X509() |
97 | | - cert.get_subject().C = "NL" |
98 | | - cert.get_subject().ST = "Rotterdam" |
99 | | - cert.get_subject().L = "Rotterdam" |
100 | | - cert.get_subject().O = "Mendix" # noqa: E741 |
101 | | - cert.get_subject().OU = "Mendix" |
102 | | - cert.get_subject().CN = gethostname() |
103 | | - cert.set_serial_number(1000) |
104 | | - cert.gmtime_adj_notBefore(0) |
105 | | - cert.gmtime_adj_notAfter(10 * 365 * 24 * 60 * 60) |
106 | | - cert.set_issuer(cert.get_subject()) |
107 | | - cert.set_pubkey(k) |
108 | | - cert.sign(k, "sha1") |
109 | | - |
110 | | - # Create a P12 container |
111 | | - p12 = crypto.PKCS12() |
112 | | - p12.set_certificate(cert) |
113 | | - |
114 | | - return p12.export() |
| 96 | +class TestClientCertificateConfiguration(TestCase): |
| 97 | + def _create_self_signed_cert(): # pylint: disable=no-method-argument |
| 98 | + # Generate a private key |
| 99 | + private_key = rsa.generate_private_key( |
| 100 | + public_exponent=65537, |
| 101 | + key_size=2048, |
| 102 | + ) |
115 | 103 |
|
| 104 | + # Create a self-signed certificate |
| 105 | + subject = issuer = x509.Name([ |
| 106 | + x509.NameAttribute(NameOID.COUNTRY_NAME, "NL"), |
| 107 | + x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "Rotterdam"), |
| 108 | + x509.NameAttribute(NameOID.LOCALITY_NAME, "Rotterdam"), |
| 109 | + x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Mendix"), |
| 110 | + x509.NameAttribute(NameOID.ORGANIZATIONAL_UNIT_NAME, "Mendix"), |
| 111 | + x509.NameAttribute(NameOID.COMMON_NAME, gethostname()), |
| 112 | + ]) |
| 113 | + cert = x509.CertificateBuilder().subject_name( |
| 114 | + subject |
| 115 | + ).issuer_name( |
| 116 | + issuer |
| 117 | + ).public_key( |
| 118 | + private_key.public_key() |
| 119 | + ).serial_number( |
| 120 | + 1000 |
| 121 | + ).not_valid_before( |
| 122 | + datetime.utcnow() |
| 123 | + ).not_valid_after( |
| 124 | + datetime.utcnow() + timedelta(days=365*10) |
| 125 | + ).add_extension( |
| 126 | + x509.BasicConstraints(ca=True, path_length=None), critical=True, |
| 127 | + ).sign(private_key, hashes.SHA256()) |
| 128 | + |
| 129 | + # Serialize private key and certificate to a PKCS12 container |
| 130 | + p12 = pkcs12.serialize_key_and_certificates( |
| 131 | + name=b"selfsigned", |
| 132 | + key=private_key, |
| 133 | + cert=cert, |
| 134 | + cas=None, |
| 135 | + encryption_algorithm=serialization.NoEncryption() |
| 136 | + ) |
116 | 137 |
|
117 | | -class TestClientCertificateConfiguration(TestCase): |
| 138 | + return p12 |
118 | 139 |
|
119 | 140 | CERTIFICATE_ENV = { |
120 | 141 | "CLIENT_CERTIFICATES": json.dumps( |
|
0 commit comments