Path: blob/master/test/jdk/sun/security/ec/ed/EdECKeyFormat.java
41152 views
/*1* Copyright (c) 2020, 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 816659726* @summary Check for correct formatting of EdDSA keys27* @library /test/lib28* @build jdk.test.lib.Convert29* @modules java.base/sun.security.util30* @run main EdECKeyFormat31*/3233import java.security.*;34import java.security.spec.*;35import java.security.interfaces.*;36import java.io.*;37import java.nio.file.*;38import java.math.*;39import java.util.*;4041import sun.security.util.*;4243public class EdECKeyFormat {4445private interface Test {46public void runTest(Provider p) throws Exception;47}4849private static void forEachProvider(Test t, String algName)50throws Exception {5152int tested = 0;53for (Provider p : Security.getProviders()) {54Provider.Service s = p.getService("KeyPairGenerator", algName);55if (s != null) {56t.runTest(p);57tested++;58}59}60if (tested == 0) {61throw new RuntimeException("no service found for " + algName);62}63}6465private static Map<String, String> privKeys = Map.of(66"Ed25519",67"302e020100300506032b657004220420d4ee72dbf913584ad5b6d8f1f769f8ad3afe" +68"7c28cbf1d4fbe097a88f44755842",69"Ed448",70"3047020100300506032b6571043b043980998f387e05852d217c1d715b177c24aa7b" +71"f3f4c3a72223f4983597b9ab2ed4793c30d871c24388b380d80bb36d963f5c276219" +72"b0677fed00"73);7475private static List<String> pubKeys = List.of(76"302a300506032b657003210019bf44096984cdfe8541bac167dc3b96c85086aa30b6" +77"b6cb0c5c38ad703166e1"78);7980public static void main(String[] args) throws Exception {81privKeyTest("Ed25519");82privKeyTest("Ed448");83pubKeyTest();84}8586private static void pubKeyTest() throws Exception {87forEachProvider(EdECKeyFormat::pubKeyTest, "EdDSA");88}8990private static void pubKeyTest(Provider p) throws Exception {91for (String s : pubKeys) {92pubKeyTest(p, s);93}94}9596private static void pubKeyTest(Provider p, String key) throws Exception {97// ensure that a properly-formatted key can be read98byte[] encodedKey = HexFormat.of().parseHex(key);99X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey);100KeyFactory kf = KeyFactory.getInstance("EdDSA", p);101kf.generatePublic(keySpec);102}103104private static void privKeyTest(String algName) throws Exception {105106forEachProvider(p -> privKeyTest(algName, p), algName);107}108109private static void privKeyTest(String algName, Provider p)110throws Exception {111112System.out.println("Testing " + algName + " in " + p.getName());113114// ensure format produced is correct115KeyPairGenerator kpg = KeyPairGenerator.getInstance(algName, p);116KeyPair kp = kpg.generateKeyPair();117PrivateKey priv = kp.getPrivate();118checkPrivKeyFormat(priv.getEncoded());119KeyFactory kf = KeyFactory.getInstance(algName, p);120PKCS8EncodedKeySpec keySpec =121kf.getKeySpec(priv, PKCS8EncodedKeySpec.class);122checkPrivKeyFormat(keySpec.getEncoded());123124// ensure that a properly-formatted key can be read125byte[] encodedKey = HexFormat.of().parseHex(privKeys.get(algName));126keySpec = new PKCS8EncodedKeySpec(encodedKey);127kf.generatePrivate(keySpec);128}129130private static void checkPrivKeyFormat(byte[] key) throws IOException {131// key value should be nested octet strings132DerValue val = new DerValue(new ByteArrayInputStream(key));133BigInteger version = val.data.getBigInteger();134DerValue algId = val.data.getDerValue();135byte[] keyValue = val.data.getOctetString();136val = new DerValue(new ByteArrayInputStream(keyValue));137if (val.tag != DerValue.tag_OctetString) {138throw new RuntimeException("incorrect format");139}140}141}142143144