Class ASN1Utils

java.lang.Object
paccor.tcg.credential.ASN1Utils

public class ASN1Utils extends Object
Common methods to transform ASN1 objects.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.bouncycastle.asn1.ASN1BitString
    Get an ASN1BitString from the given object.
    static org.bouncycastle.asn1.ASN1Boolean
    getBoolean(boolean b)
    Get an ASN1Boolean from the primitive.
    static org.bouncycastle.asn1.ASN1Boolean
    Get an ASN1Boolean from the given object.
    static org.bouncycastle.asn1.ASN1Enumerated
    Get an ASN1Enumerated from the given object.
    static org.bouncycastle.asn1.ASN1GeneralizedTime
    Get a date object from the given object.
    static org.bouncycastle.asn1.ASN1IA5String
    Get a string from the given object.
    static org.bouncycastle.asn1.ASN1Integer
    Get an ASN1Integer from the given object.
    static org.bouncycastle.asn1.ASN1OctetString
    Get an octet string from the given object.
    static org.bouncycastle.asn1.ASN1ObjectIdentifier
    Get an OID from the given object.
    static org.bouncycastle.asn1.ASN1PrintableString
    Get a string from the given object.
    static org.bouncycastle.asn1.ASN1Sequence
    Get an ASN1Sequence from the given object.
    static final String
    getStringOrDefault(org.bouncycastle.asn1.ASN1String str, String def)
    Get a string or default if null.
    static org.bouncycastle.asn1.ASN1UTF8String
    Get a string from the given object.
    static boolean
    Test if the object is an ASN1Sequence
    static final List<org.bouncycastle.asn1.ASN1Object>
    listUntaggedElements(org.bouncycastle.asn1.ASN1Sequence seq)
    List all untagged elements from the sequence in order.
    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.
    static byte[]
    resizeOctets(int sequenceSize, byte[] value)
    Resize the given byte array to the given sequenceSize.
    static org.bouncycastle.asn1.ASN1OctetString
    resizeOctets(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.ASN1OctetString
    resizeOctets(int sequenceSize, org.bouncycastle.asn1.ASN1OctetString value)
    Resize the given ASN1OctetString to the given sequenceSize.
    static final <T> T
    safeGetDefaultElement(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> T
    safeGetDefaultElementFromSequence(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> T
    safeGetFirstInstanceFromSequenceGivenRange(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.ASN1EncodableVector
    Given a list of ASN1Objects, put all elements into an ASN1 vector.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 ASN1Encodable
      U - 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 returned
      zeroBasedIndex - If this is out of bounds of the seq, the default answer will be returned
      defaultAnswer - Can be null if the default answer is null
      decoder - 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 - ASN1Object
      defaultAnswer - Can be null if the default answer is null
      decoder - 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 returned
      zeroBasedStartIndex - If this is out of bounds of the seq, the default answer will be returned
      zeroBasedMaxIndex - If this is out of bounds of the seq, the default answer will be returned
      defaultAnswer - Can be null if the default answer is null
      decoder - 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 size
      value - 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 size
      value - 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 size
      valueHex - hex data to read
      Returns:
      ASN1OctetString of requested size.
    • getSequence

      public static org.bouncycastle.asn1.ASN1Sequence getSequence(Object o)
      Get an ASN1Sequence from the given object.
      Parameters:
      o - Object to convert
      Returns:
      ASN1Sequence instance
      Throws:
      IllegalArgumentException - if the object cannot be converted
    • isSequence

      public static boolean isSequence(Object o)
      Test if the object is an ASN1Sequence
      Parameters:
      o - Object to convert
      Returns:
      True if the object is an ASN1Sequence. Otherwise, false.
    • getStringOrDefault

      public static final String getStringOrDefault(org.bouncycastle.asn1.ASN1String str, String def)
      Get a string or default if null.
      Parameters:
      str - ASN1String to check
      def - Default string
      Returns:
      String value or default
    • getUTF8String

      public static org.bouncycastle.asn1.ASN1UTF8String getUTF8String(Object o)
      Get a string from the given object.
      Parameters:
      o - Object to convert
      Returns:
      instance
      Throws:
      IllegalArgumentException - if the object cannot be converted
    • getIA5String

      public static org.bouncycastle.asn1.ASN1IA5String getIA5String(Object o)
      Get a string from the given object.
      Parameters:
      o - Object to convert
      Returns:
      instance
      Throws:
      IllegalArgumentException - if the object cannot be converted
    • getPrintableString

      public static org.bouncycastle.asn1.ASN1PrintableString getPrintableString(Object o)
      Get a string from the given object.
      Parameters:
      o - Object to convert
      Returns:
      instance
      Throws:
      IllegalArgumentException - if the object cannot be converted
    • getGeneralizedTime

      public static org.bouncycastle.asn1.ASN1GeneralizedTime getGeneralizedTime(Object o)
      Get a date object from the given object.
      Parameters:
      o - Object to convert
      Returns:
      instance
      Throws:
      IllegalArgumentException - if the object cannot be converted
    • getOID

      public static org.bouncycastle.asn1.ASN1ObjectIdentifier getOID(Object o)
      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

      public static org.bouncycastle.asn1.ASN1Boolean getBoolean(Object o)
      Get an ASN1Boolean from the given object.
      Parameters:
      o - Object to convert
      Returns:
      instance
      Throws:
      IllegalArgumentException - if the object cannot be converted
    • getBitString

      public static org.bouncycastle.asn1.ASN1BitString getBitString(Object o)
      Get an ASN1BitString from the given object.
      Parameters:
      o - Object to convert
      Returns:
      instance
      Throws:
      IllegalArgumentException - if the object cannot be converted
    • getEnumerated

      public static org.bouncycastle.asn1.ASN1Enumerated getEnumerated(Object o)
      Get an ASN1Enumerated from the given object.
      Parameters:
      o - Object to convert
      Returns:
      instance
      Throws:
      IllegalArgumentException - if the object cannot be converted
    • getInteger

      public static org.bouncycastle.asn1.ASN1Integer getInteger(Object o)
      Get an ASN1Integer from the given object.
      Parameters:
      o - Object to convert
      Returns:
      instance
      Throws:
      IllegalArgumentException - if the object cannot be converted
    • getOctetString

      public static org.bouncycastle.asn1.ASN1OctetString getOctetString(Object o)
      Get an octet string from the given object.
      Parameters:
      o - Object to convert
      Returns:
      instance
      Throws:
      IllegalArgumentException - if the object cannot be converted