Skip to content

Commit e1cda21

Browse files
committed
8349492: Update sun/security/pkcs12/KeytoolOpensslInteropTest.java to use a recent Openssl version
Backport-of: 23469f8de1c6340bc22ef6d9f99740ea9c0e3fe9
1 parent 05cc91a commit e1cda21

File tree

2 files changed

+109
-54
lines changed

2 files changed

+109
-54
lines changed

test/jdk/sun/security/pkcs12/KeytoolOpensslInteropTest.java

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, 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
@@ -22,7 +22,7 @@
2222
*/
2323

2424
/*
25-
* @test
25+
* @test id=GenerateOpensslPKCS12
2626
* @bug 8076190 8242151 8153005 8266182
2727
* @summary This is java keytool <-> openssl interop test. This test generates
2828
* some openssl keystores on the fly, java operates on it and
@@ -31,20 +31,32 @@
3131
* Note: This test executes some openssl command, so need to set
3232
* openssl path using system property "test.openssl.path" or it should
3333
* be available in /usr/bin or /usr/local/bin
34-
* Required OpenSSL version : OpenSSL 1.1.*
34+
* Required OpenSSL version : OpensslArtifactFetcher.OPENSSL_BUNDLE_VERSION
3535
*
3636
* @modules java.base/sun.security.pkcs
3737
* java.base/sun.security.util
38-
* @library /test/lib
39-
* @library /sun/security/pkcs11/
40-
* @run main/othervm/timeout=600 KeytoolOpensslInteropTest
38+
* @library /test/lib /sun/security/pkcs11/
39+
* @run main/othervm KeytoolOpensslInteropTest true
40+
*/
41+
42+
/*
43+
* @test id=UseExistingPKCS12
44+
* @bug 8076190 8242151 8153005 8266182
45+
* @summary This is java keytool <-> openssl interop test. This test uses
46+
* the existing PKCS12 files located in ./params dir and java operates on it
47+
*
48+
* @modules java.base/sun.security.pkcs
49+
* java.base/sun.security.util
50+
* @library /test/lib /sun/security/pkcs11/
51+
* @run main/othervm KeytoolOpensslInteropTest false
4152
*/
4253

4354
import jdk.test.lib.Asserts;
4455
import jdk.test.lib.SecurityTools;
4556
import jdk.test.lib.process.ProcessTools;
4657
import jdk.test.lib.process.OutputAnalyzer;
4758
import jdk.test.lib.security.OpensslArtifactFetcher;
59+
import jtreg.SkippedException;
4860

4961
import java.io.File;
5062
import java.io.FileInputStream;
@@ -67,22 +79,25 @@
6779
public class KeytoolOpensslInteropTest {
6880

6981
public static void main(String[] args) throws Throwable {
70-
String opensslPath = OpensslArtifactFetcher.getOpenssl1dot1dotStar();
71-
if (opensslPath != null) {
72-
// if preferred version of openssl is available perform all
73-
// keytool <-> openssl interop tests
74-
generateInitialKeystores(opensslPath);
75-
testWithJavaCommands();
76-
testWithOpensslCommands(opensslPath);
82+
boolean generatePKCS12 = Boolean.parseBoolean(args[0]);
83+
if (generatePKCS12) {
84+
String opensslPath = OpensslArtifactFetcher.getOpensslPath();
85+
if (opensslPath != null) {
86+
// if the current version of openssl is available, perform all
87+
// keytool <-> openssl interop tests
88+
generateInitialKeystores(opensslPath);
89+
testWithJavaCommands();
90+
testWithOpensslCommands(opensslPath);
91+
} else {
92+
String exMsg = "Can't find the version: "
93+
+ OpensslArtifactFetcher.getTestOpensslBundleVersion()
94+
+ " of openssl binary on this machine, please install"
95+
+ " and set openssl path with property 'test.openssl.path'";
96+
throw new SkippedException(exMsg);
97+
}
7798
} else {
78-
// since preferred version of openssl is not available skip all
79-
// openssl command dependent tests with a warning
80-
System.out.println("\n\u001B[31mWarning: Can't find openssl "
81-
+ "(version 1.1.*) binary on this machine, please install"
82-
+ " and set openssl path with property "
83-
+ "'test.openssl.path'. Now running only half portion of "
84-
+ "the test, skipping all tests which depends on openssl "
85-
+ "commands.\u001B[0m\n");
99+
// since this scenario is using preexisting PKCS12, skip all
100+
// openssl command dependent tests
86101
// De-BASE64 textual files in ./params to `pwd`
87102
try (DirectoryStream<Path> stream = Files.newDirectoryStream(
88103
Path.of(System.getProperty("test.src"), "params"),
@@ -103,6 +118,8 @@ public static void main(String[] args) throws Throwable {
103118

104119
private static void generateInitialKeystores(String opensslPath)
105120
throws Throwable {
121+
Path providerPath = OpensslArtifactFetcher.getProviderPath(opensslPath);
122+
106123
keytool("-keystore ks -keyalg ec -genkeypair -storepass"
107124
+ " changeit -alias a -dname CN=A").shouldHaveExitValue(0);
108125

@@ -123,7 +140,8 @@ private static void generateInitialKeystores(String opensslPath)
123140
ProcessTools.executeCommand(opensslPath, "pkcs12", "-export", "-in",
124141
"kandc", "-out", "os4", "-name", "a", "-passout",
125142
"pass:changeit", "-certpbe", "PBE-SHA1-RC4-128", "-keypbe",
126-
"PBE-SHA1-RC4-128", "-macalg", "SHA224")
143+
"PBE-SHA1-RC4-128", "-macalg", "SHA224",
144+
"-legacy", "-provider-path", providerPath.toString())
127145
.shouldHaveExitValue(0);
128146

129147
ProcessTools.executeCommand(opensslPath, "pkcs12", "-export", "-in",
@@ -480,12 +498,14 @@ private static void testWithOpensslCommands(String opensslPath)
480498
output1 = ProcessTools.executeCommand(opensslPath, "pkcs12", "-in",
481499
"ksnopass", "-passin", "pass:changeit", "-info", "-nokeys",
482500
"-nocerts");
483-
output1.shouldNotHaveExitValue(0);
501+
output1.shouldHaveExitValue(0)
502+
.shouldContain("Warning: MAC is absent!");
484503

485504
output1 = ProcessTools.executeCommand(opensslPath, "pkcs12", "-in",
486505
"ksnopass", "-passin", "pass:changeit", "-info", "-nokeys",
487506
"-nocerts", "-nomacver");
488507
output1.shouldHaveExitValue(0)
508+
.shouldNotContain("Warning: MAC is absent!")
489509
.shouldNotContain("PKCS7 Encrypted data:")
490510
.shouldContain("Shrouded Keybag: PBES2, PBKDF2, AES-256-CBC,"
491511
+ " Iteration 10000, PRF hmacWithSHA256")

test/lib/jdk/test/lib/security/OpensslArtifactFetcher.java

Lines changed: 66 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2025, 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
@@ -25,6 +25,7 @@
2525

2626
import java.io.File;
2727

28+
import java.nio.file.Path;
2829
import jdk.test.lib.Platform;
2930
import jdk.test.lib.process.ProcessTools;
3031
import jdk.test.lib.artifacts.Artifact;
@@ -33,43 +34,49 @@
3334

3435
public class OpensslArtifactFetcher {
3536

37+
private static final String OPENSSL_BUNDLE_VERSION = "3.0.14";
38+
private static final String OPENSSL_ORG = "jpg.tests.jdk.openssl";
39+
3640
/**
37-
* Gets the openssl binary path of version 1.1.*
41+
* Gets the openssl binary path of OPENSSL_BUNDLE_VERSION
3842
*
3943
* Openssl selection flow:
4044
1. Check whether property test.openssl.path is set and it's the
41-
preferred version(1.1.*) of openssl, then return that path.
42-
2. Else look for already installed openssl (version 1.1.*) in system
45+
current version of openssl, then return that path.
46+
2. Else look for already installed openssl in system
4347
path /usr/bin/openssl or /usr/local/bin/openssl, then return that
4448
path.
45-
3. Else try to download openssl (version 1.1.*) from the artifactory
49+
3. Else try to download the current version of openssl from the artifactory
4650
and return that path, if download fails then return null.
4751
*
48-
* @return openssl binary path of version 1.1.*
52+
* @return openssl binary path of the current version
4953
*/
50-
public static String getOpenssl1dot1dotStar() {
51-
String version = "1.1.";
52-
String path = getOpensslFromSystemProp(version);
54+
public static String getOpensslPath() {
55+
String path = getOpensslFromSystemProp(OPENSSL_BUNDLE_VERSION);
5356
if (path != null) {
5457
return path;
55-
} else {
56-
path = getDefaultSystemOpensslPath(version);
57-
if (path != null) {
58-
return path;
59-
} else if (Platform.is64bit()) {
60-
if (Platform.isLinux()) {
61-
path = fetchOpenssl(LINUX_X64.class);
62-
} else if (Platform.isOSX()) {
63-
path = fetchOpenssl(MACOSX_X64.class);
64-
} else if (Platform.isWindows()) {
65-
path = fetchOpenssl(WINDOWS_X64.class);
66-
}
67-
if (verifyOpensslVersion(path, version)) {
68-
return path;
69-
}
58+
}
59+
path = getDefaultSystemOpensslPath(OPENSSL_BUNDLE_VERSION);
60+
if (path != null) {
61+
return path;
62+
}
63+
if (Platform.isX64()) {
64+
if (Platform.isLinux()) {
65+
path = fetchOpenssl(LINUX_X64.class);
66+
} else if (Platform.isOSX()) {
67+
path = fetchOpenssl(MACOSX_X64.class);
68+
} else if (Platform.isWindows()) {
69+
path = fetchOpenssl(WINDOWS_X64.class);
70+
}
71+
} else if (Platform.isAArch64()) {
72+
if (Platform.isLinux()) {
73+
path = fetchOpenssl(LINUX_AARCH64.class);
74+
}
75+
if (Platform.isOSX()) {
76+
path = fetchOpenssl(MACOSX_AARCH64.class);
7077
}
7178
}
72-
return null;
79+
return verifyOpensslVersion(path, OPENSSL_BUNDLE_VERSION) ? path : null;
7380
}
7481

7582
private static String getOpensslFromSystemProp(String version) {
@@ -124,24 +131,52 @@ private static String fetchOpenssl(Class<?> clazz) {
124131
return path;
125132
}
126133

134+
// retrieve the provider directory path from <OPENSSL_HOME>/bin/openssl
135+
public static Path getProviderPath(String opensslPath) {
136+
Path openSslRootPath = Path.of(opensslPath).getParent().getParent();
137+
String libDir = "lib";
138+
if (Platform.isX64() && (Platform.isLinux() || Platform.isWindows())) {
139+
libDir = "lib64";
140+
}
141+
return openSslRootPath.resolve(libDir).resolve("ossl-modules");
142+
}
143+
144+
public static String getTestOpensslBundleVersion() {
145+
return OPENSSL_BUNDLE_VERSION;
146+
}
147+
127148
@Artifact(
128-
organization = "jpg.tests.jdk.openssl",
149+
organization = OPENSSL_ORG,
129150
name = "openssl-linux_x64",
130-
revision = "1.1.1g",
151+
revision = OPENSSL_BUNDLE_VERSION,
131152
extension = "zip")
132153
private static class LINUX_X64 { }
133154

134155
@Artifact(
135-
organization = "jpg.tests.jdk.openssl",
156+
organization = OPENSSL_ORG,
157+
name = "openssl-linux_aarch64",
158+
revision = OPENSSL_BUNDLE_VERSION,
159+
extension = "zip")
160+
private static class LINUX_AARCH64{ }
161+
162+
@Artifact(
163+
organization = OPENSSL_ORG,
136164
name = "openssl-macosx_x64",
137-
revision = "1.1.1g",
165+
revision = OPENSSL_BUNDLE_VERSION,
138166
extension = "zip")
139167
private static class MACOSX_X64 { }
140168

141169
@Artifact(
142-
organization = "jpg.tests.jdk.openssl",
170+
organization = OPENSSL_ORG,
171+
name = "openssl-macosx_aarch64",
172+
revision = OPENSSL_BUNDLE_VERSION,
173+
extension = "zip")
174+
private static class MACOSX_AARCH64 { }
175+
176+
@Artifact(
177+
organization = OPENSSL_ORG,
143178
name = "openssl-windows_x64",
144-
revision = "1.1.1g",
179+
revision = OPENSSL_BUNDLE_VERSION,
145180
extension = "zip")
146181
private static class WINDOWS_X64 { }
147182
}

0 commit comments

Comments
 (0)