build_auth.c revision 233294
1254598Sian/* 2254598Sian * Copyright (c) 1997 - 2003 Kungliga Tekniska H��gskolan 3254598Sian * (Royal Institute of Technology, Stockholm, Sweden). 4254598Sian * All rights reserved. 5254598Sian * 6254598Sian * Redistribution and use in source and binary forms, with or without 7254598Sian * modification, are permitted provided that the following conditions 8254598Sian * are met: 9254598Sian * 10254598Sian * 1. Redistributions of source code must retain the above copyright 11254598Sian * notice, this list of conditions and the following disclaimer. 12254598Sian * 13254598Sian * 2. Redistributions in binary form must reproduce the above copyright 14254598Sian * notice, this list of conditions and the following disclaimer in the 15254598Sian * documentation and/or other materials provided with the distribution. 16254598Sian * 17254598Sian * 3. Neither the name of the Institute nor the names of its contributors 18254598Sian * may be used to endorse or promote products derived from this software 19254598Sian * without specific prior written permission. 20254598Sian * 21254598Sian * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22254598Sian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23254598Sian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24254598Sian * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25254598Sian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26254598Sian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27254598Sian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28254598Sian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29254598Sian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30254598Sian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31254598Sian * SUCH DAMAGE. 32254598Sian */ 33254598Sian 34254598Sian#include "krb5_locl.h" 35254598Sian 36254598Sianstatic krb5_error_code 37254598Sianmake_etypelist(krb5_context context, 38254598Sian krb5_authdata **auth_data) 39254598Sian{ 40254598Sian EtypeList etypes; 41254598Sian krb5_error_code ret; 42254598Sian krb5_authdata ad; 43254598Sian u_char *buf; 44254598Sian size_t len = 0; 45254598Sian size_t buf_size; 46254598Sian 47254598Sian ret = _krb5_init_etype(context, KRB5_PDU_NONE, 48254598Sian &etypes.len, &etypes.val, 49254598Sian NULL); 50254598Sian if (ret) 51254598Sian return ret; 52254598Sian 53254598Sian ASN1_MALLOC_ENCODE(EtypeList, buf, buf_size, &etypes, &len, ret); 54254598Sian if (ret) { 55254598Sian free_EtypeList(&etypes); 56254598Sian return ret; 57254598Sian } 58254598Sian if(buf_size != len) 59254598Sian krb5_abortx(context, "internal error in ASN.1 encoder"); 60254598Sian free_EtypeList(&etypes); 61254598Sian 62254598Sian ALLOC_SEQ(&ad, 1); 63254598Sian if (ad.val == NULL) { 64254598Sian free(buf); 65254598Sian krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", "")); 66254598Sian return ENOMEM; 67254598Sian } 68254598Sian 69254598Sian ad.val[0].ad_type = KRB5_AUTHDATA_GSS_API_ETYPE_NEGOTIATION; 70254598Sian ad.val[0].ad_data.length = len; 71254598Sian ad.val[0].ad_data.data = buf; 72254598Sian 73254598Sian ASN1_MALLOC_ENCODE(AD_IF_RELEVANT, buf, buf_size, &ad, &len, ret); 74254598Sian if (ret) { 75254598Sian free_AuthorizationData(&ad); 76254598Sian return ret; 77254598Sian } 78254598Sian if(buf_size != len) 79254598Sian krb5_abortx(context, "internal error in ASN.1 encoder"); 80254598Sian free_AuthorizationData(&ad); 81254598Sian 82254598Sian ALLOC(*auth_data, 1); 83254598Sian if (*auth_data == NULL) { 84254598Sian free(buf); 85254598Sian krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", "")); 86254598Sian return ENOMEM; 87254598Sian } 88254598Sian 89254598Sian ALLOC_SEQ(*auth_data, 1); 90254598Sian if ((*auth_data)->val == NULL) { 91254598Sian free(*auth_data); 92254598Sian free(buf); 93254598Sian krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", "")); 94254598Sian return ENOMEM; 95254598Sian } 96254598Sian 97254598Sian (*auth_data)->val[0].ad_type = KRB5_AUTHDATA_IF_RELEVANT; 98254598Sian (*auth_data)->val[0].ad_data.length = len; 99254598Sian (*auth_data)->val[0].ad_data.data = buf; 100254598Sian 101254598Sian return 0; 102254598Sian} 103254598Sian 104254598SianKRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 105254598Sian_krb5_build_authenticator (krb5_context context, 106254598Sian krb5_auth_context auth_context, 107254598Sian krb5_enctype enctype, 108254598Sian krb5_creds *cred, 109254598Sian Checksum *cksum, 110254598Sian krb5_data *result, 111254598Sian krb5_key_usage usage) 112254598Sian{ 113254598Sian Authenticator auth; 114254598Sian u_char *buf = NULL; 115254598Sian size_t buf_size; 116254598Sian size_t len = 0; 117254598Sian krb5_error_code ret; 118254598Sian krb5_crypto crypto; 119254598Sian 120254598Sian memset(&auth, 0, sizeof(auth)); 121254598Sian 122254598Sian auth.authenticator_vno = 5; 123254598Sian copy_Realm(&cred->client->realm, &auth.crealm); 124254598Sian copy_PrincipalName(&cred->client->name, &auth.cname); 125254598Sian 126254598Sian krb5_us_timeofday (context, &auth.ctime, &auth.cusec); 127254598Sian 128254598Sian ret = krb5_auth_con_getlocalsubkey(context, auth_context, &auth.subkey); 129254598Sian if(ret) 130254598Sian goto fail; 131254598Sian 132254598Sian if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_SEQUENCE) { 133254598Sian if(auth_context->local_seqnumber == 0) 134254598Sian krb5_generate_seq_number (context, 135254598Sian &cred->session, 136254598Sian &auth_context->local_seqnumber); 137254598Sian ALLOC(auth.seq_number, 1); 138254598Sian if(auth.seq_number == NULL) { 139254598Sian ret = ENOMEM; 140254598Sian goto fail; 141254598Sian } 142 *auth.seq_number = auth_context->local_seqnumber; 143 } else 144 auth.seq_number = NULL; 145 auth.authorization_data = NULL; 146 147 if (cksum) { 148 ALLOC(auth.cksum, 1); 149 if (auth.cksum == NULL) { 150 ret = ENOMEM; 151 goto fail; 152 } 153 ret = copy_Checksum(cksum, auth.cksum); 154 if (ret) 155 goto fail; 156 157 if (auth.cksum->cksumtype == CKSUMTYPE_GSSAPI) { 158 /* 159 * This is not GSS-API specific, we only enable it for 160 * GSS for now 161 */ 162 ret = make_etypelist(context, &auth.authorization_data); 163 if (ret) 164 goto fail; 165 } 166 } 167 168 /* XXX - Copy more to auth_context? */ 169 170 auth_context->authenticator->ctime = auth.ctime; 171 auth_context->authenticator->cusec = auth.cusec; 172 173 ASN1_MALLOC_ENCODE(Authenticator, buf, buf_size, &auth, &len, ret); 174 if (ret) 175 goto fail; 176 if(buf_size != len) 177 krb5_abortx(context, "internal error in ASN.1 encoder"); 178 179 ret = krb5_crypto_init(context, &cred->session, enctype, &crypto); 180 if (ret) 181 goto fail; 182 ret = krb5_encrypt (context, 183 crypto, 184 usage /* KRB5_KU_AP_REQ_AUTH */, 185 buf, 186 len, 187 result); 188 krb5_crypto_destroy(context, crypto); 189 190 if (ret) 191 goto fail; 192 193 fail: 194 free_Authenticator (&auth); 195 free (buf); 196 197 return ret; 198} 199