xref: /freebsd/contrib/ntp/libntp/authusekey.c (revision e0c4386e)
1 /*
2  * authusekey - decode a key from ascii and use it
3  */
4 #include <config.h>
5 #include <stdio.h>
6 #include <ctype.h>
7 
8 #include "ntp_types.h"
9 #include "ntp_string.h"
10 #include "ntp_stdlib.h"
11 
12 /*
13  * Only used by ntp{q,dc} to set the key/algo/secret triple to use.
14  * Uses the same decoding scheme ntpd uses for keys in the key file.
15  */
16 int
17 authusekey(
18 	keyid_t keyno,
19 	int keytype,
20 	const u_char *str
21 	)
22 {
23 	size_t	len;
24 	u_char	buf[AUTHPWD_MAXSECLEN];
25 
26 	len = authdecodepw(buf, sizeof(buf), (const char*)str,
27 			   AUTHPWD_UNSPEC);
28 	if (len < 1 || len > sizeof(buf))
29 		return 0;
30 
31 	MD5auth_setkey(keyno, keytype, buf, len, NULL);
32 	memset(buf, 0, sizeof(buf));
33 	return 1;
34 }
35