chiark / gitweb /
uslip/uslip.c: Shut the server down on `SIGTERM'.
[tripe] / wireshark / packet-tripe.c
1 /* -*-c-*-
2  *
3  * TrIPE protocol dissector for Wireshark
4  *
5  * (c) 2003 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Trivial IP Encryption (TrIPE).
11  *
12  * TrIPE is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * TrIPE 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 TrIPE; if not, write to the Free Software Foundation,
24  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 /*----- Header files ------------------------------------------------------*/
28
29 #include "config.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include <netinet/in.h>
36
37 #include <glib.h>
38 #include <gmodule.h>
39 #include <wireshark/config.h>
40 #include <wireshark/epan/packet.h>
41 #include <wireshark/epan/prefs.h>
42
43 #include "protocol.h"
44
45 /*----- Static variables --------------------------------------------------*/
46
47 static int proto_tripe = -1;
48
49 static guint hashsz = 20, tagsz = 10, ivsz = 8;
50
51 typedef struct hfmp {
52   int hf, hf_len, hf_val, tt;
53 } hfmp;
54 typedef struct hfge {
55   int hf, hf_len, hf_val, hfx_len, hfx_val, hfy_len, hfy_val, tt;
56 } hfge;
57
58 static int hf_tripe_cat = -1;
59 static int hf_tripe_packet_type = -1;
60 static int hf_tripe_ct = -1;
61 static int hf_tripe_ct_seq = -1;
62 static int hf_tripe_ct_iv = -1;
63 static int hf_tripe_ct_ct = -1;
64 static int hf_tripe_ct_tag = -1;
65 static int hf_tripe_misc_type = -1;
66 static int hf_tripe_misc_payload = -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   col_set_str(p->cinfo, COL_PROTOCOL, "TrIPE");
135   col_clear(p->cinfo, COL_INFO);
136   ty = tvb_get_guint8(b, 0);
137   col_clear(p->cinfo, COL_INFO);
138   switch (ty & MSG_CATMASK) {
139     case MSG_PACKET:
140       switch (ty & MSG_TYPEMASK) {
141         case 0:
142           col_set_str(p->cinfo, COL_INFO, "Packet data");
143           break;
144         default:
145           col_add_fstr(p->cinfo, COL_INFO,
146                        "Packet data, unknown type code %u",
147                        ty & MSG_TYPEMASK);
148           break;
149       }
150       break;
151     case MSG_KEYEXCH:
152       switch (ty & MSG_TYPEMASK) {
153         case KX_PRECHAL:
154           col_set_str(p->cinfo, COL_INFO, "Key exchange, prechallenge");
155           break;
156         case KX_CHAL:
157           col_set_str(p->cinfo, COL_INFO, "Key exchange, challenge");
158           break;
159         case KX_REPLY:
160           col_set_str(p->cinfo, COL_INFO, "Key exchange, reply");
161           break;
162         case KX_SWITCH:
163           col_set_str(p->cinfo, COL_INFO, "Key exchange, switch request");
164           break;
165         case KX_SWITCHOK:
166           col_set_str(p->cinfo, COL_INFO, "Key exchange, switch response");
167           break;
168         default:
169           col_add_fstr(p->cinfo, COL_INFO,
170                        "Key exchange, unknown type code %u",
171                        ty & MSG_TYPEMASK);
172           break;
173       }
174       break;
175     case MSG_MISC:
176       switch (ty & MSG_TYPEMASK) {
177         case MISC_NOP:
178           col_set_str(p->cinfo, COL_INFO, "Miscellaneous, no-operation");
179           break;
180         case MISC_PING:
181           col_set_str(p->cinfo, COL_INFO, "Miscellaneous, transport ping");
182           break;
183         case MISC_PONG:
184           col_set_str(p->cinfo, COL_INFO,
185                       "Miscellaneous, transport ping reply");
186           break;
187         case MISC_EPING:
188           col_set_str(p->cinfo, COL_INFO, "Miscellaneous, encrypted ping");
189           break;
190         case MISC_EPONG:
191           col_set_str(p->cinfo, COL_INFO,
192                       "Miscellaneous, encrypted ping reply");
193           break;
194         case MISC_GREET:
195           col_set_str(p->cinfo, COL_INFO,
196                       "Miscellaneous, greeting");
197           break;
198         default:
199           col_add_fstr(p->cinfo, COL_INFO,
200                        "Miscellaneous, unknown type code %u",
201                        ty & MSG_TYPEMASK);
202           break;
203       }
204       break;
205     default:
206       col_add_fstr(p->cinfo, COL_INFO,
207                    "Unknown category code %u, unknown type code %u",
208                    ty & MSG_CATMASK, ty & MSG_TYPEMASK);
209       break;
210   }
211
212   /* --- Fill in the tree --- */
213
214   if (t) {
215     ti = proto_tree_add_item(t, proto_tripe, b, 0, -1, FALSE);
216     tt = proto_item_add_subtree(ti, tt_tripe);
217
218     proto_tree_add_item(tt, hf_tripe_cat, b, 0, 1, FALSE);
219
220     off = 1;
221     switch (ty & MSG_CATMASK) {
222       case MSG_PACKET:
223         proto_tree_add_item(tt, hf_tripe_packet_type, b, 0, 1, FALSE);
224         switch (ty & MSG_TYPEMASK) {
225           case 0:
226             goto ct;
227           default:
228             goto huh;
229         }
230         break;
231       case MSG_KEYEXCH:
232         proto_tree_add_item(tt, hf_tripe_kx_type, b, 0, 1, FALSE);
233         switch (ty & MSG_TYPEMASK) {
234           case KX_PRECHAL:
235             off = getge(tt, &hf_tripe_kx_mychal, b, off);
236             goto tail;
237           case KX_CHAL:
238             off = getge(tt, &hf_tripe_kx_mychal, b, off);
239             off = gethash(tt, hf_tripe_kx_yourcookie, b, off);
240             off = getmp(tt, &hf_tripe_kx_check, b, off);
241             goto tail;
242           case KX_REPLY:
243             off = getge(tt, &hf_tripe_kx_mychal, b, off);
244             off = gethash(tt, hf_tripe_kx_yourcookie, b, off);
245             off = getmp(tt, &hf_tripe_kx_check, b, off);
246             goto ct;
247           case KX_SWITCH:
248             off = gethash(tt, hf_tripe_kx_mycookie, b, off);
249             off = gethash(tt, hf_tripe_kx_yourcookie, b, off);
250             goto ct;
251           case KX_SWITCHOK:
252             goto ct;
253           default:
254             goto huh;
255         }
256         break;
257       case MSG_MISC:
258         proto_tree_add_item(tt, hf_tripe_misc_type, b, 0, 1, FALSE);
259         switch (ty & MSG_TYPEMASK) {
260           case MISC_NOP:
261           case MISC_PING:
262           case MISC_PONG:
263           case MISC_GREET:
264             proto_tree_add_item(tt, hf_tripe_misc_payload,
265                                 b, off, -1, FALSE);
266             goto done;
267           case MISC_EPING:
268           case MISC_EPONG:
269             goto ct;
270           default:
271             goto huh;
272         }
273         break;
274       default:
275         goto huh;
276     }
277   tail:
278     if (tvb_offset_exists(b, off))
279       goto huh;
280     goto done;
281   huh:
282     proto_tree_add_item(tt, hf_tripe_huh, b, off, -1, FALSE);
283     goto done;
284   ct:
285     ti = proto_tree_add_item(tt, hf_tripe_ct, b, off, -1, FALSE);
286     seq = tvb_get_ntohl(b, off + tagsz);
287     proto_item_set_text(ti, "Encrypted ciphertext (sequence number %lu)",
288                         (unsigned long)seq);
289     tt = proto_item_add_subtree(ti, tt_tripe_ct);
290     if (tagsz) {
291       proto_tree_add_item(tt, hf_tripe_ct_tag, b, off, tagsz, FALSE);
292       off += tagsz;
293     }
294     proto_tree_add_item(tt, hf_tripe_ct_seq, b, off, 4, FALSE);
295     off += 4;
296     if (ivsz) {
297       proto_tree_add_item(tt, hf_tripe_ct_iv, b, off, ivsz, FALSE);
298       off += ivsz;
299     }
300     proto_tree_add_item(ti, hf_tripe_ct_ct, b, off, -1, FALSE);
301     goto done;
302   done:;
303   }
304 }
305
306 void proto_register_tripe(void)
307 {
308   module_t *mod;
309
310   static value_string vs_kxtype[] = {
311     { KX_PRECHAL,       "KX_PRECHAL (prechallenge)" },
312     { KX_CHAL,          "KX_CHAL (challenge)" },
313     { KX_REPLY,         "KX_REPLY (reply)" },
314     { KX_SWITCH,        "KX_SWITCH (switch request)" },
315     { KX_SWITCHOK,      "KX_SWITCHOK (switch response)" },
316     { 0,                0 }
317   };
318
319   static hf_register_info hfs[] = {
320     { &hf_tripe_cat, {
321       "Message category", "tripe.cat",
322       FT_UINT8, BASE_HEX, 0, MSG_CATMASK
323     } },
324     { &hf_tripe_packet_type, {
325       "Packet message type", "tripe.packet.type",
326       FT_UINT8, BASE_HEX, 0, MSG_TYPEMASK,
327       "This is the TrIPE packet type subcode."
328     } },
329     { &hf_tripe_ct, {
330       "Encrypted ciphertext", "tripe.ct",
331       FT_BYTES, BASE_NONE, 0, 0,
332       "This is an encrypted message."
333     } },
334     { &hf_tripe_ct_seq, {
335       "Ciphertext sequence number", "tripe.ct.seq",
336       FT_UINT32, BASE_DEC, 0, 0,
337       "This is the unique sequence number for the ciphertext."
338     } },
339     { &hf_tripe_ct_iv, {
340       "Ciphertext initialization vector", "tripe.ct.iv",
341       FT_BYTES, BASE_NONE, 0, 0,
342       "This is the initialization vector used for the actual encryption."
343     } },
344     { &hf_tripe_ct_ct, {
345       "Actual encrypted data", "tripe.ct.ct",
346       FT_BYTES, BASE_NONE, 0, 0,
347       "This is the encrypted message.  Reading it ought to be hard."
348     } },
349     { &hf_tripe_ct_tag, {
350       "Message authentication code", "tripe.ct.tag",
351       FT_BYTES, BASE_NONE, 0, 0,
352       "This is the message authentication code tag for the ciphertext."
353     } },
354     { &hf_tripe_misc_type, {
355       "Miscellaneous message type", "tripe.misc.type",
356       FT_UINT8, BASE_HEX, 0, MSG_TYPEMASK,
357       "This is the TrIPE miscellaneous message type subcode."
358     } },
359     { &hf_tripe_misc_payload, {
360       "Miscellaneous message payload", "tripe.misc.payload",
361       FT_BYTES, BASE_NONE, 0, 0,
362       "This is the miscellaneous message payload."
363     } },
364     { &hf_tripe_kx_type, {
365       "Key-exchange message type", "tripe.kx.type",
366       FT_UINT8, BASE_HEX, vs_kxtype, MSG_TYPEMASK,
367       "This is the TrIPE key-exchange type subcode."
368     } },
369     { &hf_tripe_kx_mychal.hf, {
370       "Sender's challenge data", "tripe.kx.mychal",
371       FT_BYTES, BASE_NONE, 0, 0,
372       "This is the sender's challenge."
373     } },
374     { &hf_tripe_kx_mychal.hf_len, {
375       "Challenge length", "tripe.kx.mychal.len",
376       FT_UINT16, BASE_DEC, 0, 0,
377       "This is the length of the sender's challenge."
378     } },
379     { &hf_tripe_kx_mychal.hf_val, {
380       "Challenge", "tripe.kx.mychal.val",
381       FT_BYTES, BASE_NONE, 0, 0,
382       "This is the value of the sender's challenge."
383     } },
384     { &hf_tripe_kx_mychal.hfx_len, {
385       "Challenge x length", "tripe.kx.mychal.x.len",
386       FT_UINT16, BASE_DEC, 0, 0,
387       "This is the length of the sender's challenge x-coordinate."
388     } },
389     { &hf_tripe_kx_mychal.hfx_val, {
390       "Challenge x value", "tripe.kx.mychal.x.val",
391       FT_BYTES, BASE_NONE, 0, 0,
392       "This is the value of the sender's challenge x-coordinate."
393     } },
394     { &hf_tripe_kx_mychal.hfy_len, {
395       "Challenge y length", "tripe.kx.mychal.y.len",
396       FT_UINT16, BASE_DEC, 0, 0,
397       "This is the length of the sender's challenge x-coordinate."
398     } },
399     { &hf_tripe_kx_mychal.hfy_val, {
400       "Challenge y value", "tripe.kx.mychal.y.val",
401       FT_BYTES, BASE_NONE, 0, 0,
402       "This is the value of the sender's challenge x-coordinate."
403     } },
404     { &hf_tripe_kx_mycookie, {
405       "Sender's hashed cookie", "tripe.kx.mycookie",
406       FT_BYTES, BASE_NONE, 0, 0,
407       "This is the hash of the sender's challenge."
408     } },
409     { &hf_tripe_kx_yourcookie, {
410       "Recipient's hashed cookie", "tripe.kx.yourcookie",
411       FT_BYTES, BASE_NONE, 0, 0,
412       "This is the hash of the recipient's challenge."
413     } },
414     { &hf_tripe_kx_check.hf, {
415       "Challenge check-value", "tripe.kx.check",
416       FT_BYTES, BASE_NONE, 0, 0,
417       "This is an encrypted check-value which proves that the sender "
418       "knows the answer to the challenge, and that it is therefore honest."
419     } },
420     { &hf_tripe_kx_check.hf_len, {
421       "Check-value length", "tripe.kx.check.len",
422       FT_UINT16, BASE_DEC, 0, 0,
423       "This is the length of the encrypted check-value."
424     } },
425     { &hf_tripe_kx_check.hf_val, {
426       "Check-value data", "tripe.kx.check.val",
427       FT_BYTES, BASE_NONE, 0, 0,
428       "This is the actual encrypted check-value."
429     } },
430     { &hf_tripe_huh, {
431       "Unknown data", "tripe.huh",
432       FT_BYTES, BASE_NONE, 0, 0,
433       "I don't know what's meant to appear here."
434     } },
435   };
436
437   static gint *tts[] = {
438     &tt_tripe,
439     &tt_tripe_ct,
440     &hf_tripe_kx_mychal.tt,
441     &hf_tripe_kx_check.tt,
442   };
443
444   proto_tripe = proto_register_protocol("TrIPE", "TrIPE", "tripe");
445   proto_register_field_array(proto_tripe, hfs, array_length(hfs));
446   proto_register_subtree_array(tts, array_length(tts));
447
448   mod = prefs_register_protocol(proto_tripe, prefcb);
449   prefs_register_uint_preference(mod, "hashsz", "Hash length",
450                                  "hash function output length (in octets)",
451                                  10, &hashsz);
452   prefs_register_uint_preference(mod, "tagsz", "MAC tag length",
453                                  "MAC tag length (in octets)", 10, &tagsz);
454   prefs_register_uint_preference(mod, "ivsz", "IV length",
455                                  "block cipher initialization vector length"
456                                  " (in octets)",
457                                  10, &ivsz);
458 }
459
460 void proto_reg_handoff_tripe(void)
461 {
462   dissector_handle_t dh;
463
464   dh = create_dissector_handle(dissect_tripe, proto_tripe);
465   dissector_add_uint("udp.port", TRIPE_PORT, dh);
466 }
467
468 G_MODULE_EXPORT void plugin_reg_handoff(void)
469 {
470   proto_reg_handoff_tripe();
471 }
472
473 G_MODULE_EXPORT void plugin_register(void)
474 {
475   if (proto_tripe == -1)
476     proto_register_tripe();
477 }
478
479 /*----- That's all, folks -------------------------------------------------*/