aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-vsmartcard.c
blob: e19ec35d8ebda53a7f3b966709dd74b91b1f9ae4 (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
/* packet-vsmartcard.c
 * Routines for packet dissection of vsmartcard VPCD/VPICC protocol
 * Copyright 2019 by Harald Welte <laforge@gnumonks.org>
 *
 * 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>

void proto_register_vsmartcard(void);
void proto_reg_handoff_vsmartcard(void);

/*
 * Protocol used by Frank Morgners vsmartcard VPICC + VPCD
 *
 *	https://frankmorgner.github.io/vsmartcard/virtualsmartcard/README.html
 */

/*
 * These ports are not registered for other protocols, as per
 * http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml
 *
 * But, as that document says:
 *
 ************************************************************************
 * PLEASE NOTE THE FOLLOWING:                                           *
 *                                                                      *
 * ASSIGNMENT OF A PORT NUMBER DOES NOT IN ANY WAY IMPLY AN             *
 * ENDORSEMENT OF AN APPLICATION OR PRODUCT, AND THE FACT THAT NETWORK  *
 * TRAFFIC IS FLOWING TO OR FROM A REGISTERED PORT DOES NOT MEAN THAT   *
 * IT IS "GOOD" TRAFFIC, NOR THAT IT NECESSARILY CORRESPONDS TO THE     *
 * ASSIGNED SERVICE. FIREWALL AND SYSTEM ADMINISTRATORS SHOULD          *
 * CHOOSE HOW TO CONFIGURE THEIR SYSTEMS BASED ON THEIR KNOWLEDGE OF    *
 * THE TRAFFIC IN QUESTION, NOT WHETHER THERE IS A PORT NUMBER          *
 * REGISTERED OR NOT.                                                   *
 ************************************************************************
 */
#define VSMARTCARD_TCP_PORTS "35963,35964"

static dissector_handle_t vsmartcard_tcp_handle;
//static gboolean global_ipa_in_root = FALSE;
//static gboolean global_ipa_in_info = FALSE;

/* Initialize the protocol and registered fields */
static int proto_vsmartcard = -1;

static int hf_vsmartcard_len = -1;
static int hf_vsmartcard_cmd = -1;
static int hf_vsmartcard_data = -1;

/* Initialize the subtree pointers */
static gint ett_vsmartcard = -1;

static dissector_handle_t sub_handle_simcard;

static const value_string vsmartcard_cmd_vals[] = {
	{ 0x00,	"OFF" },
	{ 0x01,	"ON" },
	{ 0x02,	"RESET" },
	{ 0x04,	"ATR" },
	{ 0,	NULL }
};

/* Code to actually dissect the packets */
static int
dissect_vsmartcard(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
	gint remaining;
	const gint header_length = 2;
	int offset = 0;
	guint16 len;

	if (tvb_reported_length(tvb) < 3)
		return 0;

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

	while ((remaining = tvb_reported_length_remaining(tvb, offset)) > 0) {
		proto_item *ti;
		proto_tree *vsmartcard_tree = NULL;
		tvbuff_t *next_tvb;

		len = tvb_get_ntohs(tvb, offset);
		if ((guint) tvb_reported_length(tvb) < (guint) len + header_length)
			return 0;

		ti = proto_tree_add_protocol_format(tree, proto_vsmartcard,
				tvb, offset, len+header_length, "vsmartcard protocol");
		vsmartcard_tree = proto_item_add_subtree(ti, ett_vsmartcard);
		proto_tree_add_item(vsmartcard_tree, hf_vsmartcard_len,
				    tvb, offset, 2, ENC_BIG_ENDIAN);
		if (len == 1) {
			proto_tree_add_item(vsmartcard_tree, hf_vsmartcard_cmd,
					    tvb, offset, 1, ENC_NA);
		} else {
			next_tvb = tvb_new_subset_length(tvb, offset+header_length, len);
			call_dissector(sub_handle_simcard, next_tvb, pinfo, tree);
		}
		offset += len + header_length;
	}

	return tvb_captured_length(tvb);
}

void proto_register_vsmartcard(void)
{
	//module_t *vsmartcard_module;

	static hf_register_info hf[] = {
		{&hf_vsmartcard_len,
		 {"Length", "vsmartcard.length",
		  FT_UINT16, BASE_DEC, NULL, 0x0,
		  "The length of the data (in bytes)", HFILL}
		 },
		{&hf_vsmartcard_cmd,
		 {"Command", "vsmartcard.command",
		  FT_UINT8, BASE_HEX, VALS(vsmartcard_cmd_vals), 0x0,
		  NULL, HFILL}
		 },
		{&hf_vsmartcard_data,
		 {"Data", "vsmartcard.data",
		  FT_BYTES, BASE_NONE, NULL, 0, NULL, HFILL}
		},
	};

	static gint *ett[] = {
		&ett_vsmartcard,
	};

	proto_vsmartcard = proto_register_protocol("Smartcard (ISO7816) over TCP", "vsmartcard", "vsmartcard");
	proto_register_field_array(proto_vsmartcard, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));

#if 0
	vsmartcard_module = prefs_register_protocol(proto_vsmartcard, NULL);
	prefs_register_bool_preference(vsmartcard_module, "hsl_debug_in_root_tree",
					"HSL Debug messages in root protocol tree",
					NULL, &global_ipa_in_root);
	prefs_register_bool_preference(vsmartcard_module, "hsl_debug_in_info",
					"HSL Debug messages in INFO column",
					NULL, &global_ipa_in_info);
#endif
}

void proto_reg_handoff_vsmartcard(void)
{
	sub_handle_simcard = find_dissector_add_dependency("gsm_sim", proto_vsmartcard);
	vsmartcard_tcp_handle = create_dissector_handle(dissect_vsmartcard, proto_vsmartcard);
	dissector_add_uint_range_with_preference("tcp.port", VSMARTCARD_TCP_PORTS, vsmartcard_tcp_handle);
}

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