summary refs log tree commit diff
path: root/crypto/src/asn1/x509/SubjectAltPublicKeyInfo.cs
blob: 4fe1658980b4d121554f10cad82e3d8f0ef16703 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;

namespace Org.BouncyCastle.Asn1.X509
{
    /**
     * X.509 Section 9.8.2.
     * <br/>
     * This public-key certificate extension, when present, shall contain the subject’s alternative public key information
     * <pre>
     * subjectAltPublicKeyInfo EXTENSION ::= {
     *      SYNTAX SubjectAltPublicKeyInfo
     *      IDENTIFIED BY id-ce-subjectAltPublicKeyInfo }
     *
     * SubjectAltPublicKeyInfo ::= SEQUENCE {
     *     algorithm AlgorithmIdentifier{{SupportedAlgorithms}},
     *     subjectAltPublicKey BIT STRING }
     * </pre>
     * The SubjectAltPublicKeyInfo data type has the following components:
     * <ul>
     * <li>the algorithm subcomponent, which shall hold the algorithm that this public key is an instance of</li>
     * <li>the subjectAltPublicKey subcomponent, which shall hold the alternative public key</li>
     * </ul>
     * This extension may be flagged as critical or as non-critical.
     * <br/>
     * NOTE – It is recommended that it be flagged as non-critical. Flagging it as critical would require relying parties to understand this
     * extension and the alternative public-key algorithm.
     */
    public class SubjectAltPublicKeyInfo
        : Asn1Encodable
    {
        private readonly AlgorithmIdentifier m_algorithm;
        private readonly DerBitString m_subjectAltPublicKey;

        public static SubjectAltPublicKeyInfo GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is SubjectAltPublicKeyInfo subjectAltPublicKeyInfo)
                return subjectAltPublicKeyInfo;
            return new SubjectAltPublicKeyInfo(Asn1Sequence.GetInstance(obj));
        }

        public static SubjectAltPublicKeyInfo GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
        {
            return GetInstance(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
        }

        public static SubjectAltPublicKeyInfo FromExtensions(X509Extensions extensions)
        {
            return GetInstance(
                X509Extensions.GetExtensionParsedValue(extensions, X509Extensions.SubjectAltPublicKeyInfo));
        }

        private SubjectAltPublicKeyInfo(Asn1Sequence seq)
        {
            if (seq.Count != 2)
                throw new ArgumentException("extension should contain only 2 elements");

            m_algorithm = AlgorithmIdentifier.GetInstance(seq[0]);
            m_subjectAltPublicKey = DerBitString.GetInstance(seq[1]);
        }

        public SubjectAltPublicKeyInfo(AlgorithmIdentifier algorithm, DerBitString subjectAltPublicKey)
        {
            m_algorithm = algorithm;
            m_subjectAltPublicKey = subjectAltPublicKey;
        }

        public SubjectAltPublicKeyInfo(SubjectPublicKeyInfo subjectPublicKeyInfo)
        {
            m_algorithm = subjectPublicKeyInfo.AlgorithmID;
            m_subjectAltPublicKey = subjectPublicKeyInfo.PublicKeyData;
        }

        public AlgorithmIdentifier Algorithm => Algorithm;

        public DerBitString SubjectAltPublicKey => m_subjectAltPublicKey;

        public override Asn1Object ToAsn1Object()
        {
            return new DerSequence(m_algorithm, m_subjectAltPublicKey);
        }
    }
}