in jvm/src/main/kotlin/com/jetbrains/signatureverifier/bouncycastle/cms/CMSSignedData.kt [396:443]
fun addDigestAlgorithm(
signedData: CMSSignedData,
digestAlgorithm: AlgorithmIdentifier
): CMSSignedData {
val digestAlgorithms = signedData.digestAlgorithmIDs
val digestAlg = CMSSignedHelper.INSTANCE.fixDigestAlgID(digestAlgorithm, dgstAlgFinder)
//
// if the algorithm is already present there is no need to add it.
//
if (digestAlgorithms.contains(digestAlg)) {
return signedData
}
//
// copy
//
val cms = CMSSignedData(signedData)
//
// build up the new set
//
val digestAlgs: MutableSet<AlgorithmIdentifier> = HashSet()
val it: Iterator<*> = digestAlgorithms.iterator()
while (it.hasNext()) {
digestAlgs.add(CMSSignedHelper.INSTANCE.fixDigestAlgID(it.next() as AlgorithmIdentifier, dgstAlgFinder))
}
digestAlgs.add(digestAlg)
val digests = CMSUtils.convertToBERSet(digestAlgs)
val sD = signedData.signedData.toASN1Primitive() as ASN1Sequence
val vec = ASN1EncodableVector()
//
// signers are the last item in the sequence.
//
vec.add(sD.getObjectAt(0)) // version
vec.add(digests)
for (i in 2 until sD.size()) {
vec.add(sD.getObjectAt(i))
}
cms.signedData = SignedData.getInstance(BERSequence(vec))
//
// replace the contentInfo with the new one
//
cms.contentInfo = ContentInfo(cms.contentInfo.contentType, cms.signedData)
return cms
}