1
1
"""Make the custom certificate and private key files used by test_ssl
2
2
and friends."""
3
3
4
+ import argparse
4
5
import os
5
6
import pprint
6
7
import shutil
7
8
import tempfile
8
9
from subprocess import *
9
10
10
11
startdate = "20180829142316Z"
11
- enddate = "20371028142316Z"
12
+ enddate_default = "20371028142316Z"
13
+ days_default = "7000"
12
14
13
15
req_template = """
14
16
[ default ]
79
81
default_startdate = {startdate}
80
82
enddate = {enddate}
81
83
default_enddate = {enddate}
82
- default_days = 7000
83
- default_crl_days = 7000
84
+ default_days = {days}
85
+ default_crl_days = {days}
84
86
certificate = pycacert.pem
85
87
private_key = pycakey.pem
86
88
serial = $dir/serial
117
119
here = os .path .abspath (os .path .dirname (__file__ ))
118
120
119
121
120
- def make_cert_key (hostname , sign = False , extra_san = '' ,
122
+ def make_cert_key (cmdlineargs , hostname , sign = False , extra_san = '' ,
121
123
ext = 'req_x509_extensions_full' , key = 'rsa:3072' ):
122
124
print ("creating cert for " + hostname )
123
125
tempnames = []
@@ -130,11 +132,12 @@ def make_cert_key(hostname, sign=False, extra_san='',
130
132
hostname = hostname ,
131
133
extra_san = extra_san ,
132
134
startdate = startdate ,
133
- enddate = enddate
135
+ enddate = cmdlineargs .enddate ,
136
+ days = cmdlineargs .days
134
137
)
135
138
with open (req_file , 'w' ) as f :
136
139
f .write (req )
137
- args = ['req' , '-new' , '-nodes' , '-days' , '7000' ,
140
+ args = ['req' , '-new' , '-nodes' , '-days' , cmdlineargs . days ,
138
141
'-newkey' , key , '-keyout' , key_file ,
139
142
'-extensions' , ext ,
140
143
'-config' , req_file ]
@@ -175,7 +178,7 @@ def make_cert_key(hostname, sign=False, extra_san='',
175
178
def unmake_ca ():
176
179
shutil .rmtree (TMP_CADIR )
177
180
178
- def make_ca ():
181
+ def make_ca (cmdlineargs ):
179
182
os .mkdir (TMP_CADIR )
180
183
with open (os .path .join ('cadir' ,'index.txt' ),'a+' ) as f :
181
184
pass # empty file
@@ -192,7 +195,8 @@ def make_ca():
192
195
hostname = 'our-ca-server' ,
193
196
extra_san = '' ,
194
197
startdate = startdate ,
195
- enddate = enddate
198
+ enddate = cmdlineargs .enddate ,
199
+ days = cmdlineargs .days
196
200
)
197
201
t .write (req )
198
202
t .flush ()
@@ -219,14 +223,22 @@ def make_ca():
219
223
shutil .copy ('capath/ceff1710.0' , 'capath/b1930218.0' )
220
224
221
225
222
- def print_cert (path ):
226
+ def write_cert_reference (path ):
223
227
import _ssl
224
- pprint .pprint (_ssl ._test_decode_cert (path ))
228
+ refdata = pprint .pformat (_ssl ._test_decode_cert (path ))
229
+ print (refdata )
230
+ with open (path + '.reference' , 'w' ) as f :
231
+ print (refdata , file = f )
225
232
226
233
227
234
if __name__ == '__main__' :
235
+ parser = argparse .ArgumentParser (description = 'Make the custom certificate and private key files used by test_ssl and friends.' )
236
+ parser .add_argument ('--days' , default = days_default )
237
+ parser .add_argument ('--enddate' , default = enddate_default )
238
+ cmdlineargs = parser .parse_args ()
239
+
228
240
os .chdir (here )
229
- cert , key = make_cert_key ('localhost' , ext = 'req_x509_extensions_simple' )
241
+ cert , key = make_cert_key (cmdlineargs , 'localhost' , ext = 'req_x509_extensions_simple' )
230
242
with open ('ssl_cert.pem' , 'w' ) as f :
231
243
f .write (cert )
232
244
with open ('ssl_key.pem' , 'w' ) as f :
@@ -243,24 +255,24 @@ def print_cert(path):
243
255
f .write (cert )
244
256
245
257
# For certificate matching tests
246
- make_ca ()
247
- cert , key = make_cert_key ('fakehostname' , ext = 'req_x509_extensions_simple' )
258
+ make_ca (cmdlineargs )
259
+ cert , key = make_cert_key (cmdlineargs , 'fakehostname' , ext = 'req_x509_extensions_simple' )
248
260
with open ('keycert2.pem' , 'w' ) as f :
249
261
f .write (key )
250
262
f .write (cert )
251
263
252
- cert , key = make_cert_key ('localhost' , sign = True )
264
+ cert , key = make_cert_key (cmdlineargs , 'localhost' , sign = True )
253
265
with open ('keycert3.pem' , 'w' ) as f :
254
266
f .write (key )
255
267
f .write (cert )
256
268
257
- cert , key = make_cert_key ('fakehostname' , sign = True )
269
+ cert , key = make_cert_key (cmdlineargs , 'fakehostname' , sign = True )
258
270
with open ('keycert4.pem' , 'w' ) as f :
259
271
f .write (key )
260
272
f .write (cert )
261
273
262
274
cert , key = make_cert_key (
263
- 'localhost-ecc' , sign = True , key = 'param:secp384r1.pem'
275
+ cmdlineargs , 'localhost-ecc' , sign = True , key = 'param:secp384r1.pem'
264
276
)
265
277
with open ('keycertecc.pem' , 'w' ) as f :
266
278
f .write (key )
@@ -280,7 +292,7 @@ def print_cert(path):
280
292
'RID.1 = 1.2.3.4.5' ,
281
293
]
282
294
283
- cert , key = make_cert_key ('allsans' , sign = True , extra_san = '\n ' .join (extra_san ))
295
+ cert , key = make_cert_key (cmdlineargs , 'allsans' , sign = True , extra_san = '\n ' .join (extra_san ))
284
296
with open ('allsans.pem' , 'w' ) as f :
285
297
f .write (key )
286
298
f .write (cert )
@@ -297,17 +309,17 @@ def print_cert(path):
297
309
]
298
310
299
311
# IDN SANS, signed
300
- cert , key = make_cert_key ('idnsans' , sign = True , extra_san = '\n ' .join (extra_san ))
312
+ cert , key = make_cert_key (cmdlineargs , 'idnsans' , sign = True , extra_san = '\n ' .join (extra_san ))
301
313
with open ('idnsans.pem' , 'w' ) as f :
302
314
f .write (key )
303
315
f .write (cert )
304
316
305
- cert , key = make_cert_key ('nosan' , sign = True , ext = 'req_x509_extensions_nosan' )
317
+ cert , key = make_cert_key (cmdlineargs , 'nosan' , sign = True , ext = 'req_x509_extensions_nosan' )
306
318
with open ('nosan.pem' , 'w' ) as f :
307
319
f .write (key )
308
320
f .write (cert )
309
321
310
322
unmake_ca ()
311
- print ("update Lib/test/test_ssl.py and Lib/test/test_asyncio/utils.py" )
312
- print_cert ('keycert.pem' )
313
- print_cert ('keycert3.pem' )
323
+ print ("Writing out reference data for Lib/test/test_ssl.py and Lib/test/test_asyncio/utils.py" )
324
+ write_cert_reference ('keycert.pem' )
325
+ write_cert_reference ('keycert3.pem' )
0 commit comments