| | |
| | | */ |
| | | public ASN1Element(byte type) |
| | | { |
| | | |
| | | this.type = type; |
| | | this.value = NO_VALUE; |
| | | } |
| | |
| | | */ |
| | | public ASN1Element(byte type, byte[] value) |
| | | { |
| | | |
| | | this.type = type; |
| | | |
| | | if (value == null) |
| | |
| | | */ |
| | | public byte getType() |
| | | { |
| | | |
| | | return type; |
| | | } |
| | | |
| | |
| | | */ |
| | | public void setType(byte type) |
| | | { |
| | | |
| | | this.type = type; |
| | | } |
| | | |
| | |
| | | */ |
| | | public boolean isUniversal() |
| | | { |
| | | |
| | | return ((type & TYPE_MASK_ALL_BUT_CLASS) == TYPE_MASK_UNIVERSAL); |
| | | } |
| | | |
| | |
| | | */ |
| | | public boolean isApplicationSpecific() |
| | | { |
| | | |
| | | return ((type & TYPE_MASK_ALL_BUT_CLASS) == TYPE_MASK_APPLICATION); |
| | | } |
| | | |
| | |
| | | */ |
| | | public boolean isContextSpecific() |
| | | { |
| | | |
| | | return ((type & TYPE_MASK_ALL_BUT_CLASS) == TYPE_MASK_CONTEXT); |
| | | } |
| | | |
| | |
| | | */ |
| | | public boolean isPrivate() |
| | | { |
| | | |
| | | return ((type & TYPE_MASK_ALL_BUT_CLASS) == TYPE_MASK_PRIVATE); |
| | | } |
| | | |
| | |
| | | */ |
| | | public boolean isPrimitive() |
| | | { |
| | | |
| | | return ((type & TYPE_MASK_ALL_BUT_PC) == TYPE_MASK_PRIMITIVE); |
| | | } |
| | | |
| | |
| | | */ |
| | | public boolean isConstructed() |
| | | { |
| | | |
| | | return ((type & TYPE_MASK_ALL_BUT_PC) == TYPE_MASK_CONSTRUCTED); |
| | | } |
| | | |
| | |
| | | */ |
| | | public byte[] value() |
| | | { |
| | | |
| | | return value; |
| | | } |
| | | |
| | |
| | | public void setValue(byte[] value) |
| | | throws ASN1Exception |
| | | { |
| | | |
| | | if (value == null) |
| | | { |
| | | this.value = NO_VALUE; |
| | |
| | | */ |
| | | protected final void setValueInternal(byte[] value) |
| | | { |
| | | |
| | | this.value = value; |
| | | } |
| | | |
| | |
| | | */ |
| | | public static byte[] encodeLength(int length) |
| | | { |
| | | |
| | | if (length < 128) |
| | | { |
| | | return new byte[] { (byte) length }; |
| | |
| | | */ |
| | | public byte[] encode() |
| | | { |
| | | |
| | | if (value.length == 0) |
| | | { |
| | | return new byte[] { type, 0x00 }; |
| | |
| | | */ |
| | | public static byte[] encodeValue(boolean booleanValue) |
| | | { |
| | | |
| | | return (booleanValue ? BOOLEAN_VALUE_TRUE : BOOLEAN_VALUE_FALSE); |
| | | } |
| | | |
| | |
| | | */ |
| | | public static byte[] encodeValue(int intValue) |
| | | { |
| | | |
| | | if ((intValue & 0x0000007F) == intValue) |
| | | { |
| | | return new byte[] |
| | |
| | | */ |
| | | public static byte[] encodeLongValue(long longValue) |
| | | { |
| | | |
| | | if ((longValue & 0x000000000000007FL) == longValue) |
| | | { |
| | | return new byte[] |
| | |
| | | */ |
| | | public static byte[] encodeValue(ArrayList<ASN1Element> elements) |
| | | { |
| | | |
| | | if (elements == null) |
| | | { |
| | | return NO_VALUE; |
| | |
| | | public static ASN1Element decode(byte[] encodedElement) |
| | | throws ASN1Exception |
| | | { |
| | | |
| | | |
| | | // First make sure that the array is not null and long enough to contain |
| | | // a valid ASN.1 element. |
| | | if (encodedElement == null) |
| | |
| | | int length) |
| | | throws ASN1Exception |
| | | { |
| | | |
| | | |
| | | // First make sure that the array is not null and long enough to contain |
| | | // a valid ASN.1 element. |
| | | if (encodedElement == null) |
| | |
| | | public ASN1Boolean decodeAsBoolean() |
| | | throws ASN1Exception |
| | | { |
| | | |
| | | return ASN1Boolean.decodeAsBoolean(this); |
| | | } |
| | | |
| | |
| | | public ASN1Enumerated decodeAsEnumerated() |
| | | throws ASN1Exception |
| | | { |
| | | |
| | | return ASN1Enumerated.decodeAsEnumerated(this); |
| | | } |
| | | |
| | |
| | | public ASN1Integer decodeAsInteger() |
| | | throws ASN1Exception |
| | | { |
| | | |
| | | return ASN1Integer.decodeAsInteger(this); |
| | | } |
| | | |
| | |
| | | public ASN1Long decodeAsLong() |
| | | throws ASN1Exception |
| | | { |
| | | |
| | | return ASN1Long.decodeAsLong(this); |
| | | } |
| | | |
| | |
| | | public ASN1Null decodeAsNull() |
| | | throws ASN1Exception |
| | | { |
| | | |
| | | return ASN1Null.decodeAsNull(this); |
| | | } |
| | | |
| | |
| | | public ASN1OctetString decodeAsOctetString() |
| | | throws ASN1Exception |
| | | { |
| | | |
| | | return ASN1OctetString.decodeAsOctetString(this); |
| | | } |
| | | |
| | |
| | | public ASN1Sequence decodeAsSequence() |
| | | throws ASN1Exception |
| | | { |
| | | |
| | | return ASN1Sequence.decodeAsSequence(this); |
| | | } |
| | | |
| | |
| | | public ASN1Set decodeAsSet() |
| | | throws ASN1Exception |
| | | { |
| | | |
| | | return ASN1Set.decodeAsSet(this); |
| | | } |
| | | |
| | |
| | | public static ArrayList<ASN1Element> decodeElements(byte[] encodedElements) |
| | | throws ASN1Exception |
| | | { |
| | | |
| | | |
| | | // Make sure that the element array is not null. |
| | | if (encodedElements == null) |
| | | { |
| | |
| | | */ |
| | | public String getProtocolElementName() |
| | | { |
| | | |
| | | return "ASN.1"; |
| | | } |
| | | |
| | |
| | | */ |
| | | public boolean equals(Object o) |
| | | { |
| | | |
| | | if (this == o) |
| | | { |
| | | return true; |
| | |
| | | */ |
| | | public boolean equalsIgnoreType(ASN1Element element) |
| | | { |
| | | |
| | | return Arrays.equals(value, element.value); |
| | | } |
| | | |
| | |
| | | */ |
| | | public boolean equalsIgnoreType(ByteString byteString) |
| | | { |
| | | |
| | | return Arrays.equals(value, byteString.value()); |
| | | } |
| | | |
| | |
| | | */ |
| | | public boolean equalsElement(ASN1Element e) |
| | | { |
| | | |
| | | if (this == e) |
| | | { |
| | | return true; |
| | |
| | | */ |
| | | public int hashCode() |
| | | { |
| | | |
| | | int hashCode = type; |
| | | int length = Math.min(20, value.length); |
| | | for (int i=0; i < length; i++) |
| | |
| | | */ |
| | | public String toString() |
| | | { |
| | | |
| | | StringBuilder buffer = new StringBuilder(); |
| | | toString(buffer); |
| | | return buffer.toString(); |
| | |
| | | */ |
| | | public void toString(StringBuilder buffer) |
| | | { |
| | | |
| | | buffer.append("ASN1Element(type="); |
| | | buffer.append(byteToHex(type)); |
| | | buffer.append(", length="); |
| | |
| | | */ |
| | | public void toString(StringBuilder buffer, int indent) |
| | | { |
| | | |
| | | StringBuilder indentBuf = new StringBuilder(indent); |
| | | for (int i=0 ; i < indent; i++) |
| | | { |