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