Make arrays of 4 octets arrays of 4 guint8.
[metze/wireshark/wip.git] / ui / ssl_key_export.c
1 /* export_sslkeys.c
2  *
3  * Export SSL Session Keys dialog
4  * by Sake Blok <sake@euronet.nl> (20110526)
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12
13 #include "config.h"
14
15 #include <glib.h>
16
17 #include <epan/address.h>
18 #include <epan/dissectors/packet-ssl.h>
19 #include <epan/dissectors/packet-ssl-utils.h>
20
21 #include "ui/ssl_key_export.h"
22
23 int
24 ssl_session_key_count(void)
25 {
26     return g_hash_table_size(ssl_session_hash) +
27            g_hash_table_size(ssl_crandom_hash);
28 }
29
30 static void
31 ssl_export_sessions_func(gpointer key, gpointer value, gpointer user_data)
32 {
33     guint i;
34     StringInfo *sslid = (StringInfo *)key;
35     StringInfo *master_secret = (StringInfo *)value;
36     GString *keylist = (GString *)user_data;
37
38     g_string_append(keylist, "RSA Session-ID:");
39
40     for (i = 0; i < sslid->data_len; i++) {
41         g_string_append_printf(keylist, "%.2x", sslid->data[i]);
42     }
43
44     g_string_append(keylist, " Master-Key:");
45
46     for (i = 0; i < master_secret->data_len; i++) {
47         g_string_append_printf(keylist, "%.2x", master_secret->data[i]);
48     }
49
50     g_string_append_c(keylist, '\n');
51 }
52
53 static void
54 ssl_export_client_randoms_func(gpointer key, gpointer value, gpointer user_data)
55 {
56     guint i;
57     StringInfo *client_random = (StringInfo *)key;
58     StringInfo *master_secret = (StringInfo *)value;
59     GString *keylist = (GString *)user_data;
60
61     g_string_append(keylist, "CLIENT_RANDOM ");
62
63     for (i = 0; i < client_random->data_len; i++) {
64         g_string_append_printf(keylist, "%.2x", client_random->data[i]);
65     }
66
67     g_string_append_c(keylist, ' ');
68
69     for (i = 0; i < master_secret->data_len; i++) {
70         g_string_append_printf(keylist, "%.2x", master_secret->data[i]);
71     }
72
73     g_string_append_c(keylist, '\n');
74 }
75
76 gchar*
77 ssl_export_sessions(void)
78 {
79     /* Output format is:
80      * "RSA Session-ID:xxxx Master-Key:yyyy\n"
81      * Where xxxx is the session ID in hex (max 64 chars)
82      * Where yyyy is the Master Key in hex (always 96 chars)
83      * So in total max 3+1+11+64+1+11+96+2 = 189 chars
84      * or
85      * "CLIENT_RANDOM zzzz yyyy\n"
86      * Where zzzz is the client random (always 64 chars)
87      * Where yyyy is same as above
88      * So length will always be 13+1+64+1+96+2 = 177 chars
89      *
90      * Wireshark can read CLIENT_RANDOM since v1.8.0.
91      * Both values are exported in case you use the Session-ID for resuming a
92      * session in a different capture.
93      */
94     gsize len = 189 * g_hash_table_size(ssl_session_hash) +
95                 177 * g_hash_table_size(ssl_crandom_hash);
96     GString *keylist = g_string_sized_new(len);
97
98     g_hash_table_foreach(ssl_session_hash, ssl_export_sessions_func, (gpointer)keylist);
99     g_hash_table_foreach(ssl_crandom_hash, ssl_export_client_randoms_func, (gpointer)keylist);
100
101     return g_string_free(keylist, FALSE);
102 }
103
104 /*
105  * Editor modelines
106  *
107  * Local Variables:
108  * c-basic-offset: 4
109  * tab-width: 8
110  * indent-tabs-mode: nil
111  * End:
112  *
113  * ex: set shiftwidth=4 tabstop=8 expandtab:
114  * :indentSize=4:tabSize=8:noTabs=true:
115  */