Do one or more of the following:
[metze/wireshark/wip.git] / ui / ssl_key_export.c
1 /* export_sslkeys.c
2  *
3  * $Id$
4  *
5  * Export SSL Session Keys dialog
6  * by Sake Blok <sake@euronet.nl> (20110526)
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #include "config.h"
28
29 #include <glib.h>
30
31 #include <epan/address.h>
32 #include <epan/dissectors/packet-ssl.h>
33 #include <epan/dissectors/packet-ssl-utils.h>
34
35 #include "ui/ssl_key_export.h"
36
37 int
38 ssl_session_key_count(void)
39 {
40     return g_hash_table_size(ssl_session_hash);
41 }
42
43 static void
44 ssl_export_sessions_func(gpointer key, gpointer value, gpointer user_data)
45 {
46     guint i;
47     StringInfo* sslid = (StringInfo*)key;
48     StringInfo* mastersecret = (StringInfo*)value;
49     GString* keylist = (GString*)user_data;
50
51     /*
52      * XXX - should this be a string that grows as necessary to hold
53      * everything in it?
54      */
55     g_string_append(keylist, "RSA Session-ID:");
56
57     for( i=0; i<sslid->data_len; i++) {
58         g_string_append_printf(keylist, "%.2x", sslid->data[i]&255);
59     }
60
61     g_string_append(keylist, " Master-Key:");
62
63     for( i=0; i<mastersecret->data_len; i++) {
64         g_string_append_printf(keylist, "%.2x", mastersecret->data[i]&255);
65     }
66
67     g_string_append_c(keylist, '\n');
68 }
69
70 gchar*
71 ssl_export_sessions(void)
72 {
73     GString* keylist = g_string_new("");
74     gchar *session_keys;
75
76     /* Output format is:
77      * "RSA Session-ID:xxxx Master-Key:yyyy\n"
78      * Where xxxx is the session ID in hex (max 64 chars)
79      * Where yyyy is the Master Key in hex (always 96 chars)
80      * So in total max 3+1+11+64+1+11+96+2 = 189 chars
81      */
82
83     g_hash_table_foreach(ssl_session_hash, ssl_export_sessions_func, (gpointer)keylist);
84
85     session_keys = keylist->str;
86     g_string_free(keylist, FALSE);
87     return session_keys;
88 }
89
90 /*
91  * Editor modelines
92  *
93  * Local Variables:
94  * c-basic-offset: 4
95  * tab-width: 8
96  * indent-tabs-mode: nil
97  * End:
98  *
99  * ex: set shiftwidth=4 tabstop=8 expandtab:
100  * :indentSize=4:tabSize=8:noTabs=true:
101  */