107d7fd85fd43ee21efa81143e8bbe812bb6e245
[metze/wireshark/wip.git] / asn1 / p1 / packet-p1-template.c
1 /* packet-p1.c
2  * Routines for X.411 (X.400 Message Transfer)  packet dissection
3  * Graeme Lunt 2005
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "config.h"
27
28 #include <glib.h>
29 #include <epan/packet.h>
30 #include <epan/prefs.h>
31 #include <epan/oids.h>
32 #include <epan/asn1.h>
33 #include <epan/expert.h>
34 #include <epan/wmem/wmem.h>
35
36 #include "packet-ber.h"
37 #include "packet-acse.h"
38 #include "packet-ros.h"
39 #include "packet-rtse.h"
40
41 #include "packet-x509af.h"
42 #include "packet-x509ce.h"
43 #include "packet-x509if.h"
44 #include "packet-x509sat.h"
45
46 #include "packet-p1.h"
47 #include <epan/strutil.h>
48
49 #define PNAME  "X.411 Message Transfer Service"
50 #define PSNAME "P1"
51 #define PFNAME "p1"
52
53 static guint global_p1_tcp_port = 102;
54 static dissector_handle_t tpkt_handle;
55 static void prefs_register_p1(void); /* forward declaration for use in preferences registration */
56
57 /* Initialize the protocol and registered fields */
58 static int proto_p1 = -1;
59 static int proto_p3 = -1;
60
61 static int hf_p1_MTS_APDU_PDU = -1;
62 static int hf_p1_MTABindArgument_PDU = -1;
63 static int hf_p1_MTABindResult_PDU = -1;
64 static int hf_p1_MTABindError_PDU = -1;
65
66 #include "packet-p1-hf.c"
67
68 /* Initialize the subtree pointers */
69 static gint ett_p1 = -1;
70 static gint ett_p3 = -1;
71 static gint ett_p1_content_unknown = -1;
72 static gint ett_p1_bilateral_information = -1;
73 static gint ett_p1_additional_information = -1;
74 static gint ett_p1_unknown_standard_extension = -1;
75 static gint ett_p1_unknown_extension_attribute_type = -1;
76 static gint ett_p1_unknown_tokendata_type = -1;
77 #include "packet-p1-ett.c"
78
79 static expert_field ei_p1_unknown_extension_attribute_type = EI_INIT;
80 static expert_field ei_p1_unknown_standard_extension = EI_INIT;
81 static expert_field ei_p1_unknown_built_in_content_type = EI_INIT;
82 static expert_field ei_p1_unknown_tokendata_type = EI_INIT;
83
84 /* Dissector tables */
85 static dissector_table_t p1_extension_dissector_table;
86 static dissector_table_t p1_extension_attribute_dissector_table;
87 static dissector_table_t p1_tokendata_dissector_table;
88
89 #include "packet-p1-table.c"   /* operation and error codes */
90
91 typedef struct p1_address_ctx {
92         gboolean do_address;
93         const char *content_type_id;
94         gboolean report_unknown_content_type;
95         wmem_strbuf_t* oraddress;
96 } p1_address_ctx_t;
97
98 static void set_do_address(asn1_ctx_t* actx, gboolean do_address)
99 {
100         p1_address_ctx_t* ctx;
101
102         if (actx->subtree.tree_ctx == NULL) {
103                 actx->subtree.tree_ctx = wmem_new0(wmem_packet_scope(), p1_address_ctx_t);
104         }
105
106         ctx = (p1_address_ctx_t*)actx->subtree.tree_ctx;
107         ctx->do_address = do_address;
108 }
109
110 static void do_address(const char* addr, tvbuff_t* tvb_string, asn1_ctx_t* actx)
111 {
112         p1_address_ctx_t* ctx = (p1_address_ctx_t*)actx->subtree.tree_ctx;
113
114         if (ctx && ctx->do_address) {
115                 if (addr) {
116                         wmem_strbuf_append(ctx->oraddress, addr);
117                 }
118                 if (tvb_string) {
119                         wmem_strbuf_append(ctx->oraddress, tvb_format_text(tvb_string, 0, tvb_length(tvb_string)));
120                 }
121         }
122
123 }
124
125 static void do_address_str(const char* addr, tvbuff_t* tvb_string, asn1_ctx_t* actx)
126 {
127         wmem_strbuf_t *ddatype = (wmem_strbuf_t *)actx->value_ptr;
128         p1_address_ctx_t* ctx = (p1_address_ctx_t*)actx->subtree.tree_ctx;
129
130         do_address(addr, tvb_string, actx);
131
132         if (ctx && ctx->do_address && ddatype && tvb_string)
133                 wmem_strbuf_append(ddatype, tvb_format_text(tvb_string, 0, tvb_length(tvb_string)));
134 }
135
136 static void do_address_str_tree(const char* addr, tvbuff_t* tvb_string, asn1_ctx_t* actx, proto_tree* tree)
137 {
138         wmem_strbuf_t *ddatype = (wmem_strbuf_t *)actx->value_ptr;
139         p1_address_ctx_t* ctx = (p1_address_ctx_t*)actx->subtree.tree_ctx;
140
141         do_address(addr, tvb_string, actx);
142
143         if (ctx && ctx->do_address && tvb_string && ddatype) {
144                 if (wmem_strbuf_get_len(ddatype) > 0) {
145                         proto_item_append_text (tree, " (%s=%s)", wmem_strbuf_get_str(ddatype), tvb_format_text(tvb_string, 0, tvb_length(tvb_string)));
146                 }
147         }
148 }
149
150 #include "packet-p1-fn.c"
151
152 #include "packet-p1-table11.c" /* operation argument/result dissectors */
153 #include "packet-p1-table21.c" /* error dissector */
154
155 static const ros_info_t p3_ros_info = {
156   "P3",
157   &proto_p3,
158   &ett_p3,
159   p3_opr_code_string_vals,
160   p3_opr_tab,
161   p3_err_code_string_vals,
162   p3_err_tab
163 };
164
165 void p1_initialize_content_globals (asn1_ctx_t* actx, proto_tree *tree, gboolean report_unknown_cont_type)
166 {
167         p1_address_ctx_t* ctx;
168
169         if (actx->subtree.tree_ctx == NULL) {
170                 actx->subtree.tree_ctx = wmem_new0(wmem_packet_scope(), p1_address_ctx_t);
171         }
172
173         ctx = (p1_address_ctx_t*)actx->subtree.tree_ctx;
174
175         actx->subtree.top_tree = tree;
176         actx->external.direct_reference = NULL;
177         ctx->content_type_id = NULL;
178         ctx->report_unknown_content_type = report_unknown_cont_type;
179 }
180
181 const char* p1_get_last_oraddress (asn1_ctx_t* actx)
182 {
183         p1_address_ctx_t* ctx;
184
185         if ((actx == NULL) || (actx->subtree.tree_ctx == NULL))
186                 return "";
187
188         ctx = (p1_address_ctx_t*)actx->subtree.tree_ctx;
189         if (wmem_strbuf_get_len(ctx->oraddress) <= 0)
190                 return "";
191
192         return wmem_strbuf_get_str(ctx->oraddress);
193 }
194
195 /*
196  * Dissect P1 MTS APDU
197  */
198 void
199 dissect_p1_mts_apdu (tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
200 {
201         proto_item *item=NULL;
202         proto_tree *tree=NULL;
203         asn1_ctx_t asn1_ctx;
204         asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
205
206         /* save parent_tree so subdissectors can create new top nodes */
207         p1_initialize_content_globals (&asn1_ctx, parent_tree, TRUE);
208
209         if(parent_tree){
210                 item = proto_tree_add_item(parent_tree, proto_p1, tvb, 0, -1, ENC_NA);
211                 tree = proto_item_add_subtree(item, ett_p1);
212         }
213
214         col_set_str(pinfo->cinfo, COL_PROTOCOL, "P1");
215         col_set_str(pinfo->cinfo, COL_INFO, "Transfer");
216
217         dissect_p1_MTS_APDU (FALSE, tvb, 0, &asn1_ctx, tree, hf_p1_MTS_APDU_PDU);
218         p1_initialize_content_globals (&asn1_ctx, NULL, FALSE);
219 }
220
221 /*
222 * Dissect P1 PDUs inside a PPDU.
223 */
224 static void
225 dissect_p1(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
226 {
227         int offset = 0;
228         int old_offset;
229         proto_item *item=NULL;
230         proto_tree *tree=NULL;
231         struct SESSION_DATA_STRUCTURE* session;
232         int (*p1_dissector)(gboolean implicit_tag _U_, tvbuff_t *tvb, int offset, asn1_ctx_t *actx _U_, proto_tree *tree, int hf_index _U_) = NULL;
233         const char *p1_op_name;
234         int hf_p1_index = -1;
235         asn1_ctx_t asn1_ctx;
236         asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
237
238         /* save parent_tree so subdissectors can create new top nodes */
239         p1_initialize_content_globals (&asn1_ctx, parent_tree, TRUE);
240
241         /* do we have operation information from the ROS dissector?  */
242         if( !pinfo->private_data ){
243                 if(parent_tree){
244                         proto_tree_add_text(parent_tree, tvb, offset, -1,
245                                 "Internal error: can't get operation information from ROS dissector.");
246                 }
247                 return;
248         }
249
250         session  = ( (struct SESSION_DATA_STRUCTURE*)(pinfo->private_data) );
251
252         if(parent_tree){
253                 item = proto_tree_add_item(parent_tree, proto_p1, tvb, 0, -1, ENC_NA);
254                 tree = proto_item_add_subtree(item, ett_p1);
255         }
256         col_set_str(pinfo->cinfo, COL_PROTOCOL, "P1");
257         col_clear(pinfo->cinfo, COL_INFO);
258
259         switch(session->ros_op & ROS_OP_MASK) {
260         case (ROS_OP_BIND | ROS_OP_ARGUMENT):   /*  BindInvoke */
261           p1_dissector = dissect_p1_MTABindArgument;
262           p1_op_name = "Bind-Argument";
263           hf_p1_index = hf_p1_MTABindArgument_PDU;
264           break;
265         case (ROS_OP_BIND | ROS_OP_RESULT):     /*  BindResult */
266           p1_dissector = dissect_p1_MTABindResult;
267           p1_op_name = "Bind-Result";
268           hf_p1_index = hf_p1_MTABindResult_PDU;
269           break;
270         case (ROS_OP_BIND | ROS_OP_ERROR):      /*  BindError */
271           p1_dissector = dissect_p1_MTABindError;
272           p1_op_name = "Bind-Error";
273           hf_p1_index = hf_p1_MTABindError_PDU;
274           break;
275         case (ROS_OP_INVOKE | ROS_OP_ARGUMENT): /*  Invoke Argument */
276           p1_dissector = dissect_p1_MTS_APDU;
277           p1_op_name = "Transfer";
278           hf_p1_index = hf_p1_MTS_APDU_PDU;
279           break;
280         default:
281           proto_tree_add_text(tree, tvb, offset, -1,"Unsupported P1 PDU");
282           return;
283         }
284
285         col_set_str(pinfo->cinfo, COL_INFO, p1_op_name);
286
287         while (tvb_reported_length_remaining(tvb, offset) > 0){
288                 old_offset=offset;
289                 offset=(*p1_dissector)(FALSE, tvb, offset, &asn1_ctx , tree, hf_p1_index);
290                 if(offset == old_offset){
291                         proto_tree_add_text(tree, tvb, offset, -1,"Internal error, zero-byte P1 PDU");
292                         break;
293                 }
294         }
295         p1_initialize_content_globals (&asn1_ctx, NULL, FALSE);
296 }
297
298
299 /*--- proto_register_p1 -------------------------------------------*/
300 void proto_register_p1(void) {
301
302   /* List of fields */
303   static hf_register_info hf[] =
304   {
305       /* "Created by defining PDU in .cnf */
306     { &hf_p1_MTABindArgument_PDU,
307       { "MTABindArgument", "p1.MTABindArgument",
308         FT_UINT32, BASE_DEC, VALS(p1_MTABindArgument_vals), 0,
309         "p1.MTABindArgument", HFILL }},
310     { &hf_p1_MTABindResult_PDU,
311       { "MTABindResult", "p1.MTABindResult",
312         FT_UINT32, BASE_DEC, VALS(p1_MTABindResult_vals), 0,
313         "p1.MTABindResult", HFILL }},
314     { &hf_p1_MTABindError_PDU,
315       { "MTABindError", "p1.MTABindError",
316         FT_UINT32, BASE_DEC, VALS(p1_MTABindError_vals), 0,
317         "p1.MTABindError", HFILL }},
318     { &hf_p1_MTS_APDU_PDU,
319       { "MTS-APDU", "p1.MTS_APDU",
320         FT_UINT32, BASE_DEC, VALS(p1_MTS_APDU_vals), 0,
321         "p1.MTS_APDU", HFILL }},
322
323 #include "packet-p1-hfarr.c"
324   };
325
326   /* List of subtrees */
327   static gint *ett[] = {
328     &ett_p1,
329     &ett_p3,
330     &ett_p1_content_unknown,
331     &ett_p1_bilateral_information,
332     &ett_p1_additional_information,
333     &ett_p1_unknown_standard_extension,
334     &ett_p1_unknown_extension_attribute_type,
335     &ett_p1_unknown_tokendata_type,
336 #include "packet-p1-ettarr.c"
337   };
338
339   static ei_register_info ei[] = {
340      { &ei_p1_unknown_extension_attribute_type, { "p1.unknown.extension_attribute_type", PI_UNDECODED, PI_WARN, "Unknown extension-attribute-type", EXPFILL }},
341      { &ei_p1_unknown_standard_extension, { "p1.unknown.standard_extension", PI_UNDECODED, PI_WARN, "Unknown standard-extension", EXPFILL }},
342      { &ei_p1_unknown_built_in_content_type, { "p1.unknown.built_in_content_type", PI_UNDECODED, PI_WARN, "P1 Unknown Content (unknown built-in content-type)", EXPFILL }},
343      { &ei_p1_unknown_tokendata_type, { "p1.unknown.tokendata_type", PI_UNDECODED, PI_WARN, "Unknown tokendata-type", EXPFILL }},
344   };
345
346   expert_module_t* expert_p1;
347   module_t *p1_module;
348
349   /* Register protocol */
350   proto_p1 = proto_register_protocol(PNAME, PSNAME, PFNAME);
351   register_dissector("p1", dissect_p1, proto_p1);
352
353   proto_p3 = proto_register_protocol("X.411 Message Access Service", "P3", "p3");
354
355   /* Register fields and subtrees */
356   proto_register_field_array(proto_p1, hf, array_length(hf));
357   proto_register_subtree_array(ett, array_length(ett));
358   expert_p1 = expert_register_protocol(proto_p1);
359   expert_register_field_array(expert_p1, ei, array_length(ei));
360
361   p1_extension_dissector_table = register_dissector_table("p1.extension", "P1-EXTENSION", FT_UINT32, BASE_DEC);
362   p1_extension_attribute_dissector_table = register_dissector_table("p1.extension-attribute", "P1-EXTENSION-ATTRIBUTE", FT_UINT32, BASE_DEC);
363   p1_tokendata_dissector_table = register_dissector_table("p1.tokendata", "P1-TOKENDATA", FT_UINT32, BASE_DEC);
364
365   /* Register our configuration options for P1, particularly our port */
366
367   p1_module = prefs_register_protocol_subtree("OSI/X.400", proto_p1, prefs_register_p1);
368
369   prefs_register_uint_preference(p1_module, "tcp.port", "P1 TCP Port",
370                                  "Set the port for P1 operations (if other"
371                                  " than the default of 102)",
372                                  10, &global_p1_tcp_port);
373
374   register_ber_syntax_dissector("P1 Message", proto_p1, dissect_p1_mts_apdu);
375 }
376
377
378 /*--- proto_reg_handoff_p1 --- */
379 void proto_reg_handoff_p1(void) {
380   dissector_handle_t p1_handle;
381
382 #include "packet-p1-dis-tab.c"
383
384   /* APPLICATION CONTEXT */
385
386   oid_add_from_string("id-ac-mts-transfer","2.6.0.1.6");
387
388   /* ABSTRACT SYNTAXES */
389
390   p1_handle = find_dissector("p1");
391   register_rtse_oid_dissector_handle("2.6.0.2.12", p1_handle, 0, "id-as-mta-rtse", TRUE);
392   register_rtse_oid_dissector_handle("2.6.0.2.7", p1_handle, 0, "id-as-mtse", FALSE);
393
394   register_rtse_oid_dissector_handle("applicationProtocol.1", p1_handle, 0, "mts-transfer-protocol-1984", FALSE);
395   register_rtse_oid_dissector_handle("applicationProtocol.12", p1_handle, 0, "mta-transfer-protocol", FALSE);
396
397   /* remember the tpkt handler for change in preferences */
398   tpkt_handle = find_dissector("tpkt");
399
400   /* APPLICATION CONTEXT */
401
402   oid_add_from_string("id-ac-mts-access-88", id_ac_mts_access_88);
403   oid_add_from_string("id-ac-mts-forced-access-88", id_ac_mts_forced_access_88);
404   oid_add_from_string("id-ac-mts-access-94", id_ac_mts_access_94);
405   oid_add_from_string("id-ac-mts-forced-access-94", id_ac_mts_forced_access_94);
406
407
408   /* Register P3 with ROS */
409   register_ros_protocol_info(id_as_msse, &p3_ros_info, 0, "id-as-msse", FALSE);
410
411   register_ros_protocol_info(id_as_mdse_88, &p3_ros_info, 0, "id-as-mdse-88", FALSE);
412   register_ros_protocol_info(id_as_mdse_94, &p3_ros_info, 0, "id-as-mdse-94", FALSE);
413
414   register_ros_protocol_info(id_as_mase_88, &p3_ros_info, 0, "id-as-mase-88", FALSE);
415   register_ros_protocol_info(id_as_mase_94, &p3_ros_info, 0, "id-as-mase-94", FALSE);
416
417   register_ros_protocol_info(id_as_mts, &p3_ros_info, 0, "id-as-mts", FALSE);
418
419 }
420
421 static void
422 prefs_register_p1(void)
423 {
424   static guint tcp_port = 0;
425
426   /* de-register the old port */
427   /* port 102 is registered by TPKT - don't undo this! */
428   if((tcp_port > 0) && (tcp_port != 102) && tpkt_handle)
429     dissector_delete_uint("tcp.port", tcp_port, tpkt_handle);
430
431   /* Set our port number for future use */
432   tcp_port = global_p1_tcp_port;
433
434   if((tcp_port > 0) && (tcp_port != 102) && tpkt_handle)
435     dissector_add_uint("tcp.port", tcp_port, tpkt_handle);
436
437 }