Example usage for org.bouncycastle.asn1.pkcs DHParameter DHParameter

List of usage examples for org.bouncycastle.asn1.pkcs DHParameter DHParameter

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.pkcs DHParameter DHParameter.

Prototype

public DHParameter(BigInteger p, BigInteger g, int l) 

Source Link

Usage

From source file:org.dcache.xrootd.plugins.authn.gsi.DHSession.java

License:Open Source License

/**
 * Creates an DER-encoded byte sequence from the DHParameter object
 * @param paramspec the DH parameter object
 * @return the DER-encoded byte sequence of the DH Parameter object
 *///from  w  ww. j  ava  2s .  c  om
private static byte[] toDER(DHParameterSpec paramspec) throws IOException {
    DHParameter derParams = new DHParameter(paramspec.getP(), // Prime
            // (public
            // key)
            paramspec.getG(), // generator
            paramspec.getP().bitLength()); // keylength of Prime

    return derParams.getEncoded("DER");
}

From source file:org.jmrtd.Util.java

License:Open Source License

public static SubjectPublicKeyInfo toSubjectPublicKeyInfo(PublicKey publicKey) {
    try {/*w w  w  .  ja  v  a2s . c om*/
        String algorithm = publicKey.getAlgorithm();
        if ("EC".equals(algorithm) || "ECDH".equals(algorithm) || (publicKey instanceof ECPublicKey)) {
            ASN1InputStream asn1In = new ASN1InputStream(publicKey.getEncoded());
            SubjectPublicKeyInfo subjectPublicKeyInfo = new SubjectPublicKeyInfo(
                    (ASN1Sequence) asn1In.readObject());
            asn1In.close();
            AlgorithmIdentifier algorithmIdentifier = subjectPublicKeyInfo.getAlgorithm();
            String algOID = algorithmIdentifier.getAlgorithm().getId();
            if (!SecurityInfo.ID_EC_PUBLIC_KEY.equals(algOID)) {
                throw new IllegalStateException("Was expecting id-ecPublicKey ("
                        + SecurityInfo.ID_EC_PUBLIC_KEY_TYPE + "), found " + algOID);
            }
            ASN1Primitive derEncodedParams = algorithmIdentifier.getParameters().toASN1Primitive();
            X9ECParameters params = null;
            if (derEncodedParams instanceof ASN1ObjectIdentifier) {
                ASN1ObjectIdentifier paramsOID = (ASN1ObjectIdentifier) derEncodedParams;

                /* It's a named curve from X9.62. */
                params = X962NamedCurves.getByOID(paramsOID);
                if (params == null) {
                    throw new IllegalStateException(
                            "Could not find X9.62 named curve for OID " + paramsOID.getId());
                }

                /* Reconstruct the parameters. */
                org.bouncycastle.math.ec.ECPoint generator = params.getG();
                org.bouncycastle.math.ec.ECCurve curve = generator.getCurve();
                generator = curve.createPoint(generator.getX().toBigInteger(), generator.getY().toBigInteger(),
                        false);
                params = new X9ECParameters(params.getCurve(), generator, params.getN(), params.getH(),
                        params.getSeed());
            } else {
                /* It's not a named curve, we can just return the decoded public key info. */
                return subjectPublicKeyInfo;
            }

            if (publicKey instanceof org.bouncycastle.jce.interfaces.ECPublicKey) {
                org.bouncycastle.jce.interfaces.ECPublicKey ecPublicKey = (org.bouncycastle.jce.interfaces.ECPublicKey) publicKey;
                AlgorithmIdentifier id = new AlgorithmIdentifier(
                        subjectPublicKeyInfo.getAlgorithm().getAlgorithm(), params.toASN1Primitive());
                org.bouncycastle.math.ec.ECPoint q = ecPublicKey.getQ();
                /* FIXME: investigate the compressed versus uncompressed point issue. What is allowed in TR03110? -- MO */
                // In case we would like to compress the point:
                // p = p.getCurve().createPoint(p.getX().toBigInteger(), p.getY().toBigInteger(), true);
                subjectPublicKeyInfo = new SubjectPublicKeyInfo(id, q.getEncoded());
                return subjectPublicKeyInfo;
            } else {
                return subjectPublicKeyInfo;
            }
        } else if ("DH".equals(algorithm) || (publicKey instanceof DHPublicKey)) {
            DHPublicKey dhPublicKey = (DHPublicKey) publicKey;
            DHParameterSpec dhSpec = dhPublicKey.getParams();
            return new SubjectPublicKeyInfo(
                    new AlgorithmIdentifier(EACObjectIdentifiers.id_PK_DH,
                            new DHParameter(dhSpec.getP(), dhSpec.getG(), dhSpec.getL()).toASN1Primitive()),
                    new ASN1Integer(dhPublicKey.getY()));
        } else {
            throw new IllegalArgumentException(
                    "Unrecognized key type, found " + publicKey.getAlgorithm() + ", should be DH or ECDH");
        }
    } catch (Exception e) {
        LOGGER.severe("Exception: " + e.getMessage());
        return null;
    }
}