Remove a bunch of duplicate semicolons.
[obnox/wireshark/wip.git] / packet-isakmp.c
1 /* packet-isakmp.c
2  * Routines for the Internet Security Association and Key Management Protocol
3  * (ISAKMP) (RFC 2408) and the Internet IP Security Domain of Interpretation
4  * for ISAKMP (RFC 2407)
5  * Brad Robel-Forrest <brad.robel-forrest@watchguard.com>
6  *
7  * $Id: packet-isakmp.c,v 1.68 2003/09/15 19:08:27 guy Exp $
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
11  * Copyright 1998 Gerald Combs
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <stdio.h>
33 #include <string.h>
34
35 #include <glib.h>
36
37 #ifdef NEED_SNPRINTF_H
38 # include "snprintf.h"
39 #endif
40
41 #include <epan/packet.h>
42 #include "ipproto.h"
43
44 #define isakmp_min(a, b)  ((a<b) ? a : b)
45
46 static int proto_isakmp = -1;
47
48 static gint ett_isakmp = -1;
49 static gint ett_isakmp_flags = -1;
50 static gint ett_isakmp_payload = -1;
51
52 #define UDP_PORT_ISAKMP 500
53 #define TCP_PORT_ISAKMP 500
54
55 #define NUM_PROTO_TYPES 5
56 #define proto2str(t)    \
57   ((t < NUM_PROTO_TYPES) ? prototypestr[t] : "UNKNOWN-PROTO-TYPE")
58
59 static const char *prototypestr[NUM_PROTO_TYPES] = {
60   "RESERVED",
61   "ISAKMP",
62   "IPSEC_AH",
63   "IPSEC_ESP",
64   "IPCOMP"
65 };
66
67 #define NUM_P1_ATT_TYPES        17
68 #define p1_atttype2str(t)       \
69   ((t < NUM_P1_ATT_TYPES) ? p1_atttypestr[t] : "UNKNOWN-ATTRIBUTE-TYPE")
70
71 static const char *p1_atttypestr[NUM_P1_ATT_TYPES] = {
72   "UNKNOWN-ATTRIBUTE-TYPE",
73   "Encryption-Algorithm",
74   "Hash-Algorithm",
75   "Authentication-Method",
76   "Group-Description",
77   "Group-Type",
78   "Group-Prime",
79   "Group-Generator-One",
80   "Group-Generator-Two",
81   "Group-Curve-A",
82   "Group-Curve-B",
83   "Life-Type",
84   "Life-Duration",
85   "PRF",
86   "Key-Length",
87   "Field-Size",
88   "Group-Order"
89 };
90
91 #define NUM_ATT_TYPES   11
92 #define atttype2str(t)  \
93   ((t < NUM_ATT_TYPES) ? atttypestr[t] : "UNKNOWN-ATTRIBUTE-TYPE")
94
95 static const char *atttypestr[NUM_ATT_TYPES] = {
96   "UNKNOWN-ATTRIBUTE-TYPE",
97   "SA-Life-Type",
98   "SA-Life-Duration",
99   "Group-Description",
100   "Encapsulation-Mode",
101   "Authentication-Algorithm",
102   "Key-Length",
103   "Key-Rounds",
104   "Compress-Dictinary-Size",
105   "Compress-Private-Algorithm",
106   "ECN Tunnel"
107 };
108
109 #define NUM_TRANS_TYPES 2
110 #define trans2str(t)    \
111   ((t < NUM_TRANS_TYPES) ? transtypestr[t] : "UNKNOWN-TRANS-TYPE")
112
113 static const char *transtypestr[NUM_TRANS_TYPES] = {
114   "RESERVED",
115   "KEY_IKE"
116 };
117
118 #define NUM_AH_TRANS_TYPES      8
119 #define ah_trans2str(t)         \
120   ((t < NUM_AH_TRANS_TYPES) ? ah_transtypestr[t] : "UNKNOWN-AH-TRANS-TYPE")
121
122 static const char *ah_transtypestr[NUM_AH_TRANS_TYPES] = {
123   "RESERVED",
124   "RESERVED",
125   "MD5",
126   "SHA",
127   "DES",
128   "SHA2-256",
129   "SHA2-384",
130   "SHA2-512"
131 };
132
133 #define NUM_ESP_TRANS_TYPES     13
134 #define esp_trans2str(t)        \
135   ((t < NUM_ESP_TRANS_TYPES) ? esp_transtypestr[t] : "UNKNOWN-ESP-TRANS-TYPE")
136
137 static const char *esp_transtypestr[NUM_ESP_TRANS_TYPES] = {
138   "RESERVED",
139   "DES-IV64",
140   "DES",
141   "3DES",
142   "RC5",
143   "IDEA",
144   "CAST",
145   "BLOWFISH",
146   "3IDEA",
147   "DES-IV32",
148   "RC4",
149   "NULL",
150   "AES"
151 };
152
153 #define NUM_IPCOMP_TRANS_TYPES    5
154 #define ipcomp_trans2str(t)  \
155   ((t < NUM_IPCOMP_TRANS_TYPES) ? ipcomp_transtypestr[t] : "UNKNOWN-IPCOMP-TRANS-TYPE")
156
157 static const char *ipcomp_transtypestr[NUM_IPCOMP_TRANS_TYPES] = {
158   "RESERVED",
159   "OUI",
160   "DEFLATE",
161   "LZS",
162   "LZJH"
163 };
164
165 #define NUM_ID_TYPES    12
166 #define id2str(t)       \
167   ((t < NUM_ID_TYPES) ? idtypestr[t] : "UNKNOWN-ID-TYPE")
168
169 static const char *idtypestr[NUM_ID_TYPES] = {
170   "RESERVED",
171   "IPV4_ADDR",
172   "FQDN",
173   "USER_FQDN",
174   "IPV4_ADDR_SUBNET",
175   "IPV6_ADDR",
176   "IPV6_ADDR_SUBNET",
177   "IPV4_ADDR_RANGE",
178   "IPV6_ADDR_RANGE",
179   "DER_ASN1_DN",
180   "DER_ASN1_GN",
181   "KEY_ID"
182 };
183
184 #define NUM_GRPDESC_TYPES 19
185 #define grpdesc2str(t) ((t < NUM_GRPDESC_TYPES) ? grpdescstr[t] : "UNKNOWN-GROUP-DESCRIPTION")
186
187 static const char *grpdescstr[NUM_GRPDESC_TYPES] = {
188   "UNDEFINED - 0",
189   "Default 768-bit MODP group",
190   "Alternate 1024-bit MODP group",
191   "EC2N group on GP[2^155] group",
192   "EC2N group on GP[2^185] group",
193   "1536 bit MODP group",
194   "EC2N group over GF[2^163]",
195   "EC2N group over GF[2^163]",
196   "EC2N group over GF[2^283]",
197   "EC2N group over GF[2^283]",
198   "EC2N group over GF[2^409]",
199   "EC2N group over GF[2^409]",
200   "EC2N group over GF[2^571]",
201   "EC2N group over GF[2^571]",
202   "2048 bit MODP group",
203   "3072 bit MODP group",
204   "4096 bit MODP group",
205   "6144 bit MODP group",
206   "8192 bit MODP group",
207 };
208
209 struct isakmp_hdr {
210   guint8        icookie[8];
211   guint8        rcookie[8];
212   guint8        next_payload;
213   guint8        version;
214   guint8        exch_type;
215   guint8        flags;
216 #define E_FLAG          0x01
217 #define C_FLAG          0x02
218 #define A_FLAG          0x04
219   guint32       message_id;
220   guint32       length;
221 };
222
223 struct udp_encap_hdr {
224   guint8        non_esp_marker[4];
225   guint32       esp_SPI;
226 };
227
228 static proto_tree *dissect_payload_header(tvbuff_t *, int, int, guint8,
229     guint8 *, guint16 *, proto_tree *);
230
231 static void dissect_sa(tvbuff_t *, int, int, proto_tree *, int);
232 static void dissect_proposal(tvbuff_t *, int, int, proto_tree *, int);
233 static void dissect_transform(tvbuff_t *, int, int, proto_tree *, int);
234 static void dissect_key_exch(tvbuff_t *, int, int, proto_tree *, int);
235 static void dissect_id(tvbuff_t *, int, int, proto_tree *, int);
236 static void dissect_cert(tvbuff_t *, int, int, proto_tree *, int);
237 static void dissect_certreq(tvbuff_t *, int, int, proto_tree *, int);
238 static void dissect_hash(tvbuff_t *, int, int, proto_tree *, int);
239 static void dissect_sig(tvbuff_t *, int, int, proto_tree *, int);
240 static void dissect_nonce(tvbuff_t *, int, int, proto_tree *, int);
241 static void dissect_notif(tvbuff_t *, int, int, proto_tree *, int);
242 static void dissect_delete(tvbuff_t *, int, int, proto_tree *, int);
243 static void dissect_vid(tvbuff_t *, int, int, proto_tree *, int);
244 static void dissect_config(tvbuff_t *, int, int, proto_tree *, int);
245
246 static const char *payloadtype2str(guint8);
247 static const char *exchtype2str(guint8);
248 static const char *doitype2str(guint32);
249 static const char *msgtype2str(guint16);
250 static const char *situation2str(guint32);
251 static const char *value2str(int, guint16, guint16);
252 static const char *attrtype2str(guint8);
253 static const char *cfgattrident2str(guint16);
254 static const char *certtype2str(guint8);
255
256 static gboolean get_num(tvbuff_t *, int, guint16, guint32 *);
257
258 #define LOAD_TYPE_NONE          0       /* payload type for None */
259 #define LOAD_TYPE_PROPOSAL      2       /* payload type for Proposal */
260 #define LOAD_TYPE_TRANSFORM     3       /* payload type for Transform */
261 #define NUM_LOAD_TYPES          17
262 #define loadtype2str(t) \
263   ((t < NUM_LOAD_TYPES) ? strfuncs[t].str : "Unknown payload type")
264
265 static struct strfunc {
266   const char *  str;
267   void          (*func)(tvbuff_t *, int, int, proto_tree *, int);
268 } strfuncs[NUM_LOAD_TYPES] = {
269   {"NONE",                      NULL              },
270   {"Security Association",      dissect_sa        },
271   {"Proposal",                  dissect_proposal  },
272   {"Transform",                 dissect_transform },
273   {"Key Exchange",              dissect_key_exch  },
274   {"Identification",            dissect_id        },
275   {"Certificate",               dissect_cert      },
276   {"Certificate Request",       dissect_certreq   },
277   {"Hash",                      dissect_hash      },
278   {"Signature",                 dissect_sig       },
279   {"Nonce",                     dissect_nonce     },
280   {"Notification",              dissect_notif     },
281   {"Delete",                    dissect_delete    },
282   {"Vendor ID",                 dissect_vid       },
283   {"Attrib",                    dissect_config    },
284   {"NAT-Discovery",             NULL              }, /* http://www.ietf.org/internet-drafts/draft-ietf-ipsec-nat-t-ike-05.txt */
285   {"NAT-Original Address",      NULL              }  /* http://www.ietf.org/internet-drafts/draft-ietf-ipsec-nat-t-ike-05.txt */
286 };
287
288 #define VID_LEN 16
289 #define VID_MS_LEN 20
290 static const guint8 VID_MS_W2K_WXP[VID_MS_LEN] = {0x1E, 0x2B, 0x51, 0x69, 0x5, 0x99, 0x1C, 0x7D, 0x7C, 0x96, 0xFC, 0xBF, 0xB5, 0x87, 0xE4, 0x61, 0x0, 0x0, 0x0, 0x2}; /* according to http://www.microsoft.com/technet/treeview/default.asp?url=/technet/columns/cableguy/cg0602.asp */
291
292 #define VID_CP_LEN 20
293 static const guint8 VID_CP[VID_CP_LEN] = {0xF4, 0xED, 0x19, 0xE0, 0xC1, 0x14, 0xEB, 0x51, 0x6F, 0xAA, 0xAC, 0x0E, 0xE3, 0x7D, 0xAF, 0x28, 0x7, 0xB4, 0x38, 0x1F};
294
295 static const guint8 VID_CYBERGUARD[VID_LEN] = {0x9A, 0xA1, 0xF3, 0xB4, 0x34, 0x72, 0xA4, 0x5D, 0x5F, 0x50, 0x6A, 0xEB, 0x26, 0xC, 0xF2, 0x14};
296
297 static const guint8 VID_draft_ietf_ipsec_nat_t_ike_03[VID_LEN] = {0x7D, 0x94, 0x19, 0xA6, 0x53, 0x10, 0xCA, 0x6F, 0x2C, 0x17, 0x9D, 0x92, 0x15, 0x52, 0x9d, 0x56}; /* according to http://www.ietf.org/internet-drafts/draft-ietf-ipsec-nat-t-ike-03.txt */
298
299 /* 
300 *  Seen in Netscreen. Suppose to be ASCII HeartBeat_Notify - but I don't know the rest yet. I suspect it then proceeds with
301 *  8k10, which means every 8K (?), and version 1.0 of the protocol (?). I won't add it to the code, until I know what it really
302 *  means. ykaul-at-netvision.net.il
303 */
304 static const guint8 VID_HeartBeat_Notify[VID_LEN] = {0x48, 0x65, 0x61, 0x72, 0x74, 0x42, 0x65, 0x61, 0x74, 0x5f, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79}; 
305
306 static dissector_handle_t esp_handle;
307 static dissector_handle_t ah_handle;
308
309 static void
310 dissect_payloads(tvbuff_t *tvb, proto_tree *tree, guint8 initial_payload,
311                  int offset, int length)
312 {
313   guint8 payload, next_payload;
314   guint16               payload_length;
315   proto_tree *          ntree;
316
317   for (payload = initial_payload; length != 0; payload = next_payload) {
318     if (payload == LOAD_TYPE_NONE) {
319       /*
320        * What?  There's more stuff in this chunk of data, but the
321        * previous payload had a "next payload" type of None?
322        */
323       proto_tree_add_text(tree, tvb, offset, length,
324                           "Extra data: %s",
325                           tvb_bytes_to_str(tvb, offset, length));
326       break;
327     }
328     ntree = dissect_payload_header(tvb, offset, length, payload,
329       &next_payload, &payload_length, tree);
330     if (ntree == NULL)
331       break;
332     if (payload_length >= 4) {  /* XXX = > 4? */
333       if (payload < NUM_LOAD_TYPES) {
334         (*strfuncs[payload].func)(tvb, offset + 4, payload_length - 4, ntree,
335                                   -1);
336       }
337       else {
338         proto_tree_add_text(ntree, tvb, offset + 4, payload_length - 4,
339             "Payload");
340       }
341     }
342     else {
343         proto_tree_add_text(ntree, tvb, offset + 4, 0,
344             "Payload (bogus, length is %u, must be at least 4)",
345             payload_length);
346         payload_length = 4;
347     }
348     offset += payload_length;
349     length -= payload_length;
350   }
351 }
352
353 static void
354 dissect_isakmp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
355 {
356   int                   offset = 0;
357   struct isakmp_hdr     hdr;
358   proto_item *          ti;
359   proto_tree *          isakmp_tree = NULL;
360   struct udp_encap_hdr  encap_hdr;
361   guint32               len;
362   static const guint8   non_esp_marker[4] = { 0, 0, 0, 0 };
363   tvbuff_t *            next_tvb;
364
365   if (check_col(pinfo->cinfo, COL_PROTOCOL))
366     col_set_str(pinfo->cinfo, COL_PROTOCOL, "ISAKMP");
367   if (check_col(pinfo->cinfo, COL_INFO))
368     col_clear(pinfo->cinfo, COL_INFO);
369
370   hdr.length = tvb_get_ntohl(tvb, offset + sizeof(hdr) - sizeof(hdr.length));
371
372   if (tree) {
373     ti = proto_tree_add_item(tree, proto_isakmp, tvb, offset, hdr.length, FALSE);
374     isakmp_tree = proto_item_add_subtree(ti, ett_isakmp);
375   }
376
377   tvb_memcpy(tvb, (guint8 *)&encap_hdr, 0, sizeof(encap_hdr));
378
379   if (encap_hdr.non_esp_marker[0] == 0xFF) {
380     if (check_col(pinfo->cinfo, COL_INFO))
381       col_add_str(pinfo->cinfo, COL_INFO, "UDP encapsulated IPSec - NAT Keepalive");
382     return;
383   }
384   if (memcmp(encap_hdr.non_esp_marker,non_esp_marker,4) == 0) {
385     if (check_col(pinfo->cinfo, COL_INFO))
386           col_add_str(pinfo->cinfo, COL_INFO, "UDP encapsulated IPSec - ESP");
387     if (tree)
388       proto_tree_add_text(isakmp_tree, tvb, offset,
389                           sizeof(encap_hdr.non_esp_marker),
390                           "Non-ESP-Marker");
391     offset += sizeof(encap_hdr.non_esp_marker);
392     next_tvb = tvb_new_subset(tvb, offset, -1, -1);
393     call_dissector(esp_handle, next_tvb, pinfo, tree);
394     return;
395   }
396   hdr.exch_type = tvb_get_guint8(tvb, sizeof(hdr.icookie) + sizeof(hdr.rcookie) + sizeof(hdr.next_payload) + sizeof(hdr.version));
397   if (check_col(pinfo->cinfo, COL_INFO))
398     col_add_str(pinfo->cinfo, COL_INFO, exchtype2str(hdr.exch_type));
399
400   if (tree) {
401     tvb_memcpy(tvb, (guint8 *)&hdr.icookie, offset, sizeof(hdr.icookie));
402     proto_tree_add_text(isakmp_tree, tvb, offset, sizeof(hdr.icookie),
403                         "Initiator cookie: 0x%s", tvb_bytes_to_str(tvb, offset, sizeof(hdr.icookie)));
404     offset += sizeof(hdr.icookie);
405
406     tvb_memcpy(tvb, (guint8 *)&hdr.rcookie, offset, sizeof(hdr.rcookie));
407     proto_tree_add_text(isakmp_tree, tvb, offset, sizeof(hdr.rcookie),
408                         "Responder cookie: 0x%s", tvb_bytes_to_str(tvb, offset, sizeof(hdr.rcookie)));
409     offset += sizeof(hdr.rcookie);
410
411     hdr.next_payload = tvb_get_guint8(tvb, offset);
412     proto_tree_add_text(isakmp_tree, tvb, offset, sizeof(hdr.next_payload),
413                         "Next payload: %s (%u)",
414                         payloadtype2str(hdr.next_payload), hdr.next_payload);
415     offset += sizeof(hdr.next_payload);
416
417     hdr.version = tvb_get_guint8(tvb, offset);
418     proto_tree_add_text(isakmp_tree, tvb, offset, sizeof(hdr.version),
419                         "Version: %u.%u",
420                         hi_nibble(hdr.version), lo_nibble(hdr.version));
421     offset += sizeof(hdr.version);
422
423     hdr.exch_type = tvb_get_guint8(tvb, offset);
424     proto_tree_add_text(isakmp_tree, tvb, offset, sizeof(hdr.exch_type),
425                         "Exchange type: %s (%u)",
426                         exchtype2str(hdr.exch_type), hdr.exch_type);
427     offset += sizeof(hdr.exch_type);
428
429     {
430       proto_item *      fti;
431       proto_tree *      ftree;
432
433       hdr.flags = tvb_get_guint8(tvb, offset);
434       fti   = proto_tree_add_text(isakmp_tree, tvb, offset, sizeof(hdr.flags), "Flags");
435       ftree = proto_item_add_subtree(fti, ett_isakmp_flags);
436
437       proto_tree_add_text(ftree, tvb, offset, 1, "%s",
438                           decode_boolean_bitfield(hdr.flags, E_FLAG, sizeof(hdr.flags)*8,
439                                                   "Encryption", "No encryption"));
440       proto_tree_add_text(ftree, tvb, offset, 1, "%s",
441                           decode_boolean_bitfield(hdr.flags, C_FLAG, sizeof(hdr.flags)*8,
442                                                   "Commit", "No commit"));
443       proto_tree_add_text(ftree, tvb, offset, 1, "%s",
444                           decode_boolean_bitfield(hdr.flags, A_FLAG, sizeof(hdr.flags)*8,
445                                                   "Authentication", "No authentication"));
446       offset += sizeof(hdr.flags);
447     }
448
449     proto_tree_add_text(isakmp_tree, tvb, offset, sizeof(hdr.message_id),
450         "Message ID: 0x%s", tvb_bytes_to_str(tvb, offset, sizeof(hdr.message_id)));
451     offset += sizeof(hdr.message_id);
452
453     proto_tree_add_text(isakmp_tree, tvb, offset, sizeof(hdr.length),
454                         "Length: %u", hdr.length);
455     offset += sizeof(hdr.length);
456
457     len = hdr.length - sizeof(hdr);
458
459     if (hdr.flags & E_FLAG) {
460       if (len && isakmp_tree) {
461         proto_tree_add_text(isakmp_tree, tvb, offset, len,
462                         "Encrypted payload (%d byte%s)",
463                         len, plurality(len, "", "s"));
464       }
465     } else
466       dissect_payloads(tvb, isakmp_tree, hdr.next_payload, offset, len);
467   }
468 }
469
470 static proto_tree *
471 dissect_payload_header(tvbuff_t *tvb, int offset, int length, guint8 payload,
472     guint8 *next_payload_p, guint16 *payload_length_p, proto_tree *tree)
473 {
474   guint8                next_payload;
475   guint16               payload_length;
476   proto_item *          ti;
477   proto_tree *          ntree;
478
479   if (length < 4) {
480     proto_tree_add_text(tree, tvb, offset, length,
481           "Not enough room in payload for all transforms");
482     return NULL;
483   }
484   next_payload = tvb_get_guint8(tvb, offset);
485   payload_length = tvb_get_ntohs(tvb, offset + 2);
486
487   ti = proto_tree_add_text(tree, tvb, offset, payload_length,
488             "%s payload", loadtype2str(payload));
489   ntree = proto_item_add_subtree(ti, ett_isakmp_payload);
490
491   proto_tree_add_text(ntree, tvb, offset, 1,
492                       "Next payload: %s (%u)",
493                       payloadtype2str(next_payload), next_payload);
494   proto_tree_add_text(ntree, tvb, offset+2, 2, "Length: %u", payload_length);
495
496   *next_payload_p = next_payload;
497   *payload_length_p = payload_length;
498   return ntree;
499 }
500
501 static void
502 dissect_sa(tvbuff_t *tvb, int offset, int length, proto_tree *tree,
503     int unused _U_)
504 {
505   guint32               doi;
506   guint32               situation;
507
508   if (length < 4) {
509     proto_tree_add_text(tree, tvb, offset, length,
510                         "DOI %s (length is %u, should be >= 4)",
511                         tvb_bytes_to_str(tvb, offset, length), length);
512     return;
513   }
514   doi = tvb_get_ntohl(tvb, offset);
515   proto_tree_add_text(tree, tvb, offset, 4,
516                       "Domain of interpretation: %s (%u)",
517                       doitype2str(doi), doi);
518   offset += 4;
519   length -= 4;
520
521   if (doi == 1) {
522     /* IPSEC */
523     if (length < 4) {
524       proto_tree_add_text(tree, tvb, offset, length,
525                           "Situation: %s (length is %u, should be >= 4)",
526                           tvb_bytes_to_str(tvb, offset, length), length);
527       return;
528     }
529     situation = tvb_get_ntohl(tvb, offset);
530     proto_tree_add_text(tree, tvb, offset, 4,
531                         "Situation: %s (%u)",
532                         situation2str(situation), situation);
533     offset += 4;
534     length -= 4;
535
536     dissect_payloads(tvb, tree, LOAD_TYPE_PROPOSAL, offset, length);
537   } else {
538     /* Unknown */
539     proto_tree_add_text(tree, tvb, offset, length,
540                         "Situation: %s",
541                         tvb_bytes_to_str(tvb, offset, length));
542   }
543 }
544
545 static void
546 dissect_proposal(tvbuff_t *tvb, int offset, int length, proto_tree *tree,
547     int unused _U_)
548 {
549   guint8                protocol_id;
550   guint8                spi_size;
551   guint8                num_transforms;
552   guint8                next_payload;
553   guint16               payload_length;
554   proto_tree *          ntree;
555   guint8                proposal_num;
556
557   proposal_num = tvb_get_guint8(tvb, offset);
558
559   proto_item_append_text(tree, " # %d",proposal_num);
560   proto_tree_add_text(tree, tvb, offset, 1,
561                       "Proposal number: %u", proposal_num);
562   offset += 1;
563   length -= 1;
564
565   protocol_id = tvb_get_guint8(tvb, offset);
566   proto_tree_add_text(tree, tvb, offset, 1,
567                       "Protocol ID: %s (%u)",
568                       proto2str(protocol_id), protocol_id);
569   offset += 1;
570   length -= 1;
571
572   spi_size = tvb_get_guint8(tvb, offset);
573   proto_tree_add_text(tree, tvb, offset, 1,
574                       "SPI size: %u", spi_size);
575   offset += 1;
576   length -= 1;
577
578   num_transforms = tvb_get_guint8(tvb, offset);
579   proto_tree_add_text(tree, tvb, offset, 1,
580                       "Number of transforms: %u", num_transforms);
581   offset += 1;
582   length -= 1;
583
584   if (spi_size) {
585     proto_tree_add_text(tree, tvb, offset, spi_size, "SPI: %s",
586                         tvb_bytes_to_str(tvb, offset, spi_size));
587     offset += spi_size;
588     length -= spi_size;
589   }
590
591   while (num_transforms > 0) {
592     ntree = dissect_payload_header(tvb, offset, length, LOAD_TYPE_TRANSFORM,
593       &next_payload, &payload_length, tree);
594     if (ntree == NULL)
595       break;
596     if (length < payload_length) {
597       proto_tree_add_text(tree, tvb, offset + 4, length,
598           "Not enough room in payload for all transforms");
599       break;
600     }
601     if (payload_length >= 4)
602       dissect_transform(tvb, offset + 4, payload_length - 4, ntree, protocol_id);
603     else
604       proto_tree_add_text(ntree, tvb, offset + 4, payload_length - 4, "Payload");
605     offset += payload_length;
606     length -= payload_length;
607     num_transforms--;
608   }
609 }
610
611 static void
612 dissect_transform(tvbuff_t *tvb, int offset, int length, proto_tree *tree,
613     int protocol_id)
614 {
615   guint8                transform_id;
616   guint8                transform_num;
617
618   transform_num = tvb_get_guint8(tvb, offset);
619   proto_item_append_text(tree," # %d",transform_num);
620   proto_tree_add_text(tree, tvb, offset, 1,
621                       "Transform number: %u", transform_num);
622   offset += 1;
623   length -= 1;
624
625   transform_id = tvb_get_guint8(tvb, offset);
626   switch (protocol_id) {
627   default:
628     proto_tree_add_text(tree, tvb, offset, 1,
629                         "Transform ID: %u", transform_id);
630     break;
631   case 1:       /* ISAKMP */
632     proto_tree_add_text(tree, tvb, offset, 1,
633                         "Transform ID: %s (%u)",
634                         trans2str(transform_id), transform_id);
635     break;
636   case 2:       /* AH */
637     proto_tree_add_text(tree, tvb, offset, 1,
638                         "Transform ID: %s (%u)",
639                         ah_trans2str(transform_id), transform_id);
640     break;
641   case 3:       /* ESP */
642     proto_tree_add_text(tree, tvb, offset, 1,
643                         "Transform ID: %s (%u)",
644                         esp_trans2str(transform_id), transform_id);
645     break;
646   case 4:       /* IPCOMP */
647     proto_tree_add_text(tree, tvb, offset, 1,
648                         "Transform ID: %s (%u)",
649                         ipcomp_trans2str(transform_id), transform_id);
650     break;
651   }
652   offset += 3;
653   length -= 3;
654
655   while (length>0) {
656     const char *str;
657     int ike_phase1 = 0;
658     guint16 aft     = tvb_get_ntohs(tvb, offset);
659     guint16 type    = aft & 0x7fff;
660     guint16 len;
661     guint32 val;
662     guint pack_len;
663
664     if (protocol_id == 1 && transform_id == 1) {
665       ike_phase1 = 1;
666       str = p1_atttype2str(type);
667     }
668     else {
669       str = atttype2str(type);
670     }
671
672     if (aft & 0x8000) {
673       val = tvb_get_ntohs(tvb, offset + 2);
674       proto_tree_add_text(tree, tvb, offset, 4,
675                           "%s (%u): %s (%u)",
676                           str, type,
677                           value2str(ike_phase1, type, val), val);
678       offset += 4;
679       length -= 4;
680     }
681     else {
682       len = tvb_get_ntohs(tvb, offset + 2);
683       pack_len = 4 + len;
684       if (!get_num(tvb, offset + 4, len, &val)) {
685         proto_tree_add_text(tree, tvb, offset, pack_len,
686                             "%s (%u): <too big (%u bytes)>",
687                             str, type, len);
688       } else {
689         proto_tree_add_text(tree, tvb, offset, pack_len,
690                             "%s (%u): %s (%u)",
691                             str, type,
692                             value2str(ike_phase1, type, val), val);
693       }
694       offset += pack_len;
695       length -= pack_len;
696     }
697   }
698 }
699
700 static void
701 dissect_key_exch(tvbuff_t *tvb, int offset, int length, proto_tree *tree,
702     int unused _U_)
703 {
704   proto_tree_add_text(tree, tvb, offset, length, "Key Exchange Data");
705 }
706
707 static void
708 dissect_id(tvbuff_t *tvb, int offset, int length, proto_tree *tree,
709     int unused _U_)
710 {
711   guint8                id_type;
712   guint8                protocol_id;
713   guint16               port;
714
715   id_type = tvb_get_guint8(tvb, offset);
716   proto_tree_add_text(tree, tvb, offset, 1,
717                       "ID type: %s (%u)", id2str(id_type), id_type);
718   offset += 1;
719   length -= 1;
720
721   protocol_id = tvb_get_guint8(tvb, offset);
722   if (protocol_id == 0) {
723     proto_tree_add_text(tree, tvb, offset, 1,
724                         "Protocol ID: Unused");
725   } else {
726     proto_tree_add_text(tree, tvb, offset, 1,
727                         "Protocol ID: %s (%u)",
728                         ipprotostr(protocol_id), protocol_id);
729   }
730   offset += 1;
731   length -= 1;
732
733   port = tvb_get_ntohs(tvb, offset);
734   if (port == 0)
735     proto_tree_add_text(tree, tvb, offset, 2, "Port: Unused");
736   else
737     proto_tree_add_text(tree, tvb, offset, 2, "Port: %u", port);
738   offset += 2;
739   length -= 2;
740
741   switch (id_type) {
742     case 1:
743       proto_tree_add_text(tree, tvb, offset, length,
744                           "Identification data: %s",
745                           ip_to_str(tvb_get_ptr(tvb, offset, 4)));
746       break;
747     case 2:
748     case 3:
749       proto_tree_add_text(tree, tvb, offset, length,
750                           "Identification data: %.*s", length,
751                           tvb_get_ptr(tvb, offset, length));
752       break;
753     case 4:
754       proto_tree_add_text(tree, tvb, offset, length,
755                           "Identification data: %s/%s",
756                           ip_to_str(tvb_get_ptr(tvb, offset, 4)),
757                           ip_to_str(tvb_get_ptr(tvb, offset+4, 4)));
758       break;
759     default:
760       proto_tree_add_text(tree, tvb, offset, length, "Identification Data");
761       break;
762   }
763 }
764
765 static void
766 dissect_cert(tvbuff_t *tvb, int offset, int length, proto_tree *tree,
767     int unused _U_)
768 {
769   guint8                cert_enc;
770
771   cert_enc = tvb_get_guint8(tvb, offset);
772   proto_tree_add_text(tree, tvb, offset, 1,
773                       "Certificate encoding: %u - %s",
774                       cert_enc, certtype2str(cert_enc));
775   offset += 1;
776   length -= 1;
777
778   proto_tree_add_text(tree, tvb, offset, length, "Certificate Data");
779 }
780
781 static void
782 dissect_certreq(tvbuff_t *tvb, int offset, int length, proto_tree *tree,
783     int unused _U_)
784 {
785   guint8                cert_type;
786
787   cert_type = tvb_get_guint8(tvb, offset);
788   proto_tree_add_text(tree, tvb, offset, 1,
789                       "Certificate type: %u - %s",
790                       cert_type, certtype2str(cert_type));
791   offset += 1;
792   length -= 1;
793
794   proto_tree_add_text(tree, tvb, offset, length, "Certificate Authority");
795 }
796
797 static void
798 dissect_hash(tvbuff_t *tvb, int offset, int length, proto_tree *tree,
799     int unused _U_)
800 {
801   proto_tree_add_text(tree, tvb, offset, length, "Hash Data");
802 }
803
804 static void
805 dissect_sig(tvbuff_t *tvb, int offset, int length, proto_tree *tree,
806     int unused _U_)
807 {
808   proto_tree_add_text(tree, tvb, offset, length, "Signature Data");
809 }
810
811 static void
812 dissect_nonce(tvbuff_t *tvb, int offset, int length, proto_tree *tree,
813     int unused _U_)
814 {
815   proto_tree_add_text(tree, tvb, offset, length, "Nonce Data");
816 }
817
818 static void
819 dissect_notif(tvbuff_t *tvb, int offset, int length, proto_tree *tree,
820     int unused _U_)
821 {
822   guint32               doi;
823   guint8                protocol_id;
824   guint8                spi_size;
825   guint16               msgtype;
826
827   doi = tvb_get_ntohl(tvb, offset);
828   proto_tree_add_text(tree, tvb, offset, 4,
829                       "Domain of Interpretation: %s (%u)",
830                       doitype2str(doi), doi);
831   offset += 4;
832   length -= 4;
833
834   protocol_id = tvb_get_guint8(tvb, offset);
835   proto_tree_add_text(tree, tvb, offset, 1,
836                       "Protocol ID: %s (%u)",
837                       proto2str(protocol_id), protocol_id);
838   offset += 1;
839   length -= 1;
840
841   spi_size = tvb_get_guint8(tvb, offset);
842   proto_tree_add_text(tree, tvb, offset, 1,
843                       "SPI size: %u", spi_size);
844   offset += 1;
845   length -= 1;
846
847   msgtype = tvb_get_ntohs(tvb, offset);
848   proto_tree_add_text(tree, tvb, offset, 2,
849                       "Message type: %s (%u)", msgtype2str(msgtype), msgtype);
850   offset += 2;
851   length -= 2;
852
853   if (spi_size) {
854     proto_tree_add_text(tree, tvb, offset, spi_size, "Security Parameter Index");
855     offset += spi_size;
856     length -= spi_size;
857   }
858
859   if (length > 0)
860     proto_tree_add_text(tree, tvb, offset, length, "Notification Data");
861 }
862
863 static void
864 dissect_delete(tvbuff_t *tvb, int offset, int length, proto_tree *tree,
865     int unused _U_)
866 {
867   guint32               doi;
868   guint8                protocol_id;
869   guint8                spi_size;
870   guint16               num_spis;
871   guint16               i;
872
873   doi = tvb_get_ntohl(tvb, offset);
874   proto_tree_add_text(tree, tvb, offset, 4,
875                       "Domain of Interpretation: %s (%u)",
876                       doitype2str(doi), doi);
877   offset += 4;
878   length -= 4;
879
880   protocol_id = tvb_get_guint8(tvb, offset);
881   proto_tree_add_text(tree, tvb, offset, 1,
882                       "Protocol ID: %s (%u)",
883                       proto2str(protocol_id), protocol_id);
884   offset += 1;
885   length -= 1;
886
887   spi_size = tvb_get_guint8(tvb, offset);
888   proto_tree_add_text(tree, tvb, offset, 1,
889                       "SPI size: %u", spi_size);
890   offset += 1;
891   length -= 1;
892
893   num_spis = tvb_get_ntohs(tvb, offset);
894   proto_tree_add_text(tree, tvb, offset, 2,
895                       "Number of SPIs: %u", num_spis);
896   offset += 2;
897   length -= 2;
898
899   for (i = 0; i < num_spis; ++i) {
900     if (length < spi_size) {
901       proto_tree_add_text(tree, tvb, offset, length,
902           "Not enough room in payload for all SPI's");
903       break;
904     }
905     proto_tree_add_text(tree, tvb, offset, spi_size,
906                         "SPI (%d)", i);
907     offset += spi_size;
908     length -= spi_size;
909   }
910 }
911
912 static void
913 dissect_vid(tvbuff_t *tvb, int offset, int length, proto_tree *tree,
914     int unused _U_)
915 {
916   guint32 CPproduct, CPversion;
917   const guint8 * pVID;
918   proto_item * pt;
919   proto_tree * ntree;
920   pVID = tvb_get_ptr(tvb, offset, length);
921   pt = proto_tree_add_text(tree, tvb, offset, length, "Vendor ID: ");
922   if (memcmp(pVID, VID_MS_W2K_WXP, isakmp_min(VID_MS_LEN, length)) == 0)
923         proto_item_append_text(pt, "Microsoft Win2K/WinXP");
924   else
925   if (memcmp(pVID, VID_CP, isakmp_min(VID_CP_LEN, length)) == 0)
926   {
927         proto_item_append_text(pt, "Check Point");
928         offset += VID_CP_LEN;
929         CPproduct = tvb_get_ntohl(tvb, offset);
930         ntree = proto_item_add_subtree(pt, ett_isakmp_payload);
931         pt = proto_tree_add_text(ntree, tvb, offset, sizeof(CPproduct), "Check Point Product: ");
932         switch (CPproduct) {
933                 case 1: proto_item_append_text(pt, "VPN-1");
934                         break;
935                 case 2: proto_item_append_text(pt, "SecuRemote/SecureClient");
936                         break;
937                 default: proto_item_append_text(pt, "Unknown CP product!");
938                         break;
939         }
940         offset += sizeof(CPproduct);
941         CPversion = tvb_get_ntohl(tvb, offset);
942         pt = proto_tree_add_text(ntree, tvb, offset, length, "Version: ");
943         switch (CPversion) {
944                 case 2: proto_item_append_text(pt, "4.1");
945                         break;
946                 case 3: proto_item_append_text(pt, "4.1 SP-1");
947                         break;
948                 case 4002: proto_item_append_text(pt, "4.1 (SP-2 or above)");
949                         break;
950                 case 5000: proto_item_append_text(pt, "NG");
951                         break;
952                 case 5001: proto_item_append_text(pt, "NG Feature Pack 1");
953                         break;
954                 case 5002: proto_item_append_text(pt, "NG Feature Pack 2");
955                         break;
956                 case 5003: proto_item_append_text(pt, "NG Feature Pack 3");
957                         break;
958                 default: proto_item_append_text(pt, " Uknown CP version!");
959                         break;
960         }
961   }
962   else
963   if (memcmp(pVID, VID_CYBERGUARD, isakmp_min(VID_LEN, length)) == 0)
964         proto_item_append_text(pt, "Cyber Guard");
965   else
966   if (memcmp(pVID,  VID_draft_ietf_ipsec_nat_t_ike_03, isakmp_min(VID_LEN, length)) == 0)
967         proto_item_append_text(pt, "draft-ietf-ipsec-nat-t-ike-03");
968   else
969         proto_item_append_text(pt, "unknown vendor ID: 0x%s",tvb_bytes_to_str(tvb, offset, length));
970 }
971
972 static void
973 dissect_config(tvbuff_t *tvb, int offset, int length, proto_tree *tree,
974     int unused _U_)
975 {
976   guint8                type;
977
978   type = tvb_get_guint8(tvb, offset);
979   proto_tree_add_text(tree, tvb, offset, 1,
980                       "Type %s (%u)",attrtype2str(type),type);
981
982   offset += 2;
983   length -= 2;
984
985   proto_tree_add_text(tree, tvb, offset, 2,
986                       "Identifier: %u", tvb_get_ntohs(tvb, offset));
987   offset += 2;
988   length -= 2;
989
990   while(length>0) {
991     guint16 aft     = tvb_get_ntohs(tvb, offset);
992     guint16 type    = aft & 0x7fff;
993     guint16 len;
994     guint32 val;
995     guint pack_len;
996
997     if (aft & 0x8000) {
998       val = tvb_get_ntohs(tvb, offset + 2);
999       proto_tree_add_text(tree, tvb, offset, 4,
1000                           "%s (%u)", cfgattrident2str(type), val);
1001       offset += 4;
1002       length -= 4;
1003     }
1004     else {
1005       len = tvb_get_ntohs(tvb, offset + 2);
1006       pack_len = 4 + len;
1007       if (!get_num(tvb, offset + 4, len, &val)) {
1008         proto_tree_add_text(tree, tvb, offset, pack_len,
1009                             "%s: <too big (%u bytes)>",
1010                             cfgattrident2str(type), len);
1011       } else {
1012         proto_tree_add_text(tree, tvb, offset, 4,
1013                             "%s (%ue)", cfgattrident2str(type),
1014                             val);
1015       }
1016       offset += pack_len;
1017       length -= pack_len;
1018     }
1019   }
1020 }
1021
1022 static const char *
1023 payloadtype2str(guint8 type) {
1024
1025   if (type < NUM_LOAD_TYPES)
1026     return strfuncs[type].str;
1027   if (type < 128)
1028     return "RESERVED";
1029   return "Private USE";
1030 }
1031
1032 static const char *
1033 exchtype2str(guint8 type) {
1034
1035 #define NUM_EXCHSTRS    7
1036   static const char * exchstrs[NUM_EXCHSTRS] = {
1037     "NONE",
1038     "Base",
1039     "Identity Protection (Main Mode)",
1040     "Authentication Only",
1041     "Aggressive",
1042     "Informational",
1043     "Transaction (Config Mode)"
1044   };
1045
1046   if (type < NUM_EXCHSTRS) return exchstrs[type];
1047   if (type < 32)           return "ISAKMP Future Use";
1048   switch (type) {
1049   case 32:
1050     return "Quick Mode";
1051   case 33:
1052     return "New Group Mode";
1053   }
1054   if (type < 240)
1055     return "DOI Specific Use";
1056   return "Private Use";
1057 }
1058
1059 static const char *
1060 doitype2str(guint32 type) {
1061   if (type == 1) return "IPSEC";
1062   return "Unknown DOI Type";
1063 }
1064
1065 static const char *
1066 msgtype2str(guint16 type) {
1067
1068 #define NUM_PREDEFINED  31
1069   static const char *msgs[NUM_PREDEFINED] = {
1070     "<UNKNOWN>",
1071     "INVALID-PAYLOAD-TYPE",
1072     "DOI-NOT-SUPPORTED",
1073     "SITUATION-NOT-SUPPORTED",
1074     "INVALID-COOKIE",
1075     "INVALID-MAJOR-VERSION",
1076     "INVALID-MINOR-VERSION",
1077     "INVALID-EXCHANGE-TYPE",
1078     "INVALID-FLAGS",
1079     "INVALID-MESSAGE-ID",
1080     "INVALID-PROTOCOL-ID",
1081     "INVALID-SPI",
1082     "INVALID-TRANSFORM-ID",
1083     "ATTRIBUTES-NOT-SUPPORTED",
1084     "NO-PROPOSAL-CHOSEN",
1085     "BAD-PROPOSAL-SYNTAX",
1086     "PAYLOAD-MALFORMED",
1087     "INVALID-KEY-INFORMATION",
1088     "INVALID-ID-INFORMATION",
1089     "INVALID-CERT-ENCODING",
1090     "INVALID-CERTIFICATE",
1091     "CERT-TYPE-UNSUPPORTED",
1092     "INVALID-CERT-AUTHORITY",
1093     "INVALID-HASH-INFORMATION",
1094     "AUTHENTICATION-FAILED",
1095     "INVALID-SIGNATURE",
1096     "ADDRESS-NOTIFICATION",
1097     "NOTIFY-SA-LIFETIME",
1098     "CERTIFICATE-UNAVAILABLE",
1099     "UNSUPPORTED-EXCHANGE-TYPE",
1100     "UNEQUAL-PAYLOAD-LENGTHS"
1101   };
1102
1103   if (type < NUM_PREDEFINED) return msgs[type];
1104   if (type < 8192)           return "RESERVED (Future Use)";
1105   if (type < 16384)          return "Private Use";
1106   if (type < 16385)          return "CONNECTED";
1107   if (type < 24576)          return "RESERVED (Future Use) - status";
1108   if (type < 24577)          return "RESPONDER-LIFETIME";
1109   if (type < 24578)          return "REPLAY-STATUS";
1110   if (type < 24579)          return "INITIAL-CONTACT";
1111   if (type < 32768)          return "DOI-specific codes";
1112   if (type < 40960)          return "Private Use - status";
1113   if (type < 65535)          return "RESERVED (Future Use) - status (2)";
1114
1115   return "Huh? You should never see this! Shame on you!";
1116 }
1117
1118 static const char *
1119 situation2str(guint32 type) {
1120
1121 #define SIT_MSG_NUM     1024
1122 #define SIT_IDENTITY    0x01
1123 #define SIT_SECRECY     0x02
1124 #define SIT_INTEGRITY   0x04
1125
1126   static char   msg[SIT_MSG_NUM];
1127   int           n = 0;
1128   char *        sep = "";
1129   int           ret;
1130
1131   if (type & SIT_IDENTITY) {
1132     ret = snprintf(msg, SIT_MSG_NUM-n, "%sIDENTITY", sep);
1133     if (ret == -1 || ret >= SIT_MSG_NUM-n) {
1134       /* Truncated. */
1135       msg[SIT_MSG_NUM-1] = '\0';
1136       return msg;
1137     }
1138     n += ret;
1139     sep = " & ";
1140   }
1141   if (type & SIT_SECRECY) {
1142     if (n >= SIT_MSG_NUM) {
1143       /* No more room. */
1144       return msg;
1145     }
1146     ret = snprintf(msg, SIT_MSG_NUM-n, "%sSECRECY", sep);
1147     if (ret == -1 || ret >= SIT_MSG_NUM-n) {
1148       /* Truncated. */
1149       msg[SIT_MSG_NUM-1] = '\0';
1150       return msg;
1151     }
1152     n += ret;
1153     sep = " & ";
1154   }
1155   if (type & SIT_INTEGRITY) {
1156     if (n >= SIT_MSG_NUM) {
1157       /* No more room. */
1158       return msg;
1159     }
1160     ret = snprintf(msg, SIT_MSG_NUM-n, "%sINTEGRITY", sep);
1161     if (ret == -1 || ret >= SIT_MSG_NUM-n) {
1162       /* Truncated. */
1163       msg[SIT_MSG_NUM-1] = '\0';
1164       return msg;
1165     }
1166     n += ret;
1167     sep = " & ";
1168   }
1169
1170   return msg;
1171 }
1172
1173 static const char *
1174 value2str(int ike_p1, guint16 att_type, guint16 value) {
1175
1176   if (value == 0) return "RESERVED";
1177
1178   if (!ike_p1) {
1179   switch (att_type) {
1180     case 1:
1181       switch (value) {
1182         case 0: return "RESERVED";
1183         case 1:  return "Seconds";
1184         case 2:  return "Kilobytes";
1185         default: return "UNKNOWN-SA-VALUE";
1186       }
1187     case 2:
1188       return "Duration-Value";
1189     case 3:
1190       return "Group-Value";
1191     case 4:
1192       switch (value) {
1193         case 0:  return "RESERVED";
1194         case 1:  return "Tunnel";
1195         case 2:  return "Transport";
1196         case 3:  return "UDP-Encapsulated-Tunnel"; /* http://www.ietf.org/internet-drafts/draft-ietf-ipsec-nat-t-ike-05.txt */
1197         case 4:  return "UDP-Encapsulated-Transport"; /* http://www.ietf.org/internet-drafts/draft-ietf-ipsec-nat-t-ike-05.txt */
1198         case 61440: return "Check Point IPSec UDP Encapsulation";
1199         case 61443: return "UDP-Encapsulated-Tunnel (draft)";
1200         case 61444: return "UDP-Encapsulated-Transport (draft)";
1201         default: return "UNKNOWN-ENCAPSULATION-VALUE";
1202       }
1203     case 5:
1204       switch (value) {
1205         case 0:  return "RESERVED";
1206         case 1:  return "HMAC-MD5";
1207         case 2:  return "HMAC-SHA";
1208         case 3:  return "DES-MAC";
1209         case 4:  return "KPDK";
1210         case 5:  return "HMAC-SHA2-256";
1211         case 6:  return "HMAC-SHA2-384";
1212         case 7:  return "HMAC-SHA2-512";
1213         default: return "UNKNOWN-AUTHENTICATION-VALUE";
1214       }
1215     case 6:
1216       return "Key-Length";
1217     case 7:
1218       return "Key-Rounds";
1219     case 8:
1220       return "Compress-Dictionary-size";
1221     case 9:
1222       return "Compress Private Algorithm";
1223     default: return "UNKNOWN-ATTRIBUTE-TYPE";
1224   }
1225   }
1226   else {
1227     switch (att_type) {
1228       case 1:
1229         switch (value) {
1230           case 1:  return "DES-CBC";
1231           case 2:  return "IDEA-CBC";
1232           case 3:  return "BLOWFISH-CBC";
1233           case 4:  return "RC5-R16-B64-CBC";
1234           case 5:  return "3DES-CBC";
1235           case 6:  return "CAST-CBC";
1236           case 7:  return "AES-CBC";
1237           default: return "UNKNOWN-ENCRYPTION-ALG";
1238         }
1239       case 2:
1240         switch (value) {
1241           case 1:  return "MD5";
1242           case 2:  return "SHA";
1243           case 3:  return "TIGER";
1244           case 4:  return "SHA2-256";
1245           case 5:  return "SHA2-384";
1246           case 6:  return "SHA2-512";
1247           default: return "UNKNOWN-HASH-ALG";
1248         }
1249       case 3:
1250         switch (value) {
1251           case 1:  return "PSK";
1252           case 2:  return "DSS-SIG";
1253           case 3:  return "RSA-SIG";
1254           case 4:  return "RSA-ENC";
1255           case 5:  return "RSA-Revised-ENC";
1256           case 6:  return "Encryption with El-Gamal";
1257           case 7:  return "Revised encryption with El-Gamal";
1258           case 8:  return "ECDSA signatures";
1259           case 9:  return "AES-XCBC-MAC";
1260           case 64221: return "HybridInitRSA";
1261           case 64222: return "HybridRespRSA";
1262           case 64223: return "HybridInitDSS";
1263           case 64224: return "HybridRespDSS";
1264           case 65001: return "XAUTHInitPreShared";
1265           case 65002: return "XAUTHRespPreShared";
1266           case 65003: return "XAUTHInitDSS";
1267           case 65004: return "XAUTHRespDSS";
1268           case 65005: return "XAUTHInitRSA";
1269           case 65006: return "XAUTHRespRSA";
1270           case 65007: return "XAUTHInitRSAEncryption";
1271           case 65008: return "XAUTHRespRSAEncryption";
1272           case 65009: return "XAUTHInitRSARevisedEncryption";
1273           case 65010: return "XAUTHRespRSARevisedEncryption";
1274           default: return "UNKNOWN-AUTH-METHOD";
1275         }
1276       case 4: return grpdesc2str(value);
1277       case 6:
1278       case 7:
1279       case 8:
1280       case 9:
1281       case 10:
1282       case 16:
1283         return "Group-Value";
1284       case 5:
1285         switch (value) {
1286           case 1:  return "MODP";
1287           case 2:  return "ECP";
1288           case 3:  return "EC2N";
1289           default: return "UNKNOWN-GROUPT-TYPE";
1290         }
1291       case 11:
1292         switch (value) {
1293           case 1:  return "Seconds";
1294           case 2:  return "Kilobytes";
1295           default: return "UNKNOWN-SA-VALUE";
1296         }
1297       case 12:
1298         return "Duration-Value";
1299       case 13:
1300         return "PRF-Value";
1301       case 14:
1302         return "Key-Length";
1303       case 15:
1304         return "Field-Size";
1305       default: return "UNKNOWN-ATTRIBUTE-TYPE";
1306     }
1307   }
1308 }
1309
1310 static const char *
1311 attrtype2str(guint8 type) {
1312   switch (type) {
1313   case 0: return "Reserved";
1314   case 1: return "ISAKMP_CFG_REQUEST";
1315   case 2: return "ISAKMP_CFG_REPLY";
1316   case 3: return "ISAKMP_CFG_SET";
1317   case 4: return "ISAKMP_CFG_ACK";
1318   }
1319   if(type < 127)
1320     return "Future use";
1321   return "Private use";
1322 }
1323
1324 static const char *
1325 cfgattrident2str(guint16 ident) {
1326 #define NUM_ATTR_DEFINED        12
1327   static const char *msgs[NUM_PREDEFINED] = {
1328     "RESERVED",
1329     "INTERNAL_IP4_ADDRESS",
1330     "INTERNAL_IP4_NETMASK",
1331     "INTERNAL_IP4_DNS",
1332     "INTERNAL_IP4_NBNS",
1333     "INTERNAL_ADDRESS_EXPIREY",
1334     "INTERNAL_IP4_DHCP",
1335     "APPLICATION_VERSION"
1336     "INTERNAL_IP6_ADDRESS",
1337     "INTERNAL_IP6_NETMASK",
1338     "INTERNAL_IP6_DNS",
1339     "INTERNAL_IP6_NBNS",
1340     "INTERNAL_IP6_DHCP",
1341   };
1342   if(ident < NUM_ATTR_DEFINED)
1343     return msgs[ident];
1344   if(ident < 16383)
1345     return "Future use";
1346   switch(ident) {
1347   case 16520: return "XAUTH_TYPE";
1348   case 16521: return "XAUTH_USER_NAME";
1349   case 16522: return "XAUTH_USER_PASSWORD";
1350   case 16523: return "XAUTH_PASSCODE";
1351   case 16524: return "XAUTH_MESSAGE";
1352   case 16525: return "XAUTH_CHALLANGE";
1353   case 16526: return "XAUTH_DOMAIN";
1354   case 16527: return "XAUTH_STATUS";
1355   case 16528: return "XAUTH_NEXT_PIN";
1356   case 16529: return "XAUTH_ANSWER";
1357   default: return "Private use";
1358   }
1359 }
1360
1361 static const char *
1362 certtype2str(guint8 type) {
1363 #define NUM_CERTTYPE 11
1364   static const char *msgs[NUM_CERTTYPE] = {
1365     "NONE",
1366     "PKCS #7 wrapped X.509 certificate",
1367     "PGP Certificate",
1368     "DNS Signed Key",
1369     "X.509 Certificate - Signature",
1370     "X.509 Certificate - Key Exchange",
1371     "Kerberos Tokens",
1372     "Certificate Revocation List (CRL)",
1373     "Authority Revocation List (ARL)",
1374     "SPKI Certificate",
1375     "X.509 Certificate - Attribute",
1376   };
1377   if(type > NUM_CERTTYPE)
1378     return "RESERVED";
1379   return msgs[type];
1380 }
1381
1382 static gboolean
1383 get_num(tvbuff_t *tvb, int offset, guint16 len, guint32 *num_p) {
1384
1385   switch (len) {
1386   case 1:
1387     *num_p = tvb_get_guint8(tvb, offset);
1388     break;
1389   case 2:
1390     *num_p = tvb_get_ntohs(tvb, offset);
1391     break;
1392   case 3:
1393     *num_p = tvb_get_ntoh24(tvb, offset);
1394     break;
1395   case 4:
1396     *num_p = tvb_get_ntohl(tvb, offset);
1397     break;
1398   default:
1399     return FALSE;
1400   }
1401
1402   return TRUE;
1403 }
1404
1405 void
1406 proto_register_isakmp(void)
1407 {
1408 /*  static hf_register_info hf[] = {
1409     { &variable,
1410     { "Name",           "isakmp.abbreviation", TYPE, VALS_POINTER }},
1411   };*/
1412   static gint *ett[] = {
1413     &ett_isakmp,
1414     &ett_isakmp_flags,
1415     &ett_isakmp_payload,
1416   };
1417
1418   proto_isakmp = proto_register_protocol("Internet Security Association and Key Management Protocol",
1419                                                "ISAKMP", "isakmp");
1420 /*  proto_register_field_array(proto_isakmp, hf, array_length(hf));*/
1421   proto_register_subtree_array(ett, array_length(ett));
1422
1423   register_dissector("isakmp", dissect_isakmp, proto_isakmp);
1424 }
1425
1426 void
1427 proto_reg_handoff_isakmp(void)
1428 {
1429   dissector_handle_t isakmp_handle;
1430
1431   /*
1432    * Get handle for the AH & ESP dissectors.
1433    */
1434   esp_handle = find_dissector("esp");
1435   ah_handle = find_dissector("ah");
1436
1437   isakmp_handle = find_dissector("isakmp");
1438   dissector_add("udp.port", UDP_PORT_ISAKMP, isakmp_handle);
1439   dissector_add("tcp.port", TCP_PORT_ISAKMP, isakmp_handle);
1440 }