Skip to content

Commit

Permalink
8341927: Replace hardcoded security providers with new test.provider.…
Browse files Browse the repository at this point in the history
…name system property

Reviewed-by: mullan, ascarpino, rhalade
  • Loading branch information
Matthew Donovan committed Oct 30, 2024
1 parent 1b177ce commit 9a9ac1d
Show file tree
Hide file tree
Showing 230 changed files with 1,007 additions and 700 deletions.
15 changes: 15 additions & 0 deletions doc/testing.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ <h1 class="title">Testing the JDK</h1>
<li><a href="#non-us-locale" id="toc-non-us-locale">Non-US
locale</a></li>
<li><a href="#pkcs11-tests" id="toc-pkcs11-tests">PKCS11 Tests</a></li>
<li><a href="#testing-with-alternative-security-providers"
id="toc-testing-with-alternative-security-providers">Testing with
alternative security providers</a></li>
<li><a href="#client-ui-tests" id="toc-client-ui-tests">Client UI
Tests</a></li>
</ul></li>
Expand Down Expand Up @@ -586,6 +589,18 @@ <h3 id="pkcs11-tests">PKCS11 Tests</h3>
JTREG=&quot;JAVA_OPTIONS=-Djdk.test.lib.artifacts.nsslib-linux_aarch64=/path/to/NSS-libs&quot;</code></pre>
<p>For more notes about the PKCS11 tests, please refer to
test/jdk/sun/security/pkcs11/README.</p>
<h3 id="testing-with-alternative-security-providers">Testing with
alternative security providers</h3>
<p>Some security tests use a hardcoded provider for
<code>KeyFactory</code>, <code>Cipher</code>,
<code>KeyPairGenerator</code>, <code>KeyGenerator</code>,
<code>AlgorithmParameterGenerator</code>, <code>KeyAgreement</code>,
<code>Mac</code>, <code>MessageDigest</code>, <code>SecureRandom</code>,
<code>Signature</code>, <code>AlgorithmParameters</code>,
<code>Configuration</code>, <code>Policy</code>, or
<code>SecretKeyFactory</code> objects. Specify the
<code>-Dtest.provider.name=NAME</code> property to use a different
provider for the service(s).</p>
<h3 id="client-ui-tests">Client UI Tests</h3>
<h4 id="system-key-shortcuts">System key shortcuts</h4>
<p>Some Client UI tests use key sequences which may be reserved by the
Expand Down
9 changes: 9 additions & 0 deletions doc/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,15 @@ $ make test TEST="jtreg:sun/security/pkcs11/Secmod/AddTrustedCert.java" \
For more notes about the PKCS11 tests, please refer to
test/jdk/sun/security/pkcs11/README.

### Testing with alternative security providers

Some security tests use a hardcoded provider for `KeyFactory`, `Cipher`,
`KeyPairGenerator`, `KeyGenerator`, `AlgorithmParameterGenerator`,
`KeyAgreement`, `Mac`, `MessageDigest`, `SecureRandom`, `Signature`,
`AlgorithmParameters`, `Configuration`, `Policy`, or `SecretKeyFactory` objects.
Specify the `-Dtest.provider.name=NAME` property to use a different provider for
the service(s).

### Client UI Tests

#### System key shortcuts
Expand Down
5 changes: 3 additions & 2 deletions test/jdk/com/sun/crypto/provider/CICO/CICODESFuncTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -66,7 +66,8 @@ public class CICODESFuncTest {
private static final int IV_LENGTH = 8;

public static void main(String[] args) throws Exception {
Provider provider = Security.getProvider("SunJCE");
Provider provider = Security.getProvider(
System.getProperty("test.provider.name", "SunJCE"));
if (provider == null) {
throw new RuntimeException("SunJCE provider does not exist.");
}
Expand Down
7 changes: 4 additions & 3 deletions test/jdk/com/sun/crypto/provider/CICO/CICOSkipTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -204,9 +204,10 @@ private void initCiphers(String algo, SecretKey key,
AlgorithmParameterSpec aps) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException,
InvalidAlgorithmParameterException {
Provider provider = Security.getProvider("SunJCE");
String providerName = System.getProperty("test.provider.name", "SunJCE");
Provider provider = Security.getProvider(providerName);
if (provider == null) {
throw new RuntimeException("SunJCE provider does not exist.");
throw new RuntimeException(providerName + " provider does not exist.");
}
Cipher ci1 = Cipher.getInstance(algo, provider);
ci1.init(Cipher.ENCRYPT_MODE, key, aps);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -69,9 +69,10 @@ public AESPBEWrapper(PBEAlgorithm algo, String passwd)
*/
@Override
protected Cipher initCipher(int mode) throws GeneralSecurityException {
Provider provider = Security.getProvider("SunJCE");
String providerName = System.getProperty("test.provider.name", "SunJCE");
Provider provider = Security.getProvider(providerName);
if (provider == null) {
throw new RuntimeException("SunJCE provider does not exist.");
throw new RuntimeException(providerName + ": provider does not exist.");
}
// get Cipher instance
Cipher ci = Cipher.getInstance(transformation, provider);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -58,9 +58,10 @@ public DefaultPBEWrapper(PBEAlgorithm algo, String passwd) {
*/
@Override
protected Cipher initCipher(int mode) throws GeneralSecurityException {
Provider provider = Security.getProvider("SunJCE");
String providerName = System.getProperty("test.provider.name", "SunJCE");
Provider provider = Security.getProvider(providerName);
if (provider == null) {
throw new RuntimeException("SunJCE provider does not exist.");
throw new RuntimeException(providerName + ": provider does not exist.");
}
SecretKey key = SecretKeyFactory.getInstance(baseAlgo)
.generateSecret(new PBEKeySpec(password.toCharArray()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -89,9 +89,10 @@ public PBKDF2Wrapper(PBEAlgorithm algo, String passwd)
*/
@Override
protected Cipher initCipher(int mode) throws GeneralSecurityException {
Provider provider = Security.getProvider("SunJCE");
String providerName = System.getProperty("test.provider.name", "SunJCE");
Provider provider = Security.getProvider(providerName);
if (provider == null) {
throw new RuntimeException("SunJCE provider does not exist.");
throw new RuntimeException(providerName + ": provider does not exist.");
}
// Generate secret key
PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(),
Expand Down
5 changes: 3 additions & 2 deletions test/jdk/com/sun/crypto/provider/Cipher/AEAD/Encrypt.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -122,7 +122,8 @@ public Encrypt(Provider provider, String algorithm, String mode,
}

public static void main(String[] args) throws Exception {
Provider p = Security.getProvider("SunJCE");
Provider p = Security.getProvider(
System.getProperty("test.provider.name", "SunJCE"));
for (String alg : ALGORITHMS) {
for (int keyStrength : KEY_STRENGTHS) {
if (keyStrength > Cipher.getMaxAllowedKeyLength(alg)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -102,7 +102,8 @@ public class GCMLargeDataKAT {

byte[] encrypt(int inLen) {
try {
cipher = Cipher.getInstance("AES/GCM/NoPadding", "SunJCE");
cipher = Cipher.getInstance("AES/GCM/NoPadding",
System.getProperty("test.provider.name", "SunJCE"));
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
return cipher.doFinal(plaintext, 0, inLen);
} catch (Exception e) {
Expand All @@ -125,7 +126,8 @@ boolean decrypt(byte[] data) {
return false;
}
try {
cipher = Cipher.getInstance("AES/GCM/NoPadding", "SunJCE");
cipher = Cipher.getInstance("AES/GCM/NoPadding",
System.getProperty("test.provider.name", "SunJCE"));
cipher.init(Cipher.DECRYPT_MODE, key, spec);
result = cipher.doFinal(data);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -87,7 +87,8 @@ public GCMParameterSpecTest(int keyLength, int tagLength, int IVlength,
AAD = Helper.generateBytes(AADLength);

// init a secret key
KeyGenerator kg = KeyGenerator.getInstance("AES", "SunJCE");
KeyGenerator kg = KeyGenerator.getInstance("AES",
System.getProperty("test.provider.name", "SunJCE"));
kg.init(keyLength);
key = kg.generateKey();
}
Expand Down Expand Up @@ -211,7 +212,8 @@ private byte[] recoverCipherText(byte[] cipherText, GCMParameterSpec spec)

private Cipher createCipher(int mode, GCMParameterSpec spec)
throws Exception {
Cipher cipher = Cipher.getInstance(TRANSFORMATION, "SunJCE");
Cipher cipher = Cipher.getInstance(TRANSFORMATION,
System.getProperty("test.provider.name", "SunJCE"));
cipher.init(mode, key, spec);
return cipher;
}
Expand Down
4 changes: 2 additions & 2 deletions test/jdk/com/sun/crypto/provider/Cipher/AEAD/KeyWrapper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -37,7 +37,7 @@ public class KeyWrapper {

static final String AES = "AES";
static final String TRANSFORMATION = "AES/GCM/NoPadding";
static final String PROVIDER = "SunJCE";
static final String PROVIDER = System.getProperty("test.provider.name", "SunJCE");
static final int KEY_LENGTH = 128;

public static void main(String argv[]) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -50,7 +50,7 @@ static enum BufferType {
static final int BLOCK = 50;
static final int SAVE = 45;
static final int DISCARD = BLOCK - SAVE;
static final String PROVIDER = "SunJCE";
static final String PROVIDER = System.getProperty("test.provider.name", "SunJCE");
static final String AES = "AES";
static final String GCM = "GCM";
static final String PADDING = "NoPadding";
Expand Down
4 changes: 2 additions & 2 deletions test/jdk/com/sun/crypto/provider/Cipher/AEAD/SameBuffer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -41,7 +41,7 @@
*/
public class SameBuffer {

private static final String PROVIDER = "SunJCE";
private static final String PROVIDER = System.getProperty("test.provider.name", "SunJCE");
private static final String AES = "AES";
private static final String GCM = "GCM";
private static final String PADDING = "NoPadding";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -37,7 +37,7 @@ public class SealedObjectTest {

private static final String AES = "AES";
private static final String TRANSFORMATION = "AES/GCM/NoPadding";
private static final String PROVIDER = "SunJCE";
private static final String PROVIDER = System.getProperty("test.provider.name", "SunJCE");
private static final int KEY_LENGTH = 128;

public static void main(String[] args) throws Exception {
Expand Down
4 changes: 2 additions & 2 deletions test/jdk/com/sun/crypto/provider/Cipher/AEAD/WrongAAD.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -44,7 +44,7 @@
*/
public class WrongAAD {

private static final String PROVIDER = "SunJCE";
private static final String PROVIDER = System.getProperty("test.provider.name", "SunJCE");
private static final String TRANSFORMATION = "AES/GCM/NoPadding";
private static final int TEXT_SIZE = 800;
private static final int KEY_SIZE = 128;
Expand Down
4 changes: 2 additions & 2 deletions test/jdk/com/sun/crypto/provider/Cipher/AES/CICO.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -52,7 +52,7 @@ public class CICO {
"cFB24", "cFB32", "Cfb40", "CFB72", "OfB", "OfB20", "OfB48",
"OfB56", "OFB64", "OFB112", "CFB112", "pCbC" };
private static final String[] PADDING = { "noPadding", "pkcs5padding" };
private static final String PROVIDER = "SunJCE";
private static final String PROVIDER = System.getProperty("test.provider.name", "SunJCE");
private static final int NREADS = 3;
private static final int KEY_LENGTH = 128;

Expand Down
5 changes: 3 additions & 2 deletions test/jdk/com/sun/crypto/provider/Cipher/AES/CTR.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -50,7 +50,8 @@ public class CTR {

private static final String ALGORITHM = "AES";

private static final String PROVIDER = "SunJCE";
private static final String PROVIDER =
System.getProperty("test.provider.name", "SunJCE");

private static final String[] MODES = {"CTR","CFB24","OFB32","GCM"};

Expand Down
5 changes: 3 additions & 2 deletions test/jdk/com/sun/crypto/provider/Cipher/AES/Padding.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -47,7 +47,8 @@
public class Padding {

private static final String ALGORITHM = "AES";
private static final String PROVIDER = "SunJCE";
private static final String PROVIDER =
System.getProperty("test.provider.name", "SunJCE");
private static final String[] MODES_PKCS5PAD = {
"ECb", "CbC", "PCBC", "OFB",
"OFB150", "cFB", "CFB7", "cFB8", "cFB16", "cFB24", "cFB32",
Expand Down
8 changes: 5 additions & 3 deletions test/jdk/com/sun/crypto/provider/Cipher/AES/Test4511676.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -41,10 +41,12 @@ public class Test4511676 {

public boolean execute() throws Exception {

Cipher ci = Cipher.getInstance(ALGO, "SunJCE");
Cipher ci = Cipher.getInstance(ALGO,
System.getProperty("test.provider.name", "SunJCE"));

// TEST FIX 4511676
KeyGenerator kg = KeyGenerator.getInstance(ALGO, "SunJCE");
KeyGenerator kg = KeyGenerator.getInstance(ALGO,
System.getProperty("test.provider.name", "SunJCE"));
kg.init(KEYSIZE*8);
SecretKey key = kg.generateKey();
try {
Expand Down
8 changes: 5 additions & 3 deletions test/jdk/com/sun/crypto/provider/Cipher/AES/Test4512524.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -45,10 +45,12 @@ public class Test4512524 {
public void execute(String mode) throws Exception {

String transformation = ALGO+"/"+mode+"/"+PADDING;
Cipher ci = Cipher.getInstance(transformation, "SunJCE");
Cipher ci = Cipher.getInstance(transformation,
System.getProperty("test.provider.name", "SunJCE"));

// TEST FIX 4512524
KeyGenerator kg = KeyGenerator.getInstance(ALGO, "SunJCE");
KeyGenerator kg = KeyGenerator.getInstance(ALGO,
System.getProperty("test.provider.name", "SunJCE"));
kg.init(KEYSIZE*8);
SecretKey key = kg.generateKey();

Expand Down
Loading

0 comments on commit 9a9ac1d

Please sign in to comment.