8 /* Generates a md5 digest of the ntp packet (exluding the MAC) concatinated
9 * with the key specified in keyid and compares this digest to the digest in
10 * the packet's MAC. If they're equal this function returns 1 (packet is
11 * authentic) or else 0 (not authentic).
25 if (cmp_key
->type
!= 'M')
30 digest_data
= emalloc(sizeof(char) * (LEN_PKT_NOMAC
+ cmp_key
->key_len
));
32 for (a
= 0; a
< LEN_PKT_NOMAC
; a
++)
33 digest_data
[a
] = pkt_data
[a
];
35 for (a
= 0; a
< cmp_key
->key_len
; a
++)
36 digest_data
[LEN_PKT_NOMAC
+ a
] = cmp_key
->key_seq
[a
];
38 MD5Update(&ctx
, (u_char
*)digest_data
, LEN_PKT_NOMAC
+ cmp_key
->key_len
);
39 MD5Final((u_char
*)digest
, &ctx
);
43 for (a
= 0; a
< 16; a
++)
44 if (digest
[a
] != pkt_data
[LEN_PKT_MAC
+ a
])
50 /* Load keys from the specified keyfile into the key structures.
51 * Returns -1 if the reading failed, otherwise it returns the
52 * number of keys it read
60 FILE *keyf
= fopen(keyfile
, "r");
61 struct key
*prev
= NULL
;
62 register int a
, line_limit
;
63 int scan_cnt
, line_cnt
= 0;
67 if (ENABLED_OPT(NORMALVERBOSE
))
68 printf("sntp auth_init: Couldn't open key file %s for reading!\n", keyfile
);
76 if (ENABLED_OPT(NORMALVERBOSE
))
77 printf("sntp auth_init: Key file %s is empty!\n", keyfile
);
84 struct key
*act
= emalloc(sizeof(struct key
));
87 fgets(kbuf
, sizeof(kbuf
), keyf
);
89 for (a
= 0; a
< strlen(kbuf
) && a
< sizeof(kbuf
); a
++) {
97 kbuf
[line_limit
] = '\0';
100 printf("sntp auth_init: fgets: %s", kbuf
);
104 if ((scan_cnt
= sscanf(kbuf
, "%i %c %16s", &act
->key_id
, &act
->type
, act
->key_seq
)) == 3) {
105 act
->key_len
= strlen(act
->key_seq
);
117 printf("sntp auth_init: key_id %i type %c with key %s\n", act
->key_id
, act
->type
, act
->key_seq
);
121 printf("sntp auth_init: scanf read %i items, doesn't look good, skipping line %i.\n", scan_cnt
, line_cnt
);
134 printf("sntp auth_init: Read %i keys from file %s:\n", line_cnt
, keyfile
);
137 struct key
*kptr
= *keys
;
139 for (a
= 0; a
< key_cnt
; a
++) {
140 printf("key_id %i type %c with key %s (key length: %i)\n",
141 kptr
->key_id
, kptr
->type
, kptr
->key_seq
, kptr
->key_len
);
154 /* Looks for the key with keyid key_id and sets the d_key pointer to the
155 * address of the key. If no matching key is found the pointer is not touched.
164 struct key
*itr_key
= key_ptr
;
169 for (a
= 0; a
< key_cnt
&& itr_key
!= NULL
; a
++) {
170 if (itr_key
->key_id
== key_id
) {