ssl_rsa.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /* ssl/ssl_rsa.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include "ssl_locl.h"
  60. #include <openssl/bio.h>
  61. #include <openssl/objects.h>
  62. #include <openssl/evp.h>
  63. #include <openssl/x509.h>
  64. #include <openssl/pem.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. {
  69. if (x == NULL) {
  70. SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
  71. return (0);
  72. }
  73. if (!ssl_cert_inst(&ssl->cert)) {
  74. SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  75. return (0);
  76. }
  77. return (ssl_set_cert(ssl->cert, x));
  78. }
  79. #ifndef OPENSSL_NO_STDIO
  80. int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
  81. {
  82. int j;
  83. BIO *in;
  84. int ret = 0;
  85. X509 *x = NULL;
  86. in = BIO_new(BIO_s_file_internal());
  87. if (in == NULL) {
  88. SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
  89. goto end;
  90. }
  91. if (BIO_read_filename(in, file) <= 0) {
  92. SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
  93. goto end;
  94. }
  95. if (type == SSL_FILETYPE_ASN1) {
  96. j = ERR_R_ASN1_LIB;
  97. x = d2i_X509_bio(in, NULL);
  98. } else if (type == SSL_FILETYPE_PEM) {
  99. j = ERR_R_PEM_LIB;
  100. x = PEM_read_bio_X509(in, NULL, ssl->ctx->default_passwd_callback,
  101. ssl->ctx->default_passwd_callback_userdata);
  102. } else {
  103. SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
  104. goto end;
  105. }
  106. if (x == NULL) {
  107. SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j);
  108. goto end;
  109. }
  110. ret = SSL_use_certificate(ssl, x);
  111. end:
  112. if (x != NULL)
  113. X509_free(x);
  114. if (in != NULL)
  115. BIO_free(in);
  116. return (ret);
  117. }
  118. #endif
  119. int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
  120. {
  121. X509 *x;
  122. int ret;
  123. x = d2i_X509(NULL, &d, (long)len);
  124. if (x == NULL) {
  125. SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
  126. return (0);
  127. }
  128. ret = SSL_use_certificate(ssl, x);
  129. X509_free(x);
  130. return (ret);
  131. }
  132. #ifndef OPENSSL_NO_RSA
  133. int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
  134. {
  135. EVP_PKEY *pkey;
  136. int ret;
  137. if (rsa == NULL) {
  138. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
  139. return (0);
  140. }
  141. if (!ssl_cert_inst(&ssl->cert)) {
  142. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  143. return (0);
  144. }
  145. if ((pkey = EVP_PKEY_new()) == NULL) {
  146. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
  147. return (0);
  148. }
  149. RSA_up_ref(rsa);
  150. EVP_PKEY_assign_RSA(pkey, rsa);
  151. ret = ssl_set_pkey(ssl->cert, pkey);
  152. EVP_PKEY_free(pkey);
  153. return (ret);
  154. }
  155. #endif
  156. static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
  157. {
  158. int i;
  159. i = ssl_cert_type(NULL, pkey);
  160. if (i < 0) {
  161. SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  162. return (0);
  163. }
  164. if (c->pkeys[i].x509 != NULL) {
  165. EVP_PKEY *pktmp;
  166. pktmp = X509_get_pubkey(c->pkeys[i].x509);
  167. EVP_PKEY_copy_parameters(pktmp, pkey);
  168. EVP_PKEY_free(pktmp);
  169. ERR_clear_error();
  170. #ifndef OPENSSL_NO_RSA
  171. /*
  172. * Don't check the public/private key, this is mostly for smart
  173. * cards.
  174. */
  175. if ((pkey->type == EVP_PKEY_RSA) &&
  176. (RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) ;
  177. else
  178. #endif
  179. if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
  180. X509_free(c->pkeys[i].x509);
  181. c->pkeys[i].x509 = NULL;
  182. return 0;
  183. }
  184. }
  185. if (c->pkeys[i].privatekey != NULL)
  186. EVP_PKEY_free(c->pkeys[i].privatekey);
  187. CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
  188. c->pkeys[i].privatekey = pkey;
  189. c->key = &(c->pkeys[i]);
  190. c->valid = 0;
  191. return (1);
  192. }
  193. #ifndef OPENSSL_NO_RSA
  194. # ifndef OPENSSL_NO_STDIO
  195. int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
  196. {
  197. int j, ret = 0;
  198. BIO *in;
  199. RSA *rsa = NULL;
  200. in = BIO_new(BIO_s_file_internal());
  201. if (in == NULL) {
  202. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
  203. goto end;
  204. }
  205. if (BIO_read_filename(in, file) <= 0) {
  206. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
  207. goto end;
  208. }
  209. if (type == SSL_FILETYPE_ASN1) {
  210. j = ERR_R_ASN1_LIB;
  211. rsa = d2i_RSAPrivateKey_bio(in, NULL);
  212. } else if (type == SSL_FILETYPE_PEM) {
  213. j = ERR_R_PEM_LIB;
  214. rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
  215. ssl->ctx->default_passwd_callback,
  216. ssl->
  217. ctx->default_passwd_callback_userdata);
  218. } else {
  219. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
  220. goto end;
  221. }
  222. if (rsa == NULL) {
  223. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
  224. goto end;
  225. }
  226. ret = SSL_use_RSAPrivateKey(ssl, rsa);
  227. RSA_free(rsa);
  228. end:
  229. if (in != NULL)
  230. BIO_free(in);
  231. return (ret);
  232. }
  233. # endif
  234. int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, unsigned char *d, long len)
  235. {
  236. int ret;
  237. const unsigned char *p;
  238. RSA *rsa;
  239. p = d;
  240. if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
  241. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
  242. return (0);
  243. }
  244. ret = SSL_use_RSAPrivateKey(ssl, rsa);
  245. RSA_free(rsa);
  246. return (ret);
  247. }
  248. #endif /* !OPENSSL_NO_RSA */
  249. int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
  250. {
  251. int ret;
  252. if (pkey == NULL) {
  253. SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
  254. return (0);
  255. }
  256. if (!ssl_cert_inst(&ssl->cert)) {
  257. SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
  258. return (0);
  259. }
  260. ret = ssl_set_pkey(ssl->cert, pkey);
  261. return (ret);
  262. }
  263. #ifndef OPENSSL_NO_STDIO
  264. int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
  265. {
  266. int j, ret = 0;
  267. BIO *in;
  268. EVP_PKEY *pkey = NULL;
  269. in = BIO_new(BIO_s_file_internal());
  270. if (in == NULL) {
  271. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
  272. goto end;
  273. }
  274. if (BIO_read_filename(in, file) <= 0) {
  275. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
  276. goto end;
  277. }
  278. if (type == SSL_FILETYPE_PEM) {
  279. j = ERR_R_PEM_LIB;
  280. pkey = PEM_read_bio_PrivateKey(in, NULL,
  281. ssl->ctx->default_passwd_callback,
  282. ssl->
  283. ctx->default_passwd_callback_userdata);
  284. } else if (type == SSL_FILETYPE_ASN1) {
  285. j = ERR_R_ASN1_LIB;
  286. pkey = d2i_PrivateKey_bio(in, NULL);
  287. } else {
  288. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
  289. goto end;
  290. }
  291. if (pkey == NULL) {
  292. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
  293. goto end;
  294. }
  295. ret = SSL_use_PrivateKey(ssl, pkey);
  296. EVP_PKEY_free(pkey);
  297. end:
  298. if (in != NULL)
  299. BIO_free(in);
  300. return (ret);
  301. }
  302. #endif
  303. int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
  304. long len)
  305. {
  306. int ret;
  307. const unsigned char *p;
  308. EVP_PKEY *pkey;
  309. p = d;
  310. if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
  311. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
  312. return (0);
  313. }
  314. ret = SSL_use_PrivateKey(ssl, pkey);
  315. EVP_PKEY_free(pkey);
  316. return (ret);
  317. }
  318. int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
  319. {
  320. if (x == NULL) {
  321. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
  322. return (0);
  323. }
  324. if (!ssl_cert_inst(&ctx->cert)) {
  325. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  326. return (0);
  327. }
  328. return (ssl_set_cert(ctx->cert, x));
  329. }
  330. static int ssl_set_cert(CERT *c, X509 *x)
  331. {
  332. EVP_PKEY *pkey;
  333. int i;
  334. pkey = X509_get_pubkey(x);
  335. if (pkey == NULL) {
  336. SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
  337. return (0);
  338. }
  339. i = ssl_cert_type(x, pkey);
  340. if (i < 0) {
  341. SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  342. EVP_PKEY_free(pkey);
  343. return (0);
  344. }
  345. if (c->pkeys[i].privatekey != NULL) {
  346. EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
  347. ERR_clear_error();
  348. #ifndef OPENSSL_NO_RSA
  349. /*
  350. * Don't check the public/private key, this is mostly for smart
  351. * cards.
  352. */
  353. if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
  354. (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
  355. RSA_METHOD_FLAG_NO_CHECK)) ;
  356. else
  357. #endif /* OPENSSL_NO_RSA */
  358. if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
  359. /*
  360. * don't fail for a cert/key mismatch, just free current private
  361. * key (when switching to a different cert & key, first this
  362. * function should be used, then ssl_set_pkey
  363. */
  364. EVP_PKEY_free(c->pkeys[i].privatekey);
  365. c->pkeys[i].privatekey = NULL;
  366. /* clear error queue */
  367. ERR_clear_error();
  368. }
  369. }
  370. EVP_PKEY_free(pkey);
  371. if (c->pkeys[i].x509 != NULL)
  372. X509_free(c->pkeys[i].x509);
  373. CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
  374. c->pkeys[i].x509 = x;
  375. c->key = &(c->pkeys[i]);
  376. c->valid = 0;
  377. return (1);
  378. }
  379. #ifndef OPENSSL_NO_STDIO
  380. int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
  381. {
  382. int j;
  383. BIO *in;
  384. int ret = 0;
  385. X509 *x = NULL;
  386. in = BIO_new(BIO_s_file_internal());
  387. if (in == NULL) {
  388. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
  389. goto end;
  390. }
  391. if (BIO_read_filename(in, file) <= 0) {
  392. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
  393. goto end;
  394. }
  395. if (type == SSL_FILETYPE_ASN1) {
  396. j = ERR_R_ASN1_LIB;
  397. x = d2i_X509_bio(in, NULL);
  398. } else if (type == SSL_FILETYPE_PEM) {
  399. j = ERR_R_PEM_LIB;
  400. x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
  401. ctx->default_passwd_callback_userdata);
  402. } else {
  403. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
  404. goto end;
  405. }
  406. if (x == NULL) {
  407. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
  408. goto end;
  409. }
  410. ret = SSL_CTX_use_certificate(ctx, x);
  411. end:
  412. if (x != NULL)
  413. X509_free(x);
  414. if (in != NULL)
  415. BIO_free(in);
  416. return (ret);
  417. }
  418. #endif
  419. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
  420. const unsigned char *d)
  421. {
  422. X509 *x;
  423. int ret;
  424. x = d2i_X509(NULL, &d, (long)len);
  425. if (x == NULL) {
  426. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
  427. return (0);
  428. }
  429. ret = SSL_CTX_use_certificate(ctx, x);
  430. X509_free(x);
  431. return (ret);
  432. }
  433. #ifndef OPENSSL_NO_RSA
  434. int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
  435. {
  436. int ret;
  437. EVP_PKEY *pkey;
  438. if (rsa == NULL) {
  439. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
  440. return (0);
  441. }
  442. if (!ssl_cert_inst(&ctx->cert)) {
  443. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  444. return (0);
  445. }
  446. if ((pkey = EVP_PKEY_new()) == NULL) {
  447. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
  448. return (0);
  449. }
  450. RSA_up_ref(rsa);
  451. EVP_PKEY_assign_RSA(pkey, rsa);
  452. ret = ssl_set_pkey(ctx->cert, pkey);
  453. EVP_PKEY_free(pkey);
  454. return (ret);
  455. }
  456. # ifndef OPENSSL_NO_STDIO
  457. int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
  458. {
  459. int j, ret = 0;
  460. BIO *in;
  461. RSA *rsa = NULL;
  462. in = BIO_new(BIO_s_file_internal());
  463. if (in == NULL) {
  464. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
  465. goto end;
  466. }
  467. if (BIO_read_filename(in, file) <= 0) {
  468. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
  469. goto end;
  470. }
  471. if (type == SSL_FILETYPE_ASN1) {
  472. j = ERR_R_ASN1_LIB;
  473. rsa = d2i_RSAPrivateKey_bio(in, NULL);
  474. } else if (type == SSL_FILETYPE_PEM) {
  475. j = ERR_R_PEM_LIB;
  476. rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
  477. ctx->default_passwd_callback,
  478. ctx->default_passwd_callback_userdata);
  479. } else {
  480. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
  481. goto end;
  482. }
  483. if (rsa == NULL) {
  484. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
  485. goto end;
  486. }
  487. ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
  488. RSA_free(rsa);
  489. end:
  490. if (in != NULL)
  491. BIO_free(in);
  492. return (ret);
  493. }
  494. # endif
  495. int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
  496. long len)
  497. {
  498. int ret;
  499. const unsigned char *p;
  500. RSA *rsa;
  501. p = d;
  502. if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
  503. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
  504. return (0);
  505. }
  506. ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
  507. RSA_free(rsa);
  508. return (ret);
  509. }
  510. #endif /* !OPENSSL_NO_RSA */
  511. int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
  512. {
  513. if (pkey == NULL) {
  514. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
  515. return (0);
  516. }
  517. if (!ssl_cert_inst(&ctx->cert)) {
  518. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
  519. return (0);
  520. }
  521. return (ssl_set_pkey(ctx->cert, pkey));
  522. }
  523. #ifndef OPENSSL_NO_STDIO
  524. int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
  525. {
  526. int j, ret = 0;
  527. BIO *in;
  528. EVP_PKEY *pkey = NULL;
  529. in = BIO_new(BIO_s_file_internal());
  530. if (in == NULL) {
  531. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
  532. goto end;
  533. }
  534. if (BIO_read_filename(in, file) <= 0) {
  535. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
  536. goto end;
  537. }
  538. if (type == SSL_FILETYPE_PEM) {
  539. j = ERR_R_PEM_LIB;
  540. pkey = PEM_read_bio_PrivateKey(in, NULL,
  541. ctx->default_passwd_callback,
  542. ctx->default_passwd_callback_userdata);
  543. } else if (type == SSL_FILETYPE_ASN1) {
  544. j = ERR_R_ASN1_LIB;
  545. pkey = d2i_PrivateKey_bio(in, NULL);
  546. } else {
  547. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
  548. goto end;
  549. }
  550. if (pkey == NULL) {
  551. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
  552. goto end;
  553. }
  554. ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  555. EVP_PKEY_free(pkey);
  556. end:
  557. if (in != NULL)
  558. BIO_free(in);
  559. return (ret);
  560. }
  561. #endif
  562. int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
  563. const unsigned char *d, long len)
  564. {
  565. int ret;
  566. const unsigned char *p;
  567. EVP_PKEY *pkey;
  568. p = d;
  569. if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
  570. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
  571. return (0);
  572. }
  573. ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  574. EVP_PKEY_free(pkey);
  575. return (ret);
  576. }
  577. #ifndef OPENSSL_NO_STDIO
  578. /*
  579. * Read a file that contains our certificate in "PEM" format, possibly
  580. * followed by a sequence of CA certificates that should be sent to the peer
  581. * in the Certificate message.
  582. */
  583. int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
  584. {
  585. BIO *in;
  586. int ret = 0;
  587. X509 *x = NULL;
  588. ERR_clear_error(); /* clear error stack for
  589. * SSL_CTX_use_certificate() */
  590. in = BIO_new(BIO_s_file_internal());
  591. if (in == NULL) {
  592. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
  593. goto end;
  594. }
  595. if (BIO_read_filename(in, file) <= 0) {
  596. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
  597. goto end;
  598. }
  599. x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
  600. ctx->default_passwd_callback_userdata);
  601. if (x == NULL) {
  602. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
  603. goto end;
  604. }
  605. ret = SSL_CTX_use_certificate(ctx, x);
  606. if (ERR_peek_error() != 0)
  607. ret = 0; /* Key/certificate mismatch doesn't imply
  608. * ret==0 ... */
  609. if (ret) {
  610. /*
  611. * If we could set up our certificate, now proceed to the CA
  612. * certificates.
  613. */
  614. X509 *ca;
  615. int r;
  616. unsigned long err;
  617. if (ctx->extra_certs != NULL) {
  618. sk_X509_pop_free(ctx->extra_certs, X509_free);
  619. ctx->extra_certs = NULL;
  620. }
  621. while ((ca =
  622. PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
  623. ctx->default_passwd_callback_userdata))
  624. != NULL) {
  625. r = SSL_CTX_add_extra_chain_cert(ctx, ca);
  626. if (!r) {
  627. X509_free(ca);
  628. ret = 0;
  629. goto end;
  630. }
  631. /*
  632. * Note that we must not free r if it was successfully added to
  633. * the chain (while we must free the main certificate, since its
  634. * reference count is increased by SSL_CTX_use_certificate).
  635. */
  636. }
  637. /* When the while loop ends, it's usually just EOF. */
  638. err = ERR_peek_last_error();
  639. if (ERR_GET_LIB(err) == ERR_LIB_PEM
  640. && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
  641. ERR_clear_error();
  642. else
  643. ret = 0; /* some real error */
  644. }
  645. end:
  646. if (x != NULL)
  647. X509_free(x);
  648. if (in != NULL)
  649. BIO_free(in);
  650. return (ret);
  651. }
  652. #endif