Revert "LATER... ei_kerberos_kdc_session_key ..."
[wireshark-sm.git] / wiretap / mplog.c
blob2be8596a0490a696595eda5ec0f887136221889f
1 /* mplog.c
3 * File format support for Micropross mplog files
4 * Copyright (c) 2016 by Martin Kaiser <martin@kaiser.cx>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
15 The mplog file format logs the communication between a contactless
16 smartcard and a card reader. Such files contain information about the
17 physical layer as well as the bytes exchanged between devices.
18 Some commercial logging and testing tools by the French company Micropross
19 use this format.
21 The information used for implementing this wiretap module were
22 obtained from reverse-engineering. There is no publicly available
23 documentation of the mplog file format.
25 Mplog files start with the string "MPCSII". This string is part of
26 the header which is in total 0x80 bytes long.
28 Following the header, the file is a sequence of 8 byte-blocks.
29 data (one byte)
30 block type (one byte)
31 timestamp (six bytes)
33 The timestamp is a counter in little-endian format. The counter is in
34 units of 10ns.
37 #include "config.h"
38 #include "mplog.h"
40 #include <string.h>
41 #include <wtap-int.h>
42 #include <file_wrappers.h>
44 /* the block types */
45 #define TYPE_PCD_PICC_A 0x70
46 #define TYPE_PICC_PCD_A 0x71
47 #define TYPE_PCD_PICC_B 0x72
48 #define TYPE_PICC_PCD_B 0x73
49 #define TYPE_UNKNOWN 0xFF
51 #define KNOWN_TYPE(x) \
52 ( \
53 ((x) == TYPE_PCD_PICC_A) || \
54 ((x) == TYPE_PICC_PCD_A) || \
55 ((x) == TYPE_PCD_PICC_B) || \
56 ((x) == TYPE_PICC_PCD_B) \
59 #define MPLOG_BLOCK_SIZE 8
61 /* ISO14443 pseudo-header, see https://www.kaiser.cx/pcap-iso14443.html */
62 #define ISO14443_PSEUDO_HDR_VER 0
63 #define ISO14443_PSEUDO_HDR_LEN 4
64 /* the two transfer events are the types that include a trailing CRC
65 the CRC is always present in mplog files */
66 #define ISO14443_PSEUDO_HDR_PICC_TO_PCD 0xFF
67 #define ISO14443_PSEUDO_HDR_PCD_TO_PICC 0xFE
70 #define ISO14443_MAX_PKT_LEN 4096
72 #define PKT_BUF_LEN (ISO14443_PSEUDO_HDR_LEN + ISO14443_MAX_PKT_LEN)
75 static int mplog_file_type_subtype = -1;
77 void register_mplog(void);
79 /* read the next packet, starting at the current position of fh
80 as we know very little about the file format, our approach is rather simple:
81 - we read block-by-block until a known block-type is found
82 - this block's type is the type of the next packet
83 - this block's timestamp will become the packet's timestamp
84 - the data byte will be our packet's first byte
85 - we carry on reading blocks and add the data bytes
86 of all blocks of "our" type
87 - if a different well-known block type is found, this is the end of
88 our packet, we go back one block so that this block can be picked
89 up as the start of the next packet
90 - if two blocks of our packet's block type are more than 200us apart,
91 we treat this as a packet boundary as described above
93 static bool mplog_read_packet(FILE_T fh, wtap_rec *rec,
94 Buffer *buf, int *err, char **err_info)
96 uint8_t *p, *start_p;
97 /* --- the last block of a known type --- */
98 uint64_t last_ctr = 0;
99 /* --- the current block --- */
100 uint8_t block[MPLOG_BLOCK_SIZE]; /* the entire block */
101 uint8_t data, type; /* its data and block type bytes */
102 uint64_t ctr; /* its timestamp counter */
103 /* --- the packet we're assembling --- */
104 int pkt_bytes = 0;
105 uint8_t pkt_type = TYPE_UNKNOWN;
106 /* the timestamp of the packet's first block,
107 this will become the packet's timestamp */
108 uint64_t pkt_ctr = 0;
111 ws_buffer_assure_space(buf, PKT_BUF_LEN);
112 p = ws_buffer_start_ptr(buf);
113 start_p = p;
115 /* leave space for the iso14443 pseudo header
116 we can't create it until we've seen the entire packet */
117 p += ISO14443_PSEUDO_HDR_LEN;
119 do {
120 if (!wtap_read_bytes_or_eof(fh, block, sizeof(block), err, err_info)) {
121 /* If we've already read some data, if this failed with an EOF,
122 so that *err is 0, it's a short read. */
123 if (pkt_bytes != 0) {
124 if (*err == 0)
125 *err = WTAP_ERR_SHORT_READ;
127 break;
129 data = block[0];
130 type = block[1];
131 ctr = pletoh48(&block[2]);
133 if (pkt_type == TYPE_UNKNOWN) {
134 if (KNOWN_TYPE(type)) {
135 pkt_type = type;
136 pkt_ctr = ctr;
140 if (type == pkt_type) {
141 if (last_ctr != 0) {
142 /* if the distance to the last byte of the
143 same type is larger than 200us, this is very likely the
144 first byte of a new packet -> go back one block and exit
145 ctr and last_ctr are in units of 10ns
146 at 106kbit/s, it takes approx 75us to send one byte */
147 if (ctr - last_ctr > 200*100) {
148 file_seek(fh, -MPLOG_BLOCK_SIZE, SEEK_CUR, err);
149 break;
153 *p++ = data;
154 pkt_bytes++;
155 last_ctr = ctr;
157 else if (KNOWN_TYPE(type)) {
158 file_seek(fh, -MPLOG_BLOCK_SIZE, SEEK_CUR, err);
159 break;
161 } while (pkt_bytes < ISO14443_MAX_PKT_LEN);
163 if (pkt_type == TYPE_UNKNOWN)
164 return false;
166 start_p[0] = ISO14443_PSEUDO_HDR_VER;
168 if (pkt_type==TYPE_PCD_PICC_A || pkt_type==TYPE_PCD_PICC_B)
169 start_p[1] = ISO14443_PSEUDO_HDR_PCD_TO_PICC;
170 else
171 start_p[1] = ISO14443_PSEUDO_HDR_PICC_TO_PCD;
173 start_p[2] = pkt_bytes >> 8;
174 start_p[3] = pkt_bytes & 0xFF;
176 rec->rec_type = REC_TYPE_PACKET;
177 rec->block = wtap_block_create(WTAP_BLOCK_PACKET);
178 rec->rec_header.packet_header.pkt_encap = WTAP_ENCAP_ISO14443;
179 rec->presence_flags = WTAP_HAS_TS | WTAP_HAS_CAP_LEN;
180 rec->ts.secs = (time_t)((pkt_ctr*10)/(1000*1000*1000));
181 rec->ts.nsecs = (int)((pkt_ctr*10)%(1000*1000*1000));
182 rec->rec_header.packet_header.caplen = ISO14443_PSEUDO_HDR_LEN + pkt_bytes;
183 rec->rec_header.packet_header.len = rec->rec_header.packet_header.caplen;
185 return true;
189 static bool
190 mplog_read(wtap *wth, wtap_rec *rec, Buffer *buf, int *err,
191 char **err_info, int64_t *data_offset)
193 *data_offset = file_tell(wth->fh);
195 return mplog_read_packet(wth->fh, rec, buf, err, err_info);
199 static bool
200 mplog_seek_read(wtap *wth, int64_t seek_off, wtap_rec *rec, Buffer *buf,
201 int *err, char **err_info)
203 if (-1 == file_seek(wth->random_fh, seek_off, SEEK_SET, err))
204 return false;
206 if (!mplog_read_packet(wth->random_fh, rec, buf, err, err_info)) {
207 /* Even if we got an immediate EOF, that's an error. */
208 if (*err == 0)
209 *err = WTAP_ERR_SHORT_READ;
210 return false;
212 return true;
216 wtap_open_return_val mplog_open(wtap *wth, int *err, char **err_info)
218 bool ok;
219 uint8_t magic[6];
221 ok = wtap_read_bytes(wth->fh, magic, 6, err, err_info);
222 if (!ok) {
223 if (*err != WTAP_ERR_SHORT_READ)
224 return WTAP_OPEN_ERROR;
225 return WTAP_OPEN_NOT_MINE;
227 if (memcmp(magic, "MPCSII", 6) != 0)
228 return WTAP_OPEN_NOT_MINE;
230 wth->file_encap = WTAP_ENCAP_ISO14443;
231 wth->snapshot_length = 0;
232 wth->file_tsprec = WTAP_TSPREC_NSEC;
234 wth->priv = NULL;
236 wth->subtype_read = mplog_read;
237 wth->subtype_seek_read = mplog_seek_read;
238 wth->file_type_subtype = mplog_file_type_subtype;
240 /* skip the file header */
241 if (-1 == file_seek(wth->fh, 0x80, SEEK_SET, err))
242 return WTAP_OPEN_ERROR;
244 *err = 0;
247 * Add an IDB; we don't know how many interfaces were
248 * involved, so we just say one interface, about which
249 * we only know the link-layer type, snapshot length,
250 * and time stamp resolution.
252 wtap_add_generated_idb(wth);
254 return WTAP_OPEN_MINE;
257 static const struct supported_block_type mplog_blocks_supported[] = {
259 * We support packet blocks, with no comments or other options.
261 { WTAP_BLOCK_PACKET, MULTIPLE_BLOCKS_SUPPORTED, NO_OPTIONS_SUPPORTED }
264 static const struct file_type_subtype_info mplog_info = {
265 "Micropross mplog", "mplog", "mplog", NULL,
266 false, BLOCKS_SUPPORTED(mplog_blocks_supported),
267 NULL, NULL, NULL
270 void register_mplog(void)
272 mplog_file_type_subtype = wtap_register_file_type_subtype(&mplog_info);
275 * Register name for backwards compatibility with the
276 * wtap_filetypes table in Lua.
278 wtap_register_backwards_compatibility_lua_name("MPLOG",
279 mplog_file_type_subtype);
283 * Editor modelines - https://www.wireshark.org/tools/modelines.html
285 * Local variables:
286 * c-basic-offset: 4
287 * tab-width: 8
288 * indent-tabs-mode: nil
289 * End:
291 * vi: set shiftwidth=4 tabstop=8 expandtab:
292 * :indentSize=4:tabSize=8:noTabs=true: