Path: blob/master/test/jdk/sun/security/ec/xec/XECKeyFormat.java
41155 views
/*1* Copyright (c) 2018, 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 821336326* @summary Check for correct formatting of X25519/X448 private keys27* @library /test/lib28* @modules java.base/sun.security.util29* @run main XECKeyFormat30*/3132import java.security.*;33import java.security.spec.*;34import java.security.interfaces.*;35import java.io.*;36import java.nio.file.*;37import java.math.*;38import java.util.*;3940import java.util.HexFormat;4142import sun.security.util.*;4344public class XECKeyFormat {4546private interface Test {47public void runTest(Provider p) throws Exception;48}4950private static void forEachProvider(Test t, String algName)51throws Exception {5253int tested = 0;54for (Provider p : Security.getProviders()) {55Provider.Service s = p.getService("KeyPairGenerator", algName);56if (s != null) {57t.runTest(p);58tested++;59}60}61if (tested == 0) {62throw new RuntimeException("no service found for " + algName);63}64}6566private static Map<String, String> privKeys = Map.of(67"X25519",68"302e020100300506032b656e0422042010fdf8358b9cda51eb98d2479fb092a80639" +69"bf31c5e7c5ba5000387fbf9c6678",70"X448",71"3046020100300506032b656f043a043880998f387e05852d217c1d715b177c24aa7b" +72"f3f4c3a72223f4983597b9ab2ed4793c30d871c24388b380d80bb36d963f5c276219" +73"b0677fed"74);7576private static List<String> pubKeys = List.of(77"302a300506032b656e03210019bf44096984cdfe8541bac167dc3b96c85086aa30b6" +78"b6cb0c5c38ad703166e1"79);8081public static void main(String[] args) throws Exception {82privKeyTest("X25519");83privKeyTest("X448");84pubKeyTest();85}8687private static void pubKeyTest() throws Exception {88forEachProvider(XECKeyFormat::pubKeyTest, "XDH");89}9091private static void pubKeyTest(Provider p) throws Exception {92for (String s : pubKeys) {93pubKeyTest(p, s);94}95}9697private static void pubKeyTest(Provider p, String key) throws Exception {98// ensure that a properly-formatted key can be read99byte[] encodedKey = HexFormat.of().parseHex(key);100X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey);101KeyFactory kf = KeyFactory.getInstance("XDH", p);102kf.generatePublic(keySpec);103}104105private static void privKeyTest(String algName) throws Exception {106107forEachProvider(p -> privKeyTest(algName, p), algName);108}109110private static void privKeyTest(String algName, Provider p)111throws Exception {112113System.out.println("Testing " + algName + " in " + p.getName());114115// ensure format produced is correct116KeyPairGenerator kpg = KeyPairGenerator.getInstance(algName, p);117KeyPair kp = kpg.generateKeyPair();118PrivateKey priv = kp.getPrivate();119checkPrivKeyFormat(priv.getEncoded());120KeyFactory kf = KeyFactory.getInstance(algName, p);121PKCS8EncodedKeySpec keySpec =122kf.getKeySpec(priv, PKCS8EncodedKeySpec.class);123checkPrivKeyFormat(keySpec.getEncoded());124125// ensure that a properly-formatted key can be read126byte[] encodedKey = HexFormat.of().parseHex(privKeys.get(algName));127keySpec = new PKCS8EncodedKeySpec(encodedKey);128kf.generatePrivate(keySpec);129}130131private static void checkPrivKeyFormat(byte[] key) throws IOException {132// key value should be nested octet strings133DerValue val = new DerValue(new ByteArrayInputStream(key));134BigInteger version = val.data.getBigInteger();135DerValue algId = val.data.getDerValue();136byte[] keyValue = val.data.getOctetString();137val = new DerValue(new ByteArrayInputStream(keyValue));138if (val.tag != DerValue.tag_OctetString) {139throw new RuntimeException("incorrect format");140}141}142}143144145146147