|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 8189441 |
| 27 | + * @library /test/lib /test/jdk/sun/security/pkcs11 |
| 28 | + * @summary make sure Generic is accepted by all KeyAgreement implementations |
| 29 | + * @run main Generic builtin |
| 30 | + * @run main/othervm Generic nss |
| 31 | + * @run main/othervm -DCUSTOM_P11_CONFIG_NAME=p11-nss-sensitive.txt Generic nss |
| 32 | + */ |
| 33 | +import jdk.test.lib.Asserts; |
| 34 | + |
| 35 | +import javax.crypto.KeyAgreement; |
| 36 | +import java.security.KeyPairGenerator; |
| 37 | +import java.security.Provider; |
| 38 | +import java.security.Security; |
| 39 | +import java.util.List; |
| 40 | + |
| 41 | +public class Generic { |
| 42 | + |
| 43 | + public static void main(String[] args) throws Exception { |
| 44 | + if (args[0].equals("nss")) { |
| 45 | + test(PKCS11Test.getSunPKCS11(PKCS11Test.getNssConfig())); |
| 46 | + } else { |
| 47 | + for (var p : Security.getProviders()) { |
| 48 | + test(p); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + static void test(Provider p) throws Exception { |
| 54 | + for (var s : p.getServices()) { |
| 55 | + if (s.getType().equalsIgnoreCase("KeyAgreement")) { |
| 56 | + try { |
| 57 | + System.out.println(s.getProvider().getName() + "." + s.getAlgorithm()); |
| 58 | + var g = KeyPairGenerator.getInstance(ka2kpg(s.getAlgorithm()), p); |
| 59 | + var kp1 = g.generateKeyPair(); |
| 60 | + var kp2 = g.generateKeyPair(); |
| 61 | + var ka = KeyAgreement.getInstance(s.getAlgorithm(), s.getProvider()); |
| 62 | + for (var alg : List.of("TlsPremasterSecret", "Generic")) { |
| 63 | + ka.init(kp1.getPrivate()); |
| 64 | + ka.doPhase(kp2.getPublic(), true); |
| 65 | + Asserts.assertEquals( |
| 66 | + ka.generateSecret(alg).getAlgorithm(), alg); |
| 67 | + } |
| 68 | + } catch (Exception e) { |
| 69 | + throw e; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Find key algorithm from KeyAgreement algorithm |
| 76 | + private static String ka2kpg(String ka) { |
| 77 | + return ka.equals("ECDH") ? "EC" : ka; |
| 78 | + } |
| 79 | +} |
0 commit comments