Use rxgk in afsconf_BuildServerSecurityObjects
[openafs.git] / src / auth / authcon.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12 #include <afs/stds.h>
13
14 #include <roken.h>
15
16 #ifdef IGNORE_SOME_GCC_WARNINGS
17 # pragma GCC diagnostic warning "-Wdeprecated-declarations"
18 #endif
19
20 #define HC_DEPRECATED
21 #include <hcrypto/des.h>
22 #include <hcrypto/rand.h>
23
24 #include <rx/rxkad.h>
25 #include <rx/rx.h>
26
27 #include <afs/pthread_glock.h>
28
29 #include "cellconfig.h"
30 #include "keys.h"
31 #include "ktc.h"
32 #include "auth.h"
33
34 #ifdef AFS_RXGK_ENV
35 # include <rx/rxgk.h>
36 #endif
37
38 /* return a null security object if nothing else can be done */
39 static afs_int32
40 QuickAuth(struct rx_securityClass **astr, afs_int32 *aindex)
41 {
42     struct rx_securityClass *tc;
43     tc = rxnull_NewClientSecurityObject();
44     *astr = tc;
45     *aindex = RX_SECIDX_NULL;
46     return 0;
47 }
48
49 static int _afsconf_GetRxkadKrb5Key(void *arock, int kvno, int enctype, void *outkey,
50                                     size_t *keylen)
51 {
52     struct afsconf_dir *adir = arock;
53     struct afsconf_typedKey *kobj;
54     struct rx_opaque *keymat;
55     afsconf_keyType tktype;
56     int tkvno, tenctype;
57     int code;
58
59     code = afsconf_GetKeyByTypes(adir, afsconf_rxkad_krb5, kvno, enctype, &kobj);
60     if (code != 0)
61         return code;
62     afsconf_typedKey_values(kobj, &tktype, &tkvno, &tenctype, &keymat);
63     if (*keylen < keymat->len) {
64         afsconf_typedKey_put(&kobj);
65         return AFSCONF_BADKEY;
66     }
67     memcpy(outkey, keymat->val, keymat->len);
68     *keylen = keymat->len;
69     afsconf_typedKey_put(&kobj);
70     return 0;
71 }
72
73
74 /* Return an appropriate security class and index */
75 afs_int32
76 afsconf_ServerAuth(void *arock,
77                    struct rx_securityClass **astr,
78                    afs_int32 *aindex)
79 {
80     struct afsconf_dir *adir = (struct afsconf_dir *) arock;
81     struct rx_securityClass *tclass;
82
83     LOCK_GLOBAL_MUTEX;
84     tclass = (struct rx_securityClass *)
85         rxkad_NewKrb5ServerSecurityObject(0, adir, afsconf_GetKey,
86                                           _afsconf_GetRxkadKrb5Key, NULL);
87     if (tclass) {
88         *astr = tclass;
89         *aindex = RX_SECIDX_KAD;
90         UNLOCK_GLOBAL_MUTEX;
91         return 0;
92     } else {
93         UNLOCK_GLOBAL_MUTEX;
94         return 2;
95     }
96 }
97
98 static afs_int32
99 GenericAuth(struct afsconf_dir *adir,
100             struct rx_securityClass **astr,
101             afs_int32 *aindex,
102             rxkad_level enclevel)
103 {
104     int enctype_preflist[]={18, 17, 23, 16, 0};
105     char tbuffer[512];
106     struct ktc_encryptionKey key, session;
107     struct rx_securityClass *tclass;
108     afs_int32 kvno;
109     afs_int32 ticketLen;
110     afs_int32 code;
111     int use_krb5=0;
112     struct afsconf_typedKey *kobj;
113     struct rx_opaque *keymat;
114     int *et;
115
116     /* first, find the right key and kvno to use */
117
118     et = enctype_preflist;
119     while(*et != 0) {
120         code = afsconf_GetLatestKeyByTypes(adir, afsconf_rxkad_krb5, *et,
121                                            &kobj);
122         if (code == 0) {
123             afsconf_keyType tktype;
124             int tenctype;
125             afsconf_typedKey_values(kobj, &tktype, &kvno, &tenctype, &keymat);
126             RAND_add(keymat->val, keymat->len, 0.0);
127             use_krb5 = 1;
128             break;
129         }
130         et++;
131     }
132
133     if (use_krb5 == 0) {
134         code = afsconf_GetLatestKey(adir, &kvno, &key);
135         if (code) {
136             return QuickAuth(astr, aindex);
137         }
138         /* next create random session key, using key for seed to good random */
139         DES_init_random_number_generator((DES_cblock *) &key);
140     }
141     code = DES_new_random_key((DES_cblock *) &session);
142     if (code) {
143         if (use_krb5)
144             afsconf_typedKey_put(&kobj);
145         return QuickAuth(astr, aindex);
146     }
147
148     if (use_krb5) {
149         ticketLen = sizeof(tbuffer);
150         memset(tbuffer, '\0', sizeof(tbuffer));
151         code =
152             tkt_MakeTicket5(tbuffer, &ticketLen, *et, &kvno, keymat->val,
153                             keymat->len, AUTH_SUPERUSER, "", "", 0, 0x7fffffff,
154                             &session, "afs", "");
155         afsconf_typedKey_put(&kobj);
156     } else {
157         /* now create the actual ticket */
158         ticketLen = sizeof(tbuffer);
159         memset(tbuffer, '\0', sizeof(tbuffer));
160         code =
161             tkt_MakeTicket(tbuffer, &ticketLen, &key, AUTH_SUPERUSER, "", "", 0,
162                            0xffffffff, &session, 0, "afs", "");
163         /* parms were buffer, ticketlen, key to seal ticket with, principal
164          * name, instance and cell, start time, end time, session key to seal
165          * in ticket, inet host, server name and server instance */
166     }
167     if (code) {
168         return QuickAuth(astr, aindex);
169     }
170
171     /* Next, we have ticket, kvno and session key, authenticate the connection.*/
172     tclass = (struct rx_securityClass *)
173         rxkad_NewClientSecurityObject(enclevel, &session, kvno, ticketLen,
174                                       tbuffer);
175     *astr = tclass;
176     *aindex = RX_SECIDX_KAD;
177     return 0;
178 }
179
180 /* build a fake ticket for 'afs' using keys from adir, returning an
181  * appropriate security class and index
182  */
183 afs_int32
184 afsconf_ClientAuth(void *arock, struct rx_securityClass ** astr,
185                    afs_int32 * aindex)
186 {
187     struct afsconf_dir * adir = (struct afsconf_dir *) arock;
188     afs_int32 rc;
189
190     LOCK_GLOBAL_MUTEX;
191     rc = GenericAuth(adir, astr, aindex, rxkad_clear);
192     UNLOCK_GLOBAL_MUTEX;
193     return rc;
194 }
195
196 /* build a fake ticket for 'afs' using keys from adir, returning an
197  * appropriate security class and index.  This one, unlike the above,
198  * tells rxkad to encrypt the data, too.
199  */
200 afs_int32
201 afsconf_ClientAuthSecure(void *arock,
202                          struct rx_securityClass **astr,
203                          afs_int32 *aindex)
204 {
205     struct afsconf_dir *adir = (struct afsconf_dir *) arock;
206     afs_int32 rc;
207
208     LOCK_GLOBAL_MUTEX;
209     rc = GenericAuth(adir, astr, aindex, rxkad_crypt);
210     UNLOCK_GLOBAL_MUTEX;
211     return rc;
212 }
213
214 /*!
215  * Build a security class from the user's current tokens
216  *
217  * This function constructs an RX security class from a user's current
218  * tokens.
219  *
220  * @param[in] info      The cell information structure
221  * @param[in] flags     Security flags describing the desired mechanism
222  * @param[out] sc       The selected security class
223  * @param[out] scIndex  The index of the selected class
224  * @parma[out] expires  The expiry time of the tokens used to build the class
225  *
226  * Only the AFSCONF_SECOPTS_ALWAYSENCRYPT flag will modify the behaviour of
227  * this function - it determines whether a cleartext, or encrypting, security
228  * class is provided.
229  *
230  * @return
231  *     0 on success, non-zero on failure. An error code of
232  *     AFSCONF_NO_SECURITY_CLASS indicates that were were unable to build a
233  *     security class using the selected tokens.
234  */
235
236 afs_int32
237 afsconf_ClientAuthToken(struct afsconf_cell *info,
238                         afsconf_secflags flags,
239                         struct rx_securityClass **sc,
240                         afs_int32 *scIndex,
241                         time_t *expires)
242 {
243     struct ktc_setTokenData *tokenSet = NULL;
244     struct ktc_token ttoken;
245     int encryptLevel;
246     afs_int32 code;
247
248     *sc = NULL;
249     *scIndex = RX_SECIDX_NULL;
250
251     code = ktc_GetTokenEx(info->name, &tokenSet);
252     if (code)
253         goto out;
254
255     code = token_extractRxkad(tokenSet, &ttoken, NULL, NULL);
256     if (code == 0) {
257         /* XXX - We should think about how to handle this */
258         if (ttoken.kvno < 0 || ttoken.kvno > 256) {
259              fprintf(stderr,
260                     "funny kvno (%d) in ticket, proceeding\n",
261                     ttoken.kvno);
262         }
263         if (flags & AFSCONF_SECOPTS_ALWAYSENCRYPT)
264             encryptLevel = rxkad_crypt;
265         else
266             encryptLevel = rxkad_clear;
267         *sc = rxkad_NewClientSecurityObject(encryptLevel,
268                                             &ttoken.sessionKey,
269                                             ttoken.kvno,
270                                             ttoken.ticketLen,
271                                             ttoken.ticket);
272         *scIndex = RX_SECIDX_KAD;
273         if (expires)
274             *expires = ttoken.endTime;
275     }
276
277 out:
278     token_FreeSet(&tokenSet);
279
280     if (*sc == NULL)
281         return AFSCONF_NO_SECURITY_CLASS;
282
283     return code;
284 }
285
286 /*!
287  * Set the security flags to be used for a particular configuration
288  */
289 void
290 afsconf_SetSecurityFlags(struct afsconf_dir *dir,
291                          afsconf_secflags flags)
292 {
293     dir->securityFlags = flags;
294 }
295
296 /*!
297  * Build a set of security classes suitable for a server accepting
298  * incoming connections
299  */
300 void
301 afsconf_BuildServerSecurityObjects(void *rock,
302                                    struct rx_securityClass ***classes,
303                                    afs_int32 *numClasses)
304 {
305     struct afsconf_dir *dir = rock;
306
307     *numClasses = RX_SECIDX_GK+1;
308
309     *classes = calloc(*numClasses, sizeof(**classes));
310
311     (*classes)[RX_SECIDX_NULL] = rxnull_NewServerSecurityObject();
312     (*classes)[RX_SECIDX_KAD] =
313         rxkad_NewKrb5ServerSecurityObject(0, dir, afsconf_GetKey,
314                                           _afsconf_GetRxkadKrb5Key, NULL);
315
316     if (dir->securityFlags & AFSCONF_SECOPTS_ALWAYSENCRYPT)
317         (*classes)[RX_SECIDX_KAE] =
318             rxkad_NewKrb5ServerSecurityObject(rxkad_crypt, dir, afsconf_GetKey,
319                                               _afsconf_GetRxkadKrb5Key, NULL);
320 #ifdef AFS_RXGK_ENV
321     (*classes)[RX_SECIDX_GK] =
322         rxgk_NewServerSecurityObject(rock, afsconf_GetRXGKKey);
323 #endif
324 }
325
326 /*!
327  * Pick a security class to use for an outgoing connection
328  *
329  * This function selects an RX security class to use for an outgoing
330  * connection, based on the set of security flags provided.
331  *
332  * @param[in] dir
333  *      The configuration directory structure for this cell. If NULL,
334  *      no classes requiring local configuration will be returned.
335  * @param[in] flags
336  *      A set of flags to determine the properties of the security class which
337  *      is selected
338  *      - AFSCONF_SECOPTS_NOAUTH - return an anonymous secirty class
339  *      - AFSCONF_SECOPTS_LOCALAUTH - use classes which have local key
340  *              material available.
341  *      - AFSCONF_SECOPTS_ALWAYSENCRYPT - use classes in encrypting, rather
342  *              than authentication or integrity modes.
343  *      - AFSCONF_SECOPTS_FALLBACK_NULL - if no suitable class can be found,
344  *              then fallback to the rxnull security class.
345  * @param[in] info
346  *      The cell information structure for the current cell. If this is NULL,
347  *      then use a version locally obtained using the cellName.
348  * @param[in] cellName
349  *      The cellName to use when obtaining cell information (may be NULL if
350  *      info is specified)
351  * @param[out] sc
352  *      The selected security class
353  * @param[out] scIndex
354  *      The index of the selected security class
355  * @param[out] expires
356  *      The expiry time of the tokens used to construct the class. Will be
357  *      NEVER_DATE if the class has an unlimited lifetime. If NULL, the
358  *      function won't store the expiry date.
359  *
360  * @return
361  *      Returns 0 on success, or a com_err error code on failure.
362  */
363 afs_int32
364 afsconf_PickClientSecObj(struct afsconf_dir *dir, afsconf_secflags flags,
365                          struct afsconf_cell *info,
366                          char *cellName, struct rx_securityClass **sc,
367                          afs_int32 *scIndex, time_t *expires) {
368     struct afsconf_cell localInfo;
369     afs_int32 code = 0;
370
371     *sc = NULL;
372     *scIndex = RX_SECIDX_NULL;
373     if (expires)
374         *expires = 0;
375
376     if ( !(flags & AFSCONF_SECOPTS_NOAUTH) ) {
377         if (!dir)
378             return AFSCONF_NOCELLDB;
379
380         if (flags & AFSCONF_SECOPTS_LOCALAUTH) {
381             if (flags & AFSCONF_SECOPTS_ALWAYSENCRYPT)
382                 code = afsconf_ClientAuthSecure(dir, sc, scIndex);
383             else
384                 code = afsconf_ClientAuth(dir, sc, scIndex);
385
386             if (code)
387                 goto out;
388
389             /* The afsconf_ClientAuth functions will fall back to giving
390              * a rxnull object, which we don't want if localauth has been
391              * explicitly requested. Check for this, and bail out if we
392              * get one. Note that this leaks a security object at present
393              */
394             if (!(flags & AFSCONF_SECOPTS_FALLBACK_NULL) &&
395                 *scIndex == RX_SECIDX_NULL) {
396                 sc = NULL;
397                 code = AFSCONF_NOTFOUND;
398                 goto out;
399             }
400
401             if (expires)
402                 *expires = NEVERDATE;
403         } else {
404             if (info == NULL) {
405                 code = afsconf_GetCellInfo(dir, cellName, NULL, &localInfo);
406                 if (code)
407                     goto out;
408                 info = &localInfo;
409             }
410
411             code = afsconf_ClientAuthToken(info, flags, sc, scIndex, expires);
412             if (code && !(flags & AFSCONF_SECOPTS_FALLBACK_NULL))
413                 goto out;
414
415             /* If we didn't get a token, we'll just run anonymously */
416             code = 0;
417         }
418     }
419     if (*sc == NULL) {
420         *sc = rxnull_NewClientSecurityObject();
421         *scIndex = RX_SECIDX_NULL;
422         if (expires)
423             *expires = NEVERDATE;
424     }
425
426 out:
427     return code;
428 }