Expand PMF_FN_* macros.
[netbsd-mini2440.git] / crypto / dist / heimdal / appl / rsh / common.c
blob09d50c369787505f6028ac4a4b81e83eb0f5c49c
1 /*
2 * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "rsh_locl.h"
35 __RCSID("$Heimdal: common.c 17450 2006-05-05 11:11:43Z lha $"
36 "$NetBSD$");
38 #if defined(KRB4) || defined(KRB5)
40 #ifdef KRB5
41 int key_usage = 1026;
43 void *ivec_in[2];
44 void *ivec_out[2];
46 void
47 init_ivecs(int client, int have_errsock)
49 size_t blocksize;
51 krb5_crypto_getblocksize(context, crypto, &blocksize);
53 ivec_in[0] = malloc(blocksize);
54 memset(ivec_in[0], client, blocksize);
56 if(have_errsock) {
57 ivec_in[1] = malloc(blocksize);
58 memset(ivec_in[1], 2 | client, blocksize);
59 } else
60 ivec_in[1] = ivec_in[0];
62 ivec_out[0] = malloc(blocksize);
63 memset(ivec_out[0], !client, blocksize);
65 if(have_errsock) {
66 ivec_out[1] = malloc(blocksize);
67 memset(ivec_out[1], 2 | !client, blocksize);
68 } else
69 ivec_out[1] = ivec_out[0];
71 #endif
74 ssize_t
75 do_read (int fd, void *buf, size_t sz, void *ivec)
77 if (do_encrypt) {
78 #ifdef KRB4
79 if (auth_method == AUTH_KRB4) {
80 return des_enc_read (fd, buf, sz, schedule, &iv);
81 } else
82 #endif /* KRB4 */
83 #ifdef KRB5
84 if(auth_method == AUTH_KRB5) {
85 krb5_error_code ret;
86 uint32_t len, outer_len;
87 int status;
88 krb5_data data;
89 void *edata;
91 ret = krb5_net_read (context, &fd, &len, 4);
92 if (ret <= 0)
93 return ret;
94 len = ntohl(len);
95 if (len > sz)
96 abort ();
97 /* ivec will be non null for protocol version 2 */
98 if(ivec != NULL)
99 outer_len = krb5_get_wrapped_length (context, crypto, len + 4);
100 else
101 outer_len = krb5_get_wrapped_length (context, crypto, len);
102 edata = malloc (outer_len);
103 if (edata == NULL)
104 errx (1, "malloc: cannot allocate %u bytes", outer_len);
105 ret = krb5_net_read (context, &fd, edata, outer_len);
106 if (ret <= 0)
107 return ret;
109 status = krb5_decrypt_ivec(context, crypto, key_usage,
110 edata, outer_len, &data, ivec);
111 free (edata);
113 if (status)
114 krb5_err (context, 1, status, "decrypting data");
115 if(ivec != NULL) {
116 unsigned long l;
117 if(data.length < len + 4)
118 errx (1, "data received is too short");
119 _krb5_get_int(data.data, &l, 4);
120 if(l != len)
121 errx (1, "inconsistency in received data");
122 memcpy (buf, (unsigned char *)data.data+4, len);
123 } else
124 memcpy (buf, data.data, len);
125 krb5_data_free (&data);
126 return len;
127 } else
128 #endif /* KRB5 */
129 abort ();
130 } else
131 return read (fd, buf, sz);
134 ssize_t
135 do_write (int fd, void *buf, size_t sz, void *ivec)
137 if (do_encrypt) {
138 #ifdef KRB4
139 if(auth_method == AUTH_KRB4) {
140 return des_enc_write (fd, buf, sz, schedule, &iv);
141 } else
142 #endif /* KRB4 */
143 #ifdef KRB5
144 if(auth_method == AUTH_KRB5) {
145 krb5_error_code status;
146 krb5_data data;
147 unsigned char len[4];
148 int ret;
150 _krb5_put_int(len, sz, 4);
151 if(ivec != NULL) {
152 unsigned char *tmp = malloc(sz + 4);
153 if(tmp == NULL)
154 err(1, "malloc");
155 _krb5_put_int(tmp, sz, 4);
156 memcpy(tmp + 4, buf, sz);
157 status = krb5_encrypt_ivec(context, crypto, key_usage,
158 tmp, sz + 4, &data, ivec);
159 free(tmp);
160 } else
161 status = krb5_encrypt_ivec(context, crypto, key_usage,
162 buf, sz, &data, ivec);
164 if (status)
165 krb5_err(context, 1, status, "encrypting data");
167 ret = krb5_net_write (context, &fd, len, 4);
168 if (ret != 4)
169 return ret;
170 ret = krb5_net_write (context, &fd, data.data, data.length);
171 if (ret != data.length)
172 return ret;
173 free (data.data);
174 return sz;
175 } else
176 #endif /* KRB5 */
177 abort();
178 } else
179 return write (fd, buf, sz);
181 #endif /* KRB4 || KRB5 */