use of org.mozilla.jss.crypto.KeyPairGenerator in project jss by dogtagpki.
the class X509CertTest method testEC.
public static void testEC(CryptoToken token, Date notBefore, Date notAfter) throws Exception {
X509CertImpl certImpl = null;
X509CertInfo certInfo = null;
KeyPairGenerator gen = token.getKeyPairGenerator(KeyPairAlgorithm.EC);
gen.initialize(gen.getCurveCodeByName("secp521r1"));
KeyPair keypairCA = gen.genKeyPair();
testKeys(keypairCA);
PublicKey pubCA = keypairCA.getPublic();
gen.initialize(gen.getCurveCodeByName("secp521r1"));
KeyPair keypairUser = gen.genKeyPair();
testKeys(keypairUser);
PublicKey pubUser = keypairUser.getPublic();
CertificateIssuerName issuernameObj = new CertificateIssuerName(new X500Name(issuerDN));
certInfo = createX509CertInfo(convertPublicKeyToX509Key(pubUser), BigInteger.valueOf(1), issuernameObj, subjectDN, notBefore, notAfter, "SHA256withEC");
certImpl = new X509CertImpl(certInfo);
certImpl.sign(keypairCA.getPrivate(), "SHA256withEC");
String certOutput = certImpl.toString();
System.out.println("Test certificate output: \n" + certOutput);
}
use of org.mozilla.jss.crypto.KeyPairGenerator in project jss by dogtagpki.
the class CertificationRequest method main.
public static void main(String[] argv) {
try {
if (argv.length > 2 || argv.length < 1) {
System.out.println("Usage: CertificationRequest <dbdir> [<certfile>]");
System.exit(0);
}
CryptoManager.initialize(argv[0]);
CryptoManager cm = CryptoManager.getInstance();
CertificationRequest cert;
// read in a cert
FileInputStream fis = new FileInputStream(argv[1]);
try (BufferedInputStream bis = new BufferedInputStream(fis)) {
cert = (CertificationRequest) CertificationRequest.getTemplate().decode(bis);
}
CertificationRequestInfo info = cert.getInfo();
info.print(System.out);
// X509CertificationRequest hardcore = cm.findCertByNickname("Hardcore");
// PublicKey key = hardcore.getPublicKey();
cert.verify();
System.out.println("verified");
FileOutputStream fos = new FileOutputStream("certinfo.der");
info.encode(fos);
fos.close();
// make a new public key
CryptoToken token = cm.getInternalKeyStorageToken();
KeyPairGenerator kpg = token.getKeyPairGenerator(KeyPairAlgorithm.RSA);
kpg.initialize(512);
System.out.println("Generating a new key pair...");
KeyPair kp = kpg.genKeyPair();
System.out.println("Generated key pair");
// set the CertificationRequest's public key
info.setSubjectPublicKeyInfo(kp.getPublic());
// make new Name
Name name = new Name();
name.addCommonName("asldkj");
name.addCountryName("US");
name.addOrganizationName("Some Corp");
name.addOrganizationalUnitName("Some Org Unit");
name.addLocalityName("Silicon Valley");
name.addStateOrProvinceName("California");
info.setSubject(name);
System.out.println("About to create a new cert request...");
// create a new cert requestfrom this certReqinfo
CertificationRequest genCert = new CertificationRequest(info, kp.getPrivate(), SignatureAlgorithm.RSASignatureWithMD5Digest);
System.out.println("Created new cert request");
genCert.verify();
System.out.println("Cert verifies!");
fos = new FileOutputStream("gencert.der");
genCert.encode(fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.mozilla.jss.crypto.KeyPairGenerator in project jss by dogtagpki.
the class Certificate method main.
public static void main(String[] argv) {
try {
if (argv.length > 2 || argv.length < 1) {
System.out.println("Usage: Certificate <dbdir> [<certfile>]");
System.exit(0);
}
CryptoManager.initialize(argv[0]);
CryptoManager cm = CryptoManager.getInstance();
Certificate cert;
// read in a cert
FileInputStream fis = new FileInputStream(argv[1]);
try (BufferedInputStream bis = new BufferedInputStream(fis)) {
cert = (Certificate) Certificate.getTemplate().decode(bis);
}
CertificateInfo info = cert.getInfo();
info.print(System.out);
// X509Certificate hardcore = cm.findCertByNickname("Hardcore");
// PublicKey key = hardcore.getPublicKey();
cert.verify();
System.out.println("verified");
FileOutputStream fos = new FileOutputStream("certinfo.der");
info.encode(fos);
fos.close();
// make a new public key
CryptoToken token = cm.getInternalKeyStorageToken();
KeyPairGenerator kpg = token.getKeyPairGenerator(KeyPairAlgorithm.RSA);
kpg.initialize(512);
System.out.println("Generating a new key pair...");
KeyPair kp = kpg.genKeyPair();
System.out.println("Generated key pair");
// set the certificate's public key
info.setSubjectPublicKeyInfo(kp.getPublic());
// increment serial number
int newSerial = info.getSerialNumber().intValue() + 1;
info.setSerialNumber(new INTEGER(newSerial));
// make new Name
Name name = new Name();
name.addCommonName("Stra\u00dfenverk\u00e4ufer 'R' Us");
name.addCountryName("US");
name.addOrganizationName("Some Corporation");
name.addOrganizationalUnitName("some org unit?");
name.addLocalityName("Silicon Valley");
name.addStateOrProvinceName("California");
info.setIssuer(name);
info.setSubject(name);
// set validity
Calendar cal = Calendar.getInstance();
cal.set(1997, Calendar.APRIL, 1);
info.setNotBefore(cal.getTime());
cal.set(2010, Calendar.APRIL, 1);
info.setNotAfter(cal.getTime());
System.out.println("About to create a new cert...");
// create a new cert from this certinfo
Certificate genCert = new Certificate(info, kp.getPrivate(), SignatureAlgorithm.RSASignatureWithMD5Digest);
System.out.println("Created new cert");
genCert.verify();
System.out.println("Cert verifies!");
fos = new FileOutputStream("gencert.der");
genCert.encode(fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.mozilla.jss.crypto.KeyPairGenerator in project candlepin by candlepin.
the class JSSPKIUtility method generateKeyPair.
/**
* {@inheritDoc}
*/
@Override
public KeyPair generateKeyPair() throws KeyException {
try {
CryptoManager manager = JSSProviderLoader.getCryptoManager(true);
CryptoToken token = manager.getInternalKeyStorageToken();
KeyPairGenerator kpgen = token.getKeyPairGenerator(KeyPairAlgorithm.fromString(KEY_ALGORITHM));
kpgen.temporaryPairs(true);
kpgen.sensitivePairs(true);
// probably extraneous; does nothing in FIPS mode
kpgen.extractablePairs(true);
kpgen.initialize(KEY_SIZE);
KeyPair keypair = kpgen.genKeyPair();
return this.buildInsecureKeyPair(keypair);
} catch (NoSuchAlgorithmException | TokenException | TokenRuntimeException e) {
throw new KeyException(e);
}
}
use of org.mozilla.jss.crypto.KeyPairGenerator in project jss by dogtagpki.
the class SigTest method main.
public static void main(String[] args) throws Exception {
CryptoToken token;
CryptoManager manager;
byte[] data = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
byte[] signature;
Signature signer;
Signature signerPSS;
PublicKey pubk;
KeyPairGenerator kpgen;
KeyPair keyPair;
if (args.length < 2 || args.length > 3) {
usage();
System.exit(1);
}
manager = CryptoManager.getInstance();
manager.setPasswordCallback(new FilePasswordCallback(args[1]));
/* Print out list of available tokens */
Enumeration<CryptoToken> en = manager.getAllTokens();
System.out.println("Available tokens:");
while (en.hasMoreElements()) {
PK11Token p = (PK11Token) en.nextElement();
System.out.println(" token : " + p.getName());
}
if (args.length >= 3) {
token = manager.getTokenByName(args[2]);
} else {
// get default internal key storage token
token = manager.getInternalKeyStorageToken();
}
// Generate an RSA keypair
kpgen = token.getKeyPairGenerator(KeyPairAlgorithm.RSA);
kpgen.initialize(Policy.RSA_MINIMUM_KEY_SIZE);
KeyPairGeneratorSpi.Usage[] usages = { KeyPairGeneratorSpi.Usage.SIGN, KeyPairGeneratorSpi.Usage.VERIFY };
KeyPairGeneratorSpi.Usage[] usages_mask = { KeyPairGeneratorSpi.Usage.SIGN, KeyPairGeneratorSpi.Usage.VERIFY };
kpgen.setKeyPairUsages(usages, usages_mask);
keyPair = kpgen.genKeyPair();
// RSA SHA256
signer = token.getSignatureContext(SignatureAlgorithm.RSASignatureWithSHA256Digest);
System.out.println("Created a signing context");
signer.initSign((org.mozilla.jss.crypto.PrivateKey) keyPair.getPrivate());
System.out.println("initialized the signing operation");
signer.update(data);
System.out.println("updated signature with data");
signature = signer.sign();
System.out.println("Successfully signed!");
signer.initVerify(keyPair.getPublic());
System.out.println("initialized verification");
signer.update(data);
System.out.println("updated verification with data");
if (signer.verify(signature)) {
System.out.println("Signature Verified Successfully!");
} else {
throw new Exception("ERROR: Signature failed to verify.");
}
signerPSS = token.getSignatureContext(SignatureAlgorithm.RSAPSSSignatureWithSHA256Digest);
signerPSS.initSign((org.mozilla.jss.crypto.PrivateKey) keyPair.getPrivate());
signerPSS.update(data);
signature = signerPSS.sign();
System.out.println("PSS Successfully signed!");
signerPSS.initVerify(keyPair.getPublic());
signerPSS.update(data);
System.out.println("updated verification with data");
if (signerPSS.verify(signature)) {
System.out.println("PSS Signature Verified Successfully!");
} else {
throw new Exception("ERROR: PSS Signature failed to verify.");
}
System.out.println("SigTest passed.");
}
Aggregations