import os from textwrap import dedent from OpenSSL import crypto def create_certificate(target_dir, ip_address): """Generate a cert and key file incl. IP SAN for `ip_address` Creates a cert.pem and key.pem file signed with a known ca cert. The generated cert will contain a IP SAN (subject alternative name) that includes the ip address of the server. This is required for log-forwarding. :return: tuple containing generated cert, key filepath pair """ ip_address = 'IP:{}'.format(ip_address) key = crypto.PKey() key.generate_key(crypto.TYPE_RSA, 2048) csr_contents = generate_csr(target_dir, key, ip_address) req = crypto.load_certificate_request(crypto.FILETYPE_PEM, csr_contents) ca_cert = crypto.load_certificate( crypto.FILETYPE_PEM, ca_pem_contents) ca_key = crypto.load_privatekey( crypto.FILETYPE_PEM, ca_key_pem_contents) cert = crypto.X509() cert.set_version(0x2) cert.set_subject(req.get_subject()) cert.set_serial_number(1) cert.gmtime_adj_notBefore(0) cert.gmtime_adj_notAfter(24 * 60 * 60) cert.set_issuer(ca_cert.get_subject()) cert.set_pubkey(req.get_pubkey()) cert.add_extensions([ crypto.X509Extension('subjectAltName', False, ip_address), crypto.X509Extension( 'extendedKeyUsage', False, 'clientAuth, serverAuth'), crypto.X509Extension( 'keyUsage', True, 'keyEncipherment'), ]) cert.sign(ca_key, "sha256") cert_filepath = os.path.join(target_dir, 'cert.pem') with open(cert_filepath, 'wt') as f: f.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert)) key_filepath = os.path.join(target_dir, 'key.pem') with open(key_filepath, 'wt') as f: f.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, key)) return (cert_filepath, key_filepath) def generate_csr(target_dir, key, ip_address): req = crypto.X509Req() req.set_version(0x2) req.get_subject().CN = "anyServer" # Add the IP SAN req.add_extensions([ crypto.X509Extension("subjectAltName", False, ip_address) ]) req.set_pubkey(key) req.sign(key, "sha256") return crypto.dump_certificate_request(crypto.FILETYPE_PEM, req) ca_pem_contents = dedent("""\ -----BEGIN CERTIFICATE----- MIIEFTCCAn2gAwIBAgIBBzANBgkqhkiG9w0BAQsFADAjMRIwEAYDVQQDEwlhbnl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SLe6TLD+Jjv05Mc Ca7NKq/n25VTDNNaXl5MCNZ048m/GGHfktxxCddaF2grhC5HTUetwkq026PE0Wcq P+cDrIq6uTA25QqyBYistSa/7z2o0NBi56ySRqxlP2J2TPFZyOb+ZiA4EgYY5no5 u2E+WuKZLVWl7eaQYOHgfYzFf3CvalSBwIjNynRwD/2Ebk7K29GPrIugb3V2+Vwh rltUXOHUkFGjEHIhr8zixfCxh5OzPJMnJwCZZRYzMO0/0Gw7ll9DmH0= -----END CERTIFICATE----- """) ca_key_pem_contents = dedent("""\ -----BEGIN RSA PRIVATE KEY----- MIIG4wIBAAKCAYEAp+jsWN98gIqwAaBOFGmSQTp0DkSMwtduUe9RNkxuYEh4xygO 9C9At3YMloVvcHMoVG1wbs/Dnqdie5B787lD8NgFlyPmNnNGn1lan4+ZLslKRoV7 Kspcn+zQzxbPIcBgOmQhUt6FAL1/GsLFbnJJcRnmTV2TSmRYpVyml9Wd7KDkZihW c1Xw0ag7qH2dhNLZj4fQuYEPKrMI3QZ064/tKwp7zXpDtGjQ+gJZAR1PvO6b3Gsd LlBej2JF8T4pCpNX7QkEiCOaAeZLALUaXGbGJeYU91P7dQzgO3N+UbiMe6IJHFz/ Y2eidrZJl/hjLVBdvShBJ1Qobd9lQ1/gt8kupW30AeKA7WKd8F0WPy101u101K4a FweJ5GG8OfYIrOQq+8w7VKYlhVWIMv5G8WpZY0jODd8NonbX8SR2I03yvfNduKmw gW+AOGTQSRNo8v/5UiHCDFyb9S10LU6keKztY+kwbq6IRTZlGKct2b+4J9gpH1+k HzRSI/HpSk+nXfkVAgMBAAECggGAJigjVYrr8xYRKz1voOngx5vt9bQUPM7SDiKR VQKHbq/panCq/Uijr01PTQFjsq0otA7upu/l527oTWYnFNq8GsYsdw08apFFsj6O /oWWbPBnRaFdvPqhk+IwDW+EgIoEFCDfBcL1fJaThNRQI2orUF1vXZNvPk+RaXql jQmJStXBMYnnI2ybPjm53O821ZFIyXo2r4Epni1zTS8DcOiTH93RBn/L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