526e52fa08d725c7772405ed65579e3a1596d84d
[stack/cam.git] / lib / gen.py
1
2
3 import os, sys
4 import logging
5 from utils import *
6 from templates import *
7 from paths import *
8 from cfg import *
9
10
11 def gen(tag):
12     
13     info = cfg2dict(cfg, tag)
14     
15     conf_file = getpath('conf', tag)
16     rsa_key_file = getpath('rsa_key', tag)
17     dsa_key_file = getpath('dsa_key', tag)
18     dsa_parms_file = getpath('dsa_parms', tag)
19     csr_file = getpath('rsa_csr', tag)
20     dsa_csr_file = getpath('dsa_csr', tag)
21     ext_file = getpath('ext', tag)
22     public_crt_file = getpath('public_crt', tag)
23     crt_file = getpath('rsa_crt', tag)
24     dsa_crt_file = getpath('dsa_crt', tag)
25     sf_file = getpath('singlefile', tag)
26
27     if os.path.exists(public_crt_file):
28         print
29         if expired(getcertdate(public_crt_file)):
30             print 'Certificate has expired. Ready to re-generate.'
31         else:
32             print 
33             ans = raw_input('This certificate seems to exist already (in %s).\nAre you really sure that you want to re-create it? [y/N]  ' % crt_file)
34             if not ans or ans[0].lower() != 'y':
35                 sys.exit(0)
36
37     # create custom config file
38     template(conf_file,
39              openssl_conf_template, 
40              dict(
41             ca_dir = ca_base,
42             default_days = ca['default_days'],
43             country = d2get(info, ca, 'country'),
44             org = d2get(info, ca, 'org'),
45             ou = d2get(info, ca, 'ou', ''),
46             cn = info['cn'],
47             email = d2get(info, ca, 'email')))
48
49     # create dsa parameters
50     openssl('dsaparam', '-out', dsa_parms_file, '1024')
51
52     # create rsa key
53     openssl('req', '-batch', '-new', '-keyout', rsa_key_file, 
54             '-config', conf_file, '-nodes', '-out', csr_file)
55     openssl('req', '-batch', '-new', '-newkey', 'dsa:' + dsa_parms_file,
56             '-keyout', dsa_key_file, '-nodes',
57             '-config', conf_file, '-out', dsa_csr_file)
58
59     # create ext file
60     altnames = [ x.strip() for x in info['alt_names'].split(',') ]
61     altnames_s = ''
62     for i in range(len(altnames)):
63         altnames_s += 'DNS.%d=%s\n' % (i + 1, altnames[i])
64     template(ext_file, 
65              ext_template,
66              dict(
67             ca_name = ca['name'],
68             ca_base_url = ca['base_url'],
69             alt_names = altnames_s))
70
71     # sign requests
72     openssl('ca', '-days', ca['default_days'],
73             '-config', conf_file, '-batch',
74             '-policy', 'policy_anything', 
75             '-out', crt_file, 
76             '-extfile', ext_file,
77             '-infiles', csr_file)
78     openssl('ca', '-days', ca['default_days'], 
79             '-config', conf_file, '-batch',
80             '-policy', 'policy_anything',
81             '-out', dsa_crt_file,
82             '-extfile', ext_file,
83             '-infiles', dsa_csr_file)
84     f = open(public_crt_file, 'w')
85     f.write(open(crt_file, 'r').read())
86     f.write(open(dsa_crt_file, 'r').read())
87     f.close()
88
89     # create single-file file
90     f = open(sf_file, 'w')
91     f.write(open(crt_file, 'r').read())
92     f.write(open(dsa_crt_file, 'r').read())
93     f.write(open(rsa_key_file, 'r').read())
94     f.write(open(dsa_key_file, 'r').read())
95     f.close()
96
97     logging.info('created certificate %s [%s]' % (tag, info['cn']))
98
99     print '''
100 Certificate '%s':
101
102     CN:          %s
103     AltNames:    %s
104
105     RSA key:     %s
106     DSA key:     %s
107     public crt:  %s
108     all-in-one:  %s
109
110 ''' % (tag, info['cn'], ', '.join(altnames),
111        rsa_key_file, dsa_key_file, public_crt_file, sf_file)