Path: blob/master/test/jdk/javax/crypto/EncryptedPrivateKeyInfo/GetEncoded.java
41152 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 826177926* @summary Check that EncryptedPrivateKeyInfo.getEncoded() calls27* AlgorithmParameters.getEncoded() when first called28*/2930import java.io.IOException;31import java.security.AlgorithmParameters;32import java.security.AlgorithmParametersSpi;33import java.security.Provider;34import java.security.spec.AlgorithmParameterSpec;35import java.security.spec.ECGenParameterSpec;36import java.security.spec.InvalidParameterSpecException;37import java.util.Arrays;38import javax.crypto.EncryptedPrivateKeyInfo;3940public class GetEncoded {4142public static void main(String[] argv) throws Exception {4344AlgorithmParameters params =45AlgorithmParameters.getInstance("EC", new MyProvider());46EncryptedPrivateKeyInfo epki =47new EncryptedPrivateKeyInfo(params, new byte[] {1, 2, 3, 4});48try {49epki.getEncoded();50throw new Exception("Should have thrown IOException");51} catch (IOException ioe) {52// test passed, expected exception53}5455AlgorithmParameters ap1 = AlgorithmParameters.getInstance("EC");56EncryptedPrivateKeyInfo epki1 =57new EncryptedPrivateKeyInfo(ap1, new byte[] {1, 2, 3, 4});58ap1.init(new ECGenParameterSpec("secp256r1"));5960EncryptedPrivateKeyInfo epki2 =61new EncryptedPrivateKeyInfo(epki1.getEncoded());6263AlgorithmParameters ap2 = epki2.getAlgParameters();64if (ap2 == null || !Arrays.equals(ap1.getEncoded(), ap2.getEncoded())) {65throw new Exception("AlgorithmParameters are not equal");66}67}6869public static class MyProvider extends Provider {7071MyProvider() {72super("MyProvider", "0.0", "My Provider");73put("AlgorithmParameters.EC", UnsupportedParameters.class.getName());74}75}7677public static class UnsupportedParameters extends AlgorithmParametersSpi {7879protected void engineInit(AlgorithmParameterSpec paramSpec)80throws InvalidParameterSpecException {81throw new InvalidParameterSpecException("Not supported");82}83protected void engineInit(byte[] params) throws IOException {84throw new IOException("Not supported");85}86protected void engineInit(byte[] params, String format) throws IOException {87throw new IOException("Not supported");88}89protected <T extends AlgorithmParameterSpec> T engineGetParameterSpec(90Class<T> paramSpec) throws InvalidParameterSpecException {91throw new InvalidParameterSpecException("Not supported");92}93protected byte[] engineGetEncoded() throws IOException {94throw new IOException("Not supported");95}96protected byte[] engineGetEncoded(String format) throws IOException {97throw new IOException("Not supported");98}99protected String engineToString() {100return null;101}102}103}104105106