Example usage for org.bouncycastle.cms CMSSignedData CMSSignedData

List of usage examples for org.bouncycastle.cms CMSSignedData CMSSignedData

Introduction

In this page you can find the example usage for org.bouncycastle.cms CMSSignedData CMSSignedData.

Prototype

public CMSSignedData(ContentInfo sigData) throws CMSException 

Source Link

Usage

From source file:eu.europa.ec.markt.dss.validation.xades.XAdESSignature.java

License:Open Source License

private TimestampToken makeTimestampToken(Element el, TimestampToken.TimestampType timestampType)
        throws XPathExpressionException {
    Element timestampTokenNode = XMLUtils.getElement(el, "./xades:EncapsulatedTimeStamp");
    try {/*from w  ww.  j  a v  a 2 s.c  o  m*/
        byte[] tokenbytes = Base64.decodeBase64(timestampTokenNode.getTextContent());
        TimeStampToken tstoken = new TimeStampToken(new CMSSignedData(tokenbytes));
        return new TimestampToken(tstoken, timestampType);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:eu.europa.ec.markt.dss.validation102853.cades.CAdESSignature.java

License:Open Source License

/**
 * @param data byte array representing CMSSignedData
 * @throws org.bouncycastle.cms.CMSException
 *//*www  .j a v a 2  s  .  co  m*/
public CAdESSignature(final byte[] data) throws CMSException {

    this(new CMSSignedData(data), new CertificatePool());
}

From source file:eu.europa.ec.markt.dss.validation102853.cades.CAdESSignature.java

License:Open Source License

/**
 * @param data     byte array representing CMSSignedData
 * @param certPool can be null//from ww w  .  j  av a 2 s  .c o m
 * @throws org.bouncycastle.cms.CMSException
 */
public CAdESSignature(final byte[] data, final CertificatePool certPool) throws CMSException {

    this(new CMSSignedData(data), certPool);
}

From source file:eu.europa.ec.markt.dss.validation102853.cades.CAdESSignature.java

License:Open Source License

private List<TimestampToken> getTimestampList(final ASN1ObjectIdentifier attrType,
        final TimestampType timestampType, final ArchiveTimestampType archiveTimestampType) {

    final List<TimestampToken> list = new ArrayList<TimestampToken>();

    final AttributeTable attributes;
    if (attrType.equals(PKCSObjectIdentifiers.id_aa_ets_contentTimestamp)) {

        attributes = signerInformation.getSignedAttributes();
    } else {// ww w  . j  av  a 2 s.  c om

        attributes = signerInformation.getUnsignedAttributes();
    }
    if (attributes == null) {
        return list;
    }
    final ASN1EncodableVector archiveList = attributes.getAll(attrType);
    for (int i = 0; i < archiveList.size(); i++) {
        final Attribute attribute = (Attribute) archiveList.get(i);

        final ASN1Set attrValues = attribute.getAttrValues();
        for (final ASN1Encodable value : attrValues.toArray()) {
            try {
                TimeStampToken token = new TimeStampToken(
                        new CMSSignedData(value.toASN1Primitive().getEncoded(ASN1Encoding.DER)));
                final TimestampToken timestampToken = new TimestampToken(token, timestampType, certPool);
                timestampToken.setArchiveTimestampType(archiveTimestampType);
                list.add(timestampToken);
            } catch (Exception e) {
                throw new RuntimeException("Parsing error", e);
            }
        }
    }
    return list;
}

From source file:eu.europa.ec.markt.dss.validation102853.cades.CAdESSignature.java

License:Open Source License

/**
 * Remove any archive-timestamp-v2/3 attribute added after the timestampToken
 */// w  ww  .ja  v a 2  s . c  o m
private ASN1Sequence filterUnauthenticatedAttributes(ASN1Set unauthenticatedAttributes,
        TimestampToken timestampToken) {

    ASN1EncodableVector result = new ASN1EncodableVector();
    for (int ii = 0; ii < unauthenticatedAttributes.size(); ii++) {

        final Attribute attribute = Attribute.getInstance(unauthenticatedAttributes.getObjectAt(ii));
        final ASN1ObjectIdentifier attrType = attribute.getAttrType();
        if (OID.id_aa_ets_archiveTimestampV2.equals(attrType)
                || OID.id_aa_ets_archiveTimestampV3.equals(attrType)) {
            try {

                TimeStampToken token = new TimeStampToken(new CMSSignedData(DSSASN1Utils
                        .getDEREncoded(attribute.getAttrValues().getObjectAt(0).toASN1Primitive())));
                if (!token.getTimeStampInfo().getGenTime().before(timestampToken.getGenerationTime())) {
                    continue;
                }
            } catch (Exception e) {
                throw new DSSException(e);
            }
        }
        result.add(unauthenticatedAttributes.getObjectAt(ii));
    }
    return new DERSequence(result);
}

From source file:eu.europa.ec.markt.dss.validation102853.cades.CMSDocumentValidator.java

License:Open Source License

/**
 * The default constructor for {@code CMSDocumentValidator}.
 *
 * @param document document to validate (with the signature(s))
 * @throws DSSException/*www.  j  a  v  a2 s.c o m*/
 */
public CMSDocumentValidator(final DSSDocument document) throws DSSException {

    this();
    this.document = document;
    InputStream inputStream = null;
    try {

        inputStream = document.openStream();
        if (DSSUtils.available(inputStream) > 0) {
            this.cmsSignedData = new CMSSignedData(inputStream);
        }
    } catch (CMSException e) {
        throw new DSSException("Not a valid CAdES file", e);
    } finally {
        DSSUtils.closeQuietly(inputStream);
    }
}

From source file:eu.europa.esig.dss.applet.util.FileTypeDetectorUtils.java

License:Open Source License

/**
 * @param file/* w ww . j a v  a 2 s.co m*/
 * @return
 * @throws FileNotFoundException
 */
private static boolean isCMS(final File file) throws FileNotFoundException {
    FileInputStream inputStream = null;

    try {
        inputStream = new FileInputStream(file);
        new CMSSignedData(inputStream);
        return true;
    } catch (final CMSException e) {
        return false;
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:eu.europa.esig.dss.cades.signature.CAdESService.java

License:Open Source License

/**
 * In case of an enveloping signature if the signed content's content is null then the null is returned.
 *
 * @param dssDocument//from   w  w w .  j  a  v  a  2 s. c o  m
 *            {@code DSSDocument} containing the data to be signed or {@code CMSSignedData}
 * @param parameters
 *            set of driving signing parameters
 * @return the {@code CMSSignedData} if the dssDocument is an CMS signed message. Null otherwise.
 */
private CMSSignedData getCmsSignedData(final DSSDocument dssDocument,
        final CAdESSignatureParameters parameters) {

    CMSSignedData cmsSignedData = null;
    try {
        // check if input dssDocument is already signed
        cmsSignedData = new CMSSignedData(DSSUtils.toByteArray(dssDocument));
        final SignaturePackaging signaturePackaging = parameters.getSignaturePackaging();
        if (signaturePackaging == SignaturePackaging.ENVELOPING) {

            if (cmsSignedData.getSignedContent().getContent() == null) {
                cmsSignedData = null;
            }
        }
    } catch (Exception e) {
        // not a parallel signature
    }
    return cmsSignedData;
}

From source file:eu.europa.esig.dss.cades.signature.CAdESSignatureExtension.java

License:Open Source License

/**
 * @param signatureToExtend//from   w  w  w . j  a  va  2s.  c o m
 *            to be extended
 * @param parameters
 *            of the extension
 * @return a new extended document
 * @throws eu.europa.esig.dss.DSSException
 */
@Override
public CMSSignedDocument extendSignatures(final DSSDocument signatureToExtend,
        final CAdESSignatureParameters parameters) throws DSSException {

    LOG.info("EXTEND SIGNATURES.");
    try {
        final InputStream inputStream = signatureToExtend.openStream();
        final CMSSignedData cmsSignedData = new CMSSignedData(inputStream);
        IOUtils.closeQuietly(inputStream);
        final CMSSignedData extendCMSSignedData = extendCMSSignatures(cmsSignedData, parameters);
        final CMSSignedDocument cmsSignedDocument = new CMSSignedDocument(extendCMSSignedData);
        return cmsSignedDocument;
    } catch (CMSException e) {
        throw new DSSException("Cannot parse CMS data", e);
    }
}

From source file:eu.europa.esig.dss.cades.signature.CAdESSignatureExtension.java

License:Open Source License

public static ASN1Object getTimeStampAttributeValue(final TSPSource tspSource, final byte[] messageToTimestamp,
        final DigestAlgorithm timestampDigestAlgorithm, final Attribute... attributesForTimestampToken) {
    try {//from   ww  w  .j a v a  2s  .  c o m

        if (LOG.isDebugEnabled()) {
            LOG.debug("Message to timestamp is: " + Hex.encodeHexString(messageToTimestamp));
        }
        byte[] timestampDigest = DSSUtils.digest(timestampDigestAlgorithm, messageToTimestamp);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Digested ({}) message to timestamp is {}",
                    new Object[] { timestampDigestAlgorithm, Hex.encodeHexString(timestampDigest) });
        }

        final TimeStampToken timeStampToken = tspSource.getTimeStampResponse(timestampDigestAlgorithm,
                timestampDigest);

        if (timeStampToken == null) {
            throw new NullPointerException();
        }

        if (LOG.isDebugEnabled()) {
            final byte[] messageImprintDigest = timeStampToken.getTimeStampInfo().getMessageImprintDigest();
            LOG.debug("Digested ({}) message in timestamp is {}",
                    new Object[] { timestampDigestAlgorithm, Hex.encodeHexString(messageImprintDigest) });
        }

        CMSSignedData cmsSignedDataTimeStampToken = new CMSSignedData(timeStampToken.getEncoded());

        // TODO (27/08/2014): attributesForTimestampToken cannot be null: to be modified
        if (attributesForTimestampToken != null) {
            // timeStampToken contains one and only one signer
            final SignerInformation signerInformation = cmsSignedDataTimeStampToken.getSignerInfos()
                    .getSigners().iterator().next();
            AttributeTable unsignedAttributes = CMSUtils.getUnsignedAttributes(signerInformation);
            for (final Attribute attributeToAdd : attributesForTimestampToken) {
                final ASN1ObjectIdentifier attrType = attributeToAdd.getAttrType();
                final ASN1Encodable objectAt = attributeToAdd.getAttrValues().getObjectAt(0);
                unsignedAttributes = unsignedAttributes.add(attrType, objectAt);
            }
            final SignerInformation newSignerInformation = SignerInformation
                    .replaceUnsignedAttributes(signerInformation, unsignedAttributes);
            final List<SignerInformation> signerInformationList = new ArrayList<SignerInformation>();
            signerInformationList.add(newSignerInformation);
            final SignerInformationStore newSignerStore = new SignerInformationStore(signerInformationList);
            cmsSignedDataTimeStampToken = CMSSignedData.replaceSigners(cmsSignedDataTimeStampToken,
                    newSignerStore);
        }
        final byte[] newTimeStampTokenBytes = cmsSignedDataTimeStampToken.getEncoded();
        return DSSASN1Utils.toASN1Primitive(newTimeStampTokenBytes);
    } catch (IOException e) {
        throw new DSSException(e);
    } catch (CMSException e) {
        throw new DSSException(e);
    }

}