|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 8261779 |
| 27 | + * @summary Check that EncryptedPrivateKeyInfo.getEncoded() calls |
| 28 | + * AlgorithmParameters.getEncoded() when first called |
| 29 | + */ |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.security.AlgorithmParameters; |
| 33 | +import java.security.AlgorithmParametersSpi; |
| 34 | +import java.security.Provider; |
| 35 | +import java.security.spec.AlgorithmParameterSpec; |
| 36 | +import java.security.spec.ECGenParameterSpec; |
| 37 | +import java.security.spec.InvalidParameterSpecException; |
| 38 | +import java.util.Arrays; |
| 39 | +import javax.crypto.EncryptedPrivateKeyInfo; |
| 40 | + |
| 41 | +public class GetEncoded { |
| 42 | + |
| 43 | + public static void main(String[] argv) throws Exception { |
| 44 | + |
| 45 | + AlgorithmParameters params = |
| 46 | + AlgorithmParameters.getInstance("EC", new MyProvider()); |
| 47 | + EncryptedPrivateKeyInfo epki = |
| 48 | + new EncryptedPrivateKeyInfo(params, new byte[] {1, 2, 3, 4}); |
| 49 | + try { |
| 50 | + epki.getEncoded(); |
| 51 | + throw new Exception("Should have thrown IOException"); |
| 52 | + } catch (IOException ioe) { |
| 53 | + // test passed, expected exception |
| 54 | + } |
| 55 | + |
| 56 | + AlgorithmParameters ap1 = AlgorithmParameters.getInstance("EC"); |
| 57 | + EncryptedPrivateKeyInfo epki1 = |
| 58 | + new EncryptedPrivateKeyInfo(ap1, new byte[] {1, 2, 3, 4}); |
| 59 | + ap1.init(new ECGenParameterSpec("secp256r1")); |
| 60 | + |
| 61 | + EncryptedPrivateKeyInfo epki2 = |
| 62 | + new EncryptedPrivateKeyInfo(epki1.getEncoded()); |
| 63 | + |
| 64 | + AlgorithmParameters ap2 = epki2.getAlgParameters(); |
| 65 | + if (ap2 == null || !Arrays.equals(ap1.getEncoded(), ap2.getEncoded())) { |
| 66 | + throw new Exception("AlgorithmParameters are not equal"); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public static class MyProvider extends Provider { |
| 71 | + |
| 72 | + MyProvider() { |
| 73 | + super("MyProvider", "0.0", "My Provider"); |
| 74 | + put("AlgorithmParameters.EC", UnsupportedParameters.class.getName()); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public static class UnsupportedParameters extends AlgorithmParametersSpi { |
| 79 | + |
| 80 | + protected void engineInit(AlgorithmParameterSpec paramSpec) |
| 81 | + throws InvalidParameterSpecException { |
| 82 | + throw new InvalidParameterSpecException("Not supported"); |
| 83 | + } |
| 84 | + protected void engineInit(byte[] params) throws IOException { |
| 85 | + throw new IOException("Not supported"); |
| 86 | + } |
| 87 | + protected void engineInit(byte[] params, String format) throws IOException { |
| 88 | + throw new IOException("Not supported"); |
| 89 | + } |
| 90 | + protected <T extends AlgorithmParameterSpec> T engineGetParameterSpec( |
| 91 | + Class<T> paramSpec) throws InvalidParameterSpecException { |
| 92 | + throw new InvalidParameterSpecException("Not supported"); |
| 93 | + } |
| 94 | + protected byte[] engineGetEncoded() throws IOException { |
| 95 | + throw new IOException("Not supported"); |
| 96 | + } |
| 97 | + protected byte[] engineGetEncoded(String format) throws IOException { |
| 98 | + throw new IOException("Not supported"); |
| 99 | + } |
| 100 | + protected String engineToString() { |
| 101 | + return null; |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments