Example usage for org.bouncycastle.asn1 ASN1BitString getOctets

List of usage examples for org.bouncycastle.asn1 ASN1BitString getOctets

Introduction

In this page you can find the example usage for org.bouncycastle.asn1 ASN1BitString getOctets.

Prototype

public byte[] getOctets() 

Source Link

Document

Return the octets contained in this BIT STRING, checking that this BIT STRING really does represent an octet aligned string.

Usage

From source file:se.tillvaxtverket.ttsigvalws.ttwssigvalidation.pdf.PdfSignatureVerifier.java

License:Open Source License

/**
 * Retrieves Public key parameters from a public key
 *
 * @param pubKey The public key// w ww.j ava 2 s .  com
 * @param sigResult The data object where result data are stored
 * @throws IOException
 */
public static void getPkParams(PublicKey pubKey, CMSSigVerifyResult sigResult) throws IOException {

    try {
        String pkStr = String.valueOf(Base64Coder.encode(pubKey.getEncoded()));

        ASN1InputStream din = new ASN1InputStream(new ByteArrayInputStream(pubKey.getEncoded()));
        //ASN1Primitive pkObject = din.readObject();
        ASN1Sequence pkSeq = ASN1Sequence.getInstance(din.readObject());
        ASN1BitString keyBits = (ASN1BitString) pkSeq.getObjectAt(1);

        AlgorithmIdentifier algoId = AlgorithmIdentifier.getInstance(pkSeq.getObjectAt(0));
        PublicKeyType pkType = PublicKeyType.getTypeFromOid(algoId.getAlgorithm().getId());
        sigResult.setPkType(pkType);
        sigResult.setPkType(pkType);
        if (pkType.equals(PublicKeyType.EC)) {
            ASN1ObjectIdentifier curveOid = ASN1ObjectIdentifier.getInstance(algoId.getParameters());
            EcCurve curve = EcCurve.getEcCurveFromOid(curveOid.getId());
            sigResult.setEcCurve(curve);
            int totalKeyBits = getEcKeyLength(keyBits);
            sigResult.setKeyLength(totalKeyBits);
            return;
        }

        if (pkType.equals(PublicKeyType.RSA)) {
            ASN1InputStream keyIs = new ASN1InputStream(keyBits.getOctets());
            ASN1Sequence keyParamsSeq = ASN1Sequence.getInstance(keyIs.readObject());
            ASN1Integer modInt = ASN1Integer.getInstance(keyParamsSeq.getObjectAt(0));
            int modLen = getAsn1IntegerBitLength(modInt);
            sigResult.setKeyLength(modLen);
            return;
        }

    } catch (Exception e) {
        int asdf = 0;
    }

}