diff options
Diffstat (limited to 'src/main/model/x501/AttributeTypeAndValue.java')
-rw-r--r-- | src/main/model/x501/AttributeTypeAndValue.java | 90 |
1 files changed, 90 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; + } +} |