37796
|
1 |
/*
|
|
2 |
* Copyright (c) 2016, 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.provider;
|
|
27 |
|
|
28 |
import java.security.AccessController;
|
|
29 |
import java.security.DrbgParameters;
|
|
30 |
import java.security.PrivilegedAction;
|
|
31 |
import java.security.SecureRandomParameters;
|
|
32 |
import java.security.SecureRandomSpi;
|
|
33 |
import java.security.Security;
|
|
34 |
import java.util.Locale;
|
|
35 |
import static java.security.DrbgParameters.Capability.*;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* Implement the "SecureRandom.DRBG" algorithm.
|
|
39 |
*
|
|
40 |
* About the default "securerandom.drbg.config" value:
|
|
41 |
*
|
|
42 |
* The default value in java.security is set to "". This is because
|
|
43 |
* the default values of different aspects are dependent (For example,
|
|
44 |
* strength depends on algorithm) and if we write a full string there
|
|
45 |
* it will be difficult to modify one and keep all others legal.
|
|
46 |
*
|
|
47 |
* When changing default values, touch all places including:
|
|
48 |
*
|
|
49 |
* 1. comments of the security property in java.security
|
|
50 |
* 2. Default mech, cap, usedf set in this class
|
|
51 |
* 3. Default algorithm set in final implementation of each mech
|
|
52 |
* 4. Default strength set in AbstractDrbg, but the effective
|
|
53 |
* value can be smaller if an algorithm does not support it.
|
|
54 |
*
|
|
55 |
* The default value is also mentioned in the @implNote part of
|
|
56 |
* {@link DrbgParameters} class.
|
|
57 |
*/
|
|
58 |
public final class DRBG extends SecureRandomSpi {
|
|
59 |
|
|
60 |
private static final String PROP_NAME = "securerandom.drbg.config";
|
|
61 |
|
|
62 |
private static final long serialVersionUID = 9L;
|
|
63 |
|
|
64 |
private final AbstractDrbg impl;
|
|
65 |
|
|
66 |
private final String mechName;
|
|
67 |
|
|
68 |
private final String algorithmName;
|
|
69 |
|
|
70 |
public DRBG(SecureRandomParameters params) {
|
|
71 |
|
|
72 |
// All parameters at unset status (null or -1).
|
|
73 |
|
|
74 |
// Configurable with the "securerandom.drbg.config" security property
|
|
75 |
String mech = null;
|
|
76 |
Boolean usedf = null;
|
|
77 |
String algorithm = null;
|
|
78 |
|
|
79 |
// Default instantiate parameters also configurable with
|
|
80 |
// "securerandom.drbg.config", and can be changed with params
|
|
81 |
// in getInstance("drbg", params)
|
|
82 |
int strength = -1;
|
|
83 |
DrbgParameters.Capability cap = null;
|
|
84 |
byte[] ps = null;
|
|
85 |
|
|
86 |
// Not configurable with public interfaces, but is a part of
|
|
87 |
// MoreDrbgParameters
|
|
88 |
EntropySource es = null;
|
|
89 |
byte[] nonce = null;
|
|
90 |
|
|
91 |
// Can be configured with a security property
|
|
92 |
|
|
93 |
String config = AccessController.doPrivileged((PrivilegedAction<String>)
|
|
94 |
() -> Security.getProperty(PROP_NAME));
|
|
95 |
|
|
96 |
if (config != null && !config.isEmpty()) {
|
|
97 |
for (String part : config.split(",")) {
|
|
98 |
part = part.trim();
|
|
99 |
switch (part.toLowerCase(Locale.ROOT)) {
|
|
100 |
case "":
|
|
101 |
throw new IllegalArgumentException(
|
|
102 |
"aspect in " + PROP_NAME + " cannot be empty");
|
|
103 |
case "pr_and_reseed":
|
|
104 |
checkTwice(cap != null, "capability");
|
|
105 |
cap = PR_AND_RESEED;
|
|
106 |
break;
|
|
107 |
case "reseed_only":
|
|
108 |
checkTwice(cap != null, "capability");
|
|
109 |
cap = RESEED_ONLY;
|
|
110 |
break;
|
|
111 |
case "none":
|
|
112 |
checkTwice(cap != null, "capability");
|
|
113 |
cap = NONE;
|
|
114 |
break;
|
|
115 |
case "hash_drbg":
|
|
116 |
case "hmac_drbg":
|
|
117 |
case "ctr_drbg":
|
|
118 |
checkTwice(mech != null, "mechanism name");
|
|
119 |
mech = part;
|
|
120 |
break;
|
|
121 |
case "no_df":
|
|
122 |
checkTwice(usedf != null, "usedf flag");
|
|
123 |
usedf = false;
|
|
124 |
break;
|
|
125 |
case "use_df":
|
|
126 |
checkTwice(usedf != null, "usedf flag");
|
|
127 |
usedf = true;
|
|
128 |
break;
|
|
129 |
default:
|
|
130 |
// For all other parts of the property, it is
|
|
131 |
// either an algorithm name or a strength
|
|
132 |
try {
|
|
133 |
int tmp = Integer.parseInt(part);
|
|
134 |
if (tmp < 0) {
|
|
135 |
throw new IllegalArgumentException(
|
|
136 |
"strength in " + PROP_NAME +
|
|
137 |
" cannot be negative: " + part);
|
|
138 |
}
|
|
139 |
checkTwice(strength >= 0, "strength");
|
|
140 |
strength = tmp;
|
|
141 |
} catch (NumberFormatException e) {
|
|
142 |
checkTwice(algorithm != null, "algorithm name");
|
|
143 |
algorithm = part;
|
|
144 |
}
|
|
145 |
}
|
|
146 |
}
|
|
147 |
}
|
|
148 |
|
|
149 |
// Can be updated by params
|
|
150 |
|
|
151 |
if (params != null) {
|
|
152 |
// MoreDrbgParameters is used for testing.
|
|
153 |
if (params instanceof MoreDrbgParameters) {
|
|
154 |
MoreDrbgParameters m = (MoreDrbgParameters)params;
|
|
155 |
params = m.config;
|
|
156 |
|
|
157 |
// No need to check null for es and nonce, they are still null
|
|
158 |
es = m.es;
|
|
159 |
nonce = m.nonce;
|
|
160 |
|
|
161 |
if (m.mech != null) {
|
|
162 |
mech = m.mech;
|
|
163 |
}
|
|
164 |
if (m.algorithm != null) {
|
|
165 |
algorithm = m.algorithm;
|
|
166 |
}
|
|
167 |
usedf = m.usedf;
|
|
168 |
}
|
|
169 |
if (params instanceof DrbgParameters.Instantiation) {
|
|
170 |
DrbgParameters.Instantiation dp =
|
|
171 |
(DrbgParameters.Instantiation) params;
|
|
172 |
|
|
173 |
// ps is still null by now
|
|
174 |
ps = dp.getPersonalizationString();
|
|
175 |
|
|
176 |
int tmp = dp.getStrength();
|
|
177 |
if (tmp != -1) {
|
|
178 |
strength = tmp;
|
|
179 |
}
|
|
180 |
cap = dp.getCapability();
|
|
181 |
} else {
|
|
182 |
throw new IllegalArgumentException("Unsupported params: "
|
|
183 |
+ params.getClass());
|
|
184 |
}
|
|
185 |
}
|
|
186 |
|
|
187 |
// Hardcoded defaults.
|
|
188 |
// Remember to sync with "securerandom.drbg.config" in java.security.
|
|
189 |
|
|
190 |
if (cap == null) {
|
|
191 |
cap = NONE;
|
|
192 |
}
|
|
193 |
if (mech == null) {
|
|
194 |
mech = "Hash_DRBG";
|
|
195 |
}
|
|
196 |
if (usedf == null) {
|
|
197 |
usedf = true;
|
|
198 |
}
|
|
199 |
|
|
200 |
MoreDrbgParameters m = new MoreDrbgParameters(
|
|
201 |
es, mech, algorithm, nonce, usedf,
|
|
202 |
DrbgParameters.instantiation(strength, cap, ps));
|
|
203 |
|
|
204 |
switch (mech.toLowerCase(Locale.ROOT)) {
|
|
205 |
case "hash_drbg":
|
|
206 |
impl = new HashDrbg(m);
|
|
207 |
break;
|
|
208 |
case "hmac_drbg":
|
|
209 |
impl = new HmacDrbg(m);
|
|
210 |
break;
|
|
211 |
case "ctr_drbg":
|
|
212 |
impl = new CtrDrbg(m);
|
|
213 |
break;
|
|
214 |
default:
|
|
215 |
throw new IllegalArgumentException("Unsupported mech: " + mech);
|
|
216 |
}
|
|
217 |
|
|
218 |
mechName = mech;
|
|
219 |
algorithmName = impl.algorithm;
|
|
220 |
}
|
|
221 |
|
|
222 |
@Override
|
|
223 |
protected void engineSetSeed(byte[] seed) {
|
|
224 |
impl.engineSetSeed(seed);
|
|
225 |
}
|
|
226 |
|
|
227 |
@Override
|
|
228 |
protected void engineNextBytes(byte[] bytes) {
|
|
229 |
impl.engineNextBytes(bytes);
|
|
230 |
}
|
|
231 |
|
|
232 |
@Override
|
|
233 |
protected byte[] engineGenerateSeed(int numBytes) {
|
|
234 |
return impl.engineGenerateSeed(numBytes);
|
|
235 |
}
|
|
236 |
|
|
237 |
@Override
|
|
238 |
protected void engineNextBytes(
|
|
239 |
byte[] bytes, SecureRandomParameters params) {
|
|
240 |
impl.engineNextBytes(bytes, params);
|
|
241 |
}
|
|
242 |
|
|
243 |
@Override
|
|
244 |
protected void engineReseed(SecureRandomParameters params) {
|
|
245 |
impl.engineReseed(params);
|
|
246 |
}
|
|
247 |
|
|
248 |
@Override
|
|
249 |
protected SecureRandomParameters engineGetParameters() {
|
|
250 |
return impl.engineGetParameters();
|
|
251 |
}
|
|
252 |
|
|
253 |
@Override
|
|
254 |
public String toString() {
|
|
255 |
return impl.toString();
|
|
256 |
}
|
|
257 |
|
|
258 |
/**
|
|
259 |
* Ensures an aspect is not set more than once.
|
|
260 |
*
|
|
261 |
* @param flag true if set more than once
|
|
262 |
* @param name the name of aspect shown in IAE
|
|
263 |
* @throws IllegalArgumentException if it happens
|
|
264 |
*/
|
|
265 |
private static void checkTwice(boolean flag, String name) {
|
|
266 |
if (flag) {
|
|
267 |
throw new IllegalArgumentException(name
|
|
268 |
+ " cannot be provided more than once in " + PROP_NAME);
|
|
269 |
}
|
|
270 |
}
|
|
271 |
}
|