Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/Cipher/PBE/NegativeLength.java
38889 views
1
/*
2
* Copyright (c) 2013, 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 8001596
27
* @summary Incorrect condition check in PBKDF2KeyImpl.java
28
*/
29
30
import java.security.*;
31
import java.security.spec.*;
32
import javax.crypto.*;
33
import javax.crypto.spec.*;
34
import java.lang.reflect.*;
35
36
public class NegativeLength {
37
38
public static void main(String[] args) throws Exception {
39
SecretKeyFactory skf = SecretKeyFactory.getInstance(
40
"PBKDF2WithHmacSHA1", "SunJCE");
41
42
// Create a valid PBEKeySpec
43
PBEKeySpec pbeks = new PBEKeySpec(
44
new char['p'], new byte[1], 1024, 8);
45
46
// Use reflection to set it negative.
47
Class c = pbeks.getClass();
48
Field f = c.getDeclaredField("keyLength");
49
f.setAccessible(true);
50
f.setInt(pbeks, -8);
51
52
System.out.println("pbeks.getKeyLength(): " + pbeks.getKeyLength());
53
54
try {
55
56
// A negative length is clearly wrong, we should get a
57
// InvalidKeySpecException. Anything else is wrong.
58
skf.generateSecret(pbeks);
59
throw new Exception("We shouldn't get here.");
60
} catch (InvalidKeySpecException ike) {
61
// swallow, this is the exception we want.
62
System.out.println("Test Passed.");
63
}
64
}
65
}
66
67
68