Replace a handful of tabs with spaces.
[obnox/wireshark/wip.git] / gtk / follow_ssl.c
1 /* follow_ssl.c
2  * SSL specific routines for following traffic streams
3  *
4  * $Id$
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,
23  * USA.
24  */
25
26 #include "config.h"
27
28 #include <gtk/gtk.h>
29
30 #include <stdio.h>
31 #include <string.h>
32
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #include <ctype.h>
39
40 #include <color.h>
41 #include <gtk/colors.h>
42 #include <gtk/main.h>
43 #include <epan/follow.h>
44 #include <gtk/dlg_utils.h>
45 #include <gtk/file_dlg.h>
46 #include <gtk/keys.h>
47 #include <globals.h>
48 #include <alert_box.h>
49 #include <simple_dialog.h>
50 #include <epan/dissectors/packet-ipv6.h>
51 #include <epan/prefs.h>
52 #include <epan/addr_resolv.h>
53 #include <util.h>
54 #include <gtk/gui_utils.h>
55 #include <epan/epan_dissect.h>
56 #include <epan/filesystem.h>
57 #include <gtk/compat_macros.h>
58 #include <epan/ipproto.h>
59 #include <gtk/font_utils.h>
60 #include <wiretap/file_util.h>
61 #include <epan/tap.h>
62
63 #ifdef SSL_PLUGIN
64 #include "packet-ssl-utils.h"
65 #else
66 #include <epan/dissectors/packet-ssl-utils.h>
67 #endif
68
69 #include "follow_ssl.h"
70
71 #include "follow_stream.h"
72
73 typedef struct {
74     gboolean is_server;
75     StringInfo data;
76 } SslDecryptedRecord;
77
78 static int
79 ssl_queue_packet_data(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, const void *ssl)
80 {
81     follow_info_t* follow_info = tapdata;
82     SslDecryptedRecord* rec;
83     SslDataInfo* appl_data;
84     gint total_len;
85     guchar *p;
86     int proto_ssl = (long) ssl;
87     SslPacketInfo* pi = p_get_proto_data(pinfo->fd, proto_ssl);
88
89     /* skip packet without decrypted data payload*/    
90     if (!pi || !pi->appl_data)
91         return 0;
92
93     /* compute total length */
94     total_len = 0;
95     appl_data = pi->appl_data;
96     do {
97       total_len += appl_data->plain_data.data_len; 
98       appl_data = appl_data->next;
99     } while (appl_data);
100     
101     /* compute packet direction */
102     rec = g_malloc(sizeof(SslDecryptedRecord) + total_len);
103
104     if (follow_info->client_port == 0) {
105         follow_info->client_port = pinfo->srcport;
106         memcpy(follow_info->client_ip, pinfo->src.data, pinfo->src.len);
107     }
108     if (memcmp(follow_info->client_ip, pinfo->src.data, pinfo->src.len) == 0 &&
109         follow_info->client_port == pinfo->srcport) {
110         rec->is_server = 0;
111     }
112     else 
113         rec->is_server = 1;
114
115     /* update stream counter */
116     follow_info->bytes_written[rec->is_server] += total_len;
117     
118     /* extract decrypted data and queue it locally */    
119     rec->data.data = (guchar*)(rec + 1);
120     rec->data.data_len = total_len;
121     appl_data = pi->appl_data;
122     p = rec->data.data;
123     do {
124       memcpy(p, appl_data->plain_data.data, appl_data->plain_data.data_len);
125       p += appl_data->plain_data.data_len; 
126       appl_data = appl_data->next;
127     } while (appl_data);
128     follow_info->payload = g_list_append(
129         follow_info->payload,rec);
130
131     return 0;
132 }
133
134 extern gboolean 
135 packet_is_ssl(epan_dissect_t* edt);
136
137
138 /* Follow the SSL stream, if any, to which the last packet that we called
139    a dissection routine on belongs (this might be the most recently
140    selected packet, or it might be the last packet in the file). */
141 void
142 follow_ssl_stream_cb(GtkWidget * w, gpointer data _U_)
143 {
144     GtkWidget   *filter_te;
145     gchar       *follow_filter;
146     const gchar *previous_filter;
147     int         filter_out_filter_len, previous_filter_len;
148     const char  *hostname0, *hostname1;
149     char        *port0, *port1;
150     gchar       *server_to_client_string = NULL;
151     gchar       *client_to_server_string = NULL;
152     gchar       *both_directions_string = NULL;
153     follow_stats_t stats;
154     follow_info_t *follow_info;
155     GString*    msg;
156
157     /* we got ssl so we can follow */
158     if (!packet_is_ssl(cfile.edt)) {
159         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
160                       "Error following stream.  Please make\n"
161                       "sure you have an SSL packet selected.");
162         return;
163     }
164
165     follow_info = g_new0(follow_info_t, 1);
166     follow_info->follow_type = FOLLOW_SSL;
167
168     /* Create a new filter that matches all packets in the SSL stream,
169        and set the display filter entry accordingly */
170     reset_tcp_reassembly();
171     follow_filter = build_follow_filter(&cfile.edt->pi);
172     if (!follow_filter)
173     {
174         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
175                       "Error creating filter for this stream.\n"
176                       "A network layer header is needed");
177         return;
178     }
179
180     /* Set the display filter entry accordingly */
181     filter_te = OBJECT_GET_DATA(w, E_DFILTER_TE_KEY);
182
183     /* needed in follow_filter_out_stream(), is there a better way? */
184     follow_info->filter_te = filter_te;
185
186     /* save previous filter, const since we're not supposed to alter */
187     previous_filter =
188         (const gchar *)gtk_entry_get_text(GTK_ENTRY(filter_te));
189
190     /* allocate our new filter. API claims g_malloc terminates program on failure */
191     /* my calc for max alloc needed is really +10 but when did a few extra bytes hurt ? */
192     previous_filter_len = previous_filter?strlen(previous_filter):0;
193     filter_out_filter_len = strlen(follow_filter) + previous_filter_len + 16;
194     follow_info->filter_out_filter = (gchar *)g_malloc(filter_out_filter_len);
195
196     /* append the negation */
197     if(previous_filter_len) {
198         g_snprintf(follow_info->filter_out_filter, filter_out_filter_len,
199         "%s and !(%s)", previous_filter, follow_filter);
200     } else {
201         g_snprintf(follow_info->filter_out_filter, filter_out_filter_len,
202         "!(%s)", follow_filter);
203     }
204
205     /* data will be passed via tap callback*/
206     msg = register_tap_listener("ssl", follow_info, follow_filter,
207         NULL, ssl_queue_packet_data, NULL);
208     if (msg)
209     {
210         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
211             "Can't register ssl tap: %s\n",msg->str);
212         return;
213     }
214     gtk_entry_set_text(GTK_ENTRY(filter_te), follow_filter);
215
216     /* Run the display filter so it goes in effect - even if it's the
217        same as the previous display filter. */
218     main_filter_packets(&cfile, follow_filter, TRUE);
219
220     /* Free the filter string, as we're done with it. */
221     g_free(follow_filter);
222
223     remove_tap_listener(follow_info);
224
225     /* Stream to show */
226     follow_stats(&stats);
227
228     if (stats.is_ipv6) {
229             struct e_in6_addr ipaddr;
230             memcpy(&ipaddr, stats.ip_address[0], 16);
231             hostname0 = get_hostname6(&ipaddr);
232             memcpy(&ipaddr, stats.ip_address[0], 16);
233             hostname1 = get_hostname6(&ipaddr);
234     } else {
235             guint32 ipaddr;
236             memcpy(&ipaddr, stats.ip_address[0], 4);
237             hostname0 = get_hostname(ipaddr);
238             memcpy(&ipaddr, stats.ip_address[1], 4);
239             hostname1 = get_hostname(ipaddr);
240     }
241     
242     port0 = get_tcp_port(stats.port[0]);
243     port1 = get_tcp_port(stats.port[1]);
244     
245     follow_info->is_ipv6 = stats.is_ipv6;
246
247    /* Both Stream Directions */
248     both_directions_string = g_strdup_printf("Entire conversation (%u bytes)", follow_info->bytes_written[0] + follow_info->bytes_written[1]);
249     
250     /* Host 0 --> Host 1 */
251     server_to_client_string =
252             g_strdup_printf("%s:%s --> %s:%s (%u bytes)",
253                             hostname0, port0,
254                             hostname1, port1,
255                             follow_info->bytes_written[0]);
256
257     /* Host 1 --> Host 0 */
258     client_to_server_string =
259             g_strdup_printf("%s:%s --> %s:%s (%u bytes)",
260                             hostname1, port1,
261                             hostname0, port0,
262                             follow_info->bytes_written[1]);
263
264     follow_stream("Follow SSL Stream", follow_info, both_directions_string,
265                   server_to_client_string, client_to_server_string);
266
267     g_free(both_directions_string);
268     g_free(server_to_client_string);
269     g_free(client_to_server_string);
270 }
271
272 #define FLT_BUF_SIZE 1024
273
274 /*
275  * XXX - the routine pointed to by "print_line" doesn't get handed lines,
276  * it gets handed bufferfuls.  That's fine for "follow_write_raw()"
277  * and "follow_add_to_gtk_text()", but, as "follow_print_text()" calls
278  * the "print_line()" routine from "print.c", and as that routine might
279  * genuinely expect to be handed a line (if, for example, it's using
280  * some OS or desktop environment's printing API, and that API expects
281  * to be handed lines), "follow_print_text()" should probably accumulate
282  * lines in a buffer and hand them "print_line()".  (If there's a
283  * complete line in a buffer - i.e., there's nothing of the line in
284  * the previous buffer or the next buffer - it can just hand that to
285  * "print_line()" after filtering out non-printables, as an
286  * optimization.)
287  *
288  * This might or might not be the reason why C arrays display
289  * correctly but get extra blank lines very other line when printed.
290  */
291 frs_return_t
292 follow_read_ssl_stream(follow_info_t *follow_info,
293                        gboolean (*print_line)(char *, size_t, gboolean, void *),
294                        void *arg)
295 {
296     int                 iplen;
297     guint32             global_client_pos = 0, global_server_pos = 0;
298     guint32             *global_pos;
299     gboolean            skip;
300     GList* cur;
301     frs_return_t        frs_return;
302
303     iplen = (follow_info->is_ipv6) ? 16 : 4;
304     
305     for (cur = follow_info->payload; cur; cur = g_list_next(cur)) {
306         SslDecryptedRecord* rec = cur->data;
307         skip = FALSE;
308         if (!rec->is_server) {
309             global_pos = &global_client_pos;
310             if (follow_info->show_stream == FROM_SERVER) {
311                 skip = TRUE;
312             }
313         }
314         else {
315             global_pos = &global_server_pos;
316             if (follow_info->show_stream == FROM_CLIENT) {
317                 skip = TRUE;
318             }
319         }
320
321         if (!skip) {
322             size_t nchars = rec->data.data_len;
323             gchar *buffer = g_memdup(rec->data.data, nchars);
324             
325             frs_return = follow_show(follow_info, print_line, buffer, nchars,
326                                      rec->is_server, arg, global_pos);
327             g_free(buffer);
328             if(frs_return == FRS_PRINT_ERROR)
329                     return frs_return;
330         }
331     }
332
333     return FRS_OK;
334 }