Skip to content

Commit

Permalink
Improve KeyAgreeRecipientIdentifier getInstance methods
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdettman committed May 16, 2024
1 parent f436c8e commit 433fc69
Showing 1 changed file with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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.
* <p>
Expand All @@ -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(
Expand Down

0 comments on commit 433fc69

Please sign in to comment.