Example usage for org.bouncycastle.asn1 DEROctetString DEROctetString

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

Introduction

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

Prototype

public DEROctetString(ASN1Encodable obj) throws IOException 

Source Link

Document

Constructor from the encoding of an ASN.1 object.

Usage

From source file:se.tillvaxtverket.ttsigvalws.ttwssigvalidation.pdf.PdfBoxSigUtil.java

License:Open Source License

public static byte[] getRSAPkcs1DigestInfo(DigestAlgorithm digestAlgo, byte[] hashValue) throws IOException {
    ASN1EncodableVector digestInfoSeq = new ASN1EncodableVector();
    AlgorithmIdentifier algoId = digestAlgo.getAlgorithmIdentifier();
    digestInfoSeq.add(algoId);// ww w .  jav a  2  s  .c  o m
    digestInfoSeq.add(new DEROctetString(hashValue));

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    DEROutputStream dout = new DEROutputStream(bout);
    dout.writeObject((new DERSequence(digestInfoSeq)));
    byte[] digestInfoBytes = bout.toByteArray();
    dout.close();
    bout.close();

    return digestInfoBytes;
}

From source file:se.tillvaxtverket.ttsigvalws.ttwssigvalidation.pdf.PdfBoxSigUtil.java

License:Open Source License

public static ASN1EncodableVector getSignedCertAttr(DigestAlgorithm digestAlgo, X509Certificate certificate,
        boolean includeIssuerSerial)
        throws NoSuchAlgorithmException, CertificateEncodingException, IOException {
    final X500Name issuerX500Name = new X509CertificateHolder(certificate.getEncoded()).getIssuer();
    final GeneralName generalName = new GeneralName(issuerX500Name);
    final GeneralNames generalNames = new GeneralNames(generalName);
    final BigInteger serialNumber = certificate.getSerialNumber();
    final IssuerSerial issuerSerial = new IssuerSerial(generalNames, serialNumber);

    ASN1EncodableVector signedCert = new ASN1EncodableVector();

    boolean essSigCertV2;
    ASN1ObjectIdentifier signedCertOid;//from  ww w . jav  a  2  s .  c om
    switch (digestAlgo) {
    case SHA1:
        signedCertOid = new ASN1ObjectIdentifier(PdfObjectIds.ID_AA_SIGNING_CERTIFICATE_V1);
        essSigCertV2 = false;
        break;
    default:
        signedCertOid = new ASN1ObjectIdentifier(PdfObjectIds.ID_AA_SIGNING_CERTIFICATE_V2);
        essSigCertV2 = true;
    }

    MessageDigest md = MessageDigest.getInstance(digestAlgo.getName());
    md.update(certificate.getEncoded());
    byte[] certHash = md.digest();
    DEROctetString certHashOctetStr = new DEROctetString(certHash);

    signedCert.add(signedCertOid);

    ASN1EncodableVector attrValSet = new ASN1EncodableVector();
    ASN1EncodableVector signingCertObjSeq = new ASN1EncodableVector();
    ASN1EncodableVector essCertV2Seq = new ASN1EncodableVector();
    ASN1EncodableVector certSeq = new ASN1EncodableVector();
    ASN1EncodableVector algoSeq = new ASN1EncodableVector();
    algoSeq.add(new ASN1ObjectIdentifier(digestAlgo.getOid()));
    algoSeq.add(DERNull.INSTANCE);
    if (essSigCertV2) {
        certSeq.add(new DERSequence(algoSeq));
    }
    //Add cert hash
    certSeq.add(new DEROctetString(certHash));
    if (includeIssuerSerial) {
        certSeq.add(issuerSerial);
    }

    //Finalize assembly
    essCertV2Seq.add(new DERSequence(certSeq));
    signingCertObjSeq.add(new DERSequence(essCertV2Seq));
    attrValSet.add(new DERSequence(signingCertObjSeq));
    signedCert.add(new DERSet(attrValSet));

    return signedCert;
}

From source file:support.revocation.OCSP.java

License:Apache License

/**
 * @return an OCSP request for the given certificate that was issued by
 * the issuer which the given issuer certificate is issued for
 * @param certificate//ww  w  .  j a  v a2  s.co m
 * @param issuerCertificate
 * @throws IOException
 * @throws GeneralSecurityException
 */
private static OCSPRequest generateOCSPRequest(X509Certificate certificate, X509Certificate issuerCertificate)
        throws IOException, GeneralSecurityException {
    MessageDigest digest = MessageDigest.getInstance("SHA1");
    AlgorithmIdentifier digestAlgorithm = new AlgorithmIdentifier(
            new ASN1ObjectIdentifier(OIWObjectIdentifiers.idSHA1.getId()));

    if (!issuerCertificate.getSubjectX500Principal().equals(certificate.getIssuerX500Principal()))
        throw new CertificateException("Issuing cerrtificate and issued certificate mismatch");

    // issuer hash
    digest.update(issuerCertificate.getSubjectX500Principal().getEncoded());
    ASN1OctetString issuerNameHash = new DEROctetString(digest.digest());

    // issuer public key hash
    SubjectPublicKeyInfo publicKey = SubjectPublicKeyInfo
            .getInstance(parseASN1(issuerCertificate.getPublicKey().getEncoded()));
    digest.update(publicKey.getPublicKeyData().getBytes());
    ASN1OctetString issuerKeyHash = new DEROctetString(digest.digest());

    // certificate serial number
    ASN1Integer serialNumber = new ASN1Integer(certificate.getSerialNumber());

    // OCSP request
    CertID certID = new CertID(digestAlgorithm, issuerNameHash, issuerKeyHash, serialNumber);
    ASN1Sequence requestList = new DERSequence(new Request(certID, null));
    TBSRequest request = new TBSRequest(null, requestList, (Extensions) null);

    return new OCSPRequest(request, null);
}

From source file:tests.asn1.CertTokenTest.java

License:Apache License

/**
 * Produces input stream containing ASN.1 representation of signature info.
 *///from   ww w  . ja  v a 2  s.  c o m
private InputStream getDerStream(Integer version, byte[] history, byte[] publishedData, byte[][] pubReferences)
        throws IOException {
    ASN1EncodableVector v = new ASN1EncodableVector();

    if (version != null) {
        v.add(new ASN1Integer(version.intValue()));
    }

    if (history != null) {
        v.add(new DEROctetString(history));
    }

    if (publishedData != null) {
        v.add(new ASN1InputStream(publishedData).readObject());
    }

    if (pubReferences != null) {
        DEROctetString[] derRefs = new DEROctetString[pubReferences.length];
        for (int i = 0; i < pubReferences.length; i++) {
            derRefs[i] = new DEROctetString(pubReferences[i]);
        }
        v.add(new DERSet(derRefs));
    }

    // Extensions skipped -- see CertToken code for comments

    byte[] der = new DERSequence(v).getEncoded(ASN1Encoding.DER);

    return new ByteArrayInputStream(der);
}

From source file:tests.asn1.MessageImprintTest.java

License:Apache License

/**
 * Produces input stream containing ASN.1 representation of message imprint.
 *//*from w w  w .j a va  2 s .c o m*/
private InputStream getDerStream(String hashAlgorithm, byte[] hashedMessage) throws IOException {
    ASN1EncodableVector v = new ASN1EncodableVector();

    if (hashAlgorithm != null) {
        v.add(new AlgorithmIdentifier(new ASN1ObjectIdentifier(hashAlgorithm)));
    }

    if (hashedMessage != null) {
        v.add(new DEROctetString(hashedMessage));
    }

    byte[] der = new DERSequence(v).getEncoded(ASN1Encoding.DER);

    return new ByteArrayInputStream(der);
}

From source file:tests.asn1.PublishedDataTest.java

License:Apache License

/**
 * Produces input stream containing ASN.1 representation of published data.
 *//*from ww  w.j  av a2s.c  o  m*/
private InputStream getDerStream(BigInteger publicationId, byte[] publicationImprint) throws IOException {
    ASN1EncodableVector v = new ASN1EncodableVector();

    if (publicationId != null) {
        v.add(new ASN1Integer(publicationId));
    }

    if (publicationImprint != null) {
        v.add(new DEROctetString(publicationImprint));
    }

    byte[] der = new DERSequence(v).getEncoded(ASN1Encoding.DER);

    return new ByteArrayInputStream(der);
}

From source file:tests.asn1.SignatureInfoTest.java

License:Apache License

/**
 * Produces input stream containing ASN.1 representation of signature info.
 * @throws IOException /*from   www.ja  v  a  2s.  c  o  m*/
 */
private InputStream getDerStream(String signatureAlgorithm, byte[] signatureValue,
        DERTaggedObject pkiReferences) throws IOException {
    ASN1EncodableVector v = new ASN1EncodableVector();

    if (signatureAlgorithm != null) {
        v.add(new AlgorithmIdentifier(new ASN1ObjectIdentifier(signatureAlgorithm)));
    }

    if (signatureValue != null) {
        v.add(new DEROctetString(signatureValue));
    }

    if (pkiReferences != null) {
        v.add(pkiReferences);
    }

    byte[] der = new DERSequence(v).getEncoded(ASN1Encoding.DER);

    return new ByteArrayInputStream(der);
}

From source file:tests.asn1.SignatureInfoTest.java

License:Apache License

/**
 * Produces ASN.1 tagged object./*w  w w  .  j a  v a 2s  .c  om*/
 */
private DERTaggedObject getDerTagged(boolean isExplicit, int tagNumber, byte[][] refs) throws IOException {
    if (refs == null) {
        return null;
    }

    DEROctetString[] derRefs = new DEROctetString[refs.length];
    for (int i = 0; i < refs.length; i++) {
        derRefs[i] = new DEROctetString(refs[i]);
    }

    // Note: octet-strings will get sorted in this DER-set
    DERSet derSet = new DERSet(derRefs);
    return new DERTaggedObject(isExplicit, tagNumber, derSet);
}

From source file:tests.asn1.SignedDataTest.java

License:Apache License

/**
 * Produces ASN.1 representation of encapsulated content info.
 *///  w  w w  .  j a  v a 2s. c o m
private DERSequence getEContent(String eContentType, boolean isExplicit, int tagNumber, byte[] eContent)
        throws IOException {
    ASN1EncodableVector v = new ASN1EncodableVector();

    if (eContentType != null) {
        v.add(new ASN1ObjectIdentifier(eContentType));
    }

    if (eContent != null) {
        ASN1Object derEContent = new ASN1InputStream(eContent).readObject();
        v.add(new DERTaggedObject(isExplicit, tagNumber, new DEROctetString(derEContent)));
    }

    return new DERSequence(v);
}

From source file:tests.asn1.SignerInfoTest.java

License:Apache License

/**
 * Produces input stream containing ASN.1 representation of signer info.
 *//*from   w w w  .  j ava  2  s .c om*/
private InputStream getDerStream(Integer version, byte[] signerId, String digestAlgorithm,
        DERTaggedObject signedAttrs, String signatureAlgorithm, byte[] signature, DERTaggedObject unsignedAttrs)
        throws IOException {
    ASN1EncodableVector v = new ASN1EncodableVector();

    if (version != null) {
        v.add(new ASN1Integer(version.intValue()));
    }

    if (signerId != null) {
        v.add(new ASN1InputStream(signerId).readObject());
    }

    if (digestAlgorithm != null) {
        v.add(new AlgorithmIdentifier(new ASN1ObjectIdentifier(digestAlgorithm)));
    }

    if (signedAttrs != null) {
        v.add(signedAttrs);
    }

    if (signatureAlgorithm != null) {
        v.add(new AlgorithmIdentifier(new ASN1ObjectIdentifier(signatureAlgorithm)));
    }

    if (signature != null) {
        v.add(new DEROctetString(signature));
    }

    if (unsignedAttrs != null) {
        v.add(unsignedAttrs);
    }

    byte[] der = new DERSequence(v).getEncoded(ASN1Encoding.DER);

    return new ByteArrayInputStream(der);
}