xref: /freebsd/crypto/krb5/src/lib/krb5/krb/in_tkt_sky.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/in_tkt_sky.c */
3 /*
4  * Copyright 1990,1991, 2008 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 
27 #include "k5-int.h"
28 #include "int-proto.h"
29 
30 /* Copy the caller-provided keyblock into the AS keyblock. */
31 static krb5_error_code
get_as_key_skey(krb5_context context,krb5_principal client,krb5_enctype etype,krb5_prompter_fct prompter,void * prompter_data,krb5_data * salt,krb5_data * params,krb5_keyblock * as_key,void * gak_data,k5_response_items * ritems)32 get_as_key_skey(krb5_context context, krb5_principal client,
33                 krb5_enctype etype, krb5_prompter_fct prompter,
34                 void *prompter_data, krb5_data *salt, krb5_data *params,
35                 krb5_keyblock *as_key, void *gak_data,
36                 k5_response_items *ritems)
37 {
38     const krb5_keyblock *key = gak_data;
39 
40     if (!krb5_c_valid_enctype(etype))
41         return(KRB5_PROG_ETYPE_NOSUPP);
42     if (as_key->length)
43         krb5_free_keyblock_contents(context, as_key);
44     return krb5int_c_copy_keyblock_contents(context, key, as_key);
45 }
46 
47 /*
48   Similar to krb5_get_in_tkt_with_password.
49 
50   Attempts to get an initial ticket for creds->client to use server
51   creds->server, (realm is taken from creds->client), with options
52   options, and using creds->times.starttime, creds->times.endtime,
53   creds->times.renew_till as from, till, and rtime.
54   creds->times.renew_till is ignored unless the RENEWABLE option is requested.
55 
56   If addrs is non-NULL, it is used for the addresses requested.  If it is
57   null, the system standard addresses are used.
58 
59   If keyblock is NULL, an appropriate key for creds->client is retrieved from
60   the system key store (e.g. /etc/krb5.keytab).  If keyblock is non-NULL, it
61   is used as the decryption key.
62 
63   A successful call will place the ticket in the credentials cache ccache.
64 
65   returns system errors, encryption errors
66 
67 */
68 krb5_error_code KRB5_CALLCONV
krb5_get_in_tkt_with_skey(krb5_context context,krb5_flags options,krb5_address * const * addrs,krb5_enctype * ktypes,krb5_preauthtype * pre_auth_types,const krb5_keyblock * key,krb5_ccache ccache,krb5_creds * creds,krb5_kdc_rep ** ret_as_reply)69 krb5_get_in_tkt_with_skey(krb5_context context, krb5_flags options,
70                           krb5_address *const *addrs, krb5_enctype *ktypes,
71                           krb5_preauthtype *pre_auth_types,
72                           const krb5_keyblock *key, krb5_ccache ccache,
73                           krb5_creds *creds, krb5_kdc_rep **ret_as_reply)
74 {
75     krb5_error_code retval;
76     char *server;
77     krb5_principal server_princ, client_princ;
78     krb5_get_init_creds_opt *opts = NULL;
79 
80     retval = k5_populate_gic_opt(context, &opts, options, addrs, ktypes,
81                                  pre_auth_types, creds);
82     if (retval)
83         return retval;
84 
85     retval = krb5_get_init_creds_opt_set_out_ccache(context, opts, ccache);
86     if (retval)
87         goto cleanup;
88 
89 #ifndef LEAN_CLIENT
90     if (key == NULL) {
91         retval = krb5_get_init_creds_keytab(context, creds, creds->client,
92                                             NULL /* keytab */,
93                                             creds->times.starttime,
94                                             NULL /* in_tkt_service */,
95                                             opts);
96         goto cleanup;
97     }
98 #endif /* LEAN_CLIENT */
99 
100     retval = krb5_unparse_name(context, creds->server, &server);
101     if (retval)
102         goto cleanup;
103     server_princ = creds->server;
104     client_princ = creds->client;
105     retval = k5_get_init_creds(context, creds, creds->client,
106                                krb5_prompter_posix, NULL, 0, server, opts,
107                                get_as_key_skey, (void *)key, ret_as_reply);
108     krb5_free_unparsed_name(context, server);
109     if (retval)
110         goto cleanup;
111     krb5_free_principal( context, creds->server);
112     krb5_free_principal( context, creds->client);
113     creds->client = client_princ;
114     creds->server = server_princ;
115 cleanup:
116     krb5_get_init_creds_opt_free(context, opts);
117     return retval;
118 }
119