summaryrefslogtreecommitdiffstats
path: root/abs/extra/nss/certdata2pem.py
diff options
context:
space:
mode:
Diffstat (limited to 'abs/extra/nss/certdata2pem.py')
-rw-r--r--abs/extra/nss/certdata2pem.py198
1 files changed, 188 insertions, 10 deletions
diff --git a/abs/extra/nss/certdata2pem.py b/abs/extra/nss/certdata2pem.py
index 021772a..e63ca0f 100644
--- a/abs/extra/nss/certdata2pem.py
+++ b/abs/extra/nss/certdata2pem.py
@@ -27,6 +27,7 @@ import re
import sys
import textwrap
import urllib
+import subprocess
objects = []
@@ -113,6 +114,17 @@ def obj_to_filename(obj):
serial = printable_serial(obj)
return label + ":" + serial
+def write_cert_ext_to_file(f, oid, value, public_key):
+ f.write("[p11-kit-object-v1]\n")
+ f.write("label: ");
+ f.write(tobj['CKA_LABEL'])
+ f.write("\n")
+ f.write("class: x-certificate-extension\n");
+ f.write("object-id: " + oid + "\n")
+ f.write("value: \"" + value + "\"\n")
+ f.write("modifiable: false\n");
+ f.write(public_key)
+
trust_types = {
"CKA_TRUST_DIGITAL_SIGNATURE": "digital-signature",
"CKA_TRUST_NON_REPUDIATION": "non-repudiation",
@@ -132,6 +144,18 @@ trust_types = {
"CKA_TRUST_STEP_UP_APPROVED": "step-up-approved",
}
+legacy_trust_types = {
+ "LEGACY_CKA_TRUST_SERVER_AUTH": "server-auth",
+ "LEGACY_CKA_TRUST_CODE_SIGNING": "code-signing",
+ "LEGACY_CKA_TRUST_EMAIL_PROTECTION": "email-protection",
+}
+
+legacy_to_real_trust_types = {
+ "LEGACY_CKA_TRUST_SERVER_AUTH": "CKA_TRUST_SERVER_AUTH",
+ "LEGACY_CKA_TRUST_CODE_SIGNING": "CKA_TRUST_CODE_SIGNING",
+ "LEGACY_CKA_TRUST_EMAIL_PROTECTION": "CKA_TRUST_EMAIL_PROTECTION",
+}
+
openssl_trust = {
"CKA_TRUST_SERVER_AUTH": "serverAuth",
"CKA_TRUST_CLIENT_AUTH": "clientAuth",
@@ -147,6 +171,8 @@ for tobj in objects:
distrustbits = []
openssl_trustflags = []
openssl_distrustflags = []
+ legacy_trustbits = []
+ legacy_openssl_trustflags = []
for t in trust_types.keys():
if tobj.has_key(t) and tobj[t] == 'CKT_NSS_TRUSTED_DELEGATOR':
trustbits.append(t)
@@ -157,29 +183,180 @@ for tobj in objects:
if t in openssl_trust:
openssl_distrustflags.append(openssl_trust[t])
+ for t in legacy_trust_types.keys():
+ if tobj.has_key(t) and tobj[t] == 'CKT_NSS_TRUSTED_DELEGATOR':
+ real_t = legacy_to_real_trust_types[t]
+ legacy_trustbits.append(real_t)
+ if real_t in openssl_trust:
+ legacy_openssl_trustflags.append(openssl_trust[real_t])
+ if tobj.has_key(t) and tobj[t] == 'CKT_NSS_NOT_TRUSTED':
+ raise NotImplementedError, 'legacy distrust not supported.\n' + line
+
fname = obj_to_filename(tobj)
try:
obj = certmap[key]
except:
obj = None
+ # optional debug code, that dumps the parsed input to files
+ #fulldump = "dump-" + fname
+ #dumpf = open(fulldump, 'w')
+ #dumpf.write(str(obj));
+ #dumpf.write(str(tobj));
+ #dumpf.close();
+
+ is_legacy = 0
+ 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'):
+ is_legacy = 1
+ if obj == None:
+ raise NotImplementedError, 'found legacy trust without certificate.\n' + line
+
+ legacy_fname = "legacy-default/" + fname + ".crt"
+ f = open(legacy_fname, 'w')
+ f.write("# alias=%s\n"%tobj['CKA_LABEL'])
+ f.write("# trust=" + " ".join(legacy_trustbits) + "\n")
+ if legacy_openssl_trustflags:
+ f.write("# openssl-trust=" + " ".join(legacy_openssl_trustflags) + "\n")
+ f.write("-----BEGIN CERTIFICATE-----\n")
+ f.write("\n".join(textwrap.wrap(base64.b64encode(obj['CKA_VALUE']), 64)))
+ f.write("\n-----END CERTIFICATE-----\n")
+ f.close()
+
+ if tobj.has_key('CKA_TRUST_SERVER_AUTH') or tobj.has_key('CKA_TRUST_EMAIL_PROTECTION') or tobj.has_key('CKA_TRUST_CODE_SIGNING'):
+ legacy_fname = "legacy-disable/" + fname + ".crt"
+ f = open(legacy_fname, 'w')
+ f.write("# alias=%s\n"%tobj['CKA_LABEL'])
+ f.write("# trust=" + " ".join(trustbits) + "\n")
+ if openssl_trustflags:
+ f.write("# openssl-trust=" + " ".join(openssl_trustflags) + "\n")
+ f.write("-----BEGIN CERTIFICATE-----\n")
+ f.write("\n".join(textwrap.wrap(base64.b64encode(obj['CKA_VALUE']), 64)))
+ f.write("\n-----END CERTIFICATE-----\n")
+ f.close()
+
+ # don't produce p11-kit output for legacy certificates
+ continue
+
+ pk = ''
+ cert_comment = ''
if obj != None:
- fname += ".crt"
- else:
- fname += ".p11-kit"
+ # must extract the public key from the cert, let's use openssl
+ cert_fname = "cert-" + fname
+ fc = open(cert_fname, 'w')
+ fc.write("-----BEGIN CERTIFICATE-----\n")
+ fc.write("\n".join(textwrap.wrap(base64.b64encode(obj['CKA_VALUE']), 64)))
+ fc.write("\n-----END CERTIFICATE-----\n")
+ fc.close();
+ pk_fname = "pubkey-" + fname
+ fpkout = open(pk_fname, "w")
+ dump_pk_command = ["openssl", "x509", "-in", cert_fname, "-noout", "-pubkey"]
+ subprocess.call(dump_pk_command, stdout=fpkout)
+ fpkout.close()
+ with open (pk_fname, "r") as myfile:
+ pk=myfile.read()
+ # obtain certificate information suitable as a comment
+ comment_fname = "comment-" + fname
+ fcout = open(comment_fname, "w")
+ comment_command = ["openssl", "x509", "-in", cert_fname, "-noout", "-text"]
+ subprocess.call(comment_command, stdout=fcout)
+ fcout.close()
+ sed_command = ["sed", "--in-place", "s/^/#/", comment_fname]
+ subprocess.call(sed_command)
+ with open (comment_fname, "r") as myfile:
+ cert_comment=myfile.read()
+ fname += ".tmp-p11-kit"
f = open(fname, 'w')
+
if obj != None:
- f.write("# alias=%s\n"%tobj['CKA_LABEL'])
- f.write("# trust=" + " ".join(trustbits) + "\n")
- f.write("# distrust=" + " ".join(distrustbits) + "\n")
- if openssl_trustflags:
- f.write("# openssl-trust=" + " ".join(openssl_trustflags) + "\n")
- if openssl_distrustflags:
- f.write("# openssl-distrust=" + " ".join(openssl_distrustflags) + "\n")
+ is_distrusted = False
+ has_server_trust = False
+ has_email_trust = False
+ has_code_trust = False
+
+ if tobj.has_key('CKA_TRUST_SERVER_AUTH'):
+ if tobj['CKA_TRUST_SERVER_AUTH'] == 'CKT_NSS_NOT_TRUSTED':
+ is_distrusted = True
+ elif tobj['CKA_TRUST_SERVER_AUTH'] == 'CKT_NSS_TRUSTED_DELEGATOR':
+ has_server_trust = True
+
+ if tobj.has_key('CKA_TRUST_EMAIL_PROTECTION'):
+ if tobj['CKA_TRUST_EMAIL_PROTECTION'] == 'CKT_NSS_NOT_TRUSTED':
+ is_distrusted = True
+ elif tobj['CKA_TRUST_EMAIL_PROTECTION'] == 'CKT_NSS_TRUSTED_DELEGATOR':
+ has_email_trust = True
+
+ if tobj.has_key('CKA_TRUST_CODE_SIGNING'):
+ if tobj['CKA_TRUST_CODE_SIGNING'] == 'CKT_NSS_NOT_TRUSTED':
+ is_distrusted = True
+ elif tobj['CKA_TRUST_CODE_SIGNING'] == 'CKT_NSS_TRUSTED_DELEGATOR':
+ has_code_trust = True
+
+ if is_distrusted:
+ trust_ext_oid = "1.3.6.1.4.1.3319.6.10.1"
+ 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"
+ write_cert_ext_to_file(f, trust_ext_oid, trust_ext_value, pk)
+
+ trust_ext_oid = "2.5.29.37"
+ if has_server_trust:
+ if has_email_trust:
+ if has_code_trust:
+ # server + email + code
+ 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"
+ else:
+ # server + email
+ 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"
+ else:
+ if has_code_trust:
+ # server + code
+ 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"
+ else:
+ # server
+ 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"
+ else:
+ if has_email_trust:
+ if has_code_trust:
+ # email + code
+ 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"
+ else:
+ # email
+ 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"
+ else:
+ if has_code_trust:
+ # code
+ 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"
+ else:
+ # none
+ 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"
+
+ # no 2.5.29.37 for neutral certificates
+ if (is_distrusted or has_server_trust or has_email_trust or has_code_trust):
+ write_cert_ext_to_file(f, trust_ext_oid, trust_ext_value, pk)
+
+ pk = ''
+ f.write("\n")
+
+ f.write("[p11-kit-object-v1]\n")
+ f.write("label: ");
+ f.write(tobj['CKA_LABEL'])
+ f.write("\n")
+ if is_distrusted:
+ f.write("x-distrusted: true\n")
+ elif has_server_trust or has_email_trust or has_code_trust:
+ f.write("trusted: true\n")
+ else:
+ f.write("trusted: false\n")
+
+ # requires p11-kit >= 0.23.4
+ f.write("nss-mozilla-ca-policy: true\n")
+ f.write("modifiable: false\n");
+
f.write("-----BEGIN CERTIFICATE-----\n")
f.write("\n".join(textwrap.wrap(base64.b64encode(obj['CKA_VALUE']), 64)))
f.write("\n-----END CERTIFICATE-----\n")
+ f.write(cert_comment)
+ f.write("\n")
+
else:
f.write("[p11-kit-object-v1]\n")
f.write("label: ");
@@ -187,6 +364,7 @@ for tobj in objects:
f.write("\n")
f.write("class: certificate\n")
f.write("certificate-type: x-509\n")
+ f.write("modifiable: false\n");
f.write("issuer: \"");
f.write(urllib.quote(tobj['CKA_ISSUER']));
f.write("\"\n")