summary refs log tree commit diff
path: root/crypto/src/tls/TrustedAuthority.cs
blob: b8266645018b940e0194182666492f4c4202d46c (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.IO;

using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Tls
{
    public sealed class TrustedAuthority
    {
        private readonly short m_identifierType;
        private readonly object m_identifier;

        public TrustedAuthority(short identifierType, object identifier)
        {
            if (!IsCorrectType(identifierType, identifier))
                throw new ArgumentException("not an instance of the correct type", "identifier");

            this.m_identifierType = identifierType;
            this.m_identifier = identifier;
        }

        public short IdentifierType
        {
            get { return m_identifierType; }
        }

        public object Identifier
        {
            get { return m_identifier; }
        }

        public byte[] GetCertSha1Hash()
        {
            return Arrays.Clone((byte[])m_identifier);
        }

        public byte[] GetKeySha1Hash()
        {
            return Arrays.Clone((byte[])m_identifier);
        }

        public X509Name X509Name
        {
            get
            {
                CheckCorrectType(Tls.IdentifierType.x509_name);
                return (X509Name)m_identifier;
            }
        }

        /// <summary>Encode this <see cref="TrustedAuthority"/> to a <see cref="Stream"/>.</summary>
        /// <param name="output">the <see cref="Stream"/> to encode to.</param>
        /// <exception cref="IOException"/>
        public void Encode(Stream output)
        {
            TlsUtilities.WriteUint8(m_identifierType, output);

            switch (m_identifierType)
            {
            case Tls.IdentifierType.cert_sha1_hash:
            case Tls.IdentifierType.key_sha1_hash:
            {
                byte[] sha1Hash = (byte[])m_identifier;
                output.Write(sha1Hash, 0, sha1Hash.Length);
                break;
            }
            case Tls.IdentifierType.pre_agreed:
            {
                break;
            }
            case Tls.IdentifierType.x509_name:
            {
                X509Name dn = (X509Name)m_identifier;
                byte[] derEncoding = dn.GetEncoded(Asn1Encodable.Der);
                TlsUtilities.WriteOpaque16(derEncoding, output);
                break;
            }
            default:
                throw new TlsFatalAlert(AlertDescription.internal_error);
            }
        }

        /// <summary>Parse a <see cref="TrustedAuthority"/> from a <see cref="Stream"/>.</summary>
        /// <param name="input">the <see cref="Stream"/> to parse from.</param>
        /// <returns>a <see cref="TrustedAuthority"/> object.</returns>
        /// <exception cref="IOException"/>
        public static TrustedAuthority Parse(Stream input)
        {
            short identifier_type = TlsUtilities.ReadUint8(input);
            object identifier;

            switch (identifier_type)
            {
            case Tls.IdentifierType.cert_sha1_hash:
            case Tls.IdentifierType.key_sha1_hash:
            {
                identifier = TlsUtilities.ReadFully(20, input);
                break;
            }
            case Tls.IdentifierType.pre_agreed:
            {
                identifier = null;
                break;
            }
            case Tls.IdentifierType.x509_name:
            {
                byte[] derEncoding = TlsUtilities.ReadOpaque16(input, 1);
                Asn1Object asn1 = TlsUtilities.ReadAsn1Object(derEncoding);
                X509Name x509Name = X509Name.GetInstance(asn1);
                TlsUtilities.RequireDerEncoding(x509Name, derEncoding);
                identifier = x509Name;
                break;
            }
            default:
                throw new TlsFatalAlert(AlertDescription.decode_error);
            }

            return new TrustedAuthority(identifier_type, identifier);
        }

        private void CheckCorrectType(short expectedIdentifierType)
        {
            if (m_identifierType != expectedIdentifierType || !IsCorrectType(expectedIdentifierType, m_identifier))
                throw new InvalidOperationException("TrustedAuthority is not of type "
                    + Tls.IdentifierType.GetName(expectedIdentifierType));
        }

        private static bool IsCorrectType(short identifierType, object identifier)
        {
            switch (identifierType)
            {
            case Tls.IdentifierType.cert_sha1_hash:
            case Tls.IdentifierType.key_sha1_hash:
                return IsSha1Hash(identifier);
            case Tls.IdentifierType.pre_agreed:
                return identifier == null;
            case Tls.IdentifierType.x509_name:
                return identifier is X509Name;
            default:
                throw new ArgumentException("unsupported IdentifierType", "identifierType");
            }
        }

        private static bool IsSha1Hash(object identifier)
        {
            return identifier is byte[] && ((byte[])identifier).Length == 20;
        }
    }
}