|
| 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 | +import java.security.KeyPair; |
| 25 | +import java.security.KeyPairGenerator; |
| 26 | +import java.security.Provider; |
| 27 | +import java.security.PrivateKey; |
| 28 | +import javax.crypto.spec.DHParameterSpec; |
| 29 | +import javax.crypto.interfaces.DHPrivateKey; |
| 30 | +import sun.security.util.SecurityProviderConstants; |
| 31 | +import sun.security.provider.ParameterCache; |
| 32 | + |
| 33 | +/** |
| 34 | + * @test |
| 35 | + * @bug 8295425 |
| 36 | + * @modules java.base/sun.security.provider java.base/sun.security.util |
| 37 | + * @library /test/lib .. |
| 38 | + * @run main TestDefaultDHPrivateExpSize |
| 39 | + * @summary This test verifies the DH private exponent size for SunPKCS11 |
| 40 | + * provider. |
| 41 | + */ |
| 42 | + |
| 43 | +public class TestDefaultDHPrivateExpSize extends PKCS11Test { |
| 44 | + |
| 45 | + @Override |
| 46 | + public void main(Provider p) throws Exception { |
| 47 | + System.out.println("Testing " + p.getName()); |
| 48 | + |
| 49 | + if (p.getService("KeyPairGenerator", "DH") == null) { |
| 50 | + System.out.println("Skip, no support for DH KeyPairGenerator"); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", p); |
| 55 | + // try common DH key sizes with built-in primes |
| 56 | + int[] cachedSizes = { 2048, 3072, 4096, 6144, 8192 }; |
| 57 | + for (int ks : cachedSizes) { |
| 58 | + // use keysize which uses JDK default parameters w/ JDK |
| 59 | + // default lSize |
| 60 | + kpg.initialize(ks); |
| 61 | + int expectedL = SecurityProviderConstants.getDefDHPrivateExpSize |
| 62 | + (ParameterCache.getCachedDHParameterSpec(ks)); |
| 63 | + System.out.println("Test against built-in DH " + ks + |
| 64 | + "-bit parameters, expectedL = " + expectedL); |
| 65 | + DHParameterSpec spec = generateAndCheck(kpg, ks, expectedL); |
| 66 | + |
| 67 | + // use custom DH parameters w/o lSize |
| 68 | + DHParameterSpec spec2 = new DHParameterSpec(spec.getP(), |
| 69 | + spec.getG()); |
| 70 | + kpg.initialize(spec2); |
| 71 | + System.out.println("Test against user DH " + ks + |
| 72 | + "-bit parameters, expectedL = " + spec2.getL()); |
| 73 | + |
| 74 | + generateAndCheck(kpg, ks, spec2.getL()); |
| 75 | + |
| 76 | + // use custom DH parameters w/ lSize |
| 77 | + expectedL += 2; |
| 78 | + spec2 = new DHParameterSpec(spec.getP(), spec.getG(), expectedL); |
| 79 | + kpg.initialize(spec2); |
| 80 | + System.out.println("Test against user DH " + ks + |
| 81 | + "-bit parameters, expectedL = " + spec2.getL()); |
| 82 | + generateAndCheck(kpg, ks, expectedL); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // initialize the specified 'kpg' with 'initParam', then check |
| 87 | + // the parameters associated with the generated key against 'initParam' |
| 88 | + // and return the actual private exponent length. |
| 89 | + private static DHParameterSpec generateAndCheck(KeyPairGenerator kpg, |
| 90 | + int expKeySize, int expL) { |
| 91 | + |
| 92 | + DHPrivateKey dhPriv = (DHPrivateKey) kpg.generateKeyPair().getPrivate(); |
| 93 | + DHParameterSpec generated = dhPriv.getParams(); |
| 94 | + // check the params associated with the key as that's what we |
| 95 | + // have control over |
| 96 | + if ((generated.getP().bitLength() != expKeySize) || |
| 97 | + generated.getL()!= expL) { |
| 98 | + new RuntimeException("Error: size check failed, got " + |
| 99 | + generated.getP().bitLength() + " and " + generated.getL()); |
| 100 | + } |
| 101 | + |
| 102 | + // Known NSS Issue/limitation: NSS ignores the supplied L value when |
| 103 | + // generating the DH private key |
| 104 | + int actualL = dhPriv.getX().bitLength(); |
| 105 | + System.out.println("INFO: actual L = " + actualL); |
| 106 | + /* |
| 107 | + if (expLSize != 0 && actualL != expLSize) { |
| 108 | + throw new RuntimeException("ERROR: actual L mismatches, got " |
| 109 | + + actualL + " vs expect " + expLSize); |
| 110 | + } |
| 111 | + */ |
| 112 | + return generated; |
| 113 | + } |
| 114 | + |
| 115 | + public static void main(String[] args) throws Exception { |
| 116 | + main(new TestDefaultDHPrivateExpSize(), args); |
| 117 | + } |
| 118 | +} |
0 commit comments