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,
35 #if defined(OPENSSL) && defined(ENABLE_CMAC)
41 key_type = keytype_from_text(macname, NULL);
43 #if defined(OPENSSL) && defined(ENABLE_CMAC)
44 /* Check if CMAC key type specific code required */
45 if (key_type == NID_cmac) {
46 CMAC_CTX * ctx = NULL;
47 u_char keybuf[AES_128_KEY_SIZE];
49 /* adjust key size (zero padded buffer) if necessary */
50 if (AES_128_KEY_SIZE > key_size) {
51 memcpy(keybuf, key_data, key_size);
52 memset((keybuf + key_size), 0,
53 (AES_128_KEY_SIZE - key_size));
57 if (!(ctx = CMAC_CTX_new())) {
58 msyslog(LOG_ERR, "make_mac: CMAC %s CTX new failed.", CMAC);
60 else if (!CMAC_Init(ctx, key_data, AES_128_KEY_SIZE,
61 EVP_aes_128_cbc(), NULL)) {
62 msyslog(LOG_ERR, "make_mac: CMAC %s Init failed.", CMAC);
64 else if (!CMAC_Update(ctx, pkt_data, (size_t)pkt_size)) {
65 msyslog(LOG_ERR, "make_mac: CMAC %s Update failed.", CMAC);
67 else if (!CMAC_Final(ctx, digest, &slen)) {
68 msyslog(LOG_ERR, "make_mac: CMAC %s Final failed.", CMAC);
73 CMAC_CTX_cleanup(ctx);
74 /* Test our AES-128-CMAC implementation */
76 } else /* MD5 MAC handling */
81 if (!(ctx = EVP_MD_CTX_new())) {
82 msyslog(LOG_ERR, "make_mac: MAC %s Digest CTX new failed.",
86 #ifdef OPENSSL /* OpenSSL 1 supports return codes 0 fail, 1 okay */
87 # ifdef EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
88 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
90 /* [Bug 3457] DON'T use plain EVP_DigestInit! It would
92 if (!EVP_DigestInit_ex(ctx, EVP_get_digestbynid(key_type), NULL)) {
93 msyslog(LOG_ERR, "make_mac: MAC %s Digest Init failed.",
97 if (!EVP_DigestUpdate(ctx, key_data, key_size)) {
98 msyslog(LOG_ERR, "make_mac: MAC %s Digest Update key failed.",
102 if (!EVP_DigestUpdate(ctx, pkt_data, pkt_size)) {
103 msyslog(LOG_ERR, "make_mac: MAC %s Digest Update data failed.",
107 if (!EVP_DigestFinal(ctx, digest, &len)) {
108 msyslog(LOG_ERR, "make_mac: MAC %s Digest Final failed.",
113 EVP_DigestInit(ctx, EVP_get_digestbynid(key_type));
114 EVP_DigestUpdate(ctx, key_data, key_size);
115 EVP_DigestUpdate(ctx, pkt_data, pkt_size);
116 EVP_DigestFinal(ctx, digest, &len);
119 EVP_MD_CTX_free(ctx);
127 const void * pkt_data,
130 Key_T const * cmp_key,
135 u_char dbuf[EVP_MAX_MD_SIZE];
137 if (cmp_key->key_len > 64 || mac_size <= 0)
139 if (pkt_size % 4 != 0)
142 len = compute_mac(dbuf, cmp_key->typen,
143 pkt_data, (u_int)pkt_size,
144 cmp_key->key_seq, (u_int)cmp_key->key_len);
148 if (len > (u_int)mac_size)
149 len = (u_int)mac_size;
150 memcpy(digest, dbuf, len);
156 /* Generates a md5 digest of the key specified in keyid concatenated with the
157 * ntp packet (exluding the MAC) and compares this digest to the digest in
158 * the packet's MAC. If they're equal this function returns 1 (packet is
159 * authentic) or else 0 (not authentic).
163 void const * pkt_data,
166 Key_T const * cmp_key
170 u_char const * pkt_ptr = pkt_data;
171 u_char dbuf[EVP_MAX_MD_SIZE];
173 if (mac_size <= 0 || (size_t)mac_size > sizeof(dbuf))
176 len = compute_mac(dbuf, cmp_key->typen,
177 pkt_ptr, (u_int)pkt_size,
178 cmp_key->key_seq, (u_int)cmp_key->key_len);
180 pkt_ptr += pkt_size + 4;
181 if (len > (u_int)mac_size)
182 len = (u_int)mac_size;
184 /* isc_tsmemcmp will be better when its easy to link with. sntp
185 * is a 1-shot program, so snooping for timing attacks is
188 return ((u_int)mac_size == len) && !memcmp(dbuf, pkt_ptr, len);
198 if ('0' <= x && x <= '9')
200 else if ('a' <= x && x <= 'f')
202 else if ('A' <= x && x <= 'F')
210 /* Load keys from the specified keyfile into the key structures.
211 * Returns -1 if the reading failed, otherwise it returns the
212 * number of keys it read
220 FILE *keyf = fopen(keyfile, "r");
221 struct key *prev = NULL;
222 int scan_cnt, line_cnt = 1;
226 /* HMS: Is it OK to do this later, after we know we have a key file? */
231 printf("sntp auth_init: Couldn't open key file %s for reading!\n", keyfile);
236 printf("sntp auth_init: Key file %s is empty!\n", keyfile);
241 while (!feof(keyf)) {
246 if (NULL == fgets(kbuf, sizeof(kbuf), keyf))
249 kbuf[sizeof(kbuf) - 1] = '\0';
250 octothorpe = strchr(kbuf, '#');
253 act = emalloc(sizeof(*act));
254 /* keep width 15 = sizeof struct key.typen - 1 synced */
255 scan_cnt = sscanf(kbuf, "%d %15s %128s",
256 &act->key_id, act->typen, keystring);
258 int len = strlen(keystring);
259 goodline = 1; /* assume best for now */
262 memcpy(act->key_seq, keystring, len + 1);
263 } else if ((len & 1) != 0) {
264 goodline = 0; /* it's bad */
267 act->key_len = len >> 1;
268 for (j = 0; j < len; j+=2) {
270 val = (hex_val(keystring[j]) << 4) |
271 hex_val(keystring[j+1]);
273 goodline = 0; /* it's bad */
276 act->key_seq[j>>1] = (char)val;
279 act->typei = keytype_from_text(act->typen, NULL);
280 if (0 == act->typei) {
281 printf("%s: line %d: key %d, %s not supported - ignoring\n",
283 act->key_id, act->typen);
284 goodline = 0; /* it's bad */
297 printf("auth_init: scanf %d items, skipping line %d.",
310 /* Looks for the key with keyid key_id and sets the d_key pointer to the
311 * address of the key. If no matching key is found the pointer is not touched.
323 for (itr_key = key_ptr; itr_key; itr_key = itr_key->next) {
324 if (itr_key->key_id == key_id) {