Example usage for org.bouncycastle.jce.spec ECNamedCurveParameterSpec getName

List of usage examples for org.bouncycastle.jce.spec ECNamedCurveParameterSpec getName

Introduction

In this page you can find the example usage for org.bouncycastle.jce.spec ECNamedCurveParameterSpec getName.

Prototype

public String getName() 

Source Link

Document

return the name of the curve the EC domain parameters belong to.

Usage

From source file:com.pazdev.jose.JWK.java

License:Apache License

/**
 * <p>//ww w. j a  v  a  2s .c o m
 * Converts the keys described in this JWK to JCE {@link Key} objects. The map
 * returned will either be empty, signifying that no keys could be obtained
 * based on the given information, or one of the following keys:
 * </p>
 * <ul>
 * <li>public</li>
 * <li>private</li>
 * <li>secret</li>
 * </ul>
 * @return a map containing all the obtainable keys.
 */
@JsonIgnore
public Map<String, Key> getKeys() {
    HashMap<String, Key> retval = new HashMap<>();
    try {
        if (null != keyType)
            switch (keyType) {
            case "EC": {
                KeyFactory fac = KeyFactory.getInstance("EC", "BC");
                ECNamedCurveParameterSpec ecParamSpecBC = ECNamedCurveTable.getParameterSpec(curve);
                ECNamedCurveSpec ecParamSpec = new ECNamedCurveSpec(ecParamSpecBC.getName(),
                        ecParamSpecBC.getCurve(), ecParamSpecBC.getG(), ecParamSpecBC.getN(),
                        ecParamSpecBC.getH(), ecParamSpecBC.getSeed());
                if (privateKey != null && privateKey.length > 0) {
                    ECPrivateKeySpec privateSpec = new ECPrivateKeySpec(new BigInteger(1, privateKey),
                            ecParamSpec);
                    retval.put("private", fac.generatePrivate(privateSpec));
                }
                if (xCoordinate != null && xCoordinate.length > 0) {
                    ECPublicKeySpec publicSpec = new ECPublicKeySpec(
                            new ECPoint(new BigInteger(1, xCoordinate), new BigInteger(1, yCoordinate)),
                            ecParamSpec);
                    retval.put("public", fac.generatePublic(publicSpec));
                }
                break;
            }
            case "RSA": {
                KeyFactory fac = KeyFactory.getInstance("RSA", "BC");
                BigInteger m = new BigInteger(1, modulus);
                BigInteger e = new BigInteger(1, exponent);
                retval.put("public", fac.generatePublic(new RSAPublicKeySpec(m, e)));
                if (privateKey != null && privateKey.length > 0) {
                    BigInteger d = new BigInteger(1, privateKey);
                    BigInteger p, q, dp, dq, qi;
                    RSAOtherPrimeInfo[] otherPrimes = null;
                    if (firstPrimeFactor != null && firstPrimeFactor.length > 0) {
                        p = new BigInteger(1, firstPrimeFactor);
                    } else {
                        p = null;
                    }
                    if (secondPrimeFactor != null && secondPrimeFactor.length > 0) {
                        q = new BigInteger(1, secondPrimeFactor);
                    } else {
                        q = null;
                    }
                    if (firstFactorCrtExponent != null && firstFactorCrtExponent.length > 0) {
                        dp = new BigInteger(1, firstFactorCrtExponent);
                    } else {
                        dp = null;
                    }
                    if (secondFactorCrtExponent != null && secondFactorCrtExponent.length > 0) {
                        dq = new BigInteger(1, secondFactorCrtExponent);
                    } else {
                        dq = null;
                    }
                    if (firstCrtCoefficient != null && firstCrtCoefficient.length > 0) {
                        qi = new BigInteger(1, firstCrtCoefficient);
                    } else {
                        qi = null;
                    }
                    if (otherPrimesInfo != null && otherPrimesInfo.size() > 0) {
                        otherPrimes = new RSAOtherPrimeInfo[otherPrimesInfo.size()];
                        for (int i = otherPrimes.length - 1; i >= 0; --i) {
                            BigInteger or, od, ot;
                            OtherPrimeInfo other = otherPrimesInfo.get(i);
                            if (other.primeFactor != null && other.primeFactor.length > 0) {
                                or = new BigInteger(1, other.primeFactor);
                            } else {
                                or = null;
                            }
                            if (other.factorCrtExponent != null && other.factorCrtExponent.length > 0) {
                                od = new BigInteger(1, other.factorCrtExponent);
                            } else {
                                od = null;
                            }
                            if (other.factorCrtCoefficient != null && other.factorCrtCoefficient.length > 0) {
                                ot = new BigInteger(1, other.factorCrtCoefficient);
                            } else {
                                ot = null;
                            }
                            otherPrimes[i] = new RSAOtherPrimeInfo(or, od, ot);
                        }
                    }
                    if (p != null || q != null || dp != null || dq != null) {
                        if (otherPrimes != null) {
                            RSAMultiPrimePrivateCrtKeySpec spec = new RSAMultiPrimePrivateCrtKeySpec(m, e, d, p,
                                    q, dp, dq, qi, otherPrimes);
                            retval.put("private", fac.generatePrivate(spec));
                        } else {
                            RSAPrivateCrtKeySpec spec = new RSAPrivateCrtKeySpec(m, e, d, p, q, dp, dq, qi);
                            retval.put("private", fac.generatePrivate(spec));
                        }
                    } else {
                        RSAPrivateKeySpec spec = new RSAPrivateKeySpec(m, d);
                        retval.put("private", fac.generatePrivate(spec));
                    }
                }
                break;
            }
            case "oct":
                retval.put("secret", new SecretKeySpec(keyValue, algorithm != null ? algorithm : "AES"));
                break;
            }
    } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException e) {
        throw new RuntimeException(e);
    }
    return retval;
}

From source file:org.jmrtd.test.lds.CardSecurityFileTest.java

License:Open Source License

public CardSecurityFile createConstructedSample() {
    try {//from w  w w  .  j a v a  2s  . com
        SecurityInfo caSecurityInfo = new ChipAuthenticationInfo(
                ChipAuthenticationInfo.ID_CA_ECDH_AES_CBC_CMAC_256_OID, ChipAuthenticationInfo.VERSION_1);
        SecurityInfo taSecurityInfo = new TerminalAuthenticationInfo();

        Set<SecurityInfo> securityInfos = new HashSet<SecurityInfo>(2);
        securityInfos.add(caSecurityInfo);
        securityInfos.add(taSecurityInfo);

        /* Generate a document signer certificate and private signing key. */
        String digestAlgorithm = "SHA-256";
        String digestEncryptionAlgorithm = "SHA256withECDSA";

        ECNamedCurveParameterSpec bcParamSpec = ECNamedCurveTable.getParameterSpec("brainpoolp256r1");
        ECParameterSpec jceParamSpec = new ECNamedCurveSpec(bcParamSpec.getName(), bcParamSpec.getCurve(),
                bcParamSpec.getG(), bcParamSpec.getN(), bcParamSpec.getH(), bcParamSpec.getSeed());

        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC");
        keyPairGenerator.initialize(jceParamSpec);

        KeyPair cscaKeyPair = keyPairGenerator.generateKeyPair();
        KeyPair dsKeyPair = keyPairGenerator.generateKeyPair();

        Calendar calendar = Calendar.getInstance();

        Date dateOfIssuing = calendar.getTime();
        calendar.add(Calendar.MONTH, 2);
        Date dateOfDSExpiry = calendar.getTime();
        calendar.add(Calendar.YEAR, 5);
        Date dateOfCSCAExpiry = calendar.getTime();

        String issuer = "C=UT, O=Gov, CN=CSCA";
        String subject = "C=UT, O=Gov, CN=DS-01";

        X509Certificate cscaCert = CertificateUtil.createCertificate(issuer, issuer, dateOfIssuing,
                dateOfCSCAExpiry, cscaKeyPair.getPublic(), cscaKeyPair.getPrivate(), digestEncryptionAlgorithm);

        X509Certificate dsCert = CertificateUtil.createCertificate(issuer, subject, dateOfIssuing,
                dateOfDSExpiry, dsKeyPair.getPublic(), cscaKeyPair.getPrivate(), digestEncryptionAlgorithm);

        /* Create the card security file. */
        return new CardSecurityFile(digestAlgorithm, digestEncryptionAlgorithm, securityInfos,
                dsKeyPair.getPrivate(), dsCert, "BC");
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Exception during construction of sample", e);
        fail(e.getMessage());
        return null;
    }
}

From source file:org.jmrtd.Util.java

License:Open Source License

/**
 * Translates internal BC named curve spec to BC provided JCA compliant named curve spec.
 *
 * @param namedParamSpec a named EC parameter spec
 *
 * @return a JCA compliant named EC parameter spec
 *///from ww w .j ava 2s .  c  o m
public static org.bouncycastle.jce.spec.ECNamedCurveSpec toECNamedCurveSpec(
        org.bouncycastle.jce.spec.ECNamedCurveParameterSpec namedParamSpec) {
    String name = namedParamSpec.getName();
    org.bouncycastle.math.ec.ECCurve curve = namedParamSpec.getCurve();
    org.bouncycastle.math.ec.ECPoint generator = namedParamSpec.getG();
    BigInteger order = namedParamSpec.getN();
    BigInteger coFactor = namedParamSpec.getH();
    byte[] seed = namedParamSpec.getSeed();
    return new org.bouncycastle.jce.spec.ECNamedCurveSpec(name, curve, generator, order, coFactor, seed);
}

From source file:org.jruby.ext.openssl.PKeyEC.java

License:Open Source License

private static ECParameterSpec getParamSpec(final String curveName) {
    ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(curveName);
    return new ECNamedCurveSpec(spec.getName(), spec.getCurve(), spec.getG(), spec.getN(), spec.getH(),
            spec.getSeed());/*  www.  ja  va2 s .  c o m*/
}

From source file:org.springframework.security.jwt.crypto.sign.EllipticCurveKeyHelper.java

License:Apache License

static ECPublicKey createPublicKey(final BigInteger x, final BigInteger y, final String curve) {
    ECNamedCurveParameterSpec curveParameterSpec;
    if ((curveParameterSpec = ECNamedCurveTable.getParameterSpec(curve)) == null) {
        throw new IllegalArgumentException("Unsupported named curve: " + curve);
    }/*w ww  .  j ava 2  s .  c om*/

    ECParameterSpec parameterSpec = new ECNamedCurveSpec(curveParameterSpec.getName(),
            curveParameterSpec.getCurve(), curveParameterSpec.getG(), curveParameterSpec.getN());
    ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(new ECPoint(x, y), parameterSpec);

    try {
        return (ECPublicKey) KeyFactory.getInstance("EC").generatePublic(publicKeySpec);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}