2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include <afs/param.h>
16 #ifdef IGNORE_SOME_GCC_WARNINGS
17 # pragma GCC diagnostic warning "-Wdeprecated-declarations"
21 #include <hcrypto/des.h>
22 #include <hcrypto/rand.h>
27 #include <afs/pthread_glock.h>
29 #include "cellconfig.h"
38 /* return a null security object if nothing else can be done */
40 QuickAuth(struct rx_securityClass **astr, afs_int32 *aindex)
42 struct rx_securityClass *tc;
43 tc = rxnull_NewClientSecurityObject();
45 *aindex = RX_SECIDX_NULL;
49 static int _afsconf_GetRxkadKrb5Key(void *arock, int kvno, int enctype, void *outkey,
52 struct afsconf_dir *adir = arock;
53 struct afsconf_typedKey *kobj;
54 struct rx_opaque *keymat;
55 afsconf_keyType tktype;
59 code = afsconf_GetKeyByTypes(adir, afsconf_rxkad_krb5, kvno, enctype, &kobj);
62 afsconf_typedKey_values(kobj, &tktype, &tkvno, &tenctype, &keymat);
63 if (*keylen < keymat->len) {
64 afsconf_typedKey_put(&kobj);
65 return AFSCONF_BADKEY;
67 memcpy(outkey, keymat->val, keymat->len);
68 *keylen = keymat->len;
69 afsconf_typedKey_put(&kobj);
74 /* Return an appropriate security class and index */
76 afsconf_ServerAuth(void *arock,
77 struct rx_securityClass **astr,
80 struct afsconf_dir *adir = (struct afsconf_dir *) arock;
81 struct rx_securityClass *tclass;
84 tclass = (struct rx_securityClass *)
85 rxkad_NewKrb5ServerSecurityObject(0, adir, afsconf_GetKey,
86 _afsconf_GetRxkadKrb5Key, NULL);
89 *aindex = RX_SECIDX_KAD;
99 GenericAuth(struct afsconf_dir *adir,
100 struct rx_securityClass **astr,
102 rxkad_level enclevel)
104 int enctype_preflist[]={18, 17, 23, 16, 0};
106 struct ktc_encryptionKey key, session;
107 struct rx_securityClass *tclass;
112 struct afsconf_typedKey *kobj;
113 struct rx_opaque *keymat;
116 /* first, find the right key and kvno to use */
118 et = enctype_preflist;
120 code = afsconf_GetLatestKeyByTypes(adir, afsconf_rxkad_krb5, *et,
123 afsconf_keyType tktype;
125 afsconf_typedKey_values(kobj, &tktype, &kvno, &tenctype, &keymat);
126 RAND_add(keymat->val, keymat->len, 0.0);
134 code = afsconf_GetLatestKey(adir, &kvno, &key);
136 return QuickAuth(astr, aindex);
138 /* next create random session key, using key for seed to good random */
139 DES_init_random_number_generator((DES_cblock *) &key);
141 code = DES_new_random_key((DES_cblock *) &session);
144 afsconf_typedKey_put(&kobj);
145 return QuickAuth(astr, aindex);
149 ticketLen = sizeof(tbuffer);
150 memset(tbuffer, '\0', sizeof(tbuffer));
152 tkt_MakeTicket5(tbuffer, &ticketLen, *et, &kvno, keymat->val,
153 keymat->len, AUTH_SUPERUSER, "", "", 0, 0x7fffffff,
154 &session, "afs", "");
155 afsconf_typedKey_put(&kobj);
157 /* now create the actual ticket */
158 ticketLen = sizeof(tbuffer);
159 memset(tbuffer, '\0', sizeof(tbuffer));
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 */
168 return QuickAuth(astr, aindex);
171 /* Next, we have ticket, kvno and session key, authenticate the connection.*/
172 tclass = (struct rx_securityClass *)
173 rxkad_NewClientSecurityObject(enclevel, &session, kvno, ticketLen,
176 *aindex = RX_SECIDX_KAD;
180 /* build a fake ticket for 'afs' using keys from adir, returning an
181 * appropriate security class and index
184 afsconf_ClientAuth(void *arock, struct rx_securityClass ** astr,
187 struct afsconf_dir * adir = (struct afsconf_dir *) arock;
191 rc = GenericAuth(adir, astr, aindex, rxkad_clear);
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.
201 afsconf_ClientAuthSecure(void *arock,
202 struct rx_securityClass **astr,
205 struct afsconf_dir *adir = (struct afsconf_dir *) arock;
209 rc = GenericAuth(adir, astr, aindex, rxkad_crypt);
215 * Build a security class from the user's current tokens
217 * This function constructs an RX security class from a user's current
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
226 * Only the AFSCONF_SECOPTS_ALWAYSENCRYPT flag will modify the behaviour of
227 * this function - it determines whether a cleartext, or encrypting, security
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.
237 afsconf_ClientAuthToken(struct afsconf_cell *info,
238 afsconf_secflags flags,
239 struct rx_securityClass **sc,
243 struct ktc_setTokenData *tokenSet = NULL;
244 struct ktc_token ttoken;
249 *scIndex = RX_SECIDX_NULL;
251 code = ktc_GetTokenEx(info->name, &tokenSet);
255 code = token_extractRxkad(tokenSet, &ttoken, NULL, NULL);
257 /* XXX - We should think about how to handle this */
258 if (ttoken.kvno < 0 || ttoken.kvno > 256) {
260 "funny kvno (%d) in ticket, proceeding\n",
263 if (flags & AFSCONF_SECOPTS_ALWAYSENCRYPT)
264 encryptLevel = rxkad_crypt;
266 encryptLevel = rxkad_clear;
267 *sc = rxkad_NewClientSecurityObject(encryptLevel,
272 *scIndex = RX_SECIDX_KAD;
274 *expires = ttoken.endTime;
278 token_FreeSet(&tokenSet);
281 return AFSCONF_NO_SECURITY_CLASS;
287 * Set the security flags to be used for a particular configuration
290 afsconf_SetSecurityFlags(struct afsconf_dir *dir,
291 afsconf_secflags flags)
293 dir->securityFlags = flags;
297 * Build a set of security classes suitable for a server accepting
298 * incoming connections
301 afsconf_BuildServerSecurityObjects(void *rock,
302 struct rx_securityClass ***classes,
303 afs_int32 *numClasses)
305 struct afsconf_dir *dir = rock;
307 *numClasses = RX_SECIDX_GK+1;
309 *classes = calloc(*numClasses, sizeof(**classes));
311 (*classes)[RX_SECIDX_NULL] = rxnull_NewServerSecurityObject();
312 (*classes)[RX_SECIDX_KAD] =
313 rxkad_NewKrb5ServerSecurityObject(0, dir, afsconf_GetKey,
314 _afsconf_GetRxkadKrb5Key, NULL);
316 if (dir->securityFlags & AFSCONF_SECOPTS_ALWAYSENCRYPT)
317 (*classes)[RX_SECIDX_KAE] =
318 rxkad_NewKrb5ServerSecurityObject(rxkad_crypt, dir, afsconf_GetKey,
319 _afsconf_GetRxkadKrb5Key, NULL);
321 (*classes)[RX_SECIDX_GK] =
322 rxgk_NewServerSecurityObject(rock, afsconf_GetRXGKKey);
327 * Pick a security class to use for an outgoing connection
329 * This function selects an RX security class to use for an outgoing
330 * connection, based on the set of security flags provided.
333 * The configuration directory structure for this cell. If NULL,
334 * no classes requiring local configuration will be returned.
336 * A set of flags to determine the properties of the security class which
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.
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
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.
361 * Returns 0 on success, or a com_err error code on failure.
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;
372 *scIndex = RX_SECIDX_NULL;
376 if ( !(flags & AFSCONF_SECOPTS_NOAUTH) ) {
378 return AFSCONF_NOCELLDB;
380 if (flags & AFSCONF_SECOPTS_LOCALAUTH) {
381 if (flags & AFSCONF_SECOPTS_ALWAYSENCRYPT)
382 code = afsconf_ClientAuthSecure(dir, sc, scIndex);
384 code = afsconf_ClientAuth(dir, sc, scIndex);
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
394 if (!(flags & AFSCONF_SECOPTS_FALLBACK_NULL) &&
395 *scIndex == RX_SECIDX_NULL) {
397 code = AFSCONF_NOTFOUND;
402 *expires = NEVERDATE;
405 code = afsconf_GetCellInfo(dir, cellName, NULL, &localInfo);
411 code = afsconf_ClientAuthToken(info, flags, sc, scIndex, expires);
412 if (code && !(flags & AFSCONF_SECOPTS_FALLBACK_NULL))
415 /* If we didn't get a token, we'll just run anonymously */
420 *sc = rxnull_NewClientSecurityObject();
421 *scIndex = RX_SECIDX_NULL;
423 *expires = NEVERDATE;