aboutsummaryrefslogtreecommitdiffstats
path: root/asn1/pkixcmp/packet-cmp-template.c
blob: a6c31ad044cfa4ecf6086e999b937bb327d9f440 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* packet-cmp.c
 * Routines for RFC2510 Certificate Management Protocol packet dissection
 *   Ronnie Sahlberg 2004
 *
 * $Id$
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@ethereal.com>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <glib.h>
#include <epan/packet.h>
#include <epan/conversation.h>

#include <stdio.h>
#include <string.h>

#include "packet-ber.h"
#include "packet-cmp.h"
#include "packet-crmf.h"
#include "packet-pkix1explicit.h"
#include "packet-pkix1implicit.h"
#include <epan/emem.h>
#include "packet-tcp.h"
#include <epan/prefs.h>
#include <epan/nstime.h>

#define PNAME  "Certificate Management Protocol"
#define PSNAME "CMP"
#define PFNAME "cmp"

#define TCP_PORT_CMP 829

/* desegmentation of CMP over TCP */
static gboolean cmp_desegment = TRUE;

/* Initialize the protocol and registered fields */
int proto_cmp = -1;
static int hf_cmp_type_oid = -1;
static int hf_cmp_rm = -1;
static int hf_cmp_type = -1;
static int hf_cmp_poll_ref = -1;
static int hf_cmp_next_poll_ref = -1;
static int hf_cmp_ttcb = -1;
#include "packet-cmp-hf.c"

/* Initialize the subtree pointers */
static gint ett_cmp = -1;
#include "packet-cmp-ett.c"

static const char *object_identifier_id;


#include "packet-cmp-fn.c"

static int
dissect_cmp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	return dissect_cmp_PKIMessage(FALSE, tvb, 0, pinfo, tree, -1);
}

#define CMP_TYPE_PKIMSG		0
#define CMP_TYPE_POLLREP	1
#define CMP_TYPE_POLLREQ	2
#define CMP_TYPE_NEGPOLLREP	3
#define CMP_TYPE_PARTIALMSGREP	4
#define CMP_TYPE_FINALMSGREP	5
#define CMP_TYPE_ERRORMSGREP	6
static const value_string cmp_pdu_types[] = {
	{ CMP_TYPE_PKIMSG,		"pkiMsg" },
	{ CMP_TYPE_POLLREP,		"pollRep" },
	{ CMP_TYPE_POLLREQ,		"pollReq" },
	{ CMP_TYPE_NEGPOLLREP,		"negPollRep" },
	{ CMP_TYPE_PARTIALMSGREP,	"partialMsgRep" },
	{ CMP_TYPE_FINALMSGREP,		"finalMsgRep" },
	{ CMP_TYPE_ERRORMSGREP,		"errorMsgRep" },
	{ 0, NULL },
};

static void dissect_cmp_tcp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
{
	tvbuff_t   *next_tvb;
	guint32 pdu_len;
	guint8 pdu_type;
	nstime_t	ts;

	proto_item *item=NULL;
	proto_tree *tree=NULL;

	if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
		col_set_str(pinfo->cinfo, COL_PROTOCOL, "CMP");

	if (check_col(pinfo->cinfo, COL_INFO)) {
		col_clear(pinfo->cinfo, COL_INFO);
		
		col_add_fstr(pinfo->cinfo, COL_INFO, "PKIXCMP");
	}


	if(parent_tree){
		item=proto_tree_add_item(parent_tree, proto_cmp, tvb, 0, -1, FALSE);
		tree = proto_item_add_subtree(item, ett_cmp);
	}

	pdu_len=tvb_get_ntohl(tvb, 0);
	pdu_type=tvb_get_guint8(tvb, 4);

	proto_tree_add_uint(tree, hf_cmp_rm, tvb, 0, 4, pdu_len);
	proto_tree_add_uint(tree, hf_cmp_type, tvb, 4, 1, pdu_type);

        if (check_col (pinfo->cinfo, COL_INFO)) {
            col_set_str (pinfo->cinfo, COL_INFO, val_to_str (pdu_type, cmp_pdu_types, "0x%x"));
        }

	switch(pdu_type){
	case CMP_TYPE_PKIMSG:
		next_tvb = tvb_new_subset(tvb, 5, tvb_length_remaining(tvb, 5), pdu_len);
		dissect_cmp_pdu(next_tvb, pinfo, tree);
		break;
	case CMP_TYPE_POLLREP:
		proto_tree_add_item(tree, hf_cmp_poll_ref, tvb, 0, 4, FALSE);

		ts.secs = tvb_get_ntohl(tvb, 4);
		ts.nsecs = 0;
		proto_tree_add_time(tree, hf_cmp_ttcb, tvb, 4, 4, &ts);
		break;
	case CMP_TYPE_POLLREQ:
		proto_tree_add_item(tree, hf_cmp_poll_ref, tvb, 0, 4, FALSE);
		break;
	case CMP_TYPE_NEGPOLLREP:
		break;
	case CMP_TYPE_PARTIALMSGREP:
		proto_tree_add_item(tree, hf_cmp_next_poll_ref, tvb, 0, 4, FALSE);

		ts.secs = tvb_get_ntohl(tvb, 4);
		ts.nsecs = 0;
		proto_tree_add_time(tree, hf_cmp_ttcb, tvb, 4, 4, &ts);

		next_tvb = tvb_new_subset(tvb, 13, tvb_length_remaining(tvb, 13), pdu_len);
		dissect_cmp_pdu(next_tvb, pinfo, tree);
		break;
	case CMP_TYPE_FINALMSGREP:
		next_tvb = tvb_new_subset(tvb, 5, tvb_length_remaining(tvb, 5), pdu_len);
		dissect_cmp_pdu(next_tvb, pinfo, tree);
		break;
	case CMP_TYPE_ERRORMSGREP:
		/*XXX to be added*/
		break;
	}

}

static guint get_cmp_pdu_len(tvbuff_t *tvb, int offset)
{
  guint32 plen;

  /*
   * Get the length of the CMP-over-TCP packet.
   */
  plen = tvb_get_ntohl(tvb, offset);

  return plen+4;
}

/* CMP over TCP    RFC2510 section 5.2 */
static int
dissect_cmp_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
{
	guint32 pdu_len;
	guint8 pdu_type;

	/* only attempt to dissect it as CMP over TCP if we have
	 * at least 5 bytes.
	 */
	if (!tvb_bytes_exist(tvb, 0, 5)) {
		return 0;
	}

	pdu_len=tvb_get_ntohl(tvb, 0);
	pdu_type=tvb_get_guint8(tvb, 4);

	/* arbitrary limit: assume a CMP over TCP pdu is never >10000 bytes 
	 * in size.
	 * It is definitely at least 1 byte to accomodate the flags byte 
	 */
	if((pdu_len<=0)||(pdu_len>10000)){
		return 0;
	}
	/* type is between 0 and 6 */
	if(pdu_type>6){
		return 0;
	}
	/* type 0 contains a PKI message and must therefore be >= 3 bytes 
	 * long (flags + BER TAG + BER LENGTH
	 */
	if((pdu_type==0)&&(pdu_len<3)){
		return 0;
	}

	tcp_dissect_pdus(tvb, pinfo, parent_tree, cmp_desegment, 4, get_cmp_pdu_len,
		dissect_cmp_tcp_pdu);

	return tvb_length(tvb);
}

static int
dissect_cmp_http(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
{
	proto_item *item=NULL;
	proto_tree *tree=NULL;

	if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
		col_set_str(pinfo->cinfo, COL_PROTOCOL, "CMP");

	if (check_col(pinfo->cinfo, COL_INFO)) {
		col_clear(pinfo->cinfo, COL_INFO);
		
		col_add_fstr(pinfo->cinfo, COL_INFO, "PKIXCMP");
	}


	if(parent_tree){
		item=proto_tree_add_item(parent_tree, proto_cmp, tvb, 0, -1, FALSE);
		tree = proto_item_add_subtree(item, ett_cmp);
	}

	return dissect_cmp_pdu(tvb, pinfo, tree);
}


/*--- proto_register_cmp ----------------------------------------------*/
void proto_register_cmp(void) {

  /* List of fields */
  static hf_register_info hf[] = {
    { &hf_cmp_type_oid,
      { "InfoType", "cmp.type.oid",
        FT_STRING, BASE_NONE, NULL, 0,
        "Type of InfoTypeAndValue", HFILL }},
    { &hf_cmp_rm,
      { "Record Marker", "cmp.rm",
        FT_UINT32, BASE_DEC, NULL, 0,
        "Record Marker  length of PDU in bytes", HFILL }},
    { &hf_cmp_type,
      { "Type", "cmp.type",
        FT_UINT8, BASE_DEC, VALS(cmp_pdu_types), 0,
        "PDU Type", HFILL }},
    { &hf_cmp_poll_ref,
      { "Polling Reference", "cmp.poll_ref",
        FT_UINT32, BASE_HEX, NULL, 0,
        "", HFILL }},
    { &hf_cmp_next_poll_ref,
      { "Next Polling Reference", "cmp.next_poll_ref",
        FT_UINT32, BASE_HEX, NULL, 0,
        "", HFILL }},
    { &hf_cmp_ttcb,
      { "Time to check Back", "cmp.ttcb",
        FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0,
        "", HFILL }},
#include "packet-cmp-hfarr.c"
  };

  /* List of subtrees */
  static gint *ett[] = {
    &ett_cmp,
#include "packet-cmp-ettarr.c"
  };
  module_t *cmp_module;

  /* Register protocol */
  proto_cmp = proto_register_protocol(PNAME, PSNAME, PFNAME);

  /* Register fields and subtrees */
  proto_register_field_array(proto_cmp, hf, array_length(hf));
  proto_register_subtree_array(ett, array_length(ett));

  cmp_module = prefs_register_protocol(proto_cmp, NULL);
  prefs_register_bool_preference(cmp_module, "desegment",
		"Reassemble CMP-over-TCP messages spanning multiple TCP segments",
		"Whether the CMP-over-TCP dissector should reassemble messages spanning multiple TCP segments. "
		"To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
		&cmp_desegment);
}


/*--- proto_reg_handoff_cmp -------------------------------------------*/
void proto_reg_handoff_cmp(void) {
	dissector_handle_t cmp_http_handle;
	dissector_handle_t cmp_tcp_handle;

	cmp_http_handle = new_create_dissector_handle(dissect_cmp_http, proto_cmp);
	dissector_add_string("media_type", "application/pkixcmp", cmp_http_handle);

	cmp_tcp_handle = new_create_dissector_handle(dissect_cmp_tcp, proto_cmp);
	dissector_add("tcp.port", TCP_PORT_CMP, cmp_tcp_handle);

/*#include "packet-cmp-dis-tab.c"*/
}