|
1 /* |
|
2 * Copyright (c) 2019, 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 /* |
|
25 * @test |
|
26 * @bug 8226374 |
|
27 * @library /javax/net/ssl/templates |
|
28 * @summary Restrict signature algorithms and named groups |
|
29 * @run main/othervm RestrictSignatureScheme |
|
30 */ |
|
31 import java.io.ByteArrayInputStream; |
|
32 import java.security.KeyFactory; |
|
33 import java.security.KeyStore; |
|
34 import java.security.PrivateKey; |
|
35 import java.security.Security; |
|
36 import java.security.cert.Certificate; |
|
37 import java.security.cert.CertificateFactory; |
|
38 import java.security.spec.PKCS8EncodedKeySpec; |
|
39 import java.util.Arrays; |
|
40 import java.util.Base64; |
|
41 import javax.net.ssl.KeyManagerFactory; |
|
42 import javax.net.ssl.SSLContext; |
|
43 import javax.net.ssl.SSLSocket; |
|
44 import javax.net.ssl.SSLServerSocket; |
|
45 import javax.net.ssl.TrustManagerFactory; |
|
46 import javax.net.ssl.SSLException; |
|
47 |
|
48 public class RestrictSignatureScheme extends SSLSocketTemplate { |
|
49 |
|
50 private static volatile int index; |
|
51 private static final String[][][] protocols = { |
|
52 {{"TLSv1.3"}, {"TLSv1.3"}}, |
|
53 {{"TLSv1.3", "TLSv1.2"}, {"TLSv1.2"}}, |
|
54 {{"TLSv1.3", "TLSv1.2"}, {"TLSv1.2"}}, |
|
55 {{"TLSv1.2"}, {"TLSv1.3", "TLSv1.2"}}, |
|
56 {{"TLSv1.2"}, {"TLSv1.2"}} |
|
57 }; |
|
58 |
|
59 private final SSLContext context; |
|
60 RestrictSignatureScheme() throws Exception { |
|
61 this.context = createSSLContext(); |
|
62 } |
|
63 |
|
64 @Override |
|
65 protected SSLContext createClientSSLContext() throws Exception { |
|
66 return context; |
|
67 } |
|
68 |
|
69 @Override |
|
70 protected SSLContext createServerSSLContext() throws Exception { |
|
71 return context; |
|
72 } |
|
73 |
|
74 // Servers are configured before clients, increment test case after. |
|
75 @Override |
|
76 protected void configureClientSocket(SSLSocket socket) { |
|
77 String[] ps = protocols[index][0]; |
|
78 |
|
79 System.out.print("Setting client protocol(s): "); |
|
80 Arrays.stream(ps).forEachOrdered(System.out::print); |
|
81 System.out.println(); |
|
82 |
|
83 socket.setEnabledProtocols(ps); |
|
84 socket.setEnabledCipherSuites(new String[] { |
|
85 "TLS_AES_128_GCM_SHA256", |
|
86 "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"}); |
|
87 } |
|
88 |
|
89 @Override |
|
90 protected void configureServerSocket(SSLServerSocket serverSocket) { |
|
91 String[] ps = protocols[index][1]; |
|
92 |
|
93 System.out.print("Setting server protocol(s): "); |
|
94 Arrays.stream(ps).forEachOrdered(System.out::print); |
|
95 System.out.println(); |
|
96 |
|
97 serverSocket.setEnabledProtocols(ps); |
|
98 serverSocket.setEnabledCipherSuites(new String[] { |
|
99 "TLS_AES_128_GCM_SHA256", |
|
100 "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"}); |
|
101 } |
|
102 |
|
103 /* |
|
104 * Run the test case. |
|
105 */ |
|
106 public static void main(String[] args) throws Exception { |
|
107 Security.setProperty("jdk.tls.disabledAlgorithms", "RSASSA-PSS"); |
|
108 |
|
109 for (index = 0; index < protocols.length; index++) { |
|
110 try { |
|
111 (new RestrictSignatureScheme()).run(); |
|
112 } catch (SSLException | IllegalStateException ssle) { |
|
113 // The named group should be restricted. |
|
114 continue; |
|
115 } |
|
116 |
|
117 throw new Exception("The test case should be disabled"); |
|
118 } |
|
119 } |
|
120 |
|
121 |
|
122 private static final String trustedCertStr = |
|
123 /** |
|
124 * Signature Algorithm: rsassaPss |
|
125 * Issuer: CN = localhost |
|
126 * Validity Not Before: Jun 6 07:11:00 2018 GMT |
|
127 * Not After : Jun 1 07:11:00 2038 GMT |
|
128 * Subject: CN = localhost |
|
129 * Public Key Algorithm: rsassaPss |
|
130 */ |
|
131 "-----BEGIN CERTIFICATE-----\n" |
|
132 + "MIIDZjCCAh2gAwIBAgIUHxwPs3eAgJ057nJwiLgWZWeNqdgwPgYJKoZIhvcNAQEK\n" |
|
133 + "MDGgDTALBglghkgBZQMEAgGhGjAYBgkqhkiG9w0BAQgwCwYJYIZIAWUDBAIBogQC\n" |
|
134 + "AgDeMBQxEjAQBgNVBAMMCWxvY2FsaG9zdDAeFw0xODA2MDYwNzExMDBaFw0zODA2\n" |
|
135 + "MDEwNzExMDBaMBQxEjAQBgNVBAMMCWxvY2FsaG9zdDCCASAwCwYJKoZIhvcNAQEK\n" |
|
136 + "A4IBDwAwggEKAoIBAQCl8r4Qrg27BYUO/1Va2Ix8QPGzN/lvzmKvP5Ff26ovNW4v\n" |
|
137 + "RUx68HzAhhiWtcl+PwLSbJqJreEkTlle7PnRAypby3fO7ZAK0Y3YiHquaBg7d+7Y\n" |
|
138 + "FhhHwv8gG0lZcyA0BkXFJHqdq76qar0xHC6DVezXm0K3mcceymGtFR9BzWmAj+7D\n" |
|
139 + "YsSwvtTQ7WNoQmf0cdDMSM71IwaTwIwvT2wzX1vv5hcdDyXdr64WFqWSA9sNJ2K6\n" |
|
140 + "arxaaU1klwKSgDokF6njafWQ4UxdR67d5W1MYoiioDs2Yy3utsMpO2OUzZVBZNdT\n" |
|
141 + "gkr1jsJhIurpz/5K51lwJIRQBezEFSb+60AFVoMJAgMBAAGjUDBOMB0GA1UdDgQW\n" |
|
142 + "BBQfFit5ilWJmZgCX4QY0HsaI9iIDDAfBgNVHSMEGDAWgBQfFit5ilWJmZgCX4QY\n" |
|
143 + "0HsaI9iIDDAMBgNVHRMEBTADAQH/MD4GCSqGSIb3DQEBCjAxoA0wCwYJYIZIAWUD\n" |
|
144 + "BAIBoRowGAYJKoZIhvcNAQEIMAsGCWCGSAFlAwQCAaIEAgIA3gOCAQEAa4yUQ3gh\n" |
|
145 + "d1YWPdEa1sv2hdkhtenw6m5yxbmaQl2+nIKSpk4RfpXC7K1EYwBF8TdfFbD8hGGh\n" |
|
146 + "5n81BT0/dn1R9SRGCv7KTxx4lfQt31frlsw/tVciwyXQtcUZ6DqfnLP0/aRVLNgx\n" |
|
147 + "zaP542JUHFYLTC3EGz2zUgv70ZUTlIsPG3/p8YO1iXdnYGQyzOuQPUBpI7nS7UtR\n" |
|
148 + "Ug8VE9ACpBxxI3qChMahFZGHlXCCSjSmxpQa6UO4SQl8q5tPNnqdzWwvAW8qkCy4\n" |
|
149 + "6barRQ4sMcGayhHh/uSTx7bcl0FMJpcI1ygbw7/Pc03zKtw0gMTBMns7q4yXjb/u\n" |
|
150 + "ef47nW0t+LRAAg==\n" |
|
151 + "-----END CERTIFICATE-----\n"; |
|
152 |
|
153 private static final String keyCertStr = trustedCertStr; |
|
154 |
|
155 private static final String privateKey = |
|
156 "MIIEuwIBADALBgkqhkiG9w0BAQoEggSnMIIEowIBAAKCAQEApfK+EK4NuwWFDv9V\n" |
|
157 + "WtiMfEDxszf5b85irz+RX9uqLzVuL0VMevB8wIYYlrXJfj8C0myaia3hJE5ZXuz5\n" |
|
158 + "0QMqW8t3zu2QCtGN2Ih6rmgYO3fu2BYYR8L/IBtJWXMgNAZFxSR6nau+qmq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B\n" |
|
173 + "lDeELYECgYAvdzU4UXzofGGJtohb332YwwlaBZP9xJLUcg6K5l+orWVSASMc8XiP\n" |
|
174 + "i3DoRXsYC8GZ4kdBOPlEJ1gA9oaLcPQpIPDSLwlLpLM6Scw4vI822uvnXl/DWxOo\n" |
|
175 + "sM1n7Jj59QLUhGPDhvYpI+/rjC4wcUQe4qR3hMbUKBVnD6u7RsU9iQKBgQCQ17VK\n" |
|
176 + "7bSCRfuRaxaoGADww7gOTv5rQ6qr1xjpxb7D1hFGR9Rc+smCsPB/GZZXQjK44SWj\n" |
|
177 + "WX3ED4Ubzaxmpe4cbNu+O5XMSmWQwB36RFBHUwdE5/nXdqDFzu/qNqJrqZLBmVKP\n" |
|
178 + "ofaiiWffsaytVvotmT6+atElvAMbAua42V+nAQKBgHtIn3mYMHLriYGhQzpkFEA2\n" |
|
179 + "8YcAMlKppueOMAKVy8nLu2r3MidmLAhMiKJQKG45I3Yg0/t/25tXLiOPJlwrOebh\n" |
|
180 + "xQqUBI/JUOIpGAEnr48jhOXnCS+i+z294G5U/RgjXrlR4bCPvrtCmwzWwe0h79w2\n" |
|
181 + "Q2hO5ZTW6UD9CVA85whf"; |
|
182 |
|
183 private static SSLContext createSSLContext() throws Exception { |
|
184 // Generate certificate from cert string |
|
185 CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
|
186 |
|
187 // Create a key store |
|
188 KeyStore ts = KeyStore.getInstance("PKCS12"); |
|
189 KeyStore ks = KeyStore.getInstance("PKCS12"); |
|
190 ts.load(null, null); |
|
191 ks.load(null, null); |
|
192 char passphrase[] = "passphrase".toCharArray(); |
|
193 |
|
194 // Import the trusted cert |
|
195 ts.setCertificateEntry("trusted-cert-RSASSA-PSS", |
|
196 cf.generateCertificate(new ByteArrayInputStream( |
|
197 trustedCertStr.getBytes()))); |
|
198 |
|
199 boolean hasKeyMaterials = keyCertStr != null && privateKey != null; |
|
200 if (hasKeyMaterials) { |
|
201 |
|
202 // Generate the private key. |
|
203 PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec( |
|
204 Base64.getMimeDecoder().decode(privateKey)); |
|
205 KeyFactory kf = KeyFactory.getInstance("RSASSA-PSS"); |
|
206 PrivateKey priKey = kf.generatePrivate(priKeySpec); |
|
207 |
|
208 // Generate certificate chain |
|
209 Certificate keyCert = cf.generateCertificate( |
|
210 new ByteArrayInputStream(keyCertStr.getBytes())); |
|
211 Certificate[] chain = new Certificate[]{keyCert}; |
|
212 |
|
213 // Import the key entry. |
|
214 ks.setKeyEntry("cert-RSASSA-PSS", priKey, passphrase, chain); |
|
215 } |
|
216 |
|
217 // Create SSL context |
|
218 TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX"); |
|
219 tmf.init(ts); |
|
220 |
|
221 SSLContext context = SSLContext.getInstance("TLS"); |
|
222 if (hasKeyMaterials) { |
|
223 KeyManagerFactory kmf = KeyManagerFactory.getInstance("NewSunX509"); |
|
224 kmf.init(ks, passphrase); |
|
225 context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
|
226 } else { |
|
227 context.init(null, tmf.getTrustManagers(), null); |
|
228 } |
|
229 |
|
230 return context; |
|
231 } |
|
232 } |