chiark / gitweb /
Remove buf, and add Ethereal analysis.
[tripe] / ethereal / packet-tripe.c
1 /* -*-c-*-
2  *
3  * $Id: packet-tripe.c,v 1.1 2003/10/15 09:30:19 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 /*----- Revision history --------------------------------------------------* 
30  *
31  * $Log: packet-tripe.c,v $
32  * Revision 1.1  2003/10/15 09:30:19  mdw
33  * Add support for Ethereal protocol analysis.
34  *
35  */
36
37 /*----- Header files ------------------------------------------------------*/
38
39 #include "config.h"
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include <netinet/in.h>
46
47 #include <glib.h>
48 #include <gmodule.h>
49 #include <epan/packet.h>
50
51 #ifdef ETHEREAL_BUGGERED
52 #  define plugin_address_table_t void
53 #  define plugin_address_table_init(x)
54 #else
55 #  include <plugins/plugin_api.h>
56 #endif
57
58 #include "tripe-protocol.h"
59
60 /*----- Static variables --------------------------------------------------*/
61
62 static int proto_tripe = -1;
63
64 typedef struct hfmp { int hf, hf_len, hf_val, tt; } hfmp;
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_cbc = -1;
72 static int hf_tripe_ct_mac = -1;
73 static int hf_tripe_kx_type = -1;
74 static hfmp hf_tripe_kx_mychal = { -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 gint gethash(proto_tree *tt, int hf, tvbuff_t *b, gint off)
88 {
89   proto_tree_add_item(tt, hf, b, off, 20, FALSE);
90   return (off + 20); 
91 }
92
93 static gint getmp(proto_tree *tt, const hfmp *hf, tvbuff_t *b, gint off)
94 {
95   guint16 len = tvb_get_ntohs(b, off);
96   proto_item *ti = proto_tree_add_item(tt, hf->hf, b, off, len + 2, FALSE);
97   tt = proto_item_add_subtree(ti, hf->tt);
98   proto_tree_add_item(tt, hf->hf_len, b, off, 2, FALSE);
99   proto_tree_add_item(tt, hf->hf_val, b, off + 2, len, FALSE);
100   return (off + 2 + len);
101 }
102
103 static void dissect_tripe(tvbuff_t *b, packet_info *p, proto_tree *t)
104 {
105   proto_item *ti;
106   proto_tree *tt;
107   guint8 ty;
108   gint off = tvb_raw_offset(b);
109   guint32 seq;
110
111   /* --- Initialize the summary cells --- */
112
113   if (check_col(p->cinfo, COL_PROTOCOL))
114     col_set_str(p->cinfo, COL_PROTOCOL, "TrIPE");
115   ty = tvb_get_guint8(b, 0);
116   if (check_col(p->cinfo, COL_INFO)) {
117     col_clear(p->cinfo, COL_INFO);
118     switch (ty & MSG_CATMASK) {
119       case MSG_PACKET:
120         switch (ty & MSG_TYPEMASK) {
121           case 0:
122             col_set_str(p->cinfo, COL_INFO, "Packet data");
123             break;
124           default:
125             col_add_fstr(p->cinfo, COL_INFO,
126                          "Packet data, unknown type code %u",
127                          ty & MSG_TYPEMASK);
128             break;
129         }
130         break;
131       case MSG_KEYEXCH:
132         switch (ty & MSG_TYPEMASK) {
133           case KX_PRECHAL:
134             col_set_str(p->cinfo, COL_INFO, "Key exchange, prechallenge");
135             break;
136           case KX_COOKIE:
137             col_set_str(p->cinfo, COL_INFO, "Key exchange, cookie");
138             break;
139           case KX_CHAL:
140             col_set_str(p->cinfo, COL_INFO, "Key exchange, challenge");
141             break;
142           case KX_REPLY:
143             col_set_str(p->cinfo, COL_INFO, "Key exchange, reply");
144             break;
145           case KX_SWITCH:
146             col_set_str(p->cinfo, COL_INFO, "Key exchange, switch request");
147             break;
148           case KX_SWITCHOK:
149             col_set_str(p->cinfo, COL_INFO, "Key exchange, switch response");
150             break;
151           default:
152             col_add_fstr(p->cinfo, COL_INFO,
153                          "Key exchange, unknown type code %u",
154                          ty & MSG_TYPEMASK);
155             break;
156         }
157         break;
158       default:
159         col_add_fstr(p->cinfo, COL_INFO,
160                      "Unknown category code %u, unknown type code %u",
161                      ty & MSG_CATMASK, ty & MSG_TYPEMASK);
162         break;
163     }
164   }
165
166   /* --- Fill in the tree --- */
167
168   if (t) {
169     ti = proto_tree_add_item(t, proto_tripe, b, 0, -1, FALSE);
170     tt = proto_item_add_subtree(ti, tt_tripe);
171
172     proto_tree_add_item(tt, hf_tripe_cat, b, 0, 1, FALSE);
173
174     off = 1;
175     switch (ty & MSG_CATMASK) {
176       case MSG_PACKET:
177         proto_tree_add_item(tt, hf_tripe_packet_type, b, 0, 1, FALSE);
178         switch (ty & MSG_TYPEMASK) {
179           case 0:
180             goto ct;
181           default:
182             goto huh;
183         }
184         break;
185       case MSG_KEYEXCH:
186         proto_tree_add_item(tt, hf_tripe_kx_type, b, 0, 1, FALSE);
187         switch (ty & MSG_TYPEMASK) {
188           case KX_PRECHAL:
189             off = getmp(tt, &hf_tripe_kx_mychal, b, off);
190             goto tail;
191           case KX_COOKIE:
192             off = getmp(tt, &hf_tripe_kx_mychal, b, off);
193             off = gethash(tt, hf_tripe_kx_yourcookie, b, off);
194             goto tail;
195           case KX_CHAL:
196             off = getmp(tt, &hf_tripe_kx_mychal, b, off);
197             off = gethash(tt, hf_tripe_kx_yourcookie, b, off);
198             off = getmp(tt, &hf_tripe_kx_check, b, off);
199             goto tail;
200           case KX_REPLY:
201             off = gethash(tt, hf_tripe_kx_mycookie, b, off);
202             off = gethash(tt, hf_tripe_kx_yourcookie, b, off);
203             off = getmp(tt, &hf_tripe_kx_check, b, off);
204             goto ct;
205           case KX_SWITCH:
206             off = gethash(tt, hf_tripe_kx_mycookie, b, off);
207             off = gethash(tt, hf_tripe_kx_yourcookie, b, off);
208             goto ct;
209           case KX_SWITCHOK:
210             goto ct;
211           default:
212             goto huh;
213         }
214         break;
215       default:
216         goto huh;
217     }
218   tail:
219     if (tvb_offset_exists(b, off))
220       goto huh;
221     goto done;
222   huh:
223     proto_tree_add_item(tt, hf_tripe_huh, b, off, -1, FALSE);
224     goto done;
225   ct:
226     ti = proto_tree_add_item(tt, hf_tripe_ct, b, off, -1, FALSE);
227     seq = tvb_get_ntohl(b, off + 10);
228     proto_item_set_text(ti, "Encrypted ciphertext (sequence number %lu)",
229                         (unsigned long)seq);
230     tt = proto_item_add_subtree(ti, tt_tripe_ct);
231     proto_tree_add_item(tt, hf_tripe_ct_mac, b, off, 10, FALSE);
232     off += 10;
233     proto_tree_add_item(tt, hf_tripe_ct_seq, b, off, 4, FALSE);
234     off += 4;
235     proto_tree_add_item(tt, hf_tripe_ct_iv, b, off, 8, FALSE);
236     off += 8;
237     proto_tree_add_item(ti, hf_tripe_ct_cbc, b, off, -1, FALSE);
238     goto done;
239   done:;
240   }  
241 }
242
243 void proto_register_tripe(void)
244 {
245   static value_string vs_kxtype[] = {
246     { KX_PRECHAL,       "KX_PRECHAL (prechallenge)" },
247     { KX_COOKIE,        "KX_COOKIE (cookie)" },
248     { KX_CHAL,          "KX_CHAL (challenge)" },
249     { KX_REPLY,         "KX_REPLY (reply)" },
250     { KX_SWITCH,        "KX_SWITCH (switch request)" },
251     { KX_SWITCHOK,      "KX_SWITCHOK (switch response)" },
252     { 0,                0 }
253   };
254
255   static hf_register_info hfs[] = {
256     &hf_tripe_cat, {
257       "Message category", "tripe.cat",
258       FT_UINT8, BASE_HEX, 0, MSG_CATMASK
259     },
260     &hf_tripe_packet_type, {
261       "Packet message type", "tripe.packet.type",
262       FT_UINT8, BASE_HEX, 0, MSG_TYPEMASK,
263       "This is the TrIPE packet type subcode."
264     },
265     &hf_tripe_ct, {
266       "Encrypted ciphertext", "tripe.ct",
267       FT_BYTES, BASE_NONE, 0, 0,
268       "This is an encrypted message."
269     },
270     &hf_tripe_ct_seq, {
271       "Ciphertext sequence number", "tripe.ct.seq",
272       FT_UINT32, BASE_DEC, 0, 0,
273       "This is the unique sequence number for the ciphertext."
274     },
275     &hf_tripe_ct_iv, {
276       "Ciphertext initialization vector", "tripe.ct.iv",
277       FT_BYTES, BASE_NONE, 0, 0,
278       "This is the initialization vector used for the actual encryption."
279     },
280     &hf_tripe_ct_cbc, {
281       "CBC-encrypted data", "tripe.ct.cbc",
282       FT_BYTES, BASE_NONE, 0, 0,
283       "This is the CBC-encrypted message.  Reading it ought to be hard."
284     },
285     &hf_tripe_ct_mac, {
286       "Message authentication code", "tripe.ct.mac",
287       FT_BYTES, BASE_NONE, 0, 0,
288       "This is the message authentication code for the ciphertext."
289     },
290     &hf_tripe_kx_type, {
291       "Key-exchange message type", "tripe.kx.type",
292       FT_UINT8, BASE_HEX, vs_kxtype, MSG_TYPEMASK,
293       "This is the TrIPE key-exchange type subcode."
294     },
295     &hf_tripe_kx_mychal.hf, {
296       "Sender's challenge data", "tripe.kx.mychal",
297       FT_BYTES, BASE_NONE, 0, 0,
298       "This is the sender's challenge value."
299     },
300     &hf_tripe_kx_mychal.hf_len, {
301       "Challenge length", "tripe.kx.mychal.len",
302       FT_UINT16, BASE_DEC, 0, 0,
303       "This is the length of the sender's challenge value."
304     },
305     &hf_tripe_kx_mychal.hf_val, {
306       "Challenge value", "tripe.kx.mychal.val",
307       FT_BYTES, BASE_NONE, 0, 0,
308       "This is the value of the sender's challenge value."
309     },
310     &hf_tripe_kx_mycookie, {
311       "Sender's hashed cookie", "tripe.kx.mycookie",
312       FT_BYTES, BASE_NONE, 0, 0,
313       "This is the hash of the sender's challenge."
314     },
315     &hf_tripe_kx_yourcookie, {
316       "Recipient's hashed cookie", "tripe.kx.yourcookie",
317       FT_BYTES, BASE_NONE, 0, 0,
318       "This is the hash of the recipient's challenge."
319     },
320     &hf_tripe_kx_check.hf, {
321       "Challenge check-value", "tripe.kx.check",
322       FT_BYTES, BASE_NONE, 0, 0,
323       "This is an encrypted check-value which proves that the sender "
324       "knows the answer to the challenge, and that it is therefore honest."
325     },
326     &hf_tripe_kx_check.hf_len, {
327       "Check-value length", "tripe.kx.check.len",
328       FT_UINT16, BASE_DEC, 0, 0,
329       "This is the length of the encrypted check-value."
330     },
331     &hf_tripe_kx_check.hf_val, {
332       "Check-value data", "tripe.kx.check.val",
333       FT_BYTES, BASE_NONE, 0, 0,
334       "This is the actual encrypted check-value."
335     },
336     &hf_tripe_huh, {
337       "Unknown data", "tripe.huh",
338       FT_BYTES, BASE_NONE, 0, 0,
339       "I don't know what's meant to appear here."
340     },
341   };
342
343   static gint *tts[] = {
344     &tt_tripe,
345     &tt_tripe_ct,
346     &hf_tripe_kx_mychal.tt,
347     &hf_tripe_kx_check.tt,
348   };
349
350   proto_tripe = proto_register_protocol("TrIPE", "TrIPE", "tripe");
351   proto_register_field_array(proto_tripe, hfs, array_length(hfs));
352   proto_register_subtree_array(tts, array_length(tts));
353 }
354
355 void proto_reg_handoff_tripe(void)
356 {
357   dissector_handle_t dh;
358
359   dh = create_dissector_handle(dissect_tripe, proto_tripe);
360   dissector_add("udp.port", 22003, dh);
361 }
362
363 G_MODULE_EXPORT void plugin_reg_handoff(void)
364 {
365   proto_reg_handoff_tripe();
366 }
367
368 G_MODULE_EXPORT void plugin_init(plugin_address_table_t *pat)
369 {
370   plugin_address_table_init(pat);
371   if (proto_tripe == -1)
372     proto_register_tripe();
373 }
374
375 /*----- That's all, folks -------------------------------------------------*/