This commit was manufactured by cvs2svn to create tag
[krb5.git] / src / lib / krb4 / password_to_key.c
1 /*
2  * lib/krb4/password_to_key.c
3  *
4  * Copyright 1999, 2002 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  * 
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  *
26  * password_to_key functions merged from KfM
27  */
28
29 #include <string.h>
30 #include <stdlib.h>
31
32 #ifdef USE_CCAPI
33 #include <CredentialsCache.h>
34 #endif
35 #include "krb.h"
36 #include "krb4int.h"
37
38 /*
39  * passwd_to_key(): given a password, return a DES key.
40  * There are extra arguments here which (used to be?)
41  * used by srvtab_to_key().
42  *
43  * If the "passwd" argument is not null, generate a DES
44  * key from it, using string_to_key().
45  *
46  * If the "passwd" argument is null, then on a Unix system we call
47  * des_read_password() to prompt for a password and then convert it
48  * into a DES key.  But "prompting" the user is harder in a Windows or
49  * Macintosh environment, so we rely on our caller to explicitly do
50  * that now.
51  *
52  * In either case, the resulting key is put in the "key" argument,
53  * and 0 is returned.
54  */
55
56
57 key_proc_type *krb_get_keyprocs (key_proc_type keyproc)
58 {
59     static key_proc_type default_keyprocs[4] = { mit_passwd_to_key, 
60                                                  afs_passwd_to_key, 
61                                                  krb5_passwd_to_key, 
62                                                  NULL };
63                                                   
64     static key_proc_type user_keyprocs[2] = { NULL, NULL };
65     
66     /* generate the list of key procs */
67     if (keyproc == NULL) {
68         return default_keyprocs; /* use the default */
69     } else {
70         user_keyprocs[0] = keyproc;
71         return user_keyprocs;  /* use the caller provided keyprocs */
72     }
73 }
74
75 int KRB5_CALLCONV
76 mit_passwd_to_key(
77     char        *user,
78     char        *instance,
79     char        *realm,
80     char        *passwd,
81     C_Block     key)
82 {
83 #if 0 /* what system? */
84 #pragma unused(user)
85 #pragma unused(instance)
86 #pragma unused(realm)
87 #endif
88
89     if (passwd) {
90         des_string_to_key(passwd, key);
91     } else {
92 #if !(defined(_WIN32) || defined(USE_LOGIN_LIBRARY))
93         des_read_password((des_cblock *)key, "Password", 0);
94 #else
95         return (-1);
96 #endif
97     }
98     return (0);
99 }
100
101 /* So we can use a v4 kinit against a v5 kdc with no krb4 salted key */
102 int KRB5_CALLCONV
103 krb5_passwd_to_key(
104     char        *user,
105     char        *instance,
106     char        *realm,
107     char        *passwd,
108     C_Block     key)
109 {
110     size_t      len, tlen;
111     char        *p;
112
113     if (user && instance && realm && passwd) {
114         len = MAX_K_NAME_SZ + strlen(passwd) + 1;
115         tlen = strlen(passwd) + strlen(realm) + strlen(user) + strlen(instance) + 1;
116         if (tlen > len)
117             return 0;
118         p = malloc (tlen);
119         if (p != NULL) {
120             sprintf (p, "%s%s%s%s", passwd, realm, user, instance);
121             des_string_to_key (p, key);
122             free (p);
123             return 0;
124         }
125     }
126     return -1;
127 }
128
129 int KRB5_CALLCONV
130 afs_passwd_to_key(
131     char        *user,
132     char        *instance,
133     char        *realm,
134     char        *passwd,
135     C_Block     key)
136 {
137 #if 0 /* what system? */
138 #pragma unused(user)
139 #pragma unused(instance)
140 #endif
141
142     if (passwd) {
143         afs_string_to_key(passwd, realm, key);
144     } else {
145 #if !(defined(_WIN32) || defined(USE_LOGIN_LIBRARY))
146         des_read_password((des_cblock *)key, "Password", 0);
147 #else
148         return (-1);
149 #endif
150     }
151     return (0);
152 }