123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include <netdissect-stdinc.h>
- #include <string.h>
- #include <stdlib.h>
- #include "netdissect.h"
- #include "signature.h"
- #ifdef HAVE_LIBCRYPTO
- #include <openssl/md5.h>
- #endif
- const struct tok signature_check_values[] = {
- { SIGNATURE_VALID, "valid"},
- { SIGNATURE_INVALID, "invalid"},
- { CANT_ALLOCATE_COPY, "can't allocate memory"},
- { CANT_CHECK_SIGNATURE, "unchecked"},
- { 0, NULL }
- };
- #ifdef HAVE_LIBCRYPTO
- USES_APPLE_DEPRECATED_API
- static void
- signature_compute_hmac_md5(const uint8_t *text, int text_len, unsigned char *key,
- unsigned int key_len, uint8_t *digest)
- {
- MD5_CTX context;
- unsigned char k_ipad[65];
- unsigned char k_opad[65];
- unsigned char tk[16];
- int i;
-
- if (key_len > 64) {
- MD5_CTX tctx;
- MD5_Init(&tctx);
- MD5_Update(&tctx, key, key_len);
- MD5_Final(tk, &tctx);
- key = tk;
- key_len = 16;
- }
-
-
- memset(k_ipad, 0, sizeof k_ipad);
- memset(k_opad, 0, sizeof k_opad);
- memcpy(k_ipad, key, key_len);
- memcpy(k_opad, key, key_len);
-
- for (i=0; i<64; i++) {
- k_ipad[i] ^= 0x36;
- k_opad[i] ^= 0x5c;
- }
-
- MD5_Init(&context);
- MD5_Update(&context, k_ipad, 64);
- MD5_Update(&context, text, text_len);
- MD5_Final(digest, &context);
-
- MD5_Init(&context);
- MD5_Update(&context, k_opad, 64);
- MD5_Update(&context, digest, 16);
- MD5_Final(digest, &context);
- }
- USES_APPLE_RST
- int
- signature_verify(netdissect_options *ndo, const u_char *pptr, u_int plen,
- const u_char *sig_ptr, void (*clear_rtn)(void *),
- const void *clear_arg)
- {
- uint8_t *packet_copy, *sig_copy;
- uint8_t sig[16];
- unsigned int i;
- if (!ndo->ndo_sigsecret) {
- return (CANT_CHECK_SIGNATURE);
- }
-
- if (!ND_TTEST2(pptr, plen)) {
-
- return (CANT_CHECK_SIGNATURE);
- }
-
- if (!ND_TTEST2(sig_ptr, sizeof(sig))) {
-
- return (CANT_CHECK_SIGNATURE);
- }
- if (sig_ptr + sizeof(sig) > pptr + plen) {
-
- return (CANT_CHECK_SIGNATURE);
- }
-
- packet_copy = malloc(plen);
- if (packet_copy == NULL) {
- return (CANT_ALLOCATE_COPY);
- }
- memcpy(packet_copy, pptr, plen);
-
- sig_copy = packet_copy + (sig_ptr - pptr);
- memset(sig_copy, 0, sizeof(sig));
-
- (*clear_rtn)((void *)(packet_copy + ((const uint8_t *)clear_arg - pptr)));
-
- signature_compute_hmac_md5(packet_copy, plen,
- (unsigned char *)ndo->ndo_sigsecret,
- strlen(ndo->ndo_sigsecret), sig);
-
- free(packet_copy);
-
- if (memcmp(sig_ptr, sig, sizeof(sig)) == 0) {
-
- return (SIGNATURE_VALID);
- } else {
-
- for (i = 0; i < sizeof(sig); ++i) {
- ND_PRINT((ndo, "%02x", sig[i]));
- }
- return (SIGNATURE_INVALID);
- }
- }
- #else
- int
- signature_verify(netdissect_options *ndo _U_, const u_char *pptr _U_,
- u_int plen _U_, const u_char *sig_ptr _U_,
- void (*clear_rtn)(void *) _U_, const void *clear_arg _U_)
- {
- return (CANT_CHECK_SIGNATURE);
- }
- #endif
|