25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ssl_file.cc 16 KiB

Switch OPENSSL_VERSION_NUMBER to 1.1.0. Although we are derived from 1.0.2, we mimic 1.1.0 in some ways around our FOO_up_ref functions and opaque libssl types. This causes some difficulties when porting third-party code as any OPENSSL_VERSION_NUMBER checks for 1.1.0 APIs we have will be wrong. Moreover, adding accessors without changing OPENSSL_VERSION_NUMBER can break external projects. It is common to implement a compatibility version of an accessor under #ifdef as a static function. This then conflicts with our headers if we, unlike OpenSSL 1.0.2, have this function. This change switches OPENSSL_VERSION_NUMBER to 1.1.0 and atomically adds enough accessors for software with 1.1.0 support already. The hope is this will unblock hiding SSL_CTX and SSL_SESSION, which will be especially useful with C++-ficiation. The cost is we will hit some growing pains as more 1.1.0 consumers enter the ecosystem and we converge on the right set of APIs to import from upstream. It does not remove any 1.0.2 APIs, so we will not require that all projects support 1.1.0. The exception is APIs which changed in 1.1.0 but did not change the function signature. Those are breaking changes. Specifically: - SSL_CTX_sess_set_get_cb is now const-correct. - X509_get0_signature is now const-correct. For C++ consumers only, this change temporarily includes an overload hack for SSL_CTX_sess_set_get_cb that keeps the old callback working. This is a workaround for Node not yet supporting OpenSSL 1.1.0. The version number is set at (the as yet unreleased) 1.1.0g to denote that this change includes https://github.com/openssl/openssl/pull/4384. Bug: 91 Change-Id: I5eeb27448a6db4c25c244afac37f9604d9608a76 Reviewed-on: https://boringssl-review.googlesource.com/10340 Commit-Queue: David Benjamin <davidben@google.com> CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org> Reviewed-by: Adam Langley <agl@google.com>
8 년 전
Switch OPENSSL_VERSION_NUMBER to 1.1.0. Although we are derived from 1.0.2, we mimic 1.1.0 in some ways around our FOO_up_ref functions and opaque libssl types. This causes some difficulties when porting third-party code as any OPENSSL_VERSION_NUMBER checks for 1.1.0 APIs we have will be wrong. Moreover, adding accessors without changing OPENSSL_VERSION_NUMBER can break external projects. It is common to implement a compatibility version of an accessor under #ifdef as a static function. This then conflicts with our headers if we, unlike OpenSSL 1.0.2, have this function. This change switches OPENSSL_VERSION_NUMBER to 1.1.0 and atomically adds enough accessors for software with 1.1.0 support already. The hope is this will unblock hiding SSL_CTX and SSL_SESSION, which will be especially useful with C++-ficiation. The cost is we will hit some growing pains as more 1.1.0 consumers enter the ecosystem and we converge on the right set of APIs to import from upstream. It does not remove any 1.0.2 APIs, so we will not require that all projects support 1.1.0. The exception is APIs which changed in 1.1.0 but did not change the function signature. Those are breaking changes. Specifically: - SSL_CTX_sess_set_get_cb is now const-correct. - X509_get0_signature is now const-correct. For C++ consumers only, this change temporarily includes an overload hack for SSL_CTX_sess_set_get_cb that keeps the old callback working. This is a workaround for Node not yet supporting OpenSSL 1.1.0. The version number is set at (the as yet unreleased) 1.1.0g to denote that this change includes https://github.com/openssl/openssl/pull/4384. Bug: 91 Change-Id: I5eeb27448a6db4c25c244afac37f9604d9608a76 Reviewed-on: https://boringssl-review.googlesource.com/10340 Commit-Queue: David Benjamin <davidben@google.com> CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org> Reviewed-by: Adam Langley <agl@google.com>
8 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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-2007 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. */
  110. #include <openssl/ssl.h>
  111. #include <errno.h>
  112. #include <string.h>
  113. #include <openssl/asn1.h>
  114. #include <openssl/bio.h>
  115. #include <openssl/err.h>
  116. #include <openssl/mem.h>
  117. #include <openssl/pem.h>
  118. #include <openssl/stack.h>
  119. #include <openssl/x509.h>
  120. #include "internal.h"
  121. static int xname_cmp(const X509_NAME **a, const X509_NAME **b) {
  122. return X509_NAME_cmp(*a, *b);
  123. }
  124. // TODO(davidben): Is there any reason this doesn't call
  125. // |SSL_add_file_cert_subjects_to_stack|?
  126. STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file) {
  127. BIO *in;
  128. X509 *x = NULL;
  129. X509_NAME *xn = NULL;
  130. STACK_OF(X509_NAME) *ret = NULL, *sk;
  131. sk = sk_X509_NAME_new(xname_cmp);
  132. in = BIO_new(BIO_s_file());
  133. if (sk == NULL || in == NULL) {
  134. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  135. goto err;
  136. }
  137. if (!BIO_read_filename(in, file)) {
  138. goto err;
  139. }
  140. for (;;) {
  141. if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
  142. break;
  143. }
  144. if (ret == NULL) {
  145. ret = sk_X509_NAME_new_null();
  146. if (ret == NULL) {
  147. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  148. goto err;
  149. }
  150. }
  151. xn = X509_get_subject_name(x);
  152. if (xn == NULL) {
  153. goto err;
  154. }
  155. // Check for duplicates.
  156. sk_X509_NAME_sort(sk);
  157. if (sk_X509_NAME_find(sk, NULL, xn)) {
  158. continue;
  159. }
  160. xn = X509_NAME_dup(xn);
  161. if (xn == NULL ||
  162. !sk_X509_NAME_push(sk /* non-owning */, xn) ||
  163. !sk_X509_NAME_push(ret /* owning */, xn)) {
  164. X509_NAME_free(xn);
  165. goto err;
  166. }
  167. }
  168. if (0) {
  169. err:
  170. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  171. ret = NULL;
  172. }
  173. sk_X509_NAME_free(sk);
  174. BIO_free(in);
  175. X509_free(x);
  176. if (ret != NULL) {
  177. ERR_clear_error();
  178. }
  179. return ret;
  180. }
  181. int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
  182. const char *file) {
  183. BIO *in;
  184. X509 *x = NULL;
  185. X509_NAME *xn = NULL;
  186. int ret = 0;
  187. int (*oldcmp)(const X509_NAME **a, const X509_NAME **b);
  188. oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp);
  189. in = BIO_new(BIO_s_file());
  190. if (in == NULL) {
  191. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  192. goto err;
  193. }
  194. if (!BIO_read_filename(in, file)) {
  195. goto err;
  196. }
  197. for (;;) {
  198. if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
  199. break;
  200. }
  201. xn = X509_get_subject_name(x);
  202. if (xn == NULL) {
  203. goto err;
  204. }
  205. // Check for duplicates.
  206. sk_X509_NAME_sort(stack);
  207. if (sk_X509_NAME_find(stack, NULL, xn)) {
  208. continue;
  209. }
  210. xn = X509_NAME_dup(xn);
  211. if (xn == NULL ||
  212. !sk_X509_NAME_push(stack, xn)) {
  213. X509_NAME_free(xn);
  214. goto err;
  215. }
  216. }
  217. ERR_clear_error();
  218. ret = 1;
  219. err:
  220. BIO_free(in);
  221. X509_free(x);
  222. (void) sk_X509_NAME_set_cmp_func(stack, oldcmp);
  223. return ret;
  224. }
  225. int SSL_use_certificate_file(SSL *ssl, const char *file, int type) {
  226. int reason_code;
  227. BIO *in;
  228. int ret = 0;
  229. X509 *x = NULL;
  230. in = BIO_new(BIO_s_file());
  231. if (in == NULL) {
  232. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  233. goto end;
  234. }
  235. if (BIO_read_filename(in, file) <= 0) {
  236. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  237. goto end;
  238. }
  239. if (type == SSL_FILETYPE_ASN1) {
  240. reason_code = ERR_R_ASN1_LIB;
  241. x = d2i_X509_bio(in, NULL);
  242. } else if (type == SSL_FILETYPE_PEM) {
  243. reason_code = ERR_R_PEM_LIB;
  244. x = PEM_read_bio_X509(in, NULL, ssl->ctx->default_passwd_callback,
  245. ssl->ctx->default_passwd_callback_userdata);
  246. } else {
  247. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE);
  248. goto end;
  249. }
  250. if (x == NULL) {
  251. OPENSSL_PUT_ERROR(SSL, reason_code);
  252. goto end;
  253. }
  254. ret = SSL_use_certificate(ssl, x);
  255. end:
  256. X509_free(x);
  257. BIO_free(in);
  258. return ret;
  259. }
  260. int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type) {
  261. int reason_code, ret = 0;
  262. BIO *in;
  263. RSA *rsa = NULL;
  264. in = BIO_new(BIO_s_file());
  265. if (in == NULL) {
  266. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  267. goto end;
  268. }
  269. if (BIO_read_filename(in, file) <= 0) {
  270. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  271. goto end;
  272. }
  273. if (type == SSL_FILETYPE_ASN1) {
  274. reason_code = ERR_R_ASN1_LIB;
  275. rsa = d2i_RSAPrivateKey_bio(in, NULL);
  276. } else if (type == SSL_FILETYPE_PEM) {
  277. reason_code = ERR_R_PEM_LIB;
  278. rsa =
  279. PEM_read_bio_RSAPrivateKey(in, NULL, ssl->ctx->default_passwd_callback,
  280. ssl->ctx->default_passwd_callback_userdata);
  281. } else {
  282. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE);
  283. goto end;
  284. }
  285. if (rsa == NULL) {
  286. OPENSSL_PUT_ERROR(SSL, reason_code);
  287. goto end;
  288. }
  289. ret = SSL_use_RSAPrivateKey(ssl, rsa);
  290. RSA_free(rsa);
  291. end:
  292. BIO_free(in);
  293. return ret;
  294. }
  295. int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type) {
  296. int reason_code, ret = 0;
  297. BIO *in;
  298. EVP_PKEY *pkey = NULL;
  299. in = BIO_new(BIO_s_file());
  300. if (in == NULL) {
  301. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  302. goto end;
  303. }
  304. if (BIO_read_filename(in, file) <= 0) {
  305. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  306. goto end;
  307. }
  308. if (type == SSL_FILETYPE_PEM) {
  309. reason_code = ERR_R_PEM_LIB;
  310. pkey = PEM_read_bio_PrivateKey(in, NULL, ssl->ctx->default_passwd_callback,
  311. ssl->ctx->default_passwd_callback_userdata);
  312. } else if (type == SSL_FILETYPE_ASN1) {
  313. reason_code = ERR_R_ASN1_LIB;
  314. pkey = d2i_PrivateKey_bio(in, NULL);
  315. } else {
  316. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE);
  317. goto end;
  318. }
  319. if (pkey == NULL) {
  320. OPENSSL_PUT_ERROR(SSL, reason_code);
  321. goto end;
  322. }
  323. ret = SSL_use_PrivateKey(ssl, pkey);
  324. EVP_PKEY_free(pkey);
  325. end:
  326. BIO_free(in);
  327. return ret;
  328. }
  329. int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type) {
  330. int reason_code;
  331. BIO *in;
  332. int ret = 0;
  333. X509 *x = NULL;
  334. in = BIO_new(BIO_s_file());
  335. if (in == NULL) {
  336. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  337. goto end;
  338. }
  339. if (BIO_read_filename(in, file) <= 0) {
  340. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  341. goto end;
  342. }
  343. if (type == SSL_FILETYPE_ASN1) {
  344. reason_code = ERR_R_ASN1_LIB;
  345. x = d2i_X509_bio(in, NULL);
  346. } else if (type == SSL_FILETYPE_PEM) {
  347. reason_code = ERR_R_PEM_LIB;
  348. x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
  349. ctx->default_passwd_callback_userdata);
  350. } else {
  351. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE);
  352. goto end;
  353. }
  354. if (x == NULL) {
  355. OPENSSL_PUT_ERROR(SSL, reason_code);
  356. goto end;
  357. }
  358. ret = SSL_CTX_use_certificate(ctx, x);
  359. end:
  360. X509_free(x);
  361. BIO_free(in);
  362. return ret;
  363. }
  364. int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type) {
  365. int reason_code, ret = 0;
  366. BIO *in;
  367. RSA *rsa = NULL;
  368. in = BIO_new(BIO_s_file());
  369. if (in == NULL) {
  370. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  371. goto end;
  372. }
  373. if (BIO_read_filename(in, file) <= 0) {
  374. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  375. goto end;
  376. }
  377. if (type == SSL_FILETYPE_ASN1) {
  378. reason_code = ERR_R_ASN1_LIB;
  379. rsa = d2i_RSAPrivateKey_bio(in, NULL);
  380. } else if (type == SSL_FILETYPE_PEM) {
  381. reason_code = ERR_R_PEM_LIB;
  382. rsa = PEM_read_bio_RSAPrivateKey(in, NULL, ctx->default_passwd_callback,
  383. ctx->default_passwd_callback_userdata);
  384. } else {
  385. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE);
  386. goto end;
  387. }
  388. if (rsa == NULL) {
  389. OPENSSL_PUT_ERROR(SSL, reason_code);
  390. goto end;
  391. }
  392. ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
  393. RSA_free(rsa);
  394. end:
  395. BIO_free(in);
  396. return ret;
  397. }
  398. int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type) {
  399. int reason_code, ret = 0;
  400. BIO *in;
  401. EVP_PKEY *pkey = NULL;
  402. in = BIO_new(BIO_s_file());
  403. if (in == NULL) {
  404. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  405. goto end;
  406. }
  407. if (BIO_read_filename(in, file) <= 0) {
  408. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  409. goto end;
  410. }
  411. if (type == SSL_FILETYPE_PEM) {
  412. reason_code = ERR_R_PEM_LIB;
  413. pkey = PEM_read_bio_PrivateKey(in, NULL, ctx->default_passwd_callback,
  414. ctx->default_passwd_callback_userdata);
  415. } else if (type == SSL_FILETYPE_ASN1) {
  416. reason_code = ERR_R_ASN1_LIB;
  417. pkey = d2i_PrivateKey_bio(in, NULL);
  418. } else {
  419. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE);
  420. goto end;
  421. }
  422. if (pkey == NULL) {
  423. OPENSSL_PUT_ERROR(SSL, reason_code);
  424. goto end;
  425. }
  426. ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  427. EVP_PKEY_free(pkey);
  428. end:
  429. BIO_free(in);
  430. return ret;
  431. }
  432. // Read a file that contains our certificate in "PEM" format, possibly followed
  433. // by a sequence of CA certificates that should be sent to the peer in the
  434. // Certificate message.
  435. int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file) {
  436. BIO *in;
  437. int ret = 0;
  438. X509 *x = NULL;
  439. ERR_clear_error(); // clear error stack for SSL_CTX_use_certificate()
  440. in = BIO_new(BIO_s_file());
  441. if (in == NULL) {
  442. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  443. goto end;
  444. }
  445. if (BIO_read_filename(in, file) <= 0) {
  446. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  447. goto end;
  448. }
  449. x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
  450. ctx->default_passwd_callback_userdata);
  451. if (x == NULL) {
  452. OPENSSL_PUT_ERROR(SSL, ERR_R_PEM_LIB);
  453. goto end;
  454. }
  455. ret = SSL_CTX_use_certificate(ctx, x);
  456. if (ERR_peek_error() != 0) {
  457. ret = 0; // Key/certificate mismatch doesn't imply ret==0 ...
  458. }
  459. if (ret) {
  460. // If we could set up our certificate, now proceed to the CA
  461. // certificates.
  462. X509 *ca;
  463. int r;
  464. uint32_t err;
  465. SSL_CTX_clear_chain_certs(ctx);
  466. while ((ca = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
  467. ctx->default_passwd_callback_userdata)) !=
  468. NULL) {
  469. r = SSL_CTX_add0_chain_cert(ctx, ca);
  470. if (!r) {
  471. X509_free(ca);
  472. ret = 0;
  473. goto end;
  474. }
  475. // Note that we must not free r if it was successfully added to the chain
  476. // (while we must free the main certificate, since its reference count is
  477. // increased by SSL_CTX_use_certificate).
  478. }
  479. // When the while loop ends, it's usually just EOF.
  480. err = ERR_peek_last_error();
  481. if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
  482. ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
  483. ERR_clear_error();
  484. } else {
  485. ret = 0; // some real error
  486. }
  487. }
  488. end:
  489. X509_free(x);
  490. BIO_free(in);
  491. return ret;
  492. }
  493. void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
  494. ctx->default_passwd_callback = cb;
  495. }
  496. pem_password_cb *SSL_CTX_get_default_passwd_cb(const SSL_CTX *ctx) {
  497. return ctx->default_passwd_callback;
  498. }
  499. void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *data) {
  500. ctx->default_passwd_callback_userdata = data;
  501. }
  502. void *SSL_CTX_get_default_passwd_cb_userdata(const SSL_CTX *ctx) {
  503. return ctx->default_passwd_callback_userdata;
  504. }