ssl_rsa.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. {
  71. SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
  72. return(0);
  73. }
  74. if (!ssl_cert_inst(&ssl->cert))
  75. {
  76. SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  77. return(0);
  78. }
  79. return(ssl_set_cert(ssl->cert,x));
  80. }
  81. #ifndef OPENSSL_NO_STDIO
  82. int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
  83. {
  84. int j;
  85. BIO *in;
  86. int ret=0;
  87. X509 *x=NULL;
  88. in=BIO_new(BIO_s_file_internal());
  89. if (in == NULL)
  90. {
  91. SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
  92. goto end;
  93. }
  94. if (BIO_read_filename(in,file) <= 0)
  95. {
  96. SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
  97. goto end;
  98. }
  99. if (type == SSL_FILETYPE_ASN1)
  100. {
  101. j=ERR_R_ASN1_LIB;
  102. x=d2i_X509_bio(in,NULL);
  103. }
  104. else if (type == SSL_FILETYPE_PEM)
  105. {
  106. j=ERR_R_PEM_LIB;
  107. x=PEM_read_bio_X509(in,NULL,ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
  108. }
  109. else
  110. {
  111. SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
  112. goto end;
  113. }
  114. if (x == NULL)
  115. {
  116. SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,j);
  117. goto end;
  118. }
  119. ret=SSL_use_certificate(ssl,x);
  120. end:
  121. if (x != NULL) X509_free(x);
  122. if (in != NULL) BIO_free(in);
  123. return(ret);
  124. }
  125. #endif
  126. int SSL_use_certificate_ASN1(SSL *ssl, unsigned char *d, int len)
  127. {
  128. X509 *x;
  129. int ret;
  130. x=d2i_X509(NULL,&d,(long)len);
  131. if (x == NULL)
  132. {
  133. SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
  134. return(0);
  135. }
  136. ret=SSL_use_certificate(ssl,x);
  137. X509_free(x);
  138. return(ret);
  139. }
  140. #ifndef OPENSSL_NO_RSA
  141. int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
  142. {
  143. EVP_PKEY *pkey;
  144. int ret;
  145. if (rsa == NULL)
  146. {
  147. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
  148. return(0);
  149. }
  150. if (!ssl_cert_inst(&ssl->cert))
  151. {
  152. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
  153. return(0);
  154. }
  155. if ((pkey=EVP_PKEY_new()) == NULL)
  156. {
  157. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
  158. return(0);
  159. }
  160. RSA_up_ref(rsa);
  161. EVP_PKEY_assign_RSA(pkey,rsa);
  162. ret=ssl_set_pkey(ssl->cert,pkey);
  163. EVP_PKEY_free(pkey);
  164. return(ret);
  165. }
  166. #endif
  167. static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
  168. {
  169. int i,ok=0,bad=0;
  170. i=ssl_cert_type(NULL,pkey);
  171. if (i < 0)
  172. {
  173. SSLerr(SSL_F_SSL_SET_PKEY,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  174. return(0);
  175. }
  176. if (c->pkeys[i].x509 != NULL)
  177. {
  178. EVP_PKEY *pktmp;
  179. pktmp = X509_get_pubkey(c->pkeys[i].x509);
  180. EVP_PKEY_copy_parameters(pktmp,pkey);
  181. EVP_PKEY_free(pktmp);
  182. ERR_clear_error();
  183. #ifndef OPENSSL_NO_RSA
  184. /* Don't check the public/private key, this is mostly
  185. * for smart cards. */
  186. if ((pkey->type == EVP_PKEY_RSA) &&
  187. (RSA_flags(pkey->pkey.rsa) &
  188. RSA_METHOD_FLAG_NO_CHECK))
  189. ok=1;
  190. else
  191. #endif
  192. if (!X509_check_private_key(c->pkeys[i].x509,pkey))
  193. {
  194. if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
  195. {
  196. i=(i == SSL_PKEY_DH_RSA)?
  197. SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
  198. if (c->pkeys[i].x509 == NULL)
  199. ok=1;
  200. else
  201. {
  202. if (!X509_check_private_key(
  203. c->pkeys[i].x509,pkey))
  204. bad=1;
  205. else
  206. ok=1;
  207. }
  208. }
  209. else
  210. bad=1;
  211. }
  212. else
  213. ok=1;
  214. }
  215. else
  216. ok=1;
  217. if (bad)
  218. {
  219. X509_free(c->pkeys[i].x509);
  220. c->pkeys[i].x509=NULL;
  221. return(0);
  222. }
  223. if (c->pkeys[i].privatekey != NULL)
  224. EVP_PKEY_free(c->pkeys[i].privatekey);
  225. CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
  226. c->pkeys[i].privatekey=pkey;
  227. c->key= &(c->pkeys[i]);
  228. c->valid=0;
  229. return(1);
  230. }
  231. #ifndef OPENSSL_NO_RSA
  232. #ifndef OPENSSL_NO_STDIO
  233. int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
  234. {
  235. int j,ret=0;
  236. BIO *in;
  237. RSA *rsa=NULL;
  238. in=BIO_new(BIO_s_file_internal());
  239. if (in == NULL)
  240. {
  241. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
  242. goto end;
  243. }
  244. if (BIO_read_filename(in,file) <= 0)
  245. {
  246. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
  247. goto end;
  248. }
  249. if (type == SSL_FILETYPE_ASN1)
  250. {
  251. j=ERR_R_ASN1_LIB;
  252. rsa=d2i_RSAPrivateKey_bio(in,NULL);
  253. }
  254. else if (type == SSL_FILETYPE_PEM)
  255. {
  256. j=ERR_R_PEM_LIB;
  257. rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
  258. ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
  259. }
  260. else
  261. {
  262. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
  263. goto end;
  264. }
  265. if (rsa == NULL)
  266. {
  267. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,j);
  268. goto end;
  269. }
  270. ret=SSL_use_RSAPrivateKey(ssl,rsa);
  271. RSA_free(rsa);
  272. end:
  273. if (in != NULL) BIO_free(in);
  274. return(ret);
  275. }
  276. #endif
  277. int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, unsigned char *d, long len)
  278. {
  279. int ret;
  280. const unsigned char *p;
  281. RSA *rsa;
  282. p=d;
  283. if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
  284. {
  285. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
  286. return(0);
  287. }
  288. ret=SSL_use_RSAPrivateKey(ssl,rsa);
  289. RSA_free(rsa);
  290. return(ret);
  291. }
  292. #endif /* !OPENSSL_NO_RSA */
  293. int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
  294. {
  295. int ret;
  296. if (pkey == NULL)
  297. {
  298. SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
  299. return(0);
  300. }
  301. if (!ssl_cert_inst(&ssl->cert))
  302. {
  303. SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
  304. return(0);
  305. }
  306. ret=ssl_set_pkey(ssl->cert,pkey);
  307. return(ret);
  308. }
  309. #ifndef OPENSSL_NO_STDIO
  310. int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
  311. {
  312. int j,ret=0;
  313. BIO *in;
  314. EVP_PKEY *pkey=NULL;
  315. in=BIO_new(BIO_s_file_internal());
  316. if (in == NULL)
  317. {
  318. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
  319. goto end;
  320. }
  321. if (BIO_read_filename(in,file) <= 0)
  322. {
  323. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
  324. goto end;
  325. }
  326. if (type == SSL_FILETYPE_PEM)
  327. {
  328. j=ERR_R_PEM_LIB;
  329. pkey=PEM_read_bio_PrivateKey(in,NULL,
  330. ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
  331. }
  332. else
  333. {
  334. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
  335. goto end;
  336. }
  337. if (pkey == NULL)
  338. {
  339. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,j);
  340. goto end;
  341. }
  342. ret=SSL_use_PrivateKey(ssl,pkey);
  343. EVP_PKEY_free(pkey);
  344. end:
  345. if (in != NULL) BIO_free(in);
  346. return(ret);
  347. }
  348. #endif
  349. int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, unsigned char *d, long len)
  350. {
  351. int ret;
  352. unsigned char *p;
  353. EVP_PKEY *pkey;
  354. p=d;
  355. if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
  356. {
  357. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
  358. return(0);
  359. }
  360. ret=SSL_use_PrivateKey(ssl,pkey);
  361. EVP_PKEY_free(pkey);
  362. return(ret);
  363. }
  364. int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
  365. {
  366. if (x == NULL)
  367. {
  368. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
  369. return(0);
  370. }
  371. if (!ssl_cert_inst(&ctx->cert))
  372. {
  373. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  374. return(0);
  375. }
  376. return(ssl_set_cert(ctx->cert, x));
  377. }
  378. static int ssl_set_cert(CERT *c, X509 *x)
  379. {
  380. EVP_PKEY *pkey;
  381. int i,ok=0,bad=0;
  382. pkey=X509_get_pubkey(x);
  383. if (pkey == NULL)
  384. {
  385. SSLerr(SSL_F_SSL_SET_CERT,SSL_R_X509_LIB);
  386. return(0);
  387. }
  388. i=ssl_cert_type(x,pkey);
  389. if (i < 0)
  390. {
  391. SSLerr(SSL_F_SSL_SET_CERT,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  392. EVP_PKEY_free(pkey);
  393. return(0);
  394. }
  395. if (c->pkeys[i].privatekey != NULL)
  396. {
  397. EVP_PKEY_copy_parameters(pkey,c->pkeys[i].privatekey);
  398. ERR_clear_error();
  399. #ifndef OPENSSL_NO_RSA
  400. /* Don't check the public/private key, this is mostly
  401. * for smart cards. */
  402. if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
  403. (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
  404. RSA_METHOD_FLAG_NO_CHECK))
  405. ok=1;
  406. else
  407. #endif
  408. {
  409. if (!X509_check_private_key(x,c->pkeys[i].privatekey))
  410. {
  411. if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
  412. {
  413. i=(i == SSL_PKEY_DH_RSA)?
  414. SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
  415. if (c->pkeys[i].privatekey == NULL)
  416. ok=1;
  417. else
  418. {
  419. if (!X509_check_private_key(x,
  420. c->pkeys[i].privatekey))
  421. bad=1;
  422. else
  423. ok=1;
  424. }
  425. }
  426. else
  427. bad=1;
  428. }
  429. else
  430. ok=1;
  431. } /* OPENSSL_NO_RSA */
  432. }
  433. else
  434. ok=1;
  435. EVP_PKEY_free(pkey);
  436. if (bad)
  437. {
  438. EVP_PKEY_free(c->pkeys[i].privatekey);
  439. c->pkeys[i].privatekey=NULL;
  440. }
  441. if (c->pkeys[i].x509 != NULL)
  442. X509_free(c->pkeys[i].x509);
  443. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  444. c->pkeys[i].x509=x;
  445. c->key= &(c->pkeys[i]);
  446. c->valid=0;
  447. return(1);
  448. }
  449. #ifndef OPENSSL_NO_STDIO
  450. int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
  451. {
  452. int j;
  453. BIO *in;
  454. int ret=0;
  455. X509 *x=NULL;
  456. in=BIO_new(BIO_s_file_internal());
  457. if (in == NULL)
  458. {
  459. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
  460. goto end;
  461. }
  462. if (BIO_read_filename(in,file) <= 0)
  463. {
  464. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
  465. goto end;
  466. }
  467. if (type == SSL_FILETYPE_ASN1)
  468. {
  469. j=ERR_R_ASN1_LIB;
  470. x=d2i_X509_bio(in,NULL);
  471. }
  472. else if (type == SSL_FILETYPE_PEM)
  473. {
  474. j=ERR_R_PEM_LIB;
  475. x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
  476. }
  477. else
  478. {
  479. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
  480. goto end;
  481. }
  482. if (x == NULL)
  483. {
  484. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,j);
  485. goto end;
  486. }
  487. ret=SSL_CTX_use_certificate(ctx,x);
  488. end:
  489. if (x != NULL) X509_free(x);
  490. if (in != NULL) BIO_free(in);
  491. return(ret);
  492. }
  493. #endif
  494. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, unsigned char *d)
  495. {
  496. X509 *x;
  497. int ret;
  498. x=d2i_X509(NULL,&d,(long)len);
  499. if (x == NULL)
  500. {
  501. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
  502. return(0);
  503. }
  504. ret=SSL_CTX_use_certificate(ctx,x);
  505. X509_free(x);
  506. return(ret);
  507. }
  508. #ifndef OPENSSL_NO_RSA
  509. int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
  510. {
  511. int ret;
  512. EVP_PKEY *pkey;
  513. if (rsa == NULL)
  514. {
  515. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
  516. return(0);
  517. }
  518. if (!ssl_cert_inst(&ctx->cert))
  519. {
  520. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
  521. return(0);
  522. }
  523. if ((pkey=EVP_PKEY_new()) == NULL)
  524. {
  525. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
  526. return(0);
  527. }
  528. RSA_up_ref(rsa);
  529. EVP_PKEY_assign_RSA(pkey,rsa);
  530. ret=ssl_set_pkey(ctx->cert, pkey);
  531. EVP_PKEY_free(pkey);
  532. return(ret);
  533. }
  534. #ifndef OPENSSL_NO_STDIO
  535. int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
  536. {
  537. int j,ret=0;
  538. BIO *in;
  539. RSA *rsa=NULL;
  540. in=BIO_new(BIO_s_file_internal());
  541. if (in == NULL)
  542. {
  543. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
  544. goto end;
  545. }
  546. if (BIO_read_filename(in,file) <= 0)
  547. {
  548. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
  549. goto end;
  550. }
  551. if (type == SSL_FILETYPE_ASN1)
  552. {
  553. j=ERR_R_ASN1_LIB;
  554. rsa=d2i_RSAPrivateKey_bio(in,NULL);
  555. }
  556. else if (type == SSL_FILETYPE_PEM)
  557. {
  558. j=ERR_R_PEM_LIB;
  559. rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
  560. ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
  561. }
  562. else
  563. {
  564. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
  565. goto end;
  566. }
  567. if (rsa == NULL)
  568. {
  569. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,j);
  570. goto end;
  571. }
  572. ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
  573. RSA_free(rsa);
  574. end:
  575. if (in != NULL) BIO_free(in);
  576. return(ret);
  577. }
  578. #endif
  579. int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, unsigned char *d, long len)
  580. {
  581. int ret;
  582. const unsigned char *p;
  583. RSA *rsa;
  584. p=d;
  585. if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
  586. {
  587. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
  588. return(0);
  589. }
  590. ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
  591. RSA_free(rsa);
  592. return(ret);
  593. }
  594. #endif /* !OPENSSL_NO_RSA */
  595. int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
  596. {
  597. if (pkey == NULL)
  598. {
  599. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
  600. return(0);
  601. }
  602. if (!ssl_cert_inst(&ctx->cert))
  603. {
  604. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
  605. return(0);
  606. }
  607. return(ssl_set_pkey(ctx->cert,pkey));
  608. }
  609. #ifndef OPENSSL_NO_STDIO
  610. int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
  611. {
  612. int j,ret=0;
  613. BIO *in;
  614. EVP_PKEY *pkey=NULL;
  615. in=BIO_new(BIO_s_file_internal());
  616. if (in == NULL)
  617. {
  618. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
  619. goto end;
  620. }
  621. if (BIO_read_filename(in,file) <= 0)
  622. {
  623. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
  624. goto end;
  625. }
  626. if (type == SSL_FILETYPE_PEM)
  627. {
  628. j=ERR_R_PEM_LIB;
  629. pkey=PEM_read_bio_PrivateKey(in,NULL,
  630. ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
  631. }
  632. else
  633. {
  634. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
  635. goto end;
  636. }
  637. if (pkey == NULL)
  638. {
  639. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,j);
  640. goto end;
  641. }
  642. ret=SSL_CTX_use_PrivateKey(ctx,pkey);
  643. EVP_PKEY_free(pkey);
  644. end:
  645. if (in != NULL) BIO_free(in);
  646. return(ret);
  647. }
  648. #endif
  649. int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, unsigned char *d,
  650. long len)
  651. {
  652. int ret;
  653. unsigned char *p;
  654. EVP_PKEY *pkey;
  655. p=d;
  656. if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
  657. {
  658. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
  659. return(0);
  660. }
  661. ret=SSL_CTX_use_PrivateKey(ctx,pkey);
  662. EVP_PKEY_free(pkey);
  663. return(ret);
  664. }
  665. #ifndef OPENSSL_NO_STDIO
  666. /* Read a file that contains our certificate in "PEM" format,
  667. * possibly followed by a sequence of CA certificates that should be
  668. * sent to the peer in the Certificate message.
  669. */
  670. int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
  671. {
  672. BIO *in;
  673. int ret=0;
  674. X509 *x=NULL;
  675. in=BIO_new(BIO_s_file_internal());
  676. if (in == NULL)
  677. {
  678. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_BUF_LIB);
  679. goto end;
  680. }
  681. if (BIO_read_filename(in,file) <= 0)
  682. {
  683. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_SYS_LIB);
  684. goto end;
  685. }
  686. x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
  687. if (x == NULL)
  688. {
  689. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_PEM_LIB);
  690. goto end;
  691. }
  692. ret=SSL_CTX_use_certificate(ctx,x);
  693. if (ERR_peek_error() != 0)
  694. ret = 0; /* Key/certificate mismatch doesn't imply ret==0 ... */
  695. if (ret)
  696. {
  697. /* If we could set up our certificate, now proceed to
  698. * the CA certificates.
  699. */
  700. X509 *ca;
  701. int r;
  702. unsigned long err;
  703. if (ctx->extra_certs != NULL)
  704. {
  705. sk_X509_pop_free(ctx->extra_certs, X509_free);
  706. ctx->extra_certs = NULL;
  707. }
  708. while ((ca = PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata))
  709. != NULL)
  710. {
  711. r = SSL_CTX_add_extra_chain_cert(ctx, ca);
  712. if (!r)
  713. {
  714. X509_free(ca);
  715. ret = 0;
  716. goto end;
  717. }
  718. /* Note that we must not free r if it was successfully
  719. * added to the chain (while we must free the main
  720. * certificate, since its reference count is increased
  721. * by SSL_CTX_use_certificate). */
  722. }
  723. /* When the while loop ends, it's usually just EOF. */
  724. err = ERR_peek_last_error();
  725. if (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
  726. (void)ERR_get_error();
  727. else
  728. ret = 0; /* some real error */
  729. }
  730. end:
  731. if (x != NULL) X509_free(x);
  732. if (in != NULL) BIO_free(in);
  733. return(ret);
  734. }
  735. #endif