Search in sources :

Example 36 with ContentInfo

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

the class CMSCompressedData method getContentStream.

public CMSTypedStream getContentStream(InputExpanderProvider expanderProvider) {
    ContentInfo content = comData.getEncapContentInfo();
    ASN1OctetString bytes = (ASN1OctetString) content.getContent();
    InputExpander expander = expanderProvider.get(comData.getCompressionAlgorithmIdentifier());
    InputStream zIn = expander.getInputStream(bytes.getOctetStream());
    return new CMSTypedStream(content.getContentType(), zIn);
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) ContentInfo(com.github.zhenwei.pkix.util.asn1.cms.ContentInfo) InputStream(java.io.InputStream) InputExpander(com.github.zhenwei.pkix.operator.InputExpander)

Example 37 with ContentInfo

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

the class CMSEncryptedDataGenerator method doGenerate.

private CMSEncryptedData doGenerate(CMSTypedData content, OutputEncryptor contentEncryptor) throws CMSException {
    AlgorithmIdentifier encAlgId;
    ASN1OctetString encContent;
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    try {
        OutputStream cOut = contentEncryptor.getOutputStream(bOut);
        content.write(cOut);
        cOut.close();
    } catch (IOException e) {
        throw new CMSException("");
    }
    byte[] encryptedContent = bOut.toByteArray();
    encAlgId = contentEncryptor.getAlgorithmIdentifier();
    encContent = new BEROctetString(encryptedContent);
    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.encryptedData, new EncryptedData(eci, unprotectedAttrSet));
    return new CMSEncryptedData(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) 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) EncryptedData(com.github.zhenwei.pkix.util.asn1.cms.EncryptedData) EncryptedContentInfo(com.github.zhenwei.pkix.util.asn1.cms.EncryptedContentInfo)

Example 38 with ContentInfo

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

the class CMSCompressedDataStreamGenerator method open.

/**
 * Open a compressing output stream.
 *
 * @param contentOID the content type OID.
 * @param out        the stream to encode to.
 * @param compressor the type of compressor to use.
 * @return an output stream to write the data be compressed to.
 * @throws IOException
 */
public OutputStream open(ASN1ObjectIdentifier contentOID, OutputStream out, OutputCompressor compressor) throws IOException {
    BERSequenceGenerator sGen = new BERSequenceGenerator(out);
    sGen.addObject(CMSObjectIdentifiers.compressedData);
    // 
    // Compressed Data
    // 
    BERSequenceGenerator cGen = new BERSequenceGenerator(sGen.getRawOutputStream(), 0, true);
    cGen.addObject(new ASN1Integer(0));
    // 
    // AlgorithmIdentifier
    // 
    cGen.addObject(compressor.getAlgorithmIdentifier());
    // 
    // Encapsulated ContentInfo
    // 
    BERSequenceGenerator eiGen = new BERSequenceGenerator(cGen.getRawOutputStream());
    eiGen.addObject(contentOID);
    OutputStream octetStream = CMSUtils.createBEROctetOutputStream(eiGen.getRawOutputStream(), 0, true, _bufferSize);
    return new CmsCompressedOutputStream(compressor.getOutputStream(octetStream), sGen, cGen, eiGen);
}
Also used : BERSequenceGenerator(com.github.zhenwei.core.asn1.BERSequenceGenerator) OutputStream(java.io.OutputStream) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer)

Example 39 with ContentInfo

use of com.github.zhenwei.core.asn1.pkcs.ContentInfo 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 40 with ContentInfo

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

the class CMSEnvelopedDataStreamGenerator method open.

protected OutputStream open(OutputStream out, ASN1EncodableVector recipientInfos, OutputEncryptor encryptor) throws CMSException {
    try {
        // 
        // ContentInfo
        // 
        BERSequenceGenerator cGen = new BERSequenceGenerator(out);
        cGen.addObject(CMSObjectIdentifiers.envelopedData);
        // 
        // Encrypted Data
        // 
        BERSequenceGenerator envGen = new BERSequenceGenerator(cGen.getRawOutputStream(), 0, true);
        ASN1Set recipients;
        if (_berEncodeRecipientSet) {
            recipients = new BERSet(recipientInfos);
        } else {
            recipients = new DERSet(recipientInfos);
        }
        envGen.addObject(getVersion(recipientInfos));
        if (originatorInfo != null) {
            envGen.addObject(new DERTaggedObject(false, 0, originatorInfo));
        }
        envGen.getRawOutputStream().write(recipients.getEncoded());
        BERSequenceGenerator eiGen = new BERSequenceGenerator(envGen.getRawOutputStream());
        eiGen.addObject(CMSObjectIdentifiers.data);
        AlgorithmIdentifier encAlgId = encryptor.getAlgorithmIdentifier();
        eiGen.getRawOutputStream().write(encAlgId.getEncoded());
        OutputStream octetStream = CMSUtils.createBEROctetOutputStream(eiGen.getRawOutputStream(), 0, false, _bufferSize);
        return new CmsEnvelopedDataOutputStream(encryptor, octetStream, cGen, envGen, eiGen);
    } catch (IOException e) {
        throw new CMSException("exception decoding algorithm parameters.", e);
    }
}
Also used : BERSet(com.github.zhenwei.core.asn1.BERSet) ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) DERTaggedObject(com.github.zhenwei.core.asn1.DERTaggedObject) BERSequenceGenerator(com.github.zhenwei.core.asn1.BERSequenceGenerator) OutputStream(java.io.OutputStream) IOException(java.io.IOException) DERSet(com.github.zhenwei.core.asn1.DERSet) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Aggregations

IOException (java.io.IOException)25 ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)19 ContentInfo (com.github.zhenwei.pkix.util.asn1.cms.ContentInfo)15 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)14 ASN1Set (com.github.zhenwei.core.asn1.ASN1Set)13 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)12 OutputStream (java.io.OutputStream)12 ContentInfo (org.bouncycastle.asn1.pkcs.ContentInfo)11 DERSet (com.github.zhenwei.core.asn1.DERSet)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 BEROctetString (com.github.zhenwei.core.asn1.BEROctetString)9 CertificateEncodingException (java.security.cert.CertificateEncodingException)9 X509Certificate (java.security.cert.X509Certificate)9 Iterator (java.util.Iterator)9 BERSequence (com.github.zhenwei.core.asn1.BERSequence)7 PrivateKey (java.security.PrivateKey)7 CertificateException (java.security.cert.CertificateException)7 BERSequenceGenerator (com.github.zhenwei.core.asn1.BERSequenceGenerator)6 ContentInfo (com.github.zhenwei.core.asn1.pkcs.ContentInfo)6 KeyStoreException (java.security.KeyStoreException)6