1 /* 2 * Copyright (c) 1997 - 1999, 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 "rsh_locl.h" 35 RCSID("$Id: common.c,v 1.16 2002/09/04 15:50:36 assar Exp $"); 36 37 #if defined(KRB4) || defined(KRB5) 38 39 #ifdef KRB5 40 int key_usage = 1026; 41 42 void *ivec_in[2]; 43 void *ivec_out[2]; 44 45 void 46 init_ivecs(int client) 47 { 48 size_t blocksize; 49 50 krb5_crypto_getblocksize(context, crypto, &blocksize); 51 52 ivec_in[0] = malloc(blocksize); 53 memset(ivec_in[0], client, blocksize); 54 55 ivec_in[1] = malloc(blocksize); 56 memset(ivec_in[1], 2 | client, blocksize); 57 58 ivec_out[0] = malloc(blocksize); 59 memset(ivec_out[0], !client, blocksize); 60 61 ivec_out[1] = malloc(blocksize); 62 memset(ivec_out[1], 2 | !client, blocksize); 63 } 64 #endif 65 66 67 ssize_t 68 do_read (int fd, void *buf, size_t sz, void *ivec) 69 { 70 if (do_encrypt) { 71 #ifdef KRB4 72 if (auth_method == AUTH_KRB4) { 73 return des_enc_read (fd, buf, sz, schedule, &iv); 74 } else 75 #endif /* KRB4 */ 76 #ifdef KRB5 77 if(auth_method == AUTH_KRB5) { 78 krb5_error_code ret; 79 u_int32_t len, outer_len; 80 int status; 81 krb5_data data; 82 void *edata; 83 84 ret = krb5_net_read (context, &fd, &len, 4); 85 if (ret <= 0) 86 return ret; 87 len = ntohl(len); 88 if (len > sz) 89 abort (); 90 /* ivec will be non null for protocol version 2 */ 91 if(ivec != NULL) 92 outer_len = krb5_get_wrapped_length (context, crypto, len + 4); 93 else 94 outer_len = krb5_get_wrapped_length (context, crypto, len); 95 edata = malloc (outer_len); 96 if (edata == NULL) 97 errx (1, "malloc: cannot allocate %u bytes", outer_len); 98 ret = krb5_net_read (context, &fd, edata, outer_len); 99 if (ret <= 0) 100 return ret; 101 102 status = krb5_decrypt_ivec(context, crypto, key_usage, 103 edata, outer_len, &data, ivec); 104 free (edata); 105 106 if (status) 107 krb5_err (context, 1, status, "decrypting data"); 108 if(ivec != NULL) { 109 unsigned long l; 110 if(data.length < len + 4) 111 errx (1, "data received is too short"); 112 _krb5_get_int(data.data, &l, 4); 113 if(l != len) 114 errx (1, "inconsistency in received data"); 115 memcpy (buf, (unsigned char *)data.data+4, len); 116 } else 117 memcpy (buf, data.data, len); 118 krb5_data_free (&data); 119 return len; 120 } else 121 #endif /* KRB5 */ 122 abort (); 123 } else 124 return read (fd, buf, sz); 125 } 126 127 ssize_t 128 do_write (int fd, void *buf, size_t sz, void *ivec) 129 { 130 if (do_encrypt) { 131 #ifdef KRB4 132 if(auth_method == AUTH_KRB4) { 133 return des_enc_write (fd, buf, sz, schedule, &iv); 134 } else 135 #endif /* KRB4 */ 136 #ifdef KRB5 137 if(auth_method == AUTH_KRB5) { 138 krb5_error_code status; 139 krb5_data data; 140 unsigned char len[4]; 141 int ret; 142 143 _krb5_put_int(len, sz, 4); 144 if(ivec != NULL) { 145 unsigned char *tmp = malloc(sz + 4); 146 if(tmp == NULL) 147 err(1, "malloc"); 148 _krb5_put_int(tmp, sz, 4); 149 memcpy(tmp + 4, buf, sz); 150 status = krb5_encrypt_ivec(context, crypto, key_usage, 151 tmp, sz + 4, &data, ivec); 152 free(tmp); 153 } else 154 status = krb5_encrypt_ivec(context, crypto, key_usage, 155 buf, sz, &data, ivec); 156 157 if (status) 158 krb5_err(context, 1, status, "encrypting data"); 159 160 ret = krb5_net_write (context, &fd, len, 4); 161 if (ret != 4) 162 return ret; 163 ret = krb5_net_write (context, &fd, data.data, data.length); 164 if (ret != data.length) 165 return ret; 166 free (data.data); 167 return sz; 168 } else 169 #endif /* KRB5 */ 170 abort(); 171 } else 172 return write (fd, buf, sz); 173 } 174 #endif /* KRB4 || KRB5 */ 175