]>
Commit | Line | Data |
---|---|---|
d02b48c6 | 1 | /* ssl/s2_clnt.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 | */ | |
58 | ||
aa82db4f UM |
59 | #include "ssl_locl.h" |
60 | #ifndef NO_SSL2 | |
d02b48c6 | 61 | #include <stdio.h> |
ec577822 BM |
62 | #include <openssl/rand.h> |
63 | #include <openssl/buffer.h> | |
64 | #include <openssl/objects.h> | |
ec577822 | 65 | #include <openssl/evp.h> |
d02b48c6 | 66 | |
9b3086fe | 67 | static SSL_METHOD *ssl2_get_client_method(int ver); |
d02b48c6 RE |
68 | static int get_server_finished(SSL *s); |
69 | static int get_server_verify(SSL *s); | |
70 | static int get_server_hello(SSL *s); | |
71 | static int client_hello(SSL *s); | |
72 | static int client_master_key(SSL *s); | |
73 | static int client_finished(SSL *s); | |
74 | static int client_certificate(SSL *s); | |
b56bce4f | 75 | static int ssl_rsa_public_encrypt(SESS_CERT *sc, int len, unsigned char *from, |
d02b48c6 | 76 | unsigned char *to,int padding); |
d02b48c6 RE |
77 | #define BREAK break |
78 | ||
6b691a5c | 79 | static SSL_METHOD *ssl2_get_client_method(int ver) |
d02b48c6 | 80 | { |
58964a49 | 81 | if (ver == SSL2_VERSION) |
d02b48c6 RE |
82 | return(SSLv2_client_method()); |
83 | else | |
84 | return(NULL); | |
85 | } | |
86 | ||
6b691a5c | 87 | SSL_METHOD *SSLv2_client_method(void) |
d02b48c6 RE |
88 | { |
89 | static int init=1; | |
90 | static SSL_METHOD SSLv2_client_data; | |
91 | ||
92 | if (init) | |
93 | { | |
d02b48c6 RE |
94 | memcpy((char *)&SSLv2_client_data,(char *)sslv2_base_method(), |
95 | sizeof(SSL_METHOD)); | |
96 | SSLv2_client_data.ssl_connect=ssl2_connect; | |
97 | SSLv2_client_data.get_ssl_method=ssl2_get_client_method; | |
5cc146f3 | 98 | init=0; |
d02b48c6 RE |
99 | } |
100 | return(&SSLv2_client_data); | |
101 | } | |
102 | ||
6b691a5c | 103 | int ssl2_connect(SSL *s) |
d02b48c6 RE |
104 | { |
105 | unsigned long l=time(NULL); | |
106 | BUF_MEM *buf=NULL; | |
107 | int ret= -1; | |
108 | void (*cb)()=NULL; | |
109 | int new_state,state; | |
110 | ||
eb952088 | 111 | RAND_add(&l,sizeof(l),0); |
d02b48c6 | 112 | ERR_clear_error(); |
58964a49 | 113 | clear_sys_error(); |
d02b48c6 RE |
114 | |
115 | if (s->info_callback != NULL) | |
116 | cb=s->info_callback; | |
117 | else if (s->ctx->info_callback != NULL) | |
118 | cb=s->ctx->info_callback; | |
119 | ||
120 | /* init things to blank */ | |
121 | if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); | |
122 | s->in_handshake++; | |
123 | ||
124 | for (;;) | |
125 | { | |
126 | state=s->state; | |
127 | ||
128 | switch (s->state) | |
129 | { | |
130 | case SSL_ST_BEFORE: | |
131 | case SSL_ST_CONNECT: | |
132 | case SSL_ST_BEFORE|SSL_ST_CONNECT: | |
133 | case SSL_ST_OK|SSL_ST_CONNECT: | |
134 | ||
413c4f45 | 135 | s->server=0; |
d02b48c6 RE |
136 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); |
137 | ||
58964a49 | 138 | s->version=SSL2_VERSION; |
d02b48c6 RE |
139 | s->type=SSL_ST_CONNECT; |
140 | ||
141 | buf=s->init_buf; | |
142 | if ((buf == NULL) && ((buf=BUF_MEM_new()) == NULL)) | |
143 | { | |
144 | ret= -1; | |
145 | goto end; | |
146 | } | |
147 | if (!BUF_MEM_grow(buf, | |
148 | SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)) | |
149 | { | |
150 | ret= -1; | |
151 | goto end; | |
152 | } | |
153 | s->init_buf=buf; | |
154 | s->init_num=0; | |
155 | s->state=SSL2_ST_SEND_CLIENT_HELLO_A; | |
413c4f45 | 156 | s->ctx->stats.sess_connect++; |
d02b48c6 RE |
157 | s->handshake_func=ssl2_connect; |
158 | BREAK; | |
159 | ||
160 | case SSL2_ST_SEND_CLIENT_HELLO_A: | |
161 | case SSL2_ST_SEND_CLIENT_HELLO_B: | |
162 | s->shutdown=0; | |
163 | ret=client_hello(s); | |
164 | if (ret <= 0) goto end; | |
165 | s->init_num=0; | |
166 | s->state=SSL2_ST_GET_SERVER_HELLO_A; | |
167 | BREAK; | |
168 | ||
169 | case SSL2_ST_GET_SERVER_HELLO_A: | |
170 | case SSL2_ST_GET_SERVER_HELLO_B: | |
171 | ret=get_server_hello(s); | |
172 | if (ret <= 0) goto end; | |
173 | s->init_num=0; | |
174 | if (!s->hit) /* new session */ | |
175 | { | |
176 | s->state=SSL2_ST_SEND_CLIENT_MASTER_KEY_A; | |
177 | BREAK; | |
178 | } | |
179 | else | |
180 | { | |
181 | s->state=SSL2_ST_CLIENT_START_ENCRYPTION; | |
182 | break; | |
183 | } | |
184 | ||
185 | case SSL2_ST_SEND_CLIENT_MASTER_KEY_A: | |
186 | case SSL2_ST_SEND_CLIENT_MASTER_KEY_B: | |
187 | ret=client_master_key(s); | |
188 | if (ret <= 0) goto end; | |
189 | s->init_num=0; | |
190 | s->state=SSL2_ST_CLIENT_START_ENCRYPTION; | |
191 | break; | |
192 | ||
193 | case SSL2_ST_CLIENT_START_ENCRYPTION: | |
194 | /* Ok, we now have all the stuff needed to | |
195 | * start encrypting, so lets fire it up :-) */ | |
196 | if (!ssl2_enc_init(s,1)) | |
197 | { | |
198 | ret= -1; | |
199 | goto end; | |
200 | } | |
201 | s->s2->clear_text=0; | |
202 | s->state=SSL2_ST_SEND_CLIENT_FINISHED_A; | |
203 | break; | |
204 | ||
205 | case SSL2_ST_SEND_CLIENT_FINISHED_A: | |
206 | case SSL2_ST_SEND_CLIENT_FINISHED_B: | |
207 | ret=client_finished(s); | |
208 | if (ret <= 0) goto end; | |
209 | s->init_num=0; | |
210 | s->state=SSL2_ST_GET_SERVER_VERIFY_A; | |
211 | break; | |
212 | ||
213 | case SSL2_ST_GET_SERVER_VERIFY_A: | |
214 | case SSL2_ST_GET_SERVER_VERIFY_B: | |
215 | ret=get_server_verify(s); | |
216 | if (ret <= 0) goto end; | |
217 | s->init_num=0; | |
218 | s->state=SSL2_ST_GET_SERVER_FINISHED_A; | |
219 | break; | |
220 | ||
221 | case SSL2_ST_GET_SERVER_FINISHED_A: | |
222 | case SSL2_ST_GET_SERVER_FINISHED_B: | |
223 | ret=get_server_finished(s); | |
224 | if (ret <= 0) goto end; | |
225 | break; | |
226 | ||
227 | case SSL2_ST_SEND_CLIENT_CERTIFICATE_A: | |
228 | case SSL2_ST_SEND_CLIENT_CERTIFICATE_B: | |
229 | case SSL2_ST_SEND_CLIENT_CERTIFICATE_C: | |
230 | case SSL2_ST_SEND_CLIENT_CERTIFICATE_D: | |
231 | case SSL2_ST_X509_GET_CLIENT_CERTIFICATE: | |
232 | ret=client_certificate(s); | |
233 | if (ret <= 0) goto end; | |
234 | s->init_num=0; | |
235 | s->state=SSL2_ST_GET_SERVER_FINISHED_A; | |
236 | break; | |
237 | ||
238 | case SSL_ST_OK: | |
413c4f45 MC |
239 | if (s->init_buf != NULL) |
240 | { | |
241 | BUF_MEM_free(s->init_buf); | |
242 | s->init_buf=NULL; | |
243 | } | |
d02b48c6 RE |
244 | s->init_num=0; |
245 | /* ERR_clear_error();*/ | |
246 | ||
247 | /* If we want to cache session-ids in the client | |
657e60fa | 248 | * and we successfully add the session-id to the |
d02b48c6 RE |
249 | * cache, and there is a callback, then pass it out. |
250 | * 26/11/96 - eay - only add if not a re-used session. | |
251 | */ | |
252 | ||
253 | ssl_update_cache(s,SSL_SESS_CACHE_CLIENT); | |
413c4f45 | 254 | if (s->hit) s->ctx->stats.sess_hit++; |
d02b48c6 RE |
255 | |
256 | ret=1; | |
257 | /* s->server=0; */ | |
413c4f45 | 258 | s->ctx->stats.sess_connect_good++; |
d02b48c6 RE |
259 | |
260 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); | |
261 | ||
262 | goto end; | |
dfeab068 | 263 | /* break; */ |
d02b48c6 RE |
264 | default: |
265 | SSLerr(SSL_F_SSL2_CONNECT,SSL_R_UNKNOWN_STATE); | |
266 | return(-1); | |
267 | /* break; */ | |
268 | } | |
269 | ||
270 | if ((cb != NULL) && (s->state != state)) | |
271 | { | |
272 | new_state=s->state; | |
273 | s->state=state; | |
274 | cb(s,SSL_CB_CONNECT_LOOP,1); | |
275 | s->state=new_state; | |
276 | } | |
277 | } | |
278 | end: | |
279 | s->in_handshake--; | |
280 | if (cb != NULL) | |
281 | cb(s,SSL_CB_CONNECT_EXIT,ret); | |
282 | return(ret); | |
283 | } | |
284 | ||
6b691a5c | 285 | static int get_server_hello(SSL *s) |
d02b48c6 RE |
286 | { |
287 | unsigned char *buf; | |
288 | unsigned char *p; | |
289 | int i,j; | |
f73e07cf | 290 | STACK_OF(SSL_CIPHER) *sk=NULL,*cl; |
d02b48c6 RE |
291 | |
292 | buf=(unsigned char *)s->init_buf->data; | |
293 | p=buf; | |
294 | if (s->state == SSL2_ST_GET_SERVER_HELLO_A) | |
295 | { | |
296 | i=ssl2_read(s,(char *)&(buf[s->init_num]),11-s->init_num); | |
297 | if (i < (11-s->init_num)) | |
298 | return(ssl2_part_read(s,SSL_F_GET_SERVER_HELLO,i)); | |
299 | ||
300 | if (*(p++) != SSL2_MT_SERVER_HELLO) | |
301 | { | |
302 | if (p[-1] != SSL2_MT_ERROR) | |
303 | { | |
304 | ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); | |
305 | SSLerr(SSL_F_GET_SERVER_HELLO, | |
306 | SSL_R_READ_WRONG_PACKET_TYPE); | |
307 | } | |
308 | else | |
309 | SSLerr(SSL_F_GET_SERVER_HELLO, | |
310 | SSL_R_PEER_ERROR); | |
311 | return(-1); | |
312 | } | |
37b0d5d0 AP |
313 | #ifdef __APPLE_CC__ |
314 | /* The Rhapsody 5.5 (a.k.a. MacOS X) compiler bug | |
315 | * workaround. <appro@fy.chalmers.se> */ | |
316 | s->hit=(i=*(p++))?1:0; | |
317 | #else | |
d02b48c6 | 318 | s->hit=(*(p++))?1:0; |
37b0d5d0 | 319 | #endif |
d02b48c6 RE |
320 | s->s2->tmp.cert_type= *(p++); |
321 | n2s(p,i); | |
322 | if (i < s->version) s->version=i; | |
323 | n2s(p,i); s->s2->tmp.cert_length=i; | |
324 | n2s(p,i); s->s2->tmp.csl=i; | |
325 | n2s(p,i); s->s2->tmp.conn_id_length=i; | |
326 | s->state=SSL2_ST_GET_SERVER_HELLO_B; | |
327 | s->init_num=0; | |
328 | } | |
329 | ||
330 | /* SSL2_ST_GET_SERVER_HELLO_B */ | |
331 | j=s->s2->tmp.cert_length+s->s2->tmp.csl+s->s2->tmp.conn_id_length | |
332 | - s->init_num; | |
333 | i=ssl2_read(s,(char *)&(buf[s->init_num]),j); | |
334 | if (i != j) return(ssl2_part_read(s,SSL_F_GET_SERVER_HELLO,i)); | |
335 | ||
336 | /* things are looking good */ | |
337 | ||
338 | p=buf; | |
339 | if (s->hit) | |
340 | { | |
341 | if (s->s2->tmp.cert_length != 0) | |
342 | { | |
343 | SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CERT_LENGTH_NOT_ZERO); | |
344 | return(-1); | |
345 | } | |
346 | if (s->s2->tmp.cert_type != 0) | |
347 | { | |
58964a49 | 348 | if (!(s->options & |
d02b48c6 RE |
349 | SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG)) |
350 | { | |
351 | SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CERT_TYPE_NOT_ZERO); | |
352 | return(-1); | |
353 | } | |
354 | } | |
355 | if (s->s2->tmp.csl != 0) | |
356 | { | |
357 | SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CIPHER_LIST_NOT_ZERO); | |
358 | return(-1); | |
359 | } | |
360 | } | |
361 | else | |
362 | { | |
363 | #ifdef undef | |
364 | /* very bad */ | |
365 | memset(s->session->session_id,0, | |
366 | SSL_MAX_SSL_SESSION_ID_LENGTH_IN_BYTES); | |
367 | s->session->session_id_length=0; | |
368 | */ | |
369 | #endif | |
370 | ||
657e60fa | 371 | /* we need to do this in case we were trying to reuse a |
d02b48c6 RE |
372 | * client session but others are already reusing it. |
373 | * If this was a new 'blank' session ID, the session-id | |
374 | * length will still be 0 */ | |
375 | if (s->session->session_id_length > 0) | |
376 | { | |
377 | if (!ssl_get_new_session(s,0)) | |
378 | { | |
379 | ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); | |
380 | return(-1); | |
381 | } | |
382 | } | |
383 | ||
384 | if (ssl2_set_certificate(s,s->s2->tmp.cert_type, | |
385 | s->s2->tmp.cert_length,p) <= 0) | |
386 | { | |
387 | ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE); | |
388 | return(-1); | |
389 | } | |
390 | p+=s->s2->tmp.cert_length; | |
391 | ||
392 | if (s->s2->tmp.csl == 0) | |
393 | { | |
394 | ssl2_return_error(s,SSL2_PE_NO_CIPHER); | |
395 | SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_NO_CIPHER_LIST); | |
396 | return(-1); | |
397 | } | |
398 | ||
399 | /* We have just received a list of ciphers back from the | |
400 | * server. We need to get the ones that match, then select | |
401 | * the one we want the most :-). */ | |
402 | ||
403 | /* load the ciphers */ | |
404 | sk=ssl_bytes_to_cipher_list(s,p,s->s2->tmp.csl, | |
f73e07cf | 405 | &s->session->ciphers); |
d02b48c6 RE |
406 | p+=s->s2->tmp.csl; |
407 | if (sk == NULL) | |
408 | { | |
409 | ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); | |
410 | SSLerr(SSL_F_GET_SERVER_HELLO,ERR_R_MALLOC_FAILURE); | |
411 | return(-1); | |
412 | } | |
413 | ||
f73e07cf | 414 | sk_SSL_CIPHER_set_cmp_func(sk,ssl_cipher_ptr_id_cmp); |
d02b48c6 RE |
415 | |
416 | /* get the array of ciphers we will accept */ | |
417 | cl=ssl_get_ciphers_by_id(s); | |
418 | ||
419 | /* In theory we could have ciphers sent back that we | |
420 | * don't want to use but that does not matter since we | |
657e60fa | 421 | * will check against the list we originally sent and |
d02b48c6 RE |
422 | * for performance reasons we should not bother to match |
423 | * the two lists up just to check. */ | |
f73e07cf | 424 | for (i=0; i<sk_SSL_CIPHER_num(cl); i++) |
d02b48c6 | 425 | { |
f73e07cf BL |
426 | if (sk_SSL_CIPHER_find(sk, |
427 | sk_SSL_CIPHER_value(cl,i)) >= 0) | |
d02b48c6 RE |
428 | break; |
429 | } | |
430 | ||
f73e07cf | 431 | if (i >= sk_SSL_CIPHER_num(cl)) |
d02b48c6 RE |
432 | { |
433 | ssl2_return_error(s,SSL2_PE_NO_CIPHER); | |
434 | SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_NO_CIPHER_MATCH); | |
435 | return(-1); | |
436 | } | |
f73e07cf | 437 | s->session->cipher=sk_SSL_CIPHER_value(cl,i); |
d02b48c6 | 438 | |
d02b48c6 | 439 | |
2557eaea BM |
440 | if (s->session->peer != NULL) /* can't happen*/ |
441 | { | |
442 | ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); | |
443 | SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_INTERNAL_ERROR); | |
444 | return(-1); | |
445 | } | |
446 | ||
447 | s->session->peer = s->session->sess_cert->peer_key->x509; | |
448 | /* peer_key->x509 has been set by ssl2_set_certificate. */ | |
449 | CRYPTO_add(&s->session->peer->references, 1, CRYPTO_LOCK_X509); | |
450 | } | |
451 | ||
452 | if (s->session->peer != s->session->sess_cert->peer_key->x509) | |
453 | /* can't happen */ | |
454 | { | |
455 | ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); | |
456 | SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_INTERNAL_ERROR); | |
457 | return(-1); | |
458 | } | |
459 | ||
d02b48c6 RE |
460 | s->s2->conn_id_length=s->s2->tmp.conn_id_length; |
461 | memcpy(s->s2->conn_id,p,s->s2->tmp.conn_id_length); | |
462 | return(1); | |
463 | } | |
464 | ||
6b691a5c | 465 | static int client_hello(SSL *s) |
d02b48c6 RE |
466 | { |
467 | unsigned char *buf; | |
468 | unsigned char *p,*d; | |
469 | /* CIPHER **cipher;*/ | |
470 | int i,n,j; | |
471 | ||
472 | buf=(unsigned char *)s->init_buf->data; | |
473 | if (s->state == SSL2_ST_SEND_CLIENT_HELLO_A) | |
474 | { | |
475 | if ((s->session == NULL) || | |
476 | (s->session->ssl_version != s->version)) | |
477 | { | |
478 | if (!ssl_get_new_session(s,0)) | |
479 | { | |
480 | ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); | |
481 | return(-1); | |
482 | } | |
483 | } | |
484 | /* else use the pre-loaded session */ | |
485 | ||
486 | p=buf; /* header */ | |
487 | d=p+9; /* data section */ | |
488 | *(p++)=SSL2_MT_CLIENT_HELLO; /* type */ | |
6d02d8e4 | 489 | s2n(SSL2_VERSION,p); /* version */ |
d02b48c6 RE |
490 | n=j=0; |
491 | ||
492 | n=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),d); | |
493 | d+=n; | |
494 | ||
495 | if (n == 0) | |
496 | { | |
497 | SSLerr(SSL_F_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE); | |
498 | return(-1); | |
499 | } | |
500 | ||
501 | s2n(n,p); /* cipher spec num bytes */ | |
502 | ||
503 | if ((s->session->session_id_length > 0) && | |
504 | (s->session->session_id_length <= | |
505 | SSL2_MAX_SSL_SESSION_ID_LENGTH)) | |
506 | { | |
507 | i=s->session->session_id_length; | |
508 | s2n(i,p); /* session id length */ | |
509 | memcpy(d,s->session->session_id,(unsigned int)i); | |
510 | d+=i; | |
511 | } | |
512 | else | |
513 | { | |
514 | s2n(0,p); | |
515 | } | |
516 | ||
517 | s->s2->challenge_length=SSL2_CHALLENGE_LENGTH; | |
518 | s2n(SSL2_CHALLENGE_LENGTH,p); /* challenge length */ | |
519 | /*challenge id data*/ | |
e7f97e2d | 520 | RAND_pseudo_bytes(s->s2->challenge,SSL2_CHALLENGE_LENGTH); |
d02b48c6 RE |
521 | memcpy(d,s->s2->challenge,SSL2_CHALLENGE_LENGTH); |
522 | d+=SSL2_CHALLENGE_LENGTH; | |
523 | ||
524 | s->state=SSL2_ST_SEND_CLIENT_HELLO_B; | |
525 | s->init_num=d-buf; | |
526 | s->init_off=0; | |
527 | } | |
528 | /* SSL2_ST_SEND_CLIENT_HELLO_B */ | |
529 | return(ssl2_do_write(s)); | |
530 | } | |
531 | ||
6b691a5c | 532 | static int client_master_key(SSL *s) |
d02b48c6 RE |
533 | { |
534 | unsigned char *buf; | |
535 | unsigned char *p,*d; | |
536 | int clear,enc,karg,i; | |
537 | SSL_SESSION *sess; | |
e778802f BL |
538 | const EVP_CIPHER *c; |
539 | const EVP_MD *md; | |
d02b48c6 RE |
540 | |
541 | buf=(unsigned char *)s->init_buf->data; | |
542 | if (s->state == SSL2_ST_SEND_CLIENT_MASTER_KEY_A) | |
543 | { | |
544 | ||
413c4f45 | 545 | if (!ssl_cipher_get_evp(s->session,&c,&md,NULL)) |
d02b48c6 RE |
546 | { |
547 | ssl2_return_error(s,SSL2_PE_NO_CIPHER); | |
548 | SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS); | |
549 | return(-1); | |
550 | } | |
551 | sess=s->session; | |
552 | p=buf; | |
553 | d=p+10; | |
554 | *(p++)=SSL2_MT_CLIENT_MASTER_KEY;/* type */ | |
555 | ||
556 | i=ssl_put_cipher_by_char(s,sess->cipher,p); | |
557 | p+=i; | |
558 | ||
559 | /* make key_arg data */ | |
560 | i=EVP_CIPHER_iv_length(c); | |
561 | sess->key_arg_length=i; | |
e7f97e2d | 562 | if (i > 0) RAND_pseudo_bytes(sess->key_arg,i); |
d02b48c6 RE |
563 | |
564 | /* make a master key */ | |
565 | i=EVP_CIPHER_key_length(c); | |
566 | sess->master_key_length=i; | |
e7f97e2d UM |
567 | if (i > 0) |
568 | { | |
569 | if (RAND_bytes(sess->master_key,i) <= 0) | |
570 | { | |
571 | ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); | |
af6f3881 | 572 | return(-1); |
e7f97e2d UM |
573 | } |
574 | } | |
d02b48c6 RE |
575 | |
576 | if (sess->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC) | |
577 | enc=8; | |
06ab81f9 | 578 | else if (SSL_C_IS_EXPORT(sess->cipher)) |
d02b48c6 RE |
579 | enc=5; |
580 | else | |
581 | enc=i; | |
582 | ||
583 | if (i < enc) | |
584 | { | |
585 | ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); | |
586 | SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_CIPHER_TABLE_SRC_ERROR); | |
587 | return(-1); | |
588 | } | |
589 | clear=i-enc; | |
590 | s2n(clear,p); | |
591 | memcpy(d,sess->master_key,(unsigned int)clear); | |
592 | d+=clear; | |
593 | ||
9d5cceac | 594 | enc=ssl_rsa_public_encrypt(sess->sess_cert,enc, |
d02b48c6 RE |
595 | &(sess->master_key[clear]),d, |
596 | (s->s2->ssl2_rollback)?RSA_SSLV23_PADDING:RSA_PKCS1_PADDING); | |
597 | if (enc <= 0) | |
598 | { | |
599 | ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); | |
600 | SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_PUBLIC_KEY_ENCRYPT_ERROR); | |
601 | return(-1); | |
602 | } | |
dfeab068 RE |
603 | #ifdef PKCS1_CHECK |
604 | if (s->options & SSL_OP_PKCS1_CHECK_1) d[1]++; | |
605 | if (s->options & SSL_OP_PKCS1_CHECK_2) | |
606 | sess->master_key[clear]++; | |
607 | #endif | |
d02b48c6 RE |
608 | s2n(enc,p); |
609 | d+=enc; | |
610 | karg=sess->key_arg_length; | |
611 | s2n(karg,p); /* key arg size */ | |
612 | memcpy(d,sess->key_arg,(unsigned int)karg); | |
613 | d+=karg; | |
614 | ||
615 | s->state=SSL2_ST_SEND_CLIENT_MASTER_KEY_B; | |
616 | s->init_num=d-buf; | |
617 | s->init_off=0; | |
618 | } | |
619 | ||
620 | /* SSL2_ST_SEND_CLIENT_MASTER_KEY_B */ | |
621 | return(ssl2_do_write(s)); | |
622 | } | |
623 | ||
6b691a5c | 624 | static int client_finished(SSL *s) |
d02b48c6 RE |
625 | { |
626 | unsigned char *p; | |
627 | ||
628 | if (s->state == SSL2_ST_SEND_CLIENT_FINISHED_A) | |
629 | { | |
630 | p=(unsigned char *)s->init_buf->data; | |
631 | *(p++)=SSL2_MT_CLIENT_FINISHED; | |
632 | memcpy(p,s->s2->conn_id,(unsigned int)s->s2->conn_id_length); | |
633 | ||
634 | s->state=SSL2_ST_SEND_CLIENT_FINISHED_B; | |
635 | s->init_num=s->s2->conn_id_length+1; | |
636 | s->init_off=0; | |
637 | } | |
638 | return(ssl2_do_write(s)); | |
639 | } | |
640 | ||
641 | /* read the data and then respond */ | |
6b691a5c | 642 | static int client_certificate(SSL *s) |
d02b48c6 RE |
643 | { |
644 | unsigned char *buf; | |
645 | unsigned char *p,*d; | |
646 | int i; | |
647 | unsigned int n; | |
648 | int cert_ch_len=0; | |
649 | unsigned char *cert_ch; | |
650 | ||
651 | buf=(unsigned char *)s->init_buf->data; | |
652 | cert_ch= &(buf[2]); | |
653 | ||
654 | /* We have a cert associated with the SSL, so attach it to | |
655 | * the session if it does not have one */ | |
656 | ||
657 | if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_A) | |
658 | { | |
659 | i=ssl2_read(s,(char *)&(buf[s->init_num]), | |
660 | SSL2_MAX_CERT_CHALLENGE_LENGTH+1-s->init_num); | |
661 | if (i<(SSL2_MIN_CERT_CHALLENGE_LENGTH+1-s->init_num)) | |
662 | return(ssl2_part_read(s,SSL_F_CLIENT_CERTIFICATE,i)); | |
663 | ||
664 | /* type=buf[0]; */ | |
665 | /* type eq x509 */ | |
666 | if (buf[1] != SSL2_AT_MD5_WITH_RSA_ENCRYPTION) | |
667 | { | |
668 | ssl2_return_error(s,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE); | |
669 | SSLerr(SSL_F_CLIENT_CERTIFICATE,SSL_R_BAD_AUTHENTICATION_TYPE); | |
670 | return(-1); | |
671 | } | |
672 | cert_ch_len=i-1; | |
673 | ||
674 | if ((s->cert == NULL) || | |
675 | (s->cert->key->x509 == NULL) || | |
676 | (s->cert->key->privatekey == NULL)) | |
677 | { | |
678 | s->state=SSL2_ST_X509_GET_CLIENT_CERTIFICATE; | |
679 | } | |
680 | else | |
681 | s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_C; | |
682 | } | |
683 | ||
684 | if (s->state == SSL2_ST_X509_GET_CLIENT_CERTIFICATE) | |
685 | { | |
686 | X509 *x509=NULL; | |
687 | EVP_PKEY *pkey=NULL; | |
688 | ||
689 | /* If we get an error we need to | |
690 | * ssl->rwstate=SSL_X509_LOOKUP; | |
691 | * return(error); | |
692 | * We should then be retried when things are ok and we | |
693 | * can get a cert or not */ | |
694 | ||
695 | i=0; | |
696 | if (s->ctx->client_cert_cb != NULL) | |
697 | { | |
698 | i=s->ctx->client_cert_cb(s,&(x509),&(pkey)); | |
699 | } | |
700 | ||
701 | if (i < 0) | |
702 | { | |
703 | s->rwstate=SSL_X509_LOOKUP; | |
704 | return(-1); | |
705 | } | |
706 | s->rwstate=SSL_NOTHING; | |
707 | ||
708 | if ((i == 1) && (pkey != NULL) && (x509 != NULL)) | |
709 | { | |
710 | s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_C; | |
711 | if ( !SSL_use_certificate(s,x509) || | |
712 | !SSL_use_PrivateKey(s,pkey)) | |
713 | { | |
714 | i=0; | |
715 | } | |
716 | X509_free(x509); | |
717 | EVP_PKEY_free(pkey); | |
718 | } | |
719 | else if (i == 1) | |
720 | { | |
721 | if (x509 != NULL) X509_free(x509); | |
722 | if (pkey != NULL) EVP_PKEY_free(pkey); | |
723 | SSLerr(SSL_F_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); | |
724 | i=0; | |
725 | } | |
726 | ||
727 | if (i == 0) | |
728 | { | |
729 | /* We have no client certificate to respond with | |
730 | * so send the correct error message back */ | |
731 | s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_B; | |
732 | p=buf; | |
733 | *(p++)=SSL2_MT_ERROR; | |
734 | s2n(SSL2_PE_NO_CERTIFICATE,p); | |
735 | s->init_off=0; | |
736 | s->init_num=3; | |
737 | /* Write is done at the end */ | |
738 | } | |
739 | } | |
740 | ||
741 | if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_B) | |
742 | { | |
743 | return(ssl2_do_write(s)); | |
744 | } | |
745 | ||
746 | if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_C) | |
747 | { | |
748 | EVP_MD_CTX ctx; | |
749 | ||
750 | /* ok, now we calculate the checksum | |
751 | * do it first so we can reuse buf :-) */ | |
752 | p=buf; | |
58964a49 | 753 | EVP_SignInit(&ctx,s->ctx->rsa_md5); |
d02b48c6 RE |
754 | EVP_SignUpdate(&ctx,s->s2->key_material, |
755 | (unsigned int)s->s2->key_material_length); | |
756 | EVP_SignUpdate(&ctx,cert_ch,(unsigned int)cert_ch_len); | |
b56bce4f | 757 | n=i2d_X509(s->session->sess_cert->peer_key->x509,&p); |
d02b48c6 RE |
758 | EVP_SignUpdate(&ctx,buf,(unsigned int)n); |
759 | ||
760 | p=buf; | |
761 | d=p+6; | |
762 | *(p++)=SSL2_MT_CLIENT_CERTIFICATE; | |
763 | *(p++)=SSL2_CT_X509_CERTIFICATE; | |
764 | n=i2d_X509(s->cert->key->x509,&d); | |
765 | s2n(n,p); | |
766 | ||
767 | if (!EVP_SignFinal(&ctx,d,&n,s->cert->key->privatekey)) | |
768 | { | |
769 | /* this is not good. If things have failed it | |
770 | * means there so something wrong with the key. | |
657e60fa | 771 | * We will continue with a 0 length signature |
d02b48c6 RE |
772 | */ |
773 | } | |
774 | memset(&ctx,0,sizeof(ctx)); | |
775 | s2n(n,p); | |
776 | d+=n; | |
777 | ||
778 | s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_D; | |
779 | s->init_num=d-buf; | |
780 | s->init_off=0; | |
781 | } | |
782 | /* if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_D) */ | |
783 | return(ssl2_do_write(s)); | |
784 | } | |
785 | ||
6b691a5c | 786 | static int get_server_verify(SSL *s) |
d02b48c6 RE |
787 | { |
788 | unsigned char *p; | |
789 | int i; | |
790 | ||
791 | p=(unsigned char *)s->init_buf->data; | |
792 | if (s->state == SSL2_ST_GET_SERVER_VERIFY_A) | |
793 | { | |
794 | i=ssl2_read(s,(char *)&(p[s->init_num]),1-s->init_num); | |
795 | if (i < (1-s->init_num)) | |
796 | return(ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i)); | |
797 | ||
798 | s->state= SSL2_ST_GET_SERVER_VERIFY_B; | |
799 | s->init_num=0; | |
800 | if (*p != SSL2_MT_SERVER_VERIFY) | |
801 | { | |
802 | if (p[0] != SSL2_MT_ERROR) | |
803 | { | |
804 | ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); | |
805 | SSLerr(SSL_F_GET_SERVER_VERIFY, | |
806 | SSL_R_READ_WRONG_PACKET_TYPE); | |
807 | } | |
808 | else | |
809 | SSLerr(SSL_F_GET_SERVER_VERIFY, | |
810 | SSL_R_PEER_ERROR); | |
811 | return(-1); | |
812 | } | |
813 | } | |
814 | ||
815 | p=(unsigned char *)s->init_buf->data; | |
816 | i=ssl2_read(s,(char *)&(p[s->init_num]), | |
817 | (unsigned int)s->s2->challenge_length-s->init_num); | |
818 | if (i < ((int)s->s2->challenge_length-s->init_num)) | |
819 | return(ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i)); | |
820 | if (memcmp(p,s->s2->challenge,(unsigned int)s->s2->challenge_length) != 0) | |
821 | { | |
822 | ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); | |
823 | SSLerr(SSL_F_GET_SERVER_VERIFY,SSL_R_CHALLENGE_IS_DIFFERENT); | |
824 | return(-1); | |
825 | } | |
826 | return(1); | |
827 | } | |
828 | ||
6b691a5c | 829 | static int get_server_finished(SSL *s) |
d02b48c6 RE |
830 | { |
831 | unsigned char *buf; | |
832 | unsigned char *p; | |
833 | int i; | |
834 | ||
835 | buf=(unsigned char *)s->init_buf->data; | |
836 | p=buf; | |
837 | if (s->state == SSL2_ST_GET_SERVER_FINISHED_A) | |
838 | { | |
839 | i=ssl2_read(s,(char *)&(buf[s->init_num]),1-s->init_num); | |
840 | if (i < (1-s->init_num)) | |
841 | return(ssl2_part_read(s,SSL_F_GET_SERVER_FINISHED,i)); | |
842 | s->init_num=i; | |
843 | if (*p == SSL2_MT_REQUEST_CERTIFICATE) | |
844 | { | |
845 | s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_A; | |
846 | return(1); | |
847 | } | |
848 | else if (*p != SSL2_MT_SERVER_FINISHED) | |
849 | { | |
850 | if (p[0] != SSL2_MT_ERROR) | |
851 | { | |
852 | ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); | |
853 | SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_READ_WRONG_PACKET_TYPE); | |
854 | } | |
855 | else | |
856 | SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_PEER_ERROR); | |
857 | return(-1); | |
858 | } | |
859 | s->state=SSL_ST_OK; | |
860 | s->init_num=0; | |
861 | } | |
862 | ||
863 | i=ssl2_read(s,(char *)&(buf[s->init_num]), | |
864 | SSL2_SSL_SESSION_ID_LENGTH-s->init_num); | |
865 | if (i < (SSL2_SSL_SESSION_ID_LENGTH-s->init_num)) | |
866 | return(ssl2_part_read(s,SSL_F_GET_SERVER_FINISHED,i)); | |
867 | ||
868 | if (!s->hit) /* new session */ | |
869 | { | |
870 | /* new session-id */ | |
58964a49 RE |
871 | /* Make sure we were not trying to re-use an old SSL_SESSION |
872 | * or bad things can happen */ | |
873 | /* ZZZZZZZZZZZZZ */ | |
d02b48c6 RE |
874 | s->session->session_id_length=SSL2_SSL_SESSION_ID_LENGTH; |
875 | memcpy(s->session->session_id,p,SSL2_SSL_SESSION_ID_LENGTH); | |
876 | } | |
877 | else | |
878 | { | |
58964a49 | 879 | if (!(s->options & SSL_OP_MICROSOFT_SESS_ID_BUG)) |
d02b48c6 RE |
880 | { |
881 | if (memcmp(buf,s->session->session_id, | |
882 | (unsigned int)s->session->session_id_length) != 0) | |
883 | { | |
884 | ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); | |
885 | SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_SSL_SESSION_ID_IS_DIFFERENT); | |
886 | return(-1); | |
887 | } | |
888 | } | |
889 | } | |
890 | return(1); | |
891 | } | |
892 | ||
893 | /* loads in the certificate from the server */ | |
6b691a5c | 894 | int ssl2_set_certificate(SSL *s, int type, int len, unsigned char *data) |
d02b48c6 | 895 | { |
f73e07cf | 896 | STACK_OF(X509) *sk=NULL; |
d02b48c6 | 897 | EVP_PKEY *pkey=NULL; |
b56bce4f | 898 | SESS_CERT *sc=NULL; |
d02b48c6 RE |
899 | int i; |
900 | X509 *x509=NULL; | |
901 | int ret=0; | |
902 | ||
903 | x509=d2i_X509(NULL,&data,(long)len); | |
904 | if (x509 == NULL) | |
905 | { | |
906 | SSLerr(SSL_F_SSL2_SET_CERTIFICATE,ERR_R_X509_LIB); | |
907 | goto err; | |
908 | } | |
909 | ||
f73e07cf | 910 | if ((sk=sk_X509_new_null()) == NULL || !sk_X509_push(sk,x509)) |
d02b48c6 RE |
911 | { |
912 | SSLerr(SSL_F_SSL2_SET_CERTIFICATE,ERR_R_MALLOC_FAILURE); | |
913 | goto err; | |
914 | } | |
915 | ||
916 | i=ssl_verify_cert_chain(s,sk); | |
917 | ||
918 | if ((s->verify_mode != SSL_VERIFY_NONE) && (!i)) | |
919 | { | |
920 | SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED); | |
921 | goto err; | |
922 | } | |
1fab73ac | 923 | ERR_clear_error(); /* but we keep s->verify_result */ |
0dd2254d | 924 | s->session->verify_result = s->verify_result; |
d02b48c6 | 925 | |
b56bce4f BM |
926 | /* server's cert for this session */ |
927 | sc=ssl_sess_cert_new(); | |
928 | if (sc == NULL) | |
d02b48c6 RE |
929 | { |
930 | ret= -1; | |
931 | goto err; | |
932 | } | |
b56bce4f BM |
933 | if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert); |
934 | s->session->sess_cert=sc; | |
d02b48c6 | 935 | |
b56bce4f BM |
936 | sc->peer_pkeys[SSL_PKEY_RSA_ENC].x509=x509; |
937 | sc->peer_key= &(sc->peer_pkeys[SSL_PKEY_RSA_ENC]); | |
d02b48c6 RE |
938 | |
939 | pkey=X509_get_pubkey(x509); | |
940 | x509=NULL; | |
941 | if (pkey == NULL) | |
942 | { | |
943 | SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_UNABLE_TO_EXTRACT_PUBLIC_KEY); | |
944 | goto err; | |
945 | } | |
946 | if (pkey->type != EVP_PKEY_RSA) | |
947 | { | |
948 | SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_PUBLIC_KEY_NOT_RSA); | |
949 | goto err; | |
950 | } | |
951 | ||
b56bce4f | 952 | if (!ssl_set_peer_cert_type(sc,SSL2_CT_X509_CERTIFICATE)) |
d02b48c6 RE |
953 | goto err; |
954 | ret=1; | |
955 | err: | |
f73e07cf | 956 | sk_X509_free(sk); |
a8236c8c DSH |
957 | X509_free(x509); |
958 | EVP_PKEY_free(pkey); | |
d02b48c6 RE |
959 | return(ret); |
960 | } | |
961 | ||
b56bce4f | 962 | static int ssl_rsa_public_encrypt(SESS_CERT *sc, int len, unsigned char *from, |
6b691a5c | 963 | unsigned char *to, int padding) |
d02b48c6 RE |
964 | { |
965 | EVP_PKEY *pkey=NULL; | |
966 | int i= -1; | |
967 | ||
b56bce4f BM |
968 | if ((sc == NULL) || (sc->peer_key->x509 == NULL) || |
969 | ((pkey=X509_get_pubkey(sc->peer_key->x509)) == NULL)) | |
d02b48c6 RE |
970 | { |
971 | SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,SSL_R_NO_PUBLICKEY); | |
972 | return(-1); | |
973 | } | |
974 | if (pkey->type != EVP_PKEY_RSA) | |
975 | { | |
976 | SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,SSL_R_PUBLIC_KEY_IS_NOT_RSA); | |
977 | goto end; | |
978 | } | |
979 | ||
980 | /* we have the public key */ | |
981 | i=RSA_public_encrypt(len,from,to,pkey->pkey.rsa,padding); | |
982 | if (i < 0) | |
983 | SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,ERR_R_RSA_LIB); | |
984 | end: | |
a8236c8c | 985 | EVP_PKEY_free(pkey); |
d02b48c6 RE |
986 | return(i); |
987 | } | |
aa82db4f | 988 | #else /* !NO_SSL2 */ |
752d706a BL |
989 | |
990 | # if PEDANTIC | |
991 | static void *dummy=&dummy; | |
992 | # endif | |
993 | ||
79df9d62 | 994 | #endif |