Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/x509/AlgIdDSA.java
41159 views
1
/*
2
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.security.x509;
27
28
import java.io.IOException;
29
import java.math.BigInteger;
30
import java.security.*;
31
import java.security.interfaces.DSAParams;
32
33
import sun.security.util.*;
34
35
36
/**
37
* This class identifies DSS/DSA Algorithm variants, which are distinguished
38
* by using different algorithm parameters <em>P, Q, G</em>. It uses the
39
* NIST/IETF standard DER encoding. These are used to implement the Digital
40
* Signature Standard (DSS), FIPS 186.
41
*
42
* <P><em><b>NOTE:</b></em> DSS/DSA Algorithm IDs may be created without these
43
* parameters. Use of DSS/DSA in modes where parameters are
44
* either implicit (e.g. a default applicable to a site or a larger scope),
45
* or are derived from some Certificate Authority's DSS certificate, is
46
* not supported directly. The application is responsible for creating a key
47
* containing the required parameters prior to using the key in cryptographic
48
* operations. The follwoing is an example of how this may be done assuming
49
* that we have a certificate called <code>currentCert</code> which doesn't
50
* contain DSS/DSA parameters and we need to derive DSS/DSA parameters
51
* from a CA's certificate called <code>caCert</code>.
52
*
53
* <pre>{@code
54
* // key containing parameters to use
55
* DSAPublicKey cAKey = (DSAPublicKey)(caCert.getPublicKey());
56
* // key without parameters
57
* DSAPublicKey nullParamsKey = (DSAPublicKey)(currentCert.getPublicKey());
58
*
59
* DSAParams cAKeyParams = cAKey.getParams();
60
* KeyFactory kf = KeyFactory.getInstance("DSA");
61
* DSAPublicKeySpec ks = new DSAPublicKeySpec(nullParamsKey.getY(),
62
* cAKeyParams.getP(),
63
* cAKeyParams.getQ(),
64
* cAKeyParams.getG());
65
* DSAPublicKey usableKey = kf.generatePublic(ks);
66
* }</pre>
67
*
68
* @see java.security.interfaces.DSAParams
69
* @see java.security.interfaces.DSAPublicKey
70
* @see java.security.KeyFactory
71
* @see java.security.spec.DSAPublicKeySpec
72
*
73
* @author David Brownell
74
*/
75
public final
76
class AlgIdDSA extends AlgorithmId implements DSAParams
77
{
78
79
@java.io.Serial
80
private static final long serialVersionUID = 3437177836797504046L;
81
82
/*
83
* The three unsigned integer parameters.
84
*/
85
private BigInteger p , q, g;
86
87
/** Returns the DSS/DSA parameter "P" */
88
public BigInteger getP () { return p; }
89
90
/** Returns the DSS/DSA parameter "Q" */
91
public BigInteger getQ () { return q; }
92
93
/** Returns the DSS/DSA parameter "G" */
94
public BigInteger getG () { return g; }
95
96
/**
97
* Default constructor. The OID and parameters must be
98
* deserialized before this algorithm ID is used.
99
*/
100
@Deprecated
101
public AlgIdDSA () {}
102
103
/**
104
* Constructs a DSS/DSA Algorithm ID from numeric parameters.
105
* If all three are null, then the parameters portion of the algorithm id
106
* is set to null. See note in header regarding use.
107
*
108
* @param p the DSS/DSA parameter "P"
109
* @param q the DSS/DSA parameter "Q"
110
* @param g the DSS/DSA parameter "G"
111
*/
112
public AlgIdDSA (BigInteger p, BigInteger q, BigInteger g) {
113
super (DSA_oid);
114
115
if (p != null || q != null || g != null) {
116
if (p == null || q == null || g == null)
117
throw new ProviderException("Invalid parameters for DSS/DSA" +
118
" Algorithm ID");
119
try {
120
this.p = p;
121
this.q = q;
122
this.g = g;
123
initializeParams ();
124
125
} catch (IOException e) {
126
/* this should not happen */
127
throw new ProviderException ("Construct DSS/DSA Algorithm ID");
128
}
129
}
130
}
131
132
/**
133
* Returns "DSA", indicating the Digital Signature Algorithm (DSA) as
134
* defined by the Digital Signature Standard (DSS), FIPS 186.
135
*/
136
public String getName ()
137
{ return "DSA"; }
138
139
140
/*
141
* For algorithm IDs which haven't been created from a DER encoded
142
* value, "params" must be created.
143
*/
144
private void initializeParams () throws IOException {
145
DerOutputStream out = new DerOutputStream();
146
out.putInteger(p);
147
out.putInteger(q);
148
out.putInteger(g);
149
DerOutputStream result = new DerOutputStream();
150
result.write(DerValue.tag_Sequence, out);
151
encodedParams = result.toByteArray();
152
}
153
154
/**
155
* Parses algorithm parameters P, Q, and G. They're found
156
* in the "params" member, which never needs to be changed.
157
*/
158
protected void decodeParams () throws IOException {
159
if (encodedParams == null) {
160
throw new IOException("DSA alg params are null");
161
}
162
163
DerValue params = new DerValue(encodedParams);
164
if (params.tag != DerValue.tag_Sequence) {
165
throw new IOException("DSA alg parsing error");
166
}
167
168
params.data.reset ();
169
170
this.p = params.data.getBigInteger();
171
this.q = params.data.getBigInteger();
172
this.g = params.data.getBigInteger();
173
174
if (params.data.available () != 0)
175
throw new IOException ("AlgIdDSA params, extra="+
176
params.data.available ());
177
}
178
179
180
/*
181
* Returns a formatted string describing the parameters.
182
*/
183
public String toString () {
184
return paramsToString();
185
}
186
187
/*
188
* Returns a string describing the parameters.
189
*/
190
protected String paramsToString () {
191
if (encodedParams == null) {
192
return " null\n";
193
} else {
194
return "\n p:\n" + Debug.toHexString(p) +
195
"\n q:\n" + Debug.toHexString(q) +
196
"\n g:\n" + Debug.toHexString(g) +
197
"\n";
198
}
199
}
200
}
201
202