Example usage for org.bouncycastle.cms SignerInformation toASN1Structure

List of usage examples for org.bouncycastle.cms SignerInformation toASN1Structure

Introduction

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

Prototype

public SignerInfo toASN1Structure() 

Source Link

Document

Return the underlying ASN.1 object defining this SignerInformation object.

Usage

From source file:eu.europa.ec.markt.dss.signature.cades.CadesLevelBaselineLTATimestampExtractor.java

License:Open Source License

/**
 * 3) Fields version, sid, digestAlgorithm, signedAttrs, signatureAlgorithm, and
 * signature within the SignedData.signerInfoss item corresponding to the signature being archive
 * time-stamped, in their order of appearance.
 *
 * @param signerInformation// w w  w .j a v  a 2 s .c o m
 * @return
 */
private byte[] geSignedFields(SignerInformation signerInformation) {
    final SignerInfo signerInfo = signerInformation.toASN1Structure();
    final ASN1Integer version = signerInfo.getVersion();
    final SignerIdentifier sid = signerInfo.getSID();
    final AlgorithmIdentifier digestAlgorithm = signerInfo.getDigestAlgorithm();
    final ASN1TaggedObject signedAttributes = new DERTaggedObject(false, 0,
            new DERSequence(signerInfo.getAuthenticatedAttributes().toArray()));
    final AlgorithmIdentifier digestEncryptionAlgorithm = signerInfo.getDigestEncryptionAlgorithm();
    final ASN1OctetString encryptedDigest = signerInfo.getEncryptedDigest();

    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {
        final byte[] derEncodedVersion = DSSASN1Utils.getDEREncoded(version);
        final byte[] derEncodedSid = DSSASN1Utils.getDEREncoded(sid);
        final byte[] derEncodedDigestAlgo = DSSASN1Utils.getDEREncoded(digestAlgorithm);
        final byte[] derEncodedSignedAttributes = DSSASN1Utils.getDEREncoded(signedAttributes);
        final byte[] derEncodedDigestEncryptionAlgo = DSSASN1Utils.getDEREncoded(digestEncryptionAlgorithm);
        final byte[] derEncodedEncryptedDigest = DSSASN1Utils.getDEREncoded(encryptedDigest);
        if (LOG.isDebugEnabled()) {
            LOG.debug("getSignedFields Version={}", DSSUtils.encodeHexString(derEncodedVersion));
            LOG.debug("getSignedFields Sid={}", DSSUtils.encodeHexString(derEncodedSid));
            LOG.debug("getSignedFields DigestAlgo={}", DSSUtils.encodeHexString(derEncodedDigestAlgo));
            LOG.debug("getSignedFields SignedAttributes={}",
                    DSSUtils.encodeHexString(derEncodedSignedAttributes)); // bad
            LOG.debug("getSignedFields DigestEncryptionAlgo={}",
                    DSSUtils.encodeHexString(derEncodedDigestEncryptionAlgo));
            LOG.debug("getSignedFields EncryptedDigest={}",
                    DSSUtils.encodeHexString(derEncodedEncryptedDigest));
        }
        byteArrayOutputStream.write(derEncodedVersion);
        byteArrayOutputStream.write(derEncodedSid);
        byteArrayOutputStream.write(derEncodedDigestAlgo);
        byteArrayOutputStream.write(derEncodedSignedAttributes);
        byteArrayOutputStream.write(derEncodedDigestEncryptionAlgo);
        byteArrayOutputStream.write(derEncodedEncryptedDigest);
        return byteArrayOutputStream.toByteArray();
    } catch (IOException e) {
        throw new DSSException(e);
    }
}

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

License:Open Source License

@Override
public byte[] getArchiveTimestampData(int index, Document originalDocument) throws IOException {

    ByteArrayOutputStream toTimestamp = new ByteArrayOutputStream();

    ContentInfo contentInfo = cmsSignedData.getContentInfo();
    SignedData signedData = SignedData.getInstance(contentInfo.getContent());

    /* The encapContentInfo should always be present according to the standard, but sometimes it's omitted */
    // 5.4.1/*w ww  .j  a v  a 2 s . c  o m*/
    if (signedData.getEncapContentInfo() == null || signedData.getEncapContentInfo().getContent() == null) {
        /* Detached signatures have either no encapContentInfo in signedData, or it exists but has no eContent */
        if (originalDocument != null) {
            toTimestamp.write(originalDocument.openStream());
        } else {
            throw new RuntimeException("Signature is detached and no original data provided.");
        }
    } else {

        ContentInfo content = signedData.getEncapContentInfo();
        DEROctetString octet = (DEROctetString) content.getContent();

        ContentInfo info2 = new ContentInfo(new ASN1ObjectIdentifier("1.2.840.113549.1.7.1"),
                new BERConstructedOctetString(octet.getOctets()));
        toTimestamp.write(info2.getEncoded());
    }

    if (signedData.getCertificates() != null) {
        DEROutputStream output = new DEROutputStream(toTimestamp);
        output.writeObject(signedData.getCertificates());
        output.close();
    }

    if (signedData.getCRLs() != null) {
        toTimestamp.write(signedData.getCRLs().getEncoded());
    }

    if (signerInformation.getUnsignedAttributes() != null) {
        ASN1EncodableVector original = signerInformation.getUnsignedAttributes().toASN1EncodableVector();
        List<Attribute> timeStampToRemove = getTimeStampToRemove(index);
        ASN1EncodableVector filtered = new ASN1EncodableVector();
        for (int i = 0; i < original.size(); i++) {
            DEREncodable enc = original.get(i);
            if (!timeStampToRemove.contains(enc)) {
                filtered.add(original.get(i));
            }
        }
        SignerInformation filteredInfo = SignerInformation.replaceUnsignedAttributes(signerInformation,
                new AttributeTable(filtered));

        toTimestamp.write(filteredInfo.toASN1Structure().getEncoded());
    }

    return toTimestamp.toByteArray();
}

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

License:Open Source License

/**
 * 3) Fields version, sid, digestAlgorithm, signedAttrs, signatureAlgorithm, and
 * signature within the SignedData.signerInfoss item corresponding to the signature being archive
 * time-stamped, in their order of appearance.
 *
 * @param signerInformation/*from   w  ww .  j a va2  s .co m*/
 * @return
 */
private byte[] getSignedFields(final SignerInformation signerInformation) {

    final SignerInfo signerInfo = signerInformation.toASN1Structure();
    final ASN1Integer version = signerInfo.getVersion();
    final SignerIdentifier sid = signerInfo.getSID();
    final AlgorithmIdentifier digestAlgorithm = signerInfo.getDigestAlgorithm();
    final DERTaggedObject signedAttributes = CMSUtils.getDERSignedAttributes(signerInformation);
    final AlgorithmIdentifier digestEncryptionAlgorithm = signerInfo.getDigestEncryptionAlgorithm();
    final ASN1OctetString encryptedDigest = signerInfo.getEncryptedDigest();

    final byte[] derEncodedVersion = DSSASN1Utils.getDEREncoded(version);
    final byte[] derEncodedSid = DSSASN1Utils.getDEREncoded(sid);
    final byte[] derEncodedDigestAlgorithm = DSSASN1Utils.getDEREncoded(digestAlgorithm);
    final byte[] derEncodedSignedAttributes = DSSASN1Utils.getDEREncoded(signedAttributes);
    final byte[] derEncodedDigestEncryptionAlgorithm = DSSASN1Utils.getDEREncoded(digestEncryptionAlgorithm);
    final byte[] derEncodedEncryptedDigest = DSSASN1Utils.getDEREncoded(encryptedDigest);
    if (LOG.isDebugEnabled()) {

        LOG.debug("getSignedFields Version={}", Base64.decodeBase64(derEncodedVersion));
        LOG.debug("getSignedFields Sid={}", Base64.decodeBase64(derEncodedSid));
        LOG.debug("getSignedFields DigestAlgorithm={}", Base64.decodeBase64(derEncodedDigestAlgorithm));
        LOG.debug("getSignedFields SignedAttributes={}", Hex.encodeHexString(derEncodedSignedAttributes));
        LOG.debug("getSignedFields DigestEncryptionAlgorithm={}",
                Base64.decodeBase64(derEncodedDigestEncryptionAlgorithm));
        LOG.debug("getSignedFields EncryptedDigest={}", Base64.decodeBase64(derEncodedEncryptedDigest));
    }
    final byte[] concatenatedArrays = DSSUtils.concatenate(derEncodedVersion, derEncodedSid,
            derEncodedDigestAlgorithm, derEncodedSignedAttributes, derEncodedDigestEncryptionAlgorithm,
            derEncodedEncryptedDigest);
    return concatenatedArrays;
}

From source file:net.jsign.timestamp.AuthenticodeTimestamper.java

License:Apache License

@Override
protected AttributeTable getUnsignedAttributes(CMSSignedData token) {
    SignerInformation timestampSignerInformation = token.getSignerInfos().getSigners().iterator().next();
    Attribute counterSignature = new Attribute(CMSAttributes.counterSignature,
            new DERSet(timestampSignerInformation.toASN1Structure()));

    return new AttributeTable(counterSignature);
}

From source file:net.jsign.timestamp.Timestamper.java

License:Apache License

/**
 * Return the encrypted digest of the specified signature.
 *///from   ww w  . ja v  a 2s  . c om
private byte[] getEncryptedDigest(CMSSignedData sigData) {
    SignerInformation signerInformation = sigData.getSignerInfos().getSigners().iterator().next();
    return signerInformation.toASN1Structure().getEncryptedDigest().getOctets();
}