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

using Org.BouncyCastle.Asn1.X509;

namespace Org.BouncyCastle.Asn1.Mozilla
{
    /// <summary>
    /// For parsing the SignedPublicKeyAndChallenge created by the KEYGEN tag included by Mozilla based browsers.
    /// </summary>
    /// <remarks>
    /// <code>
    /// SignedPublicKeyAndChallenge ::= SEQUENCE
    /// {
    ///     publicKeyAndChallenge   PublicKeyAndChallenge,
    ///     signatureAlgorithm      AlgorithmIdentifier,
    ///     signature               BIT STRING
    /// }
    /// </code>
    /// </remarks>
    public class SignedPublicKeyAndChallenge
        : Asn1Encodable
    {
        public static SignedPublicKeyAndChallenge GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is SignedPublicKeyAndChallenge signedPublicKeyAndChallenge)
                return signedPublicKeyAndChallenge;
            return new SignedPublicKeyAndChallenge(Asn1Sequence.GetInstance(obj));
        }

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

        private readonly PublicKeyAndChallenge m_publicKeyAndChallenge;
        private readonly AlgorithmIdentifier m_signatureAlgorithm;
        private readonly DerBitString m_signature;

        public SignedPublicKeyAndChallenge(PublicKeyAndChallenge publicKeyAndChallenge,
            AlgorithmIdentifier signatureAlgorithm, DerBitString signature)
        {
            m_publicKeyAndChallenge = publicKeyAndChallenge
                ?? throw new ArgumentNullException(nameof(publicKeyAndChallenge));
            m_signatureAlgorithm = signatureAlgorithm ?? throw new ArgumentNullException(nameof(signatureAlgorithm));
            m_signature = signature ?? throw new ArgumentNullException(nameof(signature));
        }

        private SignedPublicKeyAndChallenge(Asn1Sequence seq)
        {
            if (seq == null)
                throw new ArgumentNullException(nameof(seq));
            if (seq.Count != 3)
                throw new ArgumentException($"Expected 3 elements, but found {seq.Count}", nameof(seq));

            m_publicKeyAndChallenge = PublicKeyAndChallenge.GetInstance(seq[0]);
            m_signatureAlgorithm = AlgorithmIdentifier.GetInstance(seq[1]);
            m_signature = DerBitString.GetInstance(seq[2]);
        }

        public PublicKeyAndChallenge PublicKeyAndChallenge => m_publicKeyAndChallenge;

        public DerBitString Signature => m_signature;

        public AlgorithmIdentifier SignatureAlgorithm => m_signatureAlgorithm;

        public override Asn1Object ToAsn1Object() =>
            new DerSequence(m_publicKeyAndChallenge, m_signatureAlgorithm, m_signature);
    }
}