Move the common parts of iface_lists.[ch] from ui/gtk/ to ui/. Leave the
[metze/wireshark/wip.git] / ui / gtk / export_sslkeys.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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <ctype.h>
32 #include <string.h>
33
34 #ifdef HAVE_FCNTL_H
35 #include <fcntl.h>
36 #endif
37
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41
42 #include <wsutil/file_util.h>
43
44 #include <gtk/gtk.h>
45 #include <gdk/gdkkeysyms.h>
46 #if GTK_CHECK_VERSION(3,0,0)
47 # include <gdk/gdkkeysyms-compat.h>
48 #endif
49
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>
57
58 #include "../isprint.h"
59
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"
66
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"
77
78 #ifdef _WIN32
79 #include <gdk/gdkwin32.h>
80 #include <windows.h>
81 #include "ui/win32/file_dlg_win32.h"
82 #endif
83
84 static void
85 ssl_export_sessions_func(gpointer key, gpointer value, gpointer user_data)
86 {
87     guint i;
88     size_t offset;
89     StringInfo* sslid = (StringInfo*)key;
90     StringInfo* mastersecret = (StringInfo*)value;
91     StringInfo* keylist = (StringInfo*)user_data;
92
93     offset = strlen(keylist->data);
94
95     /*
96      * XXX - should this be a string that grows as necessary to hold
97      * everything in it?
98      */
99     g_snprintf(keylist->data+offset,(gulong)(keylist->data_len-offset),"RSA Session-ID:");
100     offset += 15;
101
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);
104         offset += 2;
105     }
106
107     g_snprintf(keylist->data+offset,(gulong)(keylist->data_len-offset)," Master-Key:");
108     offset += 12;
109
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);
112         offset += 2;
113     }
114
115     g_snprintf(keylist->data+offset,(gulong)(keylist->data_len-offset),"\n");
116 }
117
118 StringInfo*
119 ssl_export_sessions(GHashTable *session_hash)
120 {
121     StringInfo* keylist;
122
123     /* Output format is:
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
128      */
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);
132
133     g_hash_table_foreach(session_hash, ssl_export_sessions_func, (gpointer)keylist);
134
135     return keylist;
136 }
137 static GtkWidget *savesslkeys_dlg=NULL;
138
139 static void
140 savesslkeys_dlg_destroy_cb(GtkWidget *w _U_, gpointer user_data _U_)
141 {
142     savesslkeys_dlg = NULL;
143 }
144
145 /* save the SSL Session Keys */
146 static gboolean
147 savesslkeys_save_clicked_cb(GtkWidget * w _U_, gpointer data _U_)
148 {
149     int fd;
150     char *file;
151     StringInfo *keylist;
152
153     file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(savesslkeys_dlg));
154
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);
159         g_free(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 */
163     }
164
165     /* XXX: Must check if file name exists first */
166
167     /*
168      * Retrieve the info we need
169      */
170     keylist = ssl_export_sessions(ssl_session_hash);
171
172     if (keylist->data_len == 0 ) {
173         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
174                       "No SSL Session Keys to export!");
175         g_free(keylist);
176         g_free(file);
177         return TRUE;
178     }
179
180     fd = ws_open(file, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0666);
181     if (fd == -1) {
182         open_failure_alert_box(file, errno, TRUE);
183         g_free(keylist);
184         g_free(file);
185         return TRUE;
186     }
187     /*
188      * Thanks, Microsoft, for not using size_t for the third argument to
189      * _write().  Presumably this string will be <= 4GiB long....
190      */
191     if (ws_write(fd, keylist->data, (unsigned int)strlen(keylist->data)) < 0) {
192         write_failure_alert_box(file, errno);
193         ws_close(fd);
194         g_free(keylist);
195         g_free(file);
196         return TRUE;
197     }
198     if (ws_close(fd) < 0) {
199         write_failure_alert_box(file, errno);
200         g_free(keylist);
201         g_free(file);
202         return TRUE;
203     }
204
205     /* Get rid of the dialog box */
206     g_free(keylist);
207     g_free(file);
208     return TRUE;
209 }
210
211
212 /* Launch the dialog box to put up the file selection box etc */
213 #ifdef _WIN32
214 void
215 savesslkeys_cb(GtkWidget * w _U_, gpointer data _U_)
216 {
217     win32_export_sslkeys_file(GDK_WINDOW_HWND(gtk_widget_get_window(top_level)));
218     return;
219 }
220 #else
221 void
222 savesslkeys_cb(GtkWidget * w _U_, gpointer data _U_)
223 {
224     gchar *label;
225     GtkWidget   *dlg_lb;
226     guint keylist_len;
227
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!");
233         return;
234     }
235
236
237     /*
238      * Build the dialog box we need.
239      */
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);
242
243     /* label */
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);
247     g_free(label);
248     file_selection_set_extra_widget(savesslkeys_dlg, dlg_lb);
249     gtk_widget_show(dlg_lb);
250
251     g_signal_connect(savesslkeys_dlg, "destroy", G_CALLBACK(savesslkeys_dlg_destroy_cb), NULL);
252
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.     */
263     /*                                                                              */
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 */
269         }
270     }
271     window_destroy(savesslkeys_dlg);
272 }
273 #endif