From 433fc698e9c953a144f33a7d5b57077b4593ffec Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 16 May 2024 13:00:31 +0700 Subject: [PATCH] Improve KeyAgreeRecipientIdentifier getInstance methods --- .../asn1/cms/KeyAgreeRecipientIdentifier.java | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/util/src/main/java/org/bouncycastle/asn1/cms/KeyAgreeRecipientIdentifier.java b/util/src/main/java/org/bouncycastle/asn1/cms/KeyAgreeRecipientIdentifier.java index f67d3b88a3..98ca19a504 100644 --- a/util/src/main/java/org/bouncycastle/asn1/cms/KeyAgreeRecipientIdentifier.java +++ b/util/src/main/java/org/bouncycastle/asn1/cms/KeyAgreeRecipientIdentifier.java @@ -3,8 +3,8 @@ import org.bouncycastle.asn1.ASN1Choice; import org.bouncycastle.asn1.ASN1Object; import org.bouncycastle.asn1.ASN1Primitive; -import org.bouncycastle.asn1.ASN1Sequence; import org.bouncycastle.asn1.ASN1TaggedObject; +import org.bouncycastle.asn1.ASN1Util; import org.bouncycastle.asn1.DERTaggedObject; /** @@ -37,9 +37,14 @@ public static KeyAgreeRecipientIdentifier getInstance( ASN1TaggedObject obj, boolean explicit) { - return getInstance(ASN1Sequence.getInstance(obj, explicit)); + if (!explicit) + { + throw new IllegalArgumentException("choice item must be explicitly tagged"); + } + + return getInstance(obj.getExplicitBaseObject()); } - + /** * Return an KeyAgreeRecipientIdentifier object from the given object. *

@@ -62,19 +67,20 @@ public static KeyAgreeRecipientIdentifier getInstance( { return (KeyAgreeRecipientIdentifier)obj; } - - if (obj instanceof ASN1Sequence) - { - return new KeyAgreeRecipientIdentifier(IssuerAndSerialNumber.getInstance(obj)); - } - - if (obj instanceof ASN1TaggedObject && ((ASN1TaggedObject)obj).getTagNo() == 0) + + if (obj instanceof ASN1TaggedObject) { - return new KeyAgreeRecipientIdentifier(RecipientKeyIdentifier.getInstance( - (ASN1TaggedObject)obj, false)); + ASN1TaggedObject taggedObject = (ASN1TaggedObject)obj; + if (taggedObject.hasContextTag(0)) + { + return new KeyAgreeRecipientIdentifier(RecipientKeyIdentifier.getInstance(taggedObject, false)); + } + + throw new IllegalArgumentException("Invalid KeyAgreeRecipientIdentifier tag: " + + ASN1Util.getTagText(taggedObject)); } - - throw new IllegalArgumentException("Invalid KeyAgreeRecipientIdentifier: " + obj.getClass().getName()); + + return new KeyAgreeRecipientIdentifier(IssuerAndSerialNumber.getInstance(obj)); } public KeyAgreeRecipientIdentifier(