Search in sources :

Example 71 with DERSet

use of com.github.zhenwei.core.asn1.DERSet in project LinLong-Java by zhenwei1108.

the class CMSAuthEnvelopedDataGenerator method doGenerate.

private CMSAuthEnvelopedData doGenerate(CMSTypedData content, OutputAEADEncryptor contentEncryptor) throws CMSException {
    ASN1EncodableVector recipientInfos = new ASN1EncodableVector();
    AlgorithmIdentifier encAlgId;
    ASN1OctetString encContent;
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    ASN1Set authenticatedAttrSet = null;
    try {
        OutputStream cOut = contentEncryptor.getOutputStream(bOut);
        content.write(cOut);
        if (authAttrsGenerator != null) {
            AttributeTable attrTable = authAttrsGenerator.getAttributes(Collections.EMPTY_MAP);
            authenticatedAttrSet = new DERSet(attrTable.toASN1EncodableVector());
            contentEncryptor.getAADStream().write(authenticatedAttrSet.getEncoded(ASN1Encoding.DER));
        }
        cOut.close();
    } catch (IOException e) {
        throw new CMSException("unable to process authenticated content: " + e.getMessage(), e);
    }
    byte[] encryptedContent = bOut.toByteArray();
    byte[] mac = contentEncryptor.getMAC();
    encAlgId = contentEncryptor.getAlgorithmIdentifier();
    encContent = new BEROctetString(encryptedContent);
    GenericKey encKey = contentEncryptor.getKey();
    for (Iterator it = recipientInfoGenerators.iterator(); it.hasNext(); ) {
        RecipientInfoGenerator recipient = (RecipientInfoGenerator) it.next();
        recipientInfos.add(recipient.generate(encKey));
    }
    EncryptedContentInfo eci = new EncryptedContentInfo(content.getContentType(), encAlgId, encContent);
    ASN1Set unprotectedAttrSet = null;
    if (unauthAttrsGenerator != null) {
        AttributeTable attrTable = unauthAttrsGenerator.getAttributes(Collections.EMPTY_MAP);
        unprotectedAttrSet = new DLSet(attrTable.toASN1EncodableVector());
    }
    ContentInfo contentInfo = new ContentInfo(CMSObjectIdentifiers.authEnvelopedData, new AuthEnvelopedData(originatorInfo, new DERSet(recipientInfos), eci, authenticatedAttrSet, new DEROctetString(mac), unprotectedAttrSet));
    return new CMSAuthEnvelopedData(contentInfo);
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AuthEnvelopedData(com.github.zhenwei.pkix.util.asn1.cms.AuthEnvelopedData) AttributeTable(com.github.zhenwei.pkix.util.asn1.cms.AttributeTable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DERSet(com.github.zhenwei.core.asn1.DERSet) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) BEROctetString(com.github.zhenwei.core.asn1.BEROctetString) EncryptedContentInfo(com.github.zhenwei.pkix.util.asn1.cms.EncryptedContentInfo) ContentInfo(com.github.zhenwei.pkix.util.asn1.cms.ContentInfo) Iterator(java.util.Iterator) DLSet(com.github.zhenwei.core.asn1.DLSet) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) EncryptedContentInfo(com.github.zhenwei.pkix.util.asn1.cms.EncryptedContentInfo)

Example 72 with DERSet

use of com.github.zhenwei.core.asn1.DERSet in project LinLong-Java by zhenwei1108.

the class PKCS10CertificationRequestBuilder method build.

/**
 * Generate an PKCS#10 request based on the past in signer.
 *
 * @param signer the content signer to be used to generate the signature validating the
 *               certificate.
 * @return a holder containing the resulting PKCS#10 certification request.
 */
public PKCS10CertificationRequest build(ContentSigner signer) {
    CertificationRequestInfo info;
    if (attributes.isEmpty()) {
        if (leaveOffEmpty) {
            info = new CertificationRequestInfo(subject, publicKeyInfo, null);
        } else {
            info = new CertificationRequestInfo(subject, publicKeyInfo, new DERSet());
        }
    } else {
        ASN1EncodableVector v = new ASN1EncodableVector();
        for (Iterator it = attributes.iterator(); it.hasNext(); ) {
            v.add(Attribute.getInstance(it.next()));
        }
        info = new CertificationRequestInfo(subject, publicKeyInfo, new DERSet(v));
    }
    try {
        OutputStream sOut = signer.getOutputStream();
        sOut.write(info.getEncoded(ASN1Encoding.DER));
        sOut.close();
        return new PKCS10CertificationRequest(new CertificationRequest(info, signer.getAlgorithmIdentifier(), new DERBitString(signer.getSignature())));
    } catch (IOException e) {
        throw new IllegalStateException("cannot produce certification request signature");
    }
}
Also used : CertificationRequestInfo(com.github.zhenwei.core.asn1.pkcs.CertificationRequestInfo) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) DERBitString(com.github.zhenwei.core.asn1.DERBitString) IOException(java.io.IOException) DERSet(com.github.zhenwei.core.asn1.DERSet) CertificationRequest(com.github.zhenwei.core.asn1.pkcs.CertificationRequest)

Example 73 with DERSet

use of com.github.zhenwei.core.asn1.DERSet in project LinLong-Java by zhenwei1108.

the class CMSSignedDataParser method replaceSigners.

/**
 * Replace the signerinformation store associated with the passed in message contained in the
 * stream original with the new one passed in. You would probably only want to do this if you
 * wanted to change the unsigned attributes associated with a signer, or perhaps delete one.
 * <p>
 * The output stream is returned unclosed.
 * </p>
 *
 * @param original               the signed data stream to be used as a base.
 * @param signerInformationStore the new signer information store to use.
 * @param out                    the stream to write the new signed data object to.
 * @return out.
 */
public static OutputStream replaceSigners(InputStream original, SignerInformationStore signerInformationStore, OutputStream out) throws CMSException, IOException {
    ASN1StreamParser in = new ASN1StreamParser(original);
    ContentInfoParser contentInfo = new ContentInfoParser((ASN1SequenceParser) in.readObject());
    SignedDataParser signedData = SignedDataParser.getInstance(contentInfo.getContent(BERTags.SEQUENCE));
    BERSequenceGenerator sGen = new BERSequenceGenerator(out);
    sGen.addObject(CMSObjectIdentifiers.signedData);
    BERSequenceGenerator sigGen = new BERSequenceGenerator(sGen.getRawOutputStream(), 0, true);
    // version number
    sigGen.addObject(signedData.getVersion());
    // digests
    // skip old ones
    signedData.getDigestAlgorithms().toASN1Primitive();
    ASN1EncodableVector digestAlgs = new ASN1EncodableVector();
    for (Iterator it = signerInformationStore.getSigners().iterator(); it.hasNext(); ) {
        SignerInformation signer = (SignerInformation) it.next();
        digestAlgs.add(CMSSignedHelper.INSTANCE.fixDigestAlgID(signer.getDigestAlgorithmID(), dgstAlgFinder));
    }
    sigGen.getRawOutputStream().write(new DERSet(digestAlgs).getEncoded());
    // encap content info
    ContentInfoParser encapContentInfo = signedData.getEncapContentInfo();
    BERSequenceGenerator eiGen = new BERSequenceGenerator(sigGen.getRawOutputStream());
    eiGen.addObject(encapContentInfo.getContentType());
    pipeEncapsulatedOctetString(encapContentInfo, eiGen.getRawOutputStream());
    eiGen.close();
    writeSetToGeneratorTagged(sigGen, signedData.getCertificates(), 0);
    writeSetToGeneratorTagged(sigGen, signedData.getCrls(), 1);
    ASN1EncodableVector signerInfos = new ASN1EncodableVector();
    for (Iterator it = signerInformationStore.getSigners().iterator(); it.hasNext(); ) {
        SignerInformation signer = (SignerInformation) it.next();
        signerInfos.add(signer.toASN1Structure());
    }
    sigGen.getRawOutputStream().write(new DERSet(signerInfos).getEncoded());
    sigGen.close();
    sGen.close();
    return out;
}
Also used : ContentInfoParser(com.github.zhenwei.pkix.util.asn1.cms.ContentInfoParser) SignedDataParser(com.github.zhenwei.pkix.util.asn1.cms.SignedDataParser) BERSequenceGenerator(com.github.zhenwei.core.asn1.BERSequenceGenerator) Iterator(java.util.Iterator) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) DERSet(com.github.zhenwei.core.asn1.DERSet) ASN1StreamParser(com.github.zhenwei.core.asn1.ASN1StreamParser)

Example 74 with DERSet

use of com.github.zhenwei.core.asn1.DERSet in project LinLong-Java by zhenwei1108.

the class CMSEnvelopedDataGenerator method doGenerate.

private CMSEnvelopedData doGenerate(CMSTypedData content, OutputEncryptor contentEncryptor) throws CMSException {
    ASN1EncodableVector recipientInfos = new ASN1EncodableVector();
    AlgorithmIdentifier encAlgId;
    ASN1OctetString encContent;
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    try {
        OutputStream cOut = contentEncryptor.getOutputStream(bOut);
        content.write(cOut);
        cOut.close();
        if (contentEncryptor instanceof OutputAEADEncryptor) {
            byte[] mac = ((OutputAEADEncryptor) contentEncryptor).getMAC();
            bOut.write(mac, 0, mac.length);
        }
    } catch (IOException e) {
        throw new CMSException("");
    }
    byte[] encryptedContent = bOut.toByteArray();
    encAlgId = contentEncryptor.getAlgorithmIdentifier();
    encContent = new BEROctetString(encryptedContent);
    GenericKey encKey = contentEncryptor.getKey();
    for (Iterator it = recipientInfoGenerators.iterator(); it.hasNext(); ) {
        RecipientInfoGenerator recipient = (RecipientInfoGenerator) it.next();
        recipientInfos.add(recipient.generate(encKey));
    }
    EncryptedContentInfo eci = new EncryptedContentInfo(content.getContentType(), encAlgId, encContent);
    ASN1Set unprotectedAttrSet = null;
    if (unprotectedAttributeGenerator != null) {
        AttributeTable attrTable = unprotectedAttributeGenerator.getAttributes(Collections.EMPTY_MAP);
        unprotectedAttrSet = new BERSet(attrTable.toASN1EncodableVector());
    }
    ContentInfo contentInfo = new ContentInfo(CMSObjectIdentifiers.envelopedData, new EnvelopedData(originatorInfo, new DERSet(recipientInfos), eci, unprotectedAttrSet));
    return new CMSEnvelopedData(contentInfo);
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) BERSet(com.github.zhenwei.core.asn1.BERSet) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AttributeTable(com.github.zhenwei.pkix.util.asn1.cms.AttributeTable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) OutputAEADEncryptor(com.github.zhenwei.pkix.operator.OutputAEADEncryptor) DERSet(com.github.zhenwei.core.asn1.DERSet) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) BEROctetString(com.github.zhenwei.core.asn1.BEROctetString) ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) EncryptedContentInfo(com.github.zhenwei.pkix.util.asn1.cms.EncryptedContentInfo) ContentInfo(com.github.zhenwei.pkix.util.asn1.cms.ContentInfo) Iterator(java.util.Iterator) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) EnvelopedData(com.github.zhenwei.pkix.util.asn1.cms.EnvelopedData) EncryptedContentInfo(com.github.zhenwei.pkix.util.asn1.cms.EncryptedContentInfo)

Example 75 with DERSet

use of com.github.zhenwei.core.asn1.DERSet in project LinLong-Java by zhenwei1108.

the class CMSEnvelopedDataParser method getUnprotectedAttributes.

/**
 * return a table of the unprotected attributes indexed by the OID of the attribute.
 *
 * @throws IOException
 */
public AttributeTable getUnprotectedAttributes() throws IOException {
    if (unprotectedAttributes == null && attrNotRead) {
        ASN1SetParser set = envelopedData.getUnprotectedAttrs();
        attrNotRead = false;
        if (set != null) {
            ASN1EncodableVector v = new ASN1EncodableVector();
            ASN1Encodable o;
            while ((o = set.readObject()) != null) {
                ASN1SequenceParser seq = (ASN1SequenceParser) o;
                v.add(seq.toASN1Primitive());
            }
            unprotectedAttributes = new AttributeTable(new DERSet(v));
        }
    }
    return unprotectedAttributes;
}
Also used : ASN1SequenceParser(com.github.zhenwei.core.asn1.ASN1SequenceParser) ASN1SetParser(com.github.zhenwei.core.asn1.ASN1SetParser) AttributeTable(com.github.zhenwei.pkix.util.asn1.cms.AttributeTable) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) ASN1Encodable(com.github.zhenwei.core.asn1.ASN1Encodable) DERSet(com.github.zhenwei.core.asn1.DERSet)

Aggregations

DERSet (org.bouncycastle.asn1.DERSet)59 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)34 IOException (java.io.IOException)29 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)27 DERSequence (org.bouncycastle.asn1.DERSequence)27 DEROctetString (org.bouncycastle.asn1.DEROctetString)22 DERSet (com.github.zhenwei.core.asn1.DERSet)21 Attribute (org.bouncycastle.asn1.cms.Attribute)21 X509Certificate (java.security.cert.X509Certificate)19 ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)15 Iterator (java.util.Iterator)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)13 ArrayList (java.util.ArrayList)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)11 DERTaggedObject (org.bouncycastle.asn1.DERTaggedObject)11 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)11 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)9 OutputStream (java.io.OutputStream)9