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.
 
 
 
 
 
 

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