5 * Export SSL Session Keys dialog
6 * by Sake Blok <sake@euronet.nl> (20110526)
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
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.
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.
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.
42 #include <wsutil/file_util.h>
45 #include <gdk/gdkkeysyms.h>
46 #if GTK_CHECK_VERSION(3,0,0)
47 # include <gdk/gdkkeysyms-compat.h>
50 #include <epan/filesystem.h>
51 #include <epan/packet.h>
52 #include <epan/epan_dissect.h>
53 #include <epan/charsets.h>
54 #include <epan/prefs.h>
55 #include <epan/dissectors/packet-ssl.h>
56 #include <epan/dissectors/packet-ssl-utils.h>
58 #include "../isprint.h"
60 #include "ui/alert_box.h"
61 #include "ui/last_open_dir.h"
62 #include "ui/progress_dlg.h"
63 #include "ui/recent.h"
64 #include "ui/simple_dialog.h"
65 #include "ui/ui_util.h"
67 #include "ui/gtk/keys.h"
68 #include "ui/gtk/color_utils.h"
69 #include "ui/gtk/packet_win.h"
70 #include "ui/gtk/file_dlg.h"
71 #include "ui/gtk/gui_utils.h"
72 #include "ui/gtk/gtkglobals.h"
73 #include "ui/gtk/font_utils.h"
74 #include "ui/gtk/webbrowser.h"
75 #include "ui/gtk/main.h"
76 #include "ui/gtk/export_sslkeys.h"
79 #include <gdk/gdkwin32.h>
81 #include "ui/win32/file_dlg_win32.h"
85 ssl_export_sessions_func(gpointer key, gpointer value, gpointer user_data)
89 StringInfo* sslid = (StringInfo*)key;
90 StringInfo* mastersecret = (StringInfo*)value;
91 StringInfo* keylist = (StringInfo*)user_data;
93 offset = strlen(keylist->data);
96 * XXX - should this be a string that grows as necessary to hold
99 g_snprintf(keylist->data+offset,(gulong)(keylist->data_len-offset),"RSA Session-ID:");
102 for( i=0; i<sslid->data_len; i++) {
103 g_snprintf(keylist->data+offset,(gulong)(keylist->data_len-offset),"%.2x",sslid->data[i]&255);
107 g_snprintf(keylist->data+offset,(gulong)(keylist->data_len-offset)," Master-Key:");
110 for( i=0; i<mastersecret->data_len; i++) {
111 g_snprintf(keylist->data+offset,(gulong)(keylist->data_len-offset),"%.2x",mastersecret->data[i]&255);
115 g_snprintf(keylist->data+offset,(gulong)(keylist->data_len-offset),"\n");
119 ssl_export_sessions(GHashTable *session_hash)
124 * "RSA Session-ID:xxxx Master-Key:yyyy\n"
125 * Where xxxx is the session ID in hex (max 64 chars)
126 * Where yyyy is the Master Key in hex (always 96 chars)
127 * So in total max 3+1+11+64+1+11+96+2 = 189 chars
129 keylist = g_malloc0(sizeof(StringInfo)+189*g_hash_table_size (session_hash));
130 keylist->data = ((guchar*)keylist+sizeof(StringInfo));
131 keylist->data_len = sizeof(StringInfo)+189*g_hash_table_size (session_hash);
133 g_hash_table_foreach(session_hash, ssl_export_sessions_func, (gpointer)keylist);
137 static GtkWidget *savesslkeys_dlg=NULL;
140 savesslkeys_dlg_destroy_cb(GtkWidget *w _U_, gpointer user_data _U_)
142 savesslkeys_dlg = NULL;
145 /* save the SSL Session Keys */
147 savesslkeys_save_clicked_cb(GtkWidget * w _U_, gpointer data _U_)
153 file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(savesslkeys_dlg));
155 if (test_for_directory(file) == EISDIR) {
156 /* It's a directory - set the file selection box to display that
157 directory, and leave the selection box displayed. */
158 set_last_open_dir(file);
160 file_selection_set_current_folder(savesslkeys_dlg, get_last_open_dir());
161 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(savesslkeys_dlg), "");
162 return FALSE; /* do gtk_dialog_run again */
165 /* XXX: Must check if file name exists first */
168 * Retrieve the info we need
170 keylist = ssl_export_sessions(ssl_session_hash);
172 if (keylist->data_len == 0 ) {
173 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
174 "No SSL Session Keys to export!");
180 fd = ws_open(file, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0666);
182 open_failure_alert_box(file, errno, TRUE);
188 * Thanks, Microsoft, for not using size_t for the third argument to
189 * _write(). Presumably this string will be <= 4GiB long....
191 if (ws_write(fd, keylist->data, (unsigned int)strlen(keylist->data)) < 0) {
192 write_failure_alert_box(file, errno);
198 if (ws_close(fd) < 0) {
199 write_failure_alert_box(file, errno);
205 /* Get rid of the dialog box */
212 /* Launch the dialog box to put up the file selection box etc */
215 savesslkeys_cb(GtkWidget * w _U_, gpointer data _U_)
217 win32_export_sslkeys_file(GDK_WINDOW_HWND(gtk_widget_get_window(top_level)));
222 savesslkeys_cb(GtkWidget * w _U_, gpointer data _U_)
228 keylist_len = g_hash_table_size(ssl_session_hash);
229 /* don't show up the dialog, if no data has to be saved */
230 if (keylist_len==0) {
231 /* shouldn't happen as the menu item should have been greyed out */
232 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "There are no SSL Session Keys to save!");
238 * Build the dialog box we need.
240 savesslkeys_dlg = file_selection_new("Wireshark: Export SSL Session Keys", FILE_SELECTION_SAVE);
241 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(savesslkeys_dlg), TRUE);
244 label = g_strdup_printf("Will save %u SSL Session %s to specified file.",
245 keylist_len, plurality(keylist_len, "key", "keys"));
246 dlg_lb = gtk_label_new(label);
248 file_selection_set_extra_widget(savesslkeys_dlg, dlg_lb);
249 gtk_widget_show(dlg_lb);
251 g_signal_connect(savesslkeys_dlg, "destroy", G_CALLBACK(savesslkeys_dlg_destroy_cb), NULL);
253 /* "Run" the GtkFileChooserDialog. */
254 /* Upon exit: If "Accept" run the OK callback. */
255 /* If the OK callback returns with a FALSE status, re-run the dialog.*/
256 /* If not accept (ie: cancel) destroy the window. */
257 /* XXX: If the OK callback pops up an alert box (eg: for an error) it *must* */
258 /* return with a TRUE status so that the dialog window will be destroyed. */
259 /* Trying to re-run the dialog after popping up an alert box will not work */
260 /* since the user will not be able to dismiss the alert box. */
261 /* The (somewhat unfriendly) effect: the user must re-invoke the */
262 /* GtkFileChooserDialog whenever the OK callback pops up an alert box. */
264 /* ToDo: use GtkFileChooserWidget in a dialog window instead of */
265 /* GtkFileChooserDialog. */
266 while (gtk_dialog_run(GTK_DIALOG(savesslkeys_dlg)) == GTK_RESPONSE_ACCEPT) {
267 if (savesslkeys_save_clicked_cb(NULL, savesslkeys_dlg)) {
268 break; /* we're done */
271 window_destroy(savesslkeys_dlg);