Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / crypto / dist / heimdal / kdc / string2key.c
blob249ae8f4832d397a1224167c783151b5e78a7967
1 /*
2 * Copyright (c) 1997-2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "headers.h"
35 #include <getarg.h>
37 __RCSID("$Heimdal: string2key.c 19213 2006-12-04 23:36:36Z lha $"
38 "$NetBSD$");
40 int version5;
41 int version4;
42 int afs;
43 char *principal;
44 char *cell;
45 char *password;
46 const char *keytype_str = "des3-cbc-sha1";
47 int version;
48 int help;
50 struct getargs args[] = {
51 { "version5", '5', arg_flag, &version5, "Output Kerberos v5 string-to-key" },
52 { "version4", '4', arg_flag, &version4, "Output Kerberos v4 string-to-key" },
53 { "afs", 'a', arg_flag, &afs, "Output AFS string-to-key" },
54 { "cell", 'c', arg_string, &cell, "AFS cell to use", "cell" },
55 { "password", 'w', arg_string, &password, "Password to use", "password" },
56 { "principal",'p', arg_string, &principal, "Kerberos v5 principal to use", "principal" },
57 { "keytype", 'k', arg_string, &keytype_str, "Keytype" },
58 { "version", 0, arg_flag, &version, "print version" },
59 { "help", 0, arg_flag, &help, NULL }
62 int num_args = sizeof(args) / sizeof(args[0]);
64 static void
65 usage(int status)
67 arg_printusage (args, num_args, NULL, "password");
68 exit(status);
71 static void
72 tokey(krb5_context context,
73 krb5_enctype enctype,
74 const char *pw,
75 krb5_salt salt,
76 const char *label)
78 krb5_error_code ret;
79 int i;
80 krb5_keyblock key;
81 char *e;
83 ret = krb5_string_to_key_salt(context, enctype, pw, salt, &key);
84 if (ret)
85 krb5_err(context, 1, ret, "krb5_string_to_key_salt");
86 ret = krb5_enctype_to_string(context, enctype, &e);
87 if (ret)
88 krb5_err(context, 1, ret, "krb5_enctype_to_string");
89 printf(label, e);
90 printf(": ");
91 for(i = 0; i < key.keyvalue.length; i++)
92 printf("%02x", ((unsigned char*)key.keyvalue.data)[i]);
93 printf("\n");
94 krb5_free_keyblock_contents(context, &key);
95 free(e);
98 int
99 main(int argc, char **argv)
101 krb5_context context;
102 krb5_principal princ;
103 krb5_salt salt;
104 int optidx;
105 char buf[1024];
106 krb5_enctype etype;
107 krb5_error_code ret;
109 optidx = krb5_program_setup(&context, argc, argv, args, num_args, NULL);
111 if(help)
112 usage(0);
114 if(version){
115 print_version (NULL);
116 return 0;
119 argc -= optidx;
120 argv += optidx;
122 if (argc > 1)
123 usage(1);
125 if(!version5 && !version4 && !afs)
126 version5 = 1;
128 ret = krb5_string_to_enctype(context, keytype_str, &etype);
129 if(ret) {
130 krb5_keytype keytype;
131 int *etypes;
132 unsigned num;
133 char *str;
134 ret = krb5_string_to_keytype(context, keytype_str, &keytype);
135 if(ret)
136 krb5_err(context, 1, ret, "%s", keytype_str);
137 ret = krb5_keytype_to_enctypes(context, keytype, &num, &etypes);
138 if(ret)
139 krb5_err(context, 1, ret, "%s", keytype_str);
140 if(num == 0)
141 krb5_errx(context, 1, "there are no encryption types for that keytype");
142 etype = etypes[0];
143 krb5_enctype_to_string(context, etype, &str);
144 keytype_str = str;
145 if(num > 1 && version5)
146 krb5_warnx(context, "ambiguous keytype, using %s", keytype_str);
149 if((etype != ETYPE_DES_CBC_CRC &&
150 etype != ETYPE_DES_CBC_MD4 &&
151 etype != ETYPE_DES_CBC_MD5) &&
152 (afs || version4)) {
153 if(!version5) {
154 etype = ETYPE_DES_CBC_CRC;
155 } else {
156 krb5_errx(context, 1,
157 "DES is the only valid keytype for AFS and Kerberos 4");
161 if(version5 && principal == NULL){
162 printf("Kerberos v5 principal: ");
163 if(fgets(buf, sizeof(buf), stdin) == NULL)
164 return 1;
165 buf[strcspn(buf, "\r\n")] = '\0';
166 principal = estrdup(buf);
168 if(afs && cell == NULL){
169 printf("AFS cell: ");
170 if(fgets(buf, sizeof(buf), stdin) == NULL)
171 return 1;
172 buf[strcspn(buf, "\r\n")] = '\0';
173 cell = estrdup(buf);
175 if(argv[0])
176 password = argv[0];
177 if(password == NULL){
178 if(UI_UTIL_read_pw_string(buf, sizeof(buf), "Password: ", 0))
179 return 1;
180 password = buf;
183 if(version5){
184 krb5_parse_name(context, principal, &princ);
185 krb5_get_pw_salt(context, princ, &salt);
186 tokey(context, etype, password, salt, "Kerberos 5 (%s)");
187 krb5_free_salt(context, salt);
189 if(version4){
190 salt.salttype = KRB5_PW_SALT;
191 salt.saltvalue.length = 0;
192 salt.saltvalue.data = NULL;
193 tokey(context, ETYPE_DES_CBC_MD5, password, salt, "Kerberos 4");
195 if(afs){
196 salt.salttype = KRB5_AFS3_SALT;
197 salt.saltvalue.length = strlen(cell);
198 salt.saltvalue.data = cell;
199 tokey(context, ETYPE_DES_CBC_MD5, password, salt, "AFS");
201 return 0;