cert_utils.py 5.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from OpenSSL import crypto, SSL
from socket import gethostname
from pprint import pprint
import time
from os.path import exists, join

def crt_import(str):
    return crypto.load_certificate(crypto.FILETYPE_PEM, open(str).read())

def key_import(str):
    return crypto.load_privatekey(crypto.FILETYPE_PEM, open(str).read())

def crt_write(crt, file):
    open(file, "wt").write(
        crypto.dump_certificate(crypto.FILETYPE_PEM, crt))

def key_write(key, file, cipher=None, passphrase=None):
    open(file, "wt").write(
        crypto.dump_privatekey(crypto.FILETYPE_PEM, key, cipher, passphrase) if cipher else crypto.dump_privatekey(crypto.FILETYPE_PEM, key))

def write((cert, key), cert_dir="."):
    common_name = cert.get_subject().CN
    CERT_FILE = "%s.crt" % common_name
    KEY_FILE = "%s.key" % common_name
    C_F = join(cert_dir, CERT_FILE)
    K_F = join(cert_dir, KEY_FILE)
    if not exists(C_F) or not exists(K_F):
        crt_write(cert,C_F)
        key_write(key,K_F)
    else:
         print "Les fichiers %s et %s existent déjà" % (C_F, K_F)

def crl_gen(crl=None, add_crt=[]):
    if not crl:
        crl=crypto.CRL()
    for crt in add_crt:
        revoked = crypto.Revoked()
        revoked.set_serial(hex(crt.get_serial_number()))
        crl.add_revoked(revoked)
    return crl

def crl_export(crl, (ca_cert, ca_key)):
    return crl.export(ca_cert, ca_key)

def crt_create(
  common_name,
  params={},
  not_after=365*24*3600,
  size=4096,
  key=None,
  parent=None,
  extentions=[]
  ):
    """
@param parent: tuple (X509 certificate, private key)
@return: (X509 certificate, private key)
"""
    params["CN"] = common_name # common name
    params.setdefault("C", "FR") # country
    params.setdefault("ST", "Val de Marne") # state
    params.setdefault("L", "Cachan") # city
    params.setdefault("O", "Association CRANS") # organization
    params.setdefault("OU", "") # organization unit

    if not key:
        # create a key pair
        k = crypto.PKey()
        k.generate_key(crypto.TYPE_RSA, size)
    else:
        k = key
    if k.bits() != size:
        raise ValueError("Key of %s bits but requesting %s bits" % (k.bits, size))

    # create a certificate
    cert = crypto.X509()
    cert.get_subject().C = params["C"]
    cert.get_subject().ST = params["ST"]
    cert.get_subject().L = params["L"]
    cert.get_subject().O = params["O"]
    if params["OU"]:
        cert.get_subject().OU = params["OU"]
    cert.get_subject().CN = params["CN"]

    cert.set_serial_number(int((time.time() - 1386340000)*100))
    cert.gmtime_adj_notBefore(0)
    cert.gmtime_adj_notAfter(not_after)
    cert.set_pubkey(k)
    cert.add_extensions(extentions)
    if parent:
        cert.set_issuer(parent[0].get_subject())
        cert.sign(parent[1], 'sha1')
    else:
        cert.set_issuer(cert.get_subject())
        cert.sign(k, 'sha1')
    
    return (cert, k)


def crt_ca_root_create(
  common_name,
  params={},
  size=4096,
  key=None,
  not_after=365*24*3600*10,
  ):
  extentions=[
    crypto.X509Extension("basicConstraints", True, "CA:TRUE"),
    crypto.X509Extension("keyUsage", True, "keyCertSign, cRLSign"),
  ]
  return crt_create(
  common_name,
  params,
  not_after,
  size,
  key,
  extentions=extentions
  )


def crt_ca_subroot_create(
  common_name,
  parent,
  params={},
  size=4096,
  key=None,
  not_after=365*24*3600*10,
  ):
  extentions=[
    crypto.X509Extension("basicConstraints", True, "CA:TRUE, pathlen:0"),
    crypto.X509Extension("keyUsage", True, "keyCertSign, cRLSign"),
  ]
  return crt_create(
  common_name,
  params,
  not_after,
  size,
  key,
  parent=parent,
  extentions=extentions
  )

def crt_client_create(
  common_name,
  parent,
  params={},
  size=4096,
  key=None,
  not_after=365*24*3600,
  alt_names=[]
  ):
  extentions=[
    crypto.X509Extension("basicConstraints", True, "CA:FALSE"),
    crypto.X509Extension("keyUsage", True, "digitalSignature, nonRepudiation, keyEncipherment"),
    crypto.X509Extension("nsCertType", True, "sslCA"),
    crypto.X509Extension("extendedKeyUsage", True, "clientAuth"),
#    crypto.X509Extension("crlDistributionPoints", False, "URI:http://myhost.com/myca.crl"),
  ]
  for name in alt_names:
      extentions.append(crypto.X509Extension("subjectAltName", True, "%s" % name))
  return crt_create(
  common_name,
  params,
  not_after,
  size,
  key,
  parent=parent,
  extentions=extentions
  )


def createCertRequest(pkey, digest="sha1", subjectAltName=[], **name):
    """
    Create a certificate request.
    Arguments: pkey   - The key to associate with the request
               digest - Digestion method to use for signing, default is md5
               **name - The name of the subject of the request, possible
                        arguments are:
                          C     - Country name
                          ST    - State or province name
                          L     - Locality name
                          O     - Organization name
                          OU    - Organizational unit name
                          CN    - Common name
                          emailAddress - E-mail address
    Returns:   The certificate request in an X509Req object
    """
    req = crypto.X509Req()
    subj = req.get_subject()
    for (key,value) in name.items():
        setattr(subj, key, value)
    if subjectAltName:
        exts = []
        for altName in subjectAltName:
            exts.append(crypto.X509Extension("subjectAltName", True, "DNS:%s" % altName))
        req.add_extensions(exts)
    req.set_pubkey(pkey)
    req.sign(pkey, digest)
    return req