Path: blob/master/test/jdk/sun/security/rsa/TestRSAOidSupport.java
41149 views
/*1* Copyright (c) 2020, 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 824289726* @summary Ensure that RSA key factory can parse X.509 encodings containing27* non-standard RSA oid as in older JDK releases before JDK-814629328* @run main TestRSAOidSupport29*/3031import java.security.KeyFactory;32import java.security.interfaces.RSAPublicKey;33import java.security.spec.X509EncodedKeySpec;34import java.security.spec.InvalidKeySpecException;3536public class TestRSAOidSupport {3738// SubjectKeyInfo DER encoding w/ Algorithm id 1.3.14.3.2.1539// which can be used to generate RSA Public Key before PSS40// support is added41private static String DER_BYTES =42"3058300906052b0e03020f0500034b003048024100d7157c65e8f22557d8" +43"a857122cfe85bddfaba3064c21b345e2a7cdd8a6751e519ab861c5109fb8" +44"8cce45d161b9817bc0eccdc30fda69e62cc577775f2c1d66bd0203010001";4546// utility method for converting hex string to byte array47static byte[] toByteArray(String s) {48byte[] bytes = new byte[s.length() / 2];49for (int i = 0; i < bytes.length; i++) {50int index = i * 2;51int v = Integer.parseInt(s.substring(index, index + 2), 16);52bytes[i] = (byte) v;53}54return bytes;55}5657public static void main(String[] args) throws Exception {58X509EncodedKeySpec x509Spec = new X509EncodedKeySpec59(toByteArray(DER_BYTES));60String keyAlgo = "RSA";61KeyFactory kf = KeyFactory.getInstance(keyAlgo, "SunRsaSign");62RSAPublicKey rsaKey = (RSAPublicKey) kf.generatePublic(x509Spec);6364if (rsaKey.getAlgorithm() != keyAlgo) {65throw new RuntimeException("Key algo should be " + keyAlgo +66", but got " + rsaKey.getAlgorithm());67}68kf = KeyFactory.getInstance("RSASSA-PSS", "SunRsaSign");69try {70kf.generatePublic(x509Spec);71throw new RuntimeException("Should throw IKSE");72} catch (InvalidKeySpecException ikse) {73System.out.println("Expected IKSE exception thrown");74}75}76}77787980