Search in sources :

Example 6 with ASN1StreamParser

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

Aggregations

IOException (java.io.IOException)4 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)4 ASN1StreamParser (org.bouncycastle.asn1.ASN1StreamParser)4 CertprofileException (org.xipki.ca.api.profile.CertprofileException)4 HashMap (java.util.HashMap)3 ConstantExtValue (org.xipki.ca.certprofile.x509.jaxb.ConstantExtValue)3 ExtensionType (org.xipki.ca.certprofile.x509.jaxb.ExtensionType)3 ASN1StreamParser (com.github.zhenwei.core.asn1.ASN1StreamParser)2 BERSequenceGenerator (com.github.zhenwei.core.asn1.BERSequenceGenerator)2 ContentInfoParser (com.github.zhenwei.pkix.util.asn1.cms.ContentInfoParser)2 SignedDataParser (com.github.zhenwei.pkix.util.asn1.cms.SignedDataParser)2 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)2 ExtensionValue (org.xipki.ca.api.profile.ExtensionValue)2 QaExtensionValue (org.xipki.ca.qa.internal.QaExtensionValue)2 ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)1 ASN1Set (com.github.zhenwei.core.asn1.ASN1Set)1 DERSet (com.github.zhenwei.core.asn1.DERSet)1 DERTaggedObject (com.github.zhenwei.core.asn1.DERTaggedObject)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1