- dissect_ber_object_identifier() returns value as tvb
[obnox/wireshark/wip.git] / asn1 / pkixcmp / packet-cmp-template.c
1 /* packet-cmp.c
2  * Routines for RFC2510 Certificate Management Protocol packet dissection
3  *   Ronnie Sahlberg 2004
4  *
5  * $Id$
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <epan/packet.h>
32 #include <epan/conversation.h>
33
34 #include <stdio.h>
35 #include <string.h>
36
37 #include "packet-ber.h"
38 #include "packet-cmp.h"
39 #include "packet-crmf.h"
40 #include "packet-pkix1explicit.h"
41 #include "packet-pkix1implicit.h"
42 #include "packet-tcp.h"
43 #include <epan/prefs.h>
44 #include <epan/nstime.h>
45
46 #define PNAME  "Certificate Management Protocol"
47 #define PSNAME "CMP"
48 #define PFNAME "cmp"
49
50 #define TCP_PORT_CMP 829
51
52 /* desegmentation of CMP over TCP */
53 static gboolean cmp_desegment = TRUE;
54
55 /* Initialize the protocol and registered fields */
56 int proto_cmp = -1;
57 static int hf_cmp_type_oid = -1;
58 static int hf_cmp_rm = -1;
59 static int hf_cmp_type = -1;
60 static int hf_cmp_poll_ref = -1;
61 static int hf_cmp_next_poll_ref = -1;
62 static int hf_cmp_ttcb = -1;
63 #include "packet-cmp-hf.c"
64
65 /* Initialize the subtree pointers */
66 static gint ett_cmp = -1;
67 #include "packet-cmp-ett.c"
68
69 static const char *object_identifier_id;
70
71
72 #include "packet-cmp-fn.c"
73
74 static int
75 dissect_cmp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
76 {
77         return dissect_cmp_PKIMessage(FALSE, tvb, 0, pinfo, tree, -1);
78 }
79
80 #define CMP_TYPE_PKIMSG         0
81 #define CMP_TYPE_POLLREP        1
82 #define CMP_TYPE_POLLREQ        2
83 #define CMP_TYPE_NEGPOLLREP     3
84 #define CMP_TYPE_PARTIALMSGREP  4
85 #define CMP_TYPE_FINALMSGREP    5
86 #define CMP_TYPE_ERRORMSGREP    6
87 static const value_string cmp_pdu_types[] = {
88         { CMP_TYPE_PKIMSG,              "pkiMsg" },
89         { CMP_TYPE_POLLREP,             "pollRep" },
90         { CMP_TYPE_POLLREQ,             "pollReq" },
91         { CMP_TYPE_NEGPOLLREP,          "negPollRep" },
92         { CMP_TYPE_PARTIALMSGREP,       "partialMsgRep" },
93         { CMP_TYPE_FINALMSGREP,         "finalMsgRep" },
94         { CMP_TYPE_ERRORMSGREP,         "errorMsgRep" },
95         { 0, NULL },
96 };
97
98 static void dissect_cmp_tcp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
99 {
100         tvbuff_t   *next_tvb;
101         guint32 pdu_len;
102         guint8 pdu_type;
103         nstime_t        ts;
104
105         proto_item *item=NULL;
106         proto_tree *tree=NULL;
107
108         if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
109                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "CMP");
110
111         if (check_col(pinfo->cinfo, COL_INFO)) {
112                 col_clear(pinfo->cinfo, COL_INFO);
113                 
114                 col_add_fstr(pinfo->cinfo, COL_INFO, "PKIXCMP");
115         }
116
117
118         if(parent_tree){
119                 item=proto_tree_add_item(parent_tree, proto_cmp, tvb, 0, -1, FALSE);
120                 tree = proto_item_add_subtree(item, ett_cmp);
121         }
122
123         pdu_len=tvb_get_ntohl(tvb, 0);
124         pdu_type=tvb_get_guint8(tvb, 4);
125
126         proto_tree_add_uint(tree, hf_cmp_rm, tvb, 0, 4, pdu_len);
127         proto_tree_add_uint(tree, hf_cmp_type, tvb, 4, 1, pdu_type);
128
129         if (check_col (pinfo->cinfo, COL_INFO)) {
130             col_set_str (pinfo->cinfo, COL_INFO, val_to_str (pdu_type, cmp_pdu_types, "0x%x"));
131         }
132
133         switch(pdu_type){
134         case CMP_TYPE_PKIMSG:
135                 next_tvb = tvb_new_subset(tvb, 5, tvb_length_remaining(tvb, 5), pdu_len);
136                 dissect_cmp_pdu(next_tvb, pinfo, tree);
137                 break;
138         case CMP_TYPE_POLLREP:
139                 proto_tree_add_item(tree, hf_cmp_poll_ref, tvb, 0, 4, FALSE);
140
141                 ts.secs = tvb_get_ntohl(tvb, 4);
142                 ts.nsecs = 0;
143                 proto_tree_add_time(tree, hf_cmp_ttcb, tvb, 4, 4, &ts);
144                 break;
145         case CMP_TYPE_POLLREQ:
146                 proto_tree_add_item(tree, hf_cmp_poll_ref, tvb, 0, 4, FALSE);
147                 break;
148         case CMP_TYPE_NEGPOLLREP:
149                 break;
150         case CMP_TYPE_PARTIALMSGREP:
151                 proto_tree_add_item(tree, hf_cmp_next_poll_ref, tvb, 0, 4, FALSE);
152
153                 ts.secs = tvb_get_ntohl(tvb, 4);
154                 ts.nsecs = 0;
155                 proto_tree_add_time(tree, hf_cmp_ttcb, tvb, 4, 4, &ts);
156
157                 next_tvb = tvb_new_subset(tvb, 13, tvb_length_remaining(tvb, 13), pdu_len);
158                 dissect_cmp_pdu(next_tvb, pinfo, tree);
159                 break;
160         case CMP_TYPE_FINALMSGREP:
161                 next_tvb = tvb_new_subset(tvb, 5, tvb_length_remaining(tvb, 5), pdu_len);
162                 dissect_cmp_pdu(next_tvb, pinfo, tree);
163                 break;
164         case CMP_TYPE_ERRORMSGREP:
165                 /*XXX to be added*/
166                 break;
167         }
168
169 }
170
171 static guint get_cmp_pdu_len(tvbuff_t *tvb, int offset)
172 {
173   guint32 plen;
174
175   /*
176    * Get the length of the CMP-over-TCP packet.
177    */
178   plen = tvb_get_ntohl(tvb, offset);
179
180   return plen+4;
181 }
182
183 /* CMP over TCP    RFC2510 section 5.2 */
184 static int
185 dissect_cmp_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
186 {
187         guint32 pdu_len;
188         guint8 pdu_type;
189
190         /* only attempt to dissect it as CMP over TCP if we have
191          * at least 5 bytes.
192          */
193         if (!tvb_bytes_exist(tvb, 0, 5)) {
194                 return 0;
195         }
196
197         pdu_len=tvb_get_ntohl(tvb, 0);
198         pdu_type=tvb_get_guint8(tvb, 4);
199
200         /* arbitrary limit: assume a CMP over TCP pdu is never >10000 bytes 
201          * in size.
202          * It is definitely at least 1 byte to accomodate the flags byte 
203          */
204         if((pdu_len<=0)||(pdu_len>10000)){
205                 return 0;
206         }
207         /* type is between 0 and 6 */
208         if(pdu_type>6){
209                 return 0;
210         }
211         /* type 0 contains a PKI message and must therefore be >= 3 bytes 
212          * long (flags + BER TAG + BER LENGTH
213          */
214         if((pdu_type==0)&&(pdu_len<3)){
215                 return 0;
216         }
217
218         tcp_dissect_pdus(tvb, pinfo, parent_tree, cmp_desegment, 4, get_cmp_pdu_len,
219                 dissect_cmp_tcp_pdu);
220
221         return tvb_length(tvb);
222 }
223
224 static int
225 dissect_cmp_http(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
226 {
227         proto_item *item=NULL;
228         proto_tree *tree=NULL;
229
230         if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
231                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "CMP");
232
233         if (check_col(pinfo->cinfo, COL_INFO)) {
234                 col_clear(pinfo->cinfo, COL_INFO);
235                 
236                 col_add_fstr(pinfo->cinfo, COL_INFO, "PKIXCMP");
237         }
238
239
240         if(parent_tree){
241                 item=proto_tree_add_item(parent_tree, proto_cmp, tvb, 0, -1, FALSE);
242                 tree = proto_item_add_subtree(item, ett_cmp);
243         }
244
245         dissect_cmp_pdu(tvb, pinfo, tree);
246 }
247
248
249 /*--- proto_register_cmp ----------------------------------------------*/
250 void proto_register_cmp(void) {
251
252   /* List of fields */
253   static hf_register_info hf[] = {
254     { &hf_cmp_type_oid,
255       { "InfoType", "cmp.type.oid",
256         FT_STRING, BASE_NONE, NULL, 0,
257         "Type of InfoTypeAndValue", HFILL }},
258     { &hf_cmp_rm,
259       { "Record Marker", "cmp.rm",
260         FT_UINT32, BASE_DEC, NULL, 0,
261         "Record Marker  length of PDU in bytes", HFILL }},
262     { &hf_cmp_type,
263       { "Type", "cmp.type",
264         FT_UINT8, BASE_DEC, VALS(cmp_pdu_types), 0,
265         "PDU Type", HFILL }},
266     { &hf_cmp_poll_ref,
267       { "Polling Reference", "cmp.poll_ref",
268         FT_UINT32, BASE_HEX, NULL, 0,
269         "", HFILL }},
270     { &hf_cmp_next_poll_ref,
271       { "Next Polling Reference", "cmp.next_poll_ref",
272         FT_UINT32, BASE_HEX, NULL, 0,
273         "", HFILL }},
274     { &hf_cmp_ttcb,
275       { "Time to check Back", "cmp.ttcb",
276         FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0,
277         "", HFILL }},
278 #include "packet-cmp-hfarr.c"
279   };
280
281   /* List of subtrees */
282   static gint *ett[] = {
283     &ett_cmp,
284 #include "packet-cmp-ettarr.c"
285   };
286   module_t *cmp_module;
287
288   /* Register protocol */
289   proto_cmp = proto_register_protocol(PNAME, PSNAME, PFNAME);
290
291   /* Register fields and subtrees */
292   proto_register_field_array(proto_cmp, hf, array_length(hf));
293   proto_register_subtree_array(ett, array_length(ett));
294
295   cmp_module = prefs_register_protocol(proto_cmp, NULL);
296   prefs_register_bool_preference(cmp_module, "desegment",
297                 "Reassemble CMP-over-TCP messages spanning multiple TCP segments",
298                 "Whether the CMP-over-TCP dissector should reassemble messages spanning multiple TCP segments. "
299                 "To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
300                 &cmp_desegment);
301 }
302
303
304 /*--- proto_reg_handoff_cmp -------------------------------------------*/
305 void proto_reg_handoff_cmp(void) {
306         dissector_handle_t cmp_http_handle;
307         dissector_handle_t cmp_tcp_handle;
308
309         cmp_http_handle = new_create_dissector_handle(dissect_cmp_http, proto_cmp);
310         dissector_add_string("media_type", "application/pkixcmp", cmp_http_handle);
311
312         cmp_tcp_handle = new_create_dissector_handle(dissect_cmp_tcp, proto_cmp);
313         dissector_add("tcp.port", TCP_PORT_CMP, cmp_tcp_handle);
314
315 /*#include "packet-cmp-dis-tab.c"*/
316 }
317