summaryrefslogtreecommitdiffstats
path: root/bcprov/src/main/java/org/bouncycastle/crypto/tls/DefaultTlsSRPGroupVerifier.java
blob: 8fb9baa982eeb6d62ae1abd5badf379df357544c (plain)
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
package org.bouncycastle.crypto.tls;

import java.math.BigInteger;
import java.util.Vector;

import org.bouncycastle.crypto.agreement.srp.SRP6StandardGroups;
import org.bouncycastle.crypto.params.SRP6GroupParameters;

public class DefaultTlsSRPGroupVerifier
    implements TlsSRPGroupVerifier
{
    protected static final Vector DEFAULT_GROUPS = new Vector();

    static
    {
        DEFAULT_GROUPS.addElement(SRP6StandardGroups.rfc5054_1024);
        DEFAULT_GROUPS.addElement(SRP6StandardGroups.rfc5054_1536);
        DEFAULT_GROUPS.addElement(SRP6StandardGroups.rfc5054_2048);
        DEFAULT_GROUPS.addElement(SRP6StandardGroups.rfc5054_3072);
        DEFAULT_GROUPS.addElement(SRP6StandardGroups.rfc5054_4096);
        DEFAULT_GROUPS.addElement(SRP6StandardGroups.rfc5054_6144);
        DEFAULT_GROUPS.addElement(SRP6StandardGroups.rfc5054_8192);
    }

    // Vector is (SRP6GroupParameters)
    protected Vector groups;

    /**
     * Accept only the group parameters specified in RFC 5054 Appendix A.
     */
    public DefaultTlsSRPGroupVerifier()
    {
        this(DEFAULT_GROUPS);
    }

    /**
     * Specify a custom set of acceptable group parameters.
     * 
     * @param groups a {@link Vector} of acceptable {@link SRP6GroupParameters}
     */
    public DefaultTlsSRPGroupVerifier(Vector groups)
    {
        this.groups = groups;
    }

    public boolean accept(SRP6GroupParameters group)
    {
        for (int i = 0; i < groups.size(); ++i)
        {
            if (areGroupsEqual(group, (SRP6GroupParameters)groups.elementAt(i)))
            {
                return true;
            }
        }
        return false;
    }

    protected boolean areGroupsEqual(SRP6GroupParameters a, SRP6GroupParameters b)
    {
        return a == b || (areParametersEqual(a.getN(), b.getN()) && areParametersEqual(a.getG(), b.getG()));
    }
    
    protected boolean areParametersEqual(BigInteger a, BigInteger b)
    {
        return a == b || a.equals(b);
    }
}