Search in sources :

Example 6 with BERSequenceGenerator

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

the class CMSSignedDataStreamGenerator method open.

/**
 * generate a signed object that for a CMS Signed Data object using the given provider - if
 * encapsulate is true a copy of the message will be included in the signature. The content type
 * is set according to the OID represented by the string signedContentType.
 *
 * @param eContentType     OID for data to be signed.
 * @param out              stream the CMS object is to be written to.
 * @param encapsulate      true if data should be encapsulated.
 * @param dataOutputStream output stream to copy the data being signed to.
 */
public OutputStream open(ASN1ObjectIdentifier eContentType, OutputStream out, boolean encapsulate, OutputStream dataOutputStream) throws IOException {
    // TODO
    // if (_signerInfs.isEmpty())
    // {
    // /* RFC 3852 5.2
    // * "In the degenerate case where there are no signers, the
    // * EncapsulatedContentInfo value being "signed" is irrelevant.  In this
    // * case, the content type within the EncapsulatedContentInfo value being
    // * "signed" MUST be id-data (as defined in section 4), and the content
    // * field of the EncapsulatedContentInfo value MUST be omitted."
    // */
    // if (encapsulate)
    // {
    // throw new IllegalArgumentException("no signers, encapsulate must be false");
    // }
    // if (!DATA.equals(eContentType))
    // {
    // throw new IllegalArgumentException("no signers, eContentType must be id-data");
    // }
    // }
    // 
    // if (!DATA.equals(eContentType))
    // {
    // /* RFC 3852 5.3
    // * [The 'signedAttrs']...
    // * field is optional, but it MUST be present if the content type of
    // * the EncapsulatedContentInfo value being signed is not id-data.
    // */
    // // TODO signedAttrs must be present for all signers
    // }
    // 
    // ContentInfo
    // 
    BERSequenceGenerator sGen = new BERSequenceGenerator(out);
    sGen.addObject(CMSObjectIdentifiers.signedData);
    // 
    // Signed Data
    // 
    BERSequenceGenerator sigGen = new BERSequenceGenerator(sGen.getRawOutputStream(), 0, true);
    sigGen.addObject(calculateVersion(eContentType));
    Set<AlgorithmIdentifier> digestAlgs = new HashSet<AlgorithmIdentifier>();
    // 
    for (Iterator it = _signers.iterator(); it.hasNext(); ) {
        SignerInformation signer = (SignerInformation) it.next();
        CMSUtils.addDigestAlgs(digestAlgs, signer, digestAlgIdFinder);
    }
    for (Iterator it = signerGens.iterator(); it.hasNext(); ) {
        SignerInfoGenerator signerGen = (SignerInfoGenerator) it.next();
        digestAlgs.add(signerGen.getDigestAlgorithm());
    }
    sigGen.getRawOutputStream().write(CMSUtils.convertToBERSet(digestAlgs).getEncoded());
    BERSequenceGenerator eiGen = new BERSequenceGenerator(sigGen.getRawOutputStream());
    eiGen.addObject(eContentType);
    // If encapsulating, add the data as an octet string in the sequence
    OutputStream encapStream = encapsulate ? CMSUtils.createBEROctetOutputStream(eiGen.getRawOutputStream(), 0, true, _bufferSize) : null;
    // Also send the data to 'dataOutputStream' if necessary
    OutputStream contentStream = CMSUtils.getSafeTeeOutputStream(dataOutputStream, encapStream);
    // Let all the signers see the data as it is written
    OutputStream sigStream = CMSUtils.attachSignersToOutputStream(signerGens, contentStream);
    return new CmsSignedDataOutputStream(sigStream, eContentType, sGen, sigGen, eiGen);
}
Also used : BERSequenceGenerator(com.github.zhenwei.core.asn1.BERSequenceGenerator) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) HashSet(java.util.HashSet)

Example 7 with BERSequenceGenerator

use of com.github.zhenwei.core.asn1.BERSequenceGenerator 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

BERSequenceGenerator (com.github.zhenwei.core.asn1.BERSequenceGenerator)7 OutputStream (java.io.OutputStream)5 DERSet (com.github.zhenwei.core.asn1.DERSet)4 DERTaggedObject (com.github.zhenwei.core.asn1.DERTaggedObject)4 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)4 BERSet (com.github.zhenwei.core.asn1.BERSet)3 Iterator (java.util.Iterator)3 ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)2 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)2 ASN1Set (com.github.zhenwei.core.asn1.ASN1Set)2 ASN1StreamParser (com.github.zhenwei.core.asn1.ASN1StreamParser)2 ContentInfoParser (com.github.zhenwei.pkix.util.asn1.cms.ContentInfoParser)2 SignedDataParser (com.github.zhenwei.pkix.util.asn1.cms.SignedDataParser)2 IOException (java.io.IOException)2 TeeOutputStream (com.github.zhenwei.core.util.io.TeeOutputStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1