]>
Commit | Line | Data |
---|---|---|
d02b48c6 | 1 | /* ssl/s2_pkt.c */ |
58964a49 | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
3 | * All rights reserved. |
4 | * | |
5 | * This package is an SSL implementation written | |
6 | * by Eric Young (eay@cryptsoft.com). | |
7 | * The implementation was written so as to conform with Netscapes SSL. | |
8 | * | |
9 | * This library is free for commercial and non-commercial use as long as | |
10 | * the following conditions are aheared to. The following conditions | |
11 | * apply to all code found in this distribution, be it the RC4, RSA, | |
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
13 | * included with this distribution is covered by the same copyright terms | |
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
15 | * | |
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | |
17 | * the code are not to be removed. | |
18 | * If this package is used in a product, Eric Young should be given attribution | |
19 | * as the author of the parts of the library used. | |
20 | * This can be in the form of a textual message at program startup or | |
21 | * in documentation (online or textual) provided with the package. | |
22 | * | |
23 | * Redistribution and use in source and binary forms, with or without | |
24 | * modification, are permitted provided that the following conditions | |
25 | * are met: | |
26 | * 1. Redistributions of source code must retain the copyright | |
27 | * notice, this list of conditions and the following disclaimer. | |
28 | * 2. Redistributions in binary form must reproduce the above copyright | |
29 | * notice, this list of conditions and the following disclaimer in the | |
30 | * documentation and/or other materials provided with the distribution. | |
31 | * 3. All advertising materials mentioning features or use of this software | |
32 | * must display the following acknowledgement: | |
33 | * "This product includes cryptographic software written by | |
34 | * Eric Young (eay@cryptsoft.com)" | |
35 | * The word 'cryptographic' can be left out if the rouines from the library | |
36 | * being used are not cryptographic related :-). | |
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | |
38 | * the apps directory (application code) you must include an acknowledgement: | |
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
40 | * | |
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | |
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
51 | * SUCH DAMAGE. | |
52 | * | |
53 | * The licence and distribution terms for any publically available version or | |
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
55 | * copied and put under another distribution licence | |
56 | * [including the GNU Public Licence.] | |
57 | */ | |
5a4fbc69 | 58 | /* ==================================================================== |
cf82191d | 59 | * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. |
5a4fbc69 BM |
60 | * |
61 | * Redistribution and use in source and binary forms, with or without | |
62 | * modification, are permitted provided that the following conditions | |
63 | * are met: | |
64 | * | |
65 | * 1. Redistributions of source code must retain the above copyright | |
66 | * notice, this list of conditions and the following disclaimer. | |
67 | * | |
68 | * 2. Redistributions in binary form must reproduce the above copyright | |
69 | * notice, this list of conditions and the following disclaimer in | |
70 | * the documentation and/or other materials provided with the | |
71 | * distribution. | |
72 | * | |
73 | * 3. All advertising materials mentioning features or use of this | |
74 | * software must display the following acknowledgment: | |
75 | * "This product includes software developed by the OpenSSL Project | |
76 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | |
77 | * | |
78 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
79 | * endorse or promote products derived from this software without | |
80 | * prior written permission. For written permission, please contact | |
81 | * openssl-core@openssl.org. | |
82 | * | |
83 | * 5. Products derived from this software may not be called "OpenSSL" | |
84 | * nor may "OpenSSL" appear in their names without prior written | |
85 | * permission of the OpenSSL Project. | |
86 | * | |
87 | * 6. Redistributions of any form whatsoever must retain the following | |
88 | * acknowledgment: | |
89 | * "This product includes software developed by the OpenSSL Project | |
90 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | |
91 | * | |
92 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
93 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
94 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
95 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
96 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
97 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
98 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
99 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
100 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
101 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
102 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
103 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
104 | * ==================================================================== | |
105 | * | |
106 | * This product includes cryptographic software written by Eric Young | |
107 | * (eay@cryptsoft.com). This product includes software written by Tim | |
108 | * Hudson (tjh@cryptsoft.com). | |
109 | * | |
110 | */ | |
d02b48c6 | 111 | |
aa82db4f | 112 | #include "ssl_locl.h" |
bc36ee62 | 113 | #ifndef OPENSSL_NO_SSL2 |
d02b48c6 RE |
114 | #include <stdio.h> |
115 | #include <errno.h> | |
116 | #define USE_SOCKETS | |
d02b48c6 | 117 | |
d02b48c6 | 118 | static int read_n(SSL *s,unsigned int n,unsigned int max,unsigned int extend); |
0eab41fb | 119 | static int n_do_ssl_write(SSL *s, const unsigned char *buf, unsigned int len); |
61f5b6f3 | 120 | static int write_pending(SSL *s, const unsigned char *buf, unsigned int len); |
d02b48c6 | 121 | static int ssl_mt_error(int n); |
d02b48c6 | 122 | |
d02b48c6 | 123 | |
5a4fbc69 | 124 | /* SSL 2.0 imlementation for SSL_read/SSL_peek - |
d02b48c6 RE |
125 | * This routine will return 0 to len bytes, decrypted etc if required. |
126 | */ | |
5a4fbc69 | 127 | static int ssl2_read_internal(SSL *s, void *buf, int len, int peek) |
d02b48c6 RE |
128 | { |
129 | int n; | |
130 | unsigned char mac[MAX_MAC_SIZE]; | |
131 | unsigned char *p; | |
132 | int i; | |
d7ecd422 | 133 | int mac_size; |
d02b48c6 | 134 | |
a0aae68c | 135 | ssl2_read_again: |
d02b48c6 RE |
136 | if (SSL_in_init(s) && !s->in_handshake) |
137 | { | |
138 | n=s->handshake_func(s); | |
139 | if (n < 0) return(n); | |
140 | if (n == 0) | |
141 | { | |
5a4fbc69 | 142 | SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_SSL_HANDSHAKE_FAILURE); |
d02b48c6 RE |
143 | return(-1); |
144 | } | |
145 | } | |
146 | ||
58964a49 | 147 | clear_sys_error(); |
d02b48c6 RE |
148 | s->rwstate=SSL_NOTHING; |
149 | if (len <= 0) return(len); | |
150 | ||
151 | if (s->s2->ract_data_length != 0) /* read from buffer */ | |
152 | { | |
153 | if (len > s->s2->ract_data_length) | |
154 | n=s->s2->ract_data_length; | |
155 | else | |
156 | n=len; | |
157 | ||
158 | memcpy(buf,s->s2->ract_data,(unsigned int)n); | |
a0aae68c BM |
159 | if (!peek) |
160 | { | |
161 | s->s2->ract_data_length-=n; | |
162 | s->s2->ract_data+=n; | |
163 | if (s->s2->ract_data_length == 0) | |
164 | s->rstate=SSL_ST_READ_HEADER; | |
165 | } | |
166 | ||
d02b48c6 RE |
167 | return(n); |
168 | } | |
169 | ||
a0aae68c BM |
170 | /* s->s2->ract_data_length == 0 |
171 | * | |
172 | * Fill the buffer, then goto ssl2_read_again. | |
173 | */ | |
174 | ||
d02b48c6 RE |
175 | if (s->rstate == SSL_ST_READ_HEADER) |
176 | { | |
177 | if (s->first_packet) | |
178 | { | |
179 | n=read_n(s,5,SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2,0); | |
180 | if (n <= 0) return(n); /* error or non-blocking */ | |
181 | s->first_packet=0; | |
182 | p=s->packet; | |
183 | if (!((p[0] & 0x80) && ( | |
184 | (p[2] == SSL2_MT_CLIENT_HELLO) || | |
185 | (p[2] == SSL2_MT_SERVER_HELLO)))) | |
186 | { | |
5a4fbc69 | 187 | SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_NON_SSLV2_INITIAL_PACKET); |
d02b48c6 RE |
188 | return(-1); |
189 | } | |
190 | } | |
191 | else | |
192 | { | |
193 | n=read_n(s,2,SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2,0); | |
194 | if (n <= 0) return(n); /* error or non-blocking */ | |
195 | } | |
196 | /* part read stuff */ | |
197 | ||
198 | s->rstate=SSL_ST_READ_BODY; | |
199 | p=s->packet; | |
200 | /* Do header */ | |
201 | /*s->s2->padding=0;*/ | |
202 | s->s2->escape=0; | |
203 | s->s2->rlength=(((unsigned int)p[0])<<8)|((unsigned int)p[1]); | |
204 | if ((p[0] & TWO_BYTE_BIT)) /* Two byte header? */ | |
205 | { | |
206 | s->s2->three_byte_header=0; | |
207 | s->s2->rlength&=TWO_BYTE_MASK; | |
208 | } | |
209 | else | |
210 | { | |
211 | s->s2->three_byte_header=1; | |
212 | s->s2->rlength&=THREE_BYTE_MASK; | |
213 | ||
214 | /* security >s2->escape */ | |
215 | s->s2->escape=((p[0] & SEC_ESC_BIT))?1:0; | |
216 | } | |
217 | } | |
218 | ||
219 | if (s->rstate == SSL_ST_READ_BODY) | |
220 | { | |
221 | n=s->s2->rlength+2+s->s2->three_byte_header; | |
222 | if (n > (int)s->packet_length) | |
223 | { | |
224 | n-=s->packet_length; | |
225 | i=read_n(s,(unsigned int)n,(unsigned int)n,1); | |
226 | if (i <= 0) return(i); /* ERROR */ | |
227 | } | |
228 | ||
229 | p= &(s->packet[2]); | |
230 | s->rstate=SSL_ST_READ_HEADER; | |
231 | if (s->s2->three_byte_header) | |
232 | s->s2->padding= *(p++); | |
233 | else s->s2->padding=0; | |
234 | ||
235 | /* Data portion */ | |
236 | if (s->s2->clear_text) | |
237 | { | |
ee60d9fb | 238 | mac_size = 0; |
d02b48c6 RE |
239 | s->s2->mac_data=p; |
240 | s->s2->ract_data=p; | |
ee60d9fb BM |
241 | if (s->s2->padding) |
242 | { | |
243 | SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_ILLEGAL_PADDING); | |
244 | return(-1); | |
245 | } | |
d02b48c6 RE |
246 | } |
247 | else | |
248 | { | |
b948e2c5 | 249 | mac_size=EVP_MD_CTX_size(s->read_hash); |
0eab41fb BL |
250 | if (mac_size < 0) |
251 | return -1; | |
54a656ef | 252 | OPENSSL_assert(mac_size <= MAX_MAC_SIZE); |
d02b48c6 RE |
253 | s->s2->mac_data=p; |
254 | s->s2->ract_data= &p[mac_size]; | |
ee60d9fb BM |
255 | if (s->s2->padding + mac_size > s->s2->rlength) |
256 | { | |
257 | SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_ILLEGAL_PADDING); | |
258 | return(-1); | |
259 | } | |
d02b48c6 RE |
260 | } |
261 | ||
262 | s->s2->ract_data_length=s->s2->rlength; | |
263 | /* added a check for length > max_size in case | |
264 | * encryption was not turned on yet due to an error */ | |
265 | if ((!s->s2->clear_text) && | |
d7ecd422 | 266 | (s->s2->rlength >= (unsigned int)mac_size)) |
d02b48c6 | 267 | { |
ac3dc3ee MC |
268 | if(!ssl2_enc(s,0)) |
269 | { | |
270 | SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_DECRYPTION_FAILED); | |
271 | return(-1); | |
272 | } | |
d02b48c6 RE |
273 | s->s2->ract_data_length-=mac_size; |
274 | ssl2_mac(s,mac,0); | |
275 | s->s2->ract_data_length-=s->s2->padding; | |
f5cd3561 | 276 | if ( (CRYPTO_memcmp(mac,s->s2->mac_data,mac_size) != 0) || |
d02b48c6 RE |
277 | (s->s2->rlength%EVP_CIPHER_CTX_block_size(s->enc_read_ctx) != 0)) |
278 | { | |
5a4fbc69 | 279 | SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_BAD_MAC_DECODE); |
58964a49 | 280 | return(-1); |
d02b48c6 RE |
281 | } |
282 | } | |
283 | INC32(s->s2->read_sequence); /* expect next number */ | |
284 | /* s->s2->ract_data is now available for processing */ | |
285 | ||
a0aae68c BM |
286 | /* Possibly the packet that we just read had 0 actual data bytes. |
287 | * (SSLeay/OpenSSL itself never sends such packets; see ssl2_write.) | |
288 | * In this case, returning 0 would be interpreted by the caller | |
289 | * as indicating EOF, so it's not a good idea. Instead, we just | |
290 | * continue reading; thus ssl2_read_internal may have to process | |
291 | * multiple packets before it can return. | |
292 | * | |
293 | * [Note that using select() for blocking sockets *never* guarantees | |
85f48f7e | 294 | * that the next SSL_read will not block -- the available |
a0aae68c BM |
295 | * data may contain incomplete packets, and except for SSL 2, |
296 | * renegotiation can confuse things even more.] */ | |
85f48f7e BM |
297 | |
298 | goto ssl2_read_again; /* This should really be | |
a0aae68c BM |
299 | * "return ssl2_read(s,buf,len)", |
300 | * but that would allow for | |
301 | * denial-of-service attacks if a | |
302 | * C compiler is used that does not | |
303 | * recognize end-recursion. */ | |
d02b48c6 RE |
304 | } |
305 | else | |
306 | { | |
5a4fbc69 | 307 | SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_BAD_STATE); |
58964a49 | 308 | return(-1); |
d02b48c6 RE |
309 | } |
310 | } | |
311 | ||
5a4fbc69 BM |
312 | int ssl2_read(SSL *s, void *buf, int len) |
313 | { | |
314 | return ssl2_read_internal(s, buf, len, 0); | |
315 | } | |
316 | ||
e34cfcf7 | 317 | int ssl2_peek(SSL *s, void *buf, int len) |
5a4fbc69 BM |
318 | { |
319 | return ssl2_read_internal(s, buf, len, 1); | |
320 | } | |
321 | ||
6b691a5c UM |
322 | static int read_n(SSL *s, unsigned int n, unsigned int max, |
323 | unsigned int extend) | |
d02b48c6 RE |
324 | { |
325 | int i,off,newb; | |
326 | ||
327 | /* if there is stuff still in the buffer from a previous read, | |
328 | * and there is more than we want, take some. */ | |
329 | if (s->s2->rbuf_left >= (int)n) | |
330 | { | |
331 | if (extend) | |
332 | s->packet_length+=n; | |
333 | else | |
334 | { | |
335 | s->packet= &(s->s2->rbuf[s->s2->rbuf_offs]); | |
336 | s->packet_length=n; | |
337 | } | |
338 | s->s2->rbuf_left-=n; | |
339 | s->s2->rbuf_offs+=n; | |
340 | return(n); | |
341 | } | |
342 | ||
343 | if (!s->read_ahead) max=n; | |
344 | if (max > (unsigned int)(SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2)) | |
345 | max=SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2; | |
346 | ||
347 | ||
348 | /* Else we want more than we have. | |
349 | * First, if there is some left or we want to extend */ | |
350 | off=0; | |
351 | if ((s->s2->rbuf_left != 0) || ((s->packet_length != 0) && extend)) | |
352 | { | |
353 | newb=s->s2->rbuf_left; | |
354 | if (extend) | |
355 | { | |
356 | off=s->packet_length; | |
357 | if (s->packet != s->s2->rbuf) | |
358 | memcpy(s->s2->rbuf,s->packet, | |
359 | (unsigned int)newb+off); | |
360 | } | |
361 | else if (s->s2->rbuf_offs != 0) | |
362 | { | |
363 | memcpy(s->s2->rbuf,&(s->s2->rbuf[s->s2->rbuf_offs]), | |
364 | (unsigned int)newb); | |
365 | s->s2->rbuf_offs=0; | |
366 | } | |
367 | s->s2->rbuf_left=0; | |
368 | } | |
369 | else | |
370 | newb=0; | |
371 | ||
372 | /* off is the offset to start writing too. | |
373 | * r->s2->rbuf_offs is the 'unread data', now 0. | |
374 | * newb is the number of new bytes so far | |
375 | */ | |
376 | s->packet=s->s2->rbuf; | |
377 | while (newb < (int)n) | |
378 | { | |
58964a49 | 379 | clear_sys_error(); |
d02b48c6 RE |
380 | if (s->rbio != NULL) |
381 | { | |
382 | s->rwstate=SSL_READING; | |
383 | i=BIO_read(s->rbio,(char *)&(s->s2->rbuf[off+newb]), | |
384 | max-newb); | |
385 | } | |
386 | else | |
387 | { | |
388 | SSLerr(SSL_F_READ_N,SSL_R_READ_BIO_NOT_SET); | |
389 | i= -1; | |
390 | } | |
391 | #ifdef PKT_DEBUG | |
392 | if (s->debug & 0x01) sleep(1); | |
393 | #endif | |
394 | if (i <= 0) | |
395 | { | |
396 | s->s2->rbuf_left+=newb; | |
397 | return(i); | |
398 | } | |
399 | newb+=i; | |
400 | } | |
401 | ||
402 | /* record unread data */ | |
403 | if (newb > (int)n) | |
404 | { | |
405 | s->s2->rbuf_offs=n+off; | |
406 | s->s2->rbuf_left=newb-n; | |
407 | } | |
408 | else | |
409 | { | |
410 | s->s2->rbuf_offs=0; | |
411 | s->s2->rbuf_left=0; | |
412 | } | |
413 | if (extend) | |
414 | s->packet_length+=n; | |
415 | else | |
416 | s->packet_length=n; | |
417 | s->rwstate=SSL_NOTHING; | |
418 | return(n); | |
419 | } | |
420 | ||
61f5b6f3 | 421 | int ssl2_write(SSL *s, const void *_buf, int len) |
d02b48c6 | 422 | { |
61f5b6f3 | 423 | const unsigned char *buf=_buf; |
d02b48c6 RE |
424 | unsigned int n,tot; |
425 | int i; | |
426 | ||
427 | if (SSL_in_init(s) && !s->in_handshake) | |
428 | { | |
429 | i=s->handshake_func(s); | |
430 | if (i < 0) return(i); | |
431 | if (i == 0) | |
432 | { | |
433 | SSLerr(SSL_F_SSL2_WRITE,SSL_R_SSL_HANDSHAKE_FAILURE); | |
434 | return(-1); | |
435 | } | |
436 | } | |
437 | ||
438 | if (s->error) | |
439 | { | |
440 | ssl2_write_error(s); | |
441 | if (s->error) | |
442 | return(-1); | |
443 | } | |
444 | ||
58964a49 | 445 | clear_sys_error(); |
d02b48c6 RE |
446 | s->rwstate=SSL_NOTHING; |
447 | if (len <= 0) return(len); | |
448 | ||
449 | tot=s->s2->wnum; | |
450 | s->s2->wnum=0; | |
451 | ||
452 | n=(len-tot); | |
453 | for (;;) | |
454 | { | |
0eab41fb | 455 | i=n_do_ssl_write(s,&(buf[tot]),n); |
d02b48c6 RE |
456 | if (i <= 0) |
457 | { | |
458 | s->s2->wnum=tot; | |
459 | return(i); | |
460 | } | |
e1056435 | 461 | if ((i == (int)n) || |
11b1adad | 462 | (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE)) |
e1056435 BM |
463 | { |
464 | return(tot+i); | |
465 | } | |
466 | ||
d02b48c6 RE |
467 | n-=i; |
468 | tot+=i; | |
469 | } | |
470 | } | |
471 | ||
61f5b6f3 | 472 | static int write_pending(SSL *s, const unsigned char *buf, unsigned int len) |
d02b48c6 RE |
473 | { |
474 | int i; | |
475 | ||
476 | /* s->s2->wpend_len != 0 MUST be true. */ | |
477 | ||
478 | /* check that they have given us the same buffer to | |
479 | * write */ | |
e1056435 BM |
480 | if ((s->s2->wpend_tot > (int)len) || |
481 | ((s->s2->wpend_buf != buf) && | |
1afd8b39 | 482 | !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))) |
d02b48c6 RE |
483 | { |
484 | SSLerr(SSL_F_WRITE_PENDING,SSL_R_BAD_WRITE_RETRY); | |
58964a49 | 485 | return(-1); |
d02b48c6 RE |
486 | } |
487 | ||
488 | for (;;) | |
489 | { | |
58964a49 | 490 | clear_sys_error(); |
d02b48c6 RE |
491 | if (s->wbio != NULL) |
492 | { | |
493 | s->rwstate=SSL_WRITING; | |
494 | i=BIO_write(s->wbio, | |
495 | (char *)&(s->s2->write_ptr[s->s2->wpend_off]), | |
496 | (unsigned int)s->s2->wpend_len); | |
497 | } | |
498 | else | |
499 | { | |
500 | SSLerr(SSL_F_WRITE_PENDING,SSL_R_WRITE_BIO_NOT_SET); | |
501 | i= -1; | |
502 | } | |
503 | #ifdef PKT_DEBUG | |
504 | if (s->debug & 0x01) sleep(1); | |
505 | #endif | |
506 | if (i == s->s2->wpend_len) | |
507 | { | |
508 | s->s2->wpend_len=0; | |
509 | s->rwstate=SSL_NOTHING; | |
510 | return(s->s2->wpend_ret); | |
511 | } | |
512 | else if (i <= 0) | |
513 | return(i); | |
514 | s->s2->wpend_off+=i; | |
515 | s->s2->wpend_len-=i; | |
516 | } | |
517 | } | |
518 | ||
0eab41fb | 519 | static int n_do_ssl_write(SSL *s, const unsigned char *buf, unsigned int len) |
d02b48c6 | 520 | { |
b3f34078 | 521 | unsigned int j,k,olen,p,bs; |
c7ba2149 | 522 | int mac_size; |
d02b48c6 RE |
523 | register unsigned char *pp; |
524 | ||
525 | olen=len; | |
526 | ||
527 | /* first check if there is data from an encryption waiting to | |
528 | * be sent - it must be sent because the other end is waiting. | |
529 | * This will happen with non-blocking IO. We print it and then | |
530 | * return. | |
531 | */ | |
532 | if (s->s2->wpend_len != 0) return(write_pending(s,buf,len)); | |
533 | ||
534 | /* set mac_size to mac size */ | |
535 | if (s->s2->clear_text) | |
536 | mac_size=0; | |
537 | else | |
0eab41fb | 538 | { |
b948e2c5 | 539 | mac_size=EVP_MD_CTX_size(s->write_hash); |
0eab41fb BL |
540 | if (mac_size < 0) |
541 | return -1; | |
542 | } | |
d02b48c6 RE |
543 | |
544 | /* lets set the pad p */ | |
545 | if (s->s2->clear_text) | |
546 | { | |
547 | if (len > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER) | |
548 | len=SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER; | |
549 | p=0; | |
550 | s->s2->three_byte_header=0; | |
551 | /* len=len; */ | |
552 | } | |
553 | else | |
554 | { | |
555 | bs=EVP_CIPHER_CTX_block_size(s->enc_read_ctx); | |
556 | j=len+mac_size; | |
3880cd35 BM |
557 | /* Two-byte headers allow for a larger record length than |
558 | * three-byte headers, but we can't use them if we need | |
559 | * padding or if we have to set the escape bit. */ | |
d02b48c6 RE |
560 | if ((j > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) && |
561 | (!s->s2->escape)) | |
562 | { | |
563 | if (j > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER) | |
564 | j=SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER; | |
565 | /* set k to the max number of bytes with 2 | |
566 | * byte header */ | |
567 | k=j-(j%bs); | |
568 | /* how many data bytes? */ | |
569 | len=k-mac_size; | |
570 | s->s2->three_byte_header=0; | |
571 | p=0; | |
572 | } | |
573 | else if ((bs <= 1) && (!s->s2->escape)) | |
574 | { | |
c695ebe2 MC |
575 | /*- |
576 | * j <= SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER, thus | |
577 | * j < SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER | |
578 | */ | |
d02b48c6 RE |
579 | s->s2->three_byte_header=0; |
580 | p=0; | |
581 | } | |
3880cd35 | 582 | else /* we may have to use a 3 byte header */ |
d02b48c6 | 583 | { |
c695ebe2 MC |
584 | /*- |
585 | * If s->s2->escape is not set, then | |
3880cd35 | 586 | * j <= SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER, and thus |
c695ebe2 MC |
587 | * j < SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER. |
588 | */ | |
d02b48c6 RE |
589 | p=(j%bs); |
590 | p=(p == 0)?0:(bs-p); | |
591 | if (s->s2->escape) | |
3880cd35 | 592 | { |
d02b48c6 | 593 | s->s2->three_byte_header=1; |
3880cd35 BM |
594 | if (j > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) |
595 | j=SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER; | |
596 | } | |
d02b48c6 RE |
597 | else |
598 | s->s2->three_byte_header=(p == 0)?0:1; | |
599 | } | |
600 | } | |
3880cd35 | 601 | |
c695ebe2 MC |
602 | /*- |
603 | * Now | |
3880cd35 BM |
604 | * j <= SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER |
605 | * holds, and if s->s2->three_byte_header is set, then even | |
606 | * j <= SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER. | |
607 | */ | |
608 | ||
d02b48c6 RE |
609 | /* mac_size is the number of MAC bytes |
610 | * len is the number of data bytes we are going to send | |
611 | * p is the number of padding bytes | |
3880cd35 | 612 | * (if it is a two-byte header, then p == 0) */ |
d02b48c6 RE |
613 | |
614 | s->s2->wlength=len; | |
615 | s->s2->padding=p; | |
616 | s->s2->mac_data= &(s->s2->wbuf[3]); | |
617 | s->s2->wact_data= &(s->s2->wbuf[3+mac_size]); | |
618 | /* we copy the data into s->s2->wbuf */ | |
619 | memcpy(s->s2->wact_data,buf,len); | |
d02b48c6 | 620 | if (p) |
ee60d9fb | 621 | memset(&(s->s2->wact_data[len]),0,p); /* arbitrary padding */ |
d02b48c6 RE |
622 | |
623 | if (!s->s2->clear_text) | |
624 | { | |
625 | s->s2->wact_data_length=len+p; | |
626 | ssl2_mac(s,s->s2->mac_data,1); | |
627 | s->s2->wlength+=p+mac_size; | |
ac3dc3ee MC |
628 | if(ssl2_enc(s,1) < 1) |
629 | return -1; | |
d02b48c6 RE |
630 | } |
631 | ||
632 | /* package up the header */ | |
633 | s->s2->wpend_len=s->s2->wlength; | |
634 | if (s->s2->three_byte_header) /* 3 byte header */ | |
635 | { | |
636 | pp=s->s2->mac_data; | |
637 | pp-=3; | |
638 | pp[0]=(s->s2->wlength>>8)&(THREE_BYTE_MASK>>8); | |
639 | if (s->s2->escape) pp[0]|=SEC_ESC_BIT; | |
640 | pp[1]=s->s2->wlength&0xff; | |
641 | pp[2]=s->s2->padding; | |
642 | s->s2->wpend_len+=3; | |
643 | } | |
644 | else | |
645 | { | |
646 | pp=s->s2->mac_data; | |
647 | pp-=2; | |
648 | pp[0]=((s->s2->wlength>>8)&(TWO_BYTE_MASK>>8))|TWO_BYTE_BIT; | |
649 | pp[1]=s->s2->wlength&0xff; | |
650 | s->s2->wpend_len+=2; | |
651 | } | |
652 | s->s2->write_ptr=pp; | |
653 | ||
654 | INC32(s->s2->write_sequence); /* expect next number */ | |
655 | ||
656 | /* lets try to actually write the data */ | |
657 | s->s2->wpend_tot=olen; | |
e778802f | 658 | s->s2->wpend_buf=buf; |
d02b48c6 RE |
659 | |
660 | s->s2->wpend_ret=len; | |
661 | ||
662 | s->s2->wpend_off=0; | |
663 | return(write_pending(s,buf,olen)); | |
664 | } | |
665 | ||
6b691a5c | 666 | int ssl2_part_read(SSL *s, unsigned long f, int i) |
d02b48c6 RE |
667 | { |
668 | unsigned char *p; | |
669 | int j; | |
670 | ||
d02b48c6 RE |
671 | if (i < 0) |
672 | { | |
673 | /* ssl2_return_error(s); */ | |
674 | /* for non-blocking io, | |
cf82191d | 675 | * this is not necessarily fatal */ |
d02b48c6 RE |
676 | return(i); |
677 | } | |
678 | else | |
679 | { | |
680 | s->init_num+=i; | |
cf82191d BM |
681 | |
682 | /* Check for error. While there are recoverable errors, | |
683 | * this function is not called when those must be expected; | |
684 | * any error detected here is fatal. */ | |
685 | if (s->init_num >= 3) | |
686 | { | |
687 | p=(unsigned char *)s->init_buf->data; | |
688 | if (p[0] == SSL2_MT_ERROR) | |
689 | { | |
690 | j=(p[1]<<8)|p[2]; | |
691 | SSLerr((int)f,ssl_mt_error(j)); | |
692 | s->init_num -= 3; | |
693 | if (s->init_num > 0) | |
694 | memmove(p, p+3, s->init_num); | |
695 | } | |
696 | } | |
697 | ||
698 | /* If it's not an error message, we have some error anyway -- | |
699 | * the message was shorter than expected. This too is treated | |
700 | * as fatal (at least if SSL_get_error is asked for its opinion). */ | |
d02b48c6 RE |
701 | return(0); |
702 | } | |
703 | } | |
704 | ||
6b691a5c | 705 | int ssl2_do_write(SSL *s) |
d02b48c6 RE |
706 | { |
707 | int ret; | |
708 | ||
61f5b6f3 | 709 | ret=ssl2_write(s,&s->init_buf->data[s->init_off],s->init_num); |
d02b48c6 | 710 | if (ret == s->init_num) |
cf82191d BM |
711 | { |
712 | if (s->msg_callback) | |
713 | s->msg_callback(1, s->version, 0, s->init_buf->data, (size_t)(s->init_off + s->init_num), s, s->msg_callback_arg); | |
d02b48c6 | 714 | return(1); |
cf82191d | 715 | } |
d02b48c6 RE |
716 | if (ret < 0) |
717 | return(-1); | |
718 | s->init_off+=ret; | |
719 | s->init_num-=ret; | |
720 | return(0); | |
721 | } | |
722 | ||
6b691a5c | 723 | static int ssl_mt_error(int n) |
d02b48c6 RE |
724 | { |
725 | int ret; | |
726 | ||
727 | switch (n) | |
728 | { | |
729 | case SSL2_PE_NO_CIPHER: | |
730 | ret=SSL_R_PEER_ERROR_NO_CIPHER; | |
731 | break; | |
732 | case SSL2_PE_NO_CERTIFICATE: | |
733 | ret=SSL_R_PEER_ERROR_NO_CERTIFICATE; | |
734 | break; | |
735 | case SSL2_PE_BAD_CERTIFICATE: | |
736 | ret=SSL_R_PEER_ERROR_CERTIFICATE; | |
737 | break; | |
738 | case SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE: | |
739 | ret=SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE; | |
740 | break; | |
741 | default: | |
742 | ret=SSL_R_UNKNOWN_REMOTE_ERROR_TYPE; | |
743 | break; | |
744 | } | |
745 | return(ret); | |
746 | } | |
bc36ee62 | 747 | #else /* !OPENSSL_NO_SSL2 */ |
aa82db4f UM |
748 | |
749 | # if PEDANTIC | |
750 | static void *dummy=&dummy; | |
751 | # endif | |
752 | ||
753 | #endif |