Example usage for org.bouncycastle.asn1 DLSequence getEncoded

List of usage examples for org.bouncycastle.asn1 DLSequence getEncoded

Introduction

In this page you can find the example usage for org.bouncycastle.asn1 DLSequence getEncoded.

Prototype

public byte[] getEncoded() throws IOException 

Source Link

Document

Return the default BER or DER encoding for this object.

Usage

From source file:org.certificateservices.custom.c2x.its.crypto.DefaultCryptoManager.java

License:Open Source License

/**
 * @see org.certificateservices.custom.c2x.its.crypto.CryptoManager#verifySignature(byte[], Signature, PublicKey)
 *//*from  w  w w  .  java2s  .  c o  m*/
@Override
public boolean verifySignature(byte[] message, Signature signature, PublicKey publicKey)
        throws IllegalArgumentException, SignatureException, IOException {
    PublicKeyAlgorithm alg = signature.getPublicKeyAlgorithm();

    if (alg == PublicKeyAlgorithm.ecdsa_nistp256_with_sha256) {
        try {
            EcdsaSignature ecdsaSignature = signature.getSignatureValue();

            // Create Signature Data
            ASN1Integer asn1R = new ASN1Integer(ecdsaSignature.getR().getX());
            ASN1Integer asn1S = new ASN1Integer(SerializationHelper.readFixedFieldSizeKey(alg,
                    new ByteArrayInputStream(ecdsaSignature.getSignatureValue())));
            DLSequence dLSequence = new DLSequence(new ASN1Encodable[] { asn1R, asn1S });
            byte[] dERSignature = dLSequence.getEncoded();

            byte[] messageDigest = digest(message, alg);

            java.security.Signature sig = java.security.Signature.getInstance("NONEwithECDSA", provider);
            sig.initVerify(publicKey);
            sig.update(messageDigest);
            return sig.verify(dERSignature);
        } catch (Exception e) {
            if (e instanceof IllegalArgumentException) {
                throw (IllegalArgumentException) e;
            }
            if (e instanceof IOException) {
                throw (IOException) e;
            }
            if (e instanceof SignatureException) {
                throw (SignatureException) e;
            }

            throw new SignatureException("Internal error verifying signature " + e.getClass().getSimpleName()
                    + ": " + e.getMessage(), e);
        }

    } else {
        throw new IllegalArgumentException(
                "Unsupported signature algoritm: " + signature.getPublicKeyAlgorithm());
    }
}