|
|
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
|
|
|
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 |
|
|
|
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 |
|
|
|
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 = []
|
|
|
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 |
|
|
|
b4bc2f |
fname = obj_to_filename(tobj)
|
|
|
b4bc2f |
try:
|
|
|
b4bc2f |
obj = certmap[key]
|
|
|
b4bc2f |
except:
|
|
|
b4bc2f |
obj = None
|
|
|
b4bc2f |
|
|
|
b4bc2f |
if obj != None:
|
|
|
b4bc2f |
fname += ".crt"
|
|
|
b4bc2f |
else:
|
|
|
b4bc2f |
fname += ".p11-kit"
|
|
|
b4bc2f |
|
|
|
b4bc2f |
f = open(fname, 'w')
|
|
|
b4bc2f |
if obj != None:
|
|
|
b4bc2f |
f.write("# alias=%s\n"%tobj['CKA_LABEL'])
|
|
|
b4bc2f |
f.write("# trust=" + " ".join(trustbits) + "\n")
|
|
|
b4bc2f |
f.write("# distrust=" + " ".join(distrustbits) + "\n")
|
|
|
b4bc2f |
if openssl_trustflags:
|
|
|
b4bc2f |
f.write("# openssl-trust=" + " ".join(openssl_trustflags) + "\n")
|
|
|
b4bc2f |
if openssl_distrustflags:
|
|
|
b4bc2f |
f.write("# openssl-distrust=" + " ".join(openssl_distrustflags) + "\n")
|
|
|
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")
|
|
|
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")
|
|
|
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")
|
|
|
b4bc2f |
print " -> written as '%s', trust = %s, openssl-trust = %s, distrust = %s, openssl-distrust = %s" % (fname, trustbits, openssl_trustflags, distrustbits, openssl_distrustflags)
|