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);
75 /* Test our AES-128-CMAC implementation */
77 } else /* MD5 MAC handling */
82 if (!(ctx = EVP_MD_CTX_new())) {
83 msyslog(LOG_ERR, "make_mac: MAC %s Digest CTX new failed.",
87 #ifdef OPENSSL /* OpenSSL 1 supports return codes 0 fail, 1 okay */
88 # ifdef EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
89 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
91 /* [Bug 3457] DON'T use plain EVP_DigestInit! It would
93 if (!EVP_DigestInit_ex(ctx, EVP_get_digestbynid(key_type), NULL)) {
94 msyslog(LOG_ERR, "make_mac: MAC %s Digest Init failed.",
98 if (!EVP_DigestUpdate(ctx, key_data, key_size)) {
99 msyslog(LOG_ERR, "make_mac: MAC %s Digest Update key failed.",
103 if (!EVP_DigestUpdate(ctx, pkt_data, pkt_size)) {
104 msyslog(LOG_ERR, "make_mac: MAC %s Digest Update data failed.",
108 if (!EVP_DigestFinal(ctx, digest, &len)) {
109 msyslog(LOG_ERR, "make_mac: MAC %s Digest Final failed.",
114 EVP_DigestInit(ctx, EVP_get_digestbynid(key_type));
115 EVP_DigestUpdate(ctx, key_data, key_size);
116 EVP_DigestUpdate(ctx, pkt_data, pkt_size);
117 EVP_DigestFinal(ctx, digest, &len);
120 EVP_MD_CTX_free(ctx);
128 const void * pkt_data,
131 Key_T const * cmp_key,
136 u_char dbuf[EVP_MAX_MD_SIZE];
138 if (cmp_key->key_len > 64 || mac_size <= 0)
140 if (pkt_size % 4 != 0)
143 len = compute_mac(dbuf, cmp_key->typen,
144 pkt_data, (u_int)pkt_size,
145 cmp_key->key_seq, (u_int)cmp_key->key_len);
149 if (len > (u_int)mac_size)
150 len = (u_int)mac_size;
151 memcpy(digest, dbuf, len);
157 /* Generates a md5 digest of the key specified in keyid concatenated with the
158 * ntp packet (exluding the MAC) and compares this digest to the digest in
159 * the packet's MAC. If they're equal this function returns 1 (packet is
160 * authentic) or else 0 (not authentic).
164 void const * pkt_data,
167 Key_T const * cmp_key
171 u_char const * pkt_ptr = pkt_data;
172 u_char dbuf[EVP_MAX_MD_SIZE];
174 if (mac_size <= 0 || (size_t)mac_size > sizeof(dbuf))
177 len = compute_mac(dbuf, cmp_key->typen,
178 pkt_ptr, (u_int)pkt_size,
179 cmp_key->key_seq, (u_int)cmp_key->key_len);
181 pkt_ptr += pkt_size + 4;
182 if (len > (u_int)mac_size)
183 len = (u_int)mac_size;
185 /* isc_tsmemcmp will be better when its easy to link with. sntp
186 * is a 1-shot program, so snooping for timing attacks is
189 return ((u_int)mac_size == len) && !memcmp(dbuf, pkt_ptr, len);
199 if ('0' <= x && x <= '9')
201 else if ('a' <= x && x <= 'f')
203 else if ('A' <= x && x <= 'F')
211 /* Load keys from the specified keyfile into the key structures.
212 * Returns -1 if the reading failed, otherwise it returns the
213 * number of keys it read
221 FILE *keyf = fopen(keyfile, "r");
222 struct key *prev = NULL;
223 int scan_cnt, line_cnt = 1;
227 /* HMS: Is it OK to do this later, after we know we have a key file? */
232 printf("sntp auth_init: Couldn't open key file %s for reading!\n", keyfile);
237 printf("sntp auth_init: Key file %s is empty!\n", keyfile);
242 while (!feof(keyf)) {
247 if (NULL == fgets(kbuf, sizeof(kbuf), keyf))
250 kbuf[sizeof(kbuf) - 1] = '\0';
251 octothorpe = strchr(kbuf, '#');
254 act = emalloc(sizeof(*act));
255 /* keep width 15 = sizeof struct key.typen - 1 synced */
256 scan_cnt = sscanf(kbuf, "%d %15s %128s",
257 &act->key_id, act->typen, keystring);
259 int len = strlen(keystring);
260 goodline = 1; /* assume best for now */
263 memcpy(act->key_seq, keystring, len + 1);
264 } else if ((len & 1) != 0) {
265 goodline = 0; /* it's bad */
268 act->key_len = len >> 1;
269 for (j = 0; j < len; j+=2) {
271 val = (hex_val(keystring[j]) << 4) |
272 hex_val(keystring[j+1]);
274 goodline = 0; /* it's bad */
277 act->key_seq[j>>1] = (char)val;
280 act->typei = keytype_from_text(act->typen, NULL);
281 if (0 == act->typei) {
282 printf("%s: line %d: key %d, %s not supported - ignoring\n",
284 act->key_id, act->typen);
285 goodline = 0; /* it's bad */
298 printf("auth_init: scanf %d items, skipping line %d.",
311 /* Looks for the key with keyid key_id and sets the d_key pointer to the
312 * address of the key. If no matching key is found the pointer is not touched.
324 for (itr_key = key_ptr; itr_key; itr_key = itr_key->next) {
325 if (itr_key->key_id == key_id) {