Merge from vendor branch NCURSES:
[dragonfly.git] / crypto / heimdal-0.6.3 / kdc / string2key.c
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
37 RCSID("$Id: string2key.c,v 1.20 2003/03/25 12:28:52 joda Exp $");
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     { "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
61 int num_args = sizeof(args) / sizeof(args[0]);
62
63 static void
64 usage(int status)
65 {
66     arg_printusage (args, num_args, NULL, "password");
67     exit(status);
68 }
69
70 static void
71 tokey(krb5_context context, 
72       krb5_enctype enctype, 
73       const char *password, 
74       krb5_salt salt, 
75       const char *label)
76 {
77     int i;
78     krb5_keyblock key;
79     char *e;
80     krb5_string_to_key_salt(context, enctype, password, salt, &key);
81     krb5_enctype_to_string(context, enctype, &e);
82     printf(label, e);
83     printf(": ");
84     for(i = 0; i < key.keyvalue.length; i++)
85         printf("%02x", ((unsigned char*)key.keyvalue.data)[i]);
86     printf("\n");
87     krb5_free_keyblock_contents(context, &key);
88 }
89
90 int
91 main(int argc, char **argv)
92 {
93     krb5_context context;
94     krb5_principal princ;
95     krb5_salt salt;
96     int optind;
97     char buf[1024];
98     krb5_enctype etype;
99     krb5_error_code ret;
100
101     optind = krb5_program_setup(&context, argc, argv, args, num_args, NULL);
102
103     if(help)
104         usage(0);
105     
106     if(version){
107         print_version (NULL);
108         return 0;
109     }
110
111     argc -= optind;
112     argv += optind;
113
114     if (argc > 1)
115         usage(1);
116
117     if(!version5 && !version4 && !afs)
118         version5 = 1;
119
120     ret = krb5_string_to_enctype(context, keytype_str, &etype);
121     if(ret) {
122         krb5_keytype keytype;
123         int *etypes;
124         unsigned num;
125         ret = krb5_string_to_keytype(context, keytype_str, &keytype);
126         if(ret)
127             krb5_err(context, 1, ret, "%s", keytype_str);
128         ret = krb5_keytype_to_enctypes(context, keytype, &num, &etypes);
129         if(ret)
130             krb5_err(context, 1, ret, "%s", keytype_str);
131         if(num == 0)
132             krb5_errx(context, 1, "there are no encryption types for that keytype");
133         etype = etypes[0];
134         krb5_enctype_to_string(context, etype, &keytype_str);
135         if(num > 1 && version5)
136             krb5_warnx(context, "ambiguous keytype, using %s", keytype_str);
137     }
138     
139     if((etype != ETYPE_DES_CBC_CRC &&
140         etype != ETYPE_DES_CBC_MD4 &&
141         etype != ETYPE_DES_CBC_MD5) &&
142        (afs || version4)) {
143         if(!version5) {
144             etype = ETYPE_DES_CBC_CRC;
145         } else {
146             krb5_errx(context, 1, 
147                       "DES is the only valid keytype for AFS and Kerberos 4");
148         }
149     }
150
151     if(version5 && principal == NULL){
152         printf("Kerberos v5 principal: ");
153         if(fgets(buf, sizeof(buf), stdin) == NULL)
154             return 1;
155         if(buf[strlen(buf) - 1] == '\n')
156             buf[strlen(buf) - 1] = '\0';
157         principal = estrdup(buf);
158     }
159     if(afs && cell == NULL){
160         printf("AFS cell: ");
161         if(fgets(buf, sizeof(buf), stdin) == NULL)
162             return 1;
163         if(buf[strlen(buf) - 1] == '\n')
164             buf[strlen(buf) - 1] = '\0';
165         cell = estrdup(buf);
166     }
167     if(argv[0])
168         password = argv[0];
169     if(password == NULL){
170         if(des_read_pw_string(buf, sizeof(buf), "Password: ", 0))
171             return 1;
172         password = buf;
173     }
174         
175     if(version5){
176         krb5_parse_name(context, principal, &princ);
177         krb5_get_pw_salt(context, princ, &salt);
178         tokey(context, etype, password, salt, "Kerberos 5 (%s)");
179         krb5_free_salt(context, salt);
180     }
181     if(version4){
182         salt.salttype = KRB5_PW_SALT;
183         salt.saltvalue.length = 0;
184         salt.saltvalue.data = NULL;
185         tokey(context, ETYPE_DES_CBC_MD5, password, salt, "Kerberos 4");
186     }
187     if(afs){
188         salt.salttype = KRB5_AFS3_SALT;
189         salt.saltvalue.length = strlen(cell);
190         salt.saltvalue.data = cell;
191         tokey(context, ETYPE_DES_CBC_MD5, password, salt, "AFS");
192     }
193     return 0;
194 }