21 if (cmp_key->key_len > 64)
23 if (pkt_size % 4 != 0)
27 key_type = keytype_from_text(cmp_key->type, NULL);
28 EVP_DigestInit(&ctx, EVP_get_digestbynid(key_type));
29 EVP_DigestUpdate(&ctx, (u_char *)cmp_key->key_seq, (u_int)cmp_key->key_len);
30 EVP_DigestUpdate(&ctx, (u_char *)pkt_data, (u_int)pkt_size);
31 EVP_DigestFinal(&ctx, (u_char *)digest, &len);
37 /* Generates a md5 digest of the key specified in keyid concatinated with the
38 * ntp packet (exluding the MAC) and compares this digest to the digest in
39 * the packet's MAC. If they're equal this function returns 1 (packet is
40 * authentic) or else 0 (not authentic).
54 if (mac_size > (int)sizeof(digest))
56 hash_len = make_mac(pkt_data, pkt_size, sizeof(digest), cmp_key,
61 authentic = !memcmp(digest, pkt_data + pkt_size + 4,
73 if ('0' <= x && x <= '9')
75 else if ('a' <= x && x <= 'f')
77 else if ('A' <= x && x <= 'F')
85 /* Load keys from the specified keyfile into the key structures.
86 * Returns -1 if the reading failed, otherwise it returns the
87 * number of keys it read
95 FILE *keyf = fopen(keyfile, "r");
96 struct key *prev = NULL;
97 int scan_cnt, line_cnt = 0;
103 printf("sntp auth_init: Couldn't open key file %s for reading!\n", keyfile);
108 printf("sntp auth_init: Key file %s is empty!\n", keyfile);
113 while (!feof(keyf)) {
118 if (NULL == fgets(kbuf, sizeof(kbuf), keyf))
121 kbuf[sizeof(kbuf) - 1] = '\0';
122 octothorpe = strchr(kbuf, '#');
125 act = emalloc(sizeof(*act));
126 scan_cnt = sscanf(kbuf, "%d %9s %128s", &act->key_id, act->type, keystring);
128 int len = strlen(keystring);
131 memcpy(act->key_seq, keystring, len + 1);
133 } else if ((len & 1) != 0) {
134 goodline = 0; /* it's bad */
138 act->key_len = len >> 1;
139 for (j = 0; j < len; j+=2) {
141 val = (hex_val(keystring[j]) << 4) |
142 hex_val(keystring[j+1]);
144 goodline = 0; /* it's bad */
147 act->key_seq[j>>1] = (char)val;
160 msyslog(LOG_DEBUG, "auth_init: scanf %d items, skipping line %d.",
172 /* Looks for the key with keyid key_id and sets the d_key pointer to the
173 * address of the key. If no matching key is found the pointer is not touched.
185 for (itr_key = key_ptr; itr_key; itr_key = itr_key->next) {
186 if (itr_key->key_id == key_id) {