]>
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 BM |
58 | /* ==================================================================== |
59 | * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. | |
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); |
61f5b6f3 BL |
119 | static int do_ssl_write(SSL *s, const unsigned char *buf, unsigned int len); |
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; | |
133 | unsigned int mac_size=0; | |
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 | { | |
238 | s->s2->mac_data=p; | |
239 | s->s2->ract_data=p; | |
240 | s->s2->pad_data=NULL; | |
241 | } | |
242 | else | |
243 | { | |
244 | mac_size=EVP_MD_size(s->read_hash); | |
245 | s->s2->mac_data=p; | |
246 | s->s2->ract_data= &p[mac_size]; | |
247 | s->s2->pad_data= &p[mac_size+ | |
248 | s->s2->rlength-s->s2->padding]; | |
249 | } | |
250 | ||
251 | s->s2->ract_data_length=s->s2->rlength; | |
252 | /* added a check for length > max_size in case | |
253 | * encryption was not turned on yet due to an error */ | |
254 | if ((!s->s2->clear_text) && | |
255 | (s->s2->rlength >= mac_size)) | |
256 | { | |
257 | ssl2_enc(s,0); | |
258 | s->s2->ract_data_length-=mac_size; | |
259 | ssl2_mac(s,mac,0); | |
260 | s->s2->ract_data_length-=s->s2->padding; | |
261 | if ( (memcmp(mac,s->s2->mac_data, | |
262 | (unsigned int)mac_size) != 0) || | |
263 | (s->s2->rlength%EVP_CIPHER_CTX_block_size(s->enc_read_ctx) != 0)) | |
264 | { | |
5a4fbc69 | 265 | SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_BAD_MAC_DECODE); |
58964a49 | 266 | return(-1); |
d02b48c6 RE |
267 | } |
268 | } | |
269 | INC32(s->s2->read_sequence); /* expect next number */ | |
270 | /* s->s2->ract_data is now available for processing */ | |
271 | ||
a0aae68c BM |
272 | /* Possibly the packet that we just read had 0 actual data bytes. |
273 | * (SSLeay/OpenSSL itself never sends such packets; see ssl2_write.) | |
274 | * In this case, returning 0 would be interpreted by the caller | |
275 | * as indicating EOF, so it's not a good idea. Instead, we just | |
276 | * continue reading; thus ssl2_read_internal may have to process | |
277 | * multiple packets before it can return. | |
278 | * | |
279 | * [Note that using select() for blocking sockets *never* guarantees | |
85f48f7e | 280 | * that the next SSL_read will not block -- the available |
a0aae68c BM |
281 | * data may contain incomplete packets, and except for SSL 2, |
282 | * renegotiation can confuse things even more.] */ | |
85f48f7e BM |
283 | |
284 | goto ssl2_read_again; /* This should really be | |
a0aae68c BM |
285 | * "return ssl2_read(s,buf,len)", |
286 | * but that would allow for | |
287 | * denial-of-service attacks if a | |
288 | * C compiler is used that does not | |
289 | * recognize end-recursion. */ | |
d02b48c6 RE |
290 | } |
291 | else | |
292 | { | |
5a4fbc69 | 293 | SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_BAD_STATE); |
58964a49 | 294 | return(-1); |
d02b48c6 RE |
295 | } |
296 | } | |
297 | ||
5a4fbc69 BM |
298 | int ssl2_read(SSL *s, void *buf, int len) |
299 | { | |
300 | return ssl2_read_internal(s, buf, len, 0); | |
301 | } | |
302 | ||
303 | int ssl2_peek(SSL *s, char *buf, int len) | |
304 | { | |
305 | return ssl2_read_internal(s, buf, len, 1); | |
306 | } | |
307 | ||
6b691a5c UM |
308 | static int read_n(SSL *s, unsigned int n, unsigned int max, |
309 | unsigned int extend) | |
d02b48c6 RE |
310 | { |
311 | int i,off,newb; | |
312 | ||
313 | /* if there is stuff still in the buffer from a previous read, | |
314 | * and there is more than we want, take some. */ | |
315 | if (s->s2->rbuf_left >= (int)n) | |
316 | { | |
317 | if (extend) | |
318 | s->packet_length+=n; | |
319 | else | |
320 | { | |
321 | s->packet= &(s->s2->rbuf[s->s2->rbuf_offs]); | |
322 | s->packet_length=n; | |
323 | } | |
324 | s->s2->rbuf_left-=n; | |
325 | s->s2->rbuf_offs+=n; | |
326 | return(n); | |
327 | } | |
328 | ||
329 | if (!s->read_ahead) max=n; | |
330 | if (max > (unsigned int)(SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2)) | |
331 | max=SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2; | |
332 | ||
333 | ||
334 | /* Else we want more than we have. | |
335 | * First, if there is some left or we want to extend */ | |
336 | off=0; | |
337 | if ((s->s2->rbuf_left != 0) || ((s->packet_length != 0) && extend)) | |
338 | { | |
339 | newb=s->s2->rbuf_left; | |
340 | if (extend) | |
341 | { | |
342 | off=s->packet_length; | |
343 | if (s->packet != s->s2->rbuf) | |
344 | memcpy(s->s2->rbuf,s->packet, | |
345 | (unsigned int)newb+off); | |
346 | } | |
347 | else if (s->s2->rbuf_offs != 0) | |
348 | { | |
349 | memcpy(s->s2->rbuf,&(s->s2->rbuf[s->s2->rbuf_offs]), | |
350 | (unsigned int)newb); | |
351 | s->s2->rbuf_offs=0; | |
352 | } | |
353 | s->s2->rbuf_left=0; | |
354 | } | |
355 | else | |
356 | newb=0; | |
357 | ||
358 | /* off is the offset to start writing too. | |
359 | * r->s2->rbuf_offs is the 'unread data', now 0. | |
360 | * newb is the number of new bytes so far | |
361 | */ | |
362 | s->packet=s->s2->rbuf; | |
363 | while (newb < (int)n) | |
364 | { | |
58964a49 | 365 | clear_sys_error(); |
d02b48c6 RE |
366 | if (s->rbio != NULL) |
367 | { | |
368 | s->rwstate=SSL_READING; | |
369 | i=BIO_read(s->rbio,(char *)&(s->s2->rbuf[off+newb]), | |
370 | max-newb); | |
371 | } | |
372 | else | |
373 | { | |
374 | SSLerr(SSL_F_READ_N,SSL_R_READ_BIO_NOT_SET); | |
375 | i= -1; | |
376 | } | |
377 | #ifdef PKT_DEBUG | |
378 | if (s->debug & 0x01) sleep(1); | |
379 | #endif | |
380 | if (i <= 0) | |
381 | { | |
382 | s->s2->rbuf_left+=newb; | |
383 | return(i); | |
384 | } | |
385 | newb+=i; | |
386 | } | |
387 | ||
388 | /* record unread data */ | |
389 | if (newb > (int)n) | |
390 | { | |
391 | s->s2->rbuf_offs=n+off; | |
392 | s->s2->rbuf_left=newb-n; | |
393 | } | |
394 | else | |
395 | { | |
396 | s->s2->rbuf_offs=0; | |
397 | s->s2->rbuf_left=0; | |
398 | } | |
399 | if (extend) | |
400 | s->packet_length+=n; | |
401 | else | |
402 | s->packet_length=n; | |
403 | s->rwstate=SSL_NOTHING; | |
404 | return(n); | |
405 | } | |
406 | ||
61f5b6f3 | 407 | int ssl2_write(SSL *s, const void *_buf, int len) |
d02b48c6 | 408 | { |
61f5b6f3 | 409 | const unsigned char *buf=_buf; |
d02b48c6 RE |
410 | unsigned int n,tot; |
411 | int i; | |
412 | ||
413 | if (SSL_in_init(s) && !s->in_handshake) | |
414 | { | |
415 | i=s->handshake_func(s); | |
416 | if (i < 0) return(i); | |
417 | if (i == 0) | |
418 | { | |
419 | SSLerr(SSL_F_SSL2_WRITE,SSL_R_SSL_HANDSHAKE_FAILURE); | |
420 | return(-1); | |
421 | } | |
422 | } | |
423 | ||
424 | if (s->error) | |
425 | { | |
426 | ssl2_write_error(s); | |
427 | if (s->error) | |
428 | return(-1); | |
429 | } | |
430 | ||
58964a49 | 431 | clear_sys_error(); |
d02b48c6 RE |
432 | s->rwstate=SSL_NOTHING; |
433 | if (len <= 0) return(len); | |
434 | ||
435 | tot=s->s2->wnum; | |
436 | s->s2->wnum=0; | |
437 | ||
438 | n=(len-tot); | |
439 | for (;;) | |
440 | { | |
441 | i=do_ssl_write(s,&(buf[tot]),n); | |
442 | if (i <= 0) | |
443 | { | |
444 | s->s2->wnum=tot; | |
445 | return(i); | |
446 | } | |
e1056435 | 447 | if ((i == (int)n) || |
11b1adad | 448 | (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE)) |
e1056435 BM |
449 | { |
450 | return(tot+i); | |
451 | } | |
452 | ||
d02b48c6 RE |
453 | n-=i; |
454 | tot+=i; | |
455 | } | |
456 | } | |
457 | ||
61f5b6f3 | 458 | static int write_pending(SSL *s, const unsigned char *buf, unsigned int len) |
d02b48c6 RE |
459 | { |
460 | int i; | |
461 | ||
462 | /* s->s2->wpend_len != 0 MUST be true. */ | |
463 | ||
464 | /* check that they have given us the same buffer to | |
465 | * write */ | |
e1056435 BM |
466 | if ((s->s2->wpend_tot > (int)len) || |
467 | ((s->s2->wpend_buf != buf) && | |
1afd8b39 | 468 | !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))) |
d02b48c6 RE |
469 | { |
470 | SSLerr(SSL_F_WRITE_PENDING,SSL_R_BAD_WRITE_RETRY); | |
58964a49 | 471 | return(-1); |
d02b48c6 RE |
472 | } |
473 | ||
474 | for (;;) | |
475 | { | |
58964a49 | 476 | clear_sys_error(); |
d02b48c6 RE |
477 | if (s->wbio != NULL) |
478 | { | |
479 | s->rwstate=SSL_WRITING; | |
480 | i=BIO_write(s->wbio, | |
481 | (char *)&(s->s2->write_ptr[s->s2->wpend_off]), | |
482 | (unsigned int)s->s2->wpend_len); | |
483 | } | |
484 | else | |
485 | { | |
486 | SSLerr(SSL_F_WRITE_PENDING,SSL_R_WRITE_BIO_NOT_SET); | |
487 | i= -1; | |
488 | } | |
489 | #ifdef PKT_DEBUG | |
490 | if (s->debug & 0x01) sleep(1); | |
491 | #endif | |
492 | if (i == s->s2->wpend_len) | |
493 | { | |
494 | s->s2->wpend_len=0; | |
495 | s->rwstate=SSL_NOTHING; | |
496 | return(s->s2->wpend_ret); | |
497 | } | |
498 | else if (i <= 0) | |
499 | return(i); | |
500 | s->s2->wpend_off+=i; | |
501 | s->s2->wpend_len-=i; | |
502 | } | |
503 | } | |
504 | ||
61f5b6f3 | 505 | static int do_ssl_write(SSL *s, const unsigned char *buf, unsigned int len) |
d02b48c6 RE |
506 | { |
507 | unsigned int j,k,olen,p,mac_size,bs; | |
508 | register unsigned char *pp; | |
509 | ||
510 | olen=len; | |
511 | ||
512 | /* first check if there is data from an encryption waiting to | |
513 | * be sent - it must be sent because the other end is waiting. | |
514 | * This will happen with non-blocking IO. We print it and then | |
515 | * return. | |
516 | */ | |
517 | if (s->s2->wpend_len != 0) return(write_pending(s,buf,len)); | |
518 | ||
519 | /* set mac_size to mac size */ | |
520 | if (s->s2->clear_text) | |
521 | mac_size=0; | |
522 | else | |
523 | mac_size=EVP_MD_size(s->write_hash); | |
524 | ||
525 | /* lets set the pad p */ | |
526 | if (s->s2->clear_text) | |
527 | { | |
528 | if (len > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER) | |
529 | len=SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER; | |
530 | p=0; | |
531 | s->s2->three_byte_header=0; | |
532 | /* len=len; */ | |
533 | } | |
534 | else | |
535 | { | |
536 | bs=EVP_CIPHER_CTX_block_size(s->enc_read_ctx); | |
537 | j=len+mac_size; | |
3880cd35 BM |
538 | /* Two-byte headers allow for a larger record length than |
539 | * three-byte headers, but we can't use them if we need | |
540 | * padding or if we have to set the escape bit. */ | |
d02b48c6 RE |
541 | if ((j > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) && |
542 | (!s->s2->escape)) | |
543 | { | |
544 | if (j > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER) | |
545 | j=SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER; | |
546 | /* set k to the max number of bytes with 2 | |
547 | * byte header */ | |
548 | k=j-(j%bs); | |
549 | /* how many data bytes? */ | |
550 | len=k-mac_size; | |
551 | s->s2->three_byte_header=0; | |
552 | p=0; | |
553 | } | |
554 | else if ((bs <= 1) && (!s->s2->escape)) | |
555 | { | |
3880cd35 BM |
556 | /* j <= SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER, thus |
557 | * j < SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER */ | |
d02b48c6 RE |
558 | s->s2->three_byte_header=0; |
559 | p=0; | |
560 | } | |
3880cd35 | 561 | else /* we may have to use a 3 byte header */ |
d02b48c6 | 562 | { |
3880cd35 BM |
563 | /* If s->s2->escape is not set, then |
564 | * j <= SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER, and thus | |
565 | * j < SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER. */ | |
d02b48c6 RE |
566 | p=(j%bs); |
567 | p=(p == 0)?0:(bs-p); | |
568 | if (s->s2->escape) | |
3880cd35 | 569 | { |
d02b48c6 | 570 | s->s2->three_byte_header=1; |
3880cd35 BM |
571 | if (j > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) |
572 | j=SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER; | |
573 | } | |
d02b48c6 RE |
574 | else |
575 | s->s2->three_byte_header=(p == 0)?0:1; | |
576 | } | |
577 | } | |
3880cd35 BM |
578 | |
579 | /* Now | |
580 | * j <= SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER | |
581 | * holds, and if s->s2->three_byte_header is set, then even | |
582 | * j <= SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER. | |
583 | */ | |
584 | ||
d02b48c6 RE |
585 | /* mac_size is the number of MAC bytes |
586 | * len is the number of data bytes we are going to send | |
587 | * p is the number of padding bytes | |
3880cd35 | 588 | * (if it is a two-byte header, then p == 0) */ |
d02b48c6 RE |
589 | |
590 | s->s2->wlength=len; | |
591 | s->s2->padding=p; | |
592 | s->s2->mac_data= &(s->s2->wbuf[3]); | |
593 | s->s2->wact_data= &(s->s2->wbuf[3+mac_size]); | |
594 | /* we copy the data into s->s2->wbuf */ | |
595 | memcpy(s->s2->wact_data,buf,len); | |
596 | #ifdef PURIFY | |
597 | if (p) | |
598 | memset(&(s->s2->wact_data[len]),0,p); | |
599 | #endif | |
600 | ||
601 | if (!s->s2->clear_text) | |
602 | { | |
603 | s->s2->wact_data_length=len+p; | |
604 | ssl2_mac(s,s->s2->mac_data,1); | |
605 | s->s2->wlength+=p+mac_size; | |
606 | ssl2_enc(s,1); | |
607 | } | |
608 | ||
609 | /* package up the header */ | |
610 | s->s2->wpend_len=s->s2->wlength; | |
611 | if (s->s2->three_byte_header) /* 3 byte header */ | |
612 | { | |
613 | pp=s->s2->mac_data; | |
614 | pp-=3; | |
615 | pp[0]=(s->s2->wlength>>8)&(THREE_BYTE_MASK>>8); | |
616 | if (s->s2->escape) pp[0]|=SEC_ESC_BIT; | |
617 | pp[1]=s->s2->wlength&0xff; | |
618 | pp[2]=s->s2->padding; | |
619 | s->s2->wpend_len+=3; | |
620 | } | |
621 | else | |
622 | { | |
623 | pp=s->s2->mac_data; | |
624 | pp-=2; | |
625 | pp[0]=((s->s2->wlength>>8)&(TWO_BYTE_MASK>>8))|TWO_BYTE_BIT; | |
626 | pp[1]=s->s2->wlength&0xff; | |
627 | s->s2->wpend_len+=2; | |
628 | } | |
629 | s->s2->write_ptr=pp; | |
630 | ||
631 | INC32(s->s2->write_sequence); /* expect next number */ | |
632 | ||
633 | /* lets try to actually write the data */ | |
634 | s->s2->wpend_tot=olen; | |
e778802f | 635 | s->s2->wpend_buf=buf; |
d02b48c6 RE |
636 | |
637 | s->s2->wpend_ret=len; | |
638 | ||
639 | s->s2->wpend_off=0; | |
640 | return(write_pending(s,buf,olen)); | |
641 | } | |
642 | ||
6b691a5c | 643 | int ssl2_part_read(SSL *s, unsigned long f, int i) |
d02b48c6 RE |
644 | { |
645 | unsigned char *p; | |
646 | int j; | |
647 | ||
648 | /* check for error */ | |
649 | if ((s->init_num == 0) && (i >= 3)) | |
650 | { | |
651 | p=(unsigned char *)s->init_buf->data; | |
652 | if (p[0] == SSL2_MT_ERROR) | |
653 | { | |
654 | j=(p[1]<<8)|p[2]; | |
655 | SSLerr((int)f,ssl_mt_error(j)); | |
656 | } | |
657 | } | |
658 | ||
659 | if (i < 0) | |
660 | { | |
661 | /* ssl2_return_error(s); */ | |
662 | /* for non-blocking io, | |
663 | * this is not fatal */ | |
664 | return(i); | |
665 | } | |
666 | else | |
667 | { | |
668 | s->init_num+=i; | |
669 | return(0); | |
670 | } | |
671 | } | |
672 | ||
6b691a5c | 673 | int ssl2_do_write(SSL *s) |
d02b48c6 RE |
674 | { |
675 | int ret; | |
676 | ||
61f5b6f3 | 677 | ret=ssl2_write(s,&s->init_buf->data[s->init_off],s->init_num); |
d02b48c6 RE |
678 | if (ret == s->init_num) |
679 | return(1); | |
680 | if (ret < 0) | |
681 | return(-1); | |
682 | s->init_off+=ret; | |
683 | s->init_num-=ret; | |
684 | return(0); | |
685 | } | |
686 | ||
6b691a5c | 687 | static int ssl_mt_error(int n) |
d02b48c6 RE |
688 | { |
689 | int ret; | |
690 | ||
691 | switch (n) | |
692 | { | |
693 | case SSL2_PE_NO_CIPHER: | |
694 | ret=SSL_R_PEER_ERROR_NO_CIPHER; | |
695 | break; | |
696 | case SSL2_PE_NO_CERTIFICATE: | |
697 | ret=SSL_R_PEER_ERROR_NO_CERTIFICATE; | |
698 | break; | |
699 | case SSL2_PE_BAD_CERTIFICATE: | |
700 | ret=SSL_R_PEER_ERROR_CERTIFICATE; | |
701 | break; | |
702 | case SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE: | |
703 | ret=SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE; | |
704 | break; | |
705 | default: | |
706 | ret=SSL_R_UNKNOWN_REMOTE_ERROR_TYPE; | |
707 | break; | |
708 | } | |
709 | return(ret); | |
710 | } | |
bc36ee62 | 711 | #else /* !OPENSSL_NO_SSL2 */ |
aa82db4f UM |
712 | |
713 | # if PEDANTIC | |
714 | static void *dummy=&dummy; | |
715 | # endif | |
716 | ||
717 | #endif |