From d342a45d98c4795b3a3fe1aaef5236ad4a782b55 Mon Sep 17 00:00:00 2001 From: Yuuta Liang Date: Thu, 12 Oct 2023 12:10:33 +0800 Subject: 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 --- src/main/model/pki/SubjectPublicKeyInfo.java | 83 ++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/main/model/pki/SubjectPublicKeyInfo.java (limited to 'src/main/model/pki/SubjectPublicKeyInfo.java') diff --git a/src/main/model/pki/SubjectPublicKeyInfo.java b/src/main/model/pki/SubjectPublicKeyInfo.java new file mode 100644 index 0000000..ac72055 --- /dev/null +++ b/src/main/model/pki/SubjectPublicKeyInfo.java @@ -0,0 +1,83 @@ +package model.pki; + +import model.asn1.ASN1Object; +import model.asn1.BitString; +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; + +/** + * Represents the following ASN.1 structure: + *
+ *     SubjectPublicKeyInfo ::= SEQUENCE {
+ *          algorithm AlgorithmIdentifier{{SupportedAlgorithms}},
+ *          subjectPublicKey BIT STRING,
+ *     ... }
+ * 
+ * It represents the public key of a subject, in a certificate. + */ +public class SubjectPublicKeyInfo extends ASN1Object { + /** + * The algorithm used. + */ + private final AlgorithmIdentifier algorithm; + + /** + * The public key. + */ + private final BitString subjectPublicKey; + + /** + * EFFECTS: Init with tags, algorithm, subjectPublicKey. For tags, see {@link ASN1Object}. + * REQUIRES: The public key should be a valid $algorithm key. Algorithm and publicKey should have default UNIVERSAL + * tags (SEQUENCE and BIT STRING). + */ + public SubjectPublicKeyInfo(Tag tag, Tag parentTag, + final AlgorithmIdentifier algorithm, + final BitString subjectPublicKey) { + super(tag, parentTag); + this.algorithm = algorithm; + this.subjectPublicKey = subjectPublicKey; + } + + /** + * EFFECTS: Parse input DER. + * 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 SubjectPublicKeyInfo(BytesReader encoded, boolean hasParentTag) throws ParseException { + super(encoded, hasParentTag); + this.algorithm = new AlgorithmIdentifier(encoded, false); + this.algorithm.getTag().enforce(TAG_SEQUENCE); + + this.subjectPublicKey = new BitString(encoded, false); + this.subjectPublicKey.getTag().enforce(BitString.TAG); + } + + /** + * EFFECTS: Encode the fields into DER, in the order. + */ + @Override + public Byte[] encodeValueDER() { + return Stream.of(Arrays.asList(algorithm.encodeDER()), + Arrays.asList(subjectPublicKey.encodeDER())) + .flatMap(Collection::stream) + .toArray(Byte[]::new); + } + + public AlgorithmIdentifier getAlgorithm() { + return algorithm; + } + + public BitString getSubjectPublicKey() { + return subjectPublicKey; + } +} -- cgit v1.2.3