OutputStream out)
throws CMSException, IOException
{
ASN1StreamParser in = new ASN1StreamParser(original, CMSUtils.getMaximumMemory());
ContentInfoParser contentInfo = new ContentInfoParser((ASN1SequenceParser)in.readObject());
SignedDataParser signedData = SignedDataParser.getInstance(contentInfo.getContent(DERTags.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().getDERObject().getEncoded());
// encap content info
ContentInfoParser encapContentInfo = signedData.getEncapContentInfo();
BERSequenceGenerator eiGen = new BERSequenceGenerator(sigGen.getRawOutputStream());
eiGen.addObject(encapContentInfo.getContentType());
ASN1OctetStringParser octs = (ASN1OctetStringParser)
encapContentInfo.getContent(DERTags.OCTET_STRING);
if (octs != null)
{
pipeOctetString(octs, eiGen.getRawOutputStream());
}
eiGen.close();
//
// skip existing certs and CRLs
//
getASN1Set(signedData.getCertificates());
getASN1Set(signedData.getCrls());
//
// replace the certs and crls in the SignedData object
//
ASN1Set certs;
try
{
certs = CMSUtils.createBerSetFromList(CMSUtils.getCertificatesFromStore(certsAndCrls));
}
catch (CertStoreException e)
{
throw new CMSException("error getting certs from certStore", e);
}
if (certs.size() > 0)
{
sigGen.getRawOutputStream().write(new DERTaggedObject(false, 0, certs).getEncoded());
}
ASN1Set crls;
try
{
crls = CMSUtils.createBerSetFromList(CMSUtils.getCRLsFromStore(certsAndCrls));
}
catch (CertStoreException e)
{
throw new CMSException("error getting crls from certStore", e);
}
if (crls.size() > 0)
{
sigGen.getRawOutputStream().write(new DERTaggedObject(false, 1, crls).getEncoded());
}
sigGen.getRawOutputStream().write(signedData.getSignerInfos().getDERObject().getEncoded());
sigGen.close();
sGen.close();