Commit Graph

136 Commits

Author SHA1 Message Date
Guy Harris
bd22f1fd9a Clean up ESP and ISAKMP decryption.
At least as I read RFC 5996 section 3.14 and RFC 4303 section 2.4, if
the cipher has a block size of which the ciphertext's size must be a
multiple, the payload must be padded to make that happen, so the
ciphertext length must be a multiple of the block size.  Instead of
allocating a buffer, copying the ciphertext to it, and padding it to the
block size, fail if its size isn't a multiple of the block size.

(Note also that the old padding code added a block's worth of padding to
the end of a ciphertext block that *was* a multiple of the cipher block
size; this might have caused problems.)

Don't use the undocumented EVP_Cipher(); the lack of documentation means
a lack of information about whatever requirements it might impose.  Use
EVP_DecryptUpdate() instead.

Before calling it, use EVP_CIPHER_CTX_set_padding() to say "don't do
your own padding, this block is a multiple of the cipher block size".

Instead of using EVP_CipherInit() or EVP_CipherInit_ex(), use
EVP_DecryptInit() or EVP_DecryptInit_ex().  as we're always doing
decryption and never doing encryption - the extra parameter to
EVP_CipherInit() and EVP_CipherInit_ex() is always 0.

This may address GitHub issue #814.

It may also make it a bit easier to have the code use Common Crypto on
macOS (rather than requiring that OpenSSL be installed - macOS ships
with an OpenSSL shared library for binary compatibility with older
releases, but doesn't ship with the headers, because Apple wants you
using their crypto code) and use Cryptography API: Next Generation on
Windows (Vista/Server 2008 and later) (rather than requiring a Windows
build of OpenSSL).

(Hopefully this will all work with LibreSSL.)
2020-01-05 18:37:52 -08:00
Guy Harris
011ae55410 Fix check of authentication algorithm.
If you split a string by overwriting the separator character with a NUL,
and you want to compare the first part with various values, use a
pointer to the beginning of the string, not to the NUL you just dropped
in.

This is a bug found while testing some cleanups for GitHub issue #814;
it's necessary for the cleanups to work.
2020-01-05 17:49:42 -08:00
Guy Harris
9a6a650241 Don't use <ctype.h> macros.
Some of them are locale-dependent, and all of them run the risk of
failing if you hand them a char with the 8th bit set.

Move our replacements to a new netdissect-ctype.h file, and, for the
ones that check for particular character types, add _ASCII to the name,
to indicate that only ASCII characters pass the check.  Do the same for
the ones that map between cases, to indicate that they only map ASCII
letters.

For isspace(), explicitly check for the characters we care about, to
make it clearer what we're doing.
2019-09-01 16:11:32 -07:00
Francois-Xavier Le Bail
0c9cfdcb0d Use nd_ipv6 rather than struct in6_addr 2019-06-10 15:32:20 +02:00
Francois-Xavier Le Bail
6fc5b4200b Use nd_ipv4 rather than struct in_addr 2019-06-10 14:14:46 +02:00
Guy Harris
b7b72b7c63 Treat the length field in an Ethernet header as such.
If we have an Ethernet packet where the last 2 octets of the header are
a length rather than an Ethernet type, and it's less than the remaining
length of the packet, shorten the length and captured length, update the
snapshot end.

Turn the buffer stack into a "packet information" stack, so that, if we
*do* update the snapshot end, we push the old end onto the stack, and
pop it off as soon as we're done dissecting the Ethernet packet, in case
there's more data in the packet after the Ethernet packet.

Use the stack when we use the IPv4 and IPv6 length fields as well.
2019-04-22 23:49:02 -07:00
Francois-Xavier Le Bail
89c54d9bda Remove a trailing space 2019-04-02 14:24:17 +02:00
Guy Harris
6da51b3ec9 Introduce a buffer stack, and use it for ESP decryption.
If a dissector has to process its input - decryption, decompression,
etc. - rather than dissect the raw input, it should push the processed
input onto the buffer stack.  As soon as the dissection is done, the
stack should be popped, to free the buffer into which the processing was
done, and restore the "pointer to packet data" and "pointer to end of
packet data" members of the netdissect_options structure, so the code
can go back to dissecting the original data.

The stack will get everything popped off it when dissection is done.

Use this mechanism in the ESP decryption code rather than scribbling on
top of the input packet data.
2019-03-30 19:26:06 -07:00
Guy Harris
3d9ae63197 Remove "ptr" from the names of pointers into the packet.
The IV pointer is just called iv in esp_print_decrypt_buffer_by_ikev2();
do the same in esp_print().  Rename ctptr to match.
2019-03-30 19:10:27 -07:00
Guy Harris
ada17ed496 Clean up code a bit.
"ivoff" is a pointer to the IV, not the offset of the IV; call it ivptr.

Have a variable that points to the beginning of the ciphertext, and use
that.

Fix the check that makes sure the authentication data/integrity check
value length isn't too big - it needs to make sure that it doesn't go
before the beginning of the ciphertext, i.e. doesn't overlap with the
IV.

Don't bother with a variable pointing to the secret, just pass
sa->secret.

Fix the check that makes sure the padding length isn't too big - make
sure it, plus 2 for the padding length and next header bytes, isn't
bigger than the ciphertext length.

Update a test to reflect the stricter length checks.
2019-03-30 18:08:02 -07:00
Guy Harris
fefd7f527f Clean up the check to make sure we can get the next header field.
Just check whether the pointer to it is within the available packet data
- and, if it's not, report truncation.

While we're at it:

Make the initialization vector length unsigned; it's either zero or a
positive number.

Rename a variable used for the ciphertext len to ctlen.
2019-03-29 16:16:46 -07:00
Guy Harris
a0ac048cf8 Check the return values of set_cipher_parameters() and EVP_Cipher(). 2019-03-29 12:40:20 -07:00
Guy Harris
b0acedad18 Don't hand un-decrypted data to the IP demuxer.
If we can't decrypt the payload, we can't dissect it, so don't try -
just give up immediately.

While we're at it:

If EVP_CIPHER_CTX_new() fails, it means a memory allocation failed;
treat that as such.

Use some of the arguments we're passed rather than re-fetching them from
the IP header.

Add some comments.

Call nd_print_trunc() for failed length sanity checks, and note that
they can fail due to the decryption being done with the wrong key.

Update one test's output; it is, I think, being decrypted with the wrong
key.
2019-03-29 10:52:58 -07:00
Guy Harris
7fbec6b880 Add a length check. 2019-03-28 14:30:33 -07:00
Guy Harris
48644533f2 Have esp_print() handle printing its payload.
That cleans things up a bit, including removing some duplicate code.

Clean up some other things, and add a length check, while we're at it.
2019-03-28 11:45:43 -07:00
Francois-Xavier Le Bail
ee68aa3646 Use the new GET_ macros instead of the EXTRACT_ ones
The exceptions are currently:
Some EXTRACT_ in print-juniper.c, not used on packet buffer pointer.
An EXTRACT_BE_U_3 in addrtoname.c, not always used on packet buffer
pointer.
2019-03-26 21:06:24 +01:00
agnosticdev
50301a252f return-checks: Added a return check for malloc and for strsep 2018-10-08 21:18:52 -05:00
Francois-Xavier Le Bail
e88bf592be ESP: Fix some unreachable code warnings
Found with -Wunreachable-code-return clang compiler option.

The errors were:

./print-esp.c:317:10: warning: 'return' will never be executed
      [-Wunreachable-code-return]
                return 0;
                       ^
./print-esp.c:552:4: warning: 'return' will never be executed
      [-Wunreachable-code-return]
                        return;
                        ^~~~~~
2018-07-17 22:10:57 +02:00
Francois-Xavier Le Bail
dcce77e91d ESP: Fix some unreachable code warnings
Found with -Wunreachable-code clang compiler option.

The errors were:
./print-esp.c:263:3: warning: code will never be executed
                                                 [-Wunreachable-code]
                free(input_buffer);
                ^~~~
./print-esp.c:246:3: warning: code will never be executed
                                                 [-Wunreachable-code]
                EVP_CIPHER_CTX_free(ctx);
                ^~~~~~~~~~~~~~~~~~~
./print-esp.c:843:5: warning: code will never be executed
                                                 [-Wunreachable-code]
                                free(input_buffer);
                                ^~~~
./print-esp.c:826:5: warning: code will never be executed
                                                 [-Wunreachable-code]
                                EVP_CIPHER_CTX_free(ctx);
                                ^~~~~~~~~~~~~~~~~~~
2018-07-08 20:08:07 +02:00
Guy Harris
4ba56b8c2e Encrypted data buffer sizes must also be a multiple of the cipher block size.
This should address the issue in GitHub pull request #680.
2018-05-24 11:45:19 -07:00
Francois-Xavier Le Bail
00ecef01d7 Add more nd_print_trunc() calls
Update the output of some tests accordingly.
2018-05-06 11:26:45 +02:00
Francois-Xavier Le Bail
56e6581c34 Remove useless comments 2018-03-19 15:16:32 +01:00
Francois-Xavier Le Bail
546558eabd Add the ndo_protocol field in the netdissect_options structure
Update this field in printer entry functions.
It will be used for some printings.
2018-03-16 19:44:47 +01:00
Francois-Xavier Le Bail
fa196ecfbe Add a comment on a memory allocation we cannot free 2018-03-12 15:44:10 +01:00
Francois-Xavier Le Bail
5ae22f41a8 Add a status exit code to the function ndo_error()
The status are defined in an enum in status-exit-codes.h.

Moreover:
Use ndo_error() instead of ndo_warning() for malloc() errors in print-esp.c.
2018-02-25 20:58:27 +01:00
Guy Harris
3db38df8ed Use nd_ipv6 rather than struct in6_addr in packet-layout structures.
Also, use it, and nd_ipv4, in sizeof() operations.
2018-01-30 17:29:33 -08:00
Guy Harris
fb2479d733 Always include <config.h> rather than "config.h".
This can prevent bizarre failures if, for example, you've done a
configuration in the top-level source directory, leaving behind one
config.h file, and then do an out-of-tree build in another directory,
with different configuration options.  This way, we always pick up the
same config.h, in the build directory.
2018-01-21 12:27:28 -08:00
Francois-Xavier Le Bail
513f782ae1 Use quoted include netdissect-stdinc.h instead of angle-bracketed one 2018-01-21 10:28:15 +01:00
Guy Harris
64677b0d78 Clean up signed vs. unsigned. 2018-01-11 11:52:30 -08:00
Francois-Xavier Le Bail
9eca8c4ed2 Remove unneeded '&' when getting a pointer to an nd_uintN_t type 2018-01-08 19:59:56 +01:00
Francois-Xavier Le Bail
e2982e7f6f Update ND_PRINT() as a variadic macro 2018-01-07 13:36:41 +01:00
Guy Harris
5cd7d3ab27 Use nd_ types. 2017-12-30 15:08:40 -08:00
Guy Harris
195a4cfd9d Clean up ISAKMP dissection, use nd_ types and EXTRACT_ macros.
Add some RFC numbers.

Structures with nd_ types work well if you overlay them on top of the
packet buffer, but not as well with an on-the-stack structure into which
you copy from the packet, which is ugly if you do *both* with the same
structure.  Use overlaying uniformly.

Add EXTRACT_ macros as necessary for the nd_ types.

Make stuff unsigned if it's appropriate.

Use unsigned formats for unsigned values.

A number of routines are passed the length of a payload, so they don't
have to re-fetch that length themselves.  That length has been
pre-checked to make sure it's big enough for the payload header; note
that in comments.
2017-12-17 21:49:46 -08:00
Francois-Xavier Le Bail
5cea270318 Remove all storage class specifier 'register'
Let the compiler do the optimizations (or not) based on build options.

Avoid 'value has been optimized out' messages in gdb using '-O0'.
2017-12-13 19:17:47 +01:00
Francois-Xavier Le Bail
da20bc56d6 Rename EXTRACT_ macros
Now all the macros have a name meaning a count in bytes.

With _S_: signed, _U_: unsigned

e.g.:
EXTRACT_BE_32BITS -> EXTRACT_BE_U_4
EXTRACT_LE_32BITS -> EXTRACT_LE_U_4
...
EXTRACT_BE_INT32 -> EXTRACT_BE_S_4

and have:
EXTRACT_8BITS -> EXTRACT_U_1
EXTRACT_INT8  -> EXTRACT_S_1
2017-11-22 23:54:09 +01:00
Guy Harris
c0679b38a3 More EXTRACT_8BITS() and other cleanups.
Get rid of casts to (int) that aren't needed or wanted.

If a field is unsigned, use an unsigned variable for it, print it with
%u, not %d, and don't cast it to int.

Replace a static variable in print-dvmrp.c with a local variable in
dvmrp_print() and a parameter to print_neighbors2().
2017-11-22 10:15:49 -08:00
Francois-Xavier Le Bail
577621026d Rename EXTRACT_nBITS() macros to EXTRACT_BE_nBITS()
It indicates clearly that these macros are used to extract big-endian
integral values.
2017-11-18 13:56:40 +01:00
Guy Harris
6f0750ee0c Make the ESP decryption not crash with OpenSSL 1.1.
While we're at it, free the cipher context if we fail to allocate the
output buffer for decryption.
2017-09-01 04:00:38 -07:00
Guy Harris
0d2cdb5dad The cipher context must be initialized before we can get the block size. 2017-08-31 00:03:59 -07:00
Guy Harris
f7e20cf4da Cleanups.
Attempt to allocate the buffer before doing any decryption calls and, if
the attempt fails, report an error and return an error indication.

Make sizes unsigned, as they can't be negative;
EVP_CIPHER_CTX_block_size() returns an int, but that's just because they
picked the wrong data type in OpenSSL.

No need to use calloc() for the output buffer - just use malloc().
2017-08-30 11:18:32 -07:00
Guy Harris
dcf755f49b Get rid of an unneeded variable.
We don't want to try to suppress warnings about discarding the const
qualifier, as that's something we need to fix.  Adding the extra
variable doesn't suppress it on some platforms, so it's not useful
there, and if it does suppress it, we don't want that.

And we weren't doing it in the other case, anyway.

Comment in both cases with an XXX comment about the cast.
2017-08-29 15:05:00 -07:00
Guy Harris
8205015f71 Get rid of another unused variable. 2017-08-29 14:55:44 -07:00
Guy Harris
884b1b45c8 Get rid of unused variable. 2017-08-29 14:54:50 -07:00
Guy Harris
0f44948df5 Update print-esp.c 2017-08-29 11:32:18 -07:00
Guy Harris
2d861fb1a7 Update print-esp.c 2017-08-29 11:31:20 -07:00
Guy Harris
b19c83b54e Update print-esp.c 2017-08-29 11:29:26 -07:00
Martin Sehnoutka
f3c52ca01e Use separate buffers for encrypted and decrypted content in print-esp.c
The EVP_Cipher function should not be called with the same buffer as
input and output. It works fine on most architectures, but fails on
PowerPC. There is also a second problem, that we write the output to a
const buffer, but this issue is not addressed with this PR.
2017-08-29 10:04:35 +02:00
Guy Harris
ad7df8bd34 Don't use OpenSSL_add_all_algorithms() in OpenSSL 1.1.0 or later.
Should fix GitHub issue #539.
2017-01-17 16:25:00 -08:00
Francois-Xavier Le Bail
11d3a01319 Move the printer summaries from INSTALL.txt to each printer
with the tag '\summary:' for greping.
Remark: Currently some printers have no summary line.

Moreover:
Summarize all printers with a single line in INSTALL.txt
2016-08-14 17:03:43 +02:00
Guy Harris
91e08f888d Move some code around.
Move stuff to initialize and clean up libraries that are used by
netdissect code into nd_init() and nd_cleanup() routines in
libnetdissect; this includes Winsock (which is used on Windows by, for
example, the code to get names for IP addresses) and libsmi.

Call nd_init() when tcpdump starts up, and call nd_cleanup() when it
exits.

Move util.c routines to tcpdump.c, and make them static.

Use ndo->ndo_error to report a failure to open a file in print-esp.c.
2016-08-04 13:19:57 -07:00