Example usage for org.bouncycastle.asn1.x9 X9ECParameters getInstance

List of usage examples for org.bouncycastle.asn1.x9 X9ECParameters getInstance

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.x9 X9ECParameters getInstance.

Prototype

public static X9ECParameters getInstance(Object obj) 

Source Link

Usage

From source file:de.tsenger.animamea.asn1.DomainParameter.java

License:Open Source License

/**
 * Extrahiert aus dem AlogorithmIdentifier die Parameter fr DH oder ECDH.
 * Es werden standardisierte DomainParameter und explizite DP erkannt.
 * @param algorithm OID//ww  w . ja  va  2s .co  m
 */
public DomainParameter(AlgorithmIdentifier aid) {
    if (aid.getAlgorithm().toString().equals(BSIObjectIdentifiers.standardizedDomainParameters.toString())) {
        int dpref = ((ASN1Integer) aid.getParameters()).getPositiveValue().intValue();
        getParameters(dpref);
    }

    else if (aid.getAlgorithm().toString().equals("1.2.840.10045.2.1")) {
        X9ECParameters x9ecp = X9ECParameters.getInstance(aid.getParameters());
        ecSpec = new ECParameterSpec(x9ecp.getCurve(), x9ecp.getG(), x9ecp.getN());
    }

    //TODO properitre DH Domain Parameter

    else
        throw new UnsupportedOperationException(
                "unsupported Domain Parameters. Algorithm OID: " + aid.getAlgorithm().toString());
}

From source file:de.tsenger.sandbox.PKCS8PrivateKey.java

License:Open Source License

public static void main(String[] args) throws IOException {
    byte[] pkBytes = readBinaryFile(
            "/home/tsenger/Dokumente/Programming/animamea/certs/Key_DEATTIDBSIDE003.pkcs8");

    DERSequence pkSeq = (DERSequence) DERSequence.fromByteArray(pkBytes);

    PrivateKeyInfo pkInfo = new PrivateKeyInfo(pkSeq);

    AlgorithmIdentifier ecPublicKey = pkInfo.getPrivateKeyAlgorithm();
    System.out.println(ecPublicKey.getAlgorithm().toString());
    System.out.println(HexString.bufferToHex(ecPublicKey.getEncoded(null)));

    X9ECParameters ecp = X9ECParameters.getInstance(ecPublicKey.getParameters());

    System.out.println("N: \n" + HexString.bufferToHex(Converter.bigIntToByteArray(ecp.getN())));

    ECPrivateKey ecpk2 = ECPrivateKey.getInstance(ecPublicKey);
    //ECPrivateKey.getInstance(pkInfo.getPrivateKey());
    System.out.println("private Key: \n" + HexString.bufferToHex(Converter.bigIntToByteArray(ecpk2.getKey())));

}

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 a  v a 2 s.c o  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.security.p11.sun.ECParameters.java

License:Open Source License

protected void engineInit(final byte[] params) throws IOException {
    if (params.length < 30) {
        try {//from w ww  . j a  v a  2  s  .  c o  m
            ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) ASN1ObjectIdentifier.fromByteArray(params);
            ECParameterSpec spec = SunNamedCurveExtender.lookupCurve(oid.getId());
            if (spec == null) {
                throw new IOException("unknown named curve: " + oid);
            }

            namedCurve = spec;
            return;
        } catch (IllegalArgumentException e) {
        }
    }

    // The code below is incomplete.
    // It is left as a starting point for a complete parsing implementation.
    X9ECParameters x9EcParams = X9ECParameters.getInstance(params);
    ECCurve curve = x9EcParams.getCurve();

    ECNamedCurveSpec ecNamedCurveSpec = new ECNamedCurveSpec("dummy", curve, x9EcParams.getG(),
            x9EcParams.getN(), x9EcParams.getH());

    ECParameterSpec spec = new ECParameterSpec(ecNamedCurveSpec.getCurve(), ecNamedCurveSpec.getGenerator(),
            ecNamedCurveSpec.getOrder(), ecNamedCurveSpec.getCofactor());
    try {
        engineInit(spec);
    } catch (InvalidParameterSpecException e) {
        throw new IOException("InvalidParameterSpecException: " + e.getMessage(), e);
    }
}