aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/csr/Attribute.java
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/csr/Attribute.java
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/csr/Attribute.java')
-rw-r--r--src/main/model/csr/Attribute.java83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/main/model/csr/Attribute.java b/src/main/model/csr/Attribute.java
new file mode 100644
index 0000000..2fa319b
--- /dev/null
+++ b/src/main/model/csr/Attribute.java
@@ -0,0 +1,83 @@
+package model.csr;
+
+import model.asn1.ASN1Object;
+import model.asn1.ObjectIdentifier;
+import model.asn1.Tag;
+import model.asn1.exceptions.ParseException;
+import model.asn1.parsing.BytesReader;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.stream.Stream;
+
+/**
+ * Implements the following:
+ * <pre>
+ * Attribute { ATTRIBUTE:IOSet } ::= SEQUENCE {
+ * type ATTRIBUTE.&id({IOSet}),
+ * values SET SIZE(1..MAX) OF ATTRIBUTE.&Type({IOSet}{@type})
+ * }
+ * </pre>
+ *
+ * Represents a key - values pair in the CSR attribute.
+ */
+public class Attribute extends ASN1Object {
+ /**
+ * The type of that attribute. For example, <pre>2.5.29.14</pre> is subjectKeyIdentifier.
+ * It determines the format of the value.
+ */
+ private final ObjectIdentifier type;
+
+ /**
+ * Value set.
+ */
+ private final Values values;
+
+ /**
+ * 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, and values should be SET OF.
+ */
+ public Attribute(Tag tag, Tag parentTag,
+ ObjectIdentifier type, Values values) {
+ super(tag, parentTag);
+ this.type = type;
+ this.values = values;
+ }
+
+ /**
+ * EFFECTS: Parse input DER. Value is not checked against the type.
+ * Throws {@link ASN1Object} if invalid:
+ * - Any fields missing (info, algorithm, signature)
+ * - 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 Attribute(BytesReader encoded, boolean hasParentTag) throws ParseException {
+ super(encoded, hasParentTag);
+ this.type = new ObjectIdentifier(encoded, false);
+ this.type.getTag().enforce(ObjectIdentifier.TAG);
+
+ this.values = new Values(encoded, false);
+ this.values.getTag().enforce(TAG_SET);
+ }
+
+ /**
+ * EFFECTS: Encode the fields into DER, in the order.
+ */
+ @Override
+ public Byte[] encodeValueDER() {
+ return Stream.of(Arrays.asList(type.encodeDER()),
+ Arrays.asList(values.encodeDER()))
+ .flatMap(Collection::stream)
+ .toArray(Byte[]::new);
+ }
+
+ public ObjectIdentifier getType() {
+ return type;
+ }
+
+ public Values getValues() {
+ return values;
+ }
+}