pkcs10-gen.js
Summary
Generator for PKCS#10 encoded certificate requests
load("tools/file.js");
load("../lib/smartcardhsm.js");
load("../lib/hsmkeystore.js");
var data = new ASN1(ASN1.SEQUENCE);
var commonname = Dialog.prompt("Common Name", "");
if ((commonname != null) && (commonname != "")) {
data.add(new ASN1(ASN1.SET,
new ASN1(ASN1.SEQUENCE,
new ASN1(ASN1.OBJECT_IDENTIFIER, new ByteString("id-at-commonName", OID)),
new ASN1(ASN1.UTF8String, new ByteString(commonname, UTF8)))));
}
var business = Dialog.prompt("Business Name", "OpenSCDP");
if ((business != null) && (business != "")) {
data.add(new ASN1(ASN1.SET,
new ASN1(ASN1.SEQUENCE,
new ASN1(ASN1.OBJECT_IDENTIFIER, new ByteString("id-at-organizationName", OID)),
new ASN1(ASN1.PrintableString, new ByteString(business, ASCII)))));
}
var department = Dialog.prompt("Department Name", "");
if ((department != null) && (department != "")) {
data.add(new ASN1(ASN1.SET,
new ASN1(ASN1.SEQUENCE,
new ASN1(ASN1.OBJECT_IDENTIFIER, new ByteString("id-at-organizationalUnitName", OID)),
new ASN1(ASN1.PrintableString, new ByteString(department, ASCII)))));
}
var town = Dialog.prompt("Town", "");
if ((town != null) && (town != "")) {
data.add(new ASN1(ASN1.SET,
new ASN1(ASN1.SEQUENCE,
new ASN1(ASN1.OBJECT_IDENTIFIER, new ByteString("id-at-localityName", OID)),
new ASN1(ASN1.PrintableString, new ByteString(town, ASCII)))));
}
var province = Dialog.prompt("Province", "");
if ((province != null) && (province != "")) {
data.add(new ASN1(ASN1.SET,
new ASN1(ASN1.SEQUENCE,
new ASN1(ASN1.OBJECT_IDENTIFIER, new ByteString("id-at-stateOrProvinceName", OID)),
new ASN1(ASN1.PrintableString, new ByteString(province, ASCII)))));
}
var country = Dialog.prompt("Country", "DE");
if ((country != null) && (country != "")) {
data.add(new ASN1(ASN1.SET,
new ASN1(ASN1.SEQUENCE,
new ASN1(ASN1.OBJECT_IDENTIFIER, new ByteString("id-at-countryName", OID)),
new ASN1(ASN1.PrintableString, new ByteString(country, ASCII)))));
}
var eMailAddress = Dialog.prompt("Please enter your e-mail address", "");
if ((eMailAddress != null) && (eMailAddress != "")) {
data.add(new ASN1(ASN1.SET,
new ASN1(ASN1.SEQUENCE,
new ASN1(ASN1.OBJECT_IDENTIFIER, new ByteString("1 2 840 113549 1 9 1", OID)),
new ASN1(ASN1.IA5String, new ByteString(eMailAddress, ASCII)))));
}
var certificationRequestInfo = new ASN1(ASN1.SEQUENCE);
certificationRequestInfo.add(new ASN1(ASN1.INTEGER, new ByteString("00", HEX)));
certificationRequestInfo.add(data);
var crypto = new Crypto();
var card = new Card();
card.reset(Card.RESET_COLD);
var sc = new SmartCardHSM(card);
var userPIN = Dialog.prompt("Please enter user PIN for SmartCard-HSM", "648219");
assert(userPIN != null);
sc.verifyUserPIN(new ByteString(userPIN, ASCII));
var label = eMailAddress;
print("Using label \"" + label + "\" for key");
var hsmks = new HSMKeyStore(sc);
sc.enumerateKeys();
var key = sc.getKey(label);
if (key) {
assert(Dialog.prompt("A key with the label " + label + " already exists. Press OK to delete the key"));
hsmks.deleteKey(label);
}
print("Generating a 2048 bit RSA key pair can take up to 60 seconds. Please wait...");
var req = hsmks.generateRSAKeyPair(label, 2048);
var pubkey = req.getPublicKey();
var encPK = new ASN1(ASN1.SEQUENCE);
encPK.add(new ASN1(ASN1.INTEGER, pubkey.getComponent(Key.MODULUS)));
encPK.add(new ASN1(ASN1.INTEGER,pubkey.getComponent(Key.EXPONENT)));
var SubjectPublicKeyInfo = new ASN1(ASN1.SEQUENCE);
SubjectPublicKeyInfo.add(new ASN1(ASN1.SEQUENCE,
new ASN1(ASN1.OBJECT_IDENTIFIER, new ByteString("rsaEncryption", OID)),
new ASN1(ASN1.NULL)));
SubjectPublicKeyInfo.add(new ASN1(ASN1.BIT_STRING,
new ByteString("00", HEX).concat(encPK.getBytes())));
certificationRequestInfo.add(SubjectPublicKeyInfo);
var Attributes = new ASN1(ASN1.CONTEXT | 0x20 | 0x00);
certificationRequestInfo.add(Attributes);
var signature = sc.getCrypto().sign(sc.getKey(label), Crypto.RSA_SHA256, certificationRequestInfo.getBytes());
var AlgorithmIdentifier = new ASN1(ASN1.SEQUENCE);
AlgorithmIdentifier.add(new ASN1(ASN1.OBJECT_IDENTIFIER,
new ByteString("sha256WithRSAEncryption", OID)));
AlgorithmIdentifier.add(new ASN1(ASN1.NULL));
var encodedsignature = new ASN1(ASN1.BIT_STRING,
new ByteString("00", HEX).concat(signature));
var CertRequest = new ASN1(ASN1.SEQUENCE);
CertRequest.add(certificationRequestInfo);
CertRequest.add(AlgorithmIdentifier);
CertRequest.add(encodedsignature);
var csrfile = new File("CSR_" + label);
var csrbinary = CertRequest.getBytes();
var csrbase64 = csrbinary.toBase64(true);
var header = new ByteString("-----BEGIN CERTIFICATE REQUEST-----\n", ASCII);
var footer = new ByteString("\n-----END CERTIFICATE REQUEST-----", ASCII);
var pem = header.concat(csrbase64).concat(footer);
csrfile.writeAll(pem);
print("PKCS#10 Request written to file >> " + "CSR_" + label + " <<");
Documentation generated by
JSDoc on Tue Sep 3 22:29:45 2013