chiark / gitweb /
Allow admin clients to filter out async messages. Send notifications
[tripe] / ethereal / packet-tripe.c
1 /* -*-c-*-
2  *
3  * $Id$
4  *
5  * TrIPE protocol dissector for Ethereal
6  *
7  * (c) 2003 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of Trivial IP Encryption (TrIPE).
13  *
14  * TrIPE is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  * 
19  * TrIPE is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with TrIPE; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 /*----- Header files ------------------------------------------------------*/
30
31 #include "config.h"
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <netinet/in.h>
38
39 #include <glib.h>
40 #include <gmodule.h>
41 #include <ethereal/config.h>
42 #include <ethereal/epan/packet.h>
43 #include <ethereal/epan/prefs.h>
44
45 #include "tripe-protocol.h"
46
47 /*----- Static variables --------------------------------------------------*/
48
49 static int proto_tripe = -1;
50
51 static guint hashsz = 20, tagsz = 10, ivsz = 8;
52
53 typedef struct hfmp {
54   int hf, hf_len, hf_val, tt;
55 } hfmp;
56 typedef struct hfge {
57   int hf, hf_len, hf_val, hfx_len, hfx_val, hfy_len, hfy_val, tt;
58 } hfge;
59
60 static int hf_tripe_cat = -1;
61 static int hf_tripe_packet_type = -1;
62 static int hf_tripe_ct = -1;
63 static int hf_tripe_ct_seq = -1;
64 static int hf_tripe_ct_iv = -1;
65 static int hf_tripe_ct_ct = -1;
66 static int hf_tripe_ct_tag = -1;
67 static int hf_tripe_kx_type = -1;
68 static hfge hf_tripe_kx_mychal = { -1, -1, -1, -1, -1, -1, -1, -1 };
69 static int hf_tripe_kx_mycookie = -1;
70 static int hf_tripe_kx_yourcookie = -1;
71 static hfmp hf_tripe_kx_check = { -1, -1, -1, -1 };
72 static int hf_tripe_huh = -1;
73
74 static int tt_tripe = -1;
75 static int tt_tripe_ct = -1;
76
77 G_MODULE_EXPORT const gchar version[] = VERSION;
78
79 /*----- Main code ---------------------------------------------------------*/
80
81 static void prefcb(void) { }
82
83 static gint gethash(proto_tree *tt, int hf, tvbuff_t *b, gint off)
84 {
85   proto_tree_add_item(tt, hf, b, off, hashsz, FALSE);
86   return (off + hashsz); 
87 }
88
89 static gint getmp(proto_tree *tt, const hfmp *hf, tvbuff_t *b, gint off)
90 {
91   guint16 len = tvb_get_ntohs(b, off);
92   proto_item *ti = proto_tree_add_item(tt, hf->hf, b, off, len + 2, FALSE);
93   tt = proto_item_add_subtree(ti, hf->tt);
94   proto_tree_add_item(tt, hf->hf_len, b, off, 2, FALSE);
95   proto_tree_add_item(tt, hf->hf_val, b, off + 2, len, FALSE);
96   return (off + 2 + len);
97 }
98
99 static gint getge(proto_tree *tt, const hfge *hf, tvbuff_t *b, gint off)
100 {
101   guint16 len = tvb_get_ntohs(b, off), len2;
102   guint r;
103   proto_item *ti;
104   r = tvb_length_remaining(b, off);
105   if (r < 4 + len ||
106       (len2 = tvb_get_ntohs(b, off + 2 + len), r < 4 + len + len2)) {
107     ti = proto_tree_add_item(tt, hf->hf, b, off, len + 2, FALSE);
108     tt = proto_item_add_subtree(ti, hf->tt);
109     proto_tree_add_item(tt, hf->hf_len, b, off, 2, FALSE);
110     proto_tree_add_item(tt, hf->hf_val, b, off + 2, len, FALSE);
111     r = off + len + 2;
112   } else {
113     ti = proto_tree_add_item(tt, hf->hf, b, off, len + len2 + 4, FALSE);
114     tt = proto_item_add_subtree(ti, hf->tt);
115     proto_tree_add_item(tt, hf->hfx_len, b, off, 2, FALSE);
116     proto_tree_add_item(tt, hf->hfx_val, b, off + 2, len, FALSE);
117     proto_tree_add_item(tt, hf->hfy_len, b, off + 2 + len, 2, FALSE);
118     proto_tree_add_item(tt, hf->hfy_val, b, off + 4 + len, len2, FALSE);
119     r = off + len + len2 + 4;
120   }    
121   return (r);
122 }
123
124 static void dissect_tripe(tvbuff_t *b, packet_info *p, proto_tree *t)
125 {
126   proto_item *ti;
127   proto_tree *tt;
128   guint8 ty;
129   gint off = 0;
130   guint32 seq;
131
132   /* --- Initialize the summary cells --- */
133
134   if (check_col(p->cinfo, COL_PROTOCOL))
135     col_set_str(p->cinfo, COL_PROTOCOL, "TrIPE");
136   if (check_col(p->cinfo, COL_INFO))
137     col_clear(p->cinfo, COL_INFO);
138   ty = tvb_get_guint8(b, 0);
139   if (check_col(p->cinfo, COL_INFO)) {
140     col_clear(p->cinfo, COL_INFO);
141     switch (ty & MSG_CATMASK) {
142       case MSG_PACKET:
143         switch (ty & MSG_TYPEMASK) {
144           case 0:
145             col_set_str(p->cinfo, COL_INFO, "Packet data");
146             break;
147           default:
148             col_add_fstr(p->cinfo, COL_INFO,
149                          "Packet data, unknown type code %u",
150                          ty & MSG_TYPEMASK);
151             break;
152         }
153         break;
154       case MSG_KEYEXCH:
155         switch (ty & MSG_TYPEMASK) {
156           case KX_PRECHAL:
157             col_set_str(p->cinfo, COL_INFO, "Key exchange, prechallenge");
158             break;
159           case KX_COOKIE:
160             col_set_str(p->cinfo, COL_INFO, "Key exchange, cookie");
161             break;
162           case KX_CHAL:
163             col_set_str(p->cinfo, COL_INFO, "Key exchange, challenge");
164             break;
165           case KX_REPLY:
166             col_set_str(p->cinfo, COL_INFO, "Key exchange, reply");
167             break;
168           case KX_SWITCH:
169             col_set_str(p->cinfo, COL_INFO, "Key exchange, switch request");
170             break;
171           case KX_SWITCHOK:
172             col_set_str(p->cinfo, COL_INFO, "Key exchange, switch response");
173             break;
174           default:
175             col_add_fstr(p->cinfo, COL_INFO,
176                          "Key exchange, unknown type code %u",
177                          ty & MSG_TYPEMASK);
178             break;
179         }
180         break;
181       default:
182         col_add_fstr(p->cinfo, COL_INFO,
183                      "Unknown category code %u, unknown type code %u",
184                      ty & MSG_CATMASK, ty & MSG_TYPEMASK);
185         break;
186     }
187   }
188
189   /* --- Fill in the tree --- */
190
191   if (t) {
192     ti = proto_tree_add_item(t, proto_tripe, b, 0, -1, FALSE);
193     tt = proto_item_add_subtree(ti, tt_tripe);
194
195     proto_tree_add_item(tt, hf_tripe_cat, b, 0, 1, FALSE);
196
197     off = 1;
198     switch (ty & MSG_CATMASK) {
199       case MSG_PACKET:
200         proto_tree_add_item(tt, hf_tripe_packet_type, b, 0, 1, FALSE);
201         switch (ty & MSG_TYPEMASK) {
202           case 0:
203             goto ct;
204           default:
205             goto huh;
206         }
207         break;
208       case MSG_KEYEXCH:
209         proto_tree_add_item(tt, hf_tripe_kx_type, b, 0, 1, FALSE);
210         switch (ty & MSG_TYPEMASK) {
211           case KX_PRECHAL:
212             off = getge(tt, &hf_tripe_kx_mychal, b, off);
213             goto tail;
214           case KX_COOKIE:
215             off = getge(tt, &hf_tripe_kx_mychal, b, off);
216             off = gethash(tt, hf_tripe_kx_yourcookie, b, off);
217             goto tail;
218           case KX_CHAL:
219             off = getge(tt, &hf_tripe_kx_mychal, b, off);
220             off = gethash(tt, hf_tripe_kx_yourcookie, b, off);
221             off = getmp(tt, &hf_tripe_kx_check, b, off);
222             goto tail;
223           case KX_REPLY:
224             off = gethash(tt, hf_tripe_kx_mycookie, b, off);
225             off = gethash(tt, hf_tripe_kx_yourcookie, b, off);
226             off = getmp(tt, &hf_tripe_kx_check, b, off);
227             goto ct;
228           case KX_SWITCH:
229             off = gethash(tt, hf_tripe_kx_mycookie, b, off);
230             off = gethash(tt, hf_tripe_kx_yourcookie, b, off);
231             goto ct;
232           case KX_SWITCHOK:
233             goto ct;
234           default:
235             goto huh;
236         }
237         break;
238       default:
239         goto huh;
240     }
241   tail:
242     if (tvb_offset_exists(b, off))
243       goto huh;
244     goto done;
245   huh:
246     proto_tree_add_item(tt, hf_tripe_huh, b, off, -1, FALSE);
247     goto done;
248   ct:
249     ti = proto_tree_add_item(tt, hf_tripe_ct, b, off, -1, FALSE);
250     seq = tvb_get_ntohl(b, off + tagsz);
251     proto_item_set_text(ti, "Encrypted ciphertext (sequence number %lu)",
252                         (unsigned long)seq);
253     tt = proto_item_add_subtree(ti, tt_tripe_ct);
254     if (tagsz) {
255       proto_tree_add_item(tt, hf_tripe_ct_tag, b, off, tagsz, FALSE);
256       off += tagsz;
257     }
258     proto_tree_add_item(tt, hf_tripe_ct_seq, b, off, 4, FALSE);
259     off += 4;
260     if (ivsz) {
261       proto_tree_add_item(tt, hf_tripe_ct_iv, b, off, ivsz, FALSE);
262       off += ivsz;
263     }
264     proto_tree_add_item(ti, hf_tripe_ct_ct, b, off, -1, FALSE);
265     goto done;
266   done:;
267   }  
268 }
269
270 void proto_register_tripe(void)
271 {
272   module_t *mod;
273
274   static value_string vs_kxtype[] = {
275     { KX_PRECHAL,       "KX_PRECHAL (prechallenge)" },
276     { KX_COOKIE,        "KX_COOKIE (cookie)" },
277     { KX_CHAL,          "KX_CHAL (challenge)" },
278     { KX_REPLY,         "KX_REPLY (reply)" },
279     { KX_SWITCH,        "KX_SWITCH (switch request)" },
280     { KX_SWITCHOK,      "KX_SWITCHOK (switch response)" },
281     { 0,                0 }
282   };
283
284   static hf_register_info hfs[] = {
285     { &hf_tripe_cat, {
286       "Message category", "tripe.cat",
287       FT_UINT8, BASE_HEX, 0, MSG_CATMASK
288     } },
289     { &hf_tripe_packet_type, {
290       "Packet message type", "tripe.packet.type",
291       FT_UINT8, BASE_HEX, 0, MSG_TYPEMASK,
292       "This is the TrIPE packet type subcode."
293     } },
294     { &hf_tripe_ct, {
295       "Encrypted ciphertext", "tripe.ct",
296       FT_BYTES, BASE_NONE, 0, 0,
297       "This is an encrypted message."
298     } },
299     { &hf_tripe_ct_seq, {
300       "Ciphertext sequence number", "tripe.ct.seq",
301       FT_UINT32, BASE_DEC, 0, 0,
302       "This is the unique sequence number for the ciphertext."
303     } },
304     { &hf_tripe_ct_iv, {
305       "Ciphertext initialization vector", "tripe.ct.iv",
306       FT_BYTES, BASE_NONE, 0, 0,
307       "This is the initialization vector used for the actual encryption."
308     } },
309     { &hf_tripe_ct_ct, {
310       "Actual encrypted data", "tripe.ct.ct",
311       FT_BYTES, BASE_NONE, 0, 0,
312       "This is the encrypted message.  Reading it ought to be hard."
313     } },
314     { &hf_tripe_ct_tag, {
315       "Message authentication code", "tripe.ct.tag",
316       FT_BYTES, BASE_NONE, 0, 0,
317       "This is the message authentication code tag for the ciphertext."
318     } },
319     { &hf_tripe_kx_type, {
320       "Key-exchange message type", "tripe.kx.type",
321       FT_UINT8, BASE_HEX, vs_kxtype, MSG_TYPEMASK,
322       "This is the TrIPE key-exchange type subcode."
323     } },
324     { &hf_tripe_kx_mychal.hf, {
325       "Sender's challenge data", "tripe.kx.mychal",
326       FT_BYTES, BASE_NONE, 0, 0,
327       "This is the sender's challenge."
328     } },
329     { &hf_tripe_kx_mychal.hf_len, {
330       "Challenge length", "tripe.kx.mychal.len",
331       FT_UINT16, BASE_DEC, 0, 0,
332       "This is the length of the sender's challenge."
333     } },
334     { &hf_tripe_kx_mychal.hf_val, {
335       "Challenge", "tripe.kx.mychal.val",
336       FT_BYTES, BASE_NONE, 0, 0,
337       "This is the value of the sender's challenge."
338     } },
339     { &hf_tripe_kx_mychal.hfx_len, {
340       "Challenge x length", "tripe.kx.mychal.x.len",
341       FT_UINT16, BASE_DEC, 0, 0,
342       "This is the length of the sender's challenge x-coordinate."
343     } },
344     { &hf_tripe_kx_mychal.hfy_val, {
345       "Challenge x value", "tripe.kx.mychal.x.val",
346       FT_BYTES, BASE_NONE, 0, 0,
347       "This is the value of the sender's challenge x-coordinate."
348     } },
349     { &hf_tripe_kx_mychal.hfy_len, {
350       "Challenge y length", "tripe.kx.mychal.y.len",
351       FT_UINT16, BASE_DEC, 0, 0,
352       "This is the length of the sender's challenge x-coordinate."
353     } },
354     { &hf_tripe_kx_mychal.hfx_val, {
355       "Challenge y value", "tripe.kx.mychal.y.val",
356       FT_BYTES, BASE_NONE, 0, 0,
357       "This is the value of the sender's challenge x-coordinate."
358     } },
359     { &hf_tripe_kx_mycookie, {
360       "Sender's hashed cookie", "tripe.kx.mycookie",
361       FT_BYTES, BASE_NONE, 0, 0,
362       "This is the hash of the sender's challenge."
363     } },
364     { &hf_tripe_kx_yourcookie, {
365       "Recipient's hashed cookie", "tripe.kx.yourcookie",
366       FT_BYTES, BASE_NONE, 0, 0,
367       "This is the hash of the recipient's challenge."
368     } },
369     { &hf_tripe_kx_check.hf, {
370       "Challenge check-value", "tripe.kx.check",
371       FT_BYTES, BASE_NONE, 0, 0,
372       "This is an encrypted check-value which proves that the sender "
373       "knows the answer to the challenge, and that it is therefore honest."
374     } },
375     { &hf_tripe_kx_check.hf_len, {
376       "Check-value length", "tripe.kx.check.len",
377       FT_UINT16, BASE_DEC, 0, 0,
378       "This is the length of the encrypted check-value."
379     } },
380     { &hf_tripe_kx_check.hf_val, {
381       "Check-value data", "tripe.kx.check.val",
382       FT_BYTES, BASE_NONE, 0, 0,
383       "This is the actual encrypted check-value."
384     } },
385     { &hf_tripe_huh, {
386       "Unknown data", "tripe.huh",
387       FT_BYTES, BASE_NONE, 0, 0,
388       "I don't know what's meant to appear here."
389     } },
390   };
391
392   static gint *tts[] = {
393     &tt_tripe,
394     &tt_tripe_ct,
395     &hf_tripe_kx_mychal.tt,
396     &hf_tripe_kx_check.tt,
397   };
398
399   proto_tripe = proto_register_protocol("TrIPE", "TrIPE", "tripe");
400   proto_register_field_array(proto_tripe, hfs, array_length(hfs));
401   proto_register_subtree_array(tts, array_length(tts));
402
403   mod = prefs_register_protocol(proto_tripe, prefcb);
404   prefs_register_uint_preference(mod, "hashsz", "Hash length",
405                                  "hash function output length (in octets)",
406                                  10, &hashsz);
407   prefs_register_uint_preference(mod, "tagsz", "MAC tag length",
408                                  "MAC tag length (in octets)", 10, &tagsz);
409   prefs_register_uint_preference(mod, "ivsz", "IV length",
410                                  "block cipher initialization vector length"
411                                  " (in octets)",
412                                  10, &ivsz);
413 }
414
415 void proto_reg_handoff_tripe(void)
416 {
417   dissector_handle_t dh;
418
419   dh = create_dissector_handle(dissect_tripe, proto_tripe);
420   dissector_add("udp.port", 22003, dh);
421 }
422
423 G_MODULE_EXPORT void plugin_reg_handoff(void)
424 {
425   proto_reg_handoff_tripe();
426 }
427
428 G_MODULE_EXPORT void plugin_register(void)
429 {
430   if (proto_tripe == -1)
431     proto_register_tripe();
432 }
433
434 /*----- That's all, folks -------------------------------------------------*/