author | jboes |
Fri, 20 Sep 2019 11:07:52 +0100 | |
changeset 58242 | 94bb65cb37d3 |
parent 53351 | bdb29aa5fd31 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
53351
bdb29aa5fd31
8215694: keytool cannot generate RSASSA-PSS certificates
weijun
parents:
50204
diff
changeset
|
2 |
* Copyright (c) 2001, 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 |
package java.security.spec; |
|
27 |
||
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
28 |
import java.util.Objects; |
2 | 29 |
|
30 |
/** |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
31 |
* This class specifies a parameter spec for RSASSA-PSS signature scheme, |
2 | 32 |
* as defined in the |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
33 |
* <a href="https://tools.ietf.org/rfc/rfc8017.txt">PKCS#1 v2.2</a> standard. |
2 | 34 |
* |
35 |
* <p>Its ASN.1 definition in PKCS#1 standard is described below: |
|
36 |
* <pre> |
|
37 |
* RSASSA-PSS-params ::= SEQUENCE { |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
38 |
* hashAlgorithm [0] HashAlgorithm DEFAULT sha1, |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
39 |
* maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1, |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
40 |
* saltLength [2] INTEGER DEFAULT 20, |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
41 |
* trailerField [3] TrailerField DEFAULT trailerFieldBC(1) |
2 | 42 |
* } |
43 |
* </pre> |
|
44 |
* where |
|
45 |
* <pre> |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
46 |
* HashAlgorithm ::= AlgorithmIdentifier { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
47 |
* {OAEP-PSSDigestAlgorithms} |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
48 |
* } |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
49 |
* MaskGenAlgorithm ::= AlgorithmIdentifier { {PKCS1MGFAlgorithms} } |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
50 |
* TrailerField ::= INTEGER { trailerFieldBC(1) } |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
51 |
* |
2 | 52 |
* OAEP-PSSDigestAlgorithms ALGORITHM-IDENTIFIER ::= { |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
53 |
* { OID id-sha1 PARAMETERS NULL }| |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
54 |
* { OID id-sha224 PARAMETERS NULL }| |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
55 |
* { OID id-sha256 PARAMETERS NULL }| |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
56 |
* { OID id-sha384 PARAMETERS NULL }| |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
57 |
* { OID id-sha512 PARAMETERS NULL }| |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
58 |
* { OID id-sha512-224 PARAMETERS NULL }| |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
59 |
* { OID id-sha512-256 PARAMETERS NULL }, |
2 | 60 |
* ... -- Allows for future expansion -- |
61 |
* } |
|
62 |
* PKCS1MGFAlgorithms ALGORITHM-IDENTIFIER ::= { |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
63 |
* { OID id-mgf1 PARAMETERS HashAlgorithm }, |
2 | 64 |
* ... -- Allows for future expansion -- |
65 |
* } |
|
66 |
* </pre> |
|
67 |
* <p>Note: the PSSParameterSpec.DEFAULT uses the following: |
|
68 |
* message digest -- "SHA-1" |
|
69 |
* mask generation function (mgf) -- "MGF1" |
|
70 |
* parameters for mgf -- MGF1ParameterSpec.SHA1 |
|
71 |
* SaltLength -- 20 |
|
72 |
* TrailerField -- 1 |
|
73 |
* |
|
74 |
* @see MGF1ParameterSpec |
|
75 |
* @see AlgorithmParameterSpec |
|
76 |
* @see java.security.Signature |
|
77 |
* |
|
78 |
* @author Valerie Peng |
|
79 |
* |
|
80 |
* |
|
81 |
* @since 1.4 |
|
82 |
*/ |
|
83 |
||
84 |
public class PSSParameterSpec implements AlgorithmParameterSpec { |
|
85 |
||
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
86 |
private final String mdName; |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
87 |
|
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
88 |
private final String mgfName; |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
89 |
|
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
90 |
private final AlgorithmParameterSpec mgfSpec; |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
91 |
|
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
92 |
private final int saltLen; |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
93 |
|
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
94 |
private final int trailerField; |
2 | 95 |
|
96 |
/** |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
97 |
* The {@code TrailerFieldBC} constant as defined in PKCS#1 |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
98 |
* |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
99 |
* @since 11 |
2 | 100 |
*/ |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
101 |
public static final int TRAILER_FIELD_BC = 1; |
2 | 102 |
|
103 |
/** |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
104 |
* The PSS parameter set with all default values |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
105 |
* |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
106 |
* @since 1.5 |
2 | 107 |
*/ |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
108 |
public static final PSSParameterSpec DEFAULT = new PSSParameterSpec |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
109 |
("SHA-1", "MGF1", MGF1ParameterSpec.SHA1, 20, TRAILER_FIELD_BC); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
110 |
|
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
111 |
|
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
112 |
// disallowed |
2 | 113 |
private PSSParameterSpec() { |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
114 |
throw new RuntimeException("default constructor not allowed"); |
2 | 115 |
} |
116 |
||
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
117 |
|
2 | 118 |
/** |
18552
005e115dc6ee
8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents:
12685
diff
changeset
|
119 |
* Creates a new {@code PSSParameterSpec} as defined in |
2 | 120 |
* the PKCS #1 standard using the specified message digest, |
121 |
* mask generation function, parameters for mask generation |
|
122 |
* function, salt length, and trailer field values. |
|
123 |
* |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
124 |
* @param mdName the algorithm name of the hash function |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
125 |
* @param mgfName the algorithm name of the mask generation function |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
126 |
* @param mgfSpec the parameters for the mask generation function. |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
127 |
* If null is specified, null will be returned by |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
128 |
* getMGFParameters(). |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
129 |
* @param saltLen the length of salt |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
130 |
* @param trailerField the value of the trailer field |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
53351
diff
changeset
|
131 |
* @throws NullPointerException if {@code mdName}, or {@code mgfName} |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
132 |
* is null |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
53351
diff
changeset
|
133 |
* @throws IllegalArgumentException if {@code saltLen} or |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
134 |
* {@code trailerField} is less than 0 |
2 | 135 |
* @since 1.5 |
136 |
*/ |
|
137 |
public PSSParameterSpec(String mdName, String mgfName, |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
138 |
AlgorithmParameterSpec mgfSpec, int saltLen, int trailerField) { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
139 |
Objects.requireNonNull(mdName, "digest algorithm is null"); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
140 |
Objects.requireNonNull(mgfName, |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
141 |
"mask generation function algorithm is null"); |
2 | 142 |
if (saltLen < 0) { |
143 |
throw new IllegalArgumentException("negative saltLen value: " + |
|
144 |
saltLen); |
|
145 |
} |
|
146 |
if (trailerField < 0) { |
|
147 |
throw new IllegalArgumentException("negative trailerField: " + |
|
148 |
trailerField); |
|
149 |
} |
|
150 |
this.mdName = mdName; |
|
151 |
this.mgfName = mgfName; |
|
152 |
this.mgfSpec = mgfSpec; |
|
153 |
this.saltLen = saltLen; |
|
154 |
this.trailerField = trailerField; |
|
155 |
} |
|
156 |
||
157 |
/** |
|
18552
005e115dc6ee
8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents:
12685
diff
changeset
|
158 |
* Creates a new {@code PSSParameterSpec} |
2 | 159 |
* using the specified salt length and other default values as |
160 |
* defined in PKCS#1. |
|
161 |
* |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
162 |
* @param saltLen the length of salt in bytes to be used in PKCS#1 |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
163 |
* PSS encoding |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
53351
diff
changeset
|
164 |
* @throws IllegalArgumentException if {@code saltLen} is |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
165 |
* less than 0 |
2 | 166 |
*/ |
167 |
public PSSParameterSpec(int saltLen) { |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
168 |
this("SHA-1", "MGF1", MGF1ParameterSpec.SHA1, saltLen, TRAILER_FIELD_BC); |
2 | 169 |
} |
170 |
||
171 |
/** |
|
172 |
* Returns the message digest algorithm name. |
|
173 |
* |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
174 |
* @return the message digest algorithm name |
2 | 175 |
* @since 1.5 |
176 |
*/ |
|
177 |
public String getDigestAlgorithm() { |
|
178 |
return mdName; |
|
179 |
} |
|
180 |
||
181 |
/** |
|
182 |
* Returns the mask generation function algorithm name. |
|
183 |
* |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
184 |
* @return the mask generation function algorithm name |
2 | 185 |
* |
186 |
* @since 1.5 |
|
187 |
*/ |
|
188 |
public String getMGFAlgorithm() { |
|
189 |
return mgfName; |
|
190 |
} |
|
191 |
||
192 |
/** |
|
193 |
* Returns the parameters for the mask generation function. |
|
194 |
* |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
195 |
* @return the parameters for the mask generation function |
2 | 196 |
* @since 1.5 |
197 |
*/ |
|
198 |
public AlgorithmParameterSpec getMGFParameters() { |
|
199 |
return mgfSpec; |
|
200 |
} |
|
201 |
||
202 |
/** |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
203 |
* Returns the salt length in bytes. |
2 | 204 |
* |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
205 |
* @return the salt length |
2 | 206 |
*/ |
207 |
public int getSaltLength() { |
|
208 |
return saltLen; |
|
209 |
} |
|
210 |
||
211 |
/** |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
212 |
* Returns the value for the trailer field. |
2 | 213 |
* |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
214 |
* @return the value for the trailer field |
2 | 215 |
* @since 1.5 |
216 |
*/ |
|
217 |
public int getTrailerField() { |
|
218 |
return trailerField; |
|
219 |
} |
|
53351
bdb29aa5fd31
8215694: keytool cannot generate RSASSA-PSS certificates
weijun
parents:
50204
diff
changeset
|
220 |
|
bdb29aa5fd31
8215694: keytool cannot generate RSASSA-PSS certificates
weijun
parents:
50204
diff
changeset
|
221 |
@Override |
bdb29aa5fd31
8215694: keytool cannot generate RSASSA-PSS certificates
weijun
parents:
50204
diff
changeset
|
222 |
public String toString() { |
bdb29aa5fd31
8215694: keytool cannot generate RSASSA-PSS certificates
weijun
parents:
50204
diff
changeset
|
223 |
StringBuilder sb = new StringBuilder(); |
bdb29aa5fd31
8215694: keytool cannot generate RSASSA-PSS certificates
weijun
parents:
50204
diff
changeset
|
224 |
sb.append("MD: " + mdName + "\n") |
bdb29aa5fd31
8215694: keytool cannot generate RSASSA-PSS certificates
weijun
parents:
50204
diff
changeset
|
225 |
.append("MGF: " + mgfSpec + "\n") |
bdb29aa5fd31
8215694: keytool cannot generate RSASSA-PSS certificates
weijun
parents:
50204
diff
changeset
|
226 |
.append("SaltLength: " + saltLen + "\n") |
bdb29aa5fd31
8215694: keytool cannot generate RSASSA-PSS certificates
weijun
parents:
50204
diff
changeset
|
227 |
.append("TrailerField: " + trailerField + "\n"); |
bdb29aa5fd31
8215694: keytool cannot generate RSASSA-PSS certificates
weijun
parents:
50204
diff
changeset
|
228 |
return sb.toString(); |
bdb29aa5fd31
8215694: keytool cannot generate RSASSA-PSS certificates
weijun
parents:
50204
diff
changeset
|
229 |
} |
2 | 230 |
} |