1 /* 2 * Copyright (c) 1997 - 2002 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: rd_safe.c,v 1.26 2002/02/14 12:47:47 joda Exp $"); 37 38 static krb5_error_code 39 verify_checksum(krb5_context context, 40 krb5_auth_context auth_context, 41 KRB_SAFE *safe) 42 { 43 krb5_error_code ret; 44 u_char *buf; 45 size_t buf_size; 46 size_t len; 47 Checksum c; 48 krb5_crypto crypto; 49 krb5_keyblock *key; 50 51 c = safe->cksum; 52 safe->cksum.cksumtype = 0; 53 safe->cksum.checksum.data = NULL; 54 safe->cksum.checksum.length = 0; 55 56 buf_size = length_KRB_SAFE(safe); 57 buf = malloc(buf_size); 58 59 if (buf == NULL) { 60 ret = ENOMEM; 61 krb5_set_error_string (context, "malloc: out of memory"); 62 goto out; 63 } 64 65 ret = encode_KRB_SAFE (buf + buf_size - 1, 66 buf_size, 67 safe, 68 &len); 69 70 if (auth_context->remote_subkey) 71 key = auth_context->remote_subkey; 72 else if (auth_context->local_subkey) 73 key = auth_context->local_subkey; 74 else 75 key = auth_context->keyblock; 76 77 ret = krb5_crypto_init(context, key, 0, &crypto); 78 if (ret) 79 goto out; 80 ret = krb5_verify_checksum (context, 81 crypto, 82 KRB5_KU_KRB_SAFE_CKSUM, 83 buf + buf_size - len, 84 len, 85 &c); 86 krb5_crypto_destroy(context, crypto); 87 out: 88 safe->cksum = c; 89 free (buf); 90 return ret; 91 } 92 93 krb5_error_code 94 krb5_rd_safe(krb5_context context, 95 krb5_auth_context auth_context, 96 const krb5_data *inbuf, 97 krb5_data *outbuf, 98 /*krb5_replay_data*/ void *outdata) 99 { 100 krb5_error_code ret; 101 KRB_SAFE safe; 102 size_t len; 103 104 ret = decode_KRB_SAFE (inbuf->data, inbuf->length, &safe, &len); 105 if (ret) 106 return ret; 107 if (safe.pvno != 5) { 108 ret = KRB5KRB_AP_ERR_BADVERSION; 109 krb5_clear_error_string (context); 110 goto failure; 111 } 112 if (safe.msg_type != krb_safe) { 113 ret = KRB5KRB_AP_ERR_MSG_TYPE; 114 krb5_clear_error_string (context); 115 goto failure; 116 } 117 if (!krb5_checksum_is_keyed(context, safe.cksum.cksumtype) 118 || !krb5_checksum_is_collision_proof(context, safe.cksum.cksumtype)) { 119 ret = KRB5KRB_AP_ERR_INAPP_CKSUM; 120 krb5_clear_error_string (context); 121 goto failure; 122 } 123 124 /* check sender address */ 125 126 if (safe.safe_body.s_address 127 && auth_context->remote_address 128 && !krb5_address_compare (context, 129 auth_context->remote_address, 130 safe.safe_body.s_address)) { 131 ret = KRB5KRB_AP_ERR_BADADDR; 132 krb5_clear_error_string (context); 133 goto failure; 134 } 135 136 /* check receiver address */ 137 138 if (safe.safe_body.r_address 139 && auth_context->local_address 140 && !krb5_address_compare (context, 141 auth_context->local_address, 142 safe.safe_body.r_address)) { 143 ret = KRB5KRB_AP_ERR_BADADDR; 144 krb5_clear_error_string (context); 145 goto failure; 146 } 147 148 /* check timestamp */ 149 if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_TIME) { 150 krb5_timestamp sec; 151 152 krb5_timeofday (context, &sec); 153 154 if (safe.safe_body.timestamp == NULL || 155 safe.safe_body.usec == NULL || 156 abs(*safe.safe_body.timestamp - sec) > context->max_skew) { 157 ret = KRB5KRB_AP_ERR_SKEW; 158 krb5_clear_error_string (context); 159 goto failure; 160 } 161 } 162 /* XXX - check replay cache */ 163 164 /* check sequence number. since MIT krb5 cannot generate a sequence 165 number of zero but instead generates no sequence number, we accept that 166 */ 167 168 if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_SEQUENCE) { 169 if ((safe.safe_body.seq_number == NULL 170 && auth_context->remote_seqnumber != 0) 171 || (safe.safe_body.seq_number != NULL 172 && *safe.safe_body.seq_number != 173 auth_context->remote_seqnumber)) { 174 ret = KRB5KRB_AP_ERR_BADORDER; 175 krb5_clear_error_string (context); 176 goto failure; 177 } 178 auth_context->remote_seqnumber++; 179 } 180 181 ret = verify_checksum (context, auth_context, &safe); 182 if (ret) 183 goto failure; 184 185 outbuf->length = safe.safe_body.user_data.length; 186 outbuf->data = malloc(outbuf->length); 187 if (outbuf->data == NULL) { 188 ret = ENOMEM; 189 krb5_set_error_string (context, "malloc: out of memory"); 190 goto failure; 191 } 192 memcpy (outbuf->data, safe.safe_body.user_data.data, outbuf->length); 193 free_KRB_SAFE (&safe); 194 return 0; 195 failure: 196 free_KRB_SAFE (&safe); 197 return ret; 198 } 199