xref: /freebsd/contrib/ntp/sntp/crypto.c (revision 2d4e511ca269f1908d27f4e5779c53475527391d)
109100258SXin LI /*
209100258SXin LI  * HMS: we need to test:
309100258SXin LI  * - OpenSSL versions, if we are building with them
409100258SXin LI  * - our versions
509100258SXin LI  *
609100258SXin LI  * We may need to test with(out) OPENSSL separately.
709100258SXin LI  */
809100258SXin LI 
92b15cb3dSCy Schubert #include <config.h>
102b15cb3dSCy Schubert #include "crypto.h"
112b15cb3dSCy Schubert #include <ctype.h>
124990d495SXin LI #include "isc/string.h"
13f0574f5cSXin LI #include "ntp_md5.h"
142b15cb3dSCy Schubert 
1509100258SXin LI #ifndef EVP_MAX_MD_SIZE
1609100258SXin LI # define EVP_MAX_MD_SIZE 32
1709100258SXin LI #endif
1809100258SXin LI 
192b15cb3dSCy Schubert struct key *key_ptr;
202b15cb3dSCy Schubert size_t key_cnt = 0;
212b15cb3dSCy Schubert 
2209100258SXin LI typedef struct key Key_T;
2309100258SXin LI 
2409100258SXin LI static u_int
2509100258SXin LI compute_mac(
2609100258SXin LI 	u_char		digest[EVP_MAX_MD_SIZE],
2709100258SXin LI 	char const *	macname,
2809100258SXin LI 	void const *	pkt_data,
2909100258SXin LI 	u_int		pkt_size,
3009100258SXin LI 	void const *	key_data,
3109100258SXin LI 	u_int		key_size
3209100258SXin LI 	)
3309100258SXin LI {
3409100258SXin LI 	u_int		len  = 0;
35*2d4e511cSCy Schubert #if defined(OPENSSL) && defined(ENABLE_CMAC)
3609100258SXin LI 	size_t		slen = 0;
37*2d4e511cSCy Schubert #endif
3809100258SXin LI 	int		key_type;
3909100258SXin LI 
4009100258SXin LI 	INIT_SSL();
4109100258SXin LI 	key_type = keytype_from_text(macname, NULL);
4209100258SXin LI 
434e1ef62aSXin LI #if defined(OPENSSL) && defined(ENABLE_CMAC)
4409100258SXin LI 	/* Check if CMAC key type specific code required */
4509100258SXin LI 	if (key_type == NID_cmac) {
4609100258SXin LI 		CMAC_CTX *	ctx    = NULL;
4709100258SXin LI 		u_char		keybuf[AES_128_KEY_SIZE];
4809100258SXin LI 
4909100258SXin LI 		/* adjust key size (zero padded buffer) if necessary */
5009100258SXin LI 		if (AES_128_KEY_SIZE > key_size) {
5109100258SXin LI 			memcpy(keybuf, key_data, key_size);
5209100258SXin LI 			memset((keybuf + key_size), 0,
5309100258SXin LI 			       (AES_128_KEY_SIZE - key_size));
5409100258SXin LI 			key_data = keybuf;
5509100258SXin LI 		}
5609100258SXin LI 
5709100258SXin LI 		if (!(ctx = CMAC_CTX_new())) {
5809100258SXin LI 			msyslog(LOG_ERR, "make_mac: CMAC %s CTX new failed.",   CMAC);
5909100258SXin LI 		}
6009100258SXin LI 		else if (!CMAC_Init(ctx, key_data, AES_128_KEY_SIZE,
6109100258SXin LI 				    EVP_aes_128_cbc(), NULL)) {
6209100258SXin LI 			msyslog(LOG_ERR, "make_mac: CMAC %s Init failed.",      CMAC);
6309100258SXin LI 		}
6409100258SXin LI 		else if (!CMAC_Update(ctx, pkt_data, (size_t)pkt_size)) {
6509100258SXin LI 			msyslog(LOG_ERR, "make_mac: CMAC %s Update failed.",    CMAC);
6609100258SXin LI 		}
6709100258SXin LI 		else if (!CMAC_Final(ctx, digest, &slen)) {
6809100258SXin LI 			msyslog(LOG_ERR, "make_mac: CMAC %s Final failed.",     CMAC);
6909100258SXin LI 			slen = 0;
7009100258SXin LI 		}
7109100258SXin LI 		len = (u_int)slen;
7209100258SXin LI 
7309100258SXin LI 		CMAC_CTX_cleanup(ctx);
7409100258SXin LI 		/* Test our AES-128-CMAC implementation */
7509100258SXin LI 
7609100258SXin LI 	} else	/* MD5 MAC handling */
7709100258SXin LI #endif
7809100258SXin LI 	{
7909100258SXin LI 		EVP_MD_CTX *	ctx;
8009100258SXin LI 
8109100258SXin LI 		if (!(ctx = EVP_MD_CTX_new())) {
8209100258SXin LI 			msyslog(LOG_ERR, "make_mac: MAC %s Digest CTX new failed.",
8309100258SXin LI 				macname);
8409100258SXin LI 			goto mac_fail;
8509100258SXin LI 		}
8609100258SXin LI #ifdef OPENSSL	/* OpenSSL 1 supports return codes 0 fail, 1 okay */
8709100258SXin LI #	    ifdef EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
8809100258SXin LI 		EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
8909100258SXin LI #	    endif
9009100258SXin LI 		/* [Bug 3457] DON'T use plain EVP_DigestInit! It would
9109100258SXin LI 		 *  kill the flags! */
9209100258SXin LI 		if (!EVP_DigestInit_ex(ctx, EVP_get_digestbynid(key_type), NULL)) {
9309100258SXin LI 			msyslog(LOG_ERR, "make_mac: MAC %s Digest Init failed.",
9409100258SXin LI 				macname);
9509100258SXin LI 			goto mac_fail;
9609100258SXin LI 		}
9709100258SXin LI 		if (!EVP_DigestUpdate(ctx, key_data, key_size)) {
9809100258SXin LI 			msyslog(LOG_ERR, "make_mac: MAC %s Digest Update key failed.",
9909100258SXin LI 				macname);
10009100258SXin LI 			goto mac_fail;
10109100258SXin LI 		}
10209100258SXin LI 		if (!EVP_DigestUpdate(ctx, pkt_data, pkt_size)) {
10309100258SXin LI 			msyslog(LOG_ERR, "make_mac: MAC %s Digest Update data failed.",
10409100258SXin LI 				macname);
10509100258SXin LI 			goto mac_fail;
10609100258SXin LI 		}
10709100258SXin LI 		if (!EVP_DigestFinal(ctx, digest, &len)) {
10809100258SXin LI 			msyslog(LOG_ERR, "make_mac: MAC %s Digest Final failed.",
10909100258SXin LI 				macname);
11009100258SXin LI 			len = 0;
11109100258SXin LI 		}
11209100258SXin LI #else /* !OPENSSL */
11309100258SXin LI 		EVP_DigestInit(ctx, EVP_get_digestbynid(key_type));
11409100258SXin LI 		EVP_DigestUpdate(ctx, key_data, key_size);
11509100258SXin LI 		EVP_DigestUpdate(ctx, pkt_data, pkt_size);
11609100258SXin LI 		EVP_DigestFinal(ctx, digest, &len);
11709100258SXin LI #endif
11809100258SXin LI 	  mac_fail:
11909100258SXin LI 		EVP_MD_CTX_free(ctx);
12009100258SXin LI 	}
12109100258SXin LI 
12209100258SXin LI 	return len;
12309100258SXin LI }
12409100258SXin LI 
1252b15cb3dSCy Schubert int
1262b15cb3dSCy Schubert make_mac(
12768ba7e87SXin LI 	const void *	pkt_data,
1282b15cb3dSCy Schubert 	int		pkt_size,
1292b15cb3dSCy Schubert 	int		mac_size,
13009100258SXin LI 	Key_T const *	cmp_key,
13168ba7e87SXin LI 	void * 		digest
1322b15cb3dSCy Schubert 	)
1332b15cb3dSCy Schubert {
13409100258SXin LI 	u_int		len;
13509100258SXin LI 	u_char		dbuf[EVP_MAX_MD_SIZE];
1362b15cb3dSCy Schubert 
13709100258SXin LI 	if (cmp_key->key_len > 64 || mac_size <= 0)
1382b15cb3dSCy Schubert 		return 0;
1392b15cb3dSCy Schubert 	if (pkt_size % 4 != 0)
1402b15cb3dSCy Schubert 		return 0;
1412b15cb3dSCy Schubert 
14209100258SXin LI 	len = compute_mac(dbuf, cmp_key->typen,
14309100258SXin LI 			  pkt_data, (u_int)pkt_size,
14409100258SXin LI 			  cmp_key->key_seq, (u_int)cmp_key->key_len);
145f391d6bcSXin LI 
1462b15cb3dSCy Schubert 
14709100258SXin LI 	if (len) {
14809100258SXin LI 		if (len > (u_int)mac_size)
14909100258SXin LI 			len = (u_int)mac_size;
15009100258SXin LI 		memcpy(digest, dbuf, len);
15109100258SXin LI 	}
1522b15cb3dSCy Schubert 	return (int)len;
1532b15cb3dSCy Schubert }
1542b15cb3dSCy Schubert 
1552b15cb3dSCy Schubert 
15668ba7e87SXin LI /* Generates a md5 digest of the key specified in keyid concatenated with the
1572b15cb3dSCy Schubert  * ntp packet (exluding the MAC) and compares this digest to the digest in
1582b15cb3dSCy Schubert  * the packet's MAC. If they're equal this function returns 1 (packet is
1592b15cb3dSCy Schubert  * authentic) or else 0 (not authentic).
1602b15cb3dSCy Schubert  */
1612b15cb3dSCy Schubert int
1622b15cb3dSCy Schubert auth_md5(
16309100258SXin LI 	void const *	pkt_data,
1642b15cb3dSCy Schubert 	int 		pkt_size,
1652b15cb3dSCy Schubert 	int		mac_size,
16609100258SXin LI 	Key_T const *	cmp_key
1672b15cb3dSCy Schubert 	)
1682b15cb3dSCy Schubert {
16909100258SXin LI 	u_int		len       = 0;
17009100258SXin LI 	u_char const *	pkt_ptr   = pkt_data;
17109100258SXin LI 	u_char		dbuf[EVP_MAX_MD_SIZE];
17209100258SXin LI 
17309100258SXin LI 	if (mac_size <= 0 || (size_t)mac_size > sizeof(dbuf))
17409100258SXin LI 		return FALSE;
17509100258SXin LI 
17609100258SXin LI 	len = compute_mac(dbuf, cmp_key->typen,
17709100258SXin LI 			  pkt_ptr, (u_int)pkt_size,
17809100258SXin LI 			  cmp_key->key_seq, (u_int)cmp_key->key_len);
17909100258SXin LI 
18009100258SXin LI 	pkt_ptr += pkt_size + 4;
18109100258SXin LI 	if (len > (u_int)mac_size)
18209100258SXin LI 		len = (u_int)mac_size;
18309100258SXin LI 
18409100258SXin LI 	/* isc_tsmemcmp will be better when its easy to link with.  sntp
18509100258SXin LI 	 * is a 1-shot program, so snooping for timing attacks is
18609100258SXin LI 	 * Harder.
1874990d495SXin LI 	 */
18809100258SXin LI 	return ((u_int)mac_size == len) && !memcmp(dbuf, pkt_ptr, len);
1892b15cb3dSCy Schubert }
1902b15cb3dSCy Schubert 
1912b15cb3dSCy Schubert static int
1922b15cb3dSCy Schubert hex_val(
1932b15cb3dSCy Schubert 	unsigned char x
1942b15cb3dSCy Schubert 	)
1952b15cb3dSCy Schubert {
1962b15cb3dSCy Schubert 	int val;
1972b15cb3dSCy Schubert 
1982b15cb3dSCy Schubert 	if ('0' <= x && x <= '9')
1992b15cb3dSCy Schubert 		val = x - '0';
2002b15cb3dSCy Schubert 	else if ('a' <= x && x <= 'f')
2012b15cb3dSCy Schubert 		val = x - 'a' + 0xa;
2022b15cb3dSCy Schubert 	else if ('A' <= x && x <= 'F')
2032b15cb3dSCy Schubert 		val = x - 'A' + 0xA;
2042b15cb3dSCy Schubert 	else
2052b15cb3dSCy Schubert 		val = -1;
2062b15cb3dSCy Schubert 
2072b15cb3dSCy Schubert 	return val;
2082b15cb3dSCy Schubert }
2092b15cb3dSCy Schubert 
2102b15cb3dSCy Schubert /* Load keys from the specified keyfile into the key structures.
2112b15cb3dSCy Schubert  * Returns -1 if the reading failed, otherwise it returns the
2122b15cb3dSCy Schubert  * number of keys it read
2132b15cb3dSCy Schubert  */
2142b15cb3dSCy Schubert int
2152b15cb3dSCy Schubert auth_init(
2162b15cb3dSCy Schubert 	const char *keyfile,
2172b15cb3dSCy Schubert 	struct key **keys
2182b15cb3dSCy Schubert 	)
2192b15cb3dSCy Schubert {
2202b15cb3dSCy Schubert 	FILE *keyf = fopen(keyfile, "r");
2212b15cb3dSCy Schubert 	struct key *prev = NULL;
22209100258SXin LI 	int scan_cnt, line_cnt = 1;
2232b15cb3dSCy Schubert 	char kbuf[200];
2242b15cb3dSCy Schubert 	char keystring[129];
2252b15cb3dSCy Schubert 
22609100258SXin LI 	/* HMS: Is it OK to do this later, after we know we have a key file? */
22709100258SXin LI 	INIT_SSL();
22809100258SXin LI 
2292b15cb3dSCy Schubert 	if (keyf == NULL) {
2302b15cb3dSCy Schubert 		if (debug)
2312b15cb3dSCy Schubert 			printf("sntp auth_init: Couldn't open key file %s for reading!\n", keyfile);
2322b15cb3dSCy Schubert 		return -1;
2332b15cb3dSCy Schubert 	}
2342b15cb3dSCy Schubert 	if (feof(keyf)) {
2352b15cb3dSCy Schubert 		if (debug)
2362b15cb3dSCy Schubert 			printf("sntp auth_init: Key file %s is empty!\n", keyfile);
2372b15cb3dSCy Schubert 		fclose(keyf);
2382b15cb3dSCy Schubert 		return -1;
2392b15cb3dSCy Schubert 	}
2402b15cb3dSCy Schubert 	key_cnt = 0;
2412b15cb3dSCy Schubert 	while (!feof(keyf)) {
2422b15cb3dSCy Schubert 		char * octothorpe;
2432b15cb3dSCy Schubert 		struct key *act;
2442b15cb3dSCy Schubert 		int goodline = 0;
2452b15cb3dSCy Schubert 
2462b15cb3dSCy Schubert 		if (NULL == fgets(kbuf, sizeof(kbuf), keyf))
2472b15cb3dSCy Schubert 			continue;
2482b15cb3dSCy Schubert 
2492b15cb3dSCy Schubert 		kbuf[sizeof(kbuf) - 1] = '\0';
2502b15cb3dSCy Schubert 		octothorpe = strchr(kbuf, '#');
2512b15cb3dSCy Schubert 		if (octothorpe)
2522b15cb3dSCy Schubert 			*octothorpe = '\0';
2532b15cb3dSCy Schubert 		act = emalloc(sizeof(*act));
25409100258SXin LI 		/* keep width 15 = sizeof struct key.typen - 1 synced */
25509100258SXin LI 		scan_cnt = sscanf(kbuf, "%d %15s %128s",
25609100258SXin LI 					&act->key_id, act->typen, keystring);
2572b15cb3dSCy Schubert 		if (scan_cnt == 3) {
2582b15cb3dSCy Schubert 			int len = strlen(keystring);
25909100258SXin LI 			goodline = 1;	/* assume best for now */
2602b15cb3dSCy Schubert 			if (len <= 20) {
2612b15cb3dSCy Schubert 				act->key_len = len;
2622b15cb3dSCy Schubert 				memcpy(act->key_seq, keystring, len + 1);
2632b15cb3dSCy Schubert 			} else if ((len & 1) != 0) {
2642b15cb3dSCy Schubert 				goodline = 0; /* it's bad */
2652b15cb3dSCy Schubert 			} else {
2662b15cb3dSCy Schubert 				int j;
2672b15cb3dSCy Schubert 				act->key_len = len >> 1;
2682b15cb3dSCy Schubert 				for (j = 0; j < len; j+=2) {
2692b15cb3dSCy Schubert 					int val;
2702b15cb3dSCy Schubert 					val = (hex_val(keystring[j]) << 4) |
2712b15cb3dSCy Schubert 					       hex_val(keystring[j+1]);
2722b15cb3dSCy Schubert 					if (val < 0) {
2732b15cb3dSCy Schubert 						goodline = 0; /* it's bad */
2742b15cb3dSCy Schubert 						break;
2752b15cb3dSCy Schubert 					}
2762b15cb3dSCy Schubert 					act->key_seq[j>>1] = (char)val;
2772b15cb3dSCy Schubert 				}
2782b15cb3dSCy Schubert 			}
27909100258SXin LI 			act->typei = keytype_from_text(act->typen, NULL);
28009100258SXin LI 			if (0 == act->typei) {
28109100258SXin LI 				printf("%s: line %d: key %d, %s not supported - ignoring\n",
28209100258SXin LI 					keyfile, line_cnt,
28309100258SXin LI 					act->key_id, act->typen);
28409100258SXin LI 				goodline = 0; /* it's bad */
28509100258SXin LI 			}
2862b15cb3dSCy Schubert 		}
2872b15cb3dSCy Schubert 		if (goodline) {
2882b15cb3dSCy Schubert 			act->next = NULL;
2892b15cb3dSCy Schubert 			if (NULL == prev)
2902b15cb3dSCy Schubert 				*keys = act;
2912b15cb3dSCy Schubert 			else
2922b15cb3dSCy Schubert 				prev->next = act;
2932b15cb3dSCy Schubert 			prev = act;
2942b15cb3dSCy Schubert 			key_cnt++;
2952b15cb3dSCy Schubert 		} else {
29609100258SXin LI 			if (debug) {
29709100258SXin LI 				printf("auth_init: scanf %d items, skipping line %d.",
2982b15cb3dSCy Schubert 					scan_cnt, line_cnt);
29909100258SXin LI 			}
3002b15cb3dSCy Schubert 			free(act);
3012b15cb3dSCy Schubert 		}
3022b15cb3dSCy Schubert 		line_cnt++;
3032b15cb3dSCy Schubert 	}
3042b15cb3dSCy Schubert 	fclose(keyf);
3052b15cb3dSCy Schubert 
3062b15cb3dSCy Schubert 	key_ptr = *keys;
3072b15cb3dSCy Schubert 	return key_cnt;
3082b15cb3dSCy Schubert }
3092b15cb3dSCy Schubert 
3102b15cb3dSCy Schubert /* Looks for the key with keyid key_id and sets the d_key pointer to the
3112b15cb3dSCy Schubert  * address of the key. If no matching key is found the pointer is not touched.
3122b15cb3dSCy Schubert  */
3132b15cb3dSCy Schubert void
3142b15cb3dSCy Schubert get_key(
3152b15cb3dSCy Schubert 	int key_id,
3162b15cb3dSCy Schubert 	struct key **d_key
3172b15cb3dSCy Schubert 	)
3182b15cb3dSCy Schubert {
3192b15cb3dSCy Schubert 	struct key *itr_key;
3202b15cb3dSCy Schubert 
3212b15cb3dSCy Schubert 	if (key_cnt == 0)
3222b15cb3dSCy Schubert 		return;
3232b15cb3dSCy Schubert 	for (itr_key = key_ptr; itr_key; itr_key = itr_key->next) {
3242b15cb3dSCy Schubert 		if (itr_key->key_id == key_id) {
3252b15cb3dSCy Schubert 			*d_key = itr_key;
3262b15cb3dSCy Schubert 			break;
3272b15cb3dSCy Schubert 		}
3282b15cb3dSCy Schubert 	}
3292b15cb3dSCy Schubert 	return;
3302b15cb3dSCy Schubert }
331