1 From 5ba65051fea0513db0d997f0ab7cafb9826ed74a Mon Sep 17 00:00:00 2001
2 From: William Lyu <William.Lyu@windriver.com>
3 Date: Fri, 20 Oct 2023 16:22:37 -0400
4 Subject: [PATCH] Added handshake history reporting when test fails
6 Upstream-Status: Submitted [https://github.com/openssl/openssl/pull/22481]
8 Signed-off-by: William Lyu <William.Lyu@windriver.com>
10 test/helpers/handshake.c | 137 +++++++++++++++++++++++++++++----------
11 test/helpers/handshake.h | 70 +++++++++++++++++++-
12 test/ssl_test.c | 44 +++++++++++++
13 3 files changed, 217 insertions(+), 34 deletions(-)
15 diff --git a/test/helpers/handshake.c b/test/helpers/handshake.c
16 index f611b3a..5703b48 100644
17 --- a/test/helpers/handshake.c
18 +++ b/test/helpers/handshake.c
20 #include <netinet/sctp.h>
23 +/* Shamelessly copied from test/helpers/ssl_test_ctx.c */
24 +/* Maps string names to various enumeration type */
30 +static const enum_name_map connect_phase_names[] = {
31 + {"Handshake", HANDSHAKE},
32 + {"RenegAppData", RENEG_APPLICATION_DATA},
33 + {"RenegSetup", RENEG_SETUP},
34 + {"RenegHandshake", RENEG_HANDSHAKE},
35 + {"AppData", APPLICATION_DATA},
36 + {"Shutdown", SHUTDOWN},
37 + {"ConnectionDone", CONNECTION_DONE}
40 +static const enum_name_map peer_status_names[] = {
41 + {"PeerSuccess", PEER_SUCCESS},
42 + {"PeerRetry", PEER_RETRY},
43 + {"PeerError", PEER_ERROR},
44 + {"PeerWaiting", PEER_WAITING},
45 + {"PeerTestFail", PEER_TEST_FAILURE}
48 +static const enum_name_map handshake_status_names[] = {
49 + {"HandshakeSuccess", HANDSHAKE_SUCCESS},
50 + {"ClientError", CLIENT_ERROR},
51 + {"ServerError", SERVER_ERROR},
52 + {"InternalError", INTERNAL_ERROR},
53 + {"HandshakeRetry", HANDSHAKE_RETRY}
56 +/* Shamelessly copied from test/helpers/ssl_test_ctx.c */
57 +static const char *enum_name(const enum_name_map *enums, size_t num_enums,
61 + for (i = 0; i < num_enums; i++) {
62 + if (enums[i].value == value) {
63 + return enums[i].name;
66 + return "InvalidValue";
69 +const char *handshake_connect_phase_name(connect_phase_t phase)
71 + return enum_name(connect_phase_names, OSSL_NELEM(connect_phase_names),
75 +const char *handshake_status_name(handshake_status_t handshake_status)
77 + return enum_name(handshake_status_names, OSSL_NELEM(handshake_status_names),
78 + (int)handshake_status);
81 +const char *handshake_peer_status_name(peer_status_t peer_status)
83 + return enum_name(peer_status_names, OSSL_NELEM(peer_status_names),
87 +static void save_loop_history(HANDSHAKE_HISTORY *history,
88 + connect_phase_t phase,
89 + handshake_status_t handshake_status,
90 + peer_status_t server_status,
91 + peer_status_t client_status,
92 + int client_turn_count,
95 + HANDSHAKE_HISTORY_ENTRY *new_entry = NULL;
98 + * Create a new history entry for a handshake loop with statuses given in
99 + * the arguments. Potentially evicting the oldest entry when the
100 + * ring buffer is full.
102 + ++(history->last_idx);
103 + history->last_idx &= MAX_HANDSHAKE_HISTORY_ENTRY_IDX_MASK;
105 + new_entry = &((history->entries)[history->last_idx]);
106 + new_entry->phase = phase;
107 + new_entry->handshake_status = handshake_status;
108 + new_entry->server_status = server_status;
109 + new_entry->client_status = client_status;
110 + new_entry->client_turn_count = client_turn_count;
111 + new_entry->is_client_turn = is_client_turn;
113 + /* Evict the oldest handshake loop entry when the ring buffer is full. */
114 + if (history->entry_count < MAX_HANDSHAKE_HISTORY_ENTRY) {
115 + ++(history->entry_count);
119 HANDSHAKE_RESULT *HANDSHAKE_RESULT_new(void)
121 HANDSHAKE_RESULT *ret;
122 @@ -725,15 +821,6 @@ static void configure_handshake_ssl(SSL *server, SSL *client,
123 SSL_set_post_handshake_auth(client, 1);
126 -/* The status for each connection phase. */
135 /* An SSL object and associated read-write buffers. */
136 typedef struct peer_st {
138 @@ -1080,17 +1167,6 @@ static void do_shutdown_step(PEER *peer)
144 - RENEG_APPLICATION_DATA,
153 static int renegotiate_op(const SSL_TEST_CTX *test_ctx)
155 switch (test_ctx->handshake_mode) {
156 @@ -1168,19 +1244,6 @@ static void do_connect_step(const SSL_TEST_CTX *test_ctx, PEER *peer,
161 - /* Both parties succeeded. */
163 - /* Client errored. */
165 - /* Server errored. */
167 - /* Peers are in inconsistent state. */
169 - /* One or both peers not done. */
171 -} handshake_status_t;
174 * Determine the handshake outcome.
175 * last_status: the status of the peer to have acted last.
176 @@ -1545,6 +1608,10 @@ static HANDSHAKE_RESULT *do_handshake_internal(
180 + save_loop_history(&(ret->history),
181 + phase, status, server.status, client.status,
182 + client_turn_count, client_turn);
185 * Half-duplex handshake loop.
186 * Client and server speak to each other synchronously in the same process.
187 @@ -1566,6 +1633,10 @@ static HANDSHAKE_RESULT *do_handshake_internal(
188 0 /* server went last */);
191 + save_loop_history(&(ret->history),
192 + phase, status, server.status, client.status,
193 + client_turn_count, client_turn);
196 case HANDSHAKE_SUCCESS:
197 client_turn_count = 0;
198 diff --git a/test/helpers/handshake.h b/test/helpers/handshake.h
199 index 78b03f9..b9967c2 100644
200 --- a/test/helpers/handshake.h
201 +++ b/test/helpers/handshake.h
204 - * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
205 + * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved.
207 * Licensed under the Apache License 2.0 (the "License"). You may not use
208 * this file except in compliance with the License. You can obtain a copy
211 #include "ssl_test_ctx.h"
213 +#define MAX_HANDSHAKE_HISTORY_ENTRY_BIT 4
214 +#define MAX_HANDSHAKE_HISTORY_ENTRY (1 << MAX_HANDSHAKE_HISTORY_ENTRY_BIT)
215 +#define MAX_HANDSHAKE_HISTORY_ENTRY_IDX_MASK \
216 + ((1 << MAX_HANDSHAKE_HISTORY_ENTRY_BIT) - 1)
218 typedef struct ctx_data_st {
219 unsigned char *npn_protocols;
220 size_t npn_protocols_len;
221 @@ -22,6 +27,63 @@ typedef struct ctx_data_st {
222 char *session_ticket_app_data;
227 + RENEG_APPLICATION_DATA,
235 +/* The status for each connection phase. */
245 + /* Both parties succeeded. */
247 + /* Client errored. */
249 + /* Server errored. */
251 + /* Peers are in inconsistent state. */
253 + /* One or both peers not done. */
255 +} handshake_status_t;
257 +/* Stores the various status information in a handshake loop. */
258 +typedef struct handshake_history_entry_st {
259 + connect_phase_t phase;
260 + handshake_status_t handshake_status;
261 + peer_status_t server_status;
262 + peer_status_t client_status;
263 + int client_turn_count;
264 + int is_client_turn;
265 +} HANDSHAKE_HISTORY_ENTRY;
267 +typedef struct handshake_history_st {
268 + /* Implemented using ring buffer. */
270 + * The valid entries are |entries[last_idx]|, |entries[last_idx-1]|,
271 + * ..., etc., going up to |entry_count| number of entries. Note that when
272 + * the index into the array |entries| becomes < 0, we wrap around to
273 + * the end of |entries|.
275 + HANDSHAKE_HISTORY_ENTRY entries[MAX_HANDSHAKE_HISTORY_ENTRY];
276 + /* The number of valid entries in |entries| array. */
277 + size_t entry_count;
278 + /* The index of the last valid entry in the |entries| array. */
280 +} HANDSHAKE_HISTORY;
282 typedef struct handshake_result {
283 ssl_test_result_t result;
284 /* These alerts are in the 2-byte format returned by the info_callback. */
285 @@ -77,6 +139,8 @@ typedef struct handshake_result {
287 /* session ticket application data */
288 char *result_session_ticket_app_data;
289 + /* handshake loop history */
290 + HANDSHAKE_HISTORY history;
293 HANDSHAKE_RESULT *HANDSHAKE_RESULT_new(void);
294 @@ -95,4 +159,8 @@ int configure_handshake_ctx_for_srp(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
295 CTX_DATA *server2_ctx_data,
296 CTX_DATA *client_ctx_data);
298 +const char *handshake_connect_phase_name(connect_phase_t phase);
299 +const char *handshake_status_name(handshake_status_t handshake_status);
300 +const char *handshake_peer_status_name(peer_status_t peer_status);
302 #endif /* OSSL_TEST_HANDSHAKE_HELPER_H */
303 diff --git a/test/ssl_test.c b/test/ssl_test.c
304 index ea60851..9d6b093 100644
305 --- a/test/ssl_test.c
306 +++ b/test/ssl_test.c
307 @@ -26,6 +26,44 @@ static OSSL_LIB_CTX *libctx = NULL;
308 /* Currently the section names are of the form test-<number>, e.g. test-15. */
309 #define MAX_TESTCASE_NAME_LENGTH 100
311 +static void print_handshake_history(const HANDSHAKE_HISTORY *history)
316 + const HANDSHAKE_HISTORY_ENTRY *cur_entry;
317 + const char header_template[] = "|%14s|%16s|%16s|%16s|%17s|%14s|";
318 + const char body_template[] = "|%14s|%16s|%16s|%16s|%17d|%14s|";
320 + TEST_info("The following is the server/client state "
321 + "in the most recent %d handshake loops.",
322 + MAX_HANDSHAKE_HISTORY_ENTRY);
324 + TEST_note("=================================================="
325 + "==================================================");
326 + TEST_note(header_template,
327 + "phase", "handshake status", "server status",
328 + "client status", "client turn count", "is client turn");
329 + TEST_note("+--------------+----------------+----------------"
330 + "+----------------+-----------------+--------------+");
332 + first_idx = (history->last_idx - history->entry_count + 1) &
333 + MAX_HANDSHAKE_HISTORY_ENTRY_IDX_MASK;
334 + for (i = 0; i < history->entry_count; ++i) {
335 + cur_idx = (first_idx + i) & MAX_HANDSHAKE_HISTORY_ENTRY_IDX_MASK;
336 + cur_entry = &(history->entries)[cur_idx];
337 + TEST_note(body_template,
338 + handshake_connect_phase_name(cur_entry->phase),
339 + handshake_status_name(cur_entry->handshake_status),
340 + handshake_peer_status_name(cur_entry->server_status),
341 + handshake_peer_status_name(cur_entry->client_status),
342 + cur_entry->client_turn_count,
343 + cur_entry->is_client_turn ? "true" : "false");
345 + TEST_note("=================================================="
346 + "==================================================");
349 static const char *print_alert(int alert)
351 return alert ? SSL_alert_desc_string_long(alert) : "no alert";
352 @@ -388,6 +426,12 @@ static int check_test(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
353 ret &= check_client_sign_type(result, test_ctx);
354 ret &= check_client_ca_names(result, test_ctx);
357 + /* Print handshake loop history if any check fails. */
359 + print_handshake_history(&(result->history));