From 8d6e8f649894611dbe2feced7ae8eb3de863ff4b Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Wed, 1 Aug 2018 00:01:45 -0700 Subject: RFC 7468 is PEM-inspired, but it's not PEM. Do some renaming. Change-Id: If8fa85370014f9618df38d97048dd1c52a4c389f Reviewed-on: https://code.wireshark.org/review/28918 Reviewed-by: Guy Harris --- wiretap/CMakeLists.txt | 2 +- wiretap/file_access.c | 8 +-- wiretap/pem.c | 176 ------------------------------------------------- wiretap/pem.h | 27 -------- wiretap/rfc7468.c | 176 +++++++++++++++++++++++++++++++++++++++++++++++++ wiretap/rfc7468.h | 27 ++++++++ wiretap/wtap.h | 2 +- 7 files changed, 209 insertions(+), 209 deletions(-) delete mode 100644 wiretap/pem.c delete mode 100644 wiretap/pem.h create mode 100644 wiretap/rfc7468.c create mode 100644 wiretap/rfc7468.h diff --git a/wiretap/CMakeLists.txt b/wiretap/CMakeLists.txt index d17bdc4a68..a672461603 100644 --- a/wiretap/CMakeLists.txt +++ b/wiretap/CMakeLists.txt @@ -68,7 +68,7 @@ set(WIRETAP_NONGENERATED_FILES pcapng.c peekclassic.c peektagged.c - pem.c + rfc7468.c pppdump.c radcom.c ruby_marshal.c diff --git a/wiretap/file_access.c b/wiretap/file_access.c index 4cc15b3d94..4f9b3ffb6f 100644 --- a/wiretap/file_access.c +++ b/wiretap/file_access.c @@ -73,7 +73,7 @@ #include "nettrace_3gpp_32_423.h" #include "mplog.h" #include "dpa400.h" -#include "pem.h" +#include "rfc7468.h" #include "ruby_marshal.h" /* @@ -356,7 +356,7 @@ static const struct open_info open_info_base[] = { { "MIME Files Format", OPEN_INFO_MAGIC, mime_file_open, NULL, NULL, NULL }, { "Micropross mplog", OPEN_INFO_MAGIC, mplog_open, "mplog", NULL, NULL }, { "Unigraf DPA-400 capture", OPEN_INFO_MAGIC, dpa400_open, "bin", NULL, NULL }, - { "ASN.1 (PEM-like encoding)", OPEN_INFO_MAGIC, pem_open, "pem;crt", NULL, NULL }, + { "RFC 7468 files", OPEN_INFO_MAGIC, rfc7468_open, "pem;crt", NULL, NULL }, { "Novell LANalyzer", OPEN_INFO_HEURISTIC, lanalyzer_open, "tr1", NULL, NULL }, /* * PacketLogger must come before MPEG, because its files @@ -1617,8 +1617,8 @@ static const struct file_type_subtype_info dump_open_table_base[] = { FALSE, FALSE, 0, NULL, NULL, NULL }, - /* WTAP_FILE_TYPE_SUBTYPE_PEM */ - { "ASN.1 (PEM-like encoding)", "pem", NULL, NULL, + /* WTAP_FILE_TYPE_SUBTYPE_RFC7468 */ + { "RFC 7468 files", "rfc7468", NULL, NULL, FALSE, FALSE, 0, NULL, NULL, NULL } }; diff --git a/wiretap/pem.c b/wiretap/pem.c deleted file mode 100644 index d0b7b66e46..0000000000 --- a/wiretap/pem.c +++ /dev/null @@ -1,176 +0,0 @@ -/* pem.c - * - * Implements loading of files in the format specified by RFC 7468. - * - * SPDX-License-Identifier: GPL-2.0-or-later - */ - -#include "config.h" - -#include "pem.h" - -#include "file_wrappers.h" -#include "wtap-int.h" - -#include - -#include - -#include - -static gboolean pem_read_file(wtap *wth, FILE_T fh, wtap_rec *rec, - Buffer *buf, int *err, gchar **err_info) -{ - gint64 file_size; - int packet_size; - - if ((file_size = wtap_file_size(wth, err)) == -1) - return FALSE; - - if (file_size > G_MAXINT) { - /* - * Probably a corrupt capture file; don't blow up trying - * to allocate space for an immensely-large packet. - */ - *err = WTAP_ERR_BAD_FILE; - *err_info = g_strdup_printf("pem: File has %" G_GINT64_MODIFIER "d-byte packet, bigger than maximum of %u", - file_size, G_MAXINT); - return FALSE; - } - packet_size = (int)file_size; - - rec->rec_type = REC_TYPE_PACKET; - rec->presence_flags = 0; /* yes, we have no bananas^Wtime stamp */ - - rec->rec_header.packet_header.caplen = packet_size; - rec->rec_header.packet_header.len = packet_size; - - rec->ts.secs = 0; - rec->ts.nsecs = 0; - - return wtap_read_packet_bytes(fh, buf, packet_size, err, err_info); -} - -/* 128 bytes should be enough to contain any line. Strictly speaking, 64 is - enough, but we provide some leeway to accomodate nonconformant producers and - trailing whitespace. The 2 extra bytes are for the trailing newline and NUL - terminator. */ -#define MAX_LINE_LENGTH (128 + 2) - -static char *read_complete_text_line(char line[MAX_LINE_LENGTH], FILE_T fh, int *err, gchar **err_info) -{ - char *line_end; - - if (!(line_end = file_getsp(line, MAX_LINE_LENGTH, fh))) { - *err = file_error(fh, err_info); - return NULL; - } - - if (strlen(line) != (size_t)(line_end - line)) { - *err = WTAP_ERR_BAD_FILE; - *err_info = g_strdup("unexpected NUL inside a line"); - return NULL; - } - - if (line_end[-1] != '\n' && !file_eof(fh)) { - *err = WTAP_ERR_BAD_FILE; - *err_info = g_strdup("overlong line"); - return NULL; - } - - return line_end; -} - -static gboolean pem_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset) -{ - gint64 offset; - - *err = 0; - - offset = file_tell(wth->fh); - - /* there is only ever one packet */ - if (offset != 0) - return FALSE; - - *data_offset = offset; - - return pem_read_file(wth, wth->fh, &wth->rec, wth->rec_data, err, err_info); -} - -static gboolean pem_seek_read(wtap *wth, gint64 seek_off, wtap_rec *rec, - Buffer *buf, int *err, gchar **err_info) -{ - /* there is only one packet */ - if (seek_off > 0) { - *err = 0; - return FALSE; - } - - if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) - return FALSE; - - return pem_read_file(wth, wth->random_fh, rec, buf, err, err_info); -} - -// -// Arbitrary value - we don't want to read all of a huge non-PEM file -// only to find no pre-encapsulation boundary. -// -#define MAX_EXPLANATORY_TEXT_LINES 20 - -wtap_open_return_val pem_open(wtap *wth, int *err, gchar **err_info) -{ - gboolean found_preeb; - static const char preeb_begin[] = "-----BEGIN "; - char line[MAX_LINE_LENGTH]; - - // - // Skip up to MAX_EXPLANATORY_TEXT_LINES worth of lines that don't - // look like pre-encapsulation boundaries. - // - found_preeb = FALSE; - for (unsigned int i = 0; i < MAX_EXPLANATORY_TEXT_LINES; i++) { - if (!read_complete_text_line(line, wth->fh, err, err_info)) { - if (*err == WTAP_ERR_SHORT_READ) - return WTAP_OPEN_NOT_MINE; - return WTAP_OPEN_ERROR; - } - - // Does the line look like a pre-encapsulation boundary? - if (memcmp(line, preeb_begin, sizeof preeb_begin - 1) == 0) { - // Yes. - found_preeb = TRUE; - break; - } - } - if (!found_preeb) - return WTAP_OPEN_NOT_MINE; - - if (file_seek(wth->fh, 0, SEEK_SET, err) == -1) - return WTAP_OPEN_ERROR; - - wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_PEM; - wth->file_encap = WTAP_ENCAP_RFC7468; - - wth->snapshot_length = 0; - wth->file_tsprec = WTAP_TSPREC_SEC; - - wth->subtype_read = pem_read; - wth->subtype_seek_read = pem_seek_read; - - return WTAP_OPEN_MINE; -} - -/* - * Editor modelines - http://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: - */ diff --git a/wiretap/pem.h b/wiretap/pem.h deleted file mode 100644 index 5a6cff58ba..0000000000 --- a/wiretap/pem.h +++ /dev/null @@ -1,27 +0,0 @@ -/* pem.h - * - * SPDX-License-Identifier: GPL-2.0-or-later - */ - -#ifndef __PEM_H__ -#define __PEM_H__ - -#include -#include "wtap.h" - -wtap_open_return_val pem_open(wtap *wth, int *err, gchar **err_info); - -#endif - -/* -* Editor modelines - http://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: -*/ diff --git a/wiretap/rfc7468.c b/wiretap/rfc7468.c new file mode 100644 index 0000000000..08b5a35e33 --- /dev/null +++ b/wiretap/rfc7468.c @@ -0,0 +1,176 @@ +/* rfc7468.c + * + * Implements loading of files in the format specified by RFC 7468. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "config.h" + +#include "rfc7468.h" + +#include "file_wrappers.h" +#include "wtap-int.h" + +#include + +#include + +#include + +static gboolean rfc7468_read_file(wtap *wth, FILE_T fh, wtap_rec *rec, + Buffer *buf, int *err, gchar **err_info) +{ + gint64 file_size; + int packet_size; + + if ((file_size = wtap_file_size(wth, err)) == -1) + return FALSE; + + if (file_size > G_MAXINT) { + /* + * Probably a corrupt capture file; don't blow up trying + * to allocate space for an immensely-large packet. + */ + *err = WTAP_ERR_BAD_FILE; + *err_info = g_strdup_printf("rfc7468: File has %" G_GINT64_MODIFIER "d-byte packet, bigger than maximum of %u", + file_size, G_MAXINT); + return FALSE; + } + packet_size = (int)file_size; + + rec->rec_type = REC_TYPE_PACKET; + rec->presence_flags = 0; /* yes, we have no bananas^Wtime stamp */ + + rec->rec_header.packet_header.caplen = packet_size; + rec->rec_header.packet_header.len = packet_size; + + rec->ts.secs = 0; + rec->ts.nsecs = 0; + + return wtap_read_packet_bytes(fh, buf, packet_size, err, err_info); +} + +/* 128 bytes should be enough to contain any line. Strictly speaking, 64 is + enough, but we provide some leeway to accomodate nonconformant producers and + trailing whitespace. The 2 extra bytes are for the trailing newline and NUL + terminator. */ +#define MAX_LINE_LENGTH (128 + 2) + +static char *read_complete_text_line(char line[MAX_LINE_LENGTH], FILE_T fh, int *err, gchar **err_info) +{ + char *line_end; + + if (!(line_end = file_getsp(line, MAX_LINE_LENGTH, fh))) { + *err = file_error(fh, err_info); + return NULL; + } + + if (strlen(line) != (size_t)(line_end - line)) { + *err = WTAP_ERR_BAD_FILE; + *err_info = g_strdup("unexpected NUL inside a line"); + return NULL; + } + + if (line_end[-1] != '\n' && !file_eof(fh)) { + *err = WTAP_ERR_BAD_FILE; + *err_info = g_strdup("overlong line"); + return NULL; + } + + return line_end; +} + +static gboolean rfc7468_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset) +{ + gint64 offset; + + *err = 0; + + offset = file_tell(wth->fh); + + /* there is only ever one packet */ + if (offset != 0) + return FALSE; + + *data_offset = offset; + + return rfc7468_read_file(wth, wth->fh, &wth->rec, wth->rec_data, err, err_info); +} + +static gboolean rfc7468_seek_read(wtap *wth, gint64 seek_off, wtap_rec *rec, + Buffer *buf, int *err, gchar **err_info) +{ + /* there is only one packet */ + if (seek_off > 0) { + *err = 0; + return FALSE; + } + + if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) + return FALSE; + + return rfc7468_read_file(wth, wth->random_fh, rec, buf, err, err_info); +} + +// +// Arbitrary value - we don't want to read all of a huge non-RFC 7468 file +// only to find no pre-encapsulation boundary. +// +#define MAX_EXPLANATORY_TEXT_LINES 20 + +wtap_open_return_val rfc7468_open(wtap *wth, int *err, gchar **err_info) +{ + gboolean found_preeb; + static const char preeb_begin[] = "-----BEGIN "; + char line[MAX_LINE_LENGTH]; + + // + // Skip up to MAX_EXPLANATORY_TEXT_LINES worth of lines that don't + // look like pre-encapsulation boundaries. + // + found_preeb = FALSE; + for (unsigned int i = 0; i < MAX_EXPLANATORY_TEXT_LINES; i++) { + if (!read_complete_text_line(line, wth->fh, err, err_info)) { + if (*err == WTAP_ERR_SHORT_READ) + return WTAP_OPEN_NOT_MINE; + return WTAP_OPEN_ERROR; + } + + // Does the line look like a pre-encapsulation boundary? + if (memcmp(line, preeb_begin, sizeof preeb_begin - 1) == 0) { + // Yes. + found_preeb = TRUE; + break; + } + } + if (!found_preeb) + return WTAP_OPEN_NOT_MINE; + + if (file_seek(wth->fh, 0, SEEK_SET, err) == -1) + return WTAP_OPEN_ERROR; + + wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_RFC7468; + wth->file_encap = WTAP_ENCAP_RFC7468; + + wth->snapshot_length = 0; + wth->file_tsprec = WTAP_TSPREC_SEC; + + wth->subtype_read = rfc7468_read; + wth->subtype_seek_read = rfc7468_seek_read; + + return WTAP_OPEN_MINE; +} + +/* + * Editor modelines - http://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: + */ diff --git a/wiretap/rfc7468.h b/wiretap/rfc7468.h new file mode 100644 index 0000000000..9df62051c2 --- /dev/null +++ b/wiretap/rfc7468.h @@ -0,0 +1,27 @@ +/* rfc7468.h + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef __PEM_H__ +#define __PEM_H__ + +#include +#include "wtap.h" + +wtap_open_return_val rfc7468_open(wtap *wth, int *err, gchar **err_info); + +#endif + +/* +* Editor modelines - http://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: +*/ diff --git a/wiretap/wtap.h b/wiretap/wtap.h index 410001dddd..7f2eeb9184 100644 --- a/wiretap/wtap.h +++ b/wiretap/wtap.h @@ -375,7 +375,7 @@ extern "C" { #define WTAP_FILE_TYPE_SUBTYPE_NETTRACE_3GPP_32_423 79 #define WTAP_FILE_TYPE_SUBTYPE_MPLOG 80 #define WTAP_FILE_TYPE_SUBTYPE_DPA400 81 -#define WTAP_FILE_TYPE_SUBTYPE_PEM 82 +#define WTAP_FILE_TYPE_SUBTYPE_RFC7468 82 #define WTAP_FILE_TYPE_SUBTYPE_RUBY_MARSHAL 83 #define WTAP_NUM_FILE_TYPES_SUBTYPES wtap_get_num_file_types_subtypes() -- cgit v1.2.3