Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / packet-zvt.c
blobb81e93facefc5e228ce84377295eb38e89233daa
1 /* packet-zvt.c
2 * Routines for ZVT dissection
3 * Copyright 2014-2015, Martin Kaiser <martin@kaiser.cx>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
12 /* ZVT is a manufacturer-independent protocol between payment terminals and
13 * electronic cash-register systems / vending machines
15 * the specifications are available from https://www.terminalhersteller.de
17 * ZVT defines a "serial transport protocol" and a "TCP/IP transport
18 * protocol"
20 * ZVT can sit on top of USB, either the serial or the TCP/IP protocol
21 * can be used in this case - this is not supported for now
23 * a dump of ZVT data can be converted to pcap, using a user-defined DLT
24 * we register the dissector by name and try to auto-detect the serial
25 * or TCP/IP protocol
27 * finally, ZVT can run on top of TCP, the default port is 20007, only
28 * the TCP/IP protocol can be used here
32 #include "config.h"
34 #include <wsutil/array.h>
35 #include <epan/packet.h>
36 #include <epan/expert.h>
37 #include <epan/tfs.h>
38 #include "packet-tcp.h"
40 /* special characters of the serial transport protocol */
41 #define STX 0x02
42 #define ETX 0x03
43 #define ACK 0x06
44 #define DLE 0x10
45 #define NAK 0x15
47 /* an APDU needs at least a 2-byte control-field and one byte length */
48 #define ZVT_APDU_MIN_LEN 3
51 static GHashTable *apdu_table = NULL, *bitmap_table = NULL, *tlv_table;
53 static wmem_tree_t *transactions;
55 typedef struct _zvt_transaction_t {
56 uint32_t rqst_frame;
57 uint32_t resp_frame;
58 uint16_t ctrl;
59 } zvt_transaction_t;
61 typedef enum _zvt_direction_t {
62 DIRECTION_UNKNOWN,
63 DIRECTION_ECR_TO_PT,
64 DIRECTION_PT_TO_ECR
65 } zvt_direction_t;
67 /* source/destination address field */
68 #define ADDR_ECR "ECR"
69 #define ADDR_PT "PT"
71 #define CCRC_POS 0x80
72 #define CCRC_NEG 0x84
74 /* "don't care" value for min_len_field */
75 #define LEN_FIELD_ANY UINT32_MAX
77 typedef struct _apdu_info_t {
78 uint16_t ctrl;
79 uint32_t min_len_field;
80 zvt_direction_t direction;
81 void (*dissect_payload)(tvbuff_t *, int, uint16_t,
82 packet_info *, proto_tree *, zvt_transaction_t *);
83 } apdu_info_t;
85 /* control code 0 is not defined in the specification */
86 #define ZVT_CTRL_NONE 0x0000
88 #define CTRL_STATUS 0x040F
89 #define CTRL_INT_STATUS 0x04FF
90 #define CTRL_REGISTRATION 0x0600
91 #define CTRL_AUTHORISATION 0x0601
92 #define CTRL_COMPLETION 0x060F
93 #define CTRL_ABORT 0x061E
94 #define CTRL_REVERSAL 0x0630
95 #define CTRL_REFUND 0x0631
96 #define CTRL_END_OF_DAY 0x0650
97 #define CTRL_DIAG 0x0670
98 #define CTRL_INIT 0x0693
99 #define CTRL_PRINT_LINE 0x06D1
100 #define CTRL_PRINT_TEXT 0x06D3
102 static void dissect_zvt_int_status(tvbuff_t *tvb, int offset, uint16_t len,
103 packet_info *pinfo, proto_tree *tree, zvt_transaction_t *zvt_trans);
104 static void dissect_zvt_reg(tvbuff_t *tvb, int offset, uint16_t len _U_,
105 packet_info *pinfo, proto_tree *tree, zvt_transaction_t *zvt_trans);
106 static void dissect_zvt_bitmap_seq(tvbuff_t *tvb, int offset, uint16_t len,
107 packet_info *pinfo, proto_tree *tree, zvt_transaction_t *zvt_trans _U_);
108 static void dissect_zvt_init(tvbuff_t *tvb, int offset, uint16_t len _U_,
109 packet_info *pinfo _U_, proto_tree *tree, zvt_transaction_t *zvt_trans _U_);
110 static void dissect_zvt_pass_bitmap_seq(tvbuff_t *tvb, int offset, uint16_t len _U_,
111 packet_info *pinfo, proto_tree *tree, zvt_transaction_t *zvt_trans);
112 static void dissect_zvt_abort(tvbuff_t *tvb, int offset, uint16_t len _U_,
113 packet_info *pinfo, proto_tree *tree, zvt_transaction_t *zvt_trans);
115 static const apdu_info_t apdu_info[] = {
116 { CTRL_STATUS, 0, DIRECTION_PT_TO_ECR, dissect_zvt_bitmap_seq },
117 { CTRL_INT_STATUS, 0, DIRECTION_PT_TO_ECR, dissect_zvt_int_status },
118 { CTRL_REGISTRATION, 4, DIRECTION_ECR_TO_PT, dissect_zvt_reg },
119 /* authorisation has at least a 0x04 tag and 6 bytes for the amount */
120 { CTRL_AUTHORISATION, 7, DIRECTION_ECR_TO_PT, dissect_zvt_bitmap_seq },
121 { CTRL_COMPLETION, 0, DIRECTION_PT_TO_ECR, dissect_zvt_bitmap_seq },
122 { CTRL_ABORT, 0, DIRECTION_PT_TO_ECR, dissect_zvt_abort },
123 { CTRL_REVERSAL, 0, DIRECTION_ECR_TO_PT, dissect_zvt_pass_bitmap_seq },
124 { CTRL_REFUND, 0, DIRECTION_ECR_TO_PT, dissect_zvt_pass_bitmap_seq },
125 { CTRL_END_OF_DAY, 0, DIRECTION_ECR_TO_PT, NULL },
126 { CTRL_DIAG, 0, DIRECTION_ECR_TO_PT, NULL },
127 { CTRL_INIT, 0, DIRECTION_ECR_TO_PT, dissect_zvt_init },
128 { CTRL_PRINT_LINE, 0, DIRECTION_PT_TO_ECR, NULL },
129 { CTRL_PRINT_TEXT, 0, DIRECTION_PT_TO_ECR, dissect_zvt_bitmap_seq }
133 typedef struct _bitmap_info_t {
134 uint8_t bmp;
135 uint16_t payload_len;
136 int (*dissect_payload)(tvbuff_t *, int, packet_info *, proto_tree *);
137 } bitmap_info_t;
139 #define BMP_TIMEOUT 0x01
140 #define BMP_MAX_STAT_INFO 0x02
141 #define BMP_SVC_BYTE 0x03
142 #define BMP_AMOUNT 0x04
143 #define BMP_PUMP_NR 0x05
144 #define BMP_TLV_CONTAINER 0x06
145 #define BMP_TRACE_NUM 0x0B
146 #define BMP_TIME 0x0C
147 #define BMP_DATE 0x0D
148 #define BMP_EXP_DATE 0x0E
149 #define BMP_CARD_SEQ_NUM 0x17
150 #define BMP_PAYMENT_TYPE 0x19
151 #define BMP_CARD_NUM 0x22
152 #define BMP_T2_DAT 0x23
153 #define BMP_T3_DAT 0x24
154 #define BMP_RES_CODE 0x27
155 #define BMP_TID 0x29
156 #define BMP_VU_NUMBER 0x2A
157 #define BMP_T1_DAT 0x2D
158 #define BMP_CVV_CVC 0x3A
159 #define BMP_AID 0x3B
160 #define BMP_ADD_DATA 0x3C
161 #define BMP_CC 0x49
162 #define BMP_RCPT_NUM 0x87
163 #define BMP_CARD_TYPE 0x8A
164 #define BMP_CARD_NAME 0x8B
166 #define BMP_PLD_LEN_UNKNOWN 0 /* unknown/variable bitmap payload len */
168 static int dissect_zvt_amount(
169 tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree);
170 static int dissect_zvt_tlv_container(
171 tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree);
172 static inline int dissect_zvt_res_code(
173 tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree);
174 static inline int dissect_zvt_cc(
175 tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree);
176 static inline int dissect_zvt_terminal_id(
177 tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree);
178 static inline int dissect_zvt_time(
179 tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree);
180 static inline int dissect_zvt_date(
181 tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree);
182 static inline int dissect_zvt_card_type(
183 tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree);
184 static inline int dissect_zvt_trace_number(
185 tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree);
186 static inline int dissect_zvt_expiry_date(
187 tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree);
188 static inline int dissect_zvt_card_number(
189 tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree);
190 static inline int dissect_zvt_card_name(
191 tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree);
192 static inline int dissect_zvt_additional_data(
193 tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree);
195 static const bitmap_info_t bitmap_info[] = {
196 { BMP_TIMEOUT, 1, NULL },
197 { BMP_MAX_STAT_INFO, 1, NULL },
198 { BMP_SVC_BYTE, 1, NULL },
199 { BMP_AMOUNT, 6, dissect_zvt_amount },
200 { BMP_PUMP_NR, 1, NULL },
201 { BMP_TLV_CONTAINER, BMP_PLD_LEN_UNKNOWN, dissect_zvt_tlv_container },
202 { BMP_TRACE_NUM, 3, dissect_zvt_trace_number },
203 { BMP_TIME, 3, dissect_zvt_time },
204 { BMP_DATE, 2, dissect_zvt_date },
205 { BMP_EXP_DATE, 2, dissect_zvt_expiry_date },
206 { BMP_CARD_SEQ_NUM, 2, NULL },
207 { BMP_PAYMENT_TYPE, 1, NULL },
208 { BMP_CARD_NUM, BMP_PLD_LEN_UNKNOWN, dissect_zvt_card_number },
209 { BMP_T2_DAT, BMP_PLD_LEN_UNKNOWN, NULL },
210 { BMP_T3_DAT, BMP_PLD_LEN_UNKNOWN, NULL },
211 { BMP_RES_CODE, 1, dissect_zvt_res_code },
212 { BMP_TID, 4, dissect_zvt_terminal_id },
213 { BMP_VU_NUMBER, 15, NULL },
214 { BMP_T1_DAT, BMP_PLD_LEN_UNKNOWN, NULL },
215 { BMP_CVV_CVC, 2, NULL },
216 { BMP_AID, 8, NULL },
217 { BMP_ADD_DATA, BMP_PLD_LEN_UNKNOWN, dissect_zvt_additional_data },
218 { BMP_CC, 2, dissect_zvt_cc },
219 { BMP_RCPT_NUM, 2, NULL },
220 { BMP_CARD_TYPE, 1, dissect_zvt_card_type },
221 { BMP_CARD_NAME, BMP_PLD_LEN_UNKNOWN, dissect_zvt_card_name }
225 void proto_register_zvt(void);
226 void proto_reg_handoff_zvt(void);
228 static dissector_handle_t zvt_tcp_handle;
230 static int proto_zvt;
232 static int ett_zvt;
233 static int ett_zvt_apdu;
234 static int ett_zvt_bitmap;
235 static int ett_zvt_tlv_dat_obj;
236 static int ett_zvt_tlv_subseq;
237 static int ett_zvt_tlv_tag;
238 static int ett_zvt_tlv_receipt;
240 static int hf_zvt_resp_in;
241 static int hf_zvt_resp_to;
242 static int hf_zvt_serial_char;
243 static int hf_zvt_crc;
244 static int hf_zvt_ctrl;
245 static int hf_zvt_ccrc;
246 static int hf_zvt_aprc;
247 static int hf_zvt_len;
248 static int hf_zvt_data;
249 static int hf_zvt_int_status;
250 static int hf_zvt_pwd;
251 static int hf_zvt_reg_cfg;
252 static int hf_zvt_res_code;
253 static int hf_zvt_cc;
254 static int hf_zvt_amount;
255 static int hf_zvt_terminal_id;
256 static int hf_zvt_time;
257 static int hf_zvt_date;
258 static int hf_zvt_card_type;
259 static int hf_zvt_bmp;
260 static int hf_zvt_tlv_total_len;
261 static int hf_zvt_tlv_tag;
262 static int hf_zvt_tlv_tag_class;
263 static int hf_zvt_tlv_tag_type;
264 static int hf_zvt_tlv_len;
265 static int hf_zvt_text_lines_line;
266 static int hf_zvt_permitted_cmd;
267 static int hf_zvt_receipt_type;
268 static int hf_zvt_receipt_parameter_positive_customer;
269 static int hf_zvt_receipt_parameter_negative_customer;
270 static int hf_zvt_receipt_parameter_positive_merchant;
271 static int hf_zvt_receipt_parameter_negative_merchant;
272 static int hf_zvt_receipt_parameter_customer_before_merchant;
273 static int hf_zvt_receipt_parameter_print_short_receipt;
274 static int hf_zvt_receipt_parameter_no_product_data;
275 static int hf_zvt_receipt_parameter_ecr_as_printer;
276 static int hf_zvt_receipt_parameter;
277 static int hf_zvt_trace_number;
278 static int hf_zvt_expiry_date;
279 static int hf_zvt_card_number;
280 static int hf_zvt_card_name;
281 static int hf_zvt_additional_data;
282 static int hf_zvt_characters_per_line;
283 static int hf_zvt_receipt_info;
284 static int hf_zvt_receipt_info_positive;
285 static int hf_zvt_receipt_info_signature;
286 static int hf_zvt_receipt_info_negative;
287 static int hf_zvt_receipt_info_printing;
289 static int * const receipt_parameter_flag_fields[] = {
290 &hf_zvt_receipt_parameter_positive_customer,
291 &hf_zvt_receipt_parameter_negative_customer,
292 &hf_zvt_receipt_parameter_positive_merchant,
293 &hf_zvt_receipt_parameter_negative_merchant,
294 &hf_zvt_receipt_parameter_customer_before_merchant,
295 &hf_zvt_receipt_parameter_print_short_receipt,
296 &hf_zvt_receipt_parameter_no_product_data,
297 &hf_zvt_receipt_parameter_ecr_as_printer,
298 NULL
301 static int * const receipt_info_fields[] = {
302 &hf_zvt_receipt_info_positive,
303 &hf_zvt_receipt_info_signature,
304 &hf_zvt_receipt_info_negative,
305 &hf_zvt_receipt_info_printing,
306 NULL
309 static expert_field ei_invalid_apdu_len;
311 static const value_string serial_char[] = {
312 { STX, "Start of text (STX)" },
313 { ETX, "End of text (ETX)" },
314 { ACK, "Acknowledged (ACK)" },
315 { DLE, "Data line escape (DLE)" },
316 { NAK, "Not acknowledged (NAK)" },
317 { 0, NULL }
319 static value_string_ext serial_char_ext = VALUE_STRING_EXT_INIT(serial_char);
322 static const value_string ctrl_field[] = {
323 { CTRL_STATUS, "Status Information" },
324 { CTRL_INT_STATUS, "Intermediate Status Information" },
325 { CTRL_REGISTRATION, "Registration" },
326 { CTRL_AUTHORISATION, "Authorisation" },
327 { CTRL_COMPLETION, "Completion" },
328 { CTRL_ABORT, "Abort" },
329 { CTRL_REVERSAL, "Reversal" },
330 { CTRL_REFUND, "Refund" },
331 { CTRL_END_OF_DAY, "End Of Day" },
332 { CTRL_DIAG, "Diagnosis" },
333 { CTRL_INIT, "Initialisation" },
334 { CTRL_PRINT_LINE, "Print Line" },
335 { CTRL_PRINT_TEXT, "Print Text Block" },
336 { 0, NULL }
338 static value_string_ext ctrl_field_ext = VALUE_STRING_EXT_INIT(ctrl_field);
340 /* ISO 4217 currency codes */
341 static const value_string zvt_cc[] = {
342 { 0x0756, "CHF" },
343 { 0x0826, "GBP" },
344 { 0x0840, "USD" },
345 { 0x0978, "EUR" },
346 { 0, NULL }
349 static const value_string receipt_type[] = {
350 { 0x01, "Transaction receipt (merchant)" },
351 { 0x02, "Transaction receipt (customer)" },
352 { 0x03, "Administration receipt" },
353 { 0, NULL }
356 static const value_string card_type[] = {
357 { 2, "ec-card" },
358 { 5, "girocard" },
359 { 6, "Mastercard" },
360 { 10, "VISA" },
361 { 46, "Maestro" },
362 { 0, NULL }
364 static value_string_ext card_type_ext = VALUE_STRING_EXT_INIT(card_type);
366 static const value_string bitmap[] = {
367 { BMP_TIMEOUT, "Timeout" },
368 { BMP_MAX_STAT_INFO, "max. status info" },
369 { BMP_SVC_BYTE, "Service byte" },
370 { BMP_AMOUNT, "Amount" },
371 { BMP_PUMP_NR, "Pump number" },
372 { BMP_TLV_CONTAINER, "TLV container" },
373 { BMP_TRACE_NUM, "Trace number" },
374 { BMP_TIME, "Time" },
375 { BMP_DATE, "Date" },
376 { BMP_EXP_DATE, "Expiry date" },
377 { BMP_CARD_SEQ_NUM, "Card sequence number" },
378 { BMP_PAYMENT_TYPE, "Payment type" },
379 { BMP_CARD_NUM, "Card number" },
380 { BMP_T2_DAT, "Track 2 data" },
381 { BMP_T3_DAT, "Track 3 data" },
382 { BMP_RES_CODE, "Result code" },
383 { BMP_TID, "Terminal ID" },
384 { BMP_VU_NUMBER, "Contract number"},
385 { BMP_T1_DAT, "Track 1 data" },
386 { BMP_CVV_CVC, "CVV / CVC" },
387 { BMP_AID, "Authorization attribute" },
388 { BMP_ADD_DATA, "Additional data" },
389 { BMP_CC, "Currency code (CC)" },
390 { BMP_RCPT_NUM, "Receipt number" },
391 { BMP_CARD_TYPE, "Card type" },
392 { BMP_CARD_NAME, "Card name" },
393 { 0, NULL }
395 static value_string_ext bitmap_ext = VALUE_STRING_EXT_INIT(bitmap);
397 static const value_string tlv_tag_class[] = {
398 { 0x00, "Universal" },
399 { 0x01, "Application" },
400 { 0x02, "Context-specific" },
401 { 0x03, "Private" },
402 { 0, NULL }
404 static value_string_ext tlv_tag_class_ext = VALUE_STRING_EXT_INIT(tlv_tag_class);
406 #define TLV_TAG_TEXT_LINES 0x07
407 #define TLV_TAG_ATTRIBUTE 0x09
408 #define TLV_TAG_PERMITTED_ZVT_CMD 0x0A
409 #define TLV_TAG_CHARS_PER_LINE 0x12
410 #define TLV_TAG_DISPLAY_TEXTS 0x24
411 #define TLV_TAG_PRINT_TEXTS 0x25
412 #define TLV_TAG_PERMITTED_ZVT_CMDS 0x26
413 #define TLV_TAG_SUPPORTED_CHARSETS 0x27
414 #define TLV_TAG_PAYMENT_TYPE 0x2F
415 #define TLV_TAG_EMV_CFG_PARAM 0x40
416 #define TLV_TAG_CARD_TYPE_ID 0x41
417 #define TLV_TAG_RECEIPT_PARAMETER 0x45
418 #define TLV_TAG_APPLICATION 0x60
419 #define TLV_TAG_RECEIPT_PARAM 0x1F04
420 #define TLV_TAG_RECEIPT_TYPE 0x1F07
421 #define TLV_TAG_CARDHOLDER_AUTH 0x1F10
422 #define TLV_TAG_ONLINE_FLAG 0x1F11
423 #define TLV_TAG_CARD_TYPE 0x1F12
424 #define TLV_TAG_RECEIPT_INFO 0x1F37
427 typedef struct _tlv_seq_info_t {
428 unsigned txt_enc;
429 } tlv_seq_info_t;
432 static int
433 dissect_zvt_tlv_seq(tvbuff_t *tvb, int offset, uint16_t seq_max_len,
434 packet_info *pinfo, proto_tree *tree, tlv_seq_info_t *seq_info);
436 typedef struct _tlv_info_t {
437 uint32_t tag;
438 int (*dissect_payload)(tvbuff_t *, int, int,
439 packet_info *, proto_tree *, tlv_seq_info_t *);
440 } tlv_info_t;
442 static inline int dissect_zvt_tlv_text_lines(
443 tvbuff_t *tvb, int offset, int len,
444 packet_info *pinfo _U_, proto_tree *tree, tlv_seq_info_t *seq_info);
446 static inline int dissect_zvt_tlv_subseq(
447 tvbuff_t *tvb, int offset, int len,
448 packet_info *pinfo, proto_tree *tree, tlv_seq_info_t *seq_info);
450 static inline int dissect_zvt_tlv_permitted_cmd(
451 tvbuff_t *tvb, int offset, int len,
452 packet_info *pinfo _U_, proto_tree *tree, tlv_seq_info_t *seq_info _U_);
454 static inline int dissect_zvt_tlv_receipt_type(
455 tvbuff_t *tvb, int offset, int len,
456 packet_info *pinfo _U_, proto_tree *tree, tlv_seq_info_t *seq_info _U_);
458 static inline int dissect_zvt_tlv_receipt_param(
459 tvbuff_t *tvb, int offset, int len,
460 packet_info *pinfo _U_, proto_tree *tree, tlv_seq_info_t *seq_info _U_);
462 static inline int dissect_zvt_tlv_characters_per_line(
463 tvbuff_t *tvb, int offset, int len,
464 packet_info *pinfo, proto_tree *tree, tlv_seq_info_t *seq_info _U_);
466 static inline int dissect_zvt_tlv_receipt_info(
467 tvbuff_t *tvb, int offset, int len, packet_info *pinfo _U_,
468 proto_tree *tree, tlv_seq_info_t *seq_info _U_);
470 static const tlv_info_t tlv_info[] = {
471 { TLV_TAG_TEXT_LINES, dissect_zvt_tlv_text_lines },
472 { TLV_TAG_DISPLAY_TEXTS, dissect_zvt_tlv_subseq },
473 { TLV_TAG_PRINT_TEXTS, dissect_zvt_tlv_subseq },
474 { TLV_TAG_PAYMENT_TYPE, dissect_zvt_tlv_subseq },
475 { TLV_TAG_PERMITTED_ZVT_CMDS, dissect_zvt_tlv_subseq },
476 { TLV_TAG_PERMITTED_ZVT_CMD, dissect_zvt_tlv_permitted_cmd },
477 { TLV_TAG_RECEIPT_TYPE, dissect_zvt_tlv_receipt_type },
478 { TLV_TAG_RECEIPT_PARAM, dissect_zvt_tlv_receipt_param },
479 { TLV_TAG_CHARS_PER_LINE, dissect_zvt_tlv_characters_per_line },
480 { TLV_TAG_RECEIPT_INFO, dissect_zvt_tlv_receipt_info }
483 static const value_string tlv_tags[] = {
484 { TLV_TAG_TEXT_LINES, "Text lines" },
485 { TLV_TAG_ATTRIBUTE, "Attribute"},
486 { TLV_TAG_CHARS_PER_LINE,
487 "Number of characters per line of the printer" },
488 { TLV_TAG_DISPLAY_TEXTS, "Display texts" },
489 { TLV_TAG_PRINT_TEXTS, "Print texts" },
490 { TLV_TAG_PERMITTED_ZVT_CMDS, "List of permitted ZVT commands" },
491 { TLV_TAG_SUPPORTED_CHARSETS, "List of supported character sets" },
492 { TLV_TAG_PAYMENT_TYPE, "Payment type" },
493 { TLV_TAG_EMV_CFG_PARAM, "EMV config parameter" },
494 { TLV_TAG_CARD_TYPE_ID, "Card type ID" },
495 { TLV_TAG_RECEIPT_PARAMETER, "Receipt parameter (EMV)" },
496 { TLV_TAG_APPLICATION, "Application" },
497 { TLV_TAG_RECEIPT_PARAM, "Receipt parameter" },
498 { TLV_TAG_RECEIPT_TYPE, "Receipt type" },
499 { TLV_TAG_CARDHOLDER_AUTH, "Cardholder authentication" },
500 { TLV_TAG_ONLINE_FLAG, "Online flag" },
501 { TLV_TAG_CARD_TYPE, "Card type" },
502 { TLV_TAG_RECEIPT_INFO, "Receipt information" },
503 { 0, NULL }
505 static value_string_ext tlv_tags_ext = VALUE_STRING_EXT_INIT(tlv_tags);
507 static inline int dissect_zvt_tlv_text_lines(
508 tvbuff_t *tvb, int offset, int len,
509 packet_info *pinfo _U_, proto_tree *tree, tlv_seq_info_t *seq_info)
511 proto_tree_add_item(tree, hf_zvt_text_lines_line,
512 tvb, offset, len, seq_info->txt_enc | ENC_NA);
513 return len;
517 static inline int dissect_zvt_tlv_subseq(
518 tvbuff_t *tvb, int offset, int len,
519 packet_info *pinfo, proto_tree *tree, tlv_seq_info_t *seq_info)
521 proto_tree *subseq_tree;
523 subseq_tree = proto_tree_add_subtree(tree,
524 tvb, offset, len, ett_zvt_tlv_subseq, NULL,
525 "Subsequence");
527 return dissect_zvt_tlv_seq(tvb, offset, len, pinfo, subseq_tree, seq_info);
531 static inline int dissect_zvt_tlv_permitted_cmd(
532 tvbuff_t *tvb, int offset, int len,
533 packet_info *pinfo _U_, proto_tree *tree, tlv_seq_info_t *seq_info _U_)
535 proto_tree_add_item(tree, hf_zvt_permitted_cmd,
536 tvb, offset, len, ENC_BIG_ENDIAN);
537 return len;
541 static inline int dissect_zvt_tlv_receipt_type(
542 tvbuff_t *tvb, int offset, int len,
543 packet_info *pinfo _U_, proto_tree *tree, tlv_seq_info_t *seq_info _U_)
545 proto_tree_add_item(tree, hf_zvt_receipt_type,
546 tvb, offset, len, ENC_BIG_ENDIAN);
547 return len;
551 static inline int dissect_zvt_tlv_receipt_param(
552 tvbuff_t *tvb, int offset, int len,
553 packet_info *pinfo _U_, proto_tree *tree, tlv_seq_info_t *seq_info _U_)
555 proto_tree_add_bitmask(tree, tvb, offset, hf_zvt_receipt_parameter, ett_zvt_tlv_receipt, receipt_parameter_flag_fields, ENC_BIG_ENDIAN);
556 return len;
560 static inline int dissect_zvt_tlv_characters_per_line(
561 tvbuff_t *tvb, int offset, int len,
562 packet_info *pinfo _U_, proto_tree *tree, tlv_seq_info_t *seq_info _U_)
564 proto_tree_add_item(tree, hf_zvt_characters_per_line, tvb, offset, 1,
565 ENC_BCD_DIGITS_0_9 | ENC_BIG_ENDIAN);
566 return len;
570 static inline int dissect_zvt_tlv_receipt_info(
571 tvbuff_t *tvb, int offset, int len,
572 packet_info *pinfo _U_, proto_tree *tree, tlv_seq_info_t *seq_info _U_)
574 proto_tree_add_bitmask(tree, tvb, offset, hf_zvt_receipt_info,
575 ett_zvt_tlv_receipt, receipt_info_fields, ENC_BIG_ENDIAN);
576 return len;
580 static int
581 dissect_zvt_tlv_tag(tvbuff_t *tvb, int offset,
582 packet_info *pinfo _U_, proto_tree *tree, uint32_t *tag)
584 int offset_start;
585 uint8_t one_byte;
586 uint32_t _tag;
587 proto_item *tag_ti;
588 proto_tree *tag_tree;
590 offset_start = offset;
592 one_byte = tvb_get_uint8(tvb, offset);
593 _tag = one_byte;
594 offset++;
595 if ((one_byte & 0x1F) == 0x1F) {
596 do {
597 if ((offset-offset_start)>4) {
598 /* we support tags of <= 4 bytes
599 (the specification defines only 1 and 2-byte tags) */
600 return -1;
602 one_byte = tvb_get_uint8(tvb, offset);
603 _tag = _tag << 8 | (one_byte&0x7F);
604 offset++;
605 } while (one_byte & 0x80);
608 tag_ti = proto_tree_add_uint_format(tree, hf_zvt_tlv_tag,
609 tvb, offset_start, offset-offset_start, _tag,
610 "Tag: %s (0x%x)",
611 val_to_str_ext_const(_tag, &tlv_tags_ext, "unknown"), _tag);
613 tag_tree = proto_item_add_subtree(tag_ti, ett_zvt_tlv_tag);
614 proto_tree_add_item(tag_tree, hf_zvt_tlv_tag_class,
615 tvb, offset_start, 1, ENC_BIG_ENDIAN);
616 proto_tree_add_item(tag_tree, hf_zvt_tlv_tag_type,
617 tvb, offset_start, 1, ENC_BIG_ENDIAN);
619 if (tag)
620 *tag = _tag;
621 return offset-offset_start;
625 static int
626 dissect_zvt_tlv_len(tvbuff_t *tvb, int offset,
627 packet_info *pinfo _U_, proto_tree *tree, int hf, uint16_t *len)
629 uint16_t _len;
630 int len_bytes = 1;
632 _len = tvb_get_uint8(tvb, offset);
633 if (_len & 0x80) {
634 if ((_len & 0x03) == 1) {
635 len_bytes++;
636 _len = tvb_get_uint8(tvb, offset+1);
638 else if ((_len & 0x03) == 2) {
639 len_bytes += 2;
640 _len = tvb_get_ntohs(tvb, offset+1);
642 else {
643 /* XXX - expert info */
644 return -1;
648 proto_tree_add_uint(tree, hf, tvb, offset, len_bytes, _len);
649 if (len)
650 *len = _len;
652 return len_bytes;
656 static int
657 dissect_zvt_tlv_seq(tvbuff_t *tvb, int offset, uint16_t seq_max_len,
658 packet_info *pinfo, proto_tree *tree, tlv_seq_info_t *seq_info)
660 int offset_start;
661 proto_item *dat_obj_it;
662 proto_tree *dat_obj_tree;
663 int tag_len;
664 uint32_t tag;
665 int data_len_bytes;
666 uint16_t data_len = 0;
667 tlv_info_t *ti;
668 int ret;
670 if (!seq_info) {
671 seq_info = wmem_new(pinfo->pool, tlv_seq_info_t);
673 /* by default, text lines are using the CP437 charset
674 there's an object to change the encoding
675 (XXX - does this change apply only to the current message?) */
676 seq_info->txt_enc = ENC_CP437;
679 offset_start = offset;
681 while (offset-offset_start < seq_max_len) {
682 dat_obj_tree = proto_tree_add_subtree(tree,
683 tvb, offset, -1, ett_zvt_tlv_dat_obj, &dat_obj_it,
684 "TLV data object");
686 tag_len = dissect_zvt_tlv_tag(tvb, offset, pinfo, dat_obj_tree, &tag);
687 if (tag_len <= 0)
688 return offset - offset_start;
689 offset += tag_len;
691 data_len_bytes = dissect_zvt_tlv_len(tvb, offset, pinfo,
692 dat_obj_tree,hf_zvt_tlv_len, &data_len);
693 if (data_len_bytes > 0)
694 offset += data_len_bytes;
696 /* set the sequence length now that we know it
697 this way, we don't have to put the whole switch statement
698 under if (data_len > 0) */
699 proto_item_set_len(dat_obj_it, tag_len + data_len_bytes + data_len);
700 if (data_len == 0)
701 continue;
703 ti = (tlv_info_t *)g_hash_table_lookup(
704 tlv_table, GUINT_TO_POINTER((unsigned)tag));
705 if (ti && ti->dissect_payload) {
706 ret = ti->dissect_payload(
707 tvb, offset, (int)data_len, pinfo, dat_obj_tree, seq_info);
708 if (ret <= 0) {
709 /* XXX - expert info */
713 offset += data_len;
716 return offset - offset_start;
720 static int
721 dissect_zvt_tlv_container(tvbuff_t *tvb, int offset,
722 packet_info *pinfo, proto_tree *tree)
724 int offset_start;
725 int total_len_bytes, seq_len;
726 uint16_t seq_max_len = 0;
728 offset_start = offset;
730 total_len_bytes = dissect_zvt_tlv_len(tvb, offset, pinfo,
731 tree, hf_zvt_tlv_total_len, &seq_max_len);
732 if (total_len_bytes > 0)
733 offset += total_len_bytes;
735 seq_len = dissect_zvt_tlv_seq(
736 tvb, offset, seq_max_len, pinfo, tree, NULL);
737 if (seq_len > 0)
738 offset += seq_len;
740 return offset - offset_start;
744 static inline int dissect_zvt_res_code(
745 tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
747 proto_tree_add_item(tree, hf_zvt_res_code, tvb, offset, 1, ENC_BIG_ENDIAN);
748 return 1;
752 static inline int dissect_zvt_cc(
753 tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
755 proto_tree_add_item(tree, hf_zvt_cc, tvb, offset, 2, ENC_BIG_ENDIAN);
756 return 2;
760 static inline int dissect_zvt_card_type(
761 tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
763 proto_tree_add_item(tree, hf_zvt_card_type, tvb, offset, 1, ENC_BIG_ENDIAN);
764 return 1;
768 static inline int dissect_zvt_terminal_id(
769 tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
771 proto_tree_add_item(tree, hf_zvt_terminal_id, tvb, offset, 4,
772 ENC_BCD_DIGITS_0_9 | ENC_BIG_ENDIAN);
773 return 4;
777 static inline int dissect_zvt_amount(
778 tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
780 const char *str = tvb_bcd_dig_to_str_be(pinfo->pool, tvb, offset, 6, NULL, false);
781 proto_tree_add_uint64(tree, hf_zvt_amount, tvb, offset, 6, g_ascii_strtoll(str,NULL,10));
782 return 6;
786 static inline int dissect_zvt_time(
787 tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
789 const char *str = tvb_bcd_dig_to_str_be(pinfo->pool, tvb, offset, 3, NULL, false);
790 char *fstr = (char *)wmem_alloc(pinfo->pool, 9);
791 fstr[0] = str[0];
792 fstr[1] = str[1];
793 fstr[2] = ':';
794 fstr[3] = str[2];
795 fstr[4] = str[3];
796 fstr[5] = ':';
797 fstr[6] = str[4];
798 fstr[7] = str[5];
799 fstr[8] = 0;
800 proto_tree_add_string(tree, hf_zvt_time, tvb, offset, 3, fstr);
801 return 3;
805 static inline int dissect_zvt_date(
806 tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
808 const char *str = tvb_bcd_dig_to_str_be(pinfo->pool, tvb, offset, 2, NULL, false);
809 char *fstr = (char *)wmem_alloc(pinfo->pool, 6);
810 fstr[0] = str[0];
811 fstr[1] = str[1];
812 fstr[2] = '/';
813 fstr[3] = str[2];
814 fstr[4] = str[3];
815 fstr[5] = 0;
816 proto_tree_add_string(tree, hf_zvt_date, tvb, offset, 2, fstr);
817 return 2;
821 static inline int dissect_zvt_expiry_date(
822 tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
824 const char *str = tvb_bcd_dig_to_str_be(pinfo->pool, tvb, offset, 2, NULL, false);
825 char *fstr = (char *)wmem_alloc(pinfo->pool, 6);
826 fstr[0] = str[0];
827 fstr[1] = str[1];
828 fstr[2] = '/';
829 fstr[3] = str[2];
830 fstr[4] = str[3];
831 fstr[5] = 0;
832 proto_tree_add_string(tree, hf_zvt_expiry_date, tvb, offset, 2, fstr);
833 return 2;
837 static inline int dissect_zvt_trace_number(
838 tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
840 proto_tree_add_item(tree, hf_zvt_trace_number, tvb, offset, 3,
841 ENC_BCD_DIGITS_0_9 | ENC_BIG_ENDIAN);
842 return 3;
846 static inline int dissect_zvt_card_number(
847 tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
849 uint8_t tens = tvb_get_uint8(tvb, offset) & 0x0f;
850 uint8_t ones = tvb_get_uint8(tvb, offset + 1) & 0x0f;
851 uint8_t length = tens * 10 + ones;
853 proto_tree_add_item(tree, hf_zvt_card_number, tvb, offset + 2, length,
854 ENC_BCD_DIGITS_0_9 | ENC_BIG_ENDIAN);
855 return 2 + length;
859 static inline int dissect_zvt_card_name(
860 tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
862 uint8_t tens = tvb_get_uint8(tvb, offset) & 0x0f;
863 uint8_t ones = tvb_get_uint8(tvb, offset + 1) & 0x0f;
864 uint8_t length = tens * 10 + ones;
865 const uint8_t * str = NULL;
866 proto_tree_add_item_ret_string(tree, hf_zvt_card_name, tvb, offset + 2, length, ENC_ASCII, pinfo->pool, &str);
867 return 2 + length;
871 static inline int dissect_zvt_additional_data(
872 tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
874 uint8_t hundrets = tvb_get_uint8(tvb, offset) & 0x0f;
875 uint8_t tens = tvb_get_uint8(tvb, offset + 1) & 0x0f;
876 uint8_t ones = tvb_get_uint8(tvb, offset + 2) & 0x0f;
877 uint16_t length = hundrets * 100 + tens * 10 + ones;
878 const uint8_t * str = NULL;
879 proto_tree_add_item_ret_string(tree, hf_zvt_additional_data, tvb, offset + 3, length, ENC_ASCII, pinfo->pool, &str);
880 return 3 + length;
884 /* dissect one "bitmap", i.e BMP and the corresponding data */
885 static int
886 dissect_zvt_bitmap(tvbuff_t *tvb, int offset,
887 packet_info *pinfo, proto_tree *tree)
889 int offset_start;
890 uint8_t bmp;
891 proto_item *bitmap_it;
892 proto_tree *bitmap_tree;
893 bitmap_info_t *bi;
894 int ret;
896 offset_start = offset;
898 bmp = tvb_get_uint8(tvb, offset);
899 if (try_val_to_str(bmp, bitmap) == NULL)
900 return -1;
902 bitmap_tree = proto_tree_add_subtree(tree,
903 tvb, offset, -1, ett_zvt_bitmap, &bitmap_it, "Bitmap");
905 proto_tree_add_item(bitmap_tree, hf_zvt_bmp,
906 tvb, offset, 1, ENC_BIG_ENDIAN);
907 proto_item_append_text(bitmap_it, ": %s",
908 val_to_str_const(bmp, bitmap, "unknown"));
909 offset++;
911 bi = (bitmap_info_t *)g_hash_table_lookup(
912 bitmap_table, GUINT_TO_POINTER((unsigned)bmp));
913 if (bi) {
914 if (bi->dissect_payload) {
915 ret = bi->dissect_payload(tvb, offset, pinfo, bitmap_tree);
916 if (ret >= 0)
917 offset += ret;
919 else if (bi->payload_len != BMP_PLD_LEN_UNKNOWN)
920 offset += bi->payload_len;
923 proto_item_set_len(bitmap_it, offset - offset_start);
924 return offset - offset_start;
928 static void dissect_zvt_int_status(tvbuff_t *tvb, int offset, uint16_t len,
929 packet_info *pinfo, proto_tree *tree, zvt_transaction_t *zvt_trans)
931 proto_tree_add_item(tree, hf_zvt_int_status,
932 tvb, offset, 1, ENC_BIG_ENDIAN);
933 offset++;
935 if (len > 1)
936 offset++; /* skip "timeout" */
938 if (len > 2)
939 dissect_zvt_bitmap_seq(tvb, offset, len-2, pinfo, tree, zvt_trans);
943 static void
944 dissect_zvt_reg(tvbuff_t *tvb, int offset, uint16_t len _U_,
945 packet_info *pinfo, proto_tree *tree, zvt_transaction_t *zvt_trans)
947 proto_tree_add_item(tree, hf_zvt_pwd, tvb, offset, 3, ENC_NA);
948 offset += 3;
950 proto_tree_add_item(tree, hf_zvt_reg_cfg,
951 tvb, offset, 1, ENC_BIG_ENDIAN);
952 offset++;
954 /* check for the optional part CC|0x03|service byte|TLV */
955 if (tvb_captured_length_remaining(tvb, offset)>=2) {
956 offset += dissect_zvt_cc(tvb, offset, pinfo, tree);
959 /* it's ok if the remaining len is 0 */
960 dissect_zvt_bitmap_seq(tvb, offset,
961 tvb_captured_length_remaining(tvb, offset),
962 pinfo, tree, zvt_trans);
966 static void dissect_zvt_init(
967 tvbuff_t *tvb, int offset, uint16_t len _U_, packet_info *pinfo _U_,
968 proto_tree *tree, zvt_transaction_t *zvt_trans _U_)
970 proto_tree_add_item(tree, hf_zvt_pwd, tvb, offset, 3, ENC_NA);
974 static void
975 dissect_zvt_abort(tvbuff_t *tvb, int offset, uint16_t len _U_,
976 packet_info *pinfo, proto_tree *tree, zvt_transaction_t *zvt_trans)
978 proto_tree_add_item(tree, hf_zvt_res_code, tvb, offset, 1, ENC_NA);
979 offset += 1;
981 dissect_zvt_bitmap_seq(tvb, offset,
982 tvb_captured_length_remaining(tvb, offset),
983 pinfo, tree, zvt_trans);
987 static void
988 dissect_zvt_pass_bitmap_seq(tvbuff_t *tvb, int offset, uint16_t len _U_,
989 packet_info *pinfo, proto_tree *tree, zvt_transaction_t *zvt_trans)
991 proto_tree_add_item(tree, hf_zvt_pwd, tvb, offset, 3, ENC_NA);
992 offset += 3;
994 dissect_zvt_bitmap_seq(tvb, offset,
995 tvb_captured_length_remaining(tvb, offset),
996 pinfo, tree, zvt_trans);
1000 /* dissect a sequence of bitmaps
1001 (which may be the complete APDU payload or a part of it) */
1002 static void
1003 dissect_zvt_bitmap_seq(tvbuff_t *tvb, int offset, uint16_t len,
1004 packet_info *pinfo, proto_tree *tree, zvt_transaction_t *zvt_trans _U_)
1006 int offset_start, ret;
1008 offset_start = offset;
1010 while (offset - offset_start < len) {
1011 ret = dissect_zvt_bitmap(tvb, offset, pinfo, tree);
1012 if (ret <=0)
1013 break;
1014 offset += ret;
1019 static void
1020 zvt_set_addresses(packet_info *pinfo, zvt_transaction_t *zvt_trans)
1022 apdu_info_t *ai;
1023 zvt_direction_t dir = DIRECTION_UNKNOWN;
1025 if (!zvt_trans)
1026 return;
1028 ai = (apdu_info_t *)g_hash_table_lookup(
1029 apdu_table, GUINT_TO_POINTER((unsigned)zvt_trans->ctrl));
1030 if (!ai)
1031 return;
1033 if (zvt_trans->rqst_frame == pinfo->num) {
1034 dir = ai->direction;
1036 else if (zvt_trans->resp_frame == pinfo->num) {
1037 if (ai->direction == DIRECTION_ECR_TO_PT)
1038 dir = DIRECTION_PT_TO_ECR;
1039 else
1040 dir = DIRECTION_ECR_TO_PT;
1043 if (dir == DIRECTION_ECR_TO_PT) {
1044 set_address(&pinfo->src, AT_STRINGZ,
1045 (int)strlen(ADDR_ECR)+1, ADDR_ECR);
1046 set_address(&pinfo->dst, AT_STRINGZ,
1047 (int)strlen(ADDR_PT)+1, ADDR_PT);
1049 else if (dir == DIRECTION_PT_TO_ECR) {
1050 set_address(&pinfo->src, AT_STRINGZ,
1051 (int)strlen(ADDR_PT)+1, ADDR_PT);
1052 set_address(&pinfo->dst, AT_STRINGZ,
1053 (int)strlen(ADDR_ECR)+1, ADDR_ECR);
1058 /* dissect a ZVT APDU
1059 return -1 if we don't have a complete APDU, 0 if the packet is no ZVT APDU
1060 or the length of the ZVT APDU if all goes well */
1061 static int
1062 dissect_zvt_apdu(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
1064 int offset_start;
1065 uint8_t len_bytes = 1; /* number of bytes for the len field */
1066 uint16_t ctrl = ZVT_CTRL_NONE;
1067 uint16_t len;
1068 uint8_t byte;
1069 proto_item *apdu_it;
1070 proto_tree *apdu_tree;
1071 apdu_info_t *ai;
1072 zvt_transaction_t *zvt_trans = NULL;
1073 proto_item *it;
1075 offset_start = offset;
1077 if (tvb_captured_length_remaining(tvb, offset) < ZVT_APDU_MIN_LEN)
1078 return -1;
1080 len = tvb_get_uint8(tvb, offset+2);
1081 if (len == 0xFF) {
1082 len_bytes = 3;
1083 len = tvb_get_letohs(tvb, offset+3);
1086 /* ZVT_APDU_MIN_LEN already includes one length byte */
1087 if (tvb_captured_length_remaining(tvb, offset) <
1088 ZVT_APDU_MIN_LEN + (len_bytes-1) + len) {
1089 return -1;
1092 apdu_tree = proto_tree_add_subtree(tree,
1093 tvb, offset, -1, ett_zvt_apdu, &apdu_it, "ZVT APDU");
1095 byte = tvb_get_uint8(tvb, offset);
1096 if (byte == CCRC_POS || byte == CCRC_NEG) {
1097 proto_tree_add_item(apdu_tree, hf_zvt_ccrc, tvb, offset, 1, ENC_BIG_ENDIAN);
1098 col_append_sep_str(pinfo->cinfo, COL_INFO, NULL,
1099 byte == CCRC_POS ? "Positive completion" : "Negative completion");
1100 offset++;
1101 proto_tree_add_item(apdu_tree, hf_zvt_aprc, tvb, offset, 1, ENC_BIG_ENDIAN);
1102 offset++;
1104 zvt_trans = (zvt_transaction_t *)wmem_tree_lookup32_le(
1105 transactions, pinfo->num);
1106 if (zvt_trans && zvt_trans->resp_frame==0) {
1107 /* there's a pending request, this packet is the response */
1108 zvt_trans->resp_frame = pinfo->num;
1111 if (zvt_trans && zvt_trans->resp_frame == pinfo->num) {
1112 it = proto_tree_add_uint(apdu_tree, hf_zvt_resp_to,
1113 NULL, 0, 0, zvt_trans->rqst_frame);
1114 proto_item_set_generated(it);
1117 else {
1118 ctrl = tvb_get_ntohs(tvb, offset);
1119 proto_tree_add_item(apdu_tree, hf_zvt_ctrl, tvb, offset, 2, ENC_BIG_ENDIAN);
1120 col_append_sep_str(pinfo->cinfo, COL_INFO, NULL,
1121 val_to_str(ctrl, ctrl_field, "Unknown 0x%x"));
1122 offset += 2;
1124 if (PINFO_FD_VISITED(pinfo)) {
1125 zvt_trans = (zvt_transaction_t *)wmem_tree_lookup32(
1126 transactions, pinfo->num);
1127 if (zvt_trans && zvt_trans->rqst_frame==pinfo->num &&
1128 zvt_trans->resp_frame!=0) {
1129 it = proto_tree_add_uint(apdu_tree, hf_zvt_resp_in,
1130 NULL, 0, 0, zvt_trans->resp_frame);
1131 proto_item_set_generated(it);
1134 else {
1135 zvt_trans = wmem_new(wmem_file_scope(), zvt_transaction_t);
1136 zvt_trans->rqst_frame = pinfo->num;
1137 zvt_trans->resp_frame = 0;
1138 zvt_trans->ctrl = ctrl;
1139 wmem_tree_insert32(transactions,
1140 zvt_trans->rqst_frame, (void *)zvt_trans);
1144 ai = (apdu_info_t *)g_hash_table_lookup(
1145 apdu_table, GUINT_TO_POINTER((unsigned)ctrl));
1147 it = proto_tree_add_uint(apdu_tree, hf_zvt_len, tvb, offset, len_bytes, len);
1148 if (ai && ai->min_len_field!=LEN_FIELD_ANY && len<ai->min_len_field) {
1149 expert_add_info_format(pinfo, it, &ei_invalid_apdu_len,
1150 "The APDU length is too short. The minimum length is %d",
1151 ai->min_len_field);
1153 offset += len_bytes;
1155 zvt_set_addresses(pinfo, zvt_trans);
1157 if (len > 0) {
1158 if (ai && ai->dissect_payload)
1159 ai->dissect_payload(tvb, offset, len, pinfo, apdu_tree, zvt_trans);
1160 else
1161 proto_tree_add_item(apdu_tree, hf_zvt_data,
1162 tvb, offset, len, ENC_NA);
1164 offset += len;
1166 proto_item_set_len(apdu_it, offset - offset_start);
1167 return offset - offset_start;
1171 static int
1172 dissect_zvt_serial(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
1174 int offset_start;
1175 int apdu_len;
1177 offset_start = offset;
1179 if (tvb_reported_length_remaining(tvb, offset) == 1) {
1180 proto_tree_add_item(tree, hf_zvt_serial_char,
1181 tvb, offset, 1, ENC_BIG_ENDIAN);
1182 offset++; /* ACK or NAK byte */
1183 return offset - offset_start;
1186 proto_tree_add_item(tree, hf_zvt_serial_char,
1187 tvb, offset, 1, ENC_BIG_ENDIAN);
1188 offset ++; /* DLE byte */
1189 proto_tree_add_item(tree, hf_zvt_serial_char,
1190 tvb, offset, 1, ENC_BIG_ENDIAN);
1191 offset ++; /* STX byte */
1193 apdu_len = dissect_zvt_apdu(tvb, offset, pinfo, tree);
1194 if (apdu_len < 0)
1195 return apdu_len;
1197 offset += apdu_len;
1199 proto_tree_add_item(tree, hf_zvt_serial_char,
1200 tvb, offset, 1, ENC_BIG_ENDIAN);
1201 offset ++; /* DLE byte */
1202 proto_tree_add_item(tree, hf_zvt_serial_char,
1203 tvb, offset, 1, ENC_BIG_ENDIAN);
1204 offset ++; /* ETX byte */
1206 /* the CRC is little endian, the other fields are big endian */
1207 proto_tree_add_item(tree, hf_zvt_crc,
1208 tvb, offset, 2, ENC_LITTLE_ENDIAN);
1209 offset += 2; /* CRC bytes */
1211 return offset - offset_start;
1215 static bool
1216 valid_ctrl_field(tvbuff_t *tvb, int offset)
1218 if (tvb_get_uint8(tvb, offset) == 0x80 ||
1219 tvb_get_uint8(tvb, offset) == 0x84 ||
1220 try_val_to_str_ext(tvb_get_ntohs(tvb, offset), &ctrl_field_ext)) {
1221 return true;
1224 return false;
1228 static int
1229 dissect_zvt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
1231 int zvt_len = 0;
1232 proto_item *zvt_ti;
1233 proto_tree *zvt_tree;
1234 bool is_serial; /* serial or TCP/IP protocol? */
1236 if (tvb_captured_length(tvb) == 1 &&
1237 (tvb_get_uint8(tvb, 0) == ACK ||
1238 tvb_get_uint8(tvb, 0) == NAK)) {
1239 is_serial = true;
1241 else if (tvb_captured_length(tvb) >= 2 &&
1242 tvb_get_uint8(tvb, 0) == DLE &&
1243 tvb_get_uint8(tvb, 1) == STX) {
1244 is_serial = true;
1246 else if (tvb_captured_length(tvb) >= ZVT_APDU_MIN_LEN &&
1247 valid_ctrl_field(tvb, 0)) {
1248 is_serial = false;
1250 else
1251 return 0;
1253 col_set_str(pinfo->cinfo, COL_PROTOCOL, "ZVT");
1254 col_clear(pinfo->cinfo, COL_INFO);
1255 zvt_ti = proto_tree_add_protocol_format(tree, proto_zvt,
1256 tvb, 0, -1,
1257 "ZVT Kassenschnittstelle: %s", is_serial ?
1258 "Serial Transport Protocol" : "Transport Protocol TCP/IP");
1259 zvt_tree = proto_item_add_subtree(zvt_ti, ett_zvt);
1261 if (is_serial)
1262 zvt_len = dissect_zvt_serial(tvb, 0, pinfo, zvt_tree);
1263 else
1264 zvt_len = dissect_zvt_apdu(tvb, 0, pinfo, zvt_tree);
1266 /* zvt_len < 0 means that we have an incomplete APDU
1267 we can't do any reassembly here, so let's consume all bytes */
1268 if (zvt_len < 0)
1269 zvt_len = tvb_captured_length(tvb);
1271 proto_item_set_len(zvt_ti, zvt_len);
1272 return zvt_len;
1275 static unsigned get_zvt_message_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset, void *data _U_)
1277 unsigned len = tvb_get_uint8(tvb, offset+2);
1278 if (len == 0xFF)
1279 if (tvb_captured_length_remaining(tvb, offset) >= 5)
1280 len = tvb_get_letohs(tvb, offset+3) + 5;
1281 else
1282 len = 0;
1283 else
1284 len += 3;
1286 return len;
1289 static int
1290 dissect_zvt_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
1292 tcp_dissect_pdus(tvb, pinfo, tree, true, ZVT_APDU_MIN_LEN,
1293 get_zvt_message_len, dissect_zvt, data);
1294 return tvb_captured_length(tvb);
1297 static void
1298 zvt_shutdown(void)
1300 g_hash_table_destroy(tlv_table);
1301 g_hash_table_destroy(apdu_table);
1302 g_hash_table_destroy(bitmap_table);
1305 void
1306 proto_register_zvt(void)
1308 unsigned i;
1309 expert_module_t* expert_zvt;
1311 static int *ett[] = {
1312 &ett_zvt,
1313 &ett_zvt_apdu,
1314 &ett_zvt_bitmap,
1315 &ett_zvt_tlv_dat_obj,
1316 &ett_zvt_tlv_subseq,
1317 &ett_zvt_tlv_tag,
1318 &ett_zvt_tlv_receipt
1320 static hf_register_info hf[] = {
1321 { &hf_zvt_resp_in,
1322 { "Response In", "zvt.resp_in",
1323 FT_FRAMENUM, BASE_NONE, FRAMENUM_TYPE(FT_FRAMENUM_RESPONSE), 0x0, NULL, HFILL } },
1324 { &hf_zvt_resp_to,
1325 { "Response To", "zvt.resp_to",
1326 FT_FRAMENUM, BASE_NONE, FRAMENUM_TYPE(FT_FRAMENUM_REQUEST), 0x0, NULL, HFILL } },
1327 { &hf_zvt_serial_char,
1328 { "Serial character", "zvt.serial_char", FT_UINT8,
1329 BASE_HEX|BASE_EXT_STRING, &serial_char_ext, 0, NULL, HFILL } },
1330 { &hf_zvt_crc,
1331 { "CRC", "zvt.crc", FT_UINT16,
1332 BASE_HEX, NULL, 0, NULL, HFILL } },
1333 { &hf_zvt_ctrl,
1334 { "Control-field", "zvt.control_field", FT_UINT16,
1335 BASE_HEX|BASE_EXT_STRING, &ctrl_field_ext, 0, NULL, HFILL } },
1336 { &hf_zvt_ccrc,
1337 { "CCRC", "zvt.ccrc",
1338 FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL } },
1339 { &hf_zvt_aprc,
1340 { "APRC", "zvt.aprc",
1341 FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL } },
1342 { &hf_zvt_len,
1343 { "Length-field", "zvt.length_field",
1344 FT_UINT16, BASE_DEC, NULL, 0, NULL, HFILL } },
1345 { &hf_zvt_data,
1346 { "APDU data", "zvt.data",
1347 FT_BYTES, BASE_NONE, NULL, 0, NULL, HFILL } },
1348 { &hf_zvt_int_status,
1349 { "Intermediate status", "zvt.int_status",
1350 FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL } },
1351 { &hf_zvt_pwd,
1352 { "Password", "zvt.password",
1353 FT_BYTES, BASE_NONE, NULL, 0, NULL, HFILL } },
1354 { &hf_zvt_reg_cfg,
1355 { "Config byte", "zvt.reg.config_byte",
1356 FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL } },
1357 { &hf_zvt_res_code,
1358 { "Result Code", "zvt.result_code",
1359 FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL } },
1360 /* we don't call the filter zvt.reg.cc, the currency code
1361 appears in several apdus */
1362 { &hf_zvt_cc,
1363 { "Currency Code", "zvt.cc",
1364 FT_UINT16, BASE_HEX, VALS(zvt_cc), 0, NULL, HFILL } },
1365 { &hf_zvt_card_type,
1366 { "Card Type", "zvt.card_type", FT_UINT8,
1367 BASE_DEC|BASE_EXT_STRING, &card_type_ext, 0, NULL, HFILL } },
1368 { &hf_zvt_terminal_id,
1369 { "Terminal ID", "zvt.terminal_id", FT_STRING,
1370 BASE_NONE, NULL, 0, NULL, HFILL } },
1371 { &hf_zvt_amount,
1372 { "Amount", "zvt.amount", FT_UINT48,
1373 BASE_DEC, NULL, 0, NULL, HFILL } },
1374 { &hf_zvt_time,
1375 { "Time", "zvt.time", FT_STRING,
1376 BASE_NONE, NULL, 0, NULL, HFILL } },
1377 { &hf_zvt_date,
1378 { "Date", "zvt.date", FT_STRING,
1379 BASE_NONE, NULL, 0, NULL, HFILL } },
1380 { &hf_zvt_bmp,
1381 { "BMP", "zvt.bmp", FT_UINT8,
1382 BASE_HEX|BASE_EXT_STRING, &bitmap_ext, 0, NULL, HFILL } },
1383 { &hf_zvt_tlv_total_len,
1384 { "Total length", "zvt.tlv.total_len",
1385 FT_UINT32, BASE_DEC, NULL, 0, NULL, HFILL } },
1386 { &hf_zvt_tlv_tag,
1387 { "Tag", "zvt.tlv.tag", FT_UINT32,
1388 BASE_HEX|BASE_EXT_STRING, &tlv_tags_ext, 0, NULL, HFILL } },
1389 { &hf_zvt_tlv_tag_class,
1390 { "Class", "zvt.tlv.tag.class", FT_UINT8,
1391 BASE_HEX|BASE_EXT_STRING, &tlv_tag_class_ext,
1392 0xC0, NULL, HFILL } },
1393 { &hf_zvt_tlv_tag_type,
1394 { "Type", "zvt.tlv.tag.type", FT_BOOLEAN,
1395 8, TFS(&tfs_constructed_primitive), 0x20, NULL, HFILL } },
1396 { &hf_zvt_tlv_len,
1397 { "Length", "zvt.tlv.len",
1398 FT_UINT32, BASE_DEC, NULL, 0, NULL, HFILL } },
1399 { &hf_zvt_text_lines_line,
1400 { "Text line", "zvt.tlv.text_lines.line",
1401 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
1402 { &hf_zvt_permitted_cmd,
1403 { "Permitted command", "zvt.tlv.permitted_command",
1404 FT_UINT16, BASE_HEX, NULL, 0, NULL, HFILL } },
1405 { &hf_zvt_receipt_type,
1406 { "Receipt type", "zvt.tlv.receipt_type",
1407 FT_UINT16, BASE_HEX, VALS(receipt_type), 0, NULL, HFILL } },
1408 { &hf_zvt_receipt_parameter_positive_customer,
1409 { "Positive customer receipt", "zvt.tlv.receipt_parameter.positive_customer", FT_BOOLEAN,
1410 8, TFS(&tfs_required_not_required), 0x80, NULL, HFILL } },
1411 { &hf_zvt_receipt_parameter_negative_customer,
1412 { "Negative customer receipt", "zvt.tlv.receipt_parameter.negative_customer", FT_BOOLEAN,
1413 8, TFS(&tfs_required_not_required), 0x40, NULL, HFILL } },
1414 { &hf_zvt_receipt_parameter_positive_merchant,
1415 { "Positive merchant receipt", "zvt.tlv.receipt_parameter.positive_merchant", FT_BOOLEAN,
1416 8, TFS(&tfs_required_not_required), 0x20, NULL, HFILL } },
1417 { &hf_zvt_receipt_parameter_negative_merchant,
1418 { "Negative merchant receipt", "zvt.tlv.receipt_parameter.negative_merchant", FT_BOOLEAN,
1419 8, TFS(&tfs_required_not_required), 0x10, NULL, HFILL } },
1420 { &hf_zvt_receipt_parameter_customer_before_merchant,
1421 { "Customer receipt should be sent before the merchant receipt", "zvt.tlv.receipt_parameter.customer_first", FT_BOOLEAN,
1422 8, TFS(&tfs_yes_no), 0x08, NULL, HFILL } },
1423 { &hf_zvt_receipt_parameter_print_short_receipt,
1424 { "Print short receipt", "zvt.tlv.receipt_parameter.short_receipt", FT_BOOLEAN,
1425 8, TFS(&tfs_yes_no), 0x04, NULL, HFILL } },
1426 { &hf_zvt_receipt_parameter_no_product_data,
1427 { "Do not print product data (from BMP 3C) on the receipt", "zvt.tlv.receipt_parameter.no_product", FT_BOOLEAN,
1428 8, TFS(&tfs_yes_no), 0x02, NULL, HFILL } },
1429 { &hf_zvt_receipt_parameter_ecr_as_printer,
1430 { "Use ECR as printer", "zvt.tlv.receipt_parameter.ecr_as_printer", FT_BOOLEAN,
1431 8, TFS(&tfs_yes_no), 0x01, NULL, HFILL } },
1432 { &hf_zvt_receipt_parameter,
1433 { "Receipt parameter", "zvt.tlv.receipt_parameter", FT_UINT8,
1434 BASE_HEX, NULL, 0x00, NULL, HFILL } },
1435 { &hf_zvt_trace_number,
1436 { "Trace number", "zvt.trace_number", FT_STRING,
1437 BASE_NONE, NULL, 0, NULL, HFILL } },
1438 { &hf_zvt_expiry_date,
1439 { "Expiry date", "zvt.expiry_date", FT_STRING,
1440 BASE_NONE, NULL, 0, NULL, HFILL } },
1441 { &hf_zvt_card_number,
1442 { "Card number", "zvt.card_number", FT_STRING,
1443 BASE_NONE, NULL, 0, NULL, HFILL } },
1444 { &hf_zvt_card_name,
1445 { "Card name", "zvt.card_name", FT_STRING,
1446 BASE_NONE, NULL, 0, NULL, HFILL } },
1447 { &hf_zvt_additional_data,
1448 { "Additional data", "zvt.additional_data", FT_STRING,
1449 BASE_NONE, NULL, 0, NULL, HFILL } },
1450 { &hf_zvt_characters_per_line,
1451 { "Characters per line", "zvt.characters_per_line", FT_STRING,
1452 BASE_NONE, NULL, 0, NULL, HFILL } },
1453 { &hf_zvt_receipt_info,
1454 { "Receipt information", "zvt.tlv.receipt_info", FT_UINT8,
1455 BASE_HEX, NULL, 0x00, NULL, HFILL } },
1456 { &hf_zvt_receipt_info_positive,
1457 { "Positive receipt (authorised)",
1458 "zvt.tlv.receipt_info.positive", FT_BOOLEAN, 8,
1459 TFS(&tfs_yes_no), 0x01, NULL, HFILL } },
1460 { &hf_zvt_receipt_info_signature,
1461 { "Receipt contains a signature",
1462 "zvt.tlv.receipt_info.signature", FT_BOOLEAN, 8,
1463 TFS(&tfs_yes_no), 0x02, NULL, HFILL } },
1464 { &hf_zvt_receipt_info_negative,
1465 { "Negative receipt (aborted, rejected)",
1466 "zvt.tlv.receipt_info.negative", FT_BOOLEAN, 8,
1467 TFS(&tfs_yes_no), 0x04, NULL, HFILL } },
1468 { &hf_zvt_receipt_info_printing,
1469 { "Printing is mandatory", "zvt.tlv.receipt_info.printing",
1470 FT_BOOLEAN, 8, TFS(&tfs_yes_no), 0x80, NULL, HFILL } }
1473 static ei_register_info ei[] = {
1474 { &ei_invalid_apdu_len,
1475 { "zvt.apdu_len.invalid", PI_PROTOCOL, PI_WARN,
1476 "The APDU length is too short",
1477 EXPFILL }}
1480 apdu_table = g_hash_table_new(g_direct_hash, g_direct_equal);
1481 for(i=0; i<array_length(apdu_info); i++) {
1482 g_hash_table_insert(apdu_table,
1483 GUINT_TO_POINTER((unsigned)apdu_info[i].ctrl),
1484 (void *)(&apdu_info[i]));
1487 bitmap_table = g_hash_table_new(g_direct_hash, g_direct_equal);
1488 for(i=0; i<array_length(bitmap_info); i++) {
1489 g_hash_table_insert(bitmap_table,
1490 GUINT_TO_POINTER((unsigned)bitmap_info[i].bmp),
1491 (void *)(&bitmap_info[i]));
1494 tlv_table = g_hash_table_new(g_direct_hash, g_direct_equal);
1495 for(i=0; i<array_length(tlv_info); i++) {
1496 g_hash_table_insert(tlv_table,
1497 GUINT_TO_POINTER((unsigned)tlv_info[i].tag),
1498 (void *)(&tlv_info[i]));
1501 proto_zvt = proto_register_protocol("ZVT Kassenschnittstelle", "ZVT", "zvt");
1503 proto_register_field_array(proto_zvt, hf, array_length(hf));
1504 proto_register_subtree_array(ett, array_length(ett));
1505 expert_zvt = expert_register_protocol(proto_zvt);
1506 expert_register_field_array(expert_zvt, ei, array_length(ei));
1508 transactions = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope());
1510 /* register by name to allow mapping to a user DLT */
1511 register_dissector("zvt", dissect_zvt, proto_zvt);
1512 zvt_tcp_handle = register_dissector("zvt.tcp", dissect_zvt_tcp, proto_zvt);
1514 register_shutdown_routine(zvt_shutdown);
1518 void
1519 proto_reg_handoff_zvt(void)
1521 dissector_add_for_decode_as_with_preference("tcp.port", zvt_tcp_handle);
1526 * Editor modelines - https://www.wireshark.org/tools/modelines.html
1528 * Local variables:
1529 * c-basic-offset: 4
1530 * tab-width: 8
1531 * indent-tabs-mode: nil
1532 * End:
1534 * vi: set shiftwidth=4 tabstop=8 expandtab:
1535 * :indentSize=4:tabSize=8:noTabs=true: