Example usage for org.bouncycastle.asn1 ASN1EncodableVector size

List of usage examples for org.bouncycastle.asn1 ASN1EncodableVector size

Introduction

In this page you can find the example usage for org.bouncycastle.asn1 ASN1EncodableVector size.

Prototype

public int size() 

Source Link

Document

Return the size of the vector.

Usage

From source file:org.votingsystem.signature.util.CMSUtils.java

License:Open Source License

public static DERObject getSingleValuedSignedAttribute(AttributeTable signedAttrTable,
        DERObjectIdentifier attrOID, String printableName) throws CMSException {
    if (signedAttrTable == null)
        return null;
    ASN1EncodableVector vector = signedAttrTable.getAll(attrOID);
    switch (vector.size()) {
    case 0://  ww  w  .j a v  a  2s.c o  m
        return null;
    case 1:
        Attribute t = (Attribute) vector.get(0);
        ASN1Set attrValues = t.getAttrValues();
        if (attrValues.size() != 1)
            throw new CMSException("A " + printableName + " attribute MUST have a single attribute value");
        return attrValues.getObjectAt(0).getDERObject();
    default:
        throw new CMSException(
                "The SignedAttributes in a signerInfo MUST NOT include multiple instances of the "
                        + printableName + " attribute");
    }
}

From source file:org.xipki.ca.server.impl.IdentifiedX509Certprofile.java

License:Open Source License

private static ASN1Sequence createSubjectInfoAccess(final Extensions requestExtensions,
        final Map<ASN1ObjectIdentifier, Set<GeneralNameMode>> modes) throws BadCertTemplateException {
    ASN1Encodable extValue = requestExtensions.getExtensionParsedValue(Extension.subjectInfoAccess);
    if (extValue == null) {
        return null;
    }// ww w  .  j  a va  2s  .c  o  m

    ASN1Sequence reqSeq = ASN1Sequence.getInstance(extValue);
    int size = reqSeq.size();

    if (modes == null) {
        return reqSeq;
    }

    ASN1EncodableVector v = new ASN1EncodableVector();
    for (int i = 0; i < size; i++) {
        AccessDescription ad = AccessDescription.getInstance(reqSeq.getObjectAt(i));
        ASN1ObjectIdentifier accessMethod = ad.getAccessMethod();
        if (accessMethod == null) {
            accessMethod = X509Certprofile.OID_ZERO;
        }
        Set<GeneralNameMode> generalNameModes = modes.get(accessMethod);

        if (generalNameModes == null) {
            throw new BadCertTemplateException(
                    "subjectInfoAccess.accessMethod " + accessMethod.getId() + " is not allowed");
        }

        GeneralName accessLocation = createGeneralName(ad.getAccessLocation(), generalNameModes);
        v.add(new AccessDescription(accessMethod, accessLocation));
    } // end for

    return v.size() > 0 ? new DERSequence(v) : null;
}

From source file:org.xipki.pki.ca.server.impl.IdentifiedX509Certprofile.java

License:Open Source License

private static ASN1Sequence createSubjectInfoAccess(final Extensions requestedExtensions,
        final Map<ASN1ObjectIdentifier, Set<GeneralNameMode>> modes) throws BadCertTemplateException {
    if (modes == null) {
        return null;
    }//from w  ww . j av  a  2 s .c  o m

    ASN1Encodable extValue = requestedExtensions.getExtensionParsedValue(Extension.subjectInfoAccess);
    if (extValue == null) {
        return null;
    }

    ASN1Sequence reqSeq = ASN1Sequence.getInstance(extValue);
    int size = reqSeq.size();

    ASN1EncodableVector vec = new ASN1EncodableVector();
    for (int i = 0; i < size; i++) {
        AccessDescription ad = AccessDescription.getInstance(reqSeq.getObjectAt(i));
        ASN1ObjectIdentifier accessMethod = ad.getAccessMethod();
        Set<GeneralNameMode> generalNameModes = modes.get(accessMethod);

        if (generalNameModes == null) {
            throw new BadCertTemplateException(
                    "subjectInfoAccess.accessMethod " + accessMethod.getId() + " is not allowed");
        }

        GeneralName accessLocation = X509CertprofileUtil.createGeneralName(ad.getAccessLocation(),
                generalNameModes);
        vec.add(new AccessDescription(accessMethod, accessLocation));
    } // end for

    return vec.size() > 0 ? new DERSequence(vec) : null;
}

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

License:Open Source License

private static void checkTimestamps(CMSSignedDataParser sp, CMSSigVerifyResult sigResult) throws CMSException {
    List<TimeStampResult> timeStampResultList = sigResult.getTimStampResultList();
    sigResult.setTimStampResultList(timeStampResultList);
    SignerInformationStore signers = sp.getSignerInfos();
    Collection c = signers.getSigners();
    Iterator it = c.iterator();/*  w  ww  .jav  a 2  s .co m*/
    if (!it.hasNext()) {
        return;
    }
    SignerInformation signer = (SignerInformation) it.next();

    //Collect and check time stamps
    AttributeTable unsignedAttributes = signer.getUnsignedAttributes();
    if (unsignedAttributes == null) {
        return;
    }
    ASN1EncodableVector timeStamps = unsignedAttributes
            .getAll(new ASN1ObjectIdentifier("1.2.840.113549.1.9.16.2.14"));
    if (timeStamps.size() == 0) {
        return;
    }
    for (int i = 0; i < timeStamps.size(); i++) {
        try {
            Attribute timestampAttr = Attribute.getInstance(timeStamps.get(i));
            byte[] timeStampBytes = timestampAttr.getAttrValues().getObjectAt(0).toASN1Primitive().getEncoded();
            TimeStampResult tsResult = new TimeStampResult();
            tsResult.setTimestamp(timeStampBytes);
            timeStampResultList.add(tsResult);

            InputStream tsis = new ByteArrayInputStream(timeStampBytes);
            CMSSignedDataParser tsSp = new CMSSignedDataParser(new BcDigestCalculatorProvider(), tsis);

            byte[] tsInfoBytes = IOUtils.toByteArray(tsSp.getSignedContent().getContentStream());
            TimeStampData timeStampData = PdfBoxSigUtil.getTimeStampData(tsInfoBytes);
            tsResult.setTsData(timeStampData);

            //Compare TimeStamp data hash with signature hash
            byte[] sigHash = getDigest(timeStampData.getImprintHashAlgo(), signer.getSignature());
            tsResult.setTimestampMatch(Arrays.equals(sigHash, timeStampData.getImprintDigest()));

            CMSSigVerifyResult tsSigResult = new CMSSigVerifyResult();
            tsSigResult.setSignedData(timeStampBytes);
            tsResult.setSignatureVerification(tsSigResult);

            verifyCMSSignature(tsSp, tsSigResult);
        } catch (Exception e) {
        }

    }
    sigResult.setTimStampResultList(timeStampResultList);
}