Search in sources :

Example 1 with BERSequenceGenerator

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

the class CMSEnvelopedDataStreamGenerator method open.

protected OutputStream open(ASN1ObjectIdentifier dataType, OutputStream out, ASN1EncodableVector recipientInfos, OutputEncryptor encryptor) throws IOException {
    // 
    // ContentInfo
    // 
    BERSequenceGenerator cGen = new BERSequenceGenerator(out);
    cGen.addObject(CMSObjectIdentifiers.envelopedData);
    // 
    // Encrypted Data
    // 
    BERSequenceGenerator envGen = new BERSequenceGenerator(cGen.getRawOutputStream(), 0, true);
    envGen.addObject(getVersion(recipientInfos));
    if (originatorInfo != null) {
        envGen.addObject(new DERTaggedObject(false, 0, originatorInfo));
    }
    if (_berEncodeRecipientSet) {
        envGen.getRawOutputStream().write(new BERSet(recipientInfos).getEncoded());
    } else {
        envGen.getRawOutputStream().write(new DERSet(recipientInfos).getEncoded());
    }
    BERSequenceGenerator eiGen = new BERSequenceGenerator(envGen.getRawOutputStream());
    eiGen.addObject(dataType);
    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);
}
Also used : BERSet(com.github.zhenwei.core.asn1.BERSet) DERTaggedObject(com.github.zhenwei.core.asn1.DERTaggedObject) BERSequenceGenerator(com.github.zhenwei.core.asn1.BERSequenceGenerator) OutputStream(java.io.OutputStream) DERSet(com.github.zhenwei.core.asn1.DERSet) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 2 with BERSequenceGenerator

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

the class CMSSignedDataParser method replaceCertificatesAndCRLs.

/**
 * Replace the certificate and CRL information associated with this CMSSignedData object with the
 * new one passed in.
 * <p>
 * The output stream is returned unclosed.
 * </p>
 *
 * @param original  the signed data stream to be used as a base.
 * @param certs     new certificates to be used, if any.
 * @param crls      new CRLs to be used, if any.
 * @param attrCerts new attribute certificates to be used, if any.
 * @param out       the stream to write the new signed data object to.
 * @return out.
 * @throws CMSException if there is an error processing the CertStore
 */
public static OutputStream replaceCertificatesAndCRLs(InputStream original, Store certs, Store crls, Store attrCerts, 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
    sigGen.getRawOutputStream().write(signedData.getDigestAlgorithms().toASN1Primitive().getEncoded());
    // encap content info
    ContentInfoParser encapContentInfo = signedData.getEncapContentInfo();
    BERSequenceGenerator eiGen = new BERSequenceGenerator(sigGen.getRawOutputStream());
    eiGen.addObject(encapContentInfo.getContentType());
    pipeEncapsulatedOctetString(encapContentInfo, eiGen.getRawOutputStream());
    eiGen.close();
    // 
    // skip existing certs and CRLs
    // 
    getASN1Set(signedData.getCertificates());
    getASN1Set(signedData.getCrls());
    // 
    if (certs != null || attrCerts != null) {
        List certificates = new ArrayList();
        if (certs != null) {
            certificates.addAll(CMSUtils.getCertificatesFromStore(certs));
        }
        if (attrCerts != null) {
            certificates.addAll(CMSUtils.getAttributeCertificatesFromStore(attrCerts));
        }
        ASN1Set asn1Certs = CMSUtils.createBerSetFromList(certificates);
        if (asn1Certs.size() > 0) {
            sigGen.getRawOutputStream().write(new DERTaggedObject(false, 0, asn1Certs).getEncoded());
        }
    }
    if (crls != null) {
        ASN1Set asn1Crls = CMSUtils.createBerSetFromList(CMSUtils.getCRLsFromStore(crls));
        if (asn1Crls.size() > 0) {
            sigGen.getRawOutputStream().write(new DERTaggedObject(false, 1, asn1Crls).getEncoded());
        }
    }
    sigGen.getRawOutputStream().write(signedData.getSignerInfos().toASN1Primitive().getEncoded());
    sigGen.close();
    sGen.close();
    return out;
}
Also used : ContentInfoParser(com.github.zhenwei.pkix.util.asn1.cms.ContentInfoParser) ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) SignedDataParser(com.github.zhenwei.pkix.util.asn1.cms.SignedDataParser) DERTaggedObject(com.github.zhenwei.core.asn1.DERTaggedObject) BERSequenceGenerator(com.github.zhenwei.core.asn1.BERSequenceGenerator) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ASN1StreamParser(com.github.zhenwei.core.asn1.ASN1StreamParser)

Example 3 with BERSequenceGenerator

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

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

the class CMSAuthenticatedDataStreamGenerator method open.

/**
 * generate an authenticated data structure with the encapsulated bytes marked as type dataType.
 *
 * @param dataType         the type of the data been written to the object.
 * @param out              the stream to store the authenticated structure in.
 * @param macCalculator    calculator for the MAC to be attached to the data.
 * @param digestCalculator calculator for computing digest of the encapsulated data.
 */
public OutputStream open(ASN1ObjectIdentifier dataType, OutputStream out, MacCalculator macCalculator, DigestCalculator digestCalculator) throws CMSException {
    this.macCalculator = macCalculator;
    try {
        ASN1EncodableVector recipientInfos = new ASN1EncodableVector();
        for (Iterator it = recipientInfoGenerators.iterator(); it.hasNext(); ) {
            RecipientInfoGenerator recipient = (RecipientInfoGenerator) it.next();
            recipientInfos.add(recipient.generate(macCalculator.getKey()));
        }
        // 
        // ContentInfo
        // 
        BERSequenceGenerator cGen = new BERSequenceGenerator(out);
        cGen.addObject(CMSObjectIdentifiers.authenticatedData);
        // 
        // Authenticated Data
        // 
        BERSequenceGenerator authGen = new BERSequenceGenerator(cGen.getRawOutputStream(), 0, true);
        authGen.addObject(new ASN1Integer(AuthenticatedData.calculateVersion(originatorInfo)));
        if (originatorInfo != null) {
            authGen.addObject(new DERTaggedObject(false, 0, originatorInfo));
        }
        if (berEncodeRecipientSet) {
            authGen.getRawOutputStream().write(new BERSet(recipientInfos).getEncoded());
        } else {
            authGen.getRawOutputStream().write(new DERSet(recipientInfos).getEncoded());
        }
        AlgorithmIdentifier macAlgId = macCalculator.getAlgorithmIdentifier();
        authGen.getRawOutputStream().write(macAlgId.getEncoded());
        if (digestCalculator != null) {
            authGen.addObject(new DERTaggedObject(false, 1, digestCalculator.getAlgorithmIdentifier()));
        }
        BERSequenceGenerator eiGen = new BERSequenceGenerator(authGen.getRawOutputStream());
        eiGen.addObject(dataType);
        OutputStream octetStream = CMSUtils.createBEROctetOutputStream(eiGen.getRawOutputStream(), 0, true, bufferSize);
        OutputStream mOut;
        if (digestCalculator != null) {
            mOut = new TeeOutputStream(octetStream, digestCalculator.getOutputStream());
        } else {
            mOut = new TeeOutputStream(octetStream, macCalculator.getOutputStream());
        }
        return new CmsAuthenticatedDataOutputStream(macCalculator, digestCalculator, dataType, mOut, cGen, authGen, eiGen);
    } catch (IOException e) {
        throw new CMSException("exception decoding algorithm parameters.", e);
    }
}
Also used : BERSet(com.github.zhenwei.core.asn1.BERSet) TeeOutputStream(com.github.zhenwei.core.util.io.TeeOutputStream) DERTaggedObject(com.github.zhenwei.core.asn1.DERTaggedObject) OutputStream(java.io.OutputStream) TeeOutputStream(com.github.zhenwei.core.util.io.TeeOutputStream) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) IOException(java.io.IOException) DERSet(com.github.zhenwei.core.asn1.DERSet) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) BERSequenceGenerator(com.github.zhenwei.core.asn1.BERSequenceGenerator) Iterator(java.util.Iterator) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector)

Example 5 with BERSequenceGenerator

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

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