ssl_rsa.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /* ssl/ssl_rsa.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  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 ([email protected]).
  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 ([email protected])"
  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 ([email protected])"
  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. if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
  151. RSA_free(rsa);
  152. return 0;
  153. }
  154. ret = ssl_set_pkey(ssl->cert, pkey);
  155. EVP_PKEY_free(pkey);
  156. return (ret);
  157. }
  158. #endif
  159. static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
  160. {
  161. int i;
  162. i = ssl_cert_type(NULL, pkey);
  163. if (i < 0) {
  164. SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  165. return (0);
  166. }
  167. if (c->pkeys[i].x509 != NULL) {
  168. EVP_PKEY *pktmp;
  169. pktmp = X509_get_pubkey(c->pkeys[i].x509);
  170. if (pktmp == NULL) {
  171. SSLerr(SSL_F_SSL_SET_PKEY, ERR_R_MALLOC_FAILURE);
  172. EVP_PKEY_free(pktmp);
  173. return 0;
  174. }
  175. /*
  176. * The return code from EVP_PKEY_copy_parameters is deliberately
  177. * ignored. Some EVP_PKEY types cannot do this.
  178. */
  179. EVP_PKEY_copy_parameters(pktmp, pkey);
  180. EVP_PKEY_free(pktmp);
  181. ERR_clear_error();
  182. #ifndef OPENSSL_NO_RSA
  183. /*
  184. * Don't check the public/private key, this is mostly for smart
  185. * cards.
  186. */
  187. if ((pkey->type == EVP_PKEY_RSA) &&
  188. (RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) ;
  189. else
  190. #endif
  191. if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
  192. X509_free(c->pkeys[i].x509);
  193. c->pkeys[i].x509 = NULL;
  194. return 0;
  195. }
  196. }
  197. if (c->pkeys[i].privatekey != NULL)
  198. EVP_PKEY_free(c->pkeys[i].privatekey);
  199. CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
  200. c->pkeys[i].privatekey = pkey;
  201. c->key = &(c->pkeys[i]);
  202. c->valid = 0;
  203. return (1);
  204. }
  205. #ifndef OPENSSL_NO_RSA
  206. # ifndef OPENSSL_NO_STDIO
  207. int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
  208. {
  209. int j, ret = 0;
  210. BIO *in;
  211. RSA *rsa = NULL;
  212. in = BIO_new(BIO_s_file_internal());
  213. if (in == NULL) {
  214. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
  215. goto end;
  216. }
  217. if (BIO_read_filename(in, file) <= 0) {
  218. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
  219. goto end;
  220. }
  221. if (type == SSL_FILETYPE_ASN1) {
  222. j = ERR_R_ASN1_LIB;
  223. rsa = d2i_RSAPrivateKey_bio(in, NULL);
  224. } else if (type == SSL_FILETYPE_PEM) {
  225. j = ERR_R_PEM_LIB;
  226. rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
  227. ssl->ctx->default_passwd_callback,
  228. ssl->
  229. ctx->default_passwd_callback_userdata);
  230. } else {
  231. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
  232. goto end;
  233. }
  234. if (rsa == NULL) {
  235. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
  236. goto end;
  237. }
  238. ret = SSL_use_RSAPrivateKey(ssl, rsa);
  239. RSA_free(rsa);
  240. end:
  241. if (in != NULL)
  242. BIO_free(in);
  243. return (ret);
  244. }
  245. # endif
  246. int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, unsigned char *d, long len)
  247. {
  248. int ret;
  249. const unsigned char *p;
  250. RSA *rsa;
  251. p = d;
  252. if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
  253. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
  254. return (0);
  255. }
  256. ret = SSL_use_RSAPrivateKey(ssl, rsa);
  257. RSA_free(rsa);
  258. return (ret);
  259. }
  260. #endif /* !OPENSSL_NO_RSA */
  261. int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
  262. {
  263. int ret;
  264. if (pkey == NULL) {
  265. SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
  266. return (0);
  267. }
  268. if (!ssl_cert_inst(&ssl->cert)) {
  269. SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
  270. return (0);
  271. }
  272. ret = ssl_set_pkey(ssl->cert, pkey);
  273. return (ret);
  274. }
  275. #ifndef OPENSSL_NO_STDIO
  276. int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
  277. {
  278. int j, ret = 0;
  279. BIO *in;
  280. EVP_PKEY *pkey = NULL;
  281. in = BIO_new(BIO_s_file_internal());
  282. if (in == NULL) {
  283. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
  284. goto end;
  285. }
  286. if (BIO_read_filename(in, file) <= 0) {
  287. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
  288. goto end;
  289. }
  290. if (type == SSL_FILETYPE_PEM) {
  291. j = ERR_R_PEM_LIB;
  292. pkey = PEM_read_bio_PrivateKey(in, NULL,
  293. ssl->ctx->default_passwd_callback,
  294. ssl->
  295. ctx->default_passwd_callback_userdata);
  296. } else if (type == SSL_FILETYPE_ASN1) {
  297. j = ERR_R_ASN1_LIB;
  298. pkey = d2i_PrivateKey_bio(in, NULL);
  299. } else {
  300. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
  301. goto end;
  302. }
  303. if (pkey == NULL) {
  304. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
  305. goto end;
  306. }
  307. ret = SSL_use_PrivateKey(ssl, pkey);
  308. EVP_PKEY_free(pkey);
  309. end:
  310. if (in != NULL)
  311. BIO_free(in);
  312. return (ret);
  313. }
  314. #endif
  315. int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
  316. long len)
  317. {
  318. int ret;
  319. const unsigned char *p;
  320. EVP_PKEY *pkey;
  321. p = d;
  322. if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
  323. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
  324. return (0);
  325. }
  326. ret = SSL_use_PrivateKey(ssl, pkey);
  327. EVP_PKEY_free(pkey);
  328. return (ret);
  329. }
  330. int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
  331. {
  332. if (x == NULL) {
  333. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
  334. return (0);
  335. }
  336. if (!ssl_cert_inst(&ctx->cert)) {
  337. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  338. return (0);
  339. }
  340. return (ssl_set_cert(ctx->cert, x));
  341. }
  342. static int ssl_set_cert(CERT *c, X509 *x)
  343. {
  344. EVP_PKEY *pkey;
  345. int i;
  346. pkey = X509_get_pubkey(x);
  347. if (pkey == NULL) {
  348. SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
  349. return (0);
  350. }
  351. i = ssl_cert_type(x, pkey);
  352. if (i < 0) {
  353. SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  354. EVP_PKEY_free(pkey);
  355. return (0);
  356. }
  357. if (c->pkeys[i].privatekey != NULL) {
  358. /*
  359. * The return code from EVP_PKEY_copy_parameters is deliberately
  360. * ignored. Some EVP_PKEY types cannot do this.
  361. */
  362. EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
  363. ERR_clear_error();
  364. #ifndef OPENSSL_NO_RSA
  365. /*
  366. * Don't check the public/private key, this is mostly for smart
  367. * cards.
  368. */
  369. if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
  370. (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
  371. RSA_METHOD_FLAG_NO_CHECK)) ;
  372. else
  373. #endif /* OPENSSL_NO_RSA */
  374. if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
  375. /*
  376. * don't fail for a cert/key mismatch, just free current private
  377. * key (when switching to a different cert & key, first this
  378. * function should be used, then ssl_set_pkey
  379. */
  380. EVP_PKEY_free(c->pkeys[i].privatekey);
  381. c->pkeys[i].privatekey = NULL;
  382. /* clear error queue */
  383. ERR_clear_error();
  384. }
  385. }
  386. EVP_PKEY_free(pkey);
  387. if (c->pkeys[i].x509 != NULL)
  388. X509_free(c->pkeys[i].x509);
  389. CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
  390. c->pkeys[i].x509 = x;
  391. c->key = &(c->pkeys[i]);
  392. c->valid = 0;
  393. return (1);
  394. }
  395. #ifndef OPENSSL_NO_STDIO
  396. int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
  397. {
  398. int j;
  399. BIO *in;
  400. int ret = 0;
  401. X509 *x = NULL;
  402. in = BIO_new(BIO_s_file_internal());
  403. if (in == NULL) {
  404. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
  405. goto end;
  406. }
  407. if (BIO_read_filename(in, file) <= 0) {
  408. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
  409. goto end;
  410. }
  411. if (type == SSL_FILETYPE_ASN1) {
  412. j = ERR_R_ASN1_LIB;
  413. x = d2i_X509_bio(in, NULL);
  414. } else if (type == SSL_FILETYPE_PEM) {
  415. j = ERR_R_PEM_LIB;
  416. x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
  417. ctx->default_passwd_callback_userdata);
  418. } else {
  419. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
  420. goto end;
  421. }
  422. if (x == NULL) {
  423. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
  424. goto end;
  425. }
  426. ret = SSL_CTX_use_certificate(ctx, x);
  427. end:
  428. if (x != NULL)
  429. X509_free(x);
  430. if (in != NULL)
  431. BIO_free(in);
  432. return (ret);
  433. }
  434. #endif
  435. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
  436. const unsigned char *d)
  437. {
  438. X509 *x;
  439. int ret;
  440. x = d2i_X509(NULL, &d, (long)len);
  441. if (x == NULL) {
  442. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
  443. return (0);
  444. }
  445. ret = SSL_CTX_use_certificate(ctx, x);
  446. X509_free(x);
  447. return (ret);
  448. }
  449. #ifndef OPENSSL_NO_RSA
  450. int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
  451. {
  452. int ret;
  453. EVP_PKEY *pkey;
  454. if (rsa == NULL) {
  455. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
  456. return (0);
  457. }
  458. if (!ssl_cert_inst(&ctx->cert)) {
  459. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  460. return (0);
  461. }
  462. if ((pkey = EVP_PKEY_new()) == NULL) {
  463. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
  464. return (0);
  465. }
  466. RSA_up_ref(rsa);
  467. if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
  468. RSA_free(rsa);
  469. return 0;
  470. }
  471. ret = ssl_set_pkey(ctx->cert, pkey);
  472. EVP_PKEY_free(pkey);
  473. return (ret);
  474. }
  475. # ifndef OPENSSL_NO_STDIO
  476. int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
  477. {
  478. int j, ret = 0;
  479. BIO *in;
  480. RSA *rsa = NULL;
  481. in = BIO_new(BIO_s_file_internal());
  482. if (in == NULL) {
  483. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
  484. goto end;
  485. }
  486. if (BIO_read_filename(in, file) <= 0) {
  487. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
  488. goto end;
  489. }
  490. if (type == SSL_FILETYPE_ASN1) {
  491. j = ERR_R_ASN1_LIB;
  492. rsa = d2i_RSAPrivateKey_bio(in, NULL);
  493. } else if (type == SSL_FILETYPE_PEM) {
  494. j = ERR_R_PEM_LIB;
  495. rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
  496. ctx->default_passwd_callback,
  497. ctx->default_passwd_callback_userdata);
  498. } else {
  499. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
  500. goto end;
  501. }
  502. if (rsa == NULL) {
  503. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
  504. goto end;
  505. }
  506. ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
  507. RSA_free(rsa);
  508. end:
  509. if (in != NULL)
  510. BIO_free(in);
  511. return (ret);
  512. }
  513. # endif
  514. int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
  515. long len)
  516. {
  517. int ret;
  518. const unsigned char *p;
  519. RSA *rsa;
  520. p = d;
  521. if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
  522. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
  523. return (0);
  524. }
  525. ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
  526. RSA_free(rsa);
  527. return (ret);
  528. }
  529. #endif /* !OPENSSL_NO_RSA */
  530. int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
  531. {
  532. if (pkey == NULL) {
  533. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
  534. return (0);
  535. }
  536. if (!ssl_cert_inst(&ctx->cert)) {
  537. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
  538. return (0);
  539. }
  540. return (ssl_set_pkey(ctx->cert, pkey));
  541. }
  542. #ifndef OPENSSL_NO_STDIO
  543. int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
  544. {
  545. int j, ret = 0;
  546. BIO *in;
  547. EVP_PKEY *pkey = NULL;
  548. in = BIO_new(BIO_s_file_internal());
  549. if (in == NULL) {
  550. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
  551. goto end;
  552. }
  553. if (BIO_read_filename(in, file) <= 0) {
  554. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
  555. goto end;
  556. }
  557. if (type == SSL_FILETYPE_PEM) {
  558. j = ERR_R_PEM_LIB;
  559. pkey = PEM_read_bio_PrivateKey(in, NULL,
  560. ctx->default_passwd_callback,
  561. ctx->default_passwd_callback_userdata);
  562. } else if (type == SSL_FILETYPE_ASN1) {
  563. j = ERR_R_ASN1_LIB;
  564. pkey = d2i_PrivateKey_bio(in, NULL);
  565. } else {
  566. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
  567. goto end;
  568. }
  569. if (pkey == NULL) {
  570. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
  571. goto end;
  572. }
  573. ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  574. EVP_PKEY_free(pkey);
  575. end:
  576. if (in != NULL)
  577. BIO_free(in);
  578. return (ret);
  579. }
  580. #endif
  581. int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
  582. const unsigned char *d, long len)
  583. {
  584. int ret;
  585. const unsigned char *p;
  586. EVP_PKEY *pkey;
  587. p = d;
  588. if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
  589. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
  590. return (0);
  591. }
  592. ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  593. EVP_PKEY_free(pkey);
  594. return (ret);
  595. }
  596. #ifndef OPENSSL_NO_STDIO
  597. /*
  598. * Read a file that contains our certificate in "PEM" format, possibly
  599. * followed by a sequence of CA certificates that should be sent to the peer
  600. * in the Certificate message.
  601. */
  602. int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
  603. {
  604. BIO *in;
  605. int ret = 0;
  606. X509 *x = NULL;
  607. ERR_clear_error(); /* clear error stack for
  608. * SSL_CTX_use_certificate() */
  609. in = BIO_new(BIO_s_file_internal());
  610. if (in == NULL) {
  611. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
  612. goto end;
  613. }
  614. if (BIO_read_filename(in, file) <= 0) {
  615. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
  616. goto end;
  617. }
  618. x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
  619. ctx->default_passwd_callback_userdata);
  620. if (x == NULL) {
  621. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
  622. goto end;
  623. }
  624. ret = SSL_CTX_use_certificate(ctx, x);
  625. if (ERR_peek_error() != 0)
  626. ret = 0; /* Key/certificate mismatch doesn't imply
  627. * ret==0 ... */
  628. if (ret) {
  629. /*
  630. * If we could set up our certificate, now proceed to the CA
  631. * certificates.
  632. */
  633. X509 *ca;
  634. int r;
  635. unsigned long err;
  636. if (ctx->extra_certs != NULL) {
  637. sk_X509_pop_free(ctx->extra_certs, X509_free);
  638. ctx->extra_certs = NULL;
  639. }
  640. while ((ca = PEM_read_bio_X509(in, NULL,
  641. ctx->default_passwd_callback,
  642. ctx->default_passwd_callback_userdata))
  643. != NULL) {
  644. r = SSL_CTX_add_extra_chain_cert(ctx, ca);
  645. if (!r) {
  646. X509_free(ca);
  647. ret = 0;
  648. goto end;
  649. }
  650. /*
  651. * Note that we must not free r if it was successfully added to
  652. * the chain (while we must free the main certificate, since its
  653. * reference count is increased by SSL_CTX_use_certificate).
  654. */
  655. }
  656. /* When the while loop ends, it's usually just EOF. */
  657. err = ERR_peek_last_error();
  658. if (ERR_GET_LIB(err) == ERR_LIB_PEM
  659. && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
  660. ERR_clear_error();
  661. else
  662. ret = 0; /* some real error */
  663. }
  664. end:
  665. if (x != NULL)
  666. X509_free(x);
  667. if (in != NULL)
  668. BIO_free(in);
  669. return (ret);
  670. }
  671. #endif