1 /* 2 * Copyright (c) 1997 - 2001 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <krb5_locl.h> 35 36 RCSID("$Id: mk_priv.c,v 1.30 2001/06/18 02:44:54 assar Exp $"); 37 38 /* 39 * 40 */ 41 42 krb5_error_code 43 krb5_mk_priv(krb5_context context, 44 krb5_auth_context auth_context, 45 const krb5_data *userdata, 46 krb5_data *outbuf, 47 /*krb5_replay_data*/ void *outdata) 48 { 49 krb5_error_code ret; 50 KRB_PRIV s; 51 EncKrbPrivPart part; 52 u_char *buf; 53 size_t buf_size; 54 size_t len; 55 u_int32_t tmp_seq; 56 krb5_keyblock *key; 57 int32_t sec, usec; 58 KerberosTime sec2; 59 int usec2; 60 krb5_crypto crypto; 61 62 if (auth_context->local_subkey) 63 key = auth_context->local_subkey; 64 else if (auth_context->remote_subkey) 65 key = auth_context->remote_subkey; 66 else 67 key = auth_context->keyblock; 68 69 krb5_us_timeofday (context, &sec, &usec); 70 71 part.user_data = *userdata; 72 sec2 = sec; 73 part.timestamp = &sec2; 74 usec2 = usec; 75 part.usec = &usec2; 76 if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_SEQUENCE) { 77 tmp_seq = auth_context->local_seqnumber; 78 part.seq_number = &tmp_seq; 79 } else { 80 part.seq_number = NULL; 81 } 82 83 part.s_address = auth_context->local_address; 84 part.r_address = auth_context->remote_address; 85 86 buf_size = 1024; 87 buf = malloc (buf_size); 88 if (buf == NULL) { 89 krb5_set_error_string (context, "malloc: out of memory"); 90 return ENOMEM; 91 } 92 93 krb5_data_zero (&s.enc_part.cipher); 94 95 do { 96 ret = encode_EncKrbPrivPart (buf + buf_size - 1, buf_size, 97 &part, &len); 98 if (ret) { 99 if (ret == ASN1_OVERFLOW) { 100 u_char *tmp; 101 102 buf_size *= 2; 103 tmp = realloc (buf, buf_size); 104 if (tmp == NULL) { 105 krb5_set_error_string (context, "malloc: out of memory"); 106 ret = ENOMEM; 107 goto fail; 108 } 109 buf = tmp; 110 } else { 111 goto fail; 112 } 113 } 114 } while(ret == ASN1_OVERFLOW); 115 116 s.pvno = 5; 117 s.msg_type = krb_priv; 118 s.enc_part.etype = key->keytype; 119 s.enc_part.kvno = NULL; 120 121 ret = krb5_crypto_init(context, key, 0, &crypto); 122 if (ret) { 123 free (buf); 124 return ret; 125 } 126 ret = krb5_encrypt (context, 127 crypto, 128 KRB5_KU_KRB_PRIV, 129 buf + buf_size - len, 130 len, 131 &s.enc_part.cipher); 132 krb5_crypto_destroy(context, crypto); 133 if (ret) { 134 free(buf); 135 return ret; 136 } 137 138 do { 139 ret = encode_KRB_PRIV (buf + buf_size - 1, buf_size, &s, &len); 140 141 if (ret){ 142 if (ret == ASN1_OVERFLOW) { 143 u_char *tmp; 144 145 buf_size *= 2; 146 tmp = realloc (buf, buf_size); 147 if (tmp == NULL) { 148 krb5_set_error_string (context, "malloc: out of memory"); 149 ret = ENOMEM; 150 goto fail; 151 } 152 buf = tmp; 153 } else { 154 goto fail; 155 } 156 } 157 } while(ret == ASN1_OVERFLOW); 158 krb5_data_free (&s.enc_part.cipher); 159 160 outbuf->length = len; 161 outbuf->data = malloc (len); 162 if (outbuf->data == NULL) { 163 krb5_set_error_string (context, "malloc: out of memory"); 164 free(buf); 165 return ENOMEM; 166 } 167 memcpy (outbuf->data, buf + buf_size - len, len); 168 free (buf); 169 if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_SEQUENCE) 170 auth_context->local_seqnumber = 171 (auth_context->local_seqnumber + 1) & 0xFFFFFFFF; 172 return 0; 173 174 fail: 175 free (buf); 176 krb5_data_free (&s.enc_part.cipher); 177 return ret; 178 } 179