11 from cam import openssl_wrap
14 log = logging.getLogger(__name__)
17 class _CAFiles(object):
19 def __init__(self, basedir, **attrs):
20 for key, value in attrs.items():
21 setattr(self, key, os.path.join(basedir, value))
26 def __init__(self, basedir, config, password=None):
28 self.basedir = basedir
29 self.config = {'basedir': basedir, 'default_days': '365', 'ou': 'CA',
30 'days': '3650', 'country': 'XX', 'crl_url': '',
32 self.config.update(config)
33 self.files = _CAFiles(basedir,
35 public_key='public/ca.pem',
36 private_key='private/ca.key',
39 crlnumber='crlnumber',
45 self._pw = getpass.getpass(prompt='CA Password: ')
49 self._lockfd = open(os.path.join(self.basedir, '_lock'), 'w+')
53 fcntl.lockf(self._lockfd, fcntl.LOCK_EX | fcntl.LOCK_NB)
56 if e.errno in (errno.EACCES, errno.EAGAIN):
59 log.error('another instance is running')
66 fcntl.lockf(self._lockfd, fcntl.LOCK_UN)
73 old_umask = os.umask(077)
75 for pathext in ('', 'conf', 'public', 'public/certs',
76 'public/crl', 'private', 'newcerts'):
77 fullpath = os.path.join(self.basedir, pathext)
78 if not os.path.isdir(fullpath):
81 if not os.path.exists(self.files.index):
82 log.info('creating new index file')
83 open(self.files.index, 'w').close()
85 if not os.path.exists(self.files.serial):
86 serial = random.randint(1, 1000000000)
87 log.info('initializing serial number (%d)', serial)
88 with open(self.files.serial, 'w') as fd:
89 fd.write('%08X\n' % serial)
91 if not os.path.exists(self.files.crlnumber):
92 with open(self.files.crlnumber, 'w') as fd:
95 # Create the OpenSSL configuration file.
96 utils.render(self.files.conf, 'openssl_config', self.config)
98 # Generate keys if they do not exist.
99 if not os.path.exists(self.files.public_key):
100 tmpdir = tempfile.mkdtemp()
101 csr_file = os.path.join(tmpdir, 'ca.csr')
102 log.info('creating new RSA CA CSR')
103 openssl_wrap.run_with_config(
104 self.basedir, self.files.conf,
106 '-passout', 'pass:%s' % self._getpw(),
107 '-keyout', self.files.private_key, '-out', csr_file)
108 log.info('self-signing RSA CA certificate')
109 openssl_wrap.run_with_config(
110 self.basedir, self.files.conf,
111 'ca', '-keyfile', self.files.private_key,
112 '-key', self._getpw(),
113 '-extensions', 'v3_ca', '-out', self.files.public_key,
114 '-days', self.config.get('days', self.config['default_days']),
115 '-selfsign', '-infiles', csr_file)
116 shutil.rmtree(tmpdir)
120 # Make some files public.
121 for path in (os.path.join(self.basedir, 'public'),
122 os.path.join(self.basedir, 'public/certs'),
123 self.files.public_key):
124 if os.path.isdir(path):
130 log.info('generating CRL')
131 openssl_wrap.run_with_config(
132 self.basedir, self.files.conf,
133 'ca', '-gencrl', '-out', self.files.crl,
134 '-key', self._getpw())
135 os.chmod(self.files.crl, 0644)
137 def revoke(self, cert):
138 log.info('revoking certificate %s', cert.name)
139 openssl_wrap.run_with_config(
140 self.basedir, self.files.conf,
141 'ca', '-revoke', cert.public_key_file,
142 '-key', self._getpw())
145 def generate(self, cert):
146 expiry = cert.get_expiration_date()
147 if expiry and expiry > time.time():
148 log.warn('certificate is still valid, revoking previous version')
151 log.info('generating new certificate %s', cert.name)
152 tmpdir = tempfile.mkdtemp()
154 csr_file = os.path.join(tmpdir, '%s.csr' % cert.name)
155 conf_file = os.path.join(tmpdir, '%s.conf' % cert.name)
156 ext_file = os.path.join(tmpdir, '%s-ext.conf' % cert.name)
158 conf.update(self.config)
160 conf['days'] = cert.days or self.config['default_days']
163 conf['alt_names'] = ''.join(
164 ['DNS.%d=%s\n' % (idx + 1, x)
165 for idx, x in enumerate(cert.alt_names)])
166 utils.render(conf_file, 'openssl_config', conf)
167 utils.render(ext_file, 'ext_config', conf)
168 openssl_wrap.run_with_config(
169 self.basedir, conf_file,
170 'req', '-new', '-keyout', cert.private_key_file,
171 '-nodes', '-out', csr_file)
172 os.chmod(cert.private_key_file, 0600)
173 openssl_wrap.run_with_config(
174 self.basedir, conf_file,
175 'ca', '-days', conf['days'],
176 '-key', self._getpw(),
177 '-policy', 'policy_anything', '-out', cert.public_key_file,
178 '-extfile', ext_file, '-infiles', csr_file)
180 shutil.rmtree(tmpdir)