2 * HMS: we need to test:
3 * - OpenSSL versions, if we are building with them
6 * We may need to test with(out) OPENSSL separately.
12 #include "isc/string.h"
15 #ifndef EVP_MAX_MD_SIZE
16 # define EVP_MAX_MD_SIZE 32
22 typedef struct key Key_T;
26 u_char digest[EVP_MAX_MD_SIZE],
28 void const * pkt_data,
30 void const * key_data,
39 key_type = keytype_from_text(macname, NULL);
41 #if defined(OPENSSL) && defined(ENABLE_CMAC)
42 /* Check if CMAC key type specific code required */
43 if (key_type == NID_cmac) {
44 CMAC_CTX * ctx = NULL;
45 u_char keybuf[AES_128_KEY_SIZE];
47 /* adjust key size (zero padded buffer) if necessary */
48 if (AES_128_KEY_SIZE > key_size) {
49 memcpy(keybuf, key_data, key_size);
50 memset((keybuf + key_size), 0,
51 (AES_128_KEY_SIZE - key_size));
55 if (!(ctx = CMAC_CTX_new())) {
56 msyslog(LOG_ERR, "make_mac: CMAC %s CTX new failed.", CMAC);
58 else if (!CMAC_Init(ctx, key_data, AES_128_KEY_SIZE,
59 EVP_aes_128_cbc(), NULL)) {
60 msyslog(LOG_ERR, "make_mac: CMAC %s Init failed.", CMAC);
62 else if (!CMAC_Update(ctx, pkt_data, (size_t)pkt_size)) {
63 msyslog(LOG_ERR, "make_mac: CMAC %s Update failed.", CMAC);
65 else if (!CMAC_Final(ctx, digest, &slen)) {
66 msyslog(LOG_ERR, "make_mac: CMAC %s Final failed.", CMAC);
71 CMAC_CTX_cleanup(ctx);
72 /* Test our AES-128-CMAC implementation */
74 } else /* MD5 MAC handling */
79 if (!(ctx = EVP_MD_CTX_new())) {
80 msyslog(LOG_ERR, "make_mac: MAC %s Digest CTX new failed.",
84 #ifdef OPENSSL /* OpenSSL 1 supports return codes 0 fail, 1 okay */
85 # ifdef EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
86 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
88 /* [Bug 3457] DON'T use plain EVP_DigestInit! It would
90 if (!EVP_DigestInit_ex(ctx, EVP_get_digestbynid(key_type), NULL)) {
91 msyslog(LOG_ERR, "make_mac: MAC %s Digest Init failed.",
95 if (!EVP_DigestUpdate(ctx, key_data, key_size)) {
96 msyslog(LOG_ERR, "make_mac: MAC %s Digest Update key failed.",
100 if (!EVP_DigestUpdate(ctx, pkt_data, pkt_size)) {
101 msyslog(LOG_ERR, "make_mac: MAC %s Digest Update data failed.",
105 if (!EVP_DigestFinal(ctx, digest, &len)) {
106 msyslog(LOG_ERR, "make_mac: MAC %s Digest Final failed.",
111 EVP_DigestInit(ctx, EVP_get_digestbynid(key_type));
112 EVP_DigestUpdate(ctx, key_data, key_size);
113 EVP_DigestUpdate(ctx, pkt_data, pkt_size);
114 EVP_DigestFinal(ctx, digest, &len);
117 EVP_MD_CTX_free(ctx);
125 const void * pkt_data,
128 Key_T const * cmp_key,
133 u_char dbuf[EVP_MAX_MD_SIZE];
135 if (cmp_key->key_len > 64 || mac_size <= 0)
137 if (pkt_size % 4 != 0)
140 len = compute_mac(dbuf, cmp_key->typen,
141 pkt_data, (u_int)pkt_size,
142 cmp_key->key_seq, (u_int)cmp_key->key_len);
146 if (len > (u_int)mac_size)
147 len = (u_int)mac_size;
148 memcpy(digest, dbuf, len);
154 /* Generates a md5 digest of the key specified in keyid concatenated with the
155 * ntp packet (exluding the MAC) and compares this digest to the digest in
156 * the packet's MAC. If they're equal this function returns 1 (packet is
157 * authentic) or else 0 (not authentic).
161 void const * pkt_data,
164 Key_T const * cmp_key
168 u_char const * pkt_ptr = pkt_data;
169 u_char dbuf[EVP_MAX_MD_SIZE];
171 if (mac_size <= 0 || (size_t)mac_size > sizeof(dbuf))
174 len = compute_mac(dbuf, cmp_key->typen,
175 pkt_ptr, (u_int)pkt_size,
176 cmp_key->key_seq, (u_int)cmp_key->key_len);
178 pkt_ptr += pkt_size + 4;
179 if (len > (u_int)mac_size)
180 len = (u_int)mac_size;
182 /* isc_tsmemcmp will be better when its easy to link with. sntp
183 * is a 1-shot program, so snooping for timing attacks is
186 return ((u_int)mac_size == len) && !memcmp(dbuf, pkt_ptr, len);
196 if ('0' <= x && x <= '9')
198 else if ('a' <= x && x <= 'f')
200 else if ('A' <= x && x <= 'F')
208 /* Load keys from the specified keyfile into the key structures.
209 * Returns -1 if the reading failed, otherwise it returns the
210 * number of keys it read
218 FILE *keyf = fopen(keyfile, "r");
219 struct key *prev = NULL;
220 int scan_cnt, line_cnt = 1;
224 /* HMS: Is it OK to do this later, after we know we have a key file? */
229 printf("sntp auth_init: Couldn't open key file %s for reading!\n", keyfile);
234 printf("sntp auth_init: Key file %s is empty!\n", keyfile);
239 while (!feof(keyf)) {
244 if (NULL == fgets(kbuf, sizeof(kbuf), keyf))
247 kbuf[sizeof(kbuf) - 1] = '\0';
248 octothorpe = strchr(kbuf, '#');
251 act = emalloc(sizeof(*act));
252 /* keep width 15 = sizeof struct key.typen - 1 synced */
253 scan_cnt = sscanf(kbuf, "%d %15s %128s",
254 &act->key_id, act->typen, keystring);
256 int len = strlen(keystring);
257 goodline = 1; /* assume best for now */
260 memcpy(act->key_seq, keystring, len + 1);
261 } else if ((len & 1) != 0) {
262 goodline = 0; /* it's bad */
265 act->key_len = len >> 1;
266 for (j = 0; j < len; j+=2) {
268 val = (hex_val(keystring[j]) << 4) |
269 hex_val(keystring[j+1]);
271 goodline = 0; /* it's bad */
274 act->key_seq[j>>1] = (char)val;
277 act->typei = keytype_from_text(act->typen, NULL);
278 if (0 == act->typei) {
279 printf("%s: line %d: key %d, %s not supported - ignoring\n",
281 act->key_id, act->typen);
282 goodline = 0; /* it's bad */
295 printf("auth_init: scanf %d items, skipping line %d.",
308 /* Looks for the key with keyid key_id and sets the d_key pointer to the
309 * address of the key. If no matching key is found the pointer is not touched.
321 for (itr_key = key_ptr; itr_key; itr_key = itr_key->next) {
322 if (itr_key->key_id == key_id) {