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