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