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