Skip to content

Commit 08e9e6b

Browse files
committed
preservation of digest AlgIds in existing CMSSignedData github #1585
1 parent b7e9f8d commit 08e9e6b

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

pkix/src/main/java/org/bouncycastle/cms/CMSSignedData.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.bouncycastle.asn1.ASN1Sequence;
2222
import org.bouncycastle.asn1.ASN1Set;
2323
import org.bouncycastle.asn1.BERSequence;
24+
import org.bouncycastle.asn1.DERNull;
2425
import org.bouncycastle.asn1.DLSet;
2526
import org.bouncycastle.asn1.cms.ContentInfo;
2627
import org.bouncycastle.asn1.cms.SignedData;
@@ -617,8 +618,17 @@ public static CMSSignedData replaceSigners(CMSSignedData signedData, SignerInfor
617618
vec.add(signer.toASN1Structure());
618619
}
619620

620-
ASN1Set digestSet = CMSUtils.convertToDlSet(digestAlgs);
621+
// keep ourselves compatible with what was there before - issue with
622+
// NULL appearing and disappearing in AlgorithmIdentifier parameters.
623+
Set<AlgorithmIdentifier> oldDigestAlgs = signedData.getDigestAlgorithmIDs();
624+
AlgorithmIdentifier[] oldDigestAlgIds = (AlgorithmIdentifier[])oldDigestAlgs.toArray(new AlgorithmIdentifier[oldDigestAlgs.size()]);
625+
AlgorithmIdentifier[] newDigestAlgIds = (AlgorithmIdentifier[])digestAlgs.toArray(new AlgorithmIdentifier[digestAlgs.size()]);
626+
627+
compareAndReplaceAlgIds(oldDigestAlgIds, newDigestAlgIds);
628+
629+
ASN1Set digestSet = new DLSet(newDigestAlgIds);
621630
ASN1Set signerSet = new DLSet(vec);
631+
622632
ASN1Sequence sD = (ASN1Sequence)signedData.signedData.toASN1Primitive();
623633

624634
//
@@ -645,6 +655,27 @@ public static CMSSignedData replaceSigners(CMSSignedData signedData, SignerInfor
645655
return cms;
646656
}
647657

658+
private static void compareAndReplaceAlgIds(AlgorithmIdentifier[] oldDigestAlgIds, AlgorithmIdentifier[] newDigestAlgIds)
659+
{
660+
for (int i = 0; i != newDigestAlgIds.length; i++)
661+
{
662+
AlgorithmIdentifier newId = newDigestAlgIds[i];
663+
664+
for (int j = 0; j != oldDigestAlgIds.length; j++)
665+
{
666+
AlgorithmIdentifier oldId = oldDigestAlgIds[j];
667+
if (newId.getAlgorithm().equals(oldId.getAlgorithm()))
668+
{
669+
if (newId.getParameters() == null || DERNull.INSTANCE.equals(newId.getParameters()))
670+
{
671+
newDigestAlgIds[i] = oldId;
672+
break;
673+
}
674+
}
675+
}
676+
}
677+
}
678+
648679
/**
649680
* Replace the certificate and CRL information associated with this
650681
* CMSSignedData object with the new one passed in.

0 commit comments

Comments
 (0)