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

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

Introduction

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

Prototype

public static ASN1ObjectIdentifier getNamedCurveOid(ECParameterSpec ecParameterSpec) 

Source Link

Usage

From source file:org.xipki.commons.security.pkcs11.emulator.EmulatorP11Slot.java

License:Open Source License

private void savePkcs11PublicKey(final byte[] id, final String label, final PublicKey publicKey)
        throws P11TokenException {
    String hexId = Hex.toHexString(id).toLowerCase();

    StringBuilder sb = new StringBuilder(100);
    sb.append(PROP_ID).append('=').append(hexId).append('\n');
    sb.append(PROP_LABEL).append('=').append(label).append('\n');

    if (publicKey instanceof RSAPublicKey) {
        sb.append(PROP_ALGORITHM).append('=');
        sb.append(PKCSObjectIdentifiers.rsaEncryption.getId());
        sb.append('\n');

        sb.append(PROP_RSA_MODUS).append('=');

        RSAPublicKey rsaKey = (RSAPublicKey) publicKey;
        sb.append(Hex.toHexString(rsaKey.getModulus().toByteArray()));
        sb.append('\n');

        sb.append(PROP_RSA_PUBLIC_EXPONENT).append('=');
        sb.append(Hex.toHexString(rsaKey.getPublicExponent().toByteArray()));
        sb.append('\n');
    } else if (publicKey instanceof DSAPublicKey) {
        sb.append(PROP_ALGORITHM).append('=');
        sb.append(X9ObjectIdentifiers.id_dsa.getId());
        sb.append('\n');

        sb.append(PROP_DSA_PRIME).append('=');
        DSAPublicKey dsaKey = (DSAPublicKey) publicKey;
        sb.append(Hex.toHexString(dsaKey.getParams().getP().toByteArray()));
        sb.append('\n');

        sb.append(PROP_DSA_SUBPRIME).append('=');
        sb.append(Hex.toHexString(dsaKey.getParams().getQ().toByteArray()));
        sb.append('\n');

        sb.append(PROP_DSA_BASE).append('=');
        sb.append(Hex.toHexString(dsaKey.getParams().getG().toByteArray()));
        sb.append('\n');

        sb.append(PROP_DSA_VALUE).append('=');
        sb.append(Hex.toHexString(dsaKey.getY().toByteArray()));
        sb.append('\n');
    } else if (publicKey instanceof ECPublicKey) {
        sb.append(PROP_ALGORITHM).append('=');
        sb.append(X9ObjectIdentifiers.id_ecPublicKey.getId());
        sb.append('\n');

        ECPublicKey ecKey = (ECPublicKey) publicKey;
        ECParameterSpec paramSpec = ecKey.getParams();

        // ecdsaParams
        org.bouncycastle.jce.spec.ECParameterSpec bcParamSpec = EC5Util.convertSpec(paramSpec, false);
        ASN1ObjectIdentifier curveOid = ECUtil.getNamedCurveOid(bcParamSpec);
        if (curveOid == null) {
            throw new P11TokenException("EC public key is not of namedCurve");
        }// w w w. j  a v  a  2  s . c o  m

        byte[] encodedParams;
        try {
            if (namedCurveSupported) {
                encodedParams = curveOid.getEncoded();
            } else {
                encodedParams = ECNamedCurveTable.getByOID(curveOid).getEncoded();
            }
        } catch (IOException | NullPointerException ex) {
            throw new P11TokenException(ex.getMessage(), ex);
        }

        sb.append(PROP_EC_ECDSA_PARAMS).append('=');
        sb.append(Hex.toHexString(encodedParams));
        sb.append('\n');

        // EC point
        java.security.spec.ECPoint pointW = ecKey.getW();
        int keysize = (paramSpec.getOrder().bitLength() + 7) / 8;
        byte[] ecPoint = new byte[1 + keysize * 2];
        ecPoint[0] = 4; // uncompressed
        bigIntToBytes("Wx", pointW.getAffineX(), ecPoint, 1, keysize);
        bigIntToBytes("Wy", pointW.getAffineY(), ecPoint, 1 + keysize, keysize);

        byte[] encodedEcPoint;
        try {
            encodedEcPoint = new DEROctetString(ecPoint).getEncoded();
        } catch (IOException ex) {
            throw new P11TokenException("could not ASN.1 encode the ECPoint");
        }
        sb.append(PROP_EC_EC_POINT).append('=');
        sb.append(Hex.toHexString(encodedEcPoint));
        sb.append('\n');
    } else {
        throw new IllegalArgumentException("unsupported public key " + publicKey.getClass().getName());
    }

    try {
        IoUtil.save(new File(pubKeyDir, hexId + INFO_FILE_SUFFIX), sb.toString().getBytes());
    } catch (IOException ex) {
        throw new P11TokenException(ex.getMessage(), ex);
    }
}

From source file:org.xipki.commons.security.util.KeyUtil.java

License:Open Source License

private static ASN1ObjectIdentifier detectCurveOid(final ECParameterSpec paramSpec) {
    org.bouncycastle.jce.spec.ECParameterSpec bcParamSpec = EC5Util.convertSpec(paramSpec, false);
    return ECUtil.getNamedCurveOid(bcParamSpec);
}