|
| 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 8347289 |
| 27 | + * @summary make sure DPS works when non-extractable PRK is provided |
| 28 | + * @library /test/lib /test/jdk/security/unsignedjce |
| 29 | + * @build java.base/javax.crypto.ProviderVerifier |
| 30 | + * @enablePreview |
| 31 | + * @run main/othervm HKDFDelayedPRK |
| 32 | + */ |
| 33 | + |
| 34 | +import jdk.test.lib.Asserts; |
| 35 | + |
| 36 | +import javax.crypto.KDF; |
| 37 | +import javax.crypto.KDFParameters; |
| 38 | +import javax.crypto.KDFSpi; |
| 39 | +import javax.crypto.SecretKey; |
| 40 | +import javax.crypto.spec.HKDFParameterSpec; |
| 41 | +import javax.crypto.spec.SecretKeySpec; |
| 42 | +import java.security.InvalidAlgorithmParameterException; |
| 43 | +import java.security.Provider; |
| 44 | +import java.security.Security; |
| 45 | +import java.security.spec.AlgorithmParameterSpec; |
| 46 | + |
| 47 | +public class HKDFDelayedPRK { |
| 48 | + public static void main(String[] args) throws Exception { |
| 49 | + // This is a fake non-extractable key |
| 50 | + var prk = new SecretKey() { |
| 51 | + @Override |
| 52 | + public String getAlgorithm() { |
| 53 | + return "PRK"; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public String getFormat() { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public byte[] getEncoded() { |
| 63 | + return null; |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + Security.addProvider(new ProviderImpl()); |
| 68 | + var kdf = KDF.getInstance("HKDF-SHA256"); |
| 69 | + kdf.deriveData(HKDFParameterSpec.expandOnly(prk, null, 32)); |
| 70 | + |
| 71 | + // Confirms our own omnipotent impl is selected |
| 72 | + Asserts.assertEquals("P", kdf.getProviderName()); |
| 73 | + } |
| 74 | + |
| 75 | + public static class ProviderImpl extends Provider { |
| 76 | + public ProviderImpl() { |
| 77 | + super("P", "1", "info"); |
| 78 | + put("KDF.HKDF-SHA256", KDFImpl.class.getName()); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // This HKDF impl accepts everything |
| 83 | + public static class KDFImpl extends KDFSpi { |
| 84 | + |
| 85 | + public KDFImpl(KDFParameters params) throws InvalidAlgorithmParameterException { |
| 86 | + super(params); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + protected KDFParameters engineGetParameters() { |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + protected SecretKey engineDeriveKey(String alg, AlgorithmParameterSpec dummy) { |
| 96 | + return new SecretKeySpec(new byte[32], alg); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + protected byte[] engineDeriveData(AlgorithmParameterSpec dummy) { |
| 101 | + return new byte[32]; |
| 102 | + } |
| 103 | + } |
| 104 | +} |
1 commit comments
openjdk-notifier[bot] commentedon Jan 9, 2025
Review
Issues