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