Path: blob/master/test/jdk/java/security/Provider/SupportsParameter.java
41149 views
/*1* Copyright (c) 2003, 2016, 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 4911081 813018126* @summary verify that Provider.Service.supportsParameter() works27* @author Andreas Sterbenz28*/2930import java.security.*;31import java.security.Provider.Service;3233import javax.crypto.*;34import javax.crypto.spec.SecretKeySpec;3536public class SupportsParameter {3738public static void main(String[] args) throws Exception {39KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");40kpg.initialize(512);41KeyPair kp = kpg.generateKeyPair();42PublicKey dsaPublicKey = kp.getPublic();43PrivateKey dsaPrivateKey = kp.getPrivate();4445PublicKey myPublicKey = new MyPublicKey();46PrivateKey myPrivateKey = new MyPrivateKey();47SecretKey mySecretKey = new MySecretKey();4849Provider p = new MyProvider();50Service s;5152// none specified, always true53s = p.getService("Signature", "DSA0");54checkSupports(s, null, true);55checkSupports(s, dsaPublicKey, true);56checkSupports(s, dsaPrivateKey, true);57checkSupports(s, myPublicKey, true);58checkSupports(s, myPrivateKey, true);59checkSupports(s, mySecretKey, true);6061// DSAPublic/PrivateKey specified as classes62s = p.getService("Signature", "DSA");63checkSupports(s, dsaPublicKey, true);64checkSupports(s, dsaPrivateKey, true);65checkSupports(s, myPublicKey, false);66checkSupports(s, myPrivateKey, false);67checkSupports(s, mySecretKey, false);6869// X.509/PKCS#8 specified as formats70s = p.getService("Signature", "DSA2");71checkSupports(s, dsaPublicKey, true);72checkSupports(s, dsaPrivateKey, true);73checkSupports(s, myPublicKey, false);74checkSupports(s, myPrivateKey, false);75checkSupports(s, mySecretKey, false);7677// MyPublic/PrivateKey + DSAPublicKey specified as classes78s = p.getService("Signature", "DSA3");79checkSupports(s, dsaPublicKey, true);80checkSupports(s, dsaPrivateKey, false);81checkSupports(s, myPublicKey, true);82checkSupports(s, myPrivateKey, true);83checkSupports(s, mySecretKey, false);8485// MyPublic/PrivateKey + DSAPublicKey specified as classes86s = p.getService("Cipher", "DES");87checkSupports(s, dsaPublicKey, false);88checkSupports(s, dsaPrivateKey, false);89checkSupports(s, myPublicKey, false);90checkSupports(s, myPrivateKey, false);91checkSupports(s, mySecretKey, true);92Key secretKeySpec = new SecretKeySpec(new byte[8], "DES");93checkSupports(s, secretKeySpec, true);9495}9697private static void checkSupports(Service s, Key key, boolean r) throws Exception {98if (s.supportsParameter(key) != r) {99throw new Exception("Result mismatch");100}101System.out.println("Passed");102}103104private static class MyProvider extends Provider {105MyProvider() {106super("MyProvider", "1.0", "MyProvider");107108put("Signature.DSA0", "foo.DSA0");109110put("Signature.DSA", "foo.DSA");111put("Signature.DSA SupportedKeyClasses",112"java.security.interfaces.DSAPublicKey" +113"|java.security.interfaces.DSAPrivateKey");114115put("Signature.DSA2", "foo.DSA2");116put("Signature.DSA2 SupportedKeyFormats", "X.509|PKCS#8");117118put("Signature.DSA3", "foo.DSA3");119put("Signature.DSA3 SupportedKeyClasses",120"SupportsParameter$MyPrivateKey" +121"|SupportsParameter$MyPublicKey" +122"|java.security.interfaces.DSAPublicKey");123124put("Cipher.DES", "foo.DES");125put("Cipher.DES SupportedKeyFormats", "RAW");126put("Cipher.DES SupportedKeyClasses", "SupportsParameter$MySecretKey");127}128}129130private static class MyKey implements Key {131public String getAlgorithm() { return "FOO"; }132133public String getFormat() { return null; }134135public byte[] getEncoded() { return null; }136}137138private static class MyPrivateKey extends MyKey implements PrivateKey { }139140private static class MyPublicKey extends MyKey implements PublicKey {}141142private static class MySecretKey extends MyKey implements SecretKey {143public String getAlgorithm() { return "DES"; }144}145146}147148149