2006-05-08 19:56:36 +00:00
|
|
|
/* ber.c
|
|
|
|
*
|
|
|
|
* Basic Encoding Rules (BER) file reading
|
|
|
|
*
|
2018-02-07 11:26:45 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2006-05-08 19:56:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "wtap-int.h"
|
|
|
|
#include "file_wrappers.h"
|
2014-07-15 23:40:46 +00:00
|
|
|
#include <wsutil/buffer.h>
|
2006-05-08 19:56:36 +00:00
|
|
|
#include "ber.h"
|
|
|
|
|
|
|
|
|
2015-01-02 00:45:22 +00:00
|
|
|
#define BER_CLASS_UNI 0
|
|
|
|
#define BER_CLASS_APP 1
|
|
|
|
#define BER_CLASS_CON 2
|
2006-05-08 19:56:36 +00:00
|
|
|
|
2015-01-02 00:45:22 +00:00
|
|
|
#define BER_UNI_TAG_SEQ 16 /* SEQUENCE, SEQUENCE OF */
|
|
|
|
#define BER_UNI_TAG_SET 17 /* SET, SET OF */
|
2006-05-08 19:56:36 +00:00
|
|
|
|
2021-02-20 08:13:37 +00:00
|
|
|
static int ber_file_type_subtype = -1;
|
|
|
|
|
|
|
|
void register_ber(void);
|
|
|
|
|
|
|
|
static gboolean ber_full_file_read(wtap *wth, wtap_rec *rec, Buffer *buf,
|
|
|
|
int *err, gchar **err_info,
|
|
|
|
gint64 *data_offset)
|
|
|
|
{
|
|
|
|
if (!wtap_full_file_read(wth, rec, buf, err, err_info, data_offset))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Pass the file name. */
|
|
|
|
rec->rec_header.packet_header.pseudo_header.ber.pathname = wth->pathname;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean ber_full_file_seek_read(wtap *wth, gint64 seek_off,
|
|
|
|
wtap_rec *rec, Buffer *buf,
|
|
|
|
int *err, gchar **err_info)
|
|
|
|
{
|
|
|
|
if (!wtap_full_file_seek_read(wth, seek_off, rec, buf, err, err_info))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Pass the file name. */
|
|
|
|
rec->rec_header.packet_header.pseudo_header.ber.pathname = wth->pathname;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-10-09 23:44:15 +00:00
|
|
|
wtap_open_return_val ber_open(wtap *wth, int *err, gchar **err_info)
|
2006-05-08 19:56:36 +00:00
|
|
|
{
|
2009-06-02 14:36:42 +00:00
|
|
|
#define BER_BYTES_TO_CHECK 8
|
2006-05-08 19:56:36 +00:00
|
|
|
guint8 bytes[BER_BYTES_TO_CHECK];
|
2010-02-23 02:10:54 +00:00
|
|
|
guint8 ber_id;
|
|
|
|
gint8 ber_class;
|
|
|
|
gint8 ber_tag;
|
|
|
|
gboolean ber_pc;
|
2006-05-08 19:56:36 +00:00
|
|
|
guint8 oct, nlb = 0;
|
2006-09-12 21:59:54 +00:00
|
|
|
int len = 0;
|
|
|
|
gint64 file_size;
|
2006-05-08 19:56:36 +00:00
|
|
|
int offset = 0, i;
|
|
|
|
|
Add some higher-level file-read APIs and use them.
Add wtap_read_bytes(), which takes a FILE_T, a pointer, a byte count, an
error number pointer, and an error string pointer as arguments, and that
treats a short read of any sort, including a read that returns 0 bytes,
as a WTAP_ERR_SHORT_READ error, and that returns the error number and
string through its last two arguments.
Add wtap_read_bytes_or_eof(), which is similar, but that treats a read
that returns 0 bytes as an EOF, supplying an error number of 0 as an EOF
indication.
Use those in file readers; that simplifies the code and makes it less
likely that somebody will fail to supply the error number and error
string on a file read error.
Change-Id: Ia5dba2a6f81151e87b614461349d611cffc16210
Reviewed-on: https://code.wireshark.org/review/4512
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2014-10-07 01:00:57 +00:00
|
|
|
if (!wtap_read_bytes(wth->fh, &bytes, BER_BYTES_TO_CHECK, err, err_info)) {
|
|
|
|
if (*err != WTAP_ERR_SHORT_READ)
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_ERROR;
|
|
|
|
return WTAP_OPEN_NOT_MINE;
|
2006-05-08 19:56:36 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 02:10:54 +00:00
|
|
|
ber_id = bytes[offset++];
|
2006-05-08 19:56:36 +00:00
|
|
|
|
2010-02-23 02:10:54 +00:00
|
|
|
ber_class = (ber_id>>6) & 0x03;
|
|
|
|
ber_pc = (ber_id>>5) & 0x01;
|
|
|
|
ber_tag = ber_id & 0x1F;
|
2006-09-12 21:59:54 +00:00
|
|
|
|
2006-05-08 19:56:36 +00:00
|
|
|
/* it must be constructed and either a SET or a SEQUENCE */
|
2015-08-24 13:00:39 +00:00
|
|
|
/* or a CONTEXT/APPLICATION less than 32 (arbitrary) */
|
2010-02-23 02:10:54 +00:00
|
|
|
if(!(ber_pc &&
|
|
|
|
(((ber_class == BER_CLASS_UNI) && ((ber_tag == BER_UNI_TAG_SET) || (ber_tag == BER_UNI_TAG_SEQ))) ||
|
2015-08-24 13:00:39 +00:00
|
|
|
(((ber_class == BER_CLASS_CON) || (ber_class == BER_CLASS_APP)) && (ber_tag < 32)))))
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_NOT_MINE;
|
2006-05-08 19:56:36 +00:00
|
|
|
|
|
|
|
/* now check the length */
|
|
|
|
oct = bytes[offset++];
|
|
|
|
|
2009-06-02 14:36:42 +00:00
|
|
|
if(oct != 0x80) {
|
|
|
|
/* not indefinite length encoded */
|
|
|
|
|
|
|
|
if(!(oct & 0x80))
|
|
|
|
/* length fits into a single byte */
|
|
|
|
len = oct;
|
|
|
|
else {
|
|
|
|
nlb = oct & 0x7F; /* number of length bytes */
|
|
|
|
|
|
|
|
if((nlb > 0) && (nlb <= (BER_BYTES_TO_CHECK - 2))) {
|
2015-01-02 00:45:22 +00:00
|
|
|
/* not indefinite length and we have read enough bytes to compute the length */
|
|
|
|
i = nlb;
|
|
|
|
while(i--) {
|
|
|
|
oct = bytes[offset++];
|
|
|
|
len = (len<<8) + oct;
|
|
|
|
}
|
2006-05-08 19:56:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
len += (2 + nlb); /* add back Tag and Length bytes */
|
2014-05-09 05:18:49 +00:00
|
|
|
file_size = wtap_file_size(wth, err);
|
2006-05-08 19:56:36 +00:00
|
|
|
|
2006-09-12 21:59:54 +00:00
|
|
|
if(len != file_size) {
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_NOT_MINE; /* not ASN.1 */
|
2006-05-08 19:56:36 +00:00
|
|
|
}
|
2009-06-02 14:36:42 +00:00
|
|
|
} else {
|
|
|
|
/* Indefinite length encoded - assume it is BER */
|
2006-05-08 19:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* seek back to the start of the file */
|
2014-05-09 05:18:49 +00:00
|
|
|
if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_ERROR;
|
2006-05-08 19:56:36 +00:00
|
|
|
|
2021-02-20 08:13:37 +00:00
|
|
|
wth->file_type_subtype = ber_file_type_subtype;
|
2014-05-09 05:18:49 +00:00
|
|
|
wth->file_encap = WTAP_ENCAP_BER;
|
|
|
|
wth->snapshot_length = 0;
|
2006-05-08 19:56:36 +00:00
|
|
|
|
2021-02-20 08:13:37 +00:00
|
|
|
wth->subtype_read = ber_full_file_read;
|
|
|
|
wth->subtype_seek_read = ber_full_file_seek_read;
|
2014-09-28 18:37:06 +00:00
|
|
|
wth->file_tsprec = WTAP_TSPREC_SEC;
|
2006-05-08 19:56:36 +00:00
|
|
|
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_MINE;
|
2006-05-08 19:56:36 +00:00
|
|
|
}
|
2015-01-02 00:45:22 +00:00
|
|
|
|
wiretap: have file handlers advertise blocks and options supported.
Instead of a "supports name resolution" Boolean and bitflags for types of
comments supported, provide a list of block types that the file
type/subtype supports, with each block type having a list of options
supported. Indicate whether "supported" means "one instance" or
"multiple instances".
"Supports" doesn't just mean "can be written", it also means "could be
read".
Rename WTAP_BLOCK_IF_DESCRIPTION to WTAP_BLOCK_IF_ID_AND_INFO, to
indicate that it provides, in addition to information about the
interface, an ID (implicitly, in pcapng files, by its ordinal number)
that is associated with every packet in the file. Emphasize that in
comments - just because your capture file format can list the interfaces
on which a capture was done, that doesn't mean it supports this; it
doesn't do so if the file doesn't indicate, for every packet, on which
of those interfaces it was captured (I'm looking at *you*, Microsoft
Network Monitor...).
Use APIs to query that information to do what the "does this file
type/subtype support name resolution information", "does this file
type/subtype support all of these comment types", and "does this file
type/subtype support - and require - interface IDs" APIs did.
Provide backwards compatibility for Lua.
This allows us to eliminate the WTAP_FILE_TYPE_SUBTYPE_ values for IBM's
iptrace; do so.
2021-02-21 22:18:04 +00:00
|
|
|
static const struct supported_block_type ber_blocks_supported[] = {
|
|
|
|
/*
|
|
|
|
* These are file formats that we dissect, so we provide only one
|
|
|
|
* "packet" with the file's contents, and don't support any
|
|
|
|
* options.
|
|
|
|
*/
|
|
|
|
{ WTAP_BLOCK_PACKET, ONE_BLOCK_SUPPORTED, NO_OPTIONS_SUPPORTED }
|
|
|
|
};
|
|
|
|
|
2021-02-20 08:13:37 +00:00
|
|
|
static const struct file_type_subtype_info ber_info = {
|
|
|
|
"ASN.1 Basic Encoding Rules", "ber", NULL, NULL,
|
wiretap: have file handlers advertise blocks and options supported.
Instead of a "supports name resolution" Boolean and bitflags for types of
comments supported, provide a list of block types that the file
type/subtype supports, with each block type having a list of options
supported. Indicate whether "supported" means "one instance" or
"multiple instances".
"Supports" doesn't just mean "can be written", it also means "could be
read".
Rename WTAP_BLOCK_IF_DESCRIPTION to WTAP_BLOCK_IF_ID_AND_INFO, to
indicate that it provides, in addition to information about the
interface, an ID (implicitly, in pcapng files, by its ordinal number)
that is associated with every packet in the file. Emphasize that in
comments - just because your capture file format can list the interfaces
on which a capture was done, that doesn't mean it supports this; it
doesn't do so if the file doesn't indicate, for every packet, on which
of those interfaces it was captured (I'm looking at *you*, Microsoft
Network Monitor...).
Use APIs to query that information to do what the "does this file
type/subtype support name resolution information", "does this file
type/subtype support all of these comment types", and "does this file
type/subtype support - and require - interface IDs" APIs did.
Provide backwards compatibility for Lua.
This allows us to eliminate the WTAP_FILE_TYPE_SUBTYPE_ values for IBM's
iptrace; do so.
2021-02-21 22:18:04 +00:00
|
|
|
FALSE, BLOCKS_SUPPORTED(ber_blocks_supported),
|
2021-02-20 08:13:37 +00:00
|
|
|
NULL, NULL, NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
void register_ber(void)
|
|
|
|
{
|
2021-02-24 03:10:35 +00:00
|
|
|
ber_file_type_subtype = wtap_register_file_type_subtype(&ber_info);
|
2021-02-20 08:13:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Register name for backwards compatibility with the
|
|
|
|
* wtap_filetypes table in Lua.
|
|
|
|
*/
|
|
|
|
wtap_register_backwards_compatibility_lua_name("BER", ber_file_type_subtype);
|
|
|
|
}
|
|
|
|
|
2015-01-02 00:45:22 +00:00
|
|
|
/*
|
2019-07-26 18:43:17 +00:00
|
|
|
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
2015-01-02 00:45:22 +00:00
|
|
|
*
|
|
|
|
* Local Variables:
|
|
|
|
* c-basic-offset: 2
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=2 tabstop=8 expandtab:
|
|
|
|
* :indentSize=2:tabSize=8:noTabs=true:
|
|
|
|
*/
|