11/*
2- * Copyright (c) 2019, 2020 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2019, 2023 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2424import java .security .interfaces .*;
2525import java .security .spec .*;
2626
27+ import jtreg .SkippedException ;
28+
2729/**
2830 * @test
2931 * @bug 8080462 8226651 8242332
3537 */
3638public class KeyAndParamCheckForPSS extends PKCS11Test {
3739
38- /**
39- * ALGORITHM name, fixed as RSA for PKCS11
40- */
41- private static final String KEYALG = "RSA" ;
4240 private static final String SIGALG = "RSASSA-PSS" ;
4341
4442 public static void main (String [] args ) throws Exception {
4543 main (new KeyAndParamCheckForPSS (), args );
4644 }
4745
46+ private static boolean skipTest = true ;
47+
4848 @ Override
4949 public void main (Provider p ) throws Exception {
50- Signature sig ;
51- try {
52- sig = Signature .getInstance (SIGALG , p );
53- } catch (NoSuchAlgorithmException e ) {
54- System .out .println ("Skip testing RSASSA-PSS" +
55- " due to no support" );
56- return ;
50+ if (!PSSUtil .isSignatureSupported (p )) {
51+ throw new SkippedException ("Skip due to no support for " +
52+ SIGALG );
5753 }
5854
5955 // NOTE: key length >= (digest length + 2) in bytes
@@ -76,27 +72,26 @@ public void main(Provider p) throws Exception {
7672 runTest (p , 1040 , "SHA3-512" , "SHA3-256" );
7773 runTest (p , 1040 , "SHA3-512" , "SHA3-384" );
7874 runTest (p , 1040 , "SHA3-512" , "SHA3-512" );
75+
76+ if (skipTest ) {
77+ throw new SkippedException ("Test Skipped" );
78+ }
7979 }
8080
81- private void runTest (Provider p , int keySize , String hashAlg ,
81+ private static void runTest (Provider p , int keySize , String hashAlg ,
8282 String mgfHashAlg ) throws Exception {
8383
84- // skip further test if this provider does not support hashAlg or
85- // mgfHashAlg
86- try {
87- MessageDigest .getInstance (hashAlg , p );
88- MessageDigest .getInstance (mgfHashAlg , p );
89- } catch (NoSuchAlgorithmException nsae ) {
90- System .out .println ("No support for " + hashAlg + ", skip" );
84+ System .out .println ("Testing " + hashAlg + " and MGF1" + mgfHashAlg );
85+ PSSUtil .AlgoSupport s = PSSUtil .isHashSupported (p , hashAlg , mgfHashAlg );
86+ if (s == PSSUtil .AlgoSupport .NO ) {
87+ System .out .println ("=> Skip; no support" );
9188 return ;
9289 }
9390
94- System . out . println ( "Testing [" + keySize + " " + hashAlg + "]" );
91+ Signature sig = Signature . getInstance ( SIGALG , p );
9592
9693 // create a key pair with the supplied size
97- KeyPairGenerator kpg = KeyPairGenerator .getInstance (KEYALG , p );
98- kpg .initialize (keySize );
99- KeyPair kp = kpg .generateKeyPair ();
94+ KeyPair kp = PSSUtil .generateKeys (p , keySize );
10095
10196 int bigSaltLen = keySize /8 - 14 ;
10297 AlgorithmParameterSpec paramsBad = new PSSParameterSpec (hashAlg ,
@@ -108,58 +103,71 @@ private void runTest(Provider p, int keySize, String hashAlg,
108103 PublicKey pub = kp .getPublic ();
109104
110105 // test#1 - setParameter then initSign
111- Signature sig = Signature .getInstance ("RSASSA-PSS" , p );
112- sig .setParameter (paramsBad );
106+ sig = Signature .getInstance (SIGALG , p );
107+ try {
108+ sig .setParameter (paramsGood );
109+ sig .initSign (priv );
110+ // algorithm support confirmed
111+ skipTest = false ;
112+ } catch (Exception ex ) {
113+ if (s == PSSUtil .AlgoSupport .MAYBE ) {
114+ // confirmed to be unsupported; skip the rest of the test
115+ System .out .println ("=> Skip; no PSS support" );
116+ return ;
117+ } else {
118+ throw new RuntimeException ("Unexpected Exception" , ex );
119+ }
120+ }
121+
122+ sig = Signature .getInstance (SIGALG , p );
113123 try {
124+ sig .setParameter (paramsBad );
114125 sig .initSign (priv );
115126 throw new RuntimeException ("Expected IKE not thrown" );
116127 } catch (InvalidKeyException ike ) {
117- System . out . println ( "test#1: got expected IKE" );
128+ // expected
118129 }
119130
131+ // test#2 - setParameter then initVerify
132+ sig = Signature .getInstance (SIGALG , p );
120133 sig .setParameter (paramsGood );
121- sig .initSign (priv );
122- System .out .println ("test#1: pass" );
134+ sig .initVerify (pub );
123135
124- // test#2 - setParameter then initVerify
125- sig = Signature .getInstance ("RSASSA-PSS" , p );
126- sig .setParameter (paramsBad );
136+ sig = Signature .getInstance (SIGALG , p );
127137 try {
138+ sig .setParameter (paramsBad );
128139 sig .initVerify (pub );
129140 throw new RuntimeException ("Expected IKE not thrown" );
130141 } catch (InvalidKeyException ike ) {
131- System . out . println ( "test#2: got expected IKE" );
142+ // expected
132143 }
133144
134- sig .setParameter (paramsGood );
135- sig .initVerify (pub );
136-
137- System .out .println ("test#2: pass" );
138-
139145 // test#3 - initSign, then setParameter
140- sig = Signature .getInstance ("RSASSA-PSS" , p );
146+ sig = Signature .getInstance (SIGALG , p );
141147 sig .initSign (priv );
148+ sig .setParameter (paramsGood );
149+
150+ sig = Signature .getInstance (SIGALG , p );
142151 try {
152+ sig .initSign (priv );
143153 sig .setParameter (paramsBad );
144154 throw new RuntimeException ("Expected IAPE not thrown" );
145155 } catch (InvalidAlgorithmParameterException iape ) {
146- System . out . println ( "test#3: got expected IAPE" );
156+ // expected
147157 }
148158
149- sig .setParameter (paramsGood );
150- System .out .println ("test#3: pass" );
151-
152159 // test#4 - initVerify, then setParameter
153- sig = Signature .getInstance ("RSASSA-PSS" , p );
160+ sig = Signature .getInstance (SIGALG , p );
161+ sig .setParameter (paramsGood );
154162 sig .initVerify (pub );
163+
164+ sig = Signature .getInstance (SIGALG , p );
155165 try {
166+ sig .initVerify (pub );
156167 sig .setParameter (paramsBad );
157168 throw new RuntimeException ("Expected IAPE not thrown" );
158169 } catch (InvalidAlgorithmParameterException iape ) {
159- System . out . println ( "test#4: got expected IAPE" );
170+ // expected
160171 }
161-
162- sig .setParameter (paramsGood );
163- System .out .println ("test#4: pass" );
164172 }
165173}
0 commit comments