// --- BEGIN COPYRIGHT BLOCK --- // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 of the License. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License along // with this program; if not, write to the Free Software Foundation, Inc., // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. // // (C) 2007 Red Hat, Inc. // All rights reserved. // --- END COPYRIGHT BLOCK --- package com.netscape.cmstools; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.security.KeyPair; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Date; import netscape.security.x509.X500Name; import org.mozilla.jss.CryptoManager; import org.mozilla.jss.asn1.ASN1Util; import org.mozilla.jss.asn1.BIT_STRING; import org.mozilla.jss.asn1.BMPString; import org.mozilla.jss.asn1.INTEGER; import org.mozilla.jss.asn1.OBJECT_IDENTIFIER; import org.mozilla.jss.asn1.OCTET_STRING; import org.mozilla.jss.asn1.PrintableString; import org.mozilla.jss.asn1.SEQUENCE; import org.mozilla.jss.asn1.TeletexString; import org.mozilla.jss.asn1.UTF8String; import org.mozilla.jss.asn1.UniversalString; import org.mozilla.jss.crypto.AlreadyInitializedException; import org.mozilla.jss.crypto.CryptoToken; import org.mozilla.jss.crypto.IVParameterSpec; import org.mozilla.jss.crypto.KeyGenAlgorithm; import org.mozilla.jss.crypto.KeyGenerator; import org.mozilla.jss.crypto.KeyPairAlgorithm; import org.mozilla.jss.crypto.KeyPairGenerator; import org.mozilla.jss.crypto.KeyWrapAlgorithm; import org.mozilla.jss.crypto.KeyWrapper; import org.mozilla.jss.crypto.Signature; import org.mozilla.jss.crypto.SignatureAlgorithm; import org.mozilla.jss.crypto.SymmetricKey; import org.mozilla.jss.crypto.X509Certificate; import org.mozilla.jss.pkix.crmf.CertReqMsg; import org.mozilla.jss.pkix.crmf.CertRequest; import org.mozilla.jss.pkix.crmf.CertTemplate; import org.mozilla.jss.pkix.crmf.EncryptedKey; import org.mozilla.jss.pkix.crmf.EncryptedValue; import org.mozilla.jss.pkix.crmf.PKIArchiveOptions; import org.mozilla.jss.pkix.crmf.POPOSigningKey; import org.mozilla.jss.pkix.crmf.ProofOfPossession; import org.mozilla.jss.pkix.primitive.AVA; import org.mozilla.jss.pkix.primitive.AlgorithmIdentifier; import org.mozilla.jss.pkix.primitive.Name; import org.mozilla.jss.pkix.primitive.SubjectPublicKeyInfo; import org.mozilla.jss.util.Password; import com.netscape.cmsutil.crypto.CryptoUtil; import com.netscape.cmsutil.util.HMACDigest; import com.netscape.cmsutil.util.Utils; /** * A command-line utility used to generate a Certificate Request Message * Format (CRMF) request with proof of possesion (POP). * *
 * IMPORTANT:  The file "transport.txt" needs to be created to contain the
 *             transport certificate in its base64 encoded format.  This
 *             file should consist of one line containing a single certificate
 *             in base64 encoded format with the header and footer removed.
 * 
*

* * @version $Revision$, $Date$ */ public class CRMFPopClient { private static void usage() { System.out.println("Usage: CRMFPopClient -d -p -h -o -n -a -l -c -m -f -u -r -q \n"); System.out.println(" Optionally, for ECC key generation per definition in JSS pkcs11.PK11KeyPairGenerator:\n"); System.out.println(" -k \n"); System.out.println(" -t \n"); System.out.println(" -s <1 for sensitive; 0 for non-sensitive; -1 temporaryPairMode dependent; default is -1>\n"); System.out.println(" -e <1 for extractable; 0 for non-extractable; -1 token dependent; default is -1>\n"); System.out.println(" Also optional for ECC key generation:\n"); System.out.println(" -x \n"); System.out.println(" note: '-x true' can only be used with POP_NONE"); System.out.println(" available ECC curve names (if provided by the crypto module): nistp256 (secp256r1),nistp384 (secp384r1),nistp521 (secp521r1),nistk163 (sect163k1),sect163r1,nistb163 (sect163r2),sect193r1,sect193r2,nistk233 (sect233k1),nistb233 (sect233r1),sect239k1,nistk283 (sect283k1),nistb283 (sect283r1),nistk409 (sect409k1),nistb409 (sect409r1),nistk571 (sect571k1),nistb571 (sect571r1),secp160k1,secp160r1,secp160r2,secp192k1,nistp192 (secp192r1, prime192v1),secp224k1,nistp224 (secp224r1),secp256k1,prime192v2,prime192v3,prime239v1,prime239v2,prime239v3,c2pnb163v1,c2pnb163v2,c2pnb163v3,c2pnb176v1,c2tnb191v1,c2tnb191v2,c2tnb191v3,c2pnb208w1,c2tnb239v1,c2tnb239v2,c2tnb239v3,c2pnb272w1,c2pnb304w1,c2tnb359w1,c2pnb368w1,c2tnb431r1,secp112r1,secp112r2,secp128r1,secp128r2,sect113r1,sect113r2,sect131r1,sect131r2\n"); System.out.println("\n"); System.out.println("IMPORTANT: The file \"transport.txt\" needs to be created to contain the"); System.out.println(" transport certificate in its base64 encoded format. This"); System.out.println(" file should consist of one line containing a single certificate"); System.out.println(" in base64 encoded format with the header and footer removed.\n"); } private static int getRealArgsLength(String args[]) { int len = args.length; String curArg = ""; int finalLen = len; for (int i = 0; i < len; i++) { curArg = args[i]; // System.out.println("arg[" + i + "] " + curArg); if (curArg == null || curArg.equalsIgnoreCase("")) { finalLen--; } } //System.out.println("getRealArgsLength: returning " + finalLen); if (finalLen < 0) finalLen = 0; return finalLen; } public static void main(String args[]) { // int argsLen = getRealArgsLength(args); System.out.println("\n\nCRMF Proof Of Possession Utility...."); System.out.println(""); if (args.length < 4) { usage(); System.exit(1); } String DB_DIR = "./"; String TOKEN_PWD = null; String TOKEN_NAME = null; // "rsa" or "ec" String alg = "rsa"; /* default RSA key size */ int RSA_keylen = 2048; /* default ECC key curve name */ String ECC_curve = "nistp256"; boolean enable_encoding = false; /* enable encoding attribute values if true */ boolean ec_temporary = true; /* session if true; token if false */ int ec_sensitive = -1; /* -1, 0, or 1 */ int ec_extractable = -1; /* -1, 0, or 1 */ boolean ec_ssl_ecdh = false; String USER_NAME = null; String REQUESTOR_NAME = null; String PROFILE_NAME = null; // format: "host:port" String HOST_PORT = null; String SUBJ_DN = null; int doServerHit = 0; // POP_NONE, POP_SUCCESS, or POP_FAIL String POP_OPTION = "POP_SUCCESS"; int dont_do_pop = 0; String REQ_OUT_FILE = null; for (int i=0; i 0)? elementValue.substring(0, n): null; String nameValue = (n > 0)? elementValue.substring(n+1): null; if (encodingType != null && encodingType.length() > 0 && nameValue != null && nameValue.length() > 0) { if (encodingType.equals("UTF8String")) { name.addElement( new AVA(oid, new UTF8String(nameValue))); } else if (encodingType.equals("PrintableString")) { name.addElement( new AVA(oid, new PrintableString(nameValue))); } else if (encodingType.equals("BMPString")) { name.addElement( new AVA(oid, new BMPString(nameValue))); } else if (encodingType.equals("TeletexString")) { name.addElement( new AVA(oid, new TeletexString(nameValue))); } else if (encodingType.equals("UniversalString")) { name.addElement( new AVA(oid, new UniversalString(nameValue))); } } } catch (Exception e) { System.out.println("CRMFPopClient: Error adding name element: " + elementValue + " Error: " + e.toString()); } return name; } static Name getJssName(boolean enable_encoding, String dn) { X500Name x5Name = null; try { x5Name = new X500Name(dn); } catch (IOException e) { System.out.println("CRMFPopClient: Illegal Subject Name: " + dn + " Error: " + e.toString()); System.out.println("CRMFPopClient: Filling in default Subject Name......"); return null; } Name ret = new Name(); netscape.security.x509.RDN[] names = null; names = x5Name.getNames(); int nameLen = x5Name.getNamesLength(); // System.out.println("x5Name len: " + nameLen); netscape.security.x509.RDN cur = null; for (int i = 0; i < nameLen; i++) { cur = names[i]; String rdnStr = cur.toString(); String[] split = rdnStr.split("="); if (split.length != 2) continue; int n = split[1].indexOf(':'); try { if (split[0].equals("UID")) { if (enable_encoding && isEncoded(split[1])) { ret = addNameElement(ret, new OBJECT_IDENTIFIER("0.9.2342.19200300.100.1.1"), n, split[1]); } else { ret.addElement(new AVA(new OBJECT_IDENTIFIER("0.9.2342.19200300.100.1.1"), new PrintableString(split[1]))); } // System.out.println("UID found : " + split[1]); } if (split[0].equals("C")) { ret.addCountryName(split[1]); // System.out.println("C found : " + split[1]); continue; } if (split[0].equals("CN")) { if (enable_encoding && isEncoded(split[1])) { ret = addNameElement (ret, Name.commonName, n, split[1]); } else { ret.addCommonName(split[1]); } // System.out.println("CN found : " + split[1]); continue; } if (split[0].equals("L")) { if (enable_encoding && isEncoded(split[1])) { ret = addNameElement (ret, Name.localityName, n, split[1]); } else { ret.addLocalityName(split[1]); } // System.out.println("L found : " + split[1]); continue; } if (split[0].equals("O")) { if (enable_encoding && isEncoded(split[1])) { ret = addNameElement (ret, Name.organizationName, n, split[1]); } else { ret.addOrganizationName(split[1]); } // System.out.println("O found : " + split[1]); continue; } if (split[0].equals("ST")) { if (enable_encoding && isEncoded(split[1])) { ret = addNameElement (ret, Name.stateOrProvinceName, n, split[1]); } else { ret.addStateOrProvinceName(split[1]); } // System.out.println("ST found : " + split[1]); continue; } if (split[0].equals("OU")) { if (enable_encoding && isEncoded(split[1])) { ret = addNameElement (ret, Name.organizationalUnitName, n, split[1]); } else { ret.addOrganizationalUnitName(split[1]); } // System.out.println("OU found : " + split[1]); continue; } } catch (Exception e) { System.out.println("CRMFPopClient: Error constructing RDN: " + rdnStr + " Error: " + e.toString()); continue; } } return ret; } }