Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

647 righe
17 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. #include <stdio.h>
  57. #include <openssl/bio.h>
  58. #include <openssl/err.h>
  59. #include <openssl/evp.h>
  60. #include <openssl/mem.h>
  61. #include <openssl/obj.h>
  62. #include <openssl/pem.h>
  63. #include <openssl/x509.h>
  64. #include "internal.h"
  65. static int ssl_set_cert(CERT *c, X509 *x509);
  66. static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
  67. int SSL_use_certificate(SSL *ssl, X509 *x) {
  68. if (x == NULL) {
  69. OPENSSL_PUT_ERROR(SSL, SSL_use_certificate, ERR_R_PASSED_NULL_PARAMETER);
  70. return 0;
  71. }
  72. return ssl_set_cert(ssl->cert, x);
  73. }
  74. int SSL_use_certificate_file(SSL *ssl, const char *file, int type) {
  75. int reason_code;
  76. BIO *in;
  77. int ret = 0;
  78. X509 *x = NULL;
  79. in = BIO_new(BIO_s_file());
  80. if (in == NULL) {
  81. OPENSSL_PUT_ERROR(SSL, SSL_use_certificate_file, ERR_R_BUF_LIB);
  82. goto end;
  83. }
  84. if (BIO_read_filename(in, file) <= 0) {
  85. OPENSSL_PUT_ERROR(SSL, SSL_use_certificate_file, ERR_R_SYS_LIB);
  86. goto end;
  87. }
  88. if (type == SSL_FILETYPE_ASN1) {
  89. reason_code = ERR_R_ASN1_LIB;
  90. x = d2i_X509_bio(in, NULL);
  91. } else if (type == SSL_FILETYPE_PEM) {
  92. reason_code = ERR_R_PEM_LIB;
  93. x = PEM_read_bio_X509(in, NULL, ssl->ctx->default_passwd_callback,
  94. ssl->ctx->default_passwd_callback_userdata);
  95. } else {
  96. OPENSSL_PUT_ERROR(SSL, SSL_use_certificate_file, SSL_R_BAD_SSL_FILETYPE);
  97. goto end;
  98. }
  99. if (x == NULL) {
  100. OPENSSL_PUT_ERROR(SSL, SSL_use_certificate_file, reason_code);
  101. goto end;
  102. }
  103. ret = SSL_use_certificate(ssl, x);
  104. end:
  105. X509_free(x);
  106. BIO_free(in);
  107. return ret;
  108. }
  109. int SSL_use_certificate_ASN1(SSL *ssl, const uint8_t *d, int len) {
  110. X509 *x;
  111. int ret;
  112. x = d2i_X509(NULL, &d, (long)len);
  113. if (x == NULL) {
  114. OPENSSL_PUT_ERROR(SSL, SSL_use_certificate_ASN1, ERR_R_ASN1_LIB);
  115. return 0;
  116. }
  117. ret = SSL_use_certificate(ssl, x);
  118. X509_free(x);
  119. return ret;
  120. }
  121. int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) {
  122. EVP_PKEY *pkey;
  123. int ret;
  124. if (rsa == NULL) {
  125. OPENSSL_PUT_ERROR(SSL, SSL_use_RSAPrivateKey, ERR_R_PASSED_NULL_PARAMETER);
  126. return 0;
  127. }
  128. pkey = EVP_PKEY_new();
  129. if (pkey == NULL) {
  130. OPENSSL_PUT_ERROR(SSL, SSL_use_RSAPrivateKey, ERR_R_EVP_LIB);
  131. return 0;
  132. }
  133. RSA_up_ref(rsa);
  134. EVP_PKEY_assign_RSA(pkey, rsa);
  135. ret = ssl_set_pkey(ssl->cert, pkey);
  136. EVP_PKEY_free(pkey);
  137. return ret;
  138. }
  139. static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey) {
  140. int i;
  141. i = ssl_cert_type(pkey);
  142. if (i < 0) {
  143. OPENSSL_PUT_ERROR(SSL, ssl_set_pkey, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  144. return 0;
  145. }
  146. if (c->pkeys[i].x509 != NULL) {
  147. /* Sanity-check that the private key and the certificate match, unless the
  148. * key is opaque (in case of, say, a smartcard). */
  149. if (!EVP_PKEY_is_opaque(pkey) &&
  150. !X509_check_private_key(c->pkeys[i].x509, pkey)) {
  151. X509_free(c->pkeys[i].x509);
  152. c->pkeys[i].x509 = NULL;
  153. return 0;
  154. }
  155. }
  156. EVP_PKEY_free(c->pkeys[i].privatekey);
  157. c->pkeys[i].privatekey = EVP_PKEY_up_ref(pkey);
  158. c->key = &(c->pkeys[i]);
  159. return 1;
  160. }
  161. int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type) {
  162. int reason_code, ret = 0;
  163. BIO *in;
  164. RSA *rsa = NULL;
  165. in = BIO_new(BIO_s_file());
  166. if (in == NULL) {
  167. OPENSSL_PUT_ERROR(SSL, SSL_use_RSAPrivateKey_file, ERR_R_BUF_LIB);
  168. goto end;
  169. }
  170. if (BIO_read_filename(in, file) <= 0) {
  171. OPENSSL_PUT_ERROR(SSL, SSL_use_RSAPrivateKey_file, ERR_R_SYS_LIB);
  172. goto end;
  173. }
  174. if (type == SSL_FILETYPE_ASN1) {
  175. reason_code = ERR_R_ASN1_LIB;
  176. rsa = d2i_RSAPrivateKey_bio(in, NULL);
  177. } else if (type == SSL_FILETYPE_PEM) {
  178. reason_code = ERR_R_PEM_LIB;
  179. rsa =
  180. PEM_read_bio_RSAPrivateKey(in, NULL, ssl->ctx->default_passwd_callback,
  181. ssl->ctx->default_passwd_callback_userdata);
  182. } else {
  183. OPENSSL_PUT_ERROR(SSL, SSL_use_RSAPrivateKey_file, SSL_R_BAD_SSL_FILETYPE);
  184. goto end;
  185. }
  186. if (rsa == NULL) {
  187. OPENSSL_PUT_ERROR(SSL, SSL_use_RSAPrivateKey_file, reason_code);
  188. goto end;
  189. }
  190. ret = SSL_use_RSAPrivateKey(ssl, rsa);
  191. RSA_free(rsa);
  192. end:
  193. BIO_free(in);
  194. return ret;
  195. }
  196. int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, uint8_t *d, long len) {
  197. int ret;
  198. const uint8_t *p;
  199. RSA *rsa;
  200. p = d;
  201. rsa = d2i_RSAPrivateKey(NULL, &p, (long)len);
  202. if (rsa == NULL) {
  203. OPENSSL_PUT_ERROR(SSL, SSL_use_RSAPrivateKey_ASN1, ERR_R_ASN1_LIB);
  204. return 0;
  205. }
  206. ret = SSL_use_RSAPrivateKey(ssl, rsa);
  207. RSA_free(rsa);
  208. return ret;
  209. }
  210. int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) {
  211. int ret;
  212. if (pkey == NULL) {
  213. OPENSSL_PUT_ERROR(SSL, SSL_use_PrivateKey, ERR_R_PASSED_NULL_PARAMETER);
  214. return 0;
  215. }
  216. ret = ssl_set_pkey(ssl->cert, pkey);
  217. return ret;
  218. }
  219. int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type) {
  220. int reason_code, ret = 0;
  221. BIO *in;
  222. EVP_PKEY *pkey = NULL;
  223. in = BIO_new(BIO_s_file());
  224. if (in == NULL) {
  225. OPENSSL_PUT_ERROR(SSL, SSL_use_PrivateKey_file, ERR_R_BUF_LIB);
  226. goto end;
  227. }
  228. if (BIO_read_filename(in, file) <= 0) {
  229. OPENSSL_PUT_ERROR(SSL, SSL_use_PrivateKey_file, ERR_R_SYS_LIB);
  230. goto end;
  231. }
  232. if (type == SSL_FILETYPE_PEM) {
  233. reason_code = ERR_R_PEM_LIB;
  234. pkey = PEM_read_bio_PrivateKey(in, NULL, ssl->ctx->default_passwd_callback,
  235. ssl->ctx->default_passwd_callback_userdata);
  236. } else if (type == SSL_FILETYPE_ASN1) {
  237. reason_code = ERR_R_ASN1_LIB;
  238. pkey = d2i_PrivateKey_bio(in, NULL);
  239. } else {
  240. OPENSSL_PUT_ERROR(SSL, SSL_use_PrivateKey_file, SSL_R_BAD_SSL_FILETYPE);
  241. goto end;
  242. }
  243. if (pkey == NULL) {
  244. OPENSSL_PUT_ERROR(SSL, SSL_use_PrivateKey_file, reason_code);
  245. goto end;
  246. }
  247. ret = SSL_use_PrivateKey(ssl, pkey);
  248. EVP_PKEY_free(pkey);
  249. end:
  250. BIO_free(in);
  251. return ret;
  252. }
  253. int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const uint8_t *d, long len) {
  254. int ret;
  255. const uint8_t *p;
  256. EVP_PKEY *pkey;
  257. p = d;
  258. pkey = d2i_PrivateKey(type, NULL, &p, (long)len);
  259. if (pkey == NULL) {
  260. OPENSSL_PUT_ERROR(SSL, SSL_use_PrivateKey_ASN1, ERR_R_ASN1_LIB);
  261. return 0;
  262. }
  263. ret = SSL_use_PrivateKey(ssl, pkey);
  264. EVP_PKEY_free(pkey);
  265. return ret;
  266. }
  267. int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x) {
  268. if (x == NULL) {
  269. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_certificate,
  270. ERR_R_PASSED_NULL_PARAMETER);
  271. return 0;
  272. }
  273. return ssl_set_cert(ctx->cert, x);
  274. }
  275. static int ssl_set_cert(CERT *c, X509 *x) {
  276. EVP_PKEY *pkey;
  277. int i;
  278. pkey = X509_get_pubkey(x);
  279. if (pkey == NULL) {
  280. OPENSSL_PUT_ERROR(SSL, ssl_set_cert, SSL_R_X509_LIB);
  281. return 0;
  282. }
  283. i = ssl_cert_type(pkey);
  284. if (i < 0) {
  285. OPENSSL_PUT_ERROR(SSL, ssl_set_cert, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  286. EVP_PKEY_free(pkey);
  287. return 0;
  288. }
  289. if (c->pkeys[i].privatekey != NULL) {
  290. /* Sanity-check that the private key and the certificate match, unless the
  291. * key is opaque (in case of, say, a smartcard). */
  292. if (!EVP_PKEY_is_opaque(c->pkeys[i].privatekey) &&
  293. !X509_check_private_key(x, c->pkeys[i].privatekey)) {
  294. /* don't fail for a cert/key mismatch, just free current private key
  295. * (when switching to a different cert & key, first this function should
  296. * be used, then ssl_set_pkey */
  297. EVP_PKEY_free(c->pkeys[i].privatekey);
  298. c->pkeys[i].privatekey = NULL;
  299. /* clear error queue */
  300. ERR_clear_error();
  301. }
  302. }
  303. EVP_PKEY_free(pkey);
  304. X509_free(c->pkeys[i].x509);
  305. c->pkeys[i].x509 = X509_up_ref(x);
  306. c->key = &(c->pkeys[i]);
  307. return 1;
  308. }
  309. int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type) {
  310. int reason_code;
  311. BIO *in;
  312. int ret = 0;
  313. X509 *x = NULL;
  314. in = BIO_new(BIO_s_file());
  315. if (in == NULL) {
  316. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_certificate_file, ERR_R_BUF_LIB);
  317. goto end;
  318. }
  319. if (BIO_read_filename(in, file) <= 0) {
  320. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_certificate_file, ERR_R_SYS_LIB);
  321. goto end;
  322. }
  323. if (type == SSL_FILETYPE_ASN1) {
  324. reason_code = ERR_R_ASN1_LIB;
  325. x = d2i_X509_bio(in, NULL);
  326. } else if (type == SSL_FILETYPE_PEM) {
  327. reason_code = ERR_R_PEM_LIB;
  328. x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
  329. ctx->default_passwd_callback_userdata);
  330. } else {
  331. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_certificate_file,
  332. SSL_R_BAD_SSL_FILETYPE);
  333. goto end;
  334. }
  335. if (x == NULL) {
  336. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_certificate_file, reason_code);
  337. goto end;
  338. }
  339. ret = SSL_CTX_use_certificate(ctx, x);
  340. end:
  341. X509_free(x);
  342. BIO_free(in);
  343. return ret;
  344. }
  345. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const uint8_t *d) {
  346. X509 *x;
  347. int ret;
  348. x = d2i_X509(NULL, &d, (long)len);
  349. if (x == NULL) {
  350. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_certificate_ASN1, ERR_R_ASN1_LIB);
  351. return 0;
  352. }
  353. ret = SSL_CTX_use_certificate(ctx, x);
  354. X509_free(x);
  355. return ret;
  356. }
  357. int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa) {
  358. int ret;
  359. EVP_PKEY *pkey;
  360. if (rsa == NULL) {
  361. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_RSAPrivateKey,
  362. ERR_R_PASSED_NULL_PARAMETER);
  363. return 0;
  364. }
  365. pkey = EVP_PKEY_new();
  366. if (pkey == NULL) {
  367. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_RSAPrivateKey, ERR_R_EVP_LIB);
  368. return 0;
  369. }
  370. RSA_up_ref(rsa);
  371. EVP_PKEY_assign_RSA(pkey, rsa);
  372. ret = ssl_set_pkey(ctx->cert, pkey);
  373. EVP_PKEY_free(pkey);
  374. return ret;
  375. }
  376. int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type) {
  377. int reason_code, ret = 0;
  378. BIO *in;
  379. RSA *rsa = NULL;
  380. in = BIO_new(BIO_s_file());
  381. if (in == NULL) {
  382. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_RSAPrivateKey_file, ERR_R_BUF_LIB);
  383. goto end;
  384. }
  385. if (BIO_read_filename(in, file) <= 0) {
  386. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_RSAPrivateKey_file, ERR_R_SYS_LIB);
  387. goto end;
  388. }
  389. if (type == SSL_FILETYPE_ASN1) {
  390. reason_code = ERR_R_ASN1_LIB;
  391. rsa = d2i_RSAPrivateKey_bio(in, NULL);
  392. } else if (type == SSL_FILETYPE_PEM) {
  393. reason_code = ERR_R_PEM_LIB;
  394. rsa = PEM_read_bio_RSAPrivateKey(in, NULL, ctx->default_passwd_callback,
  395. ctx->default_passwd_callback_userdata);
  396. } else {
  397. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_RSAPrivateKey_file,
  398. SSL_R_BAD_SSL_FILETYPE);
  399. goto end;
  400. }
  401. if (rsa == NULL) {
  402. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_RSAPrivateKey_file, reason_code);
  403. goto end;
  404. }
  405. ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
  406. RSA_free(rsa);
  407. end:
  408. BIO_free(in);
  409. return ret;
  410. }
  411. int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const uint8_t *d, long len) {
  412. int ret;
  413. const uint8_t *p;
  414. RSA *rsa;
  415. p = d;
  416. rsa = d2i_RSAPrivateKey(NULL, &p, (long)len);
  417. if (rsa == NULL) {
  418. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_RSAPrivateKey_ASN1, ERR_R_ASN1_LIB);
  419. return 0;
  420. }
  421. ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
  422. RSA_free(rsa);
  423. return ret;
  424. }
  425. int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) {
  426. if (pkey == NULL) {
  427. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_PrivateKey, ERR_R_PASSED_NULL_PARAMETER);
  428. return 0;
  429. }
  430. return ssl_set_pkey(ctx->cert, pkey);
  431. }
  432. int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type) {
  433. int reason_code, ret = 0;
  434. BIO *in;
  435. EVP_PKEY *pkey = NULL;
  436. in = BIO_new(BIO_s_file());
  437. if (in == NULL) {
  438. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_PrivateKey_file, ERR_R_BUF_LIB);
  439. goto end;
  440. }
  441. if (BIO_read_filename(in, file) <= 0) {
  442. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_PrivateKey_file, ERR_R_SYS_LIB);
  443. goto end;
  444. }
  445. if (type == SSL_FILETYPE_PEM) {
  446. reason_code = ERR_R_PEM_LIB;
  447. pkey = PEM_read_bio_PrivateKey(in, NULL, ctx->default_passwd_callback,
  448. ctx->default_passwd_callback_userdata);
  449. } else if (type == SSL_FILETYPE_ASN1) {
  450. reason_code = ERR_R_ASN1_LIB;
  451. pkey = d2i_PrivateKey_bio(in, NULL);
  452. } else {
  453. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_PrivateKey_file, SSL_R_BAD_SSL_FILETYPE);
  454. goto end;
  455. }
  456. if (pkey == NULL) {
  457. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_PrivateKey_file, reason_code);
  458. goto end;
  459. }
  460. ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  461. EVP_PKEY_free(pkey);
  462. end:
  463. BIO_free(in);
  464. return ret;
  465. }
  466. int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const uint8_t *d,
  467. long len) {
  468. int ret;
  469. const uint8_t *p;
  470. EVP_PKEY *pkey;
  471. p = d;
  472. pkey = d2i_PrivateKey(type, NULL, &p, (long)len);
  473. if (pkey == NULL) {
  474. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_PrivateKey_ASN1, ERR_R_ASN1_LIB);
  475. return 0;
  476. }
  477. ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  478. EVP_PKEY_free(pkey);
  479. return ret;
  480. }
  481. /* Read a file that contains our certificate in "PEM" format, possibly followed
  482. * by a sequence of CA certificates that should be sent to the peer in the
  483. * Certificate message. */
  484. int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file) {
  485. BIO *in;
  486. int ret = 0;
  487. X509 *x = NULL;
  488. ERR_clear_error(); /* clear error stack for SSL_CTX_use_certificate() */
  489. in = BIO_new(BIO_s_file());
  490. if (in == NULL) {
  491. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_certificate_chain_file, ERR_R_BUF_LIB);
  492. goto end;
  493. }
  494. if (BIO_read_filename(in, file) <= 0) {
  495. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_certificate_chain_file, ERR_R_SYS_LIB);
  496. goto end;
  497. }
  498. x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
  499. ctx->default_passwd_callback_userdata);
  500. if (x == NULL) {
  501. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_certificate_chain_file, ERR_R_PEM_LIB);
  502. goto end;
  503. }
  504. ret = SSL_CTX_use_certificate(ctx, x);
  505. if (ERR_peek_error() != 0) {
  506. ret = 0; /* Key/certificate mismatch doesn't imply ret==0 ... */
  507. }
  508. if (ret) {
  509. /* If we could set up our certificate, now proceed to the CA
  510. * certificates. */
  511. X509 *ca;
  512. int r;
  513. uint32_t err;
  514. SSL_CTX_clear_chain_certs(ctx);
  515. while ((ca = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
  516. ctx->default_passwd_callback_userdata)) !=
  517. NULL) {
  518. r = SSL_CTX_add0_chain_cert(ctx, ca);
  519. if (!r) {
  520. X509_free(ca);
  521. ret = 0;
  522. goto end;
  523. }
  524. /* Note that we must not free r if it was successfully added to the chain
  525. * (while we must free the main certificate, since its reference count is
  526. * increased by SSL_CTX_use_certificate). */
  527. }
  528. /* When the while loop ends, it's usually just EOF. */
  529. err = ERR_peek_last_error();
  530. if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
  531. ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
  532. ERR_clear_error();
  533. } else {
  534. ret = 0; /* some real error */
  535. }
  536. }
  537. end:
  538. X509_free(x);
  539. BIO_free(in);
  540. return ret;
  541. }