summaryrefslogtreecommitdiffstats
path: root/base/util/src/netscape/security/x509/AlgIdDSA.java
blob: 1f00b0355d4c3a6aa9dc77553ebb0769acb4aa80 (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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package netscape.security.x509;

import java.io.IOException;
import java.math.BigInteger;
import java.security.ProviderException;
import java.security.interfaces.DSAParams;

import netscape.security.util.BigInt;
import netscape.security.util.DerOutputStream;
import netscape.security.util.DerValue;

/**
 * This class identifies DSS/DSA Algorithm variants, which are distinguished
 * by using different algorithm parameters <em>P, Q, G</em>. It uses the
 * NIST/IETF standard DER encoding. These are used to implement the Digital
 * Signature Standard (DSS), FIPS 186.
 *
 * <P>
 * <em><b>NOTE:</b>  At this time, DSS/DSA Algorithm IDs must always
 * include these parameters.  Use of DSS/DSA in modes where parameters are
 * either implicit (e.g. a default applicable to a site or a larger scope),
 * or are derived from some Certificate Authority's DSS certificate, is
 * not currently supported. </em>
 *
 * @version 1.31
 * @author David Brownell
 */
public final class AlgIdDSA extends AlgorithmId implements DSAParams {
    /**
     *
     */
    private static final long serialVersionUID = 5978220691806461631L;
    /*
     * The three unsigned integer parameters.
     */
    private BigInteger p, q, g;

    /** Returns the DSS/DSA parameter "P" */
    public BigInteger getP() {
        return p;
    }

    /** Returns the DSS/DSA parameter "Q" */
    public BigInteger getQ() {
        return q;
    }

    /** Returns the DSS/DSA parameter "G" */
    public BigInteger getG() {
        return g;
    }

    AlgIdDSA(DerValue val) throws IOException {
        super(val.getOID());
    }

    /**
     * Construct an AlgIdDSA from an X.509 encoded byte array.
     */
    public AlgIdDSA(byte[] encodedAlg) throws IOException {
        super(new DerValue(encodedAlg).getOID());
    }

    /**
     * Constructs a DSS/DSA Algorithm ID from unsigned integers that
     * define the algorithm parameters. Those integers are encoded
     * as big-endian byte arrays.
     *
     * @param p the DSS/DSA paramter "P"
     * @param q the DSS/DSA paramter "Q"
     * @param g the DSS/DSA paramter "G"
     */
    public AlgIdDSA(byte p[], byte q[], byte g[])
            throws IOException {
        this(new BigInteger(1, p),
                new BigInteger(1, q),
                new BigInteger(1, g));
    }

    /**
     * Constructs a DSS/DSA Algorithm ID from numeric parameters.
     *
     * @param p the DSS/DSA paramter "P"
     * @param q the DSS/DSA paramter "Q"
     * @param g the DSS/DSA paramter "G"
     */
    public AlgIdDSA(BigInteger p, BigInteger q, BigInteger g) {
        super(DSA_oid);

        try {
            this.p = p;
            this.q = q;
            this.g = g;
            initializeParams();

        } catch (IOException e) {
            /* this should not happen */
            throw new ProviderException("Construct DSS/DSA Algorithm ID");
        }
    }

    /**
     * Returns "DSA", indicating the Digital Signature Algorithm (DSA) as
     * defined by the Digital Signature Standard (DSS), FIPS 186.
     */
    public String getName() {
        return "DSA";
    }

    /*
     * For algorithm IDs which haven't been created from a DER encoded
     * value, "params" must be created.
     */
    private void initializeParams()
            throws IOException {
        DerOutputStream out = new DerOutputStream();

        out.putInteger(new BigInt(p.toByteArray()));
        out.putInteger(new BigInt(q.toByteArray()));
        out.putInteger(new BigInt(g.toByteArray()));
        params = new DerValue(DerValue.tag_Sequence, out.toByteArray());
    }

    /**
     * Parses algorithm parameters P, Q, and G. They're found
     * in the "params" member, which never needs to be changed.
     */
    protected void decodeParams()
            throws IOException {
        if (params == null || params.tag != DerValue.tag_Sequence)
            throw new IOException("DSA alg parsing error");

        params.data.reset();

        this.p = params.data.getInteger().toBigInteger();
        this.q = params.data.getInteger().toBigInteger();
        this.g = params.data.getInteger().toBigInteger();

        if (params.data.available() != 0)
            throw new IOException("AlgIdDSA params, extra=" +
                    params.data.available());
    }

    /*
     * Returns a formatted string describing the parameters.
     */
    public String toString() {
        return paramsToString();
    }

    /*
     * Returns a string describing the parameters.
     */
    protected String paramsToString() {
        return "\n    p:\n" + (new BigInt(p)).toString() +
                "\n    q:\n" + (new BigInt(q)).toString() +
                "\n    g:\n" + (new BigInt(g)).toString() +
                "\n";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((g == null) ? 0 : g.hashCode());
        result = prime * result + ((p == null) ? 0 : p.hashCode());
        result = prime * result + ((q == null) ? 0 : q.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object other) {
        if (other == null) {
            return false;
        }
        if (other instanceof AlgIdDSA) {
            AlgIdDSA rhs = (AlgIdDSA) other;
            if (this == rhs) {
                return true;
            }
            if (!(bigIntEquals(this.p, rhs.p) && bigIntEquals(this.q, rhs.q) && bigIntEquals(this.g, rhs.g))) {
                return false;
            }
            return super.equals(rhs);
        }
        return false;
    }

    public boolean bigIntEquals(BigInteger x, BigInteger y) {
        if (x == null) {
            if (y != null) {
                return false;
            }
        } else {
            if (!x.equals(y)) {
                return false;
            }
        }
        return true;
    }

}