| | |
| | | * CDDL HEADER END |
| | | * |
| | | * |
| | | * Portions Copyright 2006 Sun Microsystems, Inc. |
| | | * Portions Copyright 2006-2007 Sun Microsystems, Inc. |
| | | */ |
| | | package org.opends.server.protocols.asn1; |
| | | |
| | |
| | | import org.opends.server.api.ProtocolElement; |
| | | import org.opends.server.types.ByteString; |
| | | |
| | | import static org.opends.server.loggers.Debug.*; |
| | | import static org.opends.server.messages.MessageHandler.*; |
| | | import static org.opends.server.messages.ProtocolMessages.*; |
| | | import static org.opends.server.protocols.asn1.ASN1Constants.*; |
| | |
| | | public class ASN1Element |
| | | implements ProtocolElement, Serializable |
| | | { |
| | | /** |
| | | * The fully-qualified name of this class for debugging purposes. |
| | | */ |
| | | private static final String CLASS_NAME = |
| | | "org.opends.server.protocols.asn1.ASN1Element"; |
| | | |
| | | |
| | | |
| | |
| | | */ |
| | | public ASN1Element(byte type) |
| | | { |
| | | assert debugConstructor(CLASS_NAME, byteToHex(type)); |
| | | |
| | | this.type = type; |
| | | this.value = NO_VALUE; |
| | |
| | | */ |
| | | public ASN1Element(byte type, byte[] value) |
| | | { |
| | | assert debugConstructor(CLASS_NAME, byteToHex(type), bytesToHex(value)); |
| | | |
| | | this.type = type; |
| | | |
| | |
| | | */ |
| | | public byte getType() |
| | | { |
| | | assert debugEnter(CLASS_NAME, "getType"); |
| | | |
| | | return type; |
| | | } |
| | |
| | | */ |
| | | public void setType(byte type) |
| | | { |
| | | assert debugEnter(CLASS_NAME, "setType", byteToHex(type)); |
| | | |
| | | this.type = type; |
| | | } |
| | |
| | | */ |
| | | public boolean isUniversal() |
| | | { |
| | | assert debugEnter(CLASS_NAME, "isUniversal"); |
| | | |
| | | return ((type & TYPE_MASK_ALL_BUT_CLASS) == TYPE_MASK_UNIVERSAL); |
| | | } |
| | |
| | | */ |
| | | public boolean isApplicationSpecific() |
| | | { |
| | | assert debugEnter(CLASS_NAME, "isApplicationSpecific"); |
| | | |
| | | return ((type & TYPE_MASK_ALL_BUT_CLASS) == TYPE_MASK_APPLICATION); |
| | | } |
| | |
| | | */ |
| | | public boolean isContextSpecific() |
| | | { |
| | | assert debugEnter(CLASS_NAME, "isContextSpecific"); |
| | | |
| | | return ((type & TYPE_MASK_ALL_BUT_CLASS) == TYPE_MASK_CONTEXT); |
| | | } |
| | |
| | | */ |
| | | public boolean isPrivate() |
| | | { |
| | | assert debugEnter(CLASS_NAME, "isPrivate"); |
| | | |
| | | return ((type & TYPE_MASK_ALL_BUT_CLASS) == TYPE_MASK_PRIVATE); |
| | | } |
| | |
| | | */ |
| | | public boolean isPrimitive() |
| | | { |
| | | assert debugEnter(CLASS_NAME, "isPrimitive"); |
| | | |
| | | return ((type & TYPE_MASK_ALL_BUT_PC) == TYPE_MASK_PRIMITIVE); |
| | | } |
| | |
| | | */ |
| | | public boolean isConstructed() |
| | | { |
| | | assert debugEnter(CLASS_NAME, "isConstructed"); |
| | | |
| | | return ((type & TYPE_MASK_ALL_BUT_PC) == TYPE_MASK_CONSTRUCTED); |
| | | } |
| | |
| | | */ |
| | | public byte[] value() |
| | | { |
| | | assert debugEnter(CLASS_NAME, "value"); |
| | | |
| | | return value; |
| | | } |
| | |
| | | public void setValue(byte[] value) |
| | | throws ASN1Exception |
| | | { |
| | | assert debugEnter(CLASS_NAME, "setValue", bytesToHex(value)); |
| | | |
| | | if (value == null) |
| | | { |
| | |
| | | */ |
| | | protected final void setValueInternal(byte[] value) |
| | | { |
| | | assert debugEnter(CLASS_NAME, "setValueInternal", bytesToHex(value)); |
| | | |
| | | this.value = value; |
| | | } |
| | |
| | | */ |
| | | public static byte[] encodeLength(int length) |
| | | { |
| | | assert debugEnter(CLASS_NAME, "encodeLength", String.valueOf(length)); |
| | | |
| | | if (length < 128) |
| | | { |
| | |
| | | */ |
| | | public byte[] encode() |
| | | { |
| | | assert debugEnter(CLASS_NAME, "encode"); |
| | | |
| | | if (value.length == 0) |
| | | { |
| | |
| | | */ |
| | | public static byte[] encodeValue(boolean booleanValue) |
| | | { |
| | | assert debugEnter(CLASS_NAME, "encodeValue", String.valueOf(booleanValue)); |
| | | |
| | | return (booleanValue ? BOOLEAN_VALUE_TRUE : BOOLEAN_VALUE_FALSE); |
| | | } |
| | |
| | | */ |
| | | public static byte[] encodeValue(int intValue) |
| | | { |
| | | assert debugEnter(CLASS_NAME, "encodeValue", String.valueOf(intValue)); |
| | | |
| | | if ((intValue & 0x0000007F) == intValue) |
| | | { |
| | |
| | | */ |
| | | public static byte[] encodeLongValue(long longValue) |
| | | { |
| | | assert debugEnter(CLASS_NAME, "encodeLongValue", String.valueOf(longValue)); |
| | | |
| | | if ((longValue & 0x000000000000007FL) == longValue) |
| | | { |
| | |
| | | */ |
| | | public static byte[] encodeValue(ArrayList<ASN1Element> elements) |
| | | { |
| | | assert debugEnter(CLASS_NAME, "encodeValue", String.valueOf(elements)); |
| | | |
| | | if (elements == null) |
| | | { |
| | |
| | | public static ASN1Element decode(byte[] encodedElement) |
| | | throws ASN1Exception |
| | | { |
| | | assert debugEnter(CLASS_NAME, "decode", bytesToHex(encodedElement)); |
| | | |
| | | |
| | | // First make sure that the array is not null and long enough to contain |
| | |
| | | int length) |
| | | throws ASN1Exception |
| | | { |
| | | assert debugEnter(CLASS_NAME, "decode", bytesToHex(encodedElement), |
| | | String.valueOf(startPos), String.valueOf(length)); |
| | | |
| | | |
| | | // First make sure that the array is not null and long enough to contain |
| | |
| | | public ASN1Boolean decodeAsBoolean() |
| | | throws ASN1Exception |
| | | { |
| | | assert debugEnter(CLASS_NAME, "decodeAsBoolean"); |
| | | |
| | | return ASN1Boolean.decodeAsBoolean(this); |
| | | } |
| | |
| | | public ASN1Enumerated decodeAsEnumerated() |
| | | throws ASN1Exception |
| | | { |
| | | assert debugEnter(CLASS_NAME, "decodeAsEnumerated"); |
| | | |
| | | return ASN1Enumerated.decodeAsEnumerated(this); |
| | | } |
| | |
| | | public ASN1Integer decodeAsInteger() |
| | | throws ASN1Exception |
| | | { |
| | | assert debugEnter(CLASS_NAME, "decodeAsInteger"); |
| | | |
| | | return ASN1Integer.decodeAsInteger(this); |
| | | } |
| | |
| | | public ASN1Long decodeAsLong() |
| | | throws ASN1Exception |
| | | { |
| | | assert debugEnter(CLASS_NAME, "decodeAsLong"); |
| | | |
| | | return ASN1Long.decodeAsLong(this); |
| | | } |
| | |
| | | public ASN1Null decodeAsNull() |
| | | throws ASN1Exception |
| | | { |
| | | assert debugEnter(CLASS_NAME, "decodeAsNull"); |
| | | |
| | | return ASN1Null.decodeAsNull(this); |
| | | } |
| | |
| | | public ASN1OctetString decodeAsOctetString() |
| | | throws ASN1Exception |
| | | { |
| | | assert debugEnter(CLASS_NAME, "decodeAsOctetString"); |
| | | |
| | | return ASN1OctetString.decodeAsOctetString(this); |
| | | } |
| | |
| | | public ASN1Sequence decodeAsSequence() |
| | | throws ASN1Exception |
| | | { |
| | | assert debugEnter(CLASS_NAME, "decodeAsSequence"); |
| | | |
| | | return ASN1Sequence.decodeAsSequence(this); |
| | | } |
| | |
| | | public ASN1Set decodeAsSet() |
| | | throws ASN1Exception |
| | | { |
| | | assert debugEnter(CLASS_NAME, "decodeAsSet"); |
| | | |
| | | return ASN1Set.decodeAsSet(this); |
| | | } |
| | |
| | | public static ArrayList<ASN1Element> decodeElements(byte[] encodedElements) |
| | | throws ASN1Exception |
| | | { |
| | | assert debugEnter(CLASS_NAME, "decodeElements", |
| | | bytesToHex(encodedElements)); |
| | | |
| | | |
| | | // Make sure that the element array is not null. |
| | |
| | | */ |
| | | public String getProtocolElementName() |
| | | { |
| | | assert debugEnter(CLASS_NAME, "getProtocolElementName"); |
| | | |
| | | return "ASN.1"; |
| | | } |
| | |
| | | */ |
| | | public boolean equals(Object o) |
| | | { |
| | | assert debugEnter(CLASS_NAME, "equals", String.valueOf(o)); |
| | | |
| | | if (this == o) |
| | | { |
| | |
| | | */ |
| | | public boolean equalsIgnoreType(ASN1Element element) |
| | | { |
| | | assert debugEnter(CLASS_NAME, "equalsIgnoreType", |
| | | String.valueOf(element)); |
| | | |
| | | return Arrays.equals(value, element.value); |
| | | } |
| | |
| | | */ |
| | | public boolean equalsIgnoreType(ByteString byteString) |
| | | { |
| | | assert debugEnter(CLASS_NAME, "equalsIgnoreType", |
| | | String.valueOf(byteString)); |
| | | |
| | | return Arrays.equals(value, byteString.value()); |
| | | } |
| | |
| | | */ |
| | | public boolean equalsElement(ASN1Element e) |
| | | { |
| | | assert debugEnter(CLASS_NAME, "equalsElement", String.valueOf(e)); |
| | | |
| | | if (this == e) |
| | | { |
| | |
| | | */ |
| | | public int hashCode() |
| | | { |
| | | assert debugEnter(CLASS_NAME, "hashCode"); |
| | | |
| | | int hashCode = type; |
| | | int length = Math.min(20, value.length); |
| | |
| | | */ |
| | | public String toString() |
| | | { |
| | | assert debugEnter(CLASS_NAME, "toString"); |
| | | |
| | | StringBuilder buffer = new StringBuilder(); |
| | | toString(buffer); |
| | |
| | | */ |
| | | public void toString(StringBuilder buffer) |
| | | { |
| | | assert debugEnter(CLASS_NAME, "toString", "java.lang.StringBuilder"); |
| | | |
| | | buffer.append("ASN1Element(type="); |
| | | buffer.append(byteToHex(type)); |
| | |
| | | */ |
| | | public void toString(StringBuilder buffer, int indent) |
| | | { |
| | | assert debugEnter(CLASS_NAME, "toString", "java.lang.StringBuilder", |
| | | String.valueOf(indent)); |
| | | |
| | | StringBuilder indentBuf = new StringBuilder(indent); |
| | | for (int i=0 ; i < indent; i++) |