Example usage for org.bouncycastle.asn1 DERSet DERSet

List of usage examples for org.bouncycastle.asn1 DERSet DERSet

Introduction

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

Prototype

public DERSet(ASN1Encodable[] elements) 

Source Link

Document

create a set containing an array of objects.

Usage

From source file:ClientOCSPDriver.java

License:Open Source License

/**
 Method added to generate ASNSequence object of subjectDN consturcted in appropriate ASN1 type
 X509Name constructs all DN components as printablestring by default
         /*from www.jav  a2s.  co  m*/
 This code was taken and modified from X509Name.java file of BouncyCastle
 **/
public ASN1Sequence getASNSequence(Vector oids, Vector values) {
    ASN1Sequence seq;

    ASN1EncodableVector vec = new ASN1EncodableVector();
    ASN1EncodableVector sVec = new ASN1EncodableVector();
    DERObjectIdentifier lstOid = null;

    for (int i = 0; i != oids.size(); i++) {
        ASN1EncodableVector v = new ASN1EncodableVector();
        DERObjectIdentifier oid = (DERObjectIdentifier) oids.elementAt(i);

        v.add(oid);

        String str = (String) values.elementAt(i);

        v.add(getConvertedValue(oid, str));

        if (lstOid == null) {
            sVec.add(new DERSequence(v));
        } else {
            vec.add(new DERSet(sVec));
            sVec = new ASN1EncodableVector();
            sVec.add(new DERSequence(v));
        }

        lstOid = oid;
    }

    vec.add(new DERSet(sVec));

    seq = new DERSequence(vec);

    return seq;
}

From source file:CreateSignature.java

License:Apache License

/**
 * We are extending CMS Signature//from  ww  w .  j a  va  2  s .  c o m
 *
 * @param signer information about signer
 * @return information about SignerInformation
 */
private SignerInformation signTimeStamp(SignerInformation signer) throws IOException, TSPException {
    AttributeTable unsignedAttributes = signer.getUnsignedAttributes();

    ASN1EncodableVector vector = new ASN1EncodableVector();
    if (unsignedAttributes != null) {
        vector = unsignedAttributes.toASN1EncodableVector();
    }

    byte[] token = getTsaClient().getTimeStampToken(signer.getSignature());
    ASN1ObjectIdentifier oid = PKCSObjectIdentifiers.id_aa_signatureTimeStampToken;
    ASN1Encodable signatureTimeStamp = new Attribute(oid, new DERSet(ASN1Primitive.fromByteArray(token)));

    vector.add(signatureTimeStamp);
    Attributes signedAttributes = new Attributes(vector);

    SignerInformation newSigner = SignerInformation.replaceUnsignedAttributes(signer,
            new AttributeTable(signedAttributes));

    // TODO can this actually happen?
    if (newSigner == null) {
        return signer;
    }

    return newSigner;
}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

private void buildSignerInfo(ASN1EncodableVector body, byte[] signedHashContent, X509Certificate certContent,
        int hashId) throws Exception {
    // ----- Signers Info --------

    final ASN1EncodableVector vec = new ASN1EncodableVector();
    final ASN1EncodableVector signerinfoVector = new ASN1EncodableVector();
    signerinfoVector.add(new DERInteger(SI_VERSION));

    signerinfoVector.add(siAddCert(certContent));
    signerinfoVector.add(siAddDigestAlgorithm(getHashAlg(hashId)));
    signerinfoVector.add(siAddDigestEncryptionAlgorithm(getHashSignAlg(hashId)));
    // Add the digest
    signerinfoVector.add(new DEROctetString(signedHashContent));

    final DERSequence siSeq = new DERSequence(signerinfoVector);
    vec.add(siSeq);/*from ww w. ja va2s  .  c o  m*/
    DERSet siSet = new DERSet(vec);
    body.add(siSet);

}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

private void buildADRBSignerInfo(ASN1EncodableVector body, List<AppSignedInfoEx> listAsiEx, SignPolicy signPol,
        boolean signingCertFallback) throws Exception {
    final ASN1EncodableVector vec = new ASN1EncodableVector();
    // DERSequence siSeq = null;

    // ----- Signers Info --------
    for (AppSignedInfoEx next : listAsiEx) {
        final ASN1EncodableVector signerinfoVector = new ASN1EncodableVector();
        String hashId = getHashAlg(next.getIdSha());
        String hashSignId = getHashSignAlg(next.getIdSha());

        signerinfoVector.add(new DERInteger(SI_VERSION));

        signerinfoVector.add(siAddCert(next.getX509()));
        signerinfoVector.add(siAddDigestAlgorithm(hashId));
        // der encoded structure
        DERTaggedObject derEncStruct = adrbSiCreateDerEncSigned(next.getOrigHash(), signPol.getPolicyHash(),
                next.getCertHash(), next.getX509(), next.getSigningTime(), next.getIdSha(),
                signPol.getPolicyUri(), signPol.getPolicyId(), signingCertFallback);
        signerinfoVector.add(derEncStruct);

        signerinfoVector.add(siAddDigestEncryptionAlgorithm(hashSignId));
        // Add the digest
        signerinfoVector.add(new DEROctetString(next.getSignedHash()));

        final DERSequence siSeq = new DERSequence(signerinfoVector);
        vec.add(siSeq);//from  w ww  .  ja va 2 s.c om
    }
    // ----- Signers Info --------

    DERSet siSet = new DERSet(vec);
    body.add(siSet);

}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

public ASN1Set siCreateDerEncSignedADRB(byte[] origHash, byte[] polHash, byte[] certHash, X509Certificate cert,
        Date now, int hashId, String sigPolicyUri, String sigPolicyId, boolean signingCertFallback)
        throws Exception {

    DERSequence seq00 = siCreateDerEncSeqADRB(origHash, polHash, certHash, cert, now, hashId, sigPolicyUri,
            sigPolicyId, signingCertFallback);

    ASN1Set retSet = new DERSet(seq00);
    return retSet;
}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

private Attribute createContentType() {
    // // final ASN1EncodableVector desSeq = new ASN1EncodableVector();
    // // desSeq.add(new DERObjectIdentifier(ID_CONTENT_TYPE));
    final ASN1EncodableVector setEV = new ASN1EncodableVector();
    setEV.add(new DERObjectIdentifier(ID_PKCS7_DATA));

    DERSet set = new DERSet(setEV);
    // // desSeq.add(set);
    // // DERSequence seq = new DERSequence(desSeq);
    Attribute seq1 = new Attribute(new ASN1ObjectIdentifier(ID_CONTENT_TYPE), set);
    return seq1;/* w ww  .j av a  2  s  .c o  m*/
}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

private Attribute createMessageDigest(byte[] origHash) {
    final ASN1EncodableVector setEV = new ASN1EncodableVector();
    setEV.add(new DEROctetString(origHash));

    DERSet set = new DERSet(setEV);

    Attribute seq1 = new Attribute(new ASN1ObjectIdentifier(ID_MESSAGE_DIGEST), set);
    return seq1;// w  w w  .  j a v  a 2 s .c o m
}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

private Attribute createSigningTime(Date now) {
    final ASN1EncodableVector setEV = new ASN1EncodableVector();
    setEV.add(new DERUTCTime(now));

    DERSet set = new DERSet(setEV);
    Attribute seq1 = new Attribute(new ASN1ObjectIdentifier(ID_SIGNING_TIME), set);
    return seq1;/*from  www.ja v a2s .  c o  m*/
}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

private Attribute createPolicyId(byte[] polHash, String polHashAlg, String sigPolicyUri, String sigPolicyId) {

    final ASN1EncodableVector desSeq12 = new ASN1EncodableVector();
    desSeq12.add(new DERObjectIdentifier(polHashAlg));
    DERSequence seq12 = new DERSequence(desSeq12);

    final ASN1EncodableVector desSeq1 = new ASN1EncodableVector();
    desSeq1.add(seq12);//w  w  w  .  ja v  a  2 s  . co  m
    desSeq1.add(new DEROctetString(polHash));
    DERSequence seq1 = new DERSequence(desSeq1);

    // // end seq 1

    // IGUALAR AO ITAU

    final ASN1EncodableVector desSeq22 = new ASN1EncodableVector();
    desSeq22.add(new DERObjectIdentifier(ID_SIG_POLICY_URI));
    desSeq22.add(new DERIA5String(sigPolicyUri));
    DERSequence seq22 = new DERSequence(desSeq22);

    final ASN1EncodableVector desSeq2 = new ASN1EncodableVector();
    desSeq2.add(seq22);

    DERSequence seq2 = new DERSequence(desSeq2);

    final ASN1EncodableVector aevDSet1 = new ASN1EncodableVector();
    final ASN1EncodableVector aevDSeq1 = new ASN1EncodableVector();
    aevDSeq1.add(new DERObjectIdentifier(sigPolicyId));
    aevDSeq1.add(seq1);

    aevDSeq1.add(seq2);

    DERSequence dsq1 = new DERSequence(aevDSeq1);
    aevDSet1.add(dsq1);
    DERSet ds1 = new DERSet(aevDSet1);

    Attribute ret = new Attribute(new ASN1ObjectIdentifier(ID_SIG_POLICY), ds1);
    return ret;
}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

private Attribute createCertRef(byte[] certHash, X509Certificate certContent, boolean signingCertFallback,
        int hashId) throws Exception {
    // *** BEGIN ***

    // 5.2.1.1.3 Certificados Obrigatoriamente Referenciados
    // O atributo signingCertificate deve conter referncia apenas ao
    // certificado do signatrio.

    // 5.2.1.1.4 Certificados Obrigatrios do Caminho de Certificao
    // Para a verso 1.0: nenhum certificado
    // Para as verses 1.1, 2.0 e 2.1: o certificado do signatrio.

    // ESSCertIDv2 ::= SEQUENCE {
    // hashAlgorithm AlgorithmIdentifier
    // DEFAULT {algorithm id-sha256},
    // certHash Hash,
    // issuerSerial IssuerSerial OPTIONAL
    // }//from w  ww. j  a v a 2 s .c om
    //
    // Hash ::= OCTET STRING
    //
    // IssuerSerial ::= SEQUENCE {
    // issuer GeneralNames,
    // serialNumber CertificateSerialNumber
    // }
    final ASN1EncodableVector issuerSerialaev = new ASN1EncodableVector();

    final ASN1EncodableVector issuerCertaev = new ASN1EncodableVector();

    DERTaggedObject issuerName = new DERTaggedObject(true, 4, // issuer
            // GeneralNames,
            getEncodedIssuer(certContent.getTBSCertificate()));

    // DERTaggedObject issuerName = new DERTaggedObject(false, 0, // issuer
    // GeneralNames,
    // getEncodedIssuer(certContent.getTBSCertificate()));
    issuerCertaev.add(issuerName);

    DERSequence issuerCertseq = new DERSequence(issuerCertaev); // IssuerSerial
    // ::=
    // SEQUENCE
    // {
    issuerSerialaev.add(issuerCertseq);

    // serialNumber CertificateSerialNumber
    BigInteger serialNumber = certContent.getSerialNumber();
    issuerSerialaev.add(new DERInteger(serialNumber));

    DERSequence issuerSerial = new DERSequence(issuerSerialaev);
    // *** END ***

    final ASN1EncodableVector essCertIDv2aev = new ASN1EncodableVector();
    essCertIDv2aev.add(new DEROctetString(certHash)); // Hash ::= OCTET
    // STRING

    essCertIDv2aev.add(issuerSerial); // ESSCertIDv2 ::= SEQUENCE {

    // hashAlgorithm AlgorithmIdentifier

    if (!((signingCertFallback && hashId == NDX_SHA1) || (!signingCertFallback && hashId == NDX_SHA256))) {
        DERObjectIdentifier hashAlgorithm = new DERObjectIdentifier(getHashAlg(hashId));
        essCertIDv2aev.add(hashAlgorithm);
    }
    // Nota 4: Para o atributo ESSCertIDv2, utilizada nas verses 2.1 das
    // polticas de assinatura
    // baseadas em CAdES, as aplicaes NO DEVEM codificar o campo
    // hashAlgorithm caso
    // utilize o mesmo algoritmo definido como valor default (SHA-256),
    // conforme ISO 8825-1.

    DERSequence essCertIDv2seq = new DERSequence(essCertIDv2aev);

    // ************************************************************************
    //
    final ASN1EncodableVector aevSeq3 = new ASN1EncodableVector();
    aevSeq3.add(essCertIDv2seq);
    DERSequence seq3 = new DERSequence(aevSeq3);

    final ASN1EncodableVector aevSeq2 = new ASN1EncodableVector();
    aevSeq2.add(seq3);
    DERSequence seq2 = new DERSequence(aevSeq2);

    final ASN1EncodableVector aevSet = new ASN1EncodableVector();
    aevSet.add(seq2);
    ASN1Set mainSet = new DERSet(aevSet);

    Attribute seq1 = new Attribute(
            new ASN1ObjectIdentifier(signingCertFallback ? ID_SIGNING_CERT : ID_SIGNING_CERT2), mainSet);
    return seq1;
}