Debian Codegraph @

c/tests/tls_test.cpp

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #include <proton/tls.h>
21
22 #include "pn_test.hpp"
23
24 #ifdef _WIN32
25 #include <errno.h>
26 #else
27 #include <sys/socket.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #endif
31
32 #include <cstring>
33
34 using namespace pn_test;
35 using Catch::Matchers::Contains;
36 using Catch::Matchers::Equals;
37
38 /* Note must be run in the current directory to find certificate files */
39 #define SSL_FILE(NAME) "ssl-certs/" NAME
40 #define SSL_PW(NAME) NAME "pw"
41 /* Windows vs. OpenSSL certificates */
42 #if defined(_WIN32)
43 # define CERTIFICATE(NAME) SSL_FILE(NAME "-certificate.p12")
44 # define SET_CREDENTIALS(DOMAIN, NAME) \
45 pn_tls_config_set_credentials(DOMAIN, SSL_FILE(NAME "-full.p12"), "", SSL_PW(NAME))
46 #else
47 # define CERTIFICATE(NAME) SSL_FILE(NAME "-certificate.pem")
48 # define SET_CREDENTIALS(DOMAIN, NAME) \
49 pn_tls_config_set_credentials(DOMAIN, CERTIFICATE(NAME), SSL_FILE(NAME "-private-key.pem"), SSL_PW(NAME))
50 #endif
51
52 static void reset_rbuf(pn_raw_buffer_t *rb) {
53 memset(rb, 0, sizeof(*rb));
54 }
55
56 static void set_rbuf(pn_raw_buffer_t *rb, char * bytes, uint32_t capacity, uint32_t size) {
57 rb->bytes = bytes;
58 rb->capacity = capacity;
59 rb->size = size;
60 rb->offset = 0;
61 }
62
63 TEST_CASE("handshake and data") {
64 static char cli_data[] = {"sample client data (request)"};
65 static char srv_data[] = {"sample server data (response)"};
66 pn_tls_config_t *client_config = pn_tls_config(PN_TLS_MODE_CLIENT);
67 REQUIRE( client_config );
68 pn_tls_config_t *server_config = pn_tls_config(PN_TLS_MODE_SERVER);
69 REQUIRE( server_config );
70
71 REQUIRE( pn_tls_config_set_trusted_certs(client_config, CERTIFICATE("tserver")) == 0 );
72 REQUIRE(SET_CREDENTIALS(server_config, "tserver") == 0);
73
74 pn_tls_t *cli_tls = pn_tls(client_config);
75 pn_tls_set_peer_hostname(cli_tls, "test_server");
76 pn_tls_t *srv_tls = pn_tls(server_config);
77
78 CHECK( cli_tls != NULL );
79 CHECK( srv_tls != NULL );
80
81 // Config complete on both sides.
82 REQUIRE( pn_tls_start(cli_tls) == 0 );
83 REQUIRE( pn_tls_start(srv_tls) == 0 );
84
85 char wire_bytes[4096]; // encrypted data, sent between client and server
86 char app_bytes[4096]; // plain text data read or written at either peer
87 pn_raw_buffer_t app_buf, wire_buf, rb_array[2];
88 reset_rbuf(&wire_buf);
89 set_rbuf(&wire_buf, wire_bytes, sizeof(wire_bytes), 0);
90 reset_rbuf(&app_buf);
91 set_rbuf(&app_buf, app_bytes, sizeof(app_bytes), 0);
92
93 /* client hello part 1: client side */
94
95 REQUIRE( pn_tls_need_encrypt_output_buffers(cli_tls) == true );
96 REQUIRE( pn_tls_need_encrypt_output_buffers(srv_tls) == false );
97
98 pn_tls_give_encrypt_output_buffers(cli_tls, &wire_buf, 1);
99 REQUIRE( pn_tls_process(cli_tls) == 0 );
100 REQUIRE( pn_tls_take_encrypt_output_buffers(cli_tls, rb_array, 2) == 1 );
101 REQUIRE( rb_array[0].bytes == wire_bytes );
102 REQUIRE( rb_array[0].size < sizeof(wire_bytes) ); // Strictly less expected.
103
104 /* client hello part 2: server side */
105
106 REQUIRE( pn_tls_need_encrypt_output_buffers(srv_tls) == false );
107 REQUIRE( pn_tls_give_decrypt_input_buffers(srv_tls, rb_array, 1) == 1 );
108 REQUIRE( pn_tls_process(srv_tls) == 0 );
109 REQUIRE( pn_tls_need_decrypt_output_buffers(srv_tls) == false ); // nothing yet
110 REQUIRE( pn_tls_take_decrypt_input_buffers(srv_tls, rb_array, 2) == 1 );
111 REQUIRE( rb_array[0].bytes == wire_bytes );
112
113 /* server hello part1: server side */
114 REQUIRE( pn_tls_is_encrypt_output_pending(srv_tls) == true );
115 set_rbuf(&wire_buf, wire_bytes, sizeof(wire_bytes), 0);
116 REQUIRE( pn_tls_give_encrypt_output_buffers(srv_tls, &wire_buf, 1) == 1 );
117 REQUIRE( pn_tls_process(srv_tls) == 0 );
118 REQUIRE( pn_tls_take_encrypt_output_buffers(srv_tls, rb_array, 2) == 1 );
119 REQUIRE( rb_array[0].bytes == wire_bytes );
120 REQUIRE( rb_array[0].size < sizeof(wire_bytes) ); // Strictly less expected.
121
122 REQUIRE( pn_tls_is_secure(cli_tls) == false ); // negotiation incomplete both sides
123 REQUIRE( pn_tls_is_secure(srv_tls) == false );
124
125 /* server hello part2: client side */
126
127 REQUIRE( pn_tls_need_encrypt_output_buffers(cli_tls) == false );
128 REQUIRE( pn_tls_give_decrypt_input_buffers(cli_tls, rb_array, 1) == 1 );
129 REQUIRE( pn_tls_process(cli_tls) == 0 );
130 REQUIRE( pn_tls_take_decrypt_input_buffers(cli_tls, rb_array, 2) == 1 );
131 REQUIRE( rb_array[0].bytes == wire_bytes );
132
133 REQUIRE( pn_tls_is_secure(cli_tls) == true ); // Protocol and certificate acceptable to client
134 REQUIRE( pn_tls_is_secure(srv_tls) == false ); // Server doesn't know yet
135
136 /* client side finish record and application data */
137
138 REQUIRE( pn_tls_need_encrypt_output_buffers(cli_tls) == true );
139 set_rbuf(&wire_buf, wire_bytes, sizeof(wire_bytes), 0);
140 REQUIRE( pn_tls_give_encrypt_output_buffers(cli_tls, &wire_buf, 1) == 1 );
141
142 size_t len = sizeof(cli_data);
143 memcpy(app_bytes, cli_data, len);
144 app_buf.size = len;
145 REQUIRE( pn_tls_give_encrypt_input_buffers(cli_tls, &app_buf, 1) );
146
147 REQUIRE( pn_tls_process(cli_tls) == 0 );
148 REQUIRE( pn_tls_take_encrypt_input_buffers(cli_tls, rb_array, 2) == 1 );
149 REQUIRE( rb_array[0].bytes == app_bytes );
150 REQUIRE( pn_tls_take_encrypt_output_buffers(cli_tls, rb_array, 2) == 1 );
151 REQUIRE( rb_array[0].bytes == wire_bytes );
152 REQUIRE( rb_array[0].size < sizeof(wire_bytes) ); // Strictly less expected.
153
154 /* server side */
155
156 REQUIRE( pn_tls_give_decrypt_input_buffers(srv_tls, rb_array, 1) == 1 );
157 REQUIRE( pn_tls_process(srv_tls) == 0 );
158 REQUIRE( pn_tls_is_secure(srv_tls) == true ); // Handshake complete at server
159 REQUIRE( pn_tls_need_decrypt_output_buffers(srv_tls) == true ); // have client app data
160
161 memset(app_bytes, 0, sizeof(app_bytes));
162 set_rbuf(&app_buf, app_bytes, sizeof(app_bytes), 0);
163 REQUIRE( pn_tls_give_decrypt_output_buffers(srv_tls, &app_buf, 1) == 1 );
164 REQUIRE( pn_tls_process(srv_tls) == 0 );
165 REQUIRE( pn_tls_take_decrypt_output_buffers(srv_tls, rb_array, 2) == 1 );
166 REQUIRE( rb_array[0].bytes == app_bytes );
167 REQUIRE( rb_array[0].size == sizeof(cli_data) );
168 REQUIRE( strncmp(rb_array[0].bytes, cli_data, sizeof(cli_data)) == 0 );
169 REQUIRE( pn_tls_take_decrypt_input_buffers(srv_tls, rb_array, 2) == 1 );
170 REQUIRE( rb_array[0].bytes == wire_bytes );
171
172 memset(app_bytes, 0, sizeof(app_bytes)); // Received client data, send server data
173 set_rbuf(&app_buf, app_bytes, sizeof(app_bytes), 0);
174 len = sizeof(srv_data);
175 memcpy(app_bytes, srv_data, len);
176 app_buf.size = len;
177 REQUIRE( pn_tls_give_encrypt_input_buffers(srv_tls, &app_buf, 1) );
178 set_rbuf(&wire_buf, wire_bytes, sizeof(wire_bytes), 0);
179 REQUIRE( pn_tls_give_encrypt_output_buffers(srv_tls, &wire_buf, 1) == 1 );
180 pn_tls_close_output(srv_tls); // Finished sending.
181 REQUIRE( pn_tls_process(srv_tls) == 0 );
182 REQUIRE( pn_tls_take_encrypt_input_buffers(srv_tls, rb_array, 2) == 1 );
183 REQUIRE( rb_array[0].bytes == app_bytes );
184 REQUIRE( pn_tls_take_encrypt_output_buffers(srv_tls, rb_array, 2) == 1 );
185 REQUIRE( rb_array[0].bytes == wire_bytes );
186 REQUIRE( rb_array[0].size < sizeof(wire_bytes) );
187
188 /* client side: read server data and confirm end of TLS session */
189
190 REQUIRE( pn_tls_is_input_closed(cli_tls) == false );
191 memset(app_bytes, 0, sizeof(app_bytes));
192 set_rbuf(&app_buf, app_bytes, sizeof(app_bytes), 0);
193 REQUIRE( pn_tls_give_decrypt_output_buffers(cli_tls, &app_buf, 1) == 1 );
194 REQUIRE( pn_tls_give_decrypt_input_buffers(cli_tls, rb_array, 1) == 1 );
195 REQUIRE( pn_tls_process(cli_tls) == 0 );
196 REQUIRE( pn_tls_take_decrypt_input_buffers(cli_tls, rb_array, 2) == 1 );
197 REQUIRE( rb_array[0].bytes == wire_bytes );
198 REQUIRE( pn_tls_take_decrypt_output_buffers(cli_tls, rb_array, 2) == 1 );
199 REQUIRE( rb_array[0].bytes == app_bytes );
200 REQUIRE( rb_array[0].size == sizeof(srv_data) );
201 REQUIRE( strncmp(rb_array[0].bytes, srv_data, sizeof(srv_data)) == 0 );
202
203 REQUIRE( pn_tls_is_input_closed(cli_tls) == true );
204 set_rbuf(&wire_buf, wire_bytes, sizeof(wire_bytes), 0);
205 REQUIRE( pn_tls_give_encrypt_output_buffers(cli_tls, &wire_buf, 1) == 1 );
206 pn_tls_close_output(cli_tls); // Initiate symetric closure record
207 REQUIRE( pn_tls_process(cli_tls) == 0 );
208 REQUIRE( pn_tls_take_encrypt_output_buffers(cli_tls, rb_array, 2) == 1 );
209 REQUIRE( rb_array[0].bytes == wire_bytes );
210 REQUIRE( rb_array[0].size < sizeof(wire_bytes) );
211
212 /* server side */
213
214 REQUIRE( pn_tls_is_input_closed(srv_tls) == false );
215 REQUIRE( pn_tls_give_decrypt_input_buffers(srv_tls, rb_array, 1) == 1 );
216 REQUIRE( pn_tls_process(srv_tls) == 0 );
217 REQUIRE( pn_tls_take_decrypt_input_buffers(srv_tls, rb_array, 2) == 1 );
218 REQUIRE( pn_tls_is_input_closed(srv_tls) == false );
219
220 /* clean up */
221
222 pn_tls_stop(cli_tls);
223 REQUIRE( pn_tls_take_encrypt_input_buffers(cli_tls, rb_array, 1) == 0 );
224 REQUIRE( pn_tls_take_encrypt_output_buffers(cli_tls, rb_array, 1) == 0 );
225 REQUIRE( pn_tls_take_decrypt_input_buffers(cli_tls, rb_array, 1) == 0 );
226 REQUIRE( pn_tls_take_decrypt_output_buffers(cli_tls, rb_array, 1) == 0 );
227 pn_tls_free(cli_tls);
228
229 pn_tls_stop(srv_tls);
230 REQUIRE( pn_tls_take_encrypt_input_buffers(srv_tls, rb_array, 1) == 0 );
231 REQUIRE( pn_tls_take_encrypt_output_buffers(srv_tls, rb_array, 1) == 0 );
232 REQUIRE( pn_tls_take_decrypt_input_buffers(srv_tls, rb_array, 1) == 0 );
233 REQUIRE( pn_tls_take_decrypt_output_buffers(srv_tls, rb_array, 1) == 0 );
234 pn_tls_free(srv_tls);
235
236 pn_tls_config_free(client_config);
237 pn_tls_config_free(server_config);
238 }
239