Example usage for org.bouncycastle.asn1 DERObjectIdentifier DERObjectIdentifier

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

Introduction

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

Prototype

DERObjectIdentifier(byte[] bytes) 

Source Link

Usage

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

License:Open Source License

public byte[] buildCmsBody(String signedHashId, X509Certificate certContent, byte[] content, String hashId,
        int version) throws CertificateEncodingException, IOException {
    final ASN1EncodableVector whole = new ASN1EncodableVector();
    whole.add(new DERObjectIdentifier(CMS_SIGNED_ID));

    final ASN1EncodableVector body = new ASN1EncodableVector();
    // ----- versao -------
    // final int version = 1;
    body.add(new DERInteger(version));
    buildDigestAlg(body, hashId);/*w ww .j av a 2s  .co m*/
    // buildContentInfo(body, content);
    buildCerts(body, certContent);

    buildSignerInfo(body, signedHashId, certContent, hashId);

    whole.add(new DERTaggedObject(0, new DERSequence(body)));

    return genOutput(new DERSequence(whole));

}

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

License:Open Source License

public byte[] buildCmsBody(byte[] signedHashId, X509Certificate certContent, List<X509Certificate> chain,
        int hashId, int version, int attachSize) throws Exception {
    final ASN1EncodableVector whole = new ASN1EncodableVector(); // 0 SEQ
    whole.add(new DERObjectIdentifier(CMS_SIGNED_ID)); // 1 SEQ

    final ASN1EncodableVector body = new ASN1EncodableVector();
    // ----- versao -------
    // final int version = 1;
    body.add(new DERInteger(version)); // 3 INT
    buildDigestAlg(body, getHashAlg(hashId)); // 3 SET
    buildContentInfo(body, attachSize); // 3 SEQ
    buildCerts(body, chain); // 3 CS

    buildSignerInfo(body, signedHashId, certContent, hashId); // 3 SET

    whole.add(new DERTaggedObject(0, new DERSequence( // 2 SEQ
            body))); // 1 CS

    return genOutput(new DERSequence(whole));

}

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

License:Open Source License

public byte[] buildADRBBody(List<AppSignedInfoEx> listAsiEx, SignPolicy signPol, List<X509Certificate> chain,
        int version, boolean signingCertFallback, int attachSize) throws Exception {
    // AppSignedInfoEx asiEx = listAsiEx.get(0);
    final ASN1EncodableVector whole = new ASN1EncodableVector(); // 0 SEQ
    whole.add(new DERObjectIdentifier(CMS_SIGNED_ID)); // 1 SEQ

    final ASN1EncodableVector body = new ASN1EncodableVector();
    // ----- versao -------
    // final int version = 1;
    body.add(new DERInteger(version)); // 3 INT

    List<String> listHashId = createHashList(listAsiEx);
    buildDigestAlg(body, listHashId); // 3 SET

    buildContentInfo(body, attachSize); // 3 SEQ
    if (chain != null) {
        buildCerts(body, chain); // 3 CS
    } else {/*from   w w  w .java  2 s. com*/
        buildCertsASIE(body, listAsiEx); // 3 CS
    }

    // buildADRBSignerInfo(body, asiEx.getSignedHash(), asiEx.getX509(),
    // asiEx.getOrigHash(), signPol.getPolicyHash(),
    // asiEx.getCertHash(), asiEx.getSigningTime(),
    // asiEx.getIdSha(), signPol.getPolicyUri(),
    // signPol.getPolicyId(),
    // signingCertFallback); // 3 SET

    buildADRBSignerInfo(body, listAsiEx, signPol, signingCertFallback); // 3
    // SET

    whole.add(new DERTaggedObject(0, new DERSequence( // 2 SEQ
            body))); // 1 CS

    return genOutput(new DERSequence(whole));

}

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  w w.  ja va 2 s. com
}

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);/*from   ww w.  ja  v a 2s.  c  om*/
    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
    // }/* ww  w.  j  ava  2  s  . co  m*/
    //
    // 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;
}

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

License:Open Source License

private DERSequence siAddDigestEncryptionAlgorithm(String hashId) {

    // Nota 3: Em ateno  RFC 3370 (Cryptographic Message Syntax (CMS)
    // Algorithms), item
    // "2.1 SHA-1"; e RFC 5754 (Using SHA2 Algorithms with Cryptographic
    // Message Syntax),
    // item "2 - Message Digest Algorithms", recomenda-se a ausncia do
    // campo "parameters" na
    // estrutura "AlgorithmIdentifier", usada na indicao do algoritmo de
    // hash, presentes nas
    // estruturas ASN.1 "SignedData.digestAlgorithms",
    // "SignerInfo.digestAlgorithm" e
    // "SignaturePolicyId.sigPolicyHash.hashAlgorithm".
    // AlgorithmIdentifier ::= SEQUENCE {
    // algorithm OBJECT IDENTIFIER,
    // parameters ANY DEFINED BY algorithm OPTIONAL }

    // Os processos para criao e verificao de assinaturas segundo esta
    // PA devem utilizar o
    // algoritmo :
    // a) para a verso 1.0: sha1withRSAEncryption(1 2 840 113549 1 1 5),
    // b) para a verso 1.1: sha1withRSAEncryption(1 2 840 113549 1 1 5) ou
    // sha256WithRSAEncryption(1.2.840.113549.1.1.11)
    // c) para as verses 2.0 e 2.1:
    // sha256WithRSAEncryption(1.2.840.113549.1.1.11).

    ASN1EncodableVector digestEncVetor = new ASN1EncodableVector();
    digestEncVetor.add(new DERObjectIdentifier(hashId));
    // VER NOTA/*from w w w. jav a 2 s .c om*/
    // digestEncVetor.add(new DERNull());
    return new DERSequence(digestEncVetor);
}

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

License:Open Source License

private DERSequence siAddDigestAlgorithm(String hashId) {
    // Add the digestEncAlgorithm
    ASN1EncodableVector digestVetor = new ASN1EncodableVector();
    digestVetor.add(new DERObjectIdentifier(hashId)); // 6 OID
    digestVetor.add(new DERNull()); // 6 NULL
    return new DERSequence(digestVetor); // 5 SEQ
}

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

License:Open Source License

private void buildContentInfo(final ASN1EncodableVector body, int size) {

    // ------ Content Info
    ASN1EncodableVector contentInfoVector = new ASN1EncodableVector();
    contentInfoVector.add(new DERObjectIdentifier(ID_PKCS7_DATA)); // 4 OID
    if (size != DETACHED) {
        byte[] content = new byte[size];
        for (int i = 0; i < size; i++) {
            content[i] = (byte) 0xba;
        }/*from   w  w  w .  ja  v  a  2  s. com*/
        contentInfoVector.add(new DERTaggedObject(0, new DEROctetString(content)));
    }
    // CONTENT INFO

    final DERSequence contentinfo = new DERSequence(contentInfoVector); // 3
    // SEQ
    body.add(contentinfo);

}

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

License:Open Source License

private void buildDigestAlg(final ASN1EncodableVector body, String hashId) {
    // ---------- algoritmos de digest
    final ASN1EncodableVector algos = new ASN1EncodableVector();
    algos.add(new DERObjectIdentifier(hashId)); // 4 OID
    algos.add(new DERNull()); // 4 NULL
    final ASN1EncodableVector algoSet = new ASN1EncodableVector();
    algoSet.add(new DERSequence(algos));
    final DERSet digestAlgorithms = new DERSet(algoSet); // 2
    // SET/*from  ww w . j a v  a2s.co m*/
    body.add(digestAlgorithms);
}