aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/x501
diff options
context:
space:
mode:
authorYuuta Liang <yuutaw@students.cs.ubc.ca>2023-10-12 12:10:33 +0800
committerYuuta Liang <yuutaw@students.cs.ubc.ca>2023-10-12 12:10:33 +0800
commitd342a45d98c4795b3a3fe1aaef5236ad4a782b55 (patch)
treef4ebc0ad962b138d9371413fcc71c97a559df506 /src/main/model/x501
parente60c9c76243cfe0a408af98dc60bedb973e815db (diff)
downloadjca-d342a45d98c4795b3a3fe1aaef5236ad4a782b55.tar
jca-d342a45d98c4795b3a3fe1aaef5236ad4a782b55.tar.gz
jca-d342a45d98c4795b3a3fe1aaef5236ad4a782b55.tar.bz2
jca-d342a45d98c4795b3a3fe1aaef5236ad4a782b55.zip
Implement data structures from X.680, X.501, X.509, and PKCS#10, with X.690 encoding / decoding support
The implementation took four days, and it is still a little bit rough. Updated version should arrive soon. Signed-off-by: Yuuta Liang <yuutaw@students.cs.ubc.ca>
Diffstat (limited to 'src/main/model/x501')
-rw-r--r--src/main/model/x501/AttributeTypeAndValue.java90
-rw-r--r--src/main/model/x501/Name.java79
-rw-r--r--src/main/model/x501/RelativeDistinguishedName.java78
3 files changed, 247 insertions, 0 deletions
diff --git a/src/main/model/x501/AttributeTypeAndValue.java b/src/main/model/x501/AttributeTypeAndValue.java
new file mode 100644
index 0000000..d43d137
--- /dev/null
+++ b/src/main/model/x501/AttributeTypeAndValue.java
@@ -0,0 +1,90 @@
+package model.x501;
+
+import model.asn1.ASN1Object;
+import model.asn1.ObjectIdentifier;
+import model.asn1.Tag;
+import model.asn1.exceptions.ParseException;
+import model.asn1.parsing.BytesReader;
+import model.csr.Values;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.stream.Stream;
+
+/**
+ * Implements the following:
+ * <pre>
+ * AttributeTypeAndValue ::= SEQUENCE {
+ * type ATTRIBUTE.&id({SupportedAttributes}),
+ * value ATTRIBUTE.&Type({SupportedAttributes}{@type}),
+ * ... }
+ * </pre>
+ */
+public class AttributeTypeAndValue extends ASN1Object {
+ /**
+ * The type of that attribute. For example, <pre>2.5.4.10</pre> is OU.
+ * It determines the format of the value.
+ */
+ private final ObjectIdentifier type;
+
+ /**
+ * Value corresponding to type.
+ */
+ private final ASN1Object value;
+
+ /**
+ * EFFECT: Init the object with tag, parentTag, type, and values. For tag and parentTag, see {@link ASN1Object}.
+ * REQUIRES: The values must match the type. Type tag should be UNIVERSAL OID.
+ */
+ public AttributeTypeAndValue(Tag tag, Tag parentTag,
+ ObjectIdentifier type, ASN1Object value) {
+ super(tag, parentTag);
+ this.type = type;
+ this.value = value;
+ }
+
+ /**
+ * EFFECTS: Parse input DER. Value is not checked against the type.
+ * Throws {@link ASN1Object} if invalid:
+ * - Any fields missing
+ * - Any fields having an incorrect tag (as seen in the ASN.1 definition)
+ * - Any fields with encoding instructions that violate implicit / explicit encoding rules
+ * - Other issues found during parsing the object, like early EOF (see {@link ASN1Object})
+ * MODIFIES: this, encoded
+ */
+ public AttributeTypeAndValue(BytesReader encoded, boolean hasParentTag) throws ParseException {
+ super(encoded, hasParentTag);
+ this.type = new ObjectIdentifier(encoded, false);
+ this.type.getTag().enforce(ObjectIdentifier.TAG);
+
+ this.value = ASN1Object.parse(encoded, false);
+ }
+
+ /**
+ * EFFECTS: Encode the fields into DER, in the order.
+ */
+ @Override
+ public Byte[] encodeValueDER() {
+ return Stream.of(Arrays.asList(type.encodeDER()),
+ Arrays.asList(value.encodeDER()))
+ .flatMap(Collection::stream)
+ .toArray(Byte[]::new);
+ }
+
+ /**
+ * EFFECTS: Return in TYPE=Value format. Type will be either x.x.x.x.x or human-readable strings like CN. Value is
+ * input-defined.
+ */
+ @Override
+ public String toString() {
+ return type.toString() + "=" + value.toString();
+ }
+
+ public ObjectIdentifier getType() {
+ return type;
+ }
+
+ public ASN1Object getValue() {
+ return value;
+ }
+}
diff --git a/src/main/model/x501/Name.java b/src/main/model/x501/Name.java
new file mode 100644
index 0000000..dd2acb6
--- /dev/null
+++ b/src/main/model/x501/Name.java
@@ -0,0 +1,79 @@
+package model.x501;
+
+import model.asn1.ASN1Object;
+import model.asn1.Encodable;
+import model.asn1.Tag;
+import model.asn1.exceptions.ParseException;
+import model.asn1.parsing.BytesReader;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ * Represents an X.501 directory Name (a.k.a. RDNSequence).
+ * <pre>
+ * Name ::= CHOICE { -- only one possibility for now -- rdnSequence RDNSequence }
+ * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
+ * DistinguishedName ::= RDNSequence
+ * </pre>
+ */
+public class Name extends ASN1Object {
+ private final RelativeDistinguishedName[] rdnSequence;
+
+ /**
+ * EFFECT: Initialize the Name with the given tags and rdnSequence. For tag and parentTag, consult
+ * {@link ASN1Object}.
+ * REQUIRES: Items should have SET tag.
+ */
+ public Name(Tag tag, Tag parentTag, RelativeDistinguishedName[] rdnSequence) {
+ super(tag, parentTag);
+ this.rdnSequence = rdnSequence;
+ }
+
+ /**
+ * EFFECT: Parse the Name from input DER bytes. For details on parsing, refer to {@link ASN1Object}.
+ * Throws {@link ParseException} for invalid input.
+ * MODIFIES: this, encoded
+ */
+ public Name(BytesReader encoded, boolean hasParentTag) throws ParseException {
+ super(encoded, hasParentTag);
+ final List<RelativeDistinguishedName> list = new ArrayList<>();
+ for (int i = 0; i < getLength();) {
+ int index = encoded.getIndex();
+ final RelativeDistinguishedName name = new RelativeDistinguishedName(encoded, false);
+ name.getTag().enforce(TAG_SET);
+ list.add(name);
+ index = encoded.getIndex() - index;
+ i += index;
+ }
+ this.rdnSequence = list.toArray(new RelativeDistinguishedName[0]);
+ }
+
+ /**
+ * EFFECTS: Encode the SEQUENCE OF into DER, keep order. RDNs will be encoded one-by-one.
+ */
+ @Override
+ public Byte[] encodeValueDER() {
+ return Stream.of(rdnSequence)
+ .map(Encodable::encodeDER)
+ .flatMap(Arrays::stream)
+ .toArray(Byte[]::new);
+ }
+
+ /**
+ * EFFECT: Convert the name into directory string, like CN=yuuta,OU=users,DC=yuuta,DC=moe
+ */
+ @Override
+ public String toString() {
+ return Stream.of(rdnSequence)
+ .map(RelativeDistinguishedName::toString)
+ .collect(Collectors.joining(","));
+ }
+
+ public RelativeDistinguishedName[] getRdnSequence() {
+ return rdnSequence;
+ }
+}
diff --git a/src/main/model/x501/RelativeDistinguishedName.java b/src/main/model/x501/RelativeDistinguishedName.java
new file mode 100644
index 0000000..8edde09
--- /dev/null
+++ b/src/main/model/x501/RelativeDistinguishedName.java
@@ -0,0 +1,78 @@
+package model.x501;
+
+import model.asn1.ASN1Object;
+import model.asn1.Encodable;
+import model.asn1.Tag;
+import model.asn1.exceptions.ParseException;
+import model.asn1.parsing.BytesReader;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ * Represents a DN item.
+ * <pre>
+ * RelativeDistinguishedName ::= SET SIZE (1..MAX) OF AttributeTypeAndValue
+ * </pre>
+ * For more information on DN, see {@link Name}.
+ */
+public class RelativeDistinguishedName extends ASN1Object {
+ private final AttributeTypeAndValue[] array;
+
+ /**
+ * EFFECT: Initialize the list with the given tag, parentTag, and array. For tag and parentTag, consult
+ * {@link ASN1Object}.
+ * REQUIRES: Array items should have UNIVERSAL SEQUENCE tag.
+ */
+ public RelativeDistinguishedName(Tag tag, Tag parentTag, AttributeTypeAndValue[] array) {
+ super(tag, parentTag);
+ this.array = array;
+ }
+
+ /**
+ * EFFECT: Parse the list from input DER bytes. For details on parsing, refer to {@link ASN1Object}.
+ * Throws {@link ParseException} for invalid input.
+ * MODIFIES: this, encoded
+ */
+ public RelativeDistinguishedName(BytesReader encoded, boolean hasParentTag) throws ParseException {
+ super(encoded, hasParentTag);
+ final List<AttributeTypeAndValue> list = new ArrayList<>();
+ for (int i = 0; i < getLength();) {
+ int index = encoded.getIndex();
+ final AttributeTypeAndValue value = new AttributeTypeAndValue(encoded, false);
+ value.getTag().enforce(TAG_SEQUENCE);
+ list.add(value);
+ index = encoded.getIndex() - index;
+ i += index;
+ }
+ this.array = list.toArray(new AttributeTypeAndValue[0]);
+ }
+
+ /**
+ * EFFECTS: Encode the SET OF into DER, keep order. Values will be encoded one-by-one.
+ */
+ @Override
+ public Byte[] encodeValueDER() {
+ return Stream.of(array)
+ .map(Encodable::encodeDER)
+ .flatMap(Arrays::stream)
+ .toArray(Byte[]::new);
+ }
+
+ /**
+ * EFFECT: Encode into multi-valed RDN strings like CN=yuuta+CN=qwq
+ */
+ @Override
+ public String toString() {
+ return Stream.of(array)
+ .map(AttributeTypeAndValue::toString)
+ .collect(Collectors.joining("+"));
+ }
+
+ public AttributeTypeAndValue[] getArray() {
+ return array;
+ }
+}