Blame SOURCES/certdata2pem.py

b4bc2f
#!/usr/bin/python
b4bc2f
# vim:set et sw=4:
b4bc2f
#
b4bc2f
# certdata2pem.py - splits certdata.txt into multiple files
b4bc2f
#
b4bc2f
# Copyright (C) 2009 Philipp Kern <pkern@debian.org>
b4bc2f
# Copyright (C) 2013 Kai Engert <kaie@redhat.com>
b4bc2f
#
b4bc2f
# This program is free software; you can redistribute it and/or modify
b4bc2f
# it under the terms of the GNU General Public License as published by
b4bc2f
# the Free Software Foundation; either version 2 of the License, or
b4bc2f
# (at your option) any later version.
b4bc2f
#
b4bc2f
# This program is distributed in the hope that it will be useful,
b4bc2f
# but WITHOUT ANY WARRANTY; without even the implied warranty of
b4bc2f
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b4bc2f
# GNU General Public License for more details.
b4bc2f
#
b4bc2f
# You should have received a copy of the GNU General Public License
b4bc2f
# along with this program; if not, write to the Free Software
b4bc2f
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
b4bc2f
# USA.
b4bc2f
b4bc2f
import base64
b4bc2f
import os.path
b4bc2f
import re
b4bc2f
import sys
b4bc2f
import textwrap
b4bc2f
import urllib
e5c4b3
import subprocess
b4bc2f
b4bc2f
objects = []
b4bc2f
b4bc2f
def printable_serial(obj):
b4bc2f
  return ".".join(map(lambda x:str(ord(x)), obj['CKA_SERIAL_NUMBER']))
b4bc2f
b4bc2f
# Dirty file parser.
b4bc2f
in_data, in_multiline, in_obj = False, False, False
b4bc2f
field, type, value, obj = None, None, None, dict()
b4bc2f
for line in open('certdata.txt', 'r'):
b4bc2f
    # Ignore the file header.
b4bc2f
    if not in_data:
b4bc2f
        if line.startswith('BEGINDATA'):
b4bc2f
            in_data = True
b4bc2f
        continue
b4bc2f
    # Ignore comment lines.
b4bc2f
    if line.startswith('#'):
b4bc2f
        continue
b4bc2f
    # Empty lines are significant if we are inside an object.
b4bc2f
    if in_obj and len(line.strip()) == 0:
b4bc2f
        objects.append(obj)
b4bc2f
        obj = dict()
b4bc2f
        in_obj = False
b4bc2f
        continue
b4bc2f
    if len(line.strip()) == 0:
b4bc2f
        continue
b4bc2f
    if in_multiline:
b4bc2f
        if not line.startswith('END'):
b4bc2f
            if type == 'MULTILINE_OCTAL':
b4bc2f
                line = line.strip()
b4bc2f
                for i in re.finditer(r'\\([0-3][0-7][0-7])', line):
b4bc2f
                    value += chr(int(i.group(1), 8))
b4bc2f
            else:
b4bc2f
                value += line
b4bc2f
            continue
b4bc2f
        obj[field] = value
b4bc2f
        in_multiline = False
b4bc2f
        continue
b4bc2f
    if line.startswith('CKA_CLASS'):
b4bc2f
        in_obj = True
b4bc2f
    line_parts = line.strip().split(' ', 2)
b4bc2f
    if len(line_parts) > 2:
b4bc2f
        field, type = line_parts[0:2]
b4bc2f
        value = ' '.join(line_parts[2:])
b4bc2f
    elif len(line_parts) == 2:
b4bc2f
        field, type = line_parts
b4bc2f
        value = None
b4bc2f
    else:
b4bc2f
        raise NotImplementedError, 'line_parts < 2 not supported.\n' + line
b4bc2f
    if type == 'MULTILINE_OCTAL':
b4bc2f
        in_multiline = True
b4bc2f
        value = ""
b4bc2f
        continue
b4bc2f
    obj[field] = value
b4bc2f
if len(obj.items()) > 0:
b4bc2f
    objects.append(obj)
b4bc2f
b4bc2f
# Build up trust database.
b4bc2f
trustmap = dict()
b4bc2f
for obj in objects:
b4bc2f
    if obj['CKA_CLASS'] != 'CKO_NSS_TRUST':
b4bc2f
        continue
b4bc2f
    key = obj['CKA_LABEL'] + printable_serial(obj)
b4bc2f
    trustmap[key] = obj
b4bc2f
    print " added trust", key
b4bc2f
b4bc2f
# Build up cert database.
b4bc2f
certmap = dict()
b4bc2f
for obj in objects:
b4bc2f
    if obj['CKA_CLASS'] != 'CKO_CERTIFICATE':
b4bc2f
        continue
b4bc2f
    key = obj['CKA_LABEL'] + printable_serial(obj)
b4bc2f
    certmap[key] = obj
b4bc2f
    print " added cert", key
b4bc2f
b4bc2f
def obj_to_filename(obj):
b4bc2f
    label = obj['CKA_LABEL'][1:-1]
b4bc2f
    label = label.replace('/', '_')\
b4bc2f
        .replace(' ', '_')\
b4bc2f
        .replace('(', '=')\
b4bc2f
        .replace(')', '=')\
b4bc2f
        .replace(',', '_')
b4bc2f
    label = re.sub(r'\\x[0-9a-fA-F]{2}', lambda m:chr(int(m.group(0)[2:], 16)), label)
b4bc2f
    serial = printable_serial(obj)
b4bc2f
    return label + ":" + serial
b4bc2f
e5c4b3
def write_cert_ext_to_file(f, oid, value, public_key):
e5c4b3
    f.write("[p11-kit-object-v1]\n")
e5c4b3
    f.write("label: ");
e5c4b3
    f.write(tobj['CKA_LABEL'])
e5c4b3
    f.write("\n")
e5c4b3
    f.write("class: x-certificate-extension\n");
e5c4b3
    f.write("object-id: " + oid + "\n")
e5c4b3
    f.write("value: \"" + value + "\"\n")
e5c4b3
    f.write("modifiable: false\n");
e5c4b3
    f.write(public_key)
e5c4b3
b4bc2f
trust_types = {
b4bc2f
  "CKA_TRUST_DIGITAL_SIGNATURE": "digital-signature",
b4bc2f
  "CKA_TRUST_NON_REPUDIATION": "non-repudiation",
b4bc2f
  "CKA_TRUST_KEY_ENCIPHERMENT": "key-encipherment",
b4bc2f
  "CKA_TRUST_DATA_ENCIPHERMENT": "data-encipherment",
b4bc2f
  "CKA_TRUST_KEY_AGREEMENT": "key-agreement",
b4bc2f
  "CKA_TRUST_KEY_CERT_SIGN": "cert-sign",
b4bc2f
  "CKA_TRUST_CRL_SIGN": "crl-sign",
b4bc2f
  "CKA_TRUST_SERVER_AUTH": "server-auth",
b4bc2f
  "CKA_TRUST_CLIENT_AUTH": "client-auth",
b4bc2f
  "CKA_TRUST_CODE_SIGNING": "code-signing",
b4bc2f
  "CKA_TRUST_EMAIL_PROTECTION": "email-protection",
b4bc2f
  "CKA_TRUST_IPSEC_END_SYSTEM": "ipsec-end-system",
b4bc2f
  "CKA_TRUST_IPSEC_TUNNEL": "ipsec-tunnel",
b4bc2f
  "CKA_TRUST_IPSEC_USER": "ipsec-user",
b4bc2f
  "CKA_TRUST_TIME_STAMPING": "time-stamping",
b4bc2f
  "CKA_TRUST_STEP_UP_APPROVED": "step-up-approved",
b4bc2f
}
b4bc2f
b01320
legacy_trust_types = {
b01320
  "LEGACY_CKA_TRUST_SERVER_AUTH": "server-auth",
b01320
  "LEGACY_CKA_TRUST_CODE_SIGNING": "code-signing",
b01320
  "LEGACY_CKA_TRUST_EMAIL_PROTECTION": "email-protection",
b01320
}
b01320
b01320
legacy_to_real_trust_types = {
b01320
  "LEGACY_CKA_TRUST_SERVER_AUTH": "CKA_TRUST_SERVER_AUTH",
b01320
  "LEGACY_CKA_TRUST_CODE_SIGNING": "CKA_TRUST_CODE_SIGNING",
b01320
  "LEGACY_CKA_TRUST_EMAIL_PROTECTION": "CKA_TRUST_EMAIL_PROTECTION",
b01320
}
b01320
b4bc2f
openssl_trust = {
b4bc2f
  "CKA_TRUST_SERVER_AUTH": "serverAuth",
b4bc2f
  "CKA_TRUST_CLIENT_AUTH": "clientAuth",
b4bc2f
  "CKA_TRUST_CODE_SIGNING": "codeSigning",
b4bc2f
  "CKA_TRUST_EMAIL_PROTECTION": "emailProtection",
b4bc2f
}
b4bc2f
b4bc2f
for tobj in objects:
b4bc2f
    if tobj['CKA_CLASS'] == 'CKO_NSS_TRUST':
b4bc2f
        key = tobj['CKA_LABEL'] + printable_serial(tobj)
b4bc2f
        print "producing trust for " + key
b4bc2f
        trustbits = []
b4bc2f
        distrustbits = []
b4bc2f
        openssl_trustflags = []
b4bc2f
        openssl_distrustflags = []
b01320
        legacy_trustbits = []
b01320
        legacy_openssl_trustflags = []
b4bc2f
        for t in trust_types.keys():
b4bc2f
            if tobj.has_key(t) and tobj[t] == 'CKT_NSS_TRUSTED_DELEGATOR':
b4bc2f
                trustbits.append(t)
b4bc2f
                if t in openssl_trust:
b4bc2f
                    openssl_trustflags.append(openssl_trust[t])
b4bc2f
            if tobj.has_key(t) and tobj[t] == 'CKT_NSS_NOT_TRUSTED':
b4bc2f
                distrustbits.append(t)
b4bc2f
                if t in openssl_trust:
b4bc2f
                    openssl_distrustflags.append(openssl_trust[t])
b4bc2f
b01320
        for t in legacy_trust_types.keys():
b01320
            if tobj.has_key(t) and tobj[t] == 'CKT_NSS_TRUSTED_DELEGATOR':
b01320
                real_t = legacy_to_real_trust_types[t]
b01320
                legacy_trustbits.append(real_t)
b01320
                if real_t in openssl_trust:
b01320
                    legacy_openssl_trustflags.append(openssl_trust[real_t])
b01320
            if tobj.has_key(t) and tobj[t] == 'CKT_NSS_NOT_TRUSTED':
b01320
                raise NotImplementedError, 'legacy distrust not supported.\n' + line
b01320
b4bc2f
        fname = obj_to_filename(tobj)
b4bc2f
        try:
b4bc2f
            obj = certmap[key]
b4bc2f
        except:
b4bc2f
            obj = None
b4bc2f
e5c4b3
        # optional debug code, that dumps the parsed input to files
e5c4b3
        #fulldump = "dump-" + fname
e5c4b3
        #dumpf = open(fulldump, 'w')
e5c4b3
        #dumpf.write(str(obj));
e5c4b3
        #dumpf.write(str(tobj));
e5c4b3
        #dumpf.close();
b4bc2f
b01320
        is_legacy = 0
b01320
        if tobj.has_key('LEGACY_CKA_TRUST_SERVER_AUTH') or tobj.has_key('LEGACY_CKA_TRUST_EMAIL_PROTECTION') or tobj.has_key('LEGACY_CKA_TRUST_CODE_SIGNING'):
b01320
            is_legacy = 1
b01320
            if obj == None:
b01320
                raise NotImplementedError, 'found legacy trust without certificate.\n' + line
e5c4b3
e5c4b3
            legacy_fname = "legacy-default/" + fname + ".crt"
b01320
            f = open(legacy_fname, 'w')
b01320
            f.write("# alias=%s\n"%tobj['CKA_LABEL'])
b01320
            f.write("# trust=" + " ".join(legacy_trustbits) + "\n")
b01320
            if legacy_openssl_trustflags:
b01320
                f.write("# openssl-trust=" + " ".join(legacy_openssl_trustflags) + "\n")
b01320
            f.write("-----BEGIN CERTIFICATE-----\n")
b01320
            f.write("\n".join(textwrap.wrap(base64.b64encode(obj['CKA_VALUE']), 64)))
b01320
            f.write("\n-----END CERTIFICATE-----\n")
b01320
            f.close()
e5c4b3
b01320
            if tobj.has_key('CKA_TRUST_SERVER_AUTH') or tobj.has_key('CKA_TRUST_EMAIL_PROTECTION') or tobj.has_key('CKA_TRUST_CODE_SIGNING'):
e5c4b3
                legacy_fname = "legacy-disable/" + fname + ".crt"
e5c4b3
                f = open(legacy_fname, 'w')
e5c4b3
                f.write("# alias=%s\n"%tobj['CKA_LABEL'])
e5c4b3
                f.write("# trust=" + " ".join(trustbits) + "\n")
e5c4b3
                if openssl_trustflags:
e5c4b3
                    f.write("# openssl-trust=" + " ".join(openssl_trustflags) + "\n")
e5c4b3
                f.write("-----BEGIN CERTIFICATE-----\n")
e5c4b3
                f.write("\n".join(textwrap.wrap(base64.b64encode(obj['CKA_VALUE']), 64)))
e5c4b3
                f.write("\n-----END CERTIFICATE-----\n")
e5c4b3
                f.close()
e5c4b3
e5c4b3
            # don't produce p11-kit output for legacy certificates
e5c4b3
            continue
b01320
e5c4b3
        pk = ''
e5c4b3
        cert_comment = ''
e5c4b3
        if obj != None:
e5c4b3
            # must extract the public key from the cert, let's use openssl
e5c4b3
            cert_fname = "cert-" + fname
e5c4b3
            fc = open(cert_fname, 'w')
e5c4b3
            fc.write("-----BEGIN CERTIFICATE-----\n")
e5c4b3
            fc.write("\n".join(textwrap.wrap(base64.b64encode(obj['CKA_VALUE']), 64)))
e5c4b3
            fc.write("\n-----END CERTIFICATE-----\n")
e5c4b3
            fc.close();
e5c4b3
            pk_fname = "pubkey-" + fname
e5c4b3
            fpkout = open(pk_fname, "w")
e5c4b3
            dump_pk_command = ["openssl", "x509", "-in", cert_fname, "-noout", "-pubkey"]
e5c4b3
            subprocess.call(dump_pk_command, stdout=fpkout)
e5c4b3
            fpkout.close()
e5c4b3
            with open (pk_fname, "r") as myfile:
e5c4b3
                pk=myfile.read()
e5c4b3
            # obtain certificate information suitable as a comment
e5c4b3
            comment_fname = "comment-" + fname
e5c4b3
            fcout = open(comment_fname, "w")
e5c4b3
            comment_command = ["openssl", "x509", "-in", cert_fname, "-noout", "-text"]
e5c4b3
            subprocess.call(comment_command, stdout=fcout)
e5c4b3
            fcout.close()
e5c4b3
            sed_command = ["sed", "--in-place", "s/^/#/", comment_fname]
e5c4b3
            subprocess.call(sed_command)
e5c4b3
            with open (comment_fname, "r") as myfile:
e5c4b3
                cert_comment=myfile.read()
e5c4b3
e5c4b3
        fname += ".tmp-p11-kit"
b4bc2f
        f = open(fname, 'w')
e5c4b3
b4bc2f
        if obj != None:
e5c4b3
            is_distrusted = False
e5c4b3
            has_server_trust = False
e5c4b3
            has_email_trust = False
e5c4b3
            has_code_trust = False
e5c4b3
e5c4b3
            if tobj.has_key('CKA_TRUST_SERVER_AUTH'):
e5c4b3
                if tobj['CKA_TRUST_SERVER_AUTH'] == 'CKT_NSS_NOT_TRUSTED':
e5c4b3
                    is_distrusted = True
e5c4b3
                elif tobj['CKA_TRUST_SERVER_AUTH'] == 'CKT_NSS_TRUSTED_DELEGATOR':
e5c4b3
                    has_server_trust = True
e5c4b3
e5c4b3
            if tobj.has_key('CKA_TRUST_EMAIL_PROTECTION'):
e5c4b3
                if tobj['CKA_TRUST_EMAIL_PROTECTION'] == 'CKT_NSS_NOT_TRUSTED':
e5c4b3
                    is_distrusted = True
e5c4b3
                elif tobj['CKA_TRUST_EMAIL_PROTECTION'] == 'CKT_NSS_TRUSTED_DELEGATOR':
e5c4b3
                    has_email_trust = True
e5c4b3
e5c4b3
            if tobj.has_key('CKA_TRUST_CODE_SIGNING'):
e5c4b3
                if tobj['CKA_TRUST_CODE_SIGNING'] == 'CKT_NSS_NOT_TRUSTED':
e5c4b3
                    is_distrusted = True
e5c4b3
                elif tobj['CKA_TRUST_CODE_SIGNING'] == 'CKT_NSS_TRUSTED_DELEGATOR':
e5c4b3
                    has_code_trust = True
e5c4b3
e5c4b3
            if is_distrusted:
e5c4b3
                trust_ext_oid = "1.3.6.1.4.1.3319.6.10.1"
e5c4b3
                trust_ext_value = "0.%06%0a%2b%06%01%04%01%99w%06%0a%01%04 0%1e%06%08%2b%06%01%05%05%07%03%04%06%08%2b%06%01%05%05%07%03%01%06%08%2b%06%01%05%05%07%03%03"
e5c4b3
                write_cert_ext_to_file(f, trust_ext_oid, trust_ext_value, pk)
e5c4b3
e5c4b3
            trust_ext_oid = "2.5.29.37"
e5c4b3
            if has_server_trust:
e5c4b3
                if has_email_trust:
e5c4b3
                    if has_code_trust:
e5c4b3
                        # server + email + code
e5c4b3
                        trust_ext_value = "0%2a%06%03U%1d%25%01%01%ff%04 0%1e%06%08%2b%06%01%05%05%07%03%04%06%08%2b%06%01%05%05%07%03%01%06%08%2b%06%01%05%05%07%03%03"
e5c4b3
                    else:
e5c4b3
                        # server + email
e5c4b3
                        trust_ext_value = "0 %06%03U%1d%25%01%01%ff%04%160%14%06%08%2b%06%01%05%05%07%03%04%06%08%2b%06%01%05%05%07%03%01"
e5c4b3
                else:
e5c4b3
                    if has_code_trust:
e5c4b3
                        # server + code
e5c4b3
                        trust_ext_value = "0 %06%03U%1d%25%01%01%ff%04%160%14%06%08%2b%06%01%05%05%07%03%01%06%08%2b%06%01%05%05%07%03%03"
e5c4b3
                    else:
e5c4b3
                        # server
e5c4b3
                        trust_ext_value = "0%16%06%03U%1d%25%01%01%ff%04%0c0%0a%06%08%2b%06%01%05%05%07%03%01"
e5c4b3
            else:
e5c4b3
                if has_email_trust:
e5c4b3
                    if has_code_trust:
e5c4b3
                        # email + code
e5c4b3
                        trust_ext_value = "0 %06%03U%1d%25%01%01%ff%04%160%14%06%08%2b%06%01%05%05%07%03%04%06%08%2b%06%01%05%05%07%03%03"
e5c4b3
                    else:
e5c4b3
                        # email
e5c4b3
                        trust_ext_value = "0%16%06%03U%1d%25%01%01%ff%04%0c0%0a%06%08%2b%06%01%05%05%07%03%04"
e5c4b3
                else:
e5c4b3
                    if has_code_trust:
e5c4b3
                        # code
e5c4b3
                        trust_ext_value = "0%16%06%03U%1d%25%01%01%ff%04%0c0%0a%06%08%2b%06%01%05%05%07%03%03"
e5c4b3
                    else:
e5c4b3
                        # none
e5c4b3
                        trust_ext_value = "0%18%06%03U%1d%25%01%01%ff%04%0e0%0c%06%0a%2b%06%01%04%01%99w%06%0a%10"
e5c4b3
e5c4b3
            # no 2.5.29.37 for neutral certificates
e5c4b3
            if (is_distrusted or has_server_trust or has_email_trust or has_code_trust):
e5c4b3
                write_cert_ext_to_file(f, trust_ext_oid, trust_ext_value, pk)
e5c4b3
e5c4b3
            pk = ''
e5c4b3
            f.write("\n")
e5c4b3
e5c4b3
            f.write("[p11-kit-object-v1]\n")
e5c4b3
            f.write("label: ");
e5c4b3
            f.write(tobj['CKA_LABEL'])
e5c4b3
            f.write("\n")
e5c4b3
            if is_distrusted:
e5c4b3
                f.write("x-distrusted: true\n")
e5c4b3
            elif has_server_trust or has_email_trust or has_code_trust:
e5c4b3
                f.write("trusted: true\n")
e5c4b3
            else:
e5c4b3
                f.write("trusted: false\n")
e5c4b3
e5c4b3
            # requires p11-kit >= 0.23.4
e5c4b3
            f.write("nss-mozilla-ca-policy: true\n")
e5c4b3
            f.write("modifiable: false\n");
e5c4b3
b4bc2f
            f.write("-----BEGIN CERTIFICATE-----\n")
b4bc2f
            f.write("\n".join(textwrap.wrap(base64.b64encode(obj['CKA_VALUE']), 64)))
b4bc2f
            f.write("\n-----END CERTIFICATE-----\n")
e5c4b3
            f.write(cert_comment)
e5c4b3
            f.write("\n")
e5c4b3
b4bc2f
        else:
b4bc2f
            f.write("[p11-kit-object-v1]\n")
b4bc2f
            f.write("label: ");
b4bc2f
            f.write(tobj['CKA_LABEL']);
b4bc2f
            f.write("\n")
b4bc2f
            f.write("class: certificate\n")
b4bc2f
            f.write("certificate-type: x-509\n")
e5c4b3
            f.write("modifiable: false\n");
b4bc2f
            f.write("issuer: \"");
b4bc2f
            f.write(urllib.quote(tobj['CKA_ISSUER']));
b4bc2f
            f.write("\"\n")
b4bc2f
            f.write("serial-number: \"");
b4bc2f
            f.write(urllib.quote(tobj['CKA_SERIAL_NUMBER']));
b4bc2f
            f.write("\"\n")
b4bc2f
            if (tobj['CKA_TRUST_SERVER_AUTH'] == 'CKT_NSS_NOT_TRUSTED') or (tobj['CKA_TRUST_EMAIL_PROTECTION'] == 'CKT_NSS_NOT_TRUSTED') or (tobj['CKA_TRUST_CODE_SIGNING'] == 'CKT_NSS_NOT_TRUSTED'):
b4bc2f
              f.write("x-distrusted: true\n")
b4bc2f
            f.write("\n\n")
b01320
        f.close()
b4bc2f
        print " -> written as '%s', trust = %s, openssl-trust = %s, distrust = %s, openssl-distrust = %s" % (fname, trustbits, openssl_trustflags, distrustbits, openssl_distrustflags)