author | weijun |
Wed, 16 Jan 2019 11:25:55 +0800 | |
changeset 53351 | bdb29aa5fd31 |
parent 50204 | 3195a713e24d |
child 54483 | ac20c3bdc55d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
53351
bdb29aa5fd31
8215694: keytool cannot generate RSASSA-PSS certificates
weijun
parents:
50204
diff
changeset
|
2 |
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
||
10782
01689c7b34ac
6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents:
10336
diff
changeset
|
27 |
package sun.security.pkcs10; |
2 | 28 |
|
29 |
import java.io.PrintStream; |
|
30 |
import java.io.IOException; |
|
31 |
import java.math.BigInteger; |
|
32 |
||
33 |
import java.security.cert.CertificateException; |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
34 |
import java.security.*; |
2 | 35 |
|
16020
b57c48f16179
8006182: cleanup to use java.util.Base64 in java security component, providers, and regression tests
msheppar
parents:
10782
diff
changeset
|
36 |
import java.util.Base64; |
2 | 37 |
|
38 |
import sun.security.util.*; |
|
39 |
import sun.security.x509.AlgorithmId; |
|
40 |
import sun.security.x509.X509Key; |
|
41 |
import sun.security.x509.X500Name; |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
42 |
import sun.security.util.SignatureUtil; |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
43 |
|
2 | 44 |
|
45 |
/** |
|
46 |
* A PKCS #10 certificate request is created and sent to a Certificate |
|
47 |
* Authority, which then creates an X.509 certificate and returns it to |
|
48 |
* the entity that requested it. A certificate request basically consists |
|
49 |
* of the subject's X.500 name, public key, and optionally some attributes, |
|
50 |
* signed using the corresponding private key. |
|
51 |
* |
|
52 |
* The ASN.1 syntax for a Certification Request is: |
|
53 |
* <pre> |
|
54 |
* CertificationRequest ::= SEQUENCE { |
|
55 |
* certificationRequestInfo CertificationRequestInfo, |
|
56 |
* signatureAlgorithm SignatureAlgorithmIdentifier, |
|
57 |
* signature Signature |
|
58 |
* } |
|
59 |
* |
|
60 |
* SignatureAlgorithmIdentifier ::= AlgorithmIdentifier |
|
61 |
* Signature ::= BIT STRING |
|
62 |
* |
|
63 |
* CertificationRequestInfo ::= SEQUENCE { |
|
64 |
* version Version, |
|
65 |
* subject Name, |
|
66 |
* subjectPublicKeyInfo SubjectPublicKeyInfo, |
|
67 |
* attributes [0] IMPLICIT Attributes |
|
68 |
* } |
|
69 |
* Attributes ::= SET OF Attribute |
|
70 |
* </pre> |
|
71 |
* |
|
72 |
* @author David Brownell |
|
73 |
* @author Amit Kapoor |
|
74 |
* @author Hemma Prafullchandra |
|
75 |
*/ |
|
76 |
public class PKCS10 { |
|
77 |
/** |
|
78 |
* Constructs an unsigned PKCS #10 certificate request. Before this |
|
79 |
* request may be used, it must be encoded and signed. Then it |
|
80 |
* must be retrieved in some conventional format (e.g. string). |
|
81 |
* |
|
82 |
* @param publicKey the public key that should be placed |
|
83 |
* into the certificate generated by the CA. |
|
84 |
*/ |
|
85 |
public PKCS10(PublicKey publicKey) { |
|
86 |
subjectPublicKeyInfo = publicKey; |
|
87 |
attributeSet = new PKCS10Attributes(); |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Constructs an unsigned PKCS #10 certificate request. Before this |
|
92 |
* request may be used, it must be encoded and signed. Then it |
|
93 |
* must be retrieved in some conventional format (e.g. string). |
|
94 |
* |
|
95 |
* @param publicKey the public key that should be placed |
|
96 |
* into the certificate generated by the CA. |
|
97 |
* @param attributes additonal set of PKCS10 attributes requested |
|
98 |
* for in the certificate. |
|
99 |
*/ |
|
100 |
public PKCS10(PublicKey publicKey, PKCS10Attributes attributes) { |
|
101 |
subjectPublicKeyInfo = publicKey; |
|
102 |
attributeSet = attributes; |
|
103 |
} |
|
104 |
||
105 |
/** |
|
106 |
* Parses an encoded, signed PKCS #10 certificate request, verifying |
|
107 |
* the request's signature as it does so. This constructor would |
|
108 |
* typically be used by a Certificate Authority, from which a new |
|
109 |
* certificate would then be constructed. |
|
110 |
* |
|
111 |
* @param data the DER-encoded PKCS #10 request. |
|
112 |
* @exception IOException for low level errors reading the data |
|
113 |
* @exception SignatureException when the signature is invalid |
|
114 |
* @exception NoSuchAlgorithmException when the signature |
|
115 |
* algorithm is not supported in this environment |
|
116 |
*/ |
|
117 |
public PKCS10(byte[] data) |
|
118 |
throws IOException, SignatureException, NoSuchAlgorithmException { |
|
119 |
DerInputStream in; |
|
120 |
DerValue[] seq; |
|
121 |
AlgorithmId id; |
|
122 |
byte[] sigData; |
|
123 |
Signature sig; |
|
124 |
||
125 |
encoded = data; |
|
126 |
||
127 |
// |
|
128 |
// Outer sequence: request, signature algorithm, signature. |
|
129 |
// Parse, and prepare to verify later. |
|
130 |
// |
|
131 |
in = new DerInputStream(data); |
|
132 |
seq = in.getSequence(3); |
|
133 |
||
134 |
if (seq.length != 3) |
|
135 |
throw new IllegalArgumentException("not a PKCS #10 request"); |
|
136 |
||
137 |
data = seq[0].toByteArray(); // reusing this variable |
|
138 |
id = AlgorithmId.parse(seq[1]); |
|
139 |
sigData = seq[2].getBitString(); |
|
140 |
||
141 |
// |
|
142 |
// Inner sequence: version, name, key, attributes |
|
143 |
// |
|
144 |
BigInteger serial; |
|
145 |
DerValue val; |
|
146 |
||
147 |
serial = seq[0].data.getBigInteger(); |
|
148 |
if (!serial.equals(BigInteger.ZERO)) |
|
149 |
throw new IllegalArgumentException("not PKCS #10 v1"); |
|
150 |
||
151 |
subject = new X500Name(seq[0].data); |
|
152 |
subjectPublicKeyInfo = X509Key.parse(seq[0].data.getDerValue()); |
|
153 |
||
154 |
// Cope with a somewhat common illegal PKCS #10 format |
|
155 |
if (seq[0].data.available() != 0) |
|
156 |
attributeSet = new PKCS10Attributes(seq[0].data); |
|
157 |
else |
|
158 |
attributeSet = new PKCS10Attributes(); |
|
159 |
||
160 |
if (seq[0].data.available() != 0) |
|
161 |
throw new IllegalArgumentException("illegal PKCS #10 data"); |
|
162 |
||
163 |
// |
|
164 |
// OK, we parsed it all ... validate the signature using the |
|
165 |
// key and signature algorithm we found. |
|
166 |
// |
|
167 |
try { |
|
44046
762e807bfac1
8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents:
29596
diff
changeset
|
168 |
sigAlg = id.getName(); |
762e807bfac1
8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents:
29596
diff
changeset
|
169 |
sig = Signature.getInstance(sigAlg); |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
170 |
|
2 | 171 |
sig.initVerify(subjectPublicKeyInfo); |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
172 |
|
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
173 |
// set parameters after Signature.initSign/initVerify call, |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
174 |
// so the deferred provider selections occur when key is set |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
175 |
SignatureUtil.specialSetParameter(sig, id.getParameters()); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
176 |
|
2 | 177 |
sig.update(data); |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
178 |
if (!sig.verify(sigData)) { |
2 | 179 |
throw new SignatureException("Invalid PKCS #10 signature"); |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
180 |
} |
2 | 181 |
} catch (InvalidKeyException e) { |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
182 |
throw new SignatureException("Invalid key"); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
183 |
} catch (InvalidAlgorithmParameterException e) { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
184 |
throw new SignatureException("Invalid signature parameters", e); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
185 |
} catch (ProviderException e) { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
186 |
throw new SignatureException("Error parsing signature parameters", |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
187 |
e.getCause()); |
2 | 188 |
} |
189 |
} |
|
190 |
||
191 |
/** |
|
192 |
* Create the signed certificate request. This will later be |
|
193 |
* retrieved in either string or binary format. |
|
194 |
* |
|
4350
2a593a20d962
6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents:
2
diff
changeset
|
195 |
* @param subject identifies the signer (by X.500 name). |
2a593a20d962
6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents:
2
diff
changeset
|
196 |
* @param signature private key and signing algorithm to use. |
2 | 197 |
* @exception IOException on errors. |
198 |
* @exception CertificateException on certificate handling errors. |
|
199 |
* @exception SignatureException on signature handling errors. |
|
200 |
*/ |
|
4350
2a593a20d962
6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents:
2
diff
changeset
|
201 |
public void encodeAndSign(X500Name subject, Signature signature) |
2 | 202 |
throws CertificateException, IOException, SignatureException { |
203 |
DerOutputStream out, scratch; |
|
204 |
byte[] certificateRequestInfo; |
|
205 |
byte[] sig; |
|
206 |
||
207 |
if (encoded != null) |
|
208 |
throw new SignatureException("request is already signed"); |
|
209 |
||
4350
2a593a20d962
6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents:
2
diff
changeset
|
210 |
this.subject = subject; |
2 | 211 |
|
212 |
/* |
|
213 |
* Encode cert request info, wrap in a sequence for signing |
|
214 |
*/ |
|
215 |
scratch = new DerOutputStream(); |
|
216 |
scratch.putInteger(BigInteger.ZERO); // PKCS #10 v1.0 |
|
217 |
subject.encode(scratch); // X.500 name |
|
218 |
scratch.write(subjectPublicKeyInfo.getEncoded()); // public key |
|
219 |
attributeSet.encode(scratch); |
|
220 |
||
221 |
out = new DerOutputStream(); |
|
222 |
out.write(DerValue.tag_Sequence, scratch); // wrap it! |
|
223 |
certificateRequestInfo = out.toByteArray(); |
|
224 |
scratch = out; |
|
225 |
||
226 |
/* |
|
227 |
* Sign it ... |
|
228 |
*/ |
|
4350
2a593a20d962
6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents:
2
diff
changeset
|
229 |
signature.update(certificateRequestInfo, 0, |
2 | 230 |
certificateRequestInfo.length); |
4350
2a593a20d962
6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents:
2
diff
changeset
|
231 |
sig = signature.sign(); |
44046
762e807bfac1
8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents:
29596
diff
changeset
|
232 |
sigAlg = signature.getAlgorithm(); |
2 | 233 |
|
234 |
/* |
|
235 |
* Build guts of SIGNED macro |
|
236 |
*/ |
|
4350
2a593a20d962
6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents:
2
diff
changeset
|
237 |
AlgorithmId algId = null; |
2a593a20d962
6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents:
2
diff
changeset
|
238 |
try { |
53351
bdb29aa5fd31
8215694: keytool cannot generate RSASSA-PSS certificates
weijun
parents:
50204
diff
changeset
|
239 |
AlgorithmParameters params = signature.getParameters(); |
bdb29aa5fd31
8215694: keytool cannot generate RSASSA-PSS certificates
weijun
parents:
50204
diff
changeset
|
240 |
algId = params == null |
bdb29aa5fd31
8215694: keytool cannot generate RSASSA-PSS certificates
weijun
parents:
50204
diff
changeset
|
241 |
? AlgorithmId.get(signature.getAlgorithm()) |
bdb29aa5fd31
8215694: keytool cannot generate RSASSA-PSS certificates
weijun
parents:
50204
diff
changeset
|
242 |
: AlgorithmId.get(params); |
4350
2a593a20d962
6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents:
2
diff
changeset
|
243 |
} catch (NoSuchAlgorithmException nsae) { |
2a593a20d962
6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents:
2
diff
changeset
|
244 |
throw new SignatureException(nsae); |
2a593a20d962
6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents:
2
diff
changeset
|
245 |
} |
53351
bdb29aa5fd31
8215694: keytool cannot generate RSASSA-PSS certificates
weijun
parents:
50204
diff
changeset
|
246 |
|
4350
2a593a20d962
6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents:
2
diff
changeset
|
247 |
algId.encode(scratch); // sig algorithm |
2 | 248 |
scratch.putBitString(sig); // sig |
249 |
||
250 |
/* |
|
251 |
* Wrap those guts in a sequence |
|
252 |
*/ |
|
253 |
out = new DerOutputStream(); |
|
254 |
out.write(DerValue.tag_Sequence, scratch); |
|
255 |
encoded = out.toByteArray(); |
|
256 |
} |
|
257 |
||
258 |
/** |
|
259 |
* Returns the subject's name. |
|
260 |
*/ |
|
261 |
public X500Name getSubjectName() { return subject; } |
|
262 |
||
263 |
/** |
|
264 |
* Returns the subject's public key. |
|
265 |
*/ |
|
266 |
public PublicKey getSubjectPublicKeyInfo() |
|
267 |
{ return subjectPublicKeyInfo; } |
|
268 |
||
269 |
/** |
|
44046
762e807bfac1
8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents:
29596
diff
changeset
|
270 |
* Returns the signature algorithm. |
762e807bfac1
8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents:
29596
diff
changeset
|
271 |
*/ |
762e807bfac1
8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents:
29596
diff
changeset
|
272 |
public String getSigAlg() { return sigAlg; } |
762e807bfac1
8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents:
29596
diff
changeset
|
273 |
|
762e807bfac1
8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents:
29596
diff
changeset
|
274 |
/** |
2 | 275 |
* Returns the additional attributes requested. |
276 |
*/ |
|
277 |
public PKCS10Attributes getAttributes() |
|
278 |
{ return attributeSet; } |
|
279 |
||
280 |
/** |
|
281 |
* Returns the encoded and signed certificate request as a |
|
282 |
* DER-encoded byte array. |
|
283 |
* |
|
284 |
* @return the certificate request, or null if encodeAndSign() |
|
285 |
* has not yet been called. |
|
286 |
*/ |
|
287 |
public byte[] getEncoded() { |
|
288 |
if (encoded != null) |
|
289 |
return encoded.clone(); |
|
290 |
else |
|
291 |
return null; |
|
292 |
} |
|
293 |
||
294 |
/** |
|
295 |
* Prints an E-Mailable version of the certificate request on the print |
|
296 |
* stream passed. The format is a common base64 encoded one, supported |
|
297 |
* by most Certificate Authorities because Netscape web servers have |
|
298 |
* used this for some time. Some certificate authorities expect some |
|
299 |
* more information, in particular contact information for the web |
|
300 |
* server administrator. |
|
301 |
* |
|
302 |
* @param out the print stream where the certificate request |
|
303 |
* will be printed. |
|
304 |
* @exception IOException when an output operation failed |
|
305 |
* @exception SignatureException when the certificate request was |
|
306 |
* not yet signed. |
|
307 |
*/ |
|
308 |
public void print(PrintStream out) |
|
309 |
throws IOException, SignatureException { |
|
310 |
if (encoded == null) |
|
311 |
throw new SignatureException("Cert request was not signed"); |
|
312 |
||
313 |
||
29596
70399c7a7f5a
8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
25859
diff
changeset
|
314 |
byte[] CRLF = new byte[] {'\r', '\n'}; |
2 | 315 |
out.println("-----BEGIN NEW CERTIFICATE REQUEST-----"); |
29596
70399c7a7f5a
8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
25859
diff
changeset
|
316 |
out.println(Base64.getMimeEncoder(64, CRLF).encodeToString(encoded)); |
2 | 317 |
out.println("-----END NEW CERTIFICATE REQUEST-----"); |
318 |
} |
|
319 |
||
320 |
/** |
|
321 |
* Provides a short description of this request. |
|
322 |
*/ |
|
323 |
public String toString() { |
|
324 |
return "[PKCS #10 certificate request:\n" |
|
325 |
+ subjectPublicKeyInfo.toString() |
|
326 |
+ " subject: <" + subject + ">" + "\n" |
|
327 |
+ " attributes: " + attributeSet.toString() |
|
328 |
+ "\n]"; |
|
329 |
} |
|
330 |
||
331 |
/** |
|
332 |
* Compares this object for equality with the specified |
|
333 |
* object. If the <code>other</code> object is an |
|
334 |
* <code>instanceof</code> <code>PKCS10</code>, then |
|
335 |
* its encoded form is retrieved and compared with the |
|
336 |
* encoded form of this certificate request. |
|
337 |
* |
|
338 |
* @param other the object to test for equality with this object. |
|
339 |
* @return true iff the encoded forms of the two certificate |
|
340 |
* requests match, false otherwise. |
|
341 |
*/ |
|
342 |
public boolean equals(Object other) { |
|
343 |
if (this == other) |
|
344 |
return true; |
|
345 |
if (!(other instanceof PKCS10)) |
|
346 |
return false; |
|
347 |
if (encoded == null) // not signed yet |
|
348 |
return false; |
|
349 |
byte[] otherEncoded = ((PKCS10)other).getEncoded(); |
|
350 |
if (otherEncoded == null) |
|
351 |
return false; |
|
352 |
||
353 |
return java.util.Arrays.equals(encoded, otherEncoded); |
|
354 |
} |
|
355 |
||
356 |
/** |
|
357 |
* Returns a hashcode value for this certificate request from its |
|
358 |
* encoded form. |
|
359 |
* |
|
360 |
* @return the hashcode value. |
|
361 |
*/ |
|
362 |
public int hashCode() { |
|
363 |
int retval = 0; |
|
364 |
if (encoded != null) |
|
365 |
for (int i = 1; i < encoded.length; i++) |
|
366 |
retval += encoded[i] * i; |
|
367 |
return(retval); |
|
368 |
} |
|
369 |
||
370 |
private X500Name subject; |
|
371 |
private PublicKey subjectPublicKeyInfo; |
|
44046
762e807bfac1
8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents:
29596
diff
changeset
|
372 |
private String sigAlg; |
2 | 373 |
private PKCS10Attributes attributeSet; |
374 |
private byte[] encoded; // signed |
|
375 |
} |