summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/asn1/p1/packet-p1-template.c
blob: 68204b30f520268002961afe3ce5ddbb089a3bd7 (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* packet-p1.c
 * Routines for X.411 (X.400 Message Transfer)  packet dissection
 * Graeme Lunt 2005
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/prefs.h>
#include <epan/oids.h>
#include <epan/asn1.h>
#include <epan/expert.h>
#include <epan/strutil.h>
#include <epan/proto_data.h>
#include <wsutil/array.h>

#include "packet-ber.h"
#include "packet-acse.h"
#include "packet-ros.h"
#include "packet-rtse.h"

#include "packet-x509af.h"
#include "packet-x509ce.h"
#include "packet-x509if.h"
#include "packet-x509sat.h"

#include "packet-p1.h"

#define PNAME  "X.411 Message Transfer Service"
#define PSNAME "P1"
#define PFNAME "p1"

/* Initialize the protocol and registered fields */
static int proto_p1;
static int proto_p3;

static int hf_p1_MTS_APDU_PDU;
static int hf_p1_MTABindArgument_PDU;
static int hf_p1_MTABindResult_PDU;
static int hf_p1_MTABindError_PDU;

#include "packet-p1-hf.c"

/* Initialize the subtree pointers */
static int ett_p1;
static int ett_p3;
static int ett_p1_content_unknown;
static int ett_p1_bilateral_information;
static int ett_p1_additional_information;
static int ett_p1_unknown_standard_extension;
static int ett_p1_unknown_extension_attribute_type;
static int ett_p1_unknown_tokendata_type;
#include "packet-p1-ett.c"

static expert_field ei_p1_unknown_extension_attribute_type;
static expert_field ei_p1_unknown_standard_extension;
static expert_field ei_p1_unknown_built_in_content_type;
static expert_field ei_p1_unknown_tokendata_type;
static expert_field ei_p1_unsupported_pdu;
static expert_field ei_p1_zero_pdu;

/* Dissector tables */
static dissector_table_t p1_extension_dissector_table;
static dissector_table_t p1_extension_attribute_dissector_table;
static dissector_table_t p1_tokendata_dissector_table;

static dissector_handle_t p1_handle;

#include "packet-p1-table.c"   /* operation and error codes */

typedef struct p1_address_ctx {
    bool do_address;
    const char *content_type_id;
    bool report_unknown_content_type;
    wmem_strbuf_t* oraddress;
} p1_address_ctx_t;

static void set_do_address(asn1_ctx_t* actx, bool do_address)
{
    p1_address_ctx_t* ctx;

    if (actx->subtree.tree_ctx == NULL) {
        actx->subtree.tree_ctx = wmem_new0(actx->pinfo->pool, p1_address_ctx_t);
    }

    ctx = (p1_address_ctx_t*)actx->subtree.tree_ctx;
    ctx->do_address = do_address;
}

static p1_address_ctx_t *get_do_address_ctx(asn1_ctx_t* actx)
{
    p1_address_ctx_t* ctx = NULL;

    /* First check if called from an extension attribute */
    ctx = (p1_address_ctx_t *)p_get_proto_data(actx->pinfo->pool, actx->pinfo, proto_p1, 0);

    if (!ctx) {
        ctx = (p1_address_ctx_t*)actx->subtree.tree_ctx;
    }

    return ctx;
}

static void do_address(const char* addr, tvbuff_t* tvb_string, asn1_ctx_t* actx)
{
    p1_address_ctx_t* ctx = get_do_address_ctx(actx);

    if (ctx && ctx->do_address) {
        if (addr) {
            wmem_strbuf_append(ctx->oraddress, addr);
        }
        if (tvb_string) {
            wmem_strbuf_append(ctx->oraddress, tvb_format_text(actx->pinfo->pool, tvb_string, 0, tvb_captured_length(tvb_string)));
        }
    }
}

static void do_address_str(const char* addr, tvbuff_t* tvb_string, asn1_ctx_t* actx)
{
    wmem_strbuf_t *ddatype = (wmem_strbuf_t *)actx->value_ptr;
    p1_address_ctx_t* ctx = get_do_address_ctx(actx);

    do_address(addr, tvb_string, actx);

    if (ctx && ctx->do_address && ddatype && tvb_string)
        wmem_strbuf_append(ddatype, tvb_format_text(actx->pinfo->pool, tvb_string, 0, tvb_captured_length(tvb_string)));
}

static void do_address_str_tree(const char* addr, tvbuff_t* tvb_string, asn1_ctx_t* actx, proto_tree* tree)
{
    wmem_strbuf_t *ddatype = (wmem_strbuf_t *)actx->value_ptr;
    p1_address_ctx_t* ctx = get_do_address_ctx(actx);

    do_address(addr, tvb_string, actx);

    if (ctx && ctx->do_address && tvb_string && ddatype) {
        if (wmem_strbuf_get_len(ddatype) > 0) {
            proto_item_append_text (tree, " (%s=%s)", wmem_strbuf_get_str(ddatype), tvb_format_text(actx->pinfo->pool, tvb_string, 0, tvb_captured_length(tvb_string)));
        }
    }
}

#include "packet-p1-fn.c"

#include "packet-p1-table11.c" /* operation argument/result dissectors */
#include "packet-p1-table21.c" /* error dissector */

static const ros_info_t p3_ros_info = {
  "P3",
  &proto_p3,
  &ett_p3,
  p3_opr_code_string_vals,
  p3_opr_tab,
  p3_err_code_string_vals,
  p3_err_tab
};

void p1_initialize_content_globals (asn1_ctx_t* actx, proto_tree *tree, bool report_unknown_cont_type)
{
    p1_address_ctx_t* ctx;

    if (actx->subtree.tree_ctx == NULL) {
        actx->subtree.tree_ctx = wmem_new0(actx->pinfo->pool, p1_address_ctx_t);
    }

    ctx = (p1_address_ctx_t*)actx->subtree.tree_ctx;

    actx->subtree.top_tree = tree;
    actx->external.direct_reference = NULL;
    ctx->content_type_id = NULL;
    ctx->report_unknown_content_type = report_unknown_cont_type;
}

const char* p1_get_last_oraddress (asn1_ctx_t* actx)
{
    p1_address_ctx_t* ctx;

    if ((actx == NULL) || (actx->subtree.tree_ctx == NULL))
        return "";

    ctx = (p1_address_ctx_t*)actx->subtree.tree_ctx;
    if (wmem_strbuf_get_len(ctx->oraddress) <= 0)
        return "";

    return wmem_strbuf_get_str(ctx->oraddress);
}

/*
 * Dissect P1 MTS APDU
 */
int
dissect_p1_mts_apdu (tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, void* data _U_)
{
    proto_item *item=NULL;
    proto_tree *tree=NULL;
    asn1_ctx_t asn1_ctx;
    asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, true, pinfo);

    /* save parent_tree so subdissectors can create new top nodes */
    p1_initialize_content_globals (&asn1_ctx, parent_tree, true);

    if (parent_tree) {
        item = proto_tree_add_item(parent_tree, proto_p1, tvb, 0, -1, ENC_NA);
        tree = proto_item_add_subtree(item, ett_p1);
    }

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "P1");
      col_set_str(pinfo->cinfo, COL_INFO, "Transfer");

    dissect_p1_MTS_APDU (false, tvb, 0, &asn1_ctx, tree, hf_p1_MTS_APDU_PDU);
    p1_initialize_content_globals (&asn1_ctx, NULL, false);
    return tvb_captured_length(tvb);
}

/*
* Dissect P1 PDUs inside a PPDU.
*/
static int
dissect_p1(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, void* data)
{
    int offset = 0;
    int old_offset;
    proto_item *item;
    proto_tree *tree;
    struct SESSION_DATA_STRUCTURE* session;
    int (*p1_dissector)(bool implicit_tag _U_, tvbuff_t *tvb, int offset, asn1_ctx_t *actx _U_, proto_tree *tree, int hf_index _U_) = NULL;
    const char *p1_op_name;
    int hf_p1_index = 0;
    asn1_ctx_t asn1_ctx;
    asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, true, pinfo);

    /* do we have operation information from the ROS dissector? */
    if (data == NULL)
        return 0;
    session  = (struct SESSION_DATA_STRUCTURE*)data;

    /* save parent_tree so subdissectors can create new top nodes */
    p1_initialize_content_globals (&asn1_ctx, parent_tree, true);

    asn1_ctx.private_data = session;

    item = proto_tree_add_item(parent_tree, proto_p1, tvb, 0, -1, ENC_NA);
    tree = proto_item_add_subtree(item, ett_p1);

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "P1");
    col_clear(pinfo->cinfo, COL_INFO);

    switch(session->ros_op & ROS_OP_MASK) {
    case (ROS_OP_BIND | ROS_OP_ARGUMENT):    /*  BindInvoke */
      p1_dissector = dissect_p1_MTABindArgument;
      p1_op_name = "Bind-Argument";
      hf_p1_index = hf_p1_MTABindArgument_PDU;
      break;
    case (ROS_OP_BIND | ROS_OP_RESULT):    /*  BindResult */
      p1_dissector = dissect_p1_MTABindResult;
      p1_op_name = "Bind-Result";
      hf_p1_index = hf_p1_MTABindResult_PDU;
      break;
    case (ROS_OP_BIND | ROS_OP_ERROR):    /*  BindError */
      p1_dissector = dissect_p1_MTABindError;
      p1_op_name = "Bind-Error";
      hf_p1_index = hf_p1_MTABindError_PDU;
      break;
    case (ROS_OP_INVOKE | ROS_OP_ARGUMENT):    /*  Invoke Argument */
      p1_dissector = dissect_p1_MTS_APDU;
      p1_op_name = "Transfer";
      hf_p1_index = hf_p1_MTS_APDU_PDU;
      break;
    default:
      proto_tree_add_expert(tree, pinfo, &ei_p1_unsupported_pdu, tvb, offset, -1);
      return tvb_captured_length(tvb);
    }

    col_set_str(pinfo->cinfo, COL_INFO, p1_op_name);

    while (tvb_reported_length_remaining(tvb, offset) > 0) {
        old_offset=offset;
        offset=(*p1_dissector)(false, tvb, offset, &asn1_ctx , tree, hf_p1_index);
        if (offset == old_offset) {
            proto_tree_add_expert(tree, pinfo, &ei_p1_zero_pdu, tvb, offset, -1);
            break;
        }
    }
    p1_initialize_content_globals (&asn1_ctx, NULL, false);
    return tvb_captured_length(tvb);
}




/*--- proto_register_p1 -------------------------------------------*/
void proto_register_p1(void) {

  /* List of fields */
  static hf_register_info hf[] =
  {
      /* "Created by defining PDU in .cnf */
    { &hf_p1_MTABindArgument_PDU,
      { "MTABindArgument", "p1.MTABindArgument",
        FT_UINT32, BASE_DEC, VALS(p1_MTABindArgument_vals), 0,
        "p1.MTABindArgument", HFILL }},
    { &hf_p1_MTABindResult_PDU,
      { "MTABindResult", "p1.MTABindResult",
        FT_UINT32, BASE_DEC, VALS(p1_MTABindResult_vals), 0,
        "p1.MTABindResult", HFILL }},
    { &hf_p1_MTABindError_PDU,
      { "MTABindError", "p1.MTABindError",
        FT_UINT32, BASE_DEC, VALS(p1_MTABindError_vals), 0,
        "p1.MTABindError", HFILL }},
    { &hf_p1_MTS_APDU_PDU,
      { "MTS-APDU", "p1.MTS_APDU",
        FT_UINT32, BASE_DEC, VALS(p1_MTS_APDU_vals), 0,
        "p1.MTS_APDU", HFILL }},

#include "packet-p1-hfarr.c"
  };

  /* List of subtrees */
  static int *ett[] = {
    &ett_p1,
    &ett_p3,
    &ett_p1_content_unknown,
    &ett_p1_bilateral_information,
    &ett_p1_additional_information,
    &ett_p1_unknown_standard_extension,
    &ett_p1_unknown_extension_attribute_type,
    &ett_p1_unknown_tokendata_type,
#include "packet-p1-ettarr.c"
  };

  static ei_register_info ei[] = {
     { &ei_p1_unknown_extension_attribute_type, { "p1.unknown.extension_attribute_type", PI_UNDECODED, PI_WARN, "Unknown extension-attribute-type", EXPFILL }},
     { &ei_p1_unknown_standard_extension, { "p1.unknown.standard_extension", PI_UNDECODED, PI_WARN, "Unknown standard-extension", EXPFILL }},
     { &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 }},
     { &ei_p1_unknown_tokendata_type, { "p1.unknown.tokendata_type", PI_UNDECODED, PI_WARN, "Unknown tokendata-type", EXPFILL }},
     { &ei_p1_unsupported_pdu, { "p1.unsupported_pdu", PI_UNDECODED, PI_WARN, "Unsupported P1 PDU", EXPFILL }},
     { &ei_p1_zero_pdu, { "p1.zero_pdu", PI_PROTOCOL, PI_ERROR, "Internal error, zero-byte P1 PDU", EXPFILL }},
  };

  expert_module_t* expert_p1;
  module_t *p1_module;

  /* Register protocol */
  proto_p1 = proto_register_protocol(PNAME, PSNAME, PFNAME);
  p1_handle = register_dissector("p1", dissect_p1, proto_p1);

  proto_p3 = proto_register_protocol("X.411 Message Access Service", "P3", "p3");

  /* Register fields and subtrees */
  proto_register_field_array(proto_p1, hf, array_length(hf));
  proto_register_subtree_array(ett, array_length(ett));
  expert_p1 = expert_register_protocol(proto_p1);
  expert_register_field_array(expert_p1, ei, array_length(ei));

  p1_extension_dissector_table = register_dissector_table("p1.extension", "P1-EXTENSION", proto_p1, FT_UINT32, BASE_DEC);
  p1_extension_attribute_dissector_table = register_dissector_table("p1.extension-attribute", "P1-EXTENSION-ATTRIBUTE", proto_p1, FT_UINT32, BASE_DEC);
  p1_tokendata_dissector_table = register_dissector_table("p1.tokendata", "P1-TOKENDATA", proto_p1, FT_UINT32, BASE_DEC);

  /* Register our configuration options for P1, particularly our port */

  p1_module = prefs_register_protocol_subtree("OSI/X.400", proto_p1, NULL);
  /* For reading older preference files with "x411." preferences */
  prefs_register_module_alias("x411", p1_module);

  prefs_register_obsolete_preference(p1_module, "tcp.port");

  prefs_register_static_text_preference(p1_module, "tcp_port_info",
            "The TCP ports used by the P1 protocol should be added to the TPKT preference \"TPKT TCP ports\", or by selecting \"TPKT\" as the \"Transport\" protocol in the \"Decode As\" dialog.",
            "P1 TCP Port preference moved information");

  register_ber_syntax_dissector("P1 Message", proto_p1, dissect_p1_mts_apdu);
#include "packet-p1-syn-reg.c"
}


/*--- proto_reg_handoff_p1 --- */
void proto_reg_handoff_p1(void) {
#include "packet-p1-dis-tab.c"

  /* APPLICATION CONTEXT */

  oid_add_from_string("id-ac-mts-transfer","2.6.0.1.6");

  /* ABSTRACT SYNTAXES */
  register_rtse_oid_dissector_handle("2.6.0.2.12", p1_handle, 0, "id-as-mta-rtse", true);
  register_rtse_oid_dissector_handle("2.6.0.2.7", p1_handle, 0, "id-as-mtse", false);


  register_rtse_oid_dissector_handle("applicationProtocol.1", p1_handle, 0, "mts-transfer-protocol-1984", false);
  register_rtse_oid_dissector_handle("applicationProtocol.12", p1_handle, 0, "mta-transfer-protocol", false);

  /* the ROS dissector will use the registered P3 ros info */
  register_rtse_oid_dissector_handle(id_as_mts_rtse, NULL, 0, "id-as-mts-rtse", true);
  register_rtse_oid_dissector_handle(id_as_msse, NULL, 0, "id-as-msse", true);

  /* APPLICATION CONTEXT */

  oid_add_from_string("id-ac-mts-access-88", id_ac_mts_access_88);
  oid_add_from_string("id-ac-mts-forced-access-88", id_ac_mts_forced_access_88);
  oid_add_from_string("id-ac-mts-access-94", id_ac_mts_access_94);
  oid_add_from_string("id-ac-mts-forced-access-94", id_ac_mts_forced_access_94);


  /* Register P3 with ROS */

  register_ros_protocol_info(id_as_msse, &p3_ros_info, 0, "id-as-msse", false);

  register_ros_protocol_info(id_as_mdse_88, &p3_ros_info, 0, "id-as-mdse-88", false);
  register_ros_protocol_info(id_as_mdse_94, &p3_ros_info, 0, "id-as-mdse-94", false);

  register_ros_protocol_info(id_as_mase_88, &p3_ros_info, 0, "id-as-mase-88", false);
  register_ros_protocol_info(id_as_mase_94, &p3_ros_info, 0, "id-as-mase-94", false);

  register_ros_protocol_info(id_as_mts, &p3_ros_info, 0, "id-as-mts", false);
  register_ros_protocol_info(id_as_mts_rtse, &p3_ros_info, 0, "id-as-mts-rtse", true);

}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */