summary refs log tree commit diff
path: root/crypto/src/asn1/mozilla/PublicKeyAndChallenge.cs
blob: 6b43b1c1f73139409a6af20cbe7d95e5959b114e (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
using System;

using Org.BouncyCastle.Asn1.X509;

namespace Org.BouncyCastle.Asn1.Mozilla
{
    /// <summary>
    /// For parsing the PublicKeyAndChallenge created by the KEYGEN tag included by Mozilla based browsers.
    /// </summary>
    /// <remarks>
    /// <code>
    /// PublicKeyAndChallenge ::= SEQUENCE
    /// {
    ///     spki        SubjectPublicKeyInfo,
    ///     challenge   IA5STRING
    /// }
    /// </code>
    /// </remarks>
    public class PublicKeyAndChallenge
        : Asn1Encodable
    {
        public static PublicKeyAndChallenge GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is PublicKeyAndChallenge publicKeyAndChallenge)
                return publicKeyAndChallenge;
#pragma warning disable CS0618 // Type or member is obsolete
            return new PublicKeyAndChallenge(Asn1Sequence.GetInstance(obj));
#pragma warning restore CS0618 // Type or member is obsolete
        }

        public static PublicKeyAndChallenge GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
        {
#pragma warning disable CS0618 // Type or member is obsolete
            return new PublicKeyAndChallenge(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
#pragma warning restore CS0618 // Type or member is obsolete
        }

        private readonly SubjectPublicKeyInfo m_spki;
        private readonly DerIA5String m_challenge;

        public PublicKeyAndChallenge(SubjectPublicKeyInfo spki, DerIA5String challenge)
        {
            m_spki = spki ?? throw new ArgumentNullException(nameof(spki));
            m_challenge = challenge ?? throw new ArgumentNullException(nameof(m_challenge));
        }

        [Obsolete("Use 'GetInstance' instead")]
        public PublicKeyAndChallenge(Asn1Sequence seq)
        {
            if (seq == null)
                throw new ArgumentNullException(nameof(seq));
            if (seq.Count != 2)
                throw new ArgumentException($"Expected 2 elements, but found {seq.Count}", nameof(seq));

            m_spki = SubjectPublicKeyInfo.GetInstance(seq[0]);
            m_challenge = DerIA5String.GetInstance(seq[1]);
        }

        public DerIA5String Challenge => m_challenge;

        public SubjectPublicKeyInfo Spki => m_spki;

        [Obsolete("Use 'Spki' instead")]
        public SubjectPublicKeyInfo SubjectPublicKeyInfo => m_spki;

        public override Asn1Object ToAsn1Object() => new DerSequence(m_spki, m_challenge);
    }
}