give README a proper file extension so that it renders correctly
[stack/cam.git] / cam / ca.py
index a14075c..f870389 100644 (file)
--- a/cam/ca.py
+++ b/cam/ca.py
@@ -28,13 +28,13 @@ class CA(object):
         self.basedir = basedir
         self.config = {'basedir': basedir, 'default_days': '365', 'ou': 'CA',
                        'days': '3650', 'country': 'XX', 'crl_url': '',
-                       'bits': '4096'}
+                       'signature_algorithm': 'sha1', 'bits': '2048'}
         self.config.update(config)
         self.files = _CAFiles(basedir, 
                               conf='conf/ca.conf',
                               public_key='public/ca.pem',
                               private_key='private/ca.key',
-                              crl='public/crl.pem',
+                              crl='public/ca.crl',
                               serial='serial',
                               crlnumber='crlnumber',
                               index='index')
@@ -66,6 +66,10 @@ class CA(object):
         fcntl.lockf(self._lockfd, fcntl.LOCK_UN)
         self._lockfd.close()
 
+    def _update_config(self):
+        # Create the OpenSSL configuration file.
+        utils.render(self.files.conf, 'openssl_config', self.config)
+
     def close(self):
         self._unlock()
 
@@ -92,8 +96,7 @@ class CA(object):
             with open(self.files.crlnumber, 'w') as fd:
                 fd.write('01\n')
 
-        # Create the OpenSSL configuration file.
-        utils.render(self.files.conf, 'openssl_config', self.config)
+        self._update_config()
 
         # Generate keys if they do not exist.
         if not os.path.exists(self.files.public_key):
@@ -101,13 +104,16 @@ class CA(object):
             csr_file = os.path.join(tmpdir, 'ca.csr')
             log.info('creating new RSA CA CSR')
             openssl_wrap.run_with_config(
-                self.files.conf, 'req', '-new',
+                self.basedir, self.files.conf,
+                'req', '-new',
                 '-passout', 'pass:%s' % self._getpw(),
                 '-keyout', self.files.private_key, '-out', csr_file)
             log.info('self-signing RSA CA certificate')
             openssl_wrap.run_with_config(
-                self.files.conf, 'ca', '-keyfile', self.files.private_key,
+                self.basedir, self.files.conf,
+                'ca', '-keyfile', self.files.private_key,
                 '-key', self._getpw(),
+                '-md', self.config['signature_algorithm'],
                 '-extensions', 'v3_ca', '-out', self.files.public_key,
                 '-days', self.config.get('days', self.config['default_days']),
                 '-selfsign', '-infiles', csr_file)
@@ -126,22 +132,39 @@ class CA(object):
 
     def gencrl(self):
         log.info('generating CRL')
+
+        self._update_config()
+
+        # Write the CRL in PEM format to a temporary file.
+        tmpf = self.files.crl + '.tmp'
         openssl_wrap.run_with_config(
-            self.files.conf, 'ca', '-gencrl', '-out', self.files.crl,
+            self.basedir, self.files.conf,
+            'ca', '-gencrl', '-out', tmpf,
             '-key', self._getpw())
+        # Convert to DER format for distribution.
+        openssl_wrap.run(
+            'crl', '-inform', 'PEM', '-outform', 'DER',
+            '-in', tmpf, '-out', self.files.crl)
+        os.remove(tmpf)
         os.chmod(self.files.crl, 0644)
 
     def revoke(self, cert):
         log.info('revoking certificate %s', cert.name)
         openssl_wrap.run_with_config(
-            self.files.conf, 'ca', '-revoke', cert.public_key_file,
+            self.basedir, self.files.conf,
+            'ca', '-revoke', cert.public_key_file,
             '-key', self._getpw())
         self.gencrl()
 
     def generate(self, cert):
+        self._update_config()
+
         expiry = cert.get_expiration_date()
         if expiry and expiry > time.time():
-            log.warn('certificate is still valid, revoking previous version')
+            log.warn('certificate is still valid')
+
+        if cert.exists():
+            log.warn('revoking previous version')
             self.revoke(cert)
 
         log.info('generating new certificate %s', cert.name)
@@ -150,7 +173,7 @@ class CA(object):
             csr_file = os.path.join(tmpdir, '%s.csr' % cert.name)
             conf_file = os.path.join(tmpdir, '%s.conf' % cert.name)
             ext_file = os.path.join(tmpdir, '%s-ext.conf' % cert.name)
-            conf = {}
+            conf = {'usage': 'client, server'}
             conf.update(self.config)
             conf['cn'] = cert.cn
             conf['days'] = cert.days or self.config['default_days']
@@ -162,12 +185,15 @@ class CA(object):
             utils.render(conf_file, 'openssl_config', conf)
             utils.render(ext_file, 'ext_config', conf)
             openssl_wrap.run_with_config(
-                conf_file, 'req', '-new', '-keyout', cert.private_key_file,
+                self.basedir, conf_file,
+                'req', '-new', '-keyout', cert.private_key_file,
                 '-nodes', '-out', csr_file)
             os.chmod(cert.private_key_file, 0600)
             openssl_wrap.run_with_config(
-                conf_file, 'ca', '-days', conf['days'],
+                self.basedir, conf_file,
+                'ca', '-days', conf['days'],
                 '-key', self._getpw(),
+                '-md', self.config['signature_algorithm'],
                 '-policy', 'policy_anything', '-out', cert.public_key_file,
                 '-extfile', ext_file, '-infiles', csr_file)
         finally: