aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/asn1/ASN1String.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/asn1/ASN1String.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/asn1/ASN1String.java')
-rw-r--r--src/main/model/asn1/ASN1String.java85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/main/model/asn1/ASN1String.java b/src/main/model/asn1/ASN1String.java
new file mode 100644
index 0000000..148c564
--- /dev/null
+++ b/src/main/model/asn1/ASN1String.java
@@ -0,0 +1,85 @@
+package model.asn1;
+
+import model.asn1.exceptions.ParseException;
+import model.asn1.parsing.BytesReader;
+import ui.Utils;
+
+import java.nio.charset.StandardCharsets;
+
+/**
+ * Represents an ASN.1 string with an optional character set restriction. Users should choose from one of its
+ * implementations that encode a specific ASN.1 string type (e.g., {@link PrintableString} or {@link IA5String}).
+ */
+public abstract class ASN1String extends ASN1Object {
+ private String rawString;
+
+ /**
+ * EFFECTS: Constructs an ASN1String with the given tag, parent tag, and string.
+ * - Throws {@link ParseException} if the string does not pass corresponding restrictions of the specific
+ * string type (same as {@link ASN1String#validate(String)})
+ * REQUIRES: For the requirements of tag and parentTag, consult {@link ASN1Object}.
+ */
+ public ASN1String(Tag tag, Tag parentTag, String string) throws ParseException {
+ super(tag, parentTag);
+ setString(string);
+ }
+
+ /**
+ * EFFECTS: Parse the input value. See {@link ASN1Object} with the rawString.
+ * Throws {@link ParseException} when invalid:
+ * - String does not pass type restrictions
+ * - Early EOF
+ * - Other cases as seen in {@link ASN1Object}
+ * MODIFIES: this, encoded (bytes are read)
+ */
+ public ASN1String(BytesReader encoded, boolean hasParentTag) throws ParseException {
+ super(encoded, hasParentTag);
+ }
+
+ /**
+ * EFFECTS: Validate and set the string.
+ * Throws {@link ParseException} if the string is invalid.
+ * MODIFIES: this
+ */
+ protected void setString(String rawString) throws ParseException {
+ if (!validate(rawString)) {
+ throw new ParseException(String.format("The string '%s' is illegal for '%s'.",
+ rawString,
+ getClass().getSimpleName()));
+ }
+ this.rawString = rawString;
+ }
+
+ /**
+ * EFFECTS: Validate whether the given string matches the ASN.1 restrictions on this specific string type. By
+ * default, it always returns true.
+ */
+ protected boolean validate(String newString) {
+ return true;
+ }
+
+ /**
+ * Same as {@link ASN1String#getString()}.
+ */
+ @Override
+ public String toString() {
+ return rawString;
+ }
+
+ /**
+ * EFFECTS: Encode the string in DER bytes (big-endian UTF-8).
+ */
+ @Override
+ public Byte[] encodeValueDER() {
+ final byte[] bytes = rawString.getBytes(StandardCharsets.UTF_8);
+ Byte[] b = new Byte[bytes.length];
+ for (int i = 0; i < bytes.length; i++) {
+ b[i] = bytes[i];
+ }
+ return b;
+ }
+
+ public String getString() {
+ return rawString;
+ }
+}