1 /* |
|
2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 package jdk.test.lib; |
|
25 |
|
26 import java.security.*; |
|
27 import java.security.spec.*; |
|
28 import java.util.*; |
|
29 |
|
30 public class RSAUtil { |
|
31 |
|
32 public enum SignatureType { |
|
33 RSA_PKCS1_5, |
|
34 RSA_PSS, |
|
35 } |
|
36 |
|
37 // collection of all supported RSA PKCS1.5 algorithms |
|
38 // note that the entries are ordered by required key sizes |
|
39 private static final String[] PKCS1_5_ALGS = { |
|
40 // these requires key size 768 |
|
41 "SHA384withRSA", "SHA512withRSA", |
|
42 // these are supported by min key size 512 |
|
43 "MD2withRSA", "MD5withRSA", "SHA1withRSA", |
|
44 "SHA224withRSA", "SHA512/224withRSA", |
|
45 "SHA256withRSA", "SHA512/256withRSA" |
|
46 }; |
|
47 |
|
48 private static final int PKCS1_5_INDEX_768 = 0; |
|
49 private static final int PKCS1_5_INDEX_512 = 2; |
|
50 |
|
51 // collection of all supported RSA-PSS algorithms |
|
52 // note that the entries are ordered by required key sizes |
|
53 private static final String[] PSS_ALGS = { |
|
54 // these requires key size 2048 |
|
55 "SHA512withRSAandMGF1", |
|
56 // these requires key size 1024 |
|
57 "SHA384withRSAandMGF1", |
|
58 // these requires key size 768 |
|
59 "SHA256withRSAandMGF1", "SHA512/256withRSAandMGF1", |
|
60 // these are supported by min key size 512 |
|
61 "SHA1withRSAandMGF1", "SHA224withRSAandMGF1", |
|
62 "SHA512/224withRSAandMGF1" |
|
63 }; |
|
64 |
|
65 private static final int PSS_INDEX_2048 = 0; |
|
66 private static final int PSS_INDEX_1024 = 1; |
|
67 private static final int PSS_INDEX_768 = 2; |
|
68 private static final int PSS_INDEX_512 = 4; |
|
69 |
|
70 public static Iterable<String> getSignatureAlgorithms(int keysize, |
|
71 SignatureType type) throws RuntimeException { |
|
72 List<String> algos; |
|
73 Iterable<String> result; |
|
74 switch (type) { |
|
75 case RSA_PKCS1_5: |
|
76 algos = new ArrayList<>(Arrays.asList(PKCS1_5_ALGS)); |
|
77 if (keysize >= 768) { |
|
78 result = algos.subList(PKCS1_5_INDEX_768, PKCS1_5_ALGS.length); |
|
79 } else { |
|
80 result = algos.subList(PKCS1_5_INDEX_512, PKCS1_5_ALGS.length); |
|
81 } |
|
82 break; |
|
83 case RSA_PSS: |
|
84 algos = new ArrayList<>(Arrays.asList(PSS_ALGS)); |
|
85 if (keysize >= 2048) { |
|
86 result = algos.subList(PSS_INDEX_2048, PSS_ALGS.length); |
|
87 } else if (keysize >= 1024) { |
|
88 result = algos.subList(PSS_INDEX_1024, PSS_ALGS.length); |
|
89 } else if (keysize >= 768) { |
|
90 result = algos.subList(PSS_INDEX_768, PSS_ALGS.length); |
|
91 } else { |
|
92 result = algos.subList(PSS_INDEX_512, PSS_ALGS.length); |
|
93 } |
|
94 break; |
|
95 default: |
|
96 throw new RuntimeException("Unsupported RSA signature algorithm type: " + type); |
|
97 } |
|
98 return result; |
|
99 } |
|
100 |
|
101 private static final String RSA_SIG_SUFFIX = "withRSA"; |
|
102 private static final String RSAPSS_SIG_SUFFIX = "RSA-PSS"; |
|
103 private static final String RSAPSS_SIG_SUFFIX2 = "withRSAandMGF1"; |
|
104 |
|
105 public static AlgorithmParameterSpec generateDefaultParameter(String sigalg) |
|
106 throws RuntimeException { |
|
107 if (sigalg.regionMatches(true, |
|
108 sigalg.length() - RSA_SIG_SUFFIX.length(), |
|
109 RSA_SIG_SUFFIX, 0, RSA_SIG_SUFFIX.length())) { |
|
110 // RSA PKCS#1.5 signatures do not use parameters |
|
111 return null; |
|
112 } else if (sigalg.regionMatches(true, |
|
113 sigalg.length() - RSAPSS_SIG_SUFFIX.length(), |
|
114 RSAPSS_SIG_SUFFIX, 0, RSAPSS_SIG_SUFFIX.length())) { |
|
115 // no default parameters for RSA-PSS signature. |
|
116 // RSA-PSS signature with different PSS parameters |
|
117 // are tested under Known Answer Tests |
|
118 throw new RuntimeException("No default parameter generation for RSA-PSS"); |
|
119 } else if (sigalg.regionMatches(true, |
|
120 sigalg.length() - RSAPSS_SIG_SUFFIX2.length(), |
|
121 RSAPSS_SIG_SUFFIX2, 0, RSAPSS_SIG_SUFFIX2.length())) { |
|
122 // should be the expanded/friendly name for RSA-PSS |
|
123 // use the same digest algo as in its name and its |
|
124 // output length as the salt length |
|
125 String digest = sigalg.substring(0, |
|
126 sigalg.length() - RSAPSS_SIG_SUFFIX2.length()).toUpperCase(); |
|
127 if (digest.startsWith("SHA") && digest.indexOf("-") == -1) { |
|
128 // convert to standard name |
|
129 digest = "SHA-" + digest.substring(3); |
|
130 } |
|
131 // verify the digest algorithm by trying the getInstance call |
|
132 try { |
|
133 MessageDigest md = MessageDigest.getInstance(digest); |
|
134 System.out.println("Digest in PSS parameter: " + digest); |
|
135 return new PSSParameterSpec(digest, "MGF1", |
|
136 new MGF1ParameterSpec(digest), md.getDigestLength(), 1); |
|
137 } catch (NoSuchAlgorithmException e) { |
|
138 throw new RuntimeException(e); |
|
139 } |
|
140 } else { |
|
141 throw new RuntimeException("Unsupported RSA signature " + sigalg); |
|
142 } |
|
143 } |
|
144 } |
|