1 /* 2 * Redistribution and use in source and binary forms, with or without 3 * modification, are permitted provided that: (1) source code 4 * distributions retain the above copyright notice and this paragraph 5 * in its entirety, and (2) distributions including binary code include 6 * the above copyright notice and this paragraph in its entirety in 7 * the documentation or other materials provided with the distribution. 8 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND 9 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT 10 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 11 * FOR A PARTICULAR PURPOSE. 12 * 13 * Functions for signature and digest verification. 14 * 15 * Original code by Hannes Gredler (hannes@gredler.at) 16 */ 17 18 #include <sys/cdefs.h> 19 #ifndef lint 20 __RCSID("$NetBSD: signature.c,v 1.10 2023/08/17 20:19:40 christos Exp $"); 21 #endif 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #include "netdissect-stdinc.h" 28 29 #include <string.h> 30 #include <stdlib.h> 31 32 #include "netdissect.h" 33 #include "signature.h" 34 #include "diag-control.h" 35 36 #ifdef HAVE_LIBCRYPTO 37 #include <openssl/md5.h> 38 #endif 39 40 const struct tok signature_check_values[] = { 41 { SIGNATURE_VALID, "valid"}, 42 { SIGNATURE_INVALID, "invalid"}, 43 { CANT_ALLOCATE_COPY, "can't allocate memory"}, 44 { CANT_CHECK_SIGNATURE, "unchecked"}, 45 { 0, NULL } 46 }; 47 48 49 #ifdef HAVE_LIBCRYPTO 50 /* 51 * Compute a HMAC MD5 sum. 52 * Taken from rfc2104, Appendix. 53 */ 54 DIAG_OFF_DEPRECATION 55 static void 56 signature_compute_hmac_md5(const uint8_t *text, int text_len, unsigned char *key, 57 unsigned int key_len, uint8_t *digest) 58 { 59 MD5_CTX context; 60 unsigned char k_ipad[65]; /* inner padding - key XORd with ipad */ 61 unsigned char k_opad[65]; /* outer padding - key XORd with opad */ 62 unsigned char tk[16]; 63 int i; 64 65 /* if key is longer than 64 bytes reset it to key=MD5(key) */ 66 if (key_len > 64) { 67 68 MD5_CTX tctx; 69 70 MD5_Init(&tctx); 71 MD5_Update(&tctx, key, key_len); 72 MD5_Final(tk, &tctx); 73 74 key = tk; 75 key_len = 16; 76 } 77 78 /* 79 * the HMAC_MD5 transform looks like: 80 * 81 * MD5(K XOR opad, MD5(K XOR ipad, text)) 82 * 83 * where K is an n byte key 84 * ipad is the byte 0x36 repeated 64 times 85 * opad is the byte 0x5c repeated 64 times 86 * and text is the data being protected 87 */ 88 89 /* start out by storing key in pads */ 90 memset(k_ipad, 0, sizeof(k_ipad)); 91 memset(k_opad, 0, sizeof(k_opad)); 92 memcpy(k_ipad, key, key_len); 93 memcpy(k_opad, key, key_len); 94 95 /* XOR key with ipad and opad values */ 96 for (i=0; i<64; i++) { 97 k_ipad[i] ^= 0x36; 98 k_opad[i] ^= 0x5c; 99 } 100 101 /* 102 * perform inner MD5 103 */ 104 MD5_Init(&context); /* init context for 1st pass */ 105 MD5_Update(&context, k_ipad, 64); /* start with inner pad */ 106 MD5_Update(&context, text, text_len); /* then text of datagram */ 107 MD5_Final(digest, &context); /* finish up 1st pass */ 108 109 /* 110 * perform outer MD5 111 */ 112 MD5_Init(&context); /* init context for 2nd pass */ 113 MD5_Update(&context, k_opad, 64); /* start with outer pad */ 114 MD5_Update(&context, digest, 16); /* then results of 1st hash */ 115 MD5_Final(digest, &context); /* finish up 2nd pass */ 116 } 117 DIAG_ON_DEPRECATION 118 119 /* 120 * Verify a cryptographic signature of the packet. 121 * Currently only MD5 is supported. 122 */ 123 int 124 signature_verify(netdissect_options *ndo, const u_char *pptr, u_int plen, 125 const u_char *sig_ptr, void (*clear_rtn)(void *), 126 const void *clear_arg) 127 { 128 uint8_t *packet_copy, *sig_copy; 129 uint8_t sig[16]; 130 unsigned int i; 131 132 if (!ndo->ndo_sigsecret) { 133 return (CANT_CHECK_SIGNATURE); 134 } 135 136 /* 137 * Do we have all the packet data to be checked? 138 */ 139 if (!ND_TTEST_LEN(pptr, plen)) { 140 /* No. */ 141 return (CANT_CHECK_SIGNATURE); 142 } 143 144 /* 145 * Do we have the entire signature to check? 146 */ 147 if (!ND_TTEST_LEN(sig_ptr, sizeof(sig))) { 148 /* No. */ 149 return (CANT_CHECK_SIGNATURE); 150 } 151 if (sig_ptr + sizeof(sig) > pptr + plen) { 152 /* No. */ 153 return (CANT_CHECK_SIGNATURE); 154 } 155 156 /* 157 * Make a copy of the packet, so we don't overwrite the original. 158 */ 159 packet_copy = malloc(plen); 160 if (packet_copy == NULL) { 161 return (CANT_ALLOCATE_COPY); 162 } 163 164 memcpy(packet_copy, pptr, plen); 165 166 /* 167 * Clear the signature in the copy. 168 */ 169 sig_copy = packet_copy + (sig_ptr - pptr); 170 memset(sig_copy, 0, sizeof(sig)); 171 172 /* 173 * Clear anything else that needs to be cleared in the copy. 174 * Our caller is assumed to have vetted the clear_arg pointer. 175 */ 176 (*clear_rtn)((void *)(packet_copy + ((const uint8_t *)clear_arg - pptr))); 177 178 /* 179 * Compute the signature. 180 */ 181 signature_compute_hmac_md5(packet_copy, plen, 182 (unsigned char *)ndo->ndo_sigsecret, 183 strlen(ndo->ndo_sigsecret), sig); 184 185 /* 186 * Free the copy. 187 */ 188 free(packet_copy); 189 190 /* 191 * Does the computed signature match the signature in the packet? 192 */ 193 if (memcmp(sig_ptr, sig, sizeof(sig)) == 0) { 194 /* Yes. */ 195 return (SIGNATURE_VALID); 196 } else { 197 /* No - print the computed signature. */ 198 for (i = 0; i < sizeof(sig); ++i) { 199 ND_PRINT("%02x", sig[i]); 200 } 201 202 return (SIGNATURE_INVALID); 203 } 204 } 205 #else 206 int 207 signature_verify(netdissect_options *ndo _U_, const u_char *pptr _U_, 208 u_int plen _U_, const u_char *sig_ptr _U_, 209 void (*clear_rtn)(void *) _U_, const void *clear_arg _U_) 210 { 211 return (CANT_CHECK_SIGNATURE); 212 } 213 #endif 214