|
| 1 | +import datetime |
| 2 | +import os |
| 3 | + |
| 4 | +from cryptography import x509 |
| 5 | +from cryptography.hazmat.primitives import hashes, serialization |
| 6 | +from cryptography.hazmat.primitives.asymmetric import rsa |
| 7 | +from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID |
| 8 | + |
| 9 | + |
| 10 | +def generate_certificates(cert_dir="certs"): |
| 11 | + """Generate self-signed certificates with proper extensions for HTTPS proxy""" |
| 12 | + # Create certificates directory if it doesn't exist |
| 13 | + if not os.path.exists(cert_dir): |
| 14 | + print("Making: ", cert_dir) |
| 15 | + os.makedirs(cert_dir) |
| 16 | + |
| 17 | + # Generate private key |
| 18 | + ca_private_key = rsa.generate_private_key( |
| 19 | + public_exponent=65537, |
| 20 | + key_size=4096, # Increased key size for better security |
| 21 | + ) |
| 22 | + |
| 23 | + # Generate public key |
| 24 | + ca_public_key = ca_private_key.public_key() |
| 25 | + |
| 26 | + # CA BEGIN |
| 27 | + name = x509.Name( |
| 28 | + [ |
| 29 | + x509.NameAttribute(NameOID.COMMON_NAME, "Proxy Pilot CA"), |
| 30 | + x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Proxy Pilot"), |
| 31 | + x509.NameAttribute(NameOID.ORGANIZATIONAL_UNIT_NAME, "Development"), |
| 32 | + x509.NameAttribute(NameOID.COUNTRY_NAME, "UK"), |
| 33 | + ] |
| 34 | + ) |
| 35 | + |
| 36 | + builder = x509.CertificateBuilder() |
| 37 | + builder = builder.subject_name(name) |
| 38 | + builder = builder.issuer_name(name) |
| 39 | + builder = builder.public_key(ca_public_key) |
| 40 | + builder = builder.serial_number(x509.random_serial_number()) |
| 41 | + builder = builder.not_valid_before(datetime.datetime.utcnow()) |
| 42 | + builder = builder.not_valid_after( |
| 43 | + datetime.datetime.utcnow() + datetime.timedelta(days=3650) # 10 years |
| 44 | + ) |
| 45 | + |
| 46 | + builder = builder.add_extension( |
| 47 | + x509.BasicConstraints(ca=True, path_length=None), |
| 48 | + critical=True, |
| 49 | + ) |
| 50 | + |
| 51 | + builder = builder.add_extension( |
| 52 | + x509.KeyUsage( |
| 53 | + digital_signature=True, |
| 54 | + content_commitment=False, |
| 55 | + key_encipherment=True, |
| 56 | + data_encipherment=False, |
| 57 | + key_agreement=False, |
| 58 | + key_cert_sign=True, # This is a CA |
| 59 | + crl_sign=True, |
| 60 | + encipher_only=False, |
| 61 | + decipher_only=False, |
| 62 | + ), |
| 63 | + critical=True, |
| 64 | + ) |
| 65 | + |
| 66 | + ca_cert = builder.sign( |
| 67 | + private_key=ca_private_key, |
| 68 | + algorithm=hashes.SHA256(), |
| 69 | + ) |
| 70 | + |
| 71 | + # Save CA certificate and key |
| 72 | + |
| 73 | + with open("certs/ca.crt", "wb") as f: |
| 74 | + f.write(ca_cert.public_bytes(serialization.Encoding.PEM)) |
| 75 | + |
| 76 | + with open("certs/ca.key", "wb") as f: |
| 77 | + f.write( |
| 78 | + ca_private_key.private_bytes( |
| 79 | + encoding=serialization.Encoding.PEM, |
| 80 | + format=serialization.PrivateFormat.PKCS8, |
| 81 | + encryption_algorithm=serialization.NoEncryption(), |
| 82 | + ) |
| 83 | + ) |
| 84 | + # CA END |
| 85 | + |
| 86 | + # SERVER BEGIN |
| 87 | + |
| 88 | + # Generate new certificate for domain |
| 89 | + server_key = rsa.generate_private_key( |
| 90 | + public_exponent=65537, |
| 91 | + key_size=2048, # 2048 bits is sufficient for domain certs |
| 92 | + ) |
| 93 | + |
| 94 | + name = x509.Name( |
| 95 | + [ |
| 96 | + x509.NameAttribute(NameOID.COMMON_NAME, "Proxy Pilot CA"), |
| 97 | + x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Proxy Pilot Generated"), |
| 98 | + ] |
| 99 | + ) |
| 100 | + |
| 101 | + builder = x509.CertificateBuilder() |
| 102 | + builder = builder.subject_name(name) |
| 103 | + builder = builder.issuer_name(ca_cert.subject) |
| 104 | + builder = builder.public_key(server_key.public_key()) |
| 105 | + builder = builder.serial_number(x509.random_serial_number()) |
| 106 | + builder = builder.not_valid_before(datetime.datetime.utcnow()) |
| 107 | + builder = builder.not_valid_after(datetime.datetime.utcnow() + datetime.timedelta(days=365)) |
| 108 | + |
| 109 | + # Add domain to SAN |
| 110 | + builder = builder.add_extension( |
| 111 | + x509.SubjectAlternativeName([x509.DNSName("localhost")]), |
| 112 | + critical=False, |
| 113 | + ) |
| 114 | + |
| 115 | + # Add extended key usage |
| 116 | + builder = builder.add_extension( |
| 117 | + x509.ExtendedKeyUsage( |
| 118 | + [ |
| 119 | + ExtendedKeyUsageOID.SERVER_AUTH, |
| 120 | + ExtendedKeyUsageOID.CLIENT_AUTH, |
| 121 | + ] |
| 122 | + ), |
| 123 | + critical=False, |
| 124 | + ) |
| 125 | + |
| 126 | + # Basic constraints (not a CA) |
| 127 | + builder = builder.add_extension( |
| 128 | + x509.BasicConstraints(ca=False, path_length=None), |
| 129 | + critical=True, |
| 130 | + ) |
| 131 | + |
| 132 | + certificate = builder.sign( |
| 133 | + private_key=ca_private_key, |
| 134 | + algorithm=hashes.SHA256(), |
| 135 | + ) |
| 136 | + |
| 137 | + with open("certs/server.crt", "wb") as f: |
| 138 | + f.write(certificate.public_bytes(serialization.Encoding.PEM)) |
| 139 | + |
| 140 | + with open("certs/server.key", "wb") as f: |
| 141 | + f.write( |
| 142 | + server_key.private_bytes( |
| 143 | + encoding=serialization.Encoding.PEM, |
| 144 | + format=serialization.PrivateFormat.PKCS8, |
| 145 | + encryption_algorithm=serialization.NoEncryption(), |
| 146 | + ) |
| 147 | + ) |
| 148 | + |
| 149 | + print("Certificates generated successfully in the 'certs' directory") |
| 150 | + print("\nTo trust these certificates:") |
| 151 | + print("\nOn macOS:") |
| 152 | + print( |
| 153 | + "sudo security add-trusted-cert -d -r trustRoot " |
| 154 | + "-k /Library/Keychains/System.keychain certs/server.crt" |
| 155 | + ) |
| 156 | + print("\nOn Windows (PowerShell as Admin):") |
| 157 | + print( |
| 158 | + 'Import-Certificate -FilePath "certs\\server.crt" ' |
| 159 | + "-CertStoreLocation Cert:\\LocalMachine\\Root" |
| 160 | + ) |
| 161 | + print("\nOn Linux:") |
| 162 | + print("sudo cp certs/server.crt /usr/local/share/ca-certificates/proxy-pilot.crt") |
| 163 | + print("sudo update-ca-certificates") |
| 164 | + print("\nFor VSCode, add to settings.json:") |
| 165 | + print( |
| 166 | + """{ |
| 167 | + "http.proxy": "https://localhost:8989", |
| 168 | + "http.proxySupport": "on", |
| 169 | + "github.copilot.advanced": { |
| 170 | + "debug.testOverrideProxyUrl": "https://localhost:8989", |
| 171 | + "debug.overrideProxyUrl": "https://localhost:8989" |
| 172 | + } |
| 173 | +}""" |
| 174 | + ) |
| 175 | + |
| 176 | + |
| 177 | +if __name__ == "__main__": |
| 178 | + generate_certificates() |
0 commit comments