|
| 1 | +package org.bouncycastle.cms; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.io.OutputStream; |
| 6 | +import java.util.Iterator; |
| 7 | +import java.util.LinkedHashMap; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import org.bouncycastle.asn1.ASN1EncodableVector; |
| 11 | +import org.bouncycastle.asn1.ASN1SequenceParser; |
| 12 | +import org.bouncycastle.asn1.ASN1StreamParser; |
| 13 | +import org.bouncycastle.asn1.BERSequenceGenerator; |
| 14 | +import org.bouncycastle.asn1.BERTags; |
| 15 | +import org.bouncycastle.asn1.DERSet; |
| 16 | +import org.bouncycastle.asn1.DLSet; |
| 17 | +import org.bouncycastle.asn1.cms.CMSObjectIdentifiers; |
| 18 | +import org.bouncycastle.asn1.cms.ContentInfoParser; |
| 19 | +import org.bouncycastle.asn1.cms.SignedDataParser; |
| 20 | +import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
| 21 | +import org.bouncycastle.operator.DefaultDigestAlgorithmIdentifierFinder; |
| 22 | +import org.bouncycastle.operator.DigestAlgorithmIdentifierFinder; |
| 23 | +import org.bouncycastle.operator.DigestCalculator; |
| 24 | +import org.bouncycastle.operator.DigestCalculatorProvider; |
| 25 | +import org.bouncycastle.operator.OperatorCreationException; |
| 26 | + |
| 27 | +public class CMSSignedDataStreamEditor |
| 28 | +{ |
| 29 | + private static final CMSSignedHelper HELPER = CMSSignedHelper.INSTANCE; |
| 30 | + private static final DefaultDigestAlgorithmIdentifierFinder dgstAlgFinder = new DefaultDigestAlgorithmIdentifierFinder(); |
| 31 | + /** |
| 32 | + * Add the specified digest algorithm to the signed data contained in the input stream and write |
| 33 | + * the updated signed data to the provided output stream. This ensures that the output signed data |
| 34 | + * includes the specified digest algorithm. Uses the provided DigestAlgorithmIdentifierFinder to |
| 35 | + * create the digest sets and the DigestCalculatorProvider for computing the required digests. |
| 36 | + * <p> |
| 37 | + * The output stream is returned unclosed. |
| 38 | + * </p> |
| 39 | + * |
| 40 | + * @param out the output stream where the updated signed data object will be written. |
| 41 | + * @param original the input stream containing the original signed data to be modified. |
| 42 | + * @param digestAlgorithm the digest algorithm to be added to the signed data. |
| 43 | + * @param digestAlgIdFinder the DigestAlgorithmIdentifierFinder used to create the digest sets. |
| 44 | + * @param digestCalculatorProvider the DigestCalculatorProvider used to compute the digests. |
| 45 | + * @return the output stream containing the updated signed data. |
| 46 | + */ |
| 47 | + public static OutputStream addDigestAlgorithm(OutputStream out, InputStream original, |
| 48 | + AlgorithmIdentifier digestAlgorithm, |
| 49 | + DigestAlgorithmIdentifierFinder digestAlgIdFinder, |
| 50 | + DigestCalculatorProvider digestCalculatorProvider) |
| 51 | + throws IOException, CMSException |
| 52 | + { |
| 53 | + ContentInfoParser contentInfo = new ContentInfoParser((ASN1SequenceParser)new ASN1StreamParser(original).readObject()); |
| 54 | + SignedDataParser signedData = SignedDataParser.getInstance(contentInfo.getContent(BERTags.SEQUENCE)); |
| 55 | + BERSequenceGenerator sGen = new BERSequenceGenerator(out); |
| 56 | + |
| 57 | + sGen.addObject(CMSObjectIdentifiers.signedData); |
| 58 | + |
| 59 | + BERSequenceGenerator sigGen = new BERSequenceGenerator(sGen.getRawOutputStream(), 0, true); |
| 60 | + |
| 61 | + // version number |
| 62 | + sigGen.addObject(signedData.getVersion()); |
| 63 | + // digests |
| 64 | + ASN1EncodableVector digestAlgs = new ASN1EncodableVector(); |
| 65 | + Map<AlgorithmIdentifier, DigestCalculator> digests = new LinkedHashMap<AlgorithmIdentifier, DigestCalculator>(); |
| 66 | + try |
| 67 | + { |
| 68 | + for (Iterator it = ((DLSet)signedData.getDigestAlgorithms().toASN1Primitive()).iterator(); it.hasNext(); ) |
| 69 | + { |
| 70 | + AlgorithmIdentifier oid = AlgorithmIdentifier.getInstance(it.next()); |
| 71 | + digestAlgs.add(HELPER.fixDigestAlgID(oid, digestAlgIdFinder)); |
| 72 | + digests.put(oid, digestCalculatorProvider.get(oid)); |
| 73 | + } |
| 74 | + if (!digests.containsKey(digestAlgorithm)) |
| 75 | + { |
| 76 | + digestAlgs.add(HELPER.fixDigestAlgID(digestAlgorithm, digestAlgIdFinder)); |
| 77 | + digests.put(digestAlgorithm, digestCalculatorProvider.get(digestAlgorithm)); |
| 78 | + } |
| 79 | + } |
| 80 | + catch (OperatorCreationException e) |
| 81 | + { |
| 82 | + throw new CMSException("unable to find digest algorithm"); |
| 83 | + } |
| 84 | + sigGen.getRawOutputStream().write(new DERSet(digestAlgs).getEncoded()); |
| 85 | + |
| 86 | + CMSSignedDataParser.writeEncapContentInfoToGenerator(signedData, sigGen); |
| 87 | + |
| 88 | + CMSSignedDataParser.writeSetToGeneratorTagged(sigGen, signedData.getCertificates(), 0); |
| 89 | + CMSSignedDataParser.writeSetToGeneratorTagged(sigGen, signedData.getCrls(), 1); |
| 90 | + sigGen.getRawOutputStream().write(signedData.getSignerInfos().toASN1Primitive().getEncoded()); |
| 91 | + |
| 92 | + sigGen.close(); |
| 93 | + sGen.close(); |
| 94 | + |
| 95 | + return out; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Add the specified digest algorithm to the signed data contained in the input stream and write |
| 100 | + * the updated signed data to the provided output stream. This ensures that the output signed data |
| 101 | + * includes the specified digest algorithm. |
| 102 | + * <p> |
| 103 | + * The output stream is returned unclosed. |
| 104 | + * </p> |
| 105 | + * |
| 106 | + * @param out the output stream where the updated signed data object will be written. |
| 107 | + * @param original the input stream containing the original signed data to be modified. |
| 108 | + * @param digestAlgorithm the digest algorithm to be added to the signed data. |
| 109 | + * @return the output stream containing the updated signed data. |
| 110 | + */ |
| 111 | + public static OutputStream addDigestAlgorithm(OutputStream out, InputStream original, AlgorithmIdentifier digestAlgorithm, DigestCalculatorProvider digestCalculatorProvider) |
| 112 | + throws IOException, CMSException |
| 113 | + { |
| 114 | + return addDigestAlgorithm(out, original, digestAlgorithm, dgstAlgFinder, digestCalculatorProvider); |
| 115 | + } |
| 116 | +} |
0 commit comments