Search in sources :

Example 1 with KeyPairGenerator

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);
}
Also used : KeyPair(java.security.KeyPair) X509CertInfo(org.mozilla.jss.netscape.security.x509.X509CertInfo) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) PK11ECPublicKey(org.mozilla.jss.pkcs11.PK11ECPublicKey) X509CertImpl(org.mozilla.jss.netscape.security.x509.X509CertImpl) CertificateIssuerName(org.mozilla.jss.netscape.security.x509.CertificateIssuerName) KeyPairGenerator(org.mozilla.jss.crypto.KeyPairGenerator) X500Name(org.mozilla.jss.netscape.security.x509.X500Name)

Example 2 with KeyPairGenerator

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();
    }
}
Also used : KeyPair(java.security.KeyPair) CryptoToken(org.mozilla.jss.crypto.CryptoToken) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) CryptoManager(org.mozilla.jss.CryptoManager) KeyPairGenerator(org.mozilla.jss.crypto.KeyPairGenerator) FileInputStream(java.io.FileInputStream) InvalidBERException(org.mozilla.jss.asn1.InvalidBERException) InvalidKeyFormatException(org.mozilla.jss.crypto.InvalidKeyFormatException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) TokenException(org.mozilla.jss.crypto.TokenException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NotInitializedException(org.mozilla.jss.NotInitializedException) InvalidKeyException(java.security.InvalidKeyException) Name(org.mozilla.jss.pkix.primitive.Name)

Example 3 with KeyPairGenerator

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();
    }
}
Also used : KeyPair(java.security.KeyPair) CryptoToken(org.mozilla.jss.crypto.CryptoToken) Calendar(java.util.Calendar) CryptoManager(org.mozilla.jss.CryptoManager) KeyPairGenerator(org.mozilla.jss.crypto.KeyPairGenerator) FileInputStream(java.io.FileInputStream) InvalidBERException(org.mozilla.jss.asn1.InvalidBERException) InvalidKeyFormatException(org.mozilla.jss.crypto.InvalidKeyFormatException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) TokenException(org.mozilla.jss.crypto.TokenException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NotInitializedException(org.mozilla.jss.NotInitializedException) InvalidKeyException(java.security.InvalidKeyException) Name(org.mozilla.jss.pkix.primitive.Name) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) INTEGER(org.mozilla.jss.asn1.INTEGER)

Example 4 with KeyPairGenerator

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);
    }
}
Also used : KeyPair(java.security.KeyPair) CryptoToken(org.mozilla.jss.crypto.CryptoToken) TokenRuntimeException(org.mozilla.jss.crypto.TokenRuntimeException) TokenException(org.mozilla.jss.crypto.TokenException) CryptoManager(org.mozilla.jss.CryptoManager) KeyPairGenerator(org.mozilla.jss.crypto.KeyPairGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyException(java.security.KeyException)

Example 5 with KeyPairGenerator

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.");
}
Also used : KeyPair(java.security.KeyPair) CryptoToken(org.mozilla.jss.crypto.CryptoToken) PK11Token(org.mozilla.jss.pkcs11.PK11Token) PublicKey(java.security.PublicKey) CryptoManager(org.mozilla.jss.CryptoManager) KeyPairGenerator(org.mozilla.jss.crypto.KeyPairGenerator) Signature(org.mozilla.jss.crypto.Signature)

Aggregations

KeyPair (java.security.KeyPair)6 KeyPairGenerator (org.mozilla.jss.crypto.KeyPairGenerator)6 CryptoManager (org.mozilla.jss.CryptoManager)4 CryptoToken (org.mozilla.jss.crypto.CryptoToken)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 PublicKey (java.security.PublicKey)3 TokenException (org.mozilla.jss.crypto.TokenException)3 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InvalidKeyException (java.security.InvalidKeyException)2 SignatureException (java.security.SignatureException)2 CertificateException (java.security.cert.CertificateException)2 ECPublicKey (java.security.interfaces.ECPublicKey)2 RSAPublicKey (java.security.interfaces.RSAPublicKey)2 NotInitializedException (org.mozilla.jss.NotInitializedException)2 InvalidBERException (org.mozilla.jss.asn1.InvalidBERException)2 InvalidKeyFormatException (org.mozilla.jss.crypto.InvalidKeyFormatException)2 CertificateIssuerName (org.mozilla.jss.netscape.security.x509.CertificateIssuerName)2