Path: blob/master/test/jdk/sun/security/mscapi/VeryLongAlias.java
41152 views
/*1* Copyright (c) 2019, 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 8223063 815300526* @requires os.family == "windows"27* @library /test/lib28* @summary Support CNG RSA keys29*/3031import jdk.test.lib.SecurityTools;32import jdk.test.lib.process.ProcessTools;3334import java.io.File;35import java.security.KeyStore;36import java.security.MessageDigest;37import java.security.PrivateKey;38import java.security.PublicKey;39import java.security.Signature;40import java.security.cert.X509Certificate;41import java.util.List;42import java.util.Random;4344public class VeryLongAlias {4546static String alias = String.format("%0512d", new Random().nextInt(100000));4748public static void main(String[] args) throws Throwable {4950// Using the old algorithms to make sure the file is recognized51// by the certutil command on old versions of Windows.52SecurityTools.keytool(53"-J-Dkeystore.pkcs12.legacy"54+ " -genkeypair -storetype pkcs12 -keystore ks"55+ " -storepass changeit -keyalg RSA -dname CN=A -alias "56+ alias);57String id = ((X509Certificate)KeyStore.getInstance(58new File("ks"), "changeit".toCharArray())59.getCertificate(alias)).getSerialNumber().toString(16);60try {61// Importing pkcs12 file. Long alias is only supported by CNG.62ProcessTools.executeCommand("certutil", "-v", "-p", "changeit",63"-csp", "Microsoft Software Key Storage Provider",64"-user", "-importpfx", "MY", "ks", "NoRoot,NoExport")65.shouldHaveExitValue(0);66test();67} finally {68ProcessTools.executeCommand("certutil", "-user", "-delstore", "MY",69id);70}71}7273static void test() throws Exception {7475char[] pass = "changeit".toCharArray();7677KeyStore k1 = KeyStore.getInstance("Windows-MY");78k1.load(null, null);7980KeyStore k2 = KeyStore.getInstance(new File("ks"), pass);8182PrivateKey p1 = (PrivateKey)k1.getKey(alias, null);83PublicKey u1 = k1.getCertificate(alias).getPublicKey();8485PrivateKey p2 = (PrivateKey)k2.getKey(alias, pass);86PublicKey u2 = k2.getCertificate(alias).getPublicKey();8788System.out.println(p1.toString());89System.out.println(u1.toString());90if (!p1.toString().contains("type=CNG")) {91throw new Exception("Not a CNG key");92}9394testSignature(p1, u1);95testSignature(p1, u2);96testSignature(p2, u1);97testSignature(p2, u2);98}99100static void testSignature(PrivateKey p, PublicKey u) throws Exception {101byte[] data = "hello".getBytes();102for (String alg : List.of(103"NONEwithRSA", "SHA1withRSA",104"SHA256withRSA", "SHA512withRSA")) {105if (alg.contains("NONE")) {106data = MessageDigest.getInstance("SHA-256").digest(data);107}108Signature s1 = Signature.getInstance(alg);109Signature s2 = Signature.getInstance(alg);110s1.initSign(p);111s2.initVerify(u);112s1.update(data);113s2.update(data);114if (!s2.verify(s1.sign())) {115throw new Exception("Error");116}117}118}119}120121122