1 /*
2 * Copyright (c) 2003, 2013, 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 7126889
27 * @summary Incorrect SSLEngine debug output
28 *
29 * Debug output was reporting n+1 bytes of data was written when it was
30 * really was n.
31 *
32 * SunJSSE does not support dynamic system properties, no way to re-use
33 * system properties in samevm/agentvm mode.
34 */
35
36 /**
37 * A SSLEngine usage example which simplifies the presentation
38 * by removing the I/O and multi-threading concerns.
39 *
40 * The test creates two SSLEngines, simulating a client and server.
41 * The "transport" layer consists two byte buffers: think of them
42 * as directly connected pipes.
43 *
44 * Note, this is a *very* simple example: real code will be much more
45 * involved. For example, different threading and I/O models could be
46 * used, transport mechanisms could close unexpectedly, and so on.
47 *
48 * When this application runs, notice that several messages
58 * ... wrap() ServerHello/Certificate
59 * unwrap() ... ServerHello/Certificate
60 * wrap() ... ClientKeyExchange
61 * wrap() ... ChangeCipherSpec
62 * wrap() ... Finished
63 * ... unwrap() ClientKeyExchange
64 * ... unwrap() ChangeCipherSpec
65 * ... unwrap() Finished
66 * ... wrap() ChangeCipherSpec
67 * ... wrap() Finished
68 * unwrap() ... ChangeCipherSpec
69 * unwrap() ... Finished
70 */
71
72 import javax.net.ssl.*;
73 import javax.net.ssl.SSLEngineResult.*;
74 import java.io.*;
75 import java.security.*;
76 import java.nio.*;
77
78 public class DebugReportsOneExtraByte {
79
80 /*
81 * Enables logging of the SSLEngine operations.
82 */
83 private static boolean logging = true;
84
85 /*
86 * Enables the JSSE system debugging system property:
87 *
88 * -Djavax.net.debug=all
89 *
90 * This gives a lot of low-level information about operations underway,
91 * including specific handshake messages, and might be best examined
92 * after gaining some familiarity with this application.
93 */
94 private static boolean debug = false;
95
96 private SSLContext sslc;
97
98 private SSLEngine clientEngine; // client Engine
99 private ByteBuffer clientOut; // write side of clientEngine
100 private ByteBuffer clientIn; // read side of clientEngine
101
102 private SSLEngine serverEngine; // server Engine
103 private ByteBuffer serverOut; // write side of serverEngine
104 private ByteBuffer serverIn; // read side of serverEngine
105
106 /*
107 * For data transport, this example uses local ByteBuffers. This
108 * isn't really useful, but the purpose of this example is to show
109 * SSLEngine concepts, not how to do network transport.
110 */
111 private ByteBuffer cTOs; // "reliable" transport client->server
112 private ByteBuffer sTOc; // "reliable" transport server->client
113
114 /*
115 * The following is to set up the keystores.
116 */
117 private static String pathToStores = "../../../../javax/net/ssl/etc";
118 private static String keyStoreFile = "keystore";
119 private static String trustStoreFile = "truststore";
120 private static String passwd = "passphrase";
121
122 private static String keyFilename =
123 System.getProperty("test.src", ".") + "/" + pathToStores +
124 "/" + keyStoreFile;
125 private static String trustFilename =
126 System.getProperty("test.src", ".") + "/" + pathToStores +
127 "/" + trustStoreFile;
128
129 /*
130 * Main entry point for this test.
131 */
132 public static void main(String args[]) throws Exception {
133 if (debug) {
134 System.setProperty("javax.net.debug", "all");
135 }
136
137 DebugReportsOneExtraByte test = new DebugReportsOneExtraByte();
138 test.runTest();
139
140 System.out.println("Test Passed.");
141 }
142
143 /*
144 * Create an initialized SSLContext to use for these tests.
145 */
146 public DebugReportsOneExtraByte() throws Exception {
147
148 KeyStore ks = KeyStore.getInstance("JKS");
149 KeyStore ts = KeyStore.getInstance("JKS");
150
151 char[] passphrase = "passphrase".toCharArray();
152
153 ks.load(new FileInputStream(keyFilename), passphrase);
154 ts.load(new FileInputStream(trustFilename), passphrase);
155
156 KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
157 kmf.init(ks, passphrase);
158
159 TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
160 tmf.init(ts);
|
1 /*
2 * Copyright (c) 2003, 2020, 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 7126889
27 * @summary Incorrect SSLEngine debug output
28 * @library /lib /lib/security
29 * @run main DebugReportsOneExtraByte
30 */
31 /*
32 * Debug output was reporting n+1 bytes of data was written when it was
33 * really was n.
34 *
35 * SunJSSE does not support dynamic system properties, no way to re-use
36 * system properties in samevm/agentvm mode.
37 */
38
39 /**
40 * A SSLEngine usage example which simplifies the presentation
41 * by removing the I/O and multi-threading concerns.
42 *
43 * The test creates two SSLEngines, simulating a client and server.
44 * The "transport" layer consists two byte buffers: think of them
45 * as directly connected pipes.
46 *
47 * Note, this is a *very* simple example: real code will be much more
48 * involved. For example, different threading and I/O models could be
49 * used, transport mechanisms could close unexpectedly, and so on.
50 *
51 * When this application runs, notice that several messages
61 * ... wrap() ServerHello/Certificate
62 * unwrap() ... ServerHello/Certificate
63 * wrap() ... ClientKeyExchange
64 * wrap() ... ChangeCipherSpec
65 * wrap() ... Finished
66 * ... unwrap() ClientKeyExchange
67 * ... unwrap() ChangeCipherSpec
68 * ... unwrap() Finished
69 * ... wrap() ChangeCipherSpec
70 * ... wrap() Finished
71 * unwrap() ... ChangeCipherSpec
72 * unwrap() ... Finished
73 */
74
75 import javax.net.ssl.*;
76 import javax.net.ssl.SSLEngineResult.*;
77 import java.io.*;
78 import java.security.*;
79 import java.nio.*;
80
81 import jdk.test.lib.process.OutputAnalyzer;
82 import jdk.test.lib.process.ProcessTools;
83
84 public class DebugReportsOneExtraByte {
85
86 /*
87 * Enables logging of the SSLEngine operations.
88 */
89 private static boolean logging = true;
90
91 private SSLContext sslc;
92
93 private SSLEngine clientEngine; // client Engine
94 private ByteBuffer clientOut; // write side of clientEngine
95 private ByteBuffer clientIn; // read side of clientEngine
96
97 private SSLEngine serverEngine; // server Engine
98 private ByteBuffer serverOut; // write side of serverEngine
99 private ByteBuffer serverIn; // read side of serverEngine
100
101 /*
102 * For data transport, this example uses local ByteBuffers. This
103 * isn't really useful, but the purpose of this example is to show
104 * SSLEngine concepts, not how to do network transport.
105 */
106 private ByteBuffer cTOs; // "reliable" transport client->server
107 private ByteBuffer sTOc; // "reliable" transport server->client
108
109 /*
110 * The following is to set up the keystores.
111 */
112 private static String pathToStores = "../../../../javax/net/ssl/etc";
113 private static String keyStoreFile = "keystore";
114 private static String trustStoreFile = "truststore";
115 private static String passwd = "passphrase";
116
117 private static String keyFilename =
118 System.getProperty("test.src", ".") + "/" + pathToStores +
119 "/" + keyStoreFile;
120 private static String trustFilename =
121 System.getProperty("test.src", ".") + "/" + pathToStores +
122 "/" + trustStoreFile;
123
124 /*
125 * Main entry point for this test.
126 */
127 public static void main(String args[]) throws Exception {
128
129 if (args.length == 0) {
130 OutputAnalyzer output = ProcessTools.executeTestJvm(
131 "-Dtest.src=" + System.getProperty("test.src"),
132 "-Djavax.net.debug=all", "DebugReportsOneExtraByte", "p");
133 output.shouldContain("WRITE: TLS10 application_data, length = 8");
134
135 System.out.println("Test Passed.");
136 } else {
137 // Re-enable TLSv1 since test depends on it
138 SecurityUtils.removeFromDisabledTlsAlgs("TLSv1");
139
140 DebugReportsOneExtraByte test = new DebugReportsOneExtraByte();
141 test.runTest();
142 }
143 }
144
145 /*
146 * Create an initialized SSLContext to use for these tests.
147 */
148 public DebugReportsOneExtraByte() throws Exception {
149
150 KeyStore ks = KeyStore.getInstance("JKS");
151 KeyStore ts = KeyStore.getInstance("JKS");
152
153 char[] passphrase = "passphrase".toCharArray();
154
155 ks.load(new FileInputStream(keyFilename), passphrase);
156 ts.load(new FileInputStream(trustFilename), passphrase);
157
158 KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
159 kmf.init(ks, passphrase);
160
161 TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
162 tmf.init(ts);
|