165db1a8 |
1 | /* -*-c-*- |
2 | * |
52c03a2a |
3 | * $Id: packet-tripe.c,v 1.2 2004/04/03 12:35:13 mdw Exp $ |
165db1a8 |
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 $ |
52c03a2a |
32 | * Revision 1.2 2004/04/03 12:35:13 mdw |
33 | * Support elliptic curve key exchange. |
34 | * |
165db1a8 |
35 | * Revision 1.1 2003/10/15 09:30:19 mdw |
36 | * Add support for Ethereal protocol analysis. |
37 | * |
38 | */ |
39 | |
40 | /*----- Header files ------------------------------------------------------*/ |
41 | |
42 | #include "config.h" |
43 | |
44 | #include <stdio.h> |
45 | #include <stdlib.h> |
46 | #include <string.h> |
47 | |
48 | #include <netinet/in.h> |
49 | |
50 | #include <glib.h> |
51 | #include <gmodule.h> |
52 | #include <epan/packet.h> |
53 | |
54 | #ifdef ETHEREAL_BUGGERED |
55 | # define plugin_address_table_t void |
56 | # define plugin_address_table_init(x) |
57 | #else |
58 | # include <plugins/plugin_api.h> |
59 | #endif |
60 | |
61 | #include "tripe-protocol.h" |
62 | |
63 | /*----- Static variables --------------------------------------------------*/ |
64 | |
65 | static int proto_tripe = -1; |
66 | |
52c03a2a |
67 | typedef struct hfmp { |
68 | int hf, hf_len, hf_val, tt; |
69 | } hfmp; |
70 | typedef struct hfge { |
71 | int hf, hf_len, hf_val, hfx_len, hfx_val, hfy_len, hfy_val, tt; |
72 | } hfge; |
165db1a8 |
73 | |
74 | static int hf_tripe_cat = -1; |
75 | static int hf_tripe_packet_type = -1; |
76 | static int hf_tripe_ct = -1; |
77 | static int hf_tripe_ct_seq = -1; |
78 | static int hf_tripe_ct_iv = -1; |
79 | static int hf_tripe_ct_cbc = -1; |
80 | static int hf_tripe_ct_mac = -1; |
81 | static int hf_tripe_kx_type = -1; |
52c03a2a |
82 | static hfge hf_tripe_kx_mychal = { -1, -1, -1, -1, -1, -1, -1, -1 }; |
165db1a8 |
83 | static int hf_tripe_kx_mycookie = -1; |
84 | static int hf_tripe_kx_yourcookie = -1; |
85 | static hfmp hf_tripe_kx_check = { -1, -1, -1, -1 }; |
86 | static int hf_tripe_huh = -1; |
87 | |
88 | static int tt_tripe = -1; |
89 | static int tt_tripe_ct = -1; |
90 | |
91 | G_MODULE_EXPORT const gchar version[] = VERSION; |
92 | |
93 | /*----- Main code ---------------------------------------------------------*/ |
94 | |
95 | static gint gethash(proto_tree *tt, int hf, tvbuff_t *b, gint off) |
96 | { |
97 | proto_tree_add_item(tt, hf, b, off, 20, FALSE); |
98 | return (off + 20); |
99 | } |
100 | |
101 | static gint getmp(proto_tree *tt, const hfmp *hf, tvbuff_t *b, gint off) |
102 | { |
103 | guint16 len = tvb_get_ntohs(b, off); |
104 | proto_item *ti = proto_tree_add_item(tt, hf->hf, b, off, len + 2, FALSE); |
105 | tt = proto_item_add_subtree(ti, hf->tt); |
106 | proto_tree_add_item(tt, hf->hf_len, b, off, 2, FALSE); |
107 | proto_tree_add_item(tt, hf->hf_val, b, off + 2, len, FALSE); |
108 | return (off + 2 + len); |
109 | } |
110 | |
52c03a2a |
111 | static gint getge(proto_tree *tt, const hfge *hf, tvbuff_t *b, gint off) |
112 | { |
113 | guint16 len = tvb_get_ntohs(b, off), len2; |
114 | guint r; |
115 | proto_item *ti; |
116 | r = tvb_length_remaining(b, off); |
117 | if (r < 4 + len || |
118 | (len2 = tvb_get_ntohs(b, off + 2 + len), r < 4 + len + len2)) { |
119 | ti = proto_tree_add_item(tt, hf->hf, b, off, len + 2, FALSE); |
120 | tt = proto_item_add_subtree(ti, hf->tt); |
121 | proto_tree_add_item(tt, hf->hf_len, b, off, 2, FALSE); |
122 | proto_tree_add_item(tt, hf->hf_val, b, off + 2, len, FALSE); |
123 | r = off + len + 2; |
124 | } else { |
125 | ti = proto_tree_add_item(tt, hf->hf, b, off, len + len2 + 4, FALSE); |
126 | tt = proto_item_add_subtree(ti, hf->tt); |
127 | proto_tree_add_item(tt, hf->hfx_len, b, off, 2, FALSE); |
128 | proto_tree_add_item(tt, hf->hfx_val, b, off + 2, len, FALSE); |
129 | proto_tree_add_item(tt, hf->hfy_len, b, off + 2 + len, 2, FALSE); |
130 | proto_tree_add_item(tt, hf->hfy_val, b, off + 4 + len, len2, FALSE); |
131 | r = off + len + len2 + 4; |
132 | } |
133 | return (r); |
134 | } |
135 | |
165db1a8 |
136 | static void dissect_tripe(tvbuff_t *b, packet_info *p, proto_tree *t) |
137 | { |
138 | proto_item *ti; |
139 | proto_tree *tt; |
140 | guint8 ty; |
141 | gint off = tvb_raw_offset(b); |
142 | guint32 seq; |
143 | |
144 | /* --- Initialize the summary cells --- */ |
145 | |
146 | if (check_col(p->cinfo, COL_PROTOCOL)) |
147 | col_set_str(p->cinfo, COL_PROTOCOL, "TrIPE"); |
148 | ty = tvb_get_guint8(b, 0); |
149 | if (check_col(p->cinfo, COL_INFO)) { |
150 | col_clear(p->cinfo, COL_INFO); |
151 | switch (ty & MSG_CATMASK) { |
152 | case MSG_PACKET: |
153 | switch (ty & MSG_TYPEMASK) { |
154 | case 0: |
155 | col_set_str(p->cinfo, COL_INFO, "Packet data"); |
156 | break; |
157 | default: |
158 | col_add_fstr(p->cinfo, COL_INFO, |
159 | "Packet data, unknown type code %u", |
160 | ty & MSG_TYPEMASK); |
161 | break; |
162 | } |
163 | break; |
164 | case MSG_KEYEXCH: |
165 | switch (ty & MSG_TYPEMASK) { |
166 | case KX_PRECHAL: |
167 | col_set_str(p->cinfo, COL_INFO, "Key exchange, prechallenge"); |
168 | break; |
169 | case KX_COOKIE: |
170 | col_set_str(p->cinfo, COL_INFO, "Key exchange, cookie"); |
171 | break; |
172 | case KX_CHAL: |
173 | col_set_str(p->cinfo, COL_INFO, "Key exchange, challenge"); |
174 | break; |
175 | case KX_REPLY: |
176 | col_set_str(p->cinfo, COL_INFO, "Key exchange, reply"); |
177 | break; |
178 | case KX_SWITCH: |
179 | col_set_str(p->cinfo, COL_INFO, "Key exchange, switch request"); |
180 | break; |
181 | case KX_SWITCHOK: |
182 | col_set_str(p->cinfo, COL_INFO, "Key exchange, switch response"); |
183 | break; |
184 | default: |
185 | col_add_fstr(p->cinfo, COL_INFO, |
186 | "Key exchange, unknown type code %u", |
187 | ty & MSG_TYPEMASK); |
188 | break; |
189 | } |
190 | break; |
191 | default: |
192 | col_add_fstr(p->cinfo, COL_INFO, |
193 | "Unknown category code %u, unknown type code %u", |
194 | ty & MSG_CATMASK, ty & MSG_TYPEMASK); |
195 | break; |
196 | } |
197 | } |
198 | |
199 | /* --- Fill in the tree --- */ |
200 | |
201 | if (t) { |
202 | ti = proto_tree_add_item(t, proto_tripe, b, 0, -1, FALSE); |
203 | tt = proto_item_add_subtree(ti, tt_tripe); |
204 | |
205 | proto_tree_add_item(tt, hf_tripe_cat, b, 0, 1, FALSE); |
206 | |
207 | off = 1; |
208 | switch (ty & MSG_CATMASK) { |
209 | case MSG_PACKET: |
210 | proto_tree_add_item(tt, hf_tripe_packet_type, b, 0, 1, FALSE); |
211 | switch (ty & MSG_TYPEMASK) { |
212 | case 0: |
213 | goto ct; |
214 | default: |
215 | goto huh; |
216 | } |
217 | break; |
218 | case MSG_KEYEXCH: |
219 | proto_tree_add_item(tt, hf_tripe_kx_type, b, 0, 1, FALSE); |
220 | switch (ty & MSG_TYPEMASK) { |
221 | case KX_PRECHAL: |
52c03a2a |
222 | off = getge(tt, &hf_tripe_kx_mychal, b, off); |
165db1a8 |
223 | goto tail; |
224 | case KX_COOKIE: |
52c03a2a |
225 | off = getge(tt, &hf_tripe_kx_mychal, b, off); |
165db1a8 |
226 | off = gethash(tt, hf_tripe_kx_yourcookie, b, off); |
227 | goto tail; |
228 | case KX_CHAL: |
52c03a2a |
229 | off = getge(tt, &hf_tripe_kx_mychal, b, off); |
165db1a8 |
230 | off = gethash(tt, hf_tripe_kx_yourcookie, b, off); |
231 | off = getmp(tt, &hf_tripe_kx_check, b, off); |
232 | goto tail; |
233 | case KX_REPLY: |
234 | off = gethash(tt, hf_tripe_kx_mycookie, b, off); |
235 | off = gethash(tt, hf_tripe_kx_yourcookie, b, off); |
236 | off = getmp(tt, &hf_tripe_kx_check, b, off); |
237 | goto ct; |
238 | case KX_SWITCH: |
239 | off = gethash(tt, hf_tripe_kx_mycookie, b, off); |
240 | off = gethash(tt, hf_tripe_kx_yourcookie, b, off); |
241 | goto ct; |
242 | case KX_SWITCHOK: |
243 | goto ct; |
244 | default: |
245 | goto huh; |
246 | } |
247 | break; |
248 | default: |
249 | goto huh; |
250 | } |
251 | tail: |
252 | if (tvb_offset_exists(b, off)) |
253 | goto huh; |
254 | goto done; |
255 | huh: |
256 | proto_tree_add_item(tt, hf_tripe_huh, b, off, -1, FALSE); |
257 | goto done; |
258 | ct: |
259 | ti = proto_tree_add_item(tt, hf_tripe_ct, b, off, -1, FALSE); |
260 | seq = tvb_get_ntohl(b, off + 10); |
261 | proto_item_set_text(ti, "Encrypted ciphertext (sequence number %lu)", |
262 | (unsigned long)seq); |
263 | tt = proto_item_add_subtree(ti, tt_tripe_ct); |
264 | proto_tree_add_item(tt, hf_tripe_ct_mac, b, off, 10, FALSE); |
265 | off += 10; |
266 | proto_tree_add_item(tt, hf_tripe_ct_seq, b, off, 4, FALSE); |
267 | off += 4; |
268 | proto_tree_add_item(tt, hf_tripe_ct_iv, b, off, 8, FALSE); |
269 | off += 8; |
270 | proto_tree_add_item(ti, hf_tripe_ct_cbc, b, off, -1, FALSE); |
271 | goto done; |
272 | done:; |
273 | } |
274 | } |
275 | |
276 | void proto_register_tripe(void) |
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_cbc, { |
314 | "CBC-encrypted data", "tripe.ct.cbc", |
315 | FT_BYTES, BASE_NONE, 0, 0, |
316 | "This is the CBC-encrypted message. Reading it ought to be hard." |
317 | }, |
318 | &hf_tripe_ct_mac, { |
319 | "Message authentication code", "tripe.ct.mac", |
320 | FT_BYTES, BASE_NONE, 0, 0, |
321 | "This is the message authentication code 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, |
52c03a2a |
331 | "This is the sender's challenge." |
165db1a8 |
332 | }, |
333 | &hf_tripe_kx_mychal.hf_len, { |
334 | "Challenge length", "tripe.kx.mychal.len", |
335 | FT_UINT16, BASE_DEC, 0, 0, |
52c03a2a |
336 | "This is the length of the sender's challenge." |
165db1a8 |
337 | }, |
338 | &hf_tripe_kx_mychal.hf_val, { |
52c03a2a |
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", |
165db1a8 |
360 | FT_BYTES, BASE_NONE, 0, 0, |
52c03a2a |
361 | "This is the value of the sender's challenge x-coordinate." |
165db1a8 |
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 | |
408 | void proto_reg_handoff_tripe(void) |
409 | { |
410 | dissector_handle_t dh; |
411 | |
412 | dh = create_dissector_handle(dissect_tripe, proto_tripe); |
413 | dissector_add("udp.port", 22003, dh); |
414 | } |
415 | |
416 | G_MODULE_EXPORT void plugin_reg_handoff(void) |
417 | { |
418 | proto_reg_handoff_tripe(); |
419 | } |
420 | |
421 | G_MODULE_EXPORT void plugin_init(plugin_address_table_t *pat) |
422 | { |
423 | plugin_address_table_init(pat); |
424 | if (proto_tripe == -1) |
425 | proto_register_tripe(); |
426 | } |
427 | |
428 | /*----- That's all, folks -------------------------------------------------*/ |