Example usage for org.bouncycastle.jcajce.provider.asymmetric.util ECUtil getNamedCurveByOid

List of usage examples for org.bouncycastle.jcajce.provider.asymmetric.util ECUtil getNamedCurveByOid

Introduction

In this page you can find the example usage for org.bouncycastle.jcajce.provider.asymmetric.util ECUtil getNamedCurveByOid.

Prototype

public static X9ECParameters getNamedCurveByOid(ASN1ObjectIdentifier oid) 

Source Link

Usage

From source file:org.cryptacular.asn.OpenSSLPrivateKeyDecoder.java

License:Open Source License

@Override
protected AsymmetricKeyParameter decodeASN1(final byte[] encoded) {
    final ASN1Object o;
    try {//from  w ww. j  av  a2  s.  co m
        o = ASN1Primitive.fromByteArray(encoded);
    } catch (Exception e) {
        throw new IllegalArgumentException("Invalid encoded key");
    }

    final AsymmetricKeyParameter key;
    if (o instanceof ASN1ObjectIdentifier) {
        // EC private key with named curve in the default OpenSSL format emitted
        // by
        //
        // openssl ecparam -name xxxx -genkey
        //
        // which is the concatenation of the named curve OID and a sequence of 1
        // containing the private point
        final ASN1ObjectIdentifier oid = ASN1ObjectIdentifier.getInstance(o);
        final int len = encoded[1];
        final byte[] privatePart = new byte[encoded.length - len - 2];
        System.arraycopy(encoded, len + 2, privatePart, 0, privatePart.length);

        final ASN1Sequence seq = ASN1Sequence.getInstance(privatePart);
        final X9ECParameters params = ECUtil.getNamedCurveByOid(oid);
        key = new ECPrivateKeyParameters(ASN1Integer.getInstance(seq.getObjectAt(0)).getValue(),
                new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH(),
                        params.getSeed()));
    } else {
        // OpenSSL "traditional" format is an ASN.1 sequence of key parameters

        // Detect key type based on number and types of parameters:
        // RSA -> {version, mod, pubExp, privExp, prime1, prime2, exp1, exp2, c}
        // DSA -> {version, p, q, g, pubExp, privExp}
        // EC ->  {version, privateKey, parameters, publicKey}
        final ASN1Sequence sequence = ASN1Sequence.getInstance(o);
        if (sequence.size() == 9) {
            // RSA private certificate key
            key = new RSAPrivateCrtKeyParameters(ASN1Integer.getInstance(sequence.getObjectAt(1)).getValue(),
                    ASN1Integer.getInstance(sequence.getObjectAt(2)).getValue(),
                    ASN1Integer.getInstance(sequence.getObjectAt(3)).getValue(),
                    ASN1Integer.getInstance(sequence.getObjectAt(4)).getValue(),
                    ASN1Integer.getInstance(sequence.getObjectAt(5)).getValue(),
                    ASN1Integer.getInstance(sequence.getObjectAt(6)).getValue(),
                    ASN1Integer.getInstance(sequence.getObjectAt(7)).getValue(),
                    ASN1Integer.getInstance(sequence.getObjectAt(8)).getValue());
        } else if (sequence.size() == 6) {
            // DSA private key
            key = new DSAPrivateKeyParameters(ASN1Integer.getInstance(sequence.getObjectAt(5)).getValue(),
                    new DSAParameters(ASN1Integer.getInstance(sequence.getObjectAt(1)).getValue(),
                            ASN1Integer.getInstance(sequence.getObjectAt(2)).getValue(),
                            ASN1Integer.getInstance(sequence.getObjectAt(3)).getValue()));
        } else if (sequence.size() == 4) {
            // EC private key with explicit curve
            final X9ECParameters params = X9ECParameters
                    .getInstance(ASN1TaggedObject.getInstance(sequence.getObjectAt(2)).getObject());
            key = new ECPrivateKeyParameters(
                    new BigInteger(ASN1OctetString.getInstance(sequence.getObjectAt(1)).getOctets()),
                    new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH(),
                            params.getSeed()));
        } else {
            throw new IllegalArgumentException("Invalid OpenSSL traditional private key format.");
        }
    }
    return key;
}

From source file:org.xipki.ca.api.profile.x509.BaseX509Certprofile.java

License:Open Source License

private static void checkECSubjectPublicKeyInfo(final ASN1ObjectIdentifier curveOid, final byte[] encoded)
        throws BadCertTemplateException {
    Integer expectedLength = ecCurveFieldSizes.get(curveOid);
    if (expectedLength == null) {
        X9ECParameters ecP = ECUtil.getNamedCurveByOid(curveOid);
        ECCurve curve = ecP.getCurve();//from ww  w.ja  v  a 2s .  c  o  m
        expectedLength = (curve.getFieldSize() + 7) / 8;
        ecCurveFieldSizes.put(curveOid, expectedLength);
    }

    switch (encoded[0]) {
    case 0x02: // compressed
    case 0x03: // compressed
    {
        if (encoded.length != (expectedLength + 1)) {
            throw new BadCertTemplateException("incorrect length for compressed encoding");
        }
        break;
    }
    case 0x04: // uncompressed
    case 0x06: // hybrid
    case 0x07: // hybrid
    {
        if (encoded.length != (2 * expectedLength + 1)) {
            throw new BadCertTemplateException("incorrect length for uncompressed/hybrid encoding");
        }
        break;
    }
    default:
        throw new BadCertTemplateException("invalid point encoding 0x" + Integer.toString(encoded[0], 16));
    }
}

From source file:org.xipki.ca.qa.impl.X509CertprofileQAImpl.java

License:Open Source License

private static void checkECSubjectPublicKeyInfo(final ASN1ObjectIdentifier curveOid, final byte[] encoded)
        throws BadCertTemplateException {
    Integer expectedLength = ecCurveFieldSizes.get(curveOid);
    if (expectedLength == null) {
        X9ECParameters ecP = ECUtil.getNamedCurveByOid(curveOid);
        ECCurve curve = ecP.getCurve();/*ww  w.jav a2  s . c o m*/
        expectedLength = (curve.getFieldSize() + 7) / 8;
        ecCurveFieldSizes.put(curveOid, expectedLength);
    }

    switch (encoded[0]) {
    case 0x02: // compressed
    case 0x03: // compressed
    {
        if (encoded.length != (expectedLength + 1)) {
            throw new BadCertTemplateException("incorrect length for compressed encoding");
        }
        break;
    }
    case 0x04: // uncompressed
    case 0x06: // hybrid
    case 0x07: // hybrid
    {
        if (encoded.length != (2 * expectedLength + 1)) {
            throw new BadCertTemplateException("incorrect length for uncompressed/hybrid encoding");
        }
        break;
    }
    default:
        throw new BadCertTemplateException("invalid point encoding 0x" + Integer.toString(encoded[0], 16));
    }// end switch
}

From source file:org.xipki.pki.ca.api.profile.x509.BaseX509Certprofile.java

License:Open Source License

private static void checkEcSubjectPublicKeyInfo(final ASN1ObjectIdentifier curveOid, final byte[] encoded)
        throws BadCertTemplateException {
    ParamUtil.requireNonNull("curveOid", curveOid);
    ParamUtil.requireNonNull("encoded", encoded);
    ParamUtil.requireMin("encoded.length", encoded.length, 1);

    Integer expectedLength = ecCurveFieldSizes.get(curveOid);
    if (expectedLength == null) {
        X9ECParameters ecP = ECUtil.getNamedCurveByOid(curveOid);
        ECCurve curve = ecP.getCurve();// w  w  w  . jav a  2s . com
        expectedLength = (curve.getFieldSize() + 7) / 8;
        ecCurveFieldSizes.put(curveOid, expectedLength);
    }

    switch (encoded[0]) {
    case 0x02: // compressed
    case 0x03: // compressed
        if (encoded.length != (expectedLength + 1)) {
            throw new BadCertTemplateException("incorrect length for compressed encoding");
        }
        break;
    case 0x04: // uncompressed
    case 0x06: // hybrid
    case 0x07: // hybrid
        if (encoded.length != (2 * expectedLength + 1)) {
            throw new BadCertTemplateException("incorrect length for uncompressed/hybrid encoding");
        }
        break;
    default:
        throw new BadCertTemplateException(String.format("invalid point encoding 0x%02x", encoded[0]));
    }
}

From source file:org.xipki.pki.ca.qa.PublicKeyChecker.java

License:Open Source License

private static void checkECSubjectPublicKeyInfo(final ASN1ObjectIdentifier curveOid, final byte[] encoded)
        throws BadCertTemplateException {
    Integer expectedLength = EC_CURVEFIELD_SIZES.get(curveOid);
    if (expectedLength == null) {
        X9ECParameters ecP = ECUtil.getNamedCurveByOid(curveOid);
        ECCurve curve = ecP.getCurve();/*from   w w  w. j a  va2  s . c om*/
        expectedLength = (curve.getFieldSize() + 7) / 8;
        EC_CURVEFIELD_SIZES.put(curveOid, expectedLength);
    }

    switch (encoded[0]) {
    case 0x02: // compressed
    case 0x03: // compressed
        if (encoded.length != (expectedLength + 1)) {
            throw new BadCertTemplateException("incorrect length for compressed encoding");
        }
        break;
    case 0x04: // uncompressed
    case 0x06: // hybrid
    case 0x07: // hybrid
        if (encoded.length != (2 * expectedLength + 1)) {
            throw new BadCertTemplateException("incorrect length for uncompressed/hybrid encoding");
        }
        break;
    default:
        throw new BadCertTemplateException("invalid point encoding 0x" + Integer.toString(encoded[0], 16));
    } // end switch
}