Class ASN1Utils
java.lang.Object
paccor.tcg.credential.ASN1Utils
Common methods to transform ASN1 objects.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic org.bouncycastle.asn1.ASN1BitStringGet an ASN1BitString from the given object.static org.bouncycastle.asn1.ASN1BooleangetBoolean(boolean b) Get an ASN1Boolean from the primitive.static org.bouncycastle.asn1.ASN1BooleangetBoolean(Object o) Get an ASN1Boolean from the given object.static org.bouncycastle.asn1.ASN1EnumeratedGet an ASN1Enumerated from the given object.static org.bouncycastle.asn1.ASN1GeneralizedTimeGet a date object from the given object.static org.bouncycastle.asn1.ASN1IA5StringGet a string from the given object.static org.bouncycastle.asn1.ASN1IntegergetInteger(Object o) Get an ASN1Integer from the given object.static org.bouncycastle.asn1.ASN1OctetStringGet an octet string from the given object.static org.bouncycastle.asn1.ASN1ObjectIdentifierGet an OID from the given object.static org.bouncycastle.asn1.ASN1PrintableStringGet a string from the given object.static org.bouncycastle.asn1.ASN1SequenceGet an ASN1Sequence from the given object.static final StringgetStringOrDefault(org.bouncycastle.asn1.ASN1String str, String def) Get a string or default if null.static org.bouncycastle.asn1.ASN1UTF8StringGet a string from the given object.static booleanisSequence(Object o) Test if the object is an ASN1Sequencestatic final List<org.bouncycastle.asn1.ASN1Object> listUntaggedElements(org.bouncycastle.asn1.ASN1Sequence seq) List all untagged elements from the sequence in order.parseTaggedElements(org.bouncycastle.asn1.ASN1Sequence seq) Pull out all ASN1TaggedObjects from the sequence and put them into a map where the keys are tag numbers of ASN1TaggedObjects and the values are the ASN1Object they reference.static byte[]resizeOctets(int sequenceSize, byte[] value) Resize the given byte array to the given sequenceSize.static org.bouncycastle.asn1.ASN1OctetStringresizeOctets(int sequenceSize, @NonNull String valueHex) Convert the given hex string into a byte array, then resize it to the given sequenceSize.static org.bouncycastle.asn1.ASN1OctetStringresizeOctets(int sequenceSize, org.bouncycastle.asn1.ASN1OctetString value) Resize the given ASN1OctetString to the given sequenceSize.static final <T> TsafeGetDefaultElement(org.bouncycastle.asn1.ASN1Object obj, T defaultAnswer, @NonNull Function<Object, T> decoder) Attempt to transform the ASN1Object into T using the method supplied.static final <T> TsafeGetDefaultElementFromSequence(org.bouncycastle.asn1.ASN1Sequence seq, int zeroBasedIndex, T defaultAnswer, @NonNull Function<Object, T> decoder) Attempt to transform the ASN1Object at the requested position within the ASN1Sequence into T using the method supplied.static final <T> TsafeGetFirstInstanceFromSequenceGivenRange(org.bouncycastle.asn1.ASN1Sequence seq, int zeroBasedStartIndex, int zeroBasedMaxIndex, T defaultAnswer, @NonNull Function<Object, T> decoder) Look for the first element, within the supplied range of the ASN1Sequence, that can be transformed into T using the method supplied.static final <T extends org.bouncycastle.asn1.ASN1Object, U extends Collection<T>>
org.bouncycastle.asn1.ASN1EncodableVectortoASN1EncodableVector(U list) Given a list of ASN1Objects, put all elements into an ASN1 vector.
-
Constructor Details
-
ASN1Utils
public ASN1Utils()
-
-
Method Details
-
toASN1EncodableVector
public static final <T extends org.bouncycastle.asn1.ASN1Object, U extends Collection<T>> org.bouncycastle.asn1.ASN1EncodableVector toASN1EncodableVector(U list) Given a list of ASN1Objects, put all elements into an ASN1 vector.- Type Parameters:
T- Anything that extends or implements ASN1EncodableU- List of T- Parameters:
list- List of ASN1Encodable objects, usually ASN1Objects- Returns:
- ASN1EncodableVector containing all elements from the list. Empty list if the input list was null.
-
parseTaggedElements
public static final Map<Integer, org.bouncycastle.asn1.ASN1TaggedObject> parseTaggedElements(org.bouncycastle.asn1.ASN1Sequence seq) Pull out all ASN1TaggedObjects from the sequence and put them into a map where the keys are tag numbers of ASN1TaggedObjects and the values are the ASN1Object they reference.- Parameters:
seq- ASN1Sequence of any size- Returns:
- Map of the tagged objects indexed by tag number. Empty map if no tagged objects are found.
-
listUntaggedElements
public static final List<org.bouncycastle.asn1.ASN1Object> listUntaggedElements(org.bouncycastle.asn1.ASN1Sequence seq) List all untagged elements from the sequence in order. Null elements are not included in the list.- Parameters:
seq- ASN1Sequence- Returns:
- List of ASN1Objects that are not ASN1TaggedObjects in order
-
safeGetDefaultElementFromSequence
public static final <T> T safeGetDefaultElementFromSequence(org.bouncycastle.asn1.ASN1Sequence seq, int zeroBasedIndex, T defaultAnswer, @NonNull @NonNull Function<Object, T> decoder) Attempt to transform the ASN1Object at the requested position within the ASN1Sequence into T using the method supplied.- Type Parameters:
T- The return type- Parameters:
seq- If null, the default answer will be returnedzeroBasedIndex- If this is out of bounds of the seq, the default answer will be returneddefaultAnswer- Can be null if the default answer is nulldecoder- The method to transform the object found at the index within the sequence- Returns:
- The transformed object
-
safeGetDefaultElement
public static final <T> T safeGetDefaultElement(org.bouncycastle.asn1.ASN1Object obj, T defaultAnswer, @NonNull @NonNull Function<Object, T> decoder) Attempt to transform the ASN1Object into T using the method supplied.- Type Parameters:
T- The return type- Parameters:
obj- ASN1ObjectdefaultAnswer- Can be null if the default answer is nulldecoder- The method to transform the object found at the index within the sequence- Returns:
- The transformed object
-
safeGetFirstInstanceFromSequenceGivenRange
public static final <T> T safeGetFirstInstanceFromSequenceGivenRange(org.bouncycastle.asn1.ASN1Sequence seq, int zeroBasedStartIndex, int zeroBasedMaxIndex, T defaultAnswer, @NonNull @NonNull Function<Object, T> decoder) Look for the first element, within the supplied range of the ASN1Sequence, that can be transformed into T using the method supplied. If no qualified elements are found, the default answer will be returned.- Type Parameters:
T- The return type- Parameters:
seq- If null, the default answer will be returnedzeroBasedStartIndex- If this is out of bounds of the seq, the default answer will be returnedzeroBasedMaxIndex- If this is out of bounds of the seq, the default answer will be returneddefaultAnswer- Can be null if the default answer is nulldecoder- The method to transform the object found at the index within the sequence- Returns:
- The transformed object
-
resizeOctets
public static org.bouncycastle.asn1.ASN1OctetString resizeOctets(int sequenceSize, org.bouncycastle.asn1.ASN1OctetString value) Resize the given ASN1OctetString to the given sequenceSize. If the value already meets the requested sequenceSize, or if the value is null, the value will pass through. If sequenceSize is smaller than value size, octets will be truncated from MSB. If sequenceSize is larger than value size, zero bytes will be prepended.- Parameters:
sequenceSize- New sizevalue- object to resize- Returns:
- ASN1OctetString of requested size.
-
resizeOctets
public static byte[] resizeOctets(int sequenceSize, byte[] value) Resize the given byte array to the given sequenceSize. If the value already meets the requested sequenceSize, or if the value is null, the value will pass through. If sequenceSize is smaller than value size, octets will be truncated from MSB. If sequenceSize is larger than value size, zero bytes will be prepended.- Parameters:
sequenceSize- New sizevalue- object to resize- Returns:
- byte array of the requested size.
-
resizeOctets
public static org.bouncycastle.asn1.ASN1OctetString resizeOctets(int sequenceSize, @NonNull @NonNull String valueHex) Convert the given hex string into a byte array, then resize it to the given sequenceSize. If the value already meets the requested sequenceSize, the value will pass through. If sequenceSize is smaller than value size, octets will be truncated from MSB. If sequenceSize is larger than value size, zero bytes will be prepended.- Parameters:
sequenceSize- New sizevalueHex- hex data to read- Returns:
- ASN1OctetString of requested size.
-
getSequence
Get an ASN1Sequence from the given object.- Parameters:
o- Object to convert- Returns:
- ASN1Sequence instance
- Throws:
IllegalArgumentException- if the object cannot be converted
-
isSequence
Test if the object is an ASN1Sequence- Parameters:
o- Object to convert- Returns:
- True if the object is an ASN1Sequence. Otherwise, false.
-
getStringOrDefault
-
getUTF8String
Get a string from the given object.- Parameters:
o- Object to convert- Returns:
- instance
- Throws:
IllegalArgumentException- if the object cannot be converted
-
getIA5String
Get a string from the given object.- Parameters:
o- Object to convert- Returns:
- instance
- Throws:
IllegalArgumentException- if the object cannot be converted
-
getPrintableString
Get a string from the given object.- Parameters:
o- Object to convert- Returns:
- instance
- Throws:
IllegalArgumentException- if the object cannot be converted
-
getGeneralizedTime
Get a date object from the given object.- Parameters:
o- Object to convert- Returns:
- instance
- Throws:
IllegalArgumentException- if the object cannot be converted
-
getOID
Get an OID from the given object.- Parameters:
o- Object to convert- Returns:
- instance
- Throws:
IllegalArgumentException- if the object cannot be converted
-
getBoolean
public static org.bouncycastle.asn1.ASN1Boolean getBoolean(boolean b) Get an ASN1Boolean from the primitive. Complements getBoolean(Object).- Parameters:
b- primitive boolean- Returns:
- instance
-
getBoolean
Get an ASN1Boolean from the given object.- Parameters:
o- Object to convert- Returns:
- instance
- Throws:
IllegalArgumentException- if the object cannot be converted
-
getBitString
Get an ASN1BitString from the given object.- Parameters:
o- Object to convert- Returns:
- instance
- Throws:
IllegalArgumentException- if the object cannot be converted
-
getEnumerated
Get an ASN1Enumerated from the given object.- Parameters:
o- Object to convert- Returns:
- instance
- Throws:
IllegalArgumentException- if the object cannot be converted
-
getInteger
Get an ASN1Integer from the given object.- Parameters:
o- Object to convert- Returns:
- instance
- Throws:
IllegalArgumentException- if the object cannot be converted
-
getOctetString
Get an octet string from the given object.- Parameters:
o- Object to convert- Returns:
- instance
- Throws:
IllegalArgumentException- if the object cannot be converted
-