Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/mscapi/VeryLongAlias.java
38840 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 822306326* @requires os.family == "windows"27* @library /lib/testlibrary28* @summary Support CNG RSA keys29*/3031import jdk.testlibrary.SecurityTools;32import jdk.testlibrary.ProcessTools;3334import java.io.File;35import java.io.FileInputStream;36import java.security.KeyStore;37import java.security.MessageDigest;38import java.security.PrivateKey;39import java.security.PublicKey;40import java.security.Signature;41import java.security.cert.X509Certificate;42import java.util.List;43import java.util.Random;4445public class VeryLongAlias {4647static String alias = String.format("%0512d", new Random().nextInt(100000));4849public static void main(String[] args) throws Throwable {5051SecurityTools.keytool("-genkeypair -storetype pkcs12 -keystore ks"52+ " -storepass changeit -keyalg RSA -dname CN=A -alias "53+ alias);5455KeyStore ks = KeyStore.getInstance("PKCS12");5657try (FileInputStream fis = new FileInputStream("ks")) {58ks.load(fis, "changeit".toCharArray());59}6061String id = ((X509Certificate)ks.getCertificate(alias))62.getSerialNumber().toString(16);63try {64// Importing pkcs12 file. Long alias is only supported by CNG.65//ProcessTools.executeCommand("certutil", "-v", "-p", "changeit",66// "-csp", "Microsoft Software Key Storage Provider",67// "-user", "-importpfx", "MY", "ks", "NoRoot,NoExport")68// Adapt for Win7 as it only support a subset of above arguments69ProcessTools.executeCommand("certutil", "-v", "-p", "changeit",70"-user", "-importpfx", "ks", "NoRoot")71.shouldHaveExitValue(0);72test();73} finally {74ProcessTools.executeCommand("certutil", "-user", "-delstore", "MY",75id);76}77}7879static void test() throws Exception {8081char[] pass = "changeit".toCharArray();8283KeyStore k1 = KeyStore.getInstance("Windows-MY");84k1.load(null, null);8586KeyStore k2 = KeyStore.getInstance("PKCS12");8788try (FileInputStream fis = new FileInputStream("ks")) {89k2.load(fis, pass);90}9192PrivateKey p1 = (PrivateKey)k1.getKey(alias, null);93PublicKey u1 = k1.getCertificate(alias).getPublicKey();9495PrivateKey p2 = (PrivateKey)k2.getKey(alias, pass);96PublicKey u2 = k2.getCertificate(alias).getPublicKey();9798System.out.println(p1.toString());99System.out.println(u1.toString());100if (!p1.toString().contains("type=CNG")) {101throw new Exception("Not a CNG key");102}103104testSignature(p1, u1);105testSignature(p1, u2);106testSignature(p2, u1);107testSignature(p2, u2);108}109110static void testSignature(PrivateKey p, PublicKey u) throws Exception {111byte[] data = "hello".getBytes();112113String[] ALGS = {114"NONEwithRSA", "SHA1withRSA",115"SHA256withRSA", "SHA512withRSA"116};117118for (String alg : ALGS) {119if (alg.contains("NONE")) {120data = MessageDigest.getInstance("SHA-256").digest(data);121}122Signature s1 = Signature.getInstance(alg);123Signature s2 = Signature.getInstance(alg);124s1.initSign(p);125s2.initVerify(u);126s1.update(data);127s2.update(data);128if (!s2.verify(s1.sign())) {129throw new Exception("Error");130}131}132}133}134135136