summary refs log tree commit diff
path: root/crypto/src/tls/DefaultTlsSrpConfigVerifier.cs
blob: 781249829c31ae5a63628c68b2e1a590134d0682 (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
using System;
using System.Collections;

using Org.BouncyCastle.Math;
using Org.BouncyCastle.Tls.Crypto;
using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Tls
{
    public class DefaultTlsSrpConfigVerifier
        : TlsSrpConfigVerifier
    {
        private static readonly IList DefaultGroups = Platform.CreateArrayList();

        static DefaultTlsSrpConfigVerifier()
        {
            DefaultGroups.Add(Srp6StandardGroups.rfc5054_1024);
            DefaultGroups.Add(Srp6StandardGroups.rfc5054_1536);
            DefaultGroups.Add(Srp6StandardGroups.rfc5054_2048);
            DefaultGroups.Add(Srp6StandardGroups.rfc5054_3072);
            DefaultGroups.Add(Srp6StandardGroups.rfc5054_4096);
            DefaultGroups.Add(Srp6StandardGroups.rfc5054_6144);
            DefaultGroups.Add(Srp6StandardGroups.rfc5054_8192);
        }

        // IList is (SRP6Group)
        protected readonly IList m_groups;

        /// <summary>Accept only the group parameters specified in RFC 5054 Appendix A.</summary>
        public DefaultTlsSrpConfigVerifier()
            : this(DefaultGroups)
        {
        }

        /// <summary>Specify a custom set of acceptable group parameters.</summary>
        /// <param name="groups">an <see cref="IList"/> of acceptable <see cref="Srp6Group"/>.</param>
        public DefaultTlsSrpConfigVerifier(IList groups)
        {
            this.m_groups = Platform.CreateArrayList(groups);
        }

        public virtual bool Accept(TlsSrpConfig srpConfig)
        {
            foreach (Srp6Group group in m_groups)
            {
                if (AreGroupsEqual(srpConfig, group))
                    return true;
            }
            return false;
        }

        protected virtual bool AreGroupsEqual(TlsSrpConfig a, Srp6Group b)
        {
            BigInteger[] ng = a.GetExplicitNG();
            return AreParametersEqual(ng[0], b.N) && AreParametersEqual(ng[1], b.G);
        }

        protected virtual bool AreParametersEqual(BigInteger a, BigInteger b)
        {
            return a == b || a.Equals(b);
        }
    }
}