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.
 
 
 
 
 
 

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