| 1 |
"""Make the custom certificate and private key files used by test_ssl |
| 2 |
and friends.""" |
| 3 |
|
| 4 |
import os |
| 5 |
import shutil |
| 6 |
import sys |
| 7 |
import tempfile |
| 8 |
from subprocess import * |
| 9 |
|
| 10 |
req_template = """ |
| 11 |
[req] |
| 12 |
distinguished_name = req_distinguished_name |
| 13 |
x509_extensions = req_x509_extensions |
| 14 |
prompt = no |
| 15 |
|
| 16 |
[req_distinguished_name] |
| 17 |
C = XY |
| 18 |
L = Castle Anthrax |
| 19 |
O = Python Software Foundation |
| 20 |
CN = {hostname} |
| 21 |
|
| 22 |
[req_x509_extensions] |
| 23 |
subjectAltName = @san |
| 24 |
|
| 25 |
[san] |
| 26 |
DNS.1 = {hostname} |
| 27 |
{extra_san} |
| 28 |
|
| 29 |
[dir_sect] |
| 30 |
C = XY |
| 31 |
L = Castle Anthrax |
| 32 |
O = Python Software Foundation |
| 33 |
CN = dirname example |
| 34 |
|
| 35 |
[princ_name] |
| 36 |
realm = EXP:0, GeneralString:KERBEROS.REALM |
| 37 |
principal_name = EXP:1, SEQUENCE:principal_seq |
| 38 |
|
| 39 |
[principal_seq] |
| 40 |
name_type = EXP:0, INTEGER:1 |
| 41 |
name_string = EXP:1, SEQUENCE:principals |
| 42 |
|
| 43 |
[principals] |
| 44 |
princ1 = GeneralString:username |
| 45 |
|
| 46 |
[ ca ] |
| 47 |
default_ca = CA_default |
| 48 |
|
| 49 |
[ CA_default ] |
| 50 |
dir = cadir |
| 51 |
database = $dir/index.txt |
| 52 |
crlnumber = $dir/crl.txt |
| 53 |
default_md = sha256 |
| 54 |
default_days = 3600 |
| 55 |
default_crl_days = 3600 |
| 56 |
certificate = pycacert.pem |
| 57 |
private_key = pycakey.pem |
| 58 |
serial = $dir/serial |
| 59 |
RANDFILE = $dir/.rand |
| 60 |
|
| 61 |
policy = policy_match |
| 62 |
|
| 63 |
[ policy_match ] |
| 64 |
countryName = match |
| 65 |
stateOrProvinceName = optional |
| 66 |
organizationName = match |
| 67 |
organizationalUnitName = optional |
| 68 |
commonName = supplied |
| 69 |
emailAddress = optional |
| 70 |
|
| 71 |
[ policy_anything ] |
| 72 |
countryName = optional |
| 73 |
stateOrProvinceName = optional |
| 74 |
localityName = optional |
| 75 |
organizationName = optional |
| 76 |
organizationalUnitName = optional |
| 77 |
commonName = supplied |
| 78 |
emailAddress = optional |
| 79 |
|
| 80 |
|
| 81 |
[ v3_ca ] |
| 82 |
|
| 83 |
subjectKeyIdentifier=hash |
| 84 |
authorityKeyIdentifier=keyid:always,issuer |
| 85 |
basicConstraints = CA:true |
| 86 |
|
| 87 |
""" |
| 88 |
|
| 89 |
here = os.path.abspath(os.path.dirname(__file__)) |
| 90 |
|
| 91 |
|
| 92 |
def make_cert_key(hostname, sign=False, extra_san='', |
| 93 |
ext='req_x509_extensions_full', key='rsa:3072'): |
| 94 |
print("creating cert for " + hostname) |
| 95 |
tempnames = [] |
| 96 |
for i in range(3): |
| 97 |
with tempfile.NamedTemporaryFile(delete=False) as f: |
| 98 |
tempnames.append(f.name) |
| 99 |
req_file, cert_file, key_file = tempnames |
| 100 |
try: |
| 101 |
req = req_template.format(hostname=hostname, extra_san=extra_san) |
| 102 |
with open(req_file, 'w') as f: |
| 103 |
f.write(req) |
| 104 |
args = ['req', '-new', '-days', '3650', '-nodes', |
| 105 |
'-newkey', 'rsa:1024', '-keyout', key_file, |
| 106 |
'-config', req_file] |
| 107 |
if sign: |
| 108 |
with tempfile.NamedTemporaryFile(delete=False) as f: |
| 109 |
tempnames.append(f.name) |
| 110 |
reqfile = f.name |
| 111 |
args += ['-out', reqfile ] |
| 112 |
|
| 113 |
else: |
| 114 |
args += ['-x509', '-out', cert_file ] |
| 115 |
check_call(['openssl'] + args) |
| 116 |
|
| 117 |
if sign: |
| 118 |
args = ['ca', '-config', req_file, '-out', cert_file, '-outdir', 'cadir', |
| 119 |
'-policy', 'policy_anything', '-batch', '-infiles', reqfile ] |
| 120 |
check_call(['openssl'] + args) |
| 121 |
|
| 122 |
|
| 123 |
with open(cert_file, 'r') as f: |
| 124 |
cert = f.read() |
| 125 |
with open(key_file, 'r') as f: |
| 126 |
key = f.read() |
| 127 |
return cert, key |
| 128 |
finally: |
| 129 |
for name in tempnames: |
| 130 |
os.remove(name) |
| 131 |
|
| 132 |
TMP_CADIR = 'cadir' |
| 133 |
|
| 134 |
def unmake_ca(): |
| 135 |
shutil.rmtree(TMP_CADIR) |
| 136 |
|
| 137 |
def make_ca(): |
| 138 |
os.mkdir(TMP_CADIR) |
| 139 |
with open(os.path.join('cadir','index.txt'),'a+') as f: |
| 140 |
pass |
| 141 |
with open(os.path.join('cadir','crl.txt'),'a+') as f: |
| 142 |
f.write("00") |
| 143 |
with open(os.path.join('cadir','index.txt.attr'),'w+') as f: |
| 144 |
f.write('unique_subject = no') |
| 145 |
|
| 146 |
with tempfile.NamedTemporaryFile("w") as t: |
| 147 |
t.write(req_template.format(hostname='our-ca-server', extra_san='')) |
| 148 |
t.flush() |
| 149 |
with tempfile.NamedTemporaryFile() as f: |
| 150 |
args = ['req', '-new', '-days', '3650', '-extensions', 'v3_ca', '-nodes', |
| 151 |
'-newkey', 'rsa:3072', '-keyout', 'pycakey.pem', |
| 152 |
'-out', f.name, |
| 153 |
'-subj', '/C=XY/L=Castle Anthrax/O=Python Software Foundation CA/CN=our-ca-server'] |
| 154 |
check_call(['openssl'] + args) |
| 155 |
args = ['ca', '-config', t.name, '-create_serial', |
| 156 |
'-out', 'pycacert.pem', '-batch', '-outdir', TMP_CADIR, |
| 157 |
'-keyfile', 'pycakey.pem', '-days', '3650', |
| 158 |
'-selfsign', '-extensions', 'v3_ca', '-infiles', f.name ] |
| 159 |
check_call(['openssl'] + args) |
| 160 |
args = ['ca', '-config', t.name, '-gencrl', '-out', 'revocation.crl'] |
| 161 |
check_call(['openssl'] + args) |
| 162 |
|
| 163 |
if __name__ == '__main__': |
| 164 |
os.chdir(here) |
| 165 |
cert, key = make_cert_key('localhost') |
| 166 |
with open('ssl_cert.pem', 'w') as f: |
| 167 |
f.write(cert) |
| 168 |
with open('ssl_key.pem', 'w') as f: |
| 169 |
f.write(key) |
| 170 |
print("password protecting ssl_key.pem in ssl_key.passwd.pem") |
| 171 |
check_call(['openssl','rsa','-in','ssl_key.pem','-out','ssl_key.passwd.pem','-des3','-passout','pass:somepass']) |
| 172 |
check_call(['openssl','rsa','-in','ssl_key.pem','-out','keycert.passwd.pem','-des3','-passout','pass:somepass']) |
| 173 |
|
| 174 |
with open('keycert.pem', 'w') as f: |
| 175 |
f.write(key) |
| 176 |
f.write(cert) |
| 177 |
|
| 178 |
with open('keycert.passwd.pem', 'a+') as f: |
| 179 |
f.write(cert) |
| 180 |
|
| 181 |
|
| 182 |
make_ca() |
| 183 |
cert, key = make_cert_key('fakehostname') |
| 184 |
with open('keycert2.pem', 'w') as f: |
| 185 |
f.write(key) |
| 186 |
f.write(cert) |
| 187 |
|
| 188 |
cert, key = make_cert_key('localhost', True) |
| 189 |
with open('keycert3.pem', 'w') as f: |
| 190 |
f.write(key) |
| 191 |
f.write(cert) |
| 192 |
|
| 193 |
cert, key = make_cert_key('fakehostname', True) |
| 194 |
with open('keycert4.pem', 'w') as f: |
| 195 |
f.write(key) |
| 196 |
f.write(cert) |
| 197 |
|
| 198 |
extra_san = [ |
| 199 |
'otherName.1 = 1.2.3.4;UTF8:some other identifier', |
| 200 |
'otherName.2 = 1.3.6.1.5.2.2;SEQUENCE:princ_name', |
| 201 |
'email.1 = [email protected]', |
| 202 |
'DNS.2 = www.example.org', |
| 203 |
|
| 204 |
'dirName.1 = dir_sect', |
| 205 |
|
| 206 |
'URI.1 = https://www.python.org/', |
| 207 |
'IP.1 = 127.0.0.1', |
| 208 |
'IP.2 = ::1', |
| 209 |
'RID.1 = 1.2.3.4.5', |
| 210 |
] |
| 211 |
|
| 212 |
cert, key = make_cert_key('allsans', extra_san='\n'.join(extra_san)) |
| 213 |
with open('allsans.pem', 'w') as f: |
| 214 |
f.write(key) |
| 215 |
f.write(cert) |
| 216 |
|
| 217 |
unmake_ca() |
| 218 |
print("\n\nPlease change the values in test_ssl.py, test_parse_cert function related to notAfter,notBefore and serialNumber") |
| 219 |
check_call(['openssl','x509','-in','keycert.pem','-dates','-serial','-noout']) |