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