You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

772 lines
22 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com). */
  108. /* ====================================================================
  109. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  110. * ECC cipher suite support in OpenSSL originally developed by
  111. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */
  112. #include <assert.h>
  113. #include <limits.h>
  114. #include <stdio.h>
  115. #include <string.h>
  116. #include <openssl/buf.h>
  117. #include <openssl/evp.h>
  118. #include <openssl/mem.h>
  119. #include <openssl/obj.h>
  120. #include <openssl/rand.h>
  121. #include <openssl/x509.h>
  122. #include "ssl_locl.h"
  123. /* send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or SSL3_RT_CHANGE_CIPHER_SPEC) */
  124. int ssl3_do_write(SSL *s, int type)
  125. {
  126. int ret;
  127. ret=ssl3_write_bytes(s,type,&s->init_buf->data[s->init_off],
  128. s->init_num);
  129. if (ret < 0) return(-1);
  130. if (type == SSL3_RT_HANDSHAKE)
  131. /* should not be done for 'Hello Request's, but in that case
  132. * we'll ignore the result anyway */
  133. ssl3_finish_mac(s,(unsigned char *)&s->init_buf->data[s->init_off],ret);
  134. if (ret == s->init_num)
  135. {
  136. if (s->msg_callback)
  137. s->msg_callback(1, s->version, type, s->init_buf->data, (size_t)(s->init_off + s->init_num), s, s->msg_callback_arg);
  138. return(1);
  139. }
  140. s->init_off+=ret;
  141. s->init_num-=ret;
  142. return(0);
  143. }
  144. int ssl3_send_finished(SSL *s, int a, int b, const char *sender, int slen)
  145. {
  146. unsigned char *p;
  147. int i;
  148. unsigned long l;
  149. if (s->state == a)
  150. {
  151. p = ssl_handshake_start(s);
  152. i=s->method->ssl3_enc->final_finish_mac(s,
  153. sender,slen,s->s3->tmp.finish_md);
  154. if (i == 0)
  155. return 0;
  156. s->s3->tmp.finish_md_len = i;
  157. memcpy(p, s->s3->tmp.finish_md, i);
  158. l=i;
  159. /* Copy the finished so we can use it for
  160. renegotiation checks */
  161. if(s->type == SSL_ST_CONNECT)
  162. {
  163. assert(i <= EVP_MAX_MD_SIZE);
  164. memcpy(s->s3->previous_client_finished,
  165. s->s3->tmp.finish_md, i);
  166. s->s3->previous_client_finished_len=i;
  167. }
  168. else
  169. {
  170. assert(i <= EVP_MAX_MD_SIZE);
  171. memcpy(s->s3->previous_server_finished,
  172. s->s3->tmp.finish_md, i);
  173. s->s3->previous_server_finished_len=i;
  174. }
  175. #ifdef OPENSSL_SYS_WIN16
  176. /* MSVC 1.5 does not clear the top bytes of the word unless
  177. * I do this.
  178. */
  179. l&=0xffff;
  180. #endif
  181. ssl_set_handshake_header(s, SSL3_MT_FINISHED, l);
  182. s->state=b;
  183. }
  184. /* SSL3_ST_SEND_xxxxxx_HELLO_B */
  185. return ssl_do_write(s);
  186. }
  187. #ifndef OPENSSL_NO_NEXTPROTONEG
  188. /* ssl3_take_mac calculates the Finished MAC for the handshakes messages seen to far. */
  189. static void ssl3_take_mac(SSL *s)
  190. {
  191. const char *sender;
  192. int slen;
  193. /* If no new cipher setup return immediately: other functions will
  194. * set the appropriate error.
  195. */
  196. if (s->s3->tmp.new_cipher == NULL)
  197. return;
  198. if (s->state & SSL_ST_CONNECT)
  199. {
  200. sender=s->method->ssl3_enc->server_finished_label;
  201. slen=s->method->ssl3_enc->server_finished_label_len;
  202. }
  203. else
  204. {
  205. sender=s->method->ssl3_enc->client_finished_label;
  206. slen=s->method->ssl3_enc->client_finished_label_len;
  207. }
  208. s->s3->tmp.peer_finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
  209. sender,slen,s->s3->tmp.peer_finish_md);
  210. }
  211. #endif
  212. int ssl3_get_finished(SSL *s, int a, int b)
  213. {
  214. int al,i,ok;
  215. long n;
  216. unsigned char *p;
  217. #ifdef OPENSSL_NO_NEXTPROTONEG
  218. /* the mac has already been generated when we received the
  219. * change cipher spec message and is in s->s3->tmp.peer_finish_md
  220. */
  221. #endif
  222. n=s->method->ssl_get_message(s,
  223. a,
  224. b,
  225. SSL3_MT_FINISHED,
  226. 64, /* should actually be 36+4 :-) */
  227. &ok);
  228. if (!ok) return((int)n);
  229. /* If this occurs, we have missed a message.
  230. * TODO(davidben): Is this check now redundant with
  231. * SSL3_FLAGS_EXPECT_CCS? */
  232. if (!s->s3->change_cipher_spec)
  233. {
  234. al=SSL_AD_UNEXPECTED_MESSAGE;
  235. OPENSSL_PUT_ERROR(SSL, ssl3_get_finished, SSL_R_GOT_A_FIN_BEFORE_A_CCS);
  236. goto f_err;
  237. }
  238. s->s3->change_cipher_spec=0;
  239. p = s->init_msg;
  240. i = s->s3->tmp.peer_finish_md_len;
  241. if (i != n)
  242. {
  243. al=SSL_AD_DECODE_ERROR;
  244. OPENSSL_PUT_ERROR(SSL, ssl3_get_finished, SSL_R_BAD_DIGEST_LENGTH);
  245. goto f_err;
  246. }
  247. if (CRYPTO_memcmp(p, s->s3->tmp.peer_finish_md, i) != 0)
  248. {
  249. al=SSL_AD_DECRYPT_ERROR;
  250. OPENSSL_PUT_ERROR(SSL, ssl3_get_finished, SSL_R_DIGEST_CHECK_FAILED);
  251. goto f_err;
  252. }
  253. /* Copy the finished so we can use it for
  254. renegotiation checks */
  255. if(s->type == SSL_ST_ACCEPT)
  256. {
  257. assert(i <= EVP_MAX_MD_SIZE);
  258. memcpy(s->s3->previous_client_finished,
  259. s->s3->tmp.peer_finish_md, i);
  260. s->s3->previous_client_finished_len=i;
  261. }
  262. else
  263. {
  264. assert(i <= EVP_MAX_MD_SIZE);
  265. memcpy(s->s3->previous_server_finished,
  266. s->s3->tmp.peer_finish_md, i);
  267. s->s3->previous_server_finished_len=i;
  268. }
  269. return(1);
  270. f_err:
  271. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  272. return(0);
  273. }
  274. /* for these 2 messages, we need to
  275. * ssl->enc_read_ctx re-init
  276. * ssl->s3->read_sequence zero
  277. * ssl->s3->read_mac_secret re-init
  278. * ssl->session->read_sym_enc assign
  279. * ssl->session->read_compression assign
  280. * ssl->session->read_hash assign
  281. */
  282. int ssl3_send_change_cipher_spec(SSL *s, int a, int b)
  283. {
  284. unsigned char *p;
  285. if (s->state == a)
  286. {
  287. p=(unsigned char *)s->init_buf->data;
  288. *p=SSL3_MT_CCS;
  289. s->init_num=1;
  290. s->init_off=0;
  291. s->state=b;
  292. }
  293. /* SSL3_ST_CW_CHANGE_B */
  294. return(ssl3_do_write(s,SSL3_RT_CHANGE_CIPHER_SPEC));
  295. }
  296. unsigned long ssl3_output_cert_chain(SSL *s, CERT_PKEY *cpk)
  297. {
  298. unsigned char *p;
  299. unsigned long l = 3 + SSL_HM_HEADER_LENGTH(s);
  300. if (!ssl_add_cert_chain(s, cpk, &l))
  301. return 0;
  302. l -= 3 + SSL_HM_HEADER_LENGTH(s);
  303. p = ssl_handshake_start(s);
  304. l2n3(l,p);
  305. l += 3;
  306. ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE, l);
  307. return l + SSL_HM_HEADER_LENGTH(s);
  308. }
  309. /* Obtain handshake message of message type 'mt' (any if mt == -1),
  310. * maximum acceptable body length 'max'.
  311. * The first four bytes (msg_type and length) are read in state 'st1',
  312. * the body is read in state 'stn'.
  313. */
  314. long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)
  315. {
  316. unsigned char *p;
  317. unsigned long l;
  318. long n;
  319. int i,al;
  320. if (s->s3->tmp.reuse_message)
  321. {
  322. s->s3->tmp.reuse_message=0;
  323. if ((mt >= 0) && (s->s3->tmp.message_type != mt))
  324. {
  325. al=SSL_AD_UNEXPECTED_MESSAGE;
  326. OPENSSL_PUT_ERROR(SSL, ssl3_get_message, SSL_R_UNEXPECTED_MESSAGE);
  327. goto f_err;
  328. }
  329. *ok=1;
  330. s->init_msg = (uint8_t*)s->init_buf->data + 4;
  331. s->init_num = (int)s->s3->tmp.message_size;
  332. return s->init_num;
  333. }
  334. p=(unsigned char *)s->init_buf->data;
  335. if (s->state == st1) /* s->init_num < 4 */
  336. {
  337. int skip_message;
  338. do
  339. {
  340. while (s->init_num < 4)
  341. {
  342. i=s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,
  343. &p[s->init_num],4 - s->init_num, 0);
  344. if (i <= 0)
  345. {
  346. s->rwstate=SSL_READING;
  347. *ok = 0;
  348. return i;
  349. }
  350. s->init_num+=i;
  351. }
  352. skip_message = 0;
  353. if (!s->server)
  354. if (p[0] == SSL3_MT_HELLO_REQUEST)
  355. /* The server may always send 'Hello Request' messages --
  356. * we are doing a handshake anyway now, so ignore them
  357. * if their format is correct. Does not count for
  358. * 'Finished' MAC. */
  359. if (p[1] == 0 && p[2] == 0 &&p[3] == 0)
  360. {
  361. s->init_num = 0;
  362. skip_message = 1;
  363. if (s->msg_callback)
  364. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, p, 4, s, s->msg_callback_arg);
  365. }
  366. }
  367. while (skip_message);
  368. /* s->init_num == 4 */
  369. if ((mt >= 0) && (*p != mt))
  370. {
  371. al=SSL_AD_UNEXPECTED_MESSAGE;
  372. OPENSSL_PUT_ERROR(SSL, ssl3_get_message, SSL_R_UNEXPECTED_MESSAGE);
  373. goto f_err;
  374. }
  375. if ((mt < 0) && (*p == SSL3_MT_CLIENT_HELLO) &&
  376. (st1 == SSL3_ST_SR_CERT_A) &&
  377. (stn == SSL3_ST_SR_CERT_B))
  378. {
  379. /* At this point we have got an MS SGC second client
  380. * hello (maybe we should always allow the client to
  381. * start a new handshake?). We need to restart the mac.
  382. * Don't increment {num,total}_renegotiations because
  383. * we have not completed the handshake. */
  384. ssl3_init_finished_mac(s);
  385. }
  386. s->s3->tmp.message_type= *(p++);
  387. n2l3(p,l);
  388. if (l > (unsigned long)max)
  389. {
  390. al=SSL_AD_ILLEGAL_PARAMETER;
  391. OPENSSL_PUT_ERROR(SSL, ssl3_get_message, SSL_R_EXCESSIVE_MESSAGE_SIZE);
  392. goto f_err;
  393. }
  394. if (l > (INT_MAX-4)) /* BUF_MEM_grow takes an 'int' parameter */
  395. {
  396. al=SSL_AD_ILLEGAL_PARAMETER;
  397. OPENSSL_PUT_ERROR(SSL, ssl3_get_message, SSL_R_EXCESSIVE_MESSAGE_SIZE);
  398. goto f_err;
  399. }
  400. if (l && !BUF_MEM_grow_clean(s->init_buf,(int)l+4))
  401. {
  402. OPENSSL_PUT_ERROR(SSL, ssl3_get_message, ERR_R_BUF_LIB);
  403. goto err;
  404. }
  405. s->s3->tmp.message_size=l;
  406. s->state=stn;
  407. s->init_msg = (uint8_t*)s->init_buf->data + 4;
  408. s->init_num = 0;
  409. }
  410. /* next state (stn) */
  411. p = s->init_msg;
  412. n = s->s3->tmp.message_size - s->init_num;
  413. while (n > 0)
  414. {
  415. i=s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,&p[s->init_num],n,0);
  416. if (i <= 0)
  417. {
  418. s->rwstate=SSL_READING;
  419. *ok = 0;
  420. return i;
  421. }
  422. s->init_num += i;
  423. n -= i;
  424. }
  425. #ifndef OPENSSL_NO_NEXTPROTONEG
  426. /* If receiving Finished, record MAC of prior handshake messages for
  427. * Finished verification. */
  428. if (*s->init_buf->data == SSL3_MT_FINISHED)
  429. ssl3_take_mac(s);
  430. #endif
  431. /* Feed this message into MAC computation. */
  432. if (*((unsigned char*) s->init_buf->data) != SSL3_MT_ENCRYPTED_EXTENSIONS)
  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;
  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. }
  458. else if (i == EVP_PKEY_DSA)
  459. {
  460. ret=SSL_PKEY_DSA_SIGN;
  461. }
  462. #ifndef OPENSSL_NO_EC
  463. else if (i == EVP_PKEY_EC)
  464. {
  465. ret = SSL_PKEY_ECC;
  466. }
  467. #endif
  468. else if (x && (i == EVP_PKEY_DH || i == EVP_PKEY_DHX))
  469. {
  470. /* For DH two cases: DH certificate signed with RSA and
  471. * DH certificate signed with DSA.
  472. */
  473. i = X509_certificate_type(x, pk);
  474. if (i & EVP_PKS_RSA)
  475. ret = SSL_PKEY_DH_RSA;
  476. else if (i & EVP_PKS_DSA)
  477. ret = SSL_PKEY_DH_DSA;
  478. }
  479. err:
  480. if(!pkey) EVP_PKEY_free(pk);
  481. return(ret);
  482. }
  483. int ssl_verify_alarm_type(long type)
  484. {
  485. int al;
  486. switch(type)
  487. {
  488. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  489. case X509_V_ERR_UNABLE_TO_GET_CRL:
  490. case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
  491. al=SSL_AD_UNKNOWN_CA;
  492. break;
  493. case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
  494. case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
  495. case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
  496. case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  497. case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  498. case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
  499. case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
  500. case X509_V_ERR_CERT_NOT_YET_VALID:
  501. case X509_V_ERR_CRL_NOT_YET_VALID:
  502. case X509_V_ERR_CERT_UNTRUSTED:
  503. case X509_V_ERR_CERT_REJECTED:
  504. al=SSL_AD_BAD_CERTIFICATE;
  505. break;
  506. case X509_V_ERR_CERT_SIGNATURE_FAILURE:
  507. case X509_V_ERR_CRL_SIGNATURE_FAILURE:
  508. al=SSL_AD_DECRYPT_ERROR;
  509. break;
  510. case X509_V_ERR_CERT_HAS_EXPIRED:
  511. case X509_V_ERR_CRL_HAS_EXPIRED:
  512. al=SSL_AD_CERTIFICATE_EXPIRED;
  513. break;
  514. case X509_V_ERR_CERT_REVOKED:
  515. al=SSL_AD_CERTIFICATE_REVOKED;
  516. break;
  517. case X509_V_ERR_OUT_OF_MEM:
  518. al=SSL_AD_INTERNAL_ERROR;
  519. break;
  520. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  521. case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
  522. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
  523. case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
  524. case X509_V_ERR_CERT_CHAIN_TOO_LONG:
  525. case X509_V_ERR_PATH_LENGTH_EXCEEDED:
  526. case X509_V_ERR_INVALID_CA:
  527. al=SSL_AD_UNKNOWN_CA;
  528. break;
  529. case X509_V_ERR_APPLICATION_VERIFICATION:
  530. al=SSL_AD_HANDSHAKE_FAILURE;
  531. break;
  532. case X509_V_ERR_INVALID_PURPOSE:
  533. al=SSL_AD_UNSUPPORTED_CERTIFICATE;
  534. break;
  535. default:
  536. al=SSL_AD_CERTIFICATE_UNKNOWN;
  537. break;
  538. }
  539. return(al);
  540. }
  541. #ifndef OPENSSL_NO_BUF_FREELISTS
  542. /* On some platforms, malloc() performance is bad enough that you can't just
  543. * free() and malloc() buffers all the time, so we need to use freelists from
  544. * unused buffers. Currently, each freelist holds memory chunks of only a
  545. * given size (list->chunklen); other sized chunks are freed and malloced.
  546. * This doesn't help much if you're using many different SSL option settings
  547. * with a given context. (The options affecting buffer size are
  548. * max_send_fragment, read buffer vs write buffer,
  549. * SSL_OP_MICROSOFT_BIG_WRITE_BUFFER, SSL_OP_NO_COMPRESSION, and
  550. * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS.) Using a separate freelist for every
  551. * possible size is not an option, since max_send_fragment can take on many
  552. * different values.
  553. *
  554. * If you are on a platform with a slow malloc(), and you're using SSL
  555. * connections with many different settings for these options, and you need to
  556. * use the SSL_MOD_RELEASE_BUFFERS feature, you have a few options:
  557. * - Link against a faster malloc implementation.
  558. * - Use a separate SSL_CTX for each option set.
  559. * - Improve this code.
  560. */
  561. static void *
  562. freelist_extract(SSL_CTX *ctx, int for_read, int sz)
  563. {
  564. SSL3_BUF_FREELIST *list;
  565. SSL3_BUF_FREELIST_ENTRY *ent = NULL;
  566. void *result = NULL;
  567. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  568. list = for_read ? ctx->rbuf_freelist : ctx->wbuf_freelist;
  569. if (list != NULL && sz == (int)list->chunklen)
  570. ent = list->head;
  571. if (ent != NULL)
  572. {
  573. list->head = ent->next;
  574. result = ent;
  575. if (--list->len == 0)
  576. list->chunklen = 0;
  577. }
  578. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  579. if (!result)
  580. result = OPENSSL_malloc(sz);
  581. return result;
  582. }
  583. static void
  584. freelist_insert(SSL_CTX *ctx, int for_read, size_t sz, void *mem)
  585. {
  586. SSL3_BUF_FREELIST *list;
  587. SSL3_BUF_FREELIST_ENTRY *ent;
  588. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  589. list = for_read ? ctx->rbuf_freelist : ctx->wbuf_freelist;
  590. if (list != NULL &&
  591. (sz == list->chunklen || list->chunklen == 0) &&
  592. list->len < ctx->freelist_max_len &&
  593. sz >= sizeof(*ent))
  594. {
  595. list->chunklen = sz;
  596. ent = mem;
  597. ent->next = list->head;
  598. list->head = ent;
  599. ++list->len;
  600. mem = NULL;
  601. }
  602. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  603. if (mem)
  604. OPENSSL_free(mem);
  605. }
  606. #else
  607. #define freelist_extract(c,fr,sz) OPENSSL_malloc(sz)
  608. #define freelist_insert(c,fr,sz,m) OPENSSL_free(m)
  609. #endif
  610. int ssl3_setup_read_buffer(SSL *s)
  611. {
  612. unsigned char *p;
  613. size_t len,align=0,headerlen;
  614. if (SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER)
  615. headerlen = DTLS1_RT_HEADER_LENGTH;
  616. else
  617. headerlen = SSL3_RT_HEADER_LENGTH;
  618. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
  619. align = (-SSL3_RT_HEADER_LENGTH)&(SSL3_ALIGN_PAYLOAD-1);
  620. #endif
  621. if (s->s3->rbuf.buf == NULL)
  622. {
  623. len = SSL3_RT_MAX_PLAIN_LENGTH
  624. + SSL3_RT_MAX_ENCRYPTED_OVERHEAD
  625. + headerlen + align;
  626. if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER)
  627. {
  628. s->s3->init_extra = 1;
  629. len += SSL3_RT_MAX_EXTRA;
  630. }
  631. if ((p=freelist_extract(s->ctx, 1, len)) == NULL)
  632. goto err;
  633. s->s3->rbuf.buf = p;
  634. s->s3->rbuf.len = len;
  635. }
  636. s->packet= &(s->s3->rbuf.buf[0]);
  637. return 1;
  638. err:
  639. OPENSSL_PUT_ERROR(SSL, ssl3_setup_read_buffer, ERR_R_MALLOC_FAILURE);
  640. return 0;
  641. }
  642. int ssl3_setup_write_buffer(SSL *s)
  643. {
  644. unsigned char *p;
  645. size_t len,align=0,headerlen;
  646. if (SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER)
  647. headerlen = DTLS1_RT_HEADER_LENGTH + 1;
  648. else
  649. headerlen = SSL3_RT_HEADER_LENGTH;
  650. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
  651. align = (-SSL3_RT_HEADER_LENGTH)&(SSL3_ALIGN_PAYLOAD-1);
  652. #endif
  653. if (s->s3->wbuf.buf == NULL)
  654. {
  655. len = s->max_send_fragment
  656. + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD
  657. + headerlen + align;
  658. if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
  659. len += headerlen + align
  660. + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
  661. if ((p=freelist_extract(s->ctx, 0, len)) == NULL)
  662. goto err;
  663. s->s3->wbuf.buf = p;
  664. s->s3->wbuf.len = len;
  665. }
  666. return 1;
  667. err:
  668. OPENSSL_PUT_ERROR(SSL, ssl3_setup_write_buffer, ERR_R_MALLOC_FAILURE);
  669. return 0;
  670. }
  671. int ssl3_setup_buffers(SSL *s)
  672. {
  673. if (!ssl3_setup_read_buffer(s))
  674. return 0;
  675. if (!ssl3_setup_write_buffer(s))
  676. return 0;
  677. return 1;
  678. }
  679. int ssl3_release_write_buffer(SSL *s)
  680. {
  681. if (s->s3->wbuf.buf != NULL)
  682. {
  683. freelist_insert(s->ctx, 0, s->s3->wbuf.len, s->s3->wbuf.buf);
  684. s->s3->wbuf.buf = NULL;
  685. }
  686. return 1;
  687. }
  688. int ssl3_release_read_buffer(SSL *s)
  689. {
  690. if (s->s3->rbuf.buf != NULL)
  691. {
  692. freelist_insert(s->ctx, 1, s->s3->rbuf.len, s->s3->rbuf.buf);
  693. s->s3->rbuf.buf = NULL;
  694. }
  695. return 1;
  696. }