xref: /freebsd-src/crypto/heimdal/kdc/string2key.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov  * Copyright (c) 1997-2003 Kungliga Tekniska Högskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
19b528cefcSMark Murray  *    without specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray  * SUCH DAMAGE.
32b528cefcSMark Murray  */
33b528cefcSMark Murray 
34b528cefcSMark Murray #include "headers.h"
35b528cefcSMark Murray #include <getarg.h>
36b528cefcSMark Murray 
37b528cefcSMark Murray int version5;
38b528cefcSMark Murray int version4;
39b528cefcSMark Murray int afs;
40b528cefcSMark Murray char *principal;
41b528cefcSMark Murray char *cell;
42b528cefcSMark Murray char *password;
43bbd80c28SJacques Vidrine const char *keytype_str = "des3-cbc-sha1";
44b528cefcSMark Murray int version;
45b528cefcSMark Murray int help;
46b528cefcSMark Murray 
47b528cefcSMark Murray struct getargs args[] = {
48*ae771770SStanislav Sedov     { "version5", '5', arg_flag,   &version5, "Output Kerberos v5 string-to-key",
49*ae771770SStanislav Sedov 	NULL },
50*ae771770SStanislav Sedov     { "version4", '4', arg_flag,   &version4, "Output Kerberos v4 string-to-key",
51*ae771770SStanislav Sedov 	NULL },
52*ae771770SStanislav Sedov     { "afs",      'a', arg_flag,   &afs, "Output AFS string-to-key", NULL },
53b528cefcSMark Murray     { "cell",     'c', arg_string, &cell, "AFS cell to use", "cell" },
54b528cefcSMark Murray     { "password", 'w', arg_string, &password, "Password to use", "password" },
55b528cefcSMark Murray     { "principal",'p', arg_string, &principal, "Kerberos v5 principal to use", "principal" },
56*ae771770SStanislav Sedov     { "keytype",  'k', arg_string, rk_UNCONST(&keytype_str), "Keytype", NULL },
57*ae771770SStanislav Sedov     { "version",    0, arg_flag,   &version, "print version", NULL },
58*ae771770SStanislav Sedov     { "help",       0, arg_flag,   &help, NULL, NULL }
59b528cefcSMark Murray };
60b528cefcSMark Murray 
61b528cefcSMark Murray int num_args = sizeof(args) / sizeof(args[0]);
62b528cefcSMark Murray 
63b528cefcSMark Murray static void
usage(int status)64b528cefcSMark Murray usage(int status)
65b528cefcSMark Murray {
66b528cefcSMark Murray     arg_printusage (args, num_args, NULL, "password");
67b528cefcSMark Murray     exit(status);
68b528cefcSMark Murray }
69b528cefcSMark Murray 
70b528cefcSMark Murray static void
tokey(krb5_context context,krb5_enctype enctype,const char * pw,krb5_salt salt,const char * label)71b528cefcSMark Murray tokey(krb5_context context,
72b528cefcSMark Murray       krb5_enctype enctype,
73c19800e8SDoug Rabson       const char *pw,
74b528cefcSMark Murray       krb5_salt salt,
75b528cefcSMark Murray       const char *label)
76b528cefcSMark Murray {
77c19800e8SDoug Rabson     krb5_error_code ret;
78*ae771770SStanislav Sedov     size_t i;
79b528cefcSMark Murray     krb5_keyblock key;
80bbd80c28SJacques Vidrine     char *e;
81c19800e8SDoug Rabson 
82c19800e8SDoug Rabson     ret = krb5_string_to_key_salt(context, enctype, pw, salt, &key);
83c19800e8SDoug Rabson     if (ret)
84c19800e8SDoug Rabson 	krb5_err(context, 1, ret, "krb5_string_to_key_salt");
85c19800e8SDoug Rabson     ret = krb5_enctype_to_string(context, enctype, &e);
86c19800e8SDoug Rabson     if (ret)
87c19800e8SDoug Rabson 	krb5_err(context, 1, ret, "krb5_enctype_to_string");
88bbd80c28SJacques Vidrine     printf(label, e);
89bbd80c28SJacques Vidrine     printf(": ");
90b528cefcSMark Murray     for(i = 0; i < key.keyvalue.length; i++)
91b528cefcSMark Murray 	printf("%02x", ((unsigned char*)key.keyvalue.data)[i]);
92b528cefcSMark Murray     printf("\n");
93b528cefcSMark Murray     krb5_free_keyblock_contents(context, &key);
94c19800e8SDoug Rabson     free(e);
95b528cefcSMark Murray }
96b528cefcSMark Murray 
97b528cefcSMark Murray int
main(int argc,char ** argv)98b528cefcSMark Murray main(int argc, char **argv)
99b528cefcSMark Murray {
100b528cefcSMark Murray     krb5_context context;
101b528cefcSMark Murray     krb5_principal princ;
102b528cefcSMark Murray     krb5_salt salt;
103c19800e8SDoug Rabson     int optidx;
104b528cefcSMark Murray     char buf[1024];
105b528cefcSMark Murray     krb5_enctype etype;
106b528cefcSMark Murray     krb5_error_code ret;
107b528cefcSMark Murray 
108c19800e8SDoug Rabson     optidx = krb5_program_setup(&context, argc, argv, args, num_args, NULL);
109b528cefcSMark Murray 
110b528cefcSMark Murray     if(help)
111b528cefcSMark Murray 	usage(0);
112b528cefcSMark Murray 
113b528cefcSMark Murray     if(version){
114b528cefcSMark Murray 	print_version (NULL);
115b528cefcSMark Murray 	return 0;
116b528cefcSMark Murray     }
117b528cefcSMark Murray 
118c19800e8SDoug Rabson     argc -= optidx;
119c19800e8SDoug Rabson     argv += optidx;
120b528cefcSMark Murray 
121b528cefcSMark Murray     if (argc > 1)
122b528cefcSMark Murray 	usage(1);
123b528cefcSMark Murray 
124b528cefcSMark Murray     if(!version5 && !version4 && !afs)
125b528cefcSMark Murray 	version5 = 1;
126b528cefcSMark Murray 
127b528cefcSMark Murray     ret = krb5_string_to_enctype(context, keytype_str, &etype);
128b528cefcSMark Murray     if(ret)
129*ae771770SStanislav Sedov 	krb5_err(context, 1, ret, "krb5_string_to_enctype");
130b528cefcSMark Murray 
131b528cefcSMark Murray     if((etype != ETYPE_DES_CBC_CRC &&
132b528cefcSMark Murray 	etype != ETYPE_DES_CBC_MD4 &&
133b528cefcSMark Murray 	etype != ETYPE_DES_CBC_MD5) &&
134bbd80c28SJacques Vidrine        (afs || version4)) {
135bbd80c28SJacques Vidrine 	if(!version5) {
136bbd80c28SJacques Vidrine 	    etype = ETYPE_DES_CBC_CRC;
137bbd80c28SJacques Vidrine 	} else {
138b528cefcSMark Murray 	    krb5_errx(context, 1,
139b528cefcSMark Murray 		      "DES is the only valid keytype for AFS and Kerberos 4");
140bbd80c28SJacques Vidrine 	}
141bbd80c28SJacques Vidrine     }
142b528cefcSMark Murray 
143b528cefcSMark Murray     if(version5 && principal == NULL){
144b528cefcSMark Murray 	printf("Kerberos v5 principal: ");
145b528cefcSMark Murray 	if(fgets(buf, sizeof(buf), stdin) == NULL)
146b528cefcSMark Murray 	    return 1;
147c19800e8SDoug Rabson 	buf[strcspn(buf, "\r\n")] = '\0';
148b528cefcSMark Murray 	principal = estrdup(buf);
149b528cefcSMark Murray     }
150b528cefcSMark Murray     if(afs && cell == NULL){
151b528cefcSMark Murray 	printf("AFS cell: ");
152b528cefcSMark Murray 	if(fgets(buf, sizeof(buf), stdin) == NULL)
153b528cefcSMark Murray 	    return 1;
154c19800e8SDoug Rabson 	buf[strcspn(buf, "\r\n")] = '\0';
155b528cefcSMark Murray 	cell = estrdup(buf);
156b528cefcSMark Murray     }
157b528cefcSMark Murray     if(argv[0])
158b528cefcSMark Murray 	password = argv[0];
159b528cefcSMark Murray     if(password == NULL){
160c19800e8SDoug Rabson 	if(UI_UTIL_read_pw_string(buf, sizeof(buf), "Password: ", 0))
161b528cefcSMark Murray 	    return 1;
162b528cefcSMark Murray 	password = buf;
163b528cefcSMark Murray     }
164b528cefcSMark Murray 
165b528cefcSMark Murray     if(version5){
166b528cefcSMark Murray 	krb5_parse_name(context, principal, &princ);
167b528cefcSMark Murray 	krb5_get_pw_salt(context, princ, &salt);
168bbd80c28SJacques Vidrine 	tokey(context, etype, password, salt, "Kerberos 5 (%s)");
169b528cefcSMark Murray 	krb5_free_salt(context, salt);
170b528cefcSMark Murray     }
171b528cefcSMark Murray     if(version4){
172b528cefcSMark Murray 	salt.salttype = KRB5_PW_SALT;
173b528cefcSMark Murray 	salt.saltvalue.length = 0;
174b528cefcSMark Murray 	salt.saltvalue.data = NULL;
175bbd80c28SJacques Vidrine 	tokey(context, ETYPE_DES_CBC_MD5, password, salt, "Kerberos 4");
176b528cefcSMark Murray     }
177b528cefcSMark Murray     if(afs){
178b528cefcSMark Murray 	salt.salttype = KRB5_AFS3_SALT;
179b528cefcSMark Murray 	salt.saltvalue.length = strlen(cell);
180b528cefcSMark Murray 	salt.saltvalue.data = cell;
181bbd80c28SJacques Vidrine 	tokey(context, ETYPE_DES_CBC_MD5, password, salt, "AFS");
182b528cefcSMark Murray     }
183b528cefcSMark Murray     return 0;
184b528cefcSMark Murray }
185