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