Example usage for org.bouncycastle.asn1.cms SignerInfo getVersion

List of usage examples for org.bouncycastle.asn1.cms SignerInfo getVersion

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.cms SignerInfo getVersion.

Prototype

public ASN1Integer getVersion() 

Source Link

Usage

From source file:com.guardtime.asn1.Asn1Util.java

License:Apache License

/**
 * Extends the given content info with data from the given certification
 * token.// w ww.ja  v a 2  s.  c  o m
 *
 * @param contentInfo
 *            the original timestamp encoded in a CMS {@code ContentInfo}
 *            structure.
 * @param certToken
 *            the {@code CertToken} from the GuardTime online verification
 *            service.
 * @return updated (extended) timestamp encoded in a new CMS
 *         {@code ContentInfo} structure.
 */
static org.bouncycastle.asn1.cms.ContentInfo extend(org.bouncycastle.asn1.cms.ContentInfo contentInfo,
        Asn1CertToken certToken) throws Asn1FormatException {
    ASN1EncodableVector v;

    // Extract signed data
    ASN1Encodable asn1SignedData = contentInfo.getContent();
    org.bouncycastle.asn1.cms.SignedData content = org.bouncycastle.asn1.cms.SignedData
            .getInstance(asn1SignedData);

    // Extract signer info
    ASN1Encodable asn1SignerInfo = content.getSignerInfos().getObjectAt(0);
    org.bouncycastle.asn1.cms.SignerInfo signerInfo = org.bouncycastle.asn1.cms.SignerInfo
            .getInstance(asn1SignerInfo);

    // Extract time signature
    ASN1Primitive asn1TimeSignature = null;
    try {
        asn1TimeSignature = ASN1Primitive.fromByteArray(signerInfo.getEncryptedDigest().getOctets());
    } catch (IOException e) {
        throw new Asn1FormatException("time signature has invalid format");
    }
    Asn1TimeSignature timeSignature = Asn1TimeSignature.getInstance(asn1TimeSignature);

    // Extend TimeSignature
    v = new ASN1EncodableVector();
    v.add(timeSignature.getLocation());
    v.add(certToken.getHistory());
    v.add(certToken.getPublishedData());
    // Skip PK signature <- updated
    v.add(new DERTaggedObject(false, 1, certToken.getPubReference()));
    timeSignature = Asn1TimeSignature.getInstance(new DERSequence(v));

    // Extend SignerInfo
    v = new ASN1EncodableVector();
    v.add(signerInfo.getVersion());
    v.add(signerInfo.getSID());
    v.add(signerInfo.getDigestAlgorithm());

    ASN1Set signedAttrs = signerInfo.getAuthenticatedAttributes();
    if (signedAttrs != null) {
        v.add(new DERTaggedObject(false, 0, signedAttrs));
    }

    v.add(signerInfo.getDigestEncryptionAlgorithm());
    try {
        v.add(new DEROctetString(timeSignature)); // <- updated
    } catch (IOException e) {
        throw new Asn1FormatException(e);
    }

    ASN1Set unsignedAttrs = signerInfo.getUnauthenticatedAttributes();
    if (unsignedAttrs != null) {
        v.add(new DERTaggedObject(false, 1, unsignedAttrs));
    }

    signerInfo = org.bouncycastle.asn1.cms.SignerInfo.getInstance(new DERSequence(v));

    // Extend SignedData
    v = new ASN1EncodableVector();
    v.add(content.getVersion());
    v.add(content.getDigestAlgorithms());
    v.add(content.getEncapContentInfo());
    // Skipping certificates <- updated
    // Skipping CRLs <- updated
    v.add(new DERSet(signerInfo)); // <- updated
    content = org.bouncycastle.asn1.cms.SignedData.getInstance(new DERSequence(v));

    // Extend ContentInfo
    v = new ASN1EncodableVector();
    v.add(contentInfo.getContentType());
    v.add(new DERTaggedObject(true, 0, content)); // <- updated
    contentInfo = org.bouncycastle.asn1.cms.ContentInfo.getInstance(new DERSequence(v));

    return contentInfo;
}

From source file:es.gob.afirma.applet.CMSInformation.java

License:Open Source License

/**
 * Obtiene la informaci&oacute;n de diferentes tipos de formatos.
 * @param doj Etiqueta ASN.1 de la que se obtienen los datos.
 * @param envelopeType   Tipo de formato:
 * <li>0: EnvelopedData</li>
 * <li>1: AuthenticatedData</li>
 * <li>2: AuthEnvelopedData</li>
 * <li>3: SignedAndEnvelopedData</li>
 * <li>4: SignedData</li>//from   w  w  w . jav  a 2  s  .c om
 * <li>5: Encrypted</li>
 * @param tipoDetalle   Tipo de datos (literal)
 * @param signBinaryType Tipo de firmado binario (CADES o CMS)
 * @return  Representaci&oacute;n de los datos.
 */
private static String extractData(final ASN1TaggedObject doj, final int envelopeType, final String tipoDetalle,
        final int signBinaryType) {
    String detalle = ""; //$NON-NLS-1$
    detalle = detalle + tipoDetalle + CR;

    ASN1Set rins = null;
    EncryptedContentInfo encryptedContentInfo = null;
    ASN1Set unprotectedAttrs = null;
    ASN1Integer version = null;
    AlgorithmIdentifier aid = null;
    ContentInfo ci = null;
    ASN1Set authAttrs = null;
    ASN1Set ds = null;
    ASN1Set signerInfosSd = null;

    switch (envelopeType) {
    case TYPE_ENVELOPED_DATA:
        final EnvelopedData enveloped = EnvelopedData.getInstance(doj.getObject());
        version = enveloped.getVersion();
        rins = enveloped.getRecipientInfos();
        encryptedContentInfo = enveloped.getEncryptedContentInfo();
        unprotectedAttrs = enveloped.getUnprotectedAttrs();
        break;
    case TYPE_AUTHENTICATED_DATA:
        final AuthenticatedData authenticated = AuthenticatedData.getInstance(doj.getObject());
        version = authenticated.getVersion();
        rins = authenticated.getRecipientInfos();
        aid = authenticated.getMacAlgorithm();
        ci = authenticated.getEncapsulatedContentInfo();
        authAttrs = authenticated.getAuthAttrs();
        unprotectedAttrs = authenticated.getUnauthAttrs();
        break;
    case TYPE_AUTHENTICATED_ENVELOPED_DATA:
        final AuthEnvelopedData authEnveloped = AuthEnvelopedData.getInstance(doj.getObject());
        version = authEnveloped.getVersion();
        rins = authEnveloped.getRecipientInfos();
        encryptedContentInfo = authEnveloped.getAuthEncryptedContentInfo();
        authAttrs = authEnveloped.getAuthAttrs();
        unprotectedAttrs = authEnveloped.getUnauthAttrs();
        break;
    case TYPE_SIGNED_ENVELOPED_DATA:
        final SignedAndEnvelopedData signedEnv = new SignedAndEnvelopedData((ASN1Sequence) doj.getObject());
        version = signedEnv.getVersion();
        rins = signedEnv.getRecipientInfos();
        encryptedContentInfo = signedEnv.getEncryptedContentInfo();
        signerInfosSd = signedEnv.getSignerInfos();
        break;
    case TYPE_SIGNED_DATA:
        final SignedData signed = SignedData.getInstance(doj.getObject());
        version = signed.getVersion();
        ds = signed.getDigestAlgorithms();
        ci = signed.getEncapContentInfo();
        signerInfosSd = signed.getSignerInfos();
        break;
    case TYPE_ENCRYPTED_DATA:
        final ASN1Sequence encrypted = (ASN1Sequence) doj.getObject();
        version = ASN1Integer.getInstance(encrypted.getObjectAt(0));
        encryptedContentInfo = EncryptedContentInfo.getInstance(encrypted.getObjectAt(1));
        if (encrypted.size() == 3) {
            unprotectedAttrs = (ASN1Set) encrypted.getObjectAt(2);
        }
        break;
    default:
        throw new IllegalArgumentException("Tipo de sobre no soportado: " + envelopeType); //$NON-NLS-1$
    }

    //obtenemos la version
    detalle = detalle + AppletMessages.getString("CMSInformation.1") + SP + version + CR; //$NON-NLS-1$

    //recipientInfo
    if (rins != null) {
        if (envelopeType != TYPE_SIGNED_DATA && envelopeType != TYPE_ENCRYPTED_DATA && rins.size() > 0) {
            detalle = detalle + AppletMessages.getString("CMSInformation.13") + CR; //$NON-NLS-1$
        }
        for (int i = 0; i < rins.size(); i++) {
            final KeyTransRecipientInfo kti = KeyTransRecipientInfo
                    .getInstance(RecipientInfo.getInstance(rins.getObjectAt(i)).getInfo());
            detalle = detalle + AppletMessages.getString("CMSInformation.14") + SP + (i + 1) + ":" + CR; //$NON-NLS-1$//$NON-NLS-2$
            final AlgorithmIdentifier diAlg = kti.getKeyEncryptionAlgorithm();

            //issuer y serial
            final IssuerAndSerialNumber iss = (IssuerAndSerialNumber) SignerIdentifier
                    .getInstance(kti.getRecipientIdentifier().getId()).getId();
            detalle = detalle + TB + AppletMessages.getString("CMSInformation.15") + SP //$NON-NLS-1$
                    + iss.getName().toString() + CR;
            detalle = detalle + TB + AppletMessages.getString("CMSInformation.16") + SP + iss.getSerialNumber() //$NON-NLS-1$
                    + CR;

            // el algoritmo de cifrado de los datos
            AOCipherAlgorithm algorithm = null;
            final AOCipherAlgorithm[] algos = AOCipherAlgorithm.values();

            // obtenemos el algoritmo usado para cifrar la pass
            for (final AOCipherAlgorithm algo : algos) {
                if (algo.getOid().equals(diAlg.getAlgorithm().toString())) {
                    algorithm = algo;
                }
            }
            if (algorithm != null) {
                detalle = detalle + TB + AppletMessages.getString("CMSInformation.17") + SP //$NON-NLS-1$
                        + algorithm.getName() + CR;
            } else {
                detalle = detalle + TB + AppletMessages.getString("CMSInformation.18") + SP //$NON-NLS-1$
                        + diAlg.getAlgorithm() + CR;
            }
        }
    }

    if (envelopeType == TYPE_ENVELOPED_DATA || envelopeType == TYPE_ENCRYPTED_DATA) {
        //obtenemos datos de los datos cifrados.
        detalle = detalle + AppletMessages.getString("CMSInformation.19") + CR; //$NON-NLS-1$
        detalle = detalle + getEncryptedContentInfo(encryptedContentInfo);
    } else if (envelopeType == TYPE_AUTHENTICATED_DATA && aid != null && ci != null) {
        // mac algorithm
        detalle = detalle + AppletMessages.getString("CMSInformation.20") + SP + aid.getAlgorithm() + CR; //$NON-NLS-1$

        //digestAlgorithm
        final ASN1Sequence seq = (ASN1Sequence) doj.getObject();
        final ASN1TaggedObject da = (ASN1TaggedObject) seq.getObjectAt(4);
        final AlgorithmIdentifier dai = AlgorithmIdentifier.getInstance(da.getObject());
        detalle = detalle + AppletMessages.getString("CMSInformation.21") + SP + dai.getAlgorithm() + CR; //$NON-NLS-1$

        //obtenemos datos de los datos cifrados.
        detalle = detalle + AppletMessages.getString("CMSInformation.22") + SP + ci.getContentType() + CR; //$NON-NLS-1$

        detalle = getObligatorieAtrib(signBinaryType, detalle, authAttrs);
    } else if (envelopeType == TYPE_AUTHENTICATED_ENVELOPED_DATA) {
        detalle = detalle + AppletMessages.getString("CMSInformation.19") + CR; //$NON-NLS-1$
        detalle = detalle + getEncryptedContentInfo(encryptedContentInfo);

        detalle = getObligatorieAtrib(signBinaryType, detalle, authAttrs);
    } else if (envelopeType == TYPE_SIGNED_ENVELOPED_DATA) {
        //algoritmo de firma
        final ASN1Sequence seq = (ASN1Sequence) doj.getObject();
        final ASN1Set da = (ASN1Set) seq.getObjectAt(2);
        final AlgorithmIdentifier dai = AlgorithmIdentifier.getInstance(da.getObjectAt(0));
        detalle = detalle + AppletMessages.getString("CMSInformation.21") + SP + dai.getAlgorithm() + CR; //$NON-NLS-1$

        //obtenemos datos de los datos cifrados.
        detalle = detalle + AppletMessages.getString("CMSInformation.19") + CR; //$NON-NLS-1$
        detalle = detalle + getEncryptedContentInfo(encryptedContentInfo);
    } else if (envelopeType == TYPE_SIGNED_DATA && ci != null && ds != null) {
        //algoritmo de firma
        final AlgorithmIdentifier dai = AlgorithmIdentifier.getInstance(ds.getObjectAt(0));
        detalle = detalle + AppletMessages.getString("CMSInformation.21") + SP + dai.getAlgorithm() + CR; //$NON-NLS-1$
        detalle = detalle + AppletMessages.getString("CMSInformation.22") + SP + ci.getContentType() + CR; //$NON-NLS-1$
    }

    //obtenemos lo atributos opcionales
    if (envelopeType != TYPE_SIGNED_ENVELOPED_DATA) {
        if (unprotectedAttrs == null) {
            detalle = detalle + AppletMessages.getString("CMSInformation.28") + CR; //$NON-NLS-1$
        } else {
            final String atributos = getUnSignedAttributes(unprotectedAttrs.getObjects());
            detalle = detalle + AppletMessages.getString("CMSInformation.29") + CR; //$NON-NLS-1$
            detalle = detalle + atributos;
        }
    } else if ((envelopeType == TYPE_SIGNED_ENVELOPED_DATA || envelopeType == TYPE_SIGNED_DATA)
            && signerInfosSd != null) {
        //obtenemos el(los) firmate(s)
        if (signerInfosSd.size() > 0) {
            detalle = detalle + AppletMessages.getString("CMSInformation.30") + CR; //$NON-NLS-1$
        }
        for (int i = 0; i < signerInfosSd.size(); i++) {
            final SignerInfo si = SignerInfo.getInstance(signerInfosSd.getObjectAt(i));

            detalle = detalle + AppletMessages.getString("CMSInformation.31") + SP + (i + 1) + ":" + CR; //$NON-NLS-1$//$NON-NLS-2$
            // version
            detalle = detalle + TB + AppletMessages.getString("CMSInformation.1") + SP + si.getVersion() + CR; //$NON-NLS-1$
            //signerIdentifier
            final SignerIdentifier sident = si.getSID();
            final IssuerAndSerialNumber iss = IssuerAndSerialNumber.getInstance(sident.getId());
            detalle = detalle + TB + AppletMessages.getString("CMSInformation.15") + SP //$NON-NLS-1$
                    + iss.getName().toString() + CR;
            detalle = detalle + TB + AppletMessages.getString("CMSInformation.16") + SP + iss.getSerialNumber() //$NON-NLS-1$
                    + CR;

            //digestAlgorithm
            final AlgorithmIdentifier algId = si.getDigestAlgorithm();
            detalle = detalle + TB + AppletMessages.getString("CMSInformation.35") + SP + algId.getAlgorithm() //$NON-NLS-1$
                    + CR;

            //obtenemos lo atributos obligatorios
            final ASN1Set sa = si.getAuthenticatedAttributes();
            String satributes = ""; //$NON-NLS-1$
            if (sa != null) {
                satributes = getsignedAttributes(sa, signBinaryType);
            }
            detalle = detalle + TB + AppletMessages.getString("CMSInformation.36") + CR; //$NON-NLS-1$
            detalle = detalle + satributes;
        }
    }
    return detalle;
}

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//from w w  w. j a v  a 2 s . com
 * @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.validation102853.cades.CAdESSignature.java

License:Open Source License

/**
 * Copied from org.bouncycastle.asn1.cms.SignerInfo#toASN1Object() and adapted to be able to use the custom unauthenticatedAttributes
 *
 * @param signerInfo/*from   w w w . j  a va2  s . c o  m*/
 * @param unauthenticatedAttributes
 * @return
 */
private ASN1Sequence getSignerInfoEncoded(SignerInfo signerInfo, ASN1Encodable unauthenticatedAttributes) {

    ASN1EncodableVector v = new ASN1EncodableVector();

    v.add(signerInfo.getVersion());
    v.add(signerInfo.getSID());
    v.add(signerInfo.getDigestAlgorithm());

    if (signerInfo.getAuthenticatedAttributes() != null) {
        v.add(new DERTaggedObject(false, 0, signerInfo.getAuthenticatedAttributes()));
    }

    v.add(signerInfo.getDigestEncryptionAlgorithm());
    v.add(signerInfo.getEncryptedDigest());

    if (unauthenticatedAttributes != null) {
        v.add(new DERTaggedObject(false, 1, unauthenticatedAttributes));
    }

    return new DERSequence(v);
}

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//  w ww.jav a  2s. c  o  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:eu.europa.esig.dss.cades.validation.CAdESSignature.java

License:Open Source License

/**
 * Copied from org.bouncycastle.asn1.cms.SignerInfo#toASN1Object() and
 * adapted to be able to use the custom unauthenticatedAttributes
 *
 * @param signerInfo//from www .j  a  v a2  s.c o  m
 * @param signerInfo
 * @param unauthenticatedAttributes
 * @return
 */
private ASN1Sequence getSignerInfoEncoded(final SignerInfo signerInfo,
        final ASN1Encodable unauthenticatedAttributes) {

    ASN1EncodableVector v = new ASN1EncodableVector();

    v.add(signerInfo.getVersion());
    v.add(signerInfo.getSID());
    v.add(signerInfo.getDigestAlgorithm());

    final DERTaggedObject signedAttributes = CMSUtils.getDERSignedAttributes(signerInformation);
    if (signedAttributes != null) {
        v.add(signedAttributes);
    }

    v.add(signerInfo.getDigestEncryptionAlgorithm());
    v.add(signerInfo.getEncryptedDigest());

    if (unauthenticatedAttributes != null) {
        v.add(new DERTaggedObject(false, 1, unauthenticatedAttributes));
    }

    return new DERSequence(v);
}