s3_both.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /* ssl/s3_both.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  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. /* ====================================================================
  59. * Copyright (c) 1998-2002 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. */
  111. #include <limits.h>
  112. #include <string.h>
  113. #include <stdio.h>
  114. #include "ssl_locl.h"
  115. #include <openssl/buffer.h>
  116. #include <openssl/rand.h>
  117. #include <openssl/objects.h>
  118. #include <openssl/evp.h>
  119. #include <openssl/x509.h>
  120. /* send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or SSL3_RT_CHANGE_CIPHER_SPEC) */
  121. int ssl3_do_write(SSL *s, int type)
  122. {
  123. int ret;
  124. ret=ssl3_write_bytes(s,type,&s->init_buf->data[s->init_off],
  125. s->init_num);
  126. if (ret < 0) return(-1);
  127. if (type == SSL3_RT_HANDSHAKE)
  128. /* should not be done for 'Hello Request's, but in that case
  129. * we'll ignore the result anyway */
  130. ssl3_finish_mac(s,(unsigned char *)&s->init_buf->data[s->init_off],ret);
  131. if (ret == s->init_num)
  132. {
  133. if (s->msg_callback)
  134. s->msg_callback(1, s->version, type, s->init_buf->data, (size_t)(s->init_off + s->init_num), s, s->msg_callback_arg);
  135. return(1);
  136. }
  137. s->init_off+=ret;
  138. s->init_num-=ret;
  139. return(0);
  140. }
  141. int ssl3_send_finished(SSL *s, int a, int b, const char *sender, int slen)
  142. {
  143. unsigned char *p,*d;
  144. int i;
  145. unsigned long l;
  146. if (s->state == a)
  147. {
  148. d=(unsigned char *)s->init_buf->data;
  149. p= &(d[4]);
  150. i=s->method->ssl3_enc->final_finish_mac(s,
  151. &(s->s3->finish_dgst1),
  152. &(s->s3->finish_dgst2),
  153. sender,slen,s->s3->tmp.finish_md);
  154. s->s3->tmp.finish_md_len = i;
  155. memcpy(p, s->s3->tmp.finish_md, i);
  156. p+=i;
  157. l=i;
  158. #ifdef OPENSSL_SYS_WIN16
  159. /* MSVC 1.5 does not clear the top bytes of the word unless
  160. * I do this.
  161. */
  162. l&=0xffff;
  163. #endif
  164. *(d++)=SSL3_MT_FINISHED;
  165. l2n3(l,d);
  166. s->init_num=(int)l+4;
  167. s->init_off=0;
  168. s->state=b;
  169. }
  170. /* SSL3_ST_SEND_xxxxxx_HELLO_B */
  171. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  172. }
  173. int ssl3_get_finished(SSL *s, int a, int b)
  174. {
  175. int al,i,ok;
  176. long n;
  177. unsigned char *p;
  178. /* the mac has already been generated when we received the
  179. * change cipher spec message and is in s->s3->tmp.peer_finish_md
  180. */
  181. n=ssl3_get_message(s,
  182. a,
  183. b,
  184. SSL3_MT_FINISHED,
  185. 64, /* should actually be 36+4 :-) */
  186. &ok);
  187. if (!ok) return((int)n);
  188. /* If this occurs, we have missed a message */
  189. if (!s->s3->change_cipher_spec)
  190. {
  191. al=SSL_AD_UNEXPECTED_MESSAGE;
  192. SSLerr(SSL_F_SSL3_GET_FINISHED,SSL_R_GOT_A_FIN_BEFORE_A_CCS);
  193. goto f_err;
  194. }
  195. s->s3->change_cipher_spec=0;
  196. p = (unsigned char *)s->init_msg;
  197. i = s->s3->tmp.peer_finish_md_len;
  198. if (i != n)
  199. {
  200. al=SSL_AD_DECODE_ERROR;
  201. SSLerr(SSL_F_SSL3_GET_FINISHED,SSL_R_BAD_DIGEST_LENGTH);
  202. goto f_err;
  203. }
  204. if (memcmp(p, s->s3->tmp.peer_finish_md, i) != 0)
  205. {
  206. al=SSL_AD_DECRYPT_ERROR;
  207. SSLerr(SSL_F_SSL3_GET_FINISHED,SSL_R_DIGEST_CHECK_FAILED);
  208. goto f_err;
  209. }
  210. return(1);
  211. f_err:
  212. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  213. return(0);
  214. }
  215. /* for these 2 messages, we need to
  216. * ssl->enc_read_ctx re-init
  217. * ssl->s3->read_sequence zero
  218. * ssl->s3->read_mac_secret re-init
  219. * ssl->session->read_sym_enc assign
  220. * ssl->session->read_compression assign
  221. * ssl->session->read_hash assign
  222. */
  223. int ssl3_send_change_cipher_spec(SSL *s, int a, int b)
  224. {
  225. unsigned char *p;
  226. if (s->state == a)
  227. {
  228. p=(unsigned char *)s->init_buf->data;
  229. *p=SSL3_MT_CCS;
  230. s->init_num=1;
  231. s->init_off=0;
  232. s->state=b;
  233. }
  234. /* SSL3_ST_CW_CHANGE_B */
  235. return(ssl3_do_write(s,SSL3_RT_CHANGE_CIPHER_SPEC));
  236. }
  237. unsigned long ssl3_output_cert_chain(SSL *s, X509 *x)
  238. {
  239. unsigned char *p;
  240. int n,i;
  241. unsigned long l=7;
  242. BUF_MEM *buf;
  243. X509_STORE_CTX xs_ctx;
  244. X509_OBJECT obj;
  245. int no_chain;
  246. if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || s->ctx->extra_certs)
  247. no_chain = 1;
  248. else
  249. no_chain = 0;
  250. /* TLSv1 sends a chain with nothing in it, instead of an alert */
  251. buf=s->init_buf;
  252. if (!BUF_MEM_grow_clean(buf,10))
  253. {
  254. SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN,ERR_R_BUF_LIB);
  255. return(0);
  256. }
  257. if (x != NULL)
  258. {
  259. if(!no_chain && !X509_STORE_CTX_init(&xs_ctx,s->ctx->cert_store,NULL,NULL))
  260. {
  261. SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN,ERR_R_X509_LIB);
  262. return(0);
  263. }
  264. for (;;)
  265. {
  266. n=i2d_X509(x,NULL);
  267. if (!BUF_MEM_grow_clean(buf,(int)(n+l+3)))
  268. {
  269. SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN,ERR_R_BUF_LIB);
  270. return(0);
  271. }
  272. p=(unsigned char *)&(buf->data[l]);
  273. l2n3(n,p);
  274. i2d_X509(x,&p);
  275. l+=n+3;
  276. if (no_chain)
  277. break;
  278. if (X509_NAME_cmp(X509_get_subject_name(x),
  279. X509_get_issuer_name(x)) == 0) break;
  280. i=X509_STORE_get_by_subject(&xs_ctx,X509_LU_X509,
  281. X509_get_issuer_name(x),&obj);
  282. if (i <= 0) break;
  283. x=obj.data.x509;
  284. /* Count is one too high since the X509_STORE_get uped the
  285. * ref count */
  286. X509_free(x);
  287. }
  288. if (!no_chain)
  289. X509_STORE_CTX_cleanup(&xs_ctx);
  290. }
  291. /* Thawte special :-) */
  292. if (s->ctx->extra_certs != NULL)
  293. for (i=0; i<sk_X509_num(s->ctx->extra_certs); i++)
  294. {
  295. x=sk_X509_value(s->ctx->extra_certs,i);
  296. n=i2d_X509(x,NULL);
  297. if (!BUF_MEM_grow_clean(buf,(int)(n+l+3)))
  298. {
  299. SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN,ERR_R_BUF_LIB);
  300. return(0);
  301. }
  302. p=(unsigned char *)&(buf->data[l]);
  303. l2n3(n,p);
  304. i2d_X509(x,&p);
  305. l+=n+3;
  306. }
  307. l-=7;
  308. p=(unsigned char *)&(buf->data[4]);
  309. l2n3(l,p);
  310. l+=3;
  311. p=(unsigned char *)&(buf->data[0]);
  312. *(p++)=SSL3_MT_CERTIFICATE;
  313. l2n3(l,p);
  314. l+=4;
  315. return(l);
  316. }
  317. /* Obtain handshake message of message type 'mt' (any if mt == -1),
  318. * maximum acceptable body length 'max'.
  319. * The first four bytes (msg_type and length) are read in state 'st1',
  320. * the body is read in state 'stn'.
  321. */
  322. long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)
  323. {
  324. unsigned char *p;
  325. unsigned long l;
  326. long n;
  327. int i,al;
  328. if (s->s3->tmp.reuse_message)
  329. {
  330. s->s3->tmp.reuse_message=0;
  331. if ((mt >= 0) && (s->s3->tmp.message_type != mt))
  332. {
  333. al=SSL_AD_UNEXPECTED_MESSAGE;
  334. SSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_UNEXPECTED_MESSAGE);
  335. goto f_err;
  336. }
  337. *ok=1;
  338. s->init_msg = s->init_buf->data + 4;
  339. s->init_num = (int)s->s3->tmp.message_size;
  340. return s->init_num;
  341. }
  342. p=(unsigned char *)s->init_buf->data;
  343. if (s->state == st1) /* s->init_num < 4 */
  344. {
  345. int skip_message;
  346. do
  347. {
  348. while (s->init_num < 4)
  349. {
  350. i=ssl3_read_bytes(s,SSL3_RT_HANDSHAKE,&p[s->init_num],
  351. 4 - s->init_num, 0);
  352. if (i <= 0)
  353. {
  354. s->rwstate=SSL_READING;
  355. *ok = 0;
  356. return i;
  357. }
  358. s->init_num+=i;
  359. }
  360. skip_message = 0;
  361. if (!s->server)
  362. if (p[0] == SSL3_MT_HELLO_REQUEST)
  363. /* The server may always send 'Hello Request' messages --
  364. * we are doing a handshake anyway now, so ignore them
  365. * if their format is correct. Does not count for
  366. * 'Finished' MAC. */
  367. if (p[1] == 0 && p[2] == 0 &&p[3] == 0)
  368. {
  369. s->init_num = 0;
  370. skip_message = 1;
  371. if (s->msg_callback)
  372. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, p, 4, s, s->msg_callback_arg);
  373. }
  374. }
  375. while (skip_message);
  376. /* s->init_num == 4 */
  377. if ((mt >= 0) && (*p != mt))
  378. {
  379. al=SSL_AD_UNEXPECTED_MESSAGE;
  380. SSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_UNEXPECTED_MESSAGE);
  381. goto f_err;
  382. }
  383. if ((mt < 0) && (*p == SSL3_MT_CLIENT_HELLO) &&
  384. (st1 == SSL3_ST_SR_CERT_A) &&
  385. (stn == SSL3_ST_SR_CERT_B))
  386. {
  387. /* At this point we have got an MS SGC second client
  388. * hello (maybe we should always allow the client to
  389. * start a new handshake?). We need to restart the mac.
  390. * Don't increment {num,total}_renegotiations because
  391. * we have not completed the handshake. */
  392. ssl3_init_finished_mac(s);
  393. }
  394. s->s3->tmp.message_type= *(p++);
  395. n2l3(p,l);
  396. if (l > (unsigned long)max)
  397. {
  398. al=SSL_AD_ILLEGAL_PARAMETER;
  399. SSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_EXCESSIVE_MESSAGE_SIZE);
  400. goto f_err;
  401. }
  402. if (l > (INT_MAX-4)) /* BUF_MEM_grow takes an 'int' parameter */
  403. {
  404. al=SSL_AD_ILLEGAL_PARAMETER;
  405. SSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_EXCESSIVE_MESSAGE_SIZE);
  406. goto f_err;
  407. }
  408. if (l && !BUF_MEM_grow_clean(s->init_buf,(int)l+4))
  409. {
  410. SSLerr(SSL_F_SSL3_GET_MESSAGE,ERR_R_BUF_LIB);
  411. goto err;
  412. }
  413. s->s3->tmp.message_size=l;
  414. s->state=stn;
  415. s->init_msg = s->init_buf->data + 4;
  416. s->init_num = 0;
  417. }
  418. /* next state (stn) */
  419. p = s->init_msg;
  420. n = s->s3->tmp.message_size - s->init_num;
  421. while (n > 0)
  422. {
  423. i=ssl3_read_bytes(s,SSL3_RT_HANDSHAKE,&p[s->init_num],n,0);
  424. if (i <= 0)
  425. {
  426. s->rwstate=SSL_READING;
  427. *ok = 0;
  428. return i;
  429. }
  430. s->init_num += i;
  431. n -= i;
  432. }
  433. ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, s->init_num + 4);
  434. if (s->msg_callback)
  435. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data, (size_t)s->init_num + 4, s, s->msg_callback_arg);
  436. *ok=1;
  437. return s->init_num;
  438. f_err:
  439. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  440. err:
  441. *ok=0;
  442. return(-1);
  443. }
  444. int ssl_cert_type(X509 *x, EVP_PKEY *pkey)
  445. {
  446. EVP_PKEY *pk;
  447. int ret= -1,i,j;
  448. if (pkey == NULL)
  449. pk=X509_get_pubkey(x);
  450. else
  451. pk=pkey;
  452. if (pk == NULL) goto err;
  453. i=pk->type;
  454. if (i == EVP_PKEY_RSA)
  455. {
  456. ret=SSL_PKEY_RSA_ENC;
  457. if (x != NULL)
  458. {
  459. j=X509_get_ext_count(x);
  460. /* check to see if this is a signing only certificate */
  461. /* EAY EAY EAY EAY */
  462. }
  463. }
  464. else if (i == EVP_PKEY_DSA)
  465. {
  466. ret=SSL_PKEY_DSA_SIGN;
  467. }
  468. else if (i == EVP_PKEY_DH)
  469. {
  470. /* if we just have a key, we needs to be guess */
  471. if (x == NULL)
  472. ret=SSL_PKEY_DH_DSA;
  473. else
  474. {
  475. j=X509_get_signature_type(x);
  476. if (j == EVP_PKEY_RSA)
  477. ret=SSL_PKEY_DH_RSA;
  478. else if (j== EVP_PKEY_DSA)
  479. ret=SSL_PKEY_DH_DSA;
  480. else ret= -1;
  481. }
  482. }
  483. else
  484. ret= -1;
  485. err:
  486. if(!pkey) EVP_PKEY_free(pk);
  487. return(ret);
  488. }
  489. int ssl_verify_alarm_type(long type)
  490. {
  491. int al;
  492. switch(type)
  493. {
  494. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  495. case X509_V_ERR_UNABLE_TO_GET_CRL:
  496. case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
  497. al=SSL_AD_UNKNOWN_CA;
  498. break;
  499. case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
  500. case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
  501. case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
  502. case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  503. case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  504. case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
  505. case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
  506. case X509_V_ERR_CERT_NOT_YET_VALID:
  507. case X509_V_ERR_CRL_NOT_YET_VALID:
  508. case X509_V_ERR_CERT_UNTRUSTED:
  509. case X509_V_ERR_CERT_REJECTED:
  510. al=SSL_AD_BAD_CERTIFICATE;
  511. break;
  512. case X509_V_ERR_CERT_SIGNATURE_FAILURE:
  513. case X509_V_ERR_CRL_SIGNATURE_FAILURE:
  514. al=SSL_AD_DECRYPT_ERROR;
  515. break;
  516. case X509_V_ERR_CERT_HAS_EXPIRED:
  517. case X509_V_ERR_CRL_HAS_EXPIRED:
  518. al=SSL_AD_CERTIFICATE_EXPIRED;
  519. break;
  520. case X509_V_ERR_CERT_REVOKED:
  521. al=SSL_AD_CERTIFICATE_REVOKED;
  522. break;
  523. case X509_V_ERR_OUT_OF_MEM:
  524. al=SSL_AD_INTERNAL_ERROR;
  525. break;
  526. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  527. case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
  528. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
  529. case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
  530. case X509_V_ERR_CERT_CHAIN_TOO_LONG:
  531. case X509_V_ERR_PATH_LENGTH_EXCEEDED:
  532. case X509_V_ERR_INVALID_CA:
  533. al=SSL_AD_UNKNOWN_CA;
  534. break;
  535. case X509_V_ERR_APPLICATION_VERIFICATION:
  536. al=SSL_AD_HANDSHAKE_FAILURE;
  537. break;
  538. case X509_V_ERR_INVALID_PURPOSE:
  539. al=SSL_AD_UNSUPPORTED_CERTIFICATE;
  540. break;
  541. default:
  542. al=SSL_AD_CERTIFICATE_UNKNOWN;
  543. break;
  544. }
  545. return(al);
  546. }
  547. int ssl3_setup_buffers(SSL *s)
  548. {
  549. unsigned char *p;
  550. unsigned int extra;
  551. size_t len;
  552. if (s->s3->rbuf.buf == NULL)
  553. {
  554. if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER)
  555. extra=SSL3_RT_MAX_EXTRA;
  556. else
  557. extra=0;
  558. len = SSL3_RT_MAX_PACKET_SIZE + extra;
  559. if ((p=OPENSSL_malloc(len)) == NULL)
  560. goto err;
  561. s->s3->rbuf.buf = p;
  562. s->s3->rbuf.len = len;
  563. }
  564. if (s->s3->wbuf.buf == NULL)
  565. {
  566. len = SSL3_RT_MAX_PACKET_SIZE;
  567. len += SSL3_RT_HEADER_LENGTH + 256; /* extra space for empty fragment */
  568. if ((p=OPENSSL_malloc(len)) == NULL)
  569. goto err;
  570. s->s3->wbuf.buf = p;
  571. s->s3->wbuf.len = len;
  572. }
  573. s->packet= &(s->s3->rbuf.buf[0]);
  574. return(1);
  575. err:
  576. SSLerr(SSL_F_SSL3_SETUP_BUFFERS,ERR_R_MALLOC_FAILURE);
  577. return(0);
  578. }