Skip to content

Commit

Permalink
Handle ApplicationSpecific via TaggedObject
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdettman committed Nov 7, 2021
1 parent f79d0d8 commit aa76467
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 48 deletions.
11 changes: 11 additions & 0 deletions core/src/main/java/org/bouncycastle/asn1/ASN1TaggedObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,17 @@ public ASN1Primitive getObject()
return obj.toASN1Primitive();
}

/**
* Needed for open types, until we have better type-guided parsing support. Use sparingly for other
* purposes, and prefer {@link #getExplicitBaseTagged()}, {@link #getImplicitBaseTagged(int, int)} or
* {@link #getBaseUniversal(boolean, int)} where possible. Before using, check for matching tag
* {@link #getTagClass() class} and {@link #getTagNo() number}.
*/
public ASN1Object getBaseObject()
{
return obj instanceof ASN1Object ? (ASN1Object)obj : obj.toASN1Primitive();
}

/**
* Needed for open types, until we have better type-guided parsing support. Use
* sparingly for other purposes, and prefer {@link #getExplicitBaseTagged()} or
Expand Down
57 changes: 9 additions & 48 deletions core/src/main/java/org/bouncycastle/asn1/util/ASN1Dump.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.bouncycastle.asn1.util;

import java.io.IOException;

import org.bouncycastle.asn1.ASN1ApplicationSpecific;
import org.bouncycastle.asn1.ASN1BMPString;
import org.bouncycastle.asn1.ASN1BitString;
Expand Down Expand Up @@ -34,12 +32,10 @@
import org.bouncycastle.asn1.BERSequence;
import org.bouncycastle.asn1.BERSet;
import org.bouncycastle.asn1.BERTaggedObject;
import org.bouncycastle.asn1.BERTags;
import org.bouncycastle.asn1.DERApplicationSpecific;
import org.bouncycastle.asn1.DERBitString;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.DERSet;
import org.bouncycastle.asn1.DLApplicationSpecific;
import org.bouncycastle.asn1.DERTaggedObject;
import org.bouncycastle.asn1.DLBitString;
import org.bouncycastle.util.Strings;
import org.bouncycastle.util.encoders.Hex;
Expand Down Expand Up @@ -122,28 +118,19 @@ else if (obj instanceof DERSet)
}
else if (obj instanceof ASN1ApplicationSpecific)
{
if (obj instanceof DERApplicationSpecific)
{
buf.append(outputApplicationSpecific("DER", indent, verbose, obj, nl));
}
else if (obj instanceof DLApplicationSpecific)
{
buf.append(outputApplicationSpecific("", indent, verbose, obj, nl));
}
else
{
buf.append(outputApplicationSpecific("BER", indent, verbose, obj, nl));
}
_dumpAsString(indent, verbose, ((ASN1ApplicationSpecific)obj).getTaggedObject(), buf);
}
else if (obj instanceof ASN1TaggedObject)
{
String tab = indent + TAB;

buf.append(indent);
if (obj instanceof BERTaggedObject)
{
buf.append("BER Tagged ");
}
else if (obj instanceof DERTaggedObject)
{
buf.append("DER Tagged ");
}
else
{
buf.append("Tagged ");
Expand All @@ -160,7 +147,9 @@ else if (obj instanceof ASN1TaggedObject)

buf.append(nl);

_dumpAsString(tab, verbose, o.getObject(), buf);
String baseIndent = indent + TAB;

_dumpAsString(baseIndent, verbose, o.getBaseObject().toASN1Primitive(), buf);
}
else if (obj instanceof ASN1OctetString)
{
Expand Down Expand Up @@ -307,34 +296,6 @@ else if (obj instanceof ASN1External)
buf.append(indent + obj.toString() + nl);
}
}

private static String outputApplicationSpecific(String type, String indent, boolean verbose, ASN1Primitive obj, String nl)
{
ASN1ApplicationSpecific app = ASN1ApplicationSpecific.getInstance(obj);
StringBuffer buf = new StringBuffer();

String tagText = ASN1Util.getTagText(BERTags.APPLICATION, app.getApplicationTag());

if (app.isConstructed())
{
try
{
ASN1Sequence s = ASN1Sequence.getInstance(app.getObject(BERTags.SEQUENCE));
buf.append(indent + type + tagText + nl);
for (int i = 0, count = s.size(); i < count; ++i)
{
_dumpAsString(indent + TAB, verbose, s.getObjectAt(i).toASN1Primitive(), buf);
}
}
catch (IOException e)
{
buf.append(e);
}
return buf.toString();
}

return indent + type + tagText + " (" + Strings.fromByteArray(Hex.encode(app.getContents())) + ")" + nl;
}

/**
* dump out a DER object as a formatted string, in non-verbose mode.
Expand Down

0 comments on commit aa76467

Please sign in to comment.