Last active
February 9, 2018 08:54
-
-
Save Manishearth/6f280af989c95a91adfd to your computer and use it in GitHub Desktop.
Certificate PEM extractor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://mxr.mozilla.org/mozilla-central/source/security/manager/pki/resources/content/pippki.js | |
function getDERString(cert) | |
{ | |
var length = {}; | |
var derArray = cert.getRawDER(length); | |
var derString = ''; | |
for (var i = 0; i < derArray.length; i++) { | |
derString += String.fromCharCode(derArray[i]); | |
} | |
return derString; | |
} | |
// http://mxr.mozilla.org/mozilla-central/source/security/manager/pki/resources/content/pippki.js | |
function getPEMString(cert) | |
{ | |
var derb64 = btoa(getDERString(cert)); | |
// Wrap the Base64 string into lines of 64 characters, | |
// with CRLF line breaks (as specified in RFC 1421). | |
var wrapped = derb64.replace(/(\S{64}(?!$))/g, "$1\r\n"); | |
return "-----BEGIN CERTIFICATE-----\r\n" | |
+ wrapped | |
+ "\r\n-----END CERTIFICATE-----\r\n"; | |
} | |
let certcache = Components.classes["@mozilla.org/security/nsscertcache;1"].createInstance(Ci.nsINSSCertCache); | |
certcache.cacheAllCerts(); | |
let enumerator = certcache.getX509CachedCerts().getEnumerator(); | |
let certlist = []; | |
let certstring=""; | |
while(enumerator.hasMoreElements()){ | |
let cert = enumerator.getNext().QueryInterface(Ci.nsIX509Cert); | |
let pem = getPEMString(cert); | |
certlist.push({name: cert.commonName, pem: pem}); | |
certstring+=pem+"\n"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment