Skip to content

Commit 433fc69

Browse files
committed
Improve KeyAgreeRecipientIdentifier getInstance methods
1 parent f436c8e commit 433fc69

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

util/src/main/java/org/bouncycastle/asn1/cms/KeyAgreeRecipientIdentifier.java

+20-14
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import org.bouncycastle.asn1.ASN1Choice;
44
import org.bouncycastle.asn1.ASN1Object;
55
import org.bouncycastle.asn1.ASN1Primitive;
6-
import org.bouncycastle.asn1.ASN1Sequence;
76
import org.bouncycastle.asn1.ASN1TaggedObject;
7+
import org.bouncycastle.asn1.ASN1Util;
88
import org.bouncycastle.asn1.DERTaggedObject;
99

1010
/**
@@ -37,9 +37,14 @@ public static KeyAgreeRecipientIdentifier getInstance(
3737
ASN1TaggedObject obj,
3838
boolean explicit)
3939
{
40-
return getInstance(ASN1Sequence.getInstance(obj, explicit));
40+
if (!explicit)
41+
{
42+
throw new IllegalArgumentException("choice item must be explicitly tagged");
43+
}
44+
45+
return getInstance(obj.getExplicitBaseObject());
4146
}
42-
47+
4348
/**
4449
* Return an KeyAgreeRecipientIdentifier object from the given object.
4550
* <p>
@@ -62,19 +67,20 @@ public static KeyAgreeRecipientIdentifier getInstance(
6267
{
6368
return (KeyAgreeRecipientIdentifier)obj;
6469
}
65-
66-
if (obj instanceof ASN1Sequence)
67-
{
68-
return new KeyAgreeRecipientIdentifier(IssuerAndSerialNumber.getInstance(obj));
69-
}
70-
71-
if (obj instanceof ASN1TaggedObject && ((ASN1TaggedObject)obj).getTagNo() == 0)
70+
71+
if (obj instanceof ASN1TaggedObject)
7272
{
73-
return new KeyAgreeRecipientIdentifier(RecipientKeyIdentifier.getInstance(
74-
(ASN1TaggedObject)obj, false));
73+
ASN1TaggedObject taggedObject = (ASN1TaggedObject)obj;
74+
if (taggedObject.hasContextTag(0))
75+
{
76+
return new KeyAgreeRecipientIdentifier(RecipientKeyIdentifier.getInstance(taggedObject, false));
77+
}
78+
79+
throw new IllegalArgumentException("Invalid KeyAgreeRecipientIdentifier tag: "
80+
+ ASN1Util.getTagText(taggedObject));
7581
}
76-
77-
throw new IllegalArgumentException("Invalid KeyAgreeRecipientIdentifier: " + obj.getClass().getName());
82+
83+
return new KeyAgreeRecipientIdentifier(IssuerAndSerialNumber.getInstance(obj));
7884
}
7985

8086
public KeyAgreeRecipientIdentifier(

0 commit comments

Comments
 (0)