[Automatic update for 2016-03-13]
[metze/wireshark/wip.git] / asn1 / dop / packet-dop-template.c
1 /* packet-dop.c
2  * Routines for X.501 (DSA Operational Attributes)  packet dissection
3  * Graeme Lunt 2005
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include "config.h"
25
26 #include <epan/packet.h>
27 #include <epan/prefs.h>
28 #include <epan/oids.h>
29 #include <epan/asn1.h>
30 #include <epan/expert.h>
31
32 #include "packet-ber.h"
33 #include "packet-acse.h"
34 #include "packet-ros.h"
35
36 #include "packet-x509sat.h"
37 #include "packet-x509af.h"
38 #include "packet-x509if.h"
39 #include "packet-dap.h"
40 #include "packet-dsp.h"
41 #include "packet-crmf.h"
42
43
44 #include "packet-dop.h"
45
46 #define PNAME  "X.501 Directory Operational Binding Management Protocol"
47 #define PSNAME "DOP"
48 #define PFNAME "dop"
49
50 void proto_register_dop(void);
51 void proto_reg_handoff_dop(void);
52
53 static guint global_dop_tcp_port = 102;
54 static dissector_handle_t tpkt_handle;
55 static void prefs_register_dop(void); /* forward declaration for use in preferences registration */
56
57 /* Initialize the protocol and registered fields */
58 static int proto_dop = -1;
59
60 static const char *binding_type = NULL; /* binding_type */
61
62 static int call_dop_oid_callback(const char *base_string, tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, const char *col_info, void* data);
63
64 #include "packet-dop-hf.c"
65
66 /* Initialize the subtree pointers */
67 static gint ett_dop = -1;
68 static gint ett_dop_unknown = -1;
69 #include "packet-dop-ett.c"
70
71 static expert_field ei_dop_unknown_binding_parameter = EI_INIT;
72 static expert_field ei_dop_unsupported_opcode = EI_INIT;
73 static expert_field ei_dop_unsupported_errcode = EI_INIT;
74 static expert_field ei_dop_unsupported_pdu = EI_INIT;
75 static expert_field ei_dop_zero_pdu = EI_INIT;
76
77 /* Dissector table */
78 static dissector_table_t dop_dissector_table;
79
80 static void append_oid(packet_info *pinfo, const char *oid)
81 {
82         const char *name = NULL;
83
84     name = oid_resolved_from_string(wmem_packet_scope(), oid);
85     col_append_fstr(pinfo->cinfo, COL_INFO, " %s", name ? name : oid);
86 }
87
88 #include "packet-dop-fn.c"
89
90 static int
91 call_dop_oid_callback(const char *base_string, tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, const char *col_info, void* data)
92 {
93   char* binding_param;
94
95   binding_param = wmem_strdup_printf(wmem_packet_scope(), "%s.%s", base_string, binding_type ? binding_type : "");
96
97   col_append_fstr(pinfo->cinfo, COL_INFO, " %s", col_info);
98
99   if (dissector_try_string(dop_dissector_table, binding_param, tvb, pinfo, tree, data)) {
100      offset = tvb_reported_length (tvb);
101   } else {
102      proto_item *item;
103      proto_tree *next_tree;
104
105      next_tree = proto_tree_add_subtree_format(tree, tvb, 0, -1, ett_dop_unknown, &item,
106          "Dissector for parameter %s OID:%s not implemented. Contact Wireshark developers if you want this supported", base_string, binding_type ? binding_type : "<empty>");
107
108      offset = dissect_unknown_ber(pinfo, tvb, offset, next_tree);
109      expert_add_info(pinfo, item, &ei_dop_unknown_binding_parameter);
110    }
111
112    return offset;
113 }
114
115
116 /*
117 * Dissect DOP PDUs inside a ROS PDUs
118 */
119 static int
120 dissect_dop(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, void* data)
121 {
122         int offset = 0;
123         int old_offset;
124         proto_item *item;
125         proto_tree *tree;
126         struct SESSION_DATA_STRUCTURE* session;
127         int (*dop_dissector)(gboolean implicit_tag _U_, tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index _U_) = NULL;
128         const char *dop_op_name;
129         asn1_ctx_t asn1_ctx;
130
131         /* do we have operation information from the ROS dissector? */
132         if (data == NULL)
133                 return 0;
134         session = (struct SESSION_DATA_STRUCTURE*)data;
135
136         asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
137
138         item = proto_tree_add_item(parent_tree, proto_dop, tvb, 0, -1, ENC_NA);
139         tree = proto_item_add_subtree(item, ett_dop);
140
141         col_set_str(pinfo->cinfo, COL_PROTOCOL, "DOP");
142         col_clear(pinfo->cinfo, COL_INFO);
143
144         asn1_ctx.private_data = session;
145
146         switch(session->ros_op & ROS_OP_MASK) {
147         case (ROS_OP_BIND | ROS_OP_ARGUMENT):   /*  BindInvoke */
148           dop_dissector = dissect_dop_DSAOperationalManagementBindArgument;
149           dop_op_name = "DSA-Operational-Bind-Argument";
150           break;
151         case (ROS_OP_BIND | ROS_OP_RESULT):     /*  BindResult */
152           dop_dissector = dissect_dop_DSAOperationalManagementBindResult;
153           dop_op_name = "DSA-Operational-Bind-Result";
154           break;
155         case (ROS_OP_BIND | ROS_OP_ERROR):      /*  BindError */
156           dop_dissector = dissect_dop_DSAOperationalManagementBindError;
157           dop_op_name = "DSA-Operational-Management-Bind-Error";
158           break;
159         case (ROS_OP_INVOKE | ROS_OP_ARGUMENT): /*  Invoke Argument */
160           switch(session->ros_op & ROS_OP_OPCODE_MASK) {
161           case 100: /* establish */
162             dop_dissector = dissect_dop_EstablishOperationalBindingArgument;
163             dop_op_name = "Establish-Operational-Binding-Argument";
164             break;
165           case 101: /* terminate */
166             dop_dissector = dissect_dop_TerminateOperationalBindingArgument;
167             dop_op_name = "Terminate-Operational-Binding-Argument";
168             break;
169           case 102: /* modify */
170             dop_dissector = dissect_dop_ModifyOperationalBindingArgument;
171             dop_op_name = "Modify-Operational-Binding-Argument";
172             break;
173           default:
174             proto_tree_add_expert_format(tree, pinfo, &ei_dop_unsupported_opcode, tvb, offset, -1,
175                 "Unsupported DOP Argument opcode (%d)", session->ros_op & ROS_OP_OPCODE_MASK);
176             break;
177           }
178           break;
179         case (ROS_OP_INVOKE | ROS_OP_RESULT):   /*  Return Result */
180           switch(session->ros_op & ROS_OP_OPCODE_MASK) {
181           case 100: /* establish */
182             dop_dissector = dissect_dop_EstablishOperationalBindingResult;
183             dop_op_name = "Establish-Operational-Binding-Result";
184             break;
185           case 101: /* terminate */
186             dop_dissector = dissect_dop_TerminateOperationalBindingResult;
187             dop_op_name = "Terminate-Operational-Binding-Result";
188             break;
189           case 102: /* modify */
190             dop_dissector = dissect_dop_ModifyOperationalBindingResult;
191             dop_op_name = "Modify-Operational-Binding-Result";
192             break;
193           default:
194             proto_tree_add_expert_format(tree, pinfo, &ei_dop_unsupported_opcode, tvb, offset, -1,
195                     "Unsupported DOP Result opcode (%d)", session->ros_op & ROS_OP_OPCODE_MASK);
196             break;
197           }
198           break;
199         case (ROS_OP_INVOKE | ROS_OP_ERROR):    /*  Return Error */
200           switch(session->ros_op & ROS_OP_OPCODE_MASK) {
201           case 100: /* operational-binding */
202             dop_dissector = dissect_dop_OpBindingErrorParam;
203             dop_op_name = "Operational-Binding-Error";
204             break;
205           default:
206             proto_tree_add_expert_format(tree, pinfo, &ei_dop_unsupported_errcode, tvb, offset, -1,
207                 "Unsupported DOP Error opcode (%d)", session->ros_op & ROS_OP_OPCODE_MASK);
208             break;
209           }
210           break;
211         default:
212           proto_tree_add_expert(tree, pinfo, &ei_dop_unsupported_pdu, tvb, offset, -1);
213           return tvb_captured_length(tvb);
214         }
215
216         if(dop_dissector) {
217       col_set_str(pinfo->cinfo, COL_INFO, dop_op_name);
218
219           while (tvb_reported_length_remaining(tvb, offset) > 0){
220             old_offset=offset;
221             offset=(*dop_dissector)(FALSE, tvb, offset, &asn1_ctx, tree, -1);
222             if(offset == old_offset){
223               proto_tree_add_expert(tree, pinfo, &ei_dop_zero_pdu, tvb, offset, -1);
224               break;
225             }
226           }
227         }
228
229         return tvb_captured_length(tvb);
230 }
231
232
233
234 /*--- proto_register_dop -------------------------------------------*/
235 void proto_register_dop(void) {
236
237   /* List of fields */
238   static hf_register_info hf[] =
239   {
240 #include "packet-dop-hfarr.c"
241   };
242
243   /* List of subtrees */
244   static gint *ett[] = {
245     &ett_dop,
246     &ett_dop_unknown,
247 #include "packet-dop-ettarr.c"
248   };
249
250   static ei_register_info ei[] = {
251      { &ei_dop_unknown_binding_parameter, { "dop.unknown_binding_parameter", PI_UNDECODED, PI_WARN, "Unknown binding-parameter", EXPFILL }},
252      { &ei_dop_unsupported_opcode, { "dop.unsupported_opcode", PI_UNDECODED, PI_WARN, "Unsupported DOP opcode", EXPFILL }},
253      { &ei_dop_unsupported_errcode, { "dop.unsupported_errcode", PI_UNDECODED, PI_WARN, "Unsupported DOP errcode", EXPFILL }},
254      { &ei_dop_unsupported_pdu, { "dop.unsupported_pdu", PI_UNDECODED, PI_WARN, "Unsupported DOP PDU", EXPFILL }},
255      { &ei_dop_zero_pdu, { "dop.zero_pdu", PI_PROTOCOL, PI_ERROR, "Internal error, zero-byte DOP PDU", EXPFILL }},
256   };
257
258   expert_module_t* expert_dop;
259   module_t *dop_module;
260
261   /* Register protocol */
262   proto_dop = proto_register_protocol(PNAME, PSNAME, PFNAME);
263
264   register_dissector("dop", dissect_dop, proto_dop);
265
266   dop_dissector_table = register_dissector_table("dop.oid", "DOP OID Dissectors", FT_STRING, BASE_NONE, DISSECTOR_TABLE_ALLOW_DUPLICATE);
267
268   /* Register fields and subtrees */
269   proto_register_field_array(proto_dop, hf, array_length(hf));
270   proto_register_subtree_array(ett, array_length(ett));
271   expert_dop = expert_register_protocol(proto_dop);
272   expert_register_field_array(expert_dop, ei, array_length(ei));
273
274   /* Register our configuration options for DOP, particularly our port */
275
276   dop_module = prefs_register_protocol_subtree("OSI/X.500", proto_dop, prefs_register_dop);
277
278   prefs_register_uint_preference(dop_module, "tcp.port", "DOP TCP Port",
279                                  "Set the port for DOP operations (if other"
280                                  " than the default of 102)",
281                                  10, &global_dop_tcp_port);
282
283
284 }
285
286
287 /*--- proto_reg_handoff_dop --- */
288 void proto_reg_handoff_dop(void) {
289   dissector_handle_t dop_handle;
290
291 #include "packet-dop-dis-tab.c"
292   /* APPLICATION CONTEXT */
293
294   oid_add_from_string("id-ac-directory-operational-binding-management","2.5.3.3");
295
296   /* ABSTRACT SYNTAXES */
297
298   /* Register DOP with ROS (with no use of RTSE) */
299   dop_handle = find_dissector("dop");
300   register_ros_oid_dissector_handle("2.5.9.4", dop_handle, 0, "id-as-directory-operational-binding-management", FALSE);
301
302   /* BINDING TYPES */
303
304   oid_add_from_string("shadow-agreement","2.5.19.1");
305   oid_add_from_string("hierarchical-agreement","2.5.19.2");
306   oid_add_from_string("non-specific-hierarchical-agreement","2.5.19.3");
307
308   /* ACCESS CONTROL SCHEMES */
309   oid_add_from_string("basic-ACS","2.5.28.1");
310   oid_add_from_string("simplified-ACS","2.5.28.2");
311   oid_add_from_string("ruleBased-ACS","2.5.28.3");
312   oid_add_from_string("ruleAndBasic-ACS","2.5.28.4");
313   oid_add_from_string("ruleAndSimple-ACS","2.5.28.5");
314
315   /* ADMINISTRATIVE ROLES */
316   oid_add_from_string("id-ar-autonomousArea","2.5.23.1");
317   oid_add_from_string("id-ar-accessControlSpecificArea","2.5.23.2");
318   oid_add_from_string("id-ar-accessControlInnerArea","2.5.23.3");
319   oid_add_from_string("id-ar-subschemaAdminSpecificArea","2.5.23.4");
320   oid_add_from_string("id-ar-collectiveAttributeSpecificArea","2.5.23.5");
321   oid_add_from_string("id-ar-collectiveAttributeInnerArea","2.5.23.6");
322   oid_add_from_string("id-ar-contextDefaultSpecificArea","2.5.23.7");
323   oid_add_from_string("id-ar-serviceSpecificArea","2.5.23.8");
324
325   /* remember the tpkt handler for change in preferences */
326   tpkt_handle = find_dissector("tpkt");
327
328 }
329
330 static void
331 prefs_register_dop(void)
332 {
333   static guint tcp_port = 0;
334
335   /* de-register the old port */
336   /* port 102 is registered by TPKT - don't undo this! */
337   if((tcp_port > 0) && (tcp_port != 102) && tpkt_handle)
338     dissector_delete_uint("tcp.port", tcp_port, tpkt_handle);
339
340   /* Set our port number for future use */
341   tcp_port = global_dop_tcp_port;
342
343   if((tcp_port > 0) && (tcp_port != 102) && tpkt_handle)
344     dissector_add_uint("tcp.port", tcp_port, tpkt_handle);
345
346 }