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