Allow "capture info data" to not be a singleton.
[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  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include "config.h"
26
27 #include <glib.h>
28
29 #include <epan/address.h>
30 #include <epan/dissectors/packet-ssl.h>
31 #include <epan/dissectors/packet-ssl-utils.h>
32
33 #include "ui/ssl_key_export.h"
34
35 int
36 ssl_session_key_count(void)
37 {
38     return g_hash_table_size(ssl_session_hash) +
39            g_hash_table_size(ssl_crandom_hash);
40 }
41
42 static void
43 ssl_export_sessions_func(gpointer key, gpointer value, gpointer user_data)
44 {
45     guint i;
46     StringInfo *sslid = (StringInfo *)key;
47     StringInfo *master_secret = (StringInfo *)value;
48     GString *keylist = (GString *)user_data;
49
50     g_string_append(keylist, "RSA Session-ID:");
51
52     for (i = 0; i < sslid->data_len; i++) {
53         g_string_append_printf(keylist, "%.2x", sslid->data[i]);
54     }
55
56     g_string_append(keylist, " Master-Key:");
57
58     for (i = 0; i < master_secret->data_len; i++) {
59         g_string_append_printf(keylist, "%.2x", master_secret->data[i]);
60     }
61
62     g_string_append_c(keylist, '\n');
63 }
64
65 static void
66 ssl_export_client_randoms_func(gpointer key, gpointer value, gpointer user_data)
67 {
68     guint i;
69     StringInfo *client_random = (StringInfo *)key;
70     StringInfo *master_secret = (StringInfo *)value;
71     GString *keylist = (GString *)user_data;
72
73     g_string_append(keylist, "CLIENT_RANDOM ");
74
75     for (i = 0; i < client_random->data_len; i++) {
76         g_string_append_printf(keylist, "%.2x", client_random->data[i]);
77     }
78
79     g_string_append_c(keylist, ' ');
80
81     for (i = 0; i < master_secret->data_len; i++) {
82         g_string_append_printf(keylist, "%.2x", master_secret->data[i]);
83     }
84
85     g_string_append_c(keylist, '\n');
86 }
87
88 gchar*
89 ssl_export_sessions(void)
90 {
91     /* Output format is:
92      * "RSA Session-ID:xxxx Master-Key:yyyy\n"
93      * Where xxxx is the session ID in hex (max 64 chars)
94      * Where yyyy is the Master Key in hex (always 96 chars)
95      * So in total max 3+1+11+64+1+11+96+2 = 189 chars
96      * or
97      * "CLIENT_RANDOM zzzz yyyy\n"
98      * Where zzzz is the client random (always 64 chars)
99      * Where yyyy is same as above
100      * So length will always be 13+1+64+1+96+2 = 177 chars
101      *
102      * Wireshark can read CLIENT_RANDOM since v1.8.0.
103      * Both values are exported in case you use the Session-ID for resuming a
104      * session in a different capture.
105      */
106     gsize len = 189 * g_hash_table_size(ssl_session_hash) +
107                 177 * g_hash_table_size(ssl_crandom_hash);
108     GString *keylist = g_string_sized_new(len);
109
110     g_hash_table_foreach(ssl_session_hash, ssl_export_sessions_func, (gpointer)keylist);
111     g_hash_table_foreach(ssl_crandom_hash, ssl_export_client_randoms_func, (gpointer)keylist);
112
113     return g_string_free(keylist, FALSE);
114 }
115
116 /*
117  * Editor modelines
118  *
119  * Local Variables:
120  * c-basic-offset: 4
121  * tab-width: 8
122  * indent-tabs-mode: nil
123  * End:
124  *
125  * ex: set shiftwidth=4 tabstop=8 expandtab:
126  * :indentSize=4:tabSize=8:noTabs=true:
127  */