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