Skip to content

Commit 46e3d24

Browse files
author
Valerie Peng
committedMay 12, 2023
8155191: Specify that SecureRandom.nextBytes(byte[]) throws NullPointerException when byte array is null
Reviewed-by: mullan
1 parent 3bf3876 commit 46e3d24

File tree

4 files changed

+97
-14
lines changed

4 files changed

+97
-14
lines changed
 

‎src/java.base/share/classes/java/security/SecureRandom.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -257,9 +257,11 @@ private boolean getThreadSafe() {
257257
* for information about standard RNG algorithm names.
258258
*
259259
* @param seed the seed.
260+
* @throws NullPointerException if {@code seed} is {@code null}
260261
*/
261262
public SecureRandom(byte[] seed) {
262263
super(0);
264+
Objects.requireNonNull(seed);
263265
getDefaultPRNG(true, seed);
264266
this.threadSafe = getThreadSafe();
265267
}
@@ -706,10 +708,12 @@ public SecureRandomParameters getParameters() {
706708
* contains enough entropy for the security of this {@code SecureRandom}.
707709
*
708710
* @param seed the seed.
711+
* @throws NullPointerException if {@code seed} is {@code null}
709712
*
710713
* @see #getSeed
711714
*/
712715
public void setSeed(byte[] seed) {
716+
Objects.requireNonNull(seed);
713717
if (threadSafe) {
714718
secureRandomSpi.engineSetSeed(seed);
715719
} else {
@@ -755,9 +759,11 @@ public void setSeed(long seed) {
755759
* Generates a user-specified number of random bytes.
756760
*
757761
* @param bytes the array to be filled in with random bytes.
762+
* @throws NullPointerException if {@code bytes} is {@code null}
758763
*/
759764
@Override
760765
public void nextBytes(byte[] bytes) {
766+
Objects.requireNonNull(bytes);
761767
if (threadSafe) {
762768
secureRandomSpi.engineNextBytes(bytes);
763769
} else {
@@ -773,7 +779,7 @@ public void nextBytes(byte[] bytes) {
773779
*
774780
* @param bytes the array to be filled in with random bytes
775781
* @param params additional parameters
776-
* @throws NullPointerException if {@code bytes} is null
782+
* @throws NullPointerException if {@code bytes} is {@code null}
777783
* @throws UnsupportedOperationException if the underlying provider
778784
* implementation has not overridden this method
779785
* @throws IllegalArgumentException if {@code params} is {@code null},
@@ -785,13 +791,12 @@ public void nextBytes(byte[] bytes, SecureRandomParameters params) {
785791
if (params == null) {
786792
throw new IllegalArgumentException("params cannot be null");
787793
}
794+
Objects.requireNonNull(bytes);
788795
if (threadSafe) {
789-
secureRandomSpi.engineNextBytes(
790-
Objects.requireNonNull(bytes), params);
796+
secureRandomSpi.engineNextBytes(bytes, params);
791797
} else {
792798
synchronized (this) {
793-
secureRandomSpi.engineNextBytes(
794-
Objects.requireNonNull(bytes), params);
799+
secureRandomSpi.engineNextBytes(bytes, params);
795800
}
796801
}
797802
}

‎src/java.base/share/classes/sun/security/provider/AbstractDrbg.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -338,8 +338,6 @@ protected final void engineNextBytes(byte[] result) {
338338
protected final void engineNextBytes(
339339
byte[] result, SecureRandomParameters params) {
340340

341-
Objects.requireNonNull(result);
342-
343341
if (debug != null) {
344342
debug.println(this, "nextBytes");
345343
}

‎src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11SecureRandom.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -88,9 +88,6 @@ final class P11SecureRandom extends SecureRandomSpi {
8888
// see JCA spec
8989
@Override
9090
protected synchronized void engineSetSeed(byte[] seed) {
91-
if (seed == null) {
92-
throw new NullPointerException("seed must not be null");
93-
}
9491
Session session = null;
9592
try {
9693
session = token.getOpSession();
@@ -120,7 +117,7 @@ protected synchronized void engineSetSeed(byte[] seed) {
120117
// see JCA spec
121118
@Override
122119
protected void engineNextBytes(byte[] bytes) {
123-
if ((bytes == null) || (bytes.length == 0)) {
120+
if (bytes.length == 0) {
124121
return;
125122
}
126123
if (bytes.length <= IBUFFER_SIZE) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2023, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8155191
27+
* @summary check NPE is thrown for various methods of SecureRandom class,
28+
* e.g. SecureRandom(byte[]), nextBytes(byte[]), and setSeed(byte[]).
29+
* @run main NextBytesNull
30+
*/
31+
32+
import java.security.NoSuchAlgorithmException;
33+
import java.security.Provider;
34+
import java.security.SecureRandom;
35+
import java.security.SecureRandomSpi;
36+
37+
public class NextBytesNull {
38+
39+
public static void main(String[] args) throws Exception {
40+
String test = "SecureRandom(null)";
41+
try {
42+
new SecureRandom(null);
43+
throw new RuntimeException("Error: NPE not thrown for " + test);
44+
} catch (NullPointerException e) {
45+
System.out.println("OK, expected NPE thrown for " + test);
46+
}
47+
48+
// verify with an Spi impl which does not throw NPE
49+
SecureRandom sr = SecureRandom.getInstance("S1", new P());
50+
try {
51+
sr.nextBytes(null);
52+
throw new RuntimeException("Error: NPE not thrown");
53+
} catch (NullPointerException npe) {
54+
System.out.println("OK, expected NPE thrown for " + test);
55+
}
56+
try {
57+
sr.setSeed(null);
58+
throw new RuntimeException("Error: NPE not thrown for " + test);
59+
} catch (NullPointerException npe) {
60+
System.out.println("OK, expected NPE thrown for " + test);
61+
}
62+
}
63+
64+
public static final class P extends Provider {
65+
public P() {
66+
super("P", 1.0d, "Test Provider without Null Check");
67+
put("SecureRandom.S1", S.class.getName());
68+
}
69+
}
70+
71+
public static final class S extends SecureRandomSpi {
72+
@Override
73+
protected void engineSetSeed(byte[] seed) {
74+
}
75+
@Override
76+
protected void engineNextBytes(byte[] bytes) {
77+
}
78+
@Override
79+
protected byte[] engineGenerateSeed(int numBytes) {
80+
return new byte[numBytes];
81+
}
82+
}
83+
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on May 12, 2023

@openjdk-notifier[bot]
Please sign in to comment.