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