aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/asn1/ASN1Object.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/model/asn1/ASN1Object.java')
-rw-r--r--src/main/model/asn1/ASN1Object.java73
1 files changed, 40 insertions, 33 deletions
diff --git a/src/main/model/asn1/ASN1Object.java b/src/main/model/asn1/ASN1Object.java
index 9b4a98c..434a6c5 100644
--- a/src/main/model/asn1/ASN1Object.java
+++ b/src/main/model/asn1/ASN1Object.java
@@ -2,14 +2,10 @@ package model.asn1;
import model.asn1.exceptions.ParseException;
import model.asn1.parsing.BytesReader;
-import ui.Utils;
-import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
/**
* Represents an encode-able ASN.1 object. It can be a SEQUENCE, an INTEGER, an OID, or any other ASN.1 type.
@@ -50,9 +46,9 @@ public class ASN1Object implements Encodable {
* of the corresponding types. However, applications may use context-specific
* or private tags for corresponding fields, either implicitly encoded or explicitly encoded.
* REQUIRES: Three cases:
- * 1. No context-specific tag: parentTag must be null.
- * 2. Implicit encoding: parentTag must be null, and the tag must be CONTEXT_SPECIFIC.
- * 3. Explicit encoding: parentTag must be constructive and CONTEXT_SPECIFIC.
+ * 1. No context-specific tag: parentTag must be null.
+ * 2. Implicit encoding: parentTag must be null, and the tag must be CONTEXT_SPECIFIC.
+ * 3. Explicit encoding: parentTag must be constructive and CONTEXT_SPECIFIC.
*/
public ASN1Object(Tag tag, Tag parentTag) {
this.tag = tag;
@@ -66,13 +62,13 @@ public class ASN1Object implements Encodable {
* and value (length = 0 if no value in DER, but never null). It will fill the value and length but will not mark
* the value as read (only the tag will be marked). Subtypes are responsible for deserializing the values. This
* method is not appropriate for parsing an unknown input (use subtypes instead) since values will be left unread.
- * Throws {@link ParseException} if input is invalid:
- * The input data must have a valid
- * parentTag (optional) - parentLength (optional) - tag - length - value (optional).
- * The value must match the corresponding type (e.g., an INTEGER value cannot go to an OctetString type).
- * The value must be supported by the corresponding type (e.g., a Printable must only contain valid chars).
- * If parentTag presents, its class must be CONTEXT_SPECIFIC, and it must be constructive.
- * If parentLength presents, it must not be 0.
+ * Throws {@link ParseException} if input is invalid:
+ * The input data must have a valid
+ * parentTag (optional) - parentLength (optional) - tag - length - value (optional).
+ * The value must match the corresponding type (e.g., an INTEGER value cannot go to an OctetString type).
+ * The value must be supported by the corresponding type (e.g., a Printable must only contain valid chars).
+ * If parentTag presents, its class must be CONTEXT_SPECIFIC, and it must be constructive.
+ * If parentLength presents, it must not be 0.
* MODIFIES: this, encoded (bytes are read)
* REQUIRES: If hasParentTag is true, parentTag and parentLength must present. Otherwise, they must be null. Assumes
* that the length won't be lower than actual. Assumes parentLength = length(tag + length + value).
@@ -108,23 +104,34 @@ public class ASN1Object implements Encodable {
* if unrecognized or application-defined (SEQUENCE or SET). It will always mark anything to be read, including
* unrecognized type values. This method is appropriate to decode an unknown input stream into known or unknown
* types. All values will be read.
- * Throws {@link ParseException} if the input is invalid.
+ * Throws {@link ParseException} if the input is invalid.
* MODIFIES: encoded
*/
public static ASN1Object parse(BytesReader encoded, boolean hasParentTag) throws ParseException {
final Tag t = encoded.getTag(hasParentTag);
switch (t.getNumber()) {
- case 0x1: return new Bool(encoded, hasParentTag);
- case 0x2: return new Int(encoded, hasParentTag);
- case 0x3: return new BitString(encoded, hasParentTag);
- case 0x4: return new OctetString(encoded, hasParentTag);
- case 0x5: return new Null(encoded, hasParentTag);
- case 0x6: return new ObjectIdentifier(encoded, hasParentTag);
- case 0xC: return new UTF8String(encoded, hasParentTag);
- case 0x13: return new PrintableString(encoded, hasParentTag);
- case 0x16: return new IA5String(encoded, hasParentTag);
- case 0x17: return new UtcTime(encoded, hasParentTag);
- case 0x18: return new GeneralizedTime(encoded, hasParentTag);
+ case 0x1:
+ return new Bool(encoded, hasParentTag);
+ case 0x2:
+ return new Int(encoded, hasParentTag);
+ case 0x3:
+ return new BitString(encoded, hasParentTag);
+ case 0x4:
+ return new OctetString(encoded, hasParentTag);
+ case 0x5:
+ return new Null(encoded, hasParentTag);
+ case 0x6:
+ return new ObjectIdentifier(encoded, hasParentTag);
+ case 0xC:
+ return new UTF8String(encoded, hasParentTag);
+ case 0x13:
+ return new PrintableString(encoded, hasParentTag);
+ case 0x16:
+ return new IA5String(encoded, hasParentTag);
+ case 0x17:
+ return new UtcTime(encoded, hasParentTag);
+ case 0x18:
+ return new GeneralizedTime(encoded, hasParentTag);
default: {
ASN1Object object = new ASN1Object(encoded, hasParentTag);
// Mark as read unconditionally because there aren't any type handlers that read them.
@@ -139,16 +146,16 @@ public class ASN1Object implements Encodable {
* The encoding will result in:
* (Parent Tag)(Tag)(Length)(Value)
* Parent Tag - Only exists if the field has a context-specific parent tag number and use explicit tagging. In this
- * case, the parent tag is the tag supplied in the constructor. If the field uses implicit tag
- * encoding or does not have a context-specific tag number, this field does not exist. This field,
- * as specified in the REQUIRES clause in the constructor, is always constructive.
+ * case, the parent tag is the tag supplied in the constructor. If the field uses implicit tag
+ * encoding or does not have a context-specific tag number, this field does not exist. This field,
+ * as specified in the REQUIRES clause in the constructor, is always constructive.
* Parent Length - The length of the following (tag, length, and value). A detailed length description, see follows.
* Tag - The tag value.
* Length - The length of the value, in number of bytes. If the length is <= 127, it will contain only a single
- * byte of length value, with the highest bit cleared. If the length is > 127, the first length byte
- * will have its highest bit set, with the remaining bits representing how many bytes are needed to
- * store the length integer. Followed are the integer, in multiple bytes, representing the length. The
- * multibyte integer are encoded in big-endian.
+ * byte of length value, with the highest bit cleared. If the length is > 127, the first length byte
+ * will have its highest bit set, with the remaining bits representing how many bytes are needed to
+ * store the length integer. Followed are the integer, in multiple bytes, representing the length. The
+ * multibyte integer are encoded in big-endian.
* Value - The value, with a total length (in bytes) corresponding to the Length field.
* REQUIRES: encodeValueDER() != null
*/