ssl_rsa.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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 <openssl/bio.h>
  60. #include <openssl/objects.h>
  61. #include <openssl/evp.h>
  62. #include <openssl/x509.h>
  63. #include <openssl/pem.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. {
  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 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 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. CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_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 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. ERR_clear_error(); /* make sure no error from X509_check_private_key()
  224. * is left if we have chosen to ignore it */
  225. if (c->pkeys[i].privatekey != NULL)
  226. EVP_PKEY_free(c->pkeys[i].privatekey);
  227. CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
  228. c->pkeys[i].privatekey=pkey;
  229. c->key= &(c->pkeys[i]);
  230. c->valid=0;
  231. return(1);
  232. }
  233. #ifndef NO_RSA
  234. #ifndef NO_STDIO
  235. int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
  236. {
  237. int j,ret=0;
  238. BIO *in;
  239. RSA *rsa=NULL;
  240. in=BIO_new(BIO_s_file_internal());
  241. if (in == NULL)
  242. {
  243. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
  244. goto end;
  245. }
  246. if (BIO_read_filename(in,file) <= 0)
  247. {
  248. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
  249. goto end;
  250. }
  251. if (type == SSL_FILETYPE_ASN1)
  252. {
  253. j=ERR_R_ASN1_LIB;
  254. rsa=d2i_RSAPrivateKey_bio(in,NULL);
  255. }
  256. else if (type == SSL_FILETYPE_PEM)
  257. {
  258. j=ERR_R_PEM_LIB;
  259. rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
  260. ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
  261. }
  262. else
  263. {
  264. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
  265. goto end;
  266. }
  267. if (rsa == NULL)
  268. {
  269. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,j);
  270. goto end;
  271. }
  272. ret=SSL_use_RSAPrivateKey(ssl,rsa);
  273. RSA_free(rsa);
  274. end:
  275. if (in != NULL) BIO_free(in);
  276. return(ret);
  277. }
  278. #endif
  279. int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, unsigned char *d, long len)
  280. {
  281. int ret;
  282. unsigned char *p;
  283. RSA *rsa;
  284. p=d;
  285. if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
  286. {
  287. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
  288. return(0);
  289. }
  290. ret=SSL_use_RSAPrivateKey(ssl,rsa);
  291. RSA_free(rsa);
  292. return(ret);
  293. }
  294. #endif /* !NO_RSA */
  295. int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
  296. {
  297. int ret;
  298. if (pkey == NULL)
  299. {
  300. SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
  301. return(0);
  302. }
  303. if (!ssl_cert_inst(&ssl->cert))
  304. {
  305. SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
  306. return(0);
  307. }
  308. ret=ssl_set_pkey(ssl->cert,pkey);
  309. return(ret);
  310. }
  311. #ifndef NO_STDIO
  312. int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
  313. {
  314. int j,ret=0;
  315. BIO *in;
  316. EVP_PKEY *pkey=NULL;
  317. in=BIO_new(BIO_s_file_internal());
  318. if (in == NULL)
  319. {
  320. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
  321. goto end;
  322. }
  323. if (BIO_read_filename(in,file) <= 0)
  324. {
  325. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
  326. goto end;
  327. }
  328. if (type == SSL_FILETYPE_PEM)
  329. {
  330. j=ERR_R_PEM_LIB;
  331. pkey=PEM_read_bio_PrivateKey(in,NULL,
  332. ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
  333. }
  334. else
  335. {
  336. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
  337. goto end;
  338. }
  339. if (pkey == NULL)
  340. {
  341. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,j);
  342. goto end;
  343. }
  344. ret=SSL_use_PrivateKey(ssl,pkey);
  345. EVP_PKEY_free(pkey);
  346. end:
  347. if (in != NULL) BIO_free(in);
  348. return(ret);
  349. }
  350. #endif
  351. int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, unsigned char *d, long len)
  352. {
  353. int ret;
  354. unsigned char *p;
  355. EVP_PKEY *pkey;
  356. p=d;
  357. if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
  358. {
  359. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
  360. return(0);
  361. }
  362. ret=SSL_use_PrivateKey(ssl,pkey);
  363. EVP_PKEY_free(pkey);
  364. return(ret);
  365. }
  366. int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
  367. {
  368. if (x == NULL)
  369. {
  370. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
  371. return(0);
  372. }
  373. if (!ssl_cert_inst(&ctx->cert))
  374. {
  375. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  376. return(0);
  377. }
  378. return(ssl_set_cert(ctx->cert, x));
  379. }
  380. static int ssl_set_cert(CERT *c, X509 *x)
  381. {
  382. EVP_PKEY *pkey;
  383. int i,ok=0,bad=0;
  384. pkey=X509_get_pubkey(x);
  385. if (pkey == NULL)
  386. {
  387. SSLerr(SSL_F_SSL_SET_CERT,SSL_R_X509_LIB);
  388. return(0);
  389. }
  390. i=ssl_cert_type(x,pkey);
  391. if (i < 0)
  392. {
  393. SSLerr(SSL_F_SSL_SET_CERT,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  394. EVP_PKEY_free(pkey);
  395. return(0);
  396. }
  397. if (c->pkeys[i].privatekey != NULL)
  398. {
  399. EVP_PKEY_copy_parameters(pkey,c->pkeys[i].privatekey);
  400. ERR_clear_error();
  401. #ifndef NO_RSA
  402. /* Don't check the public/private key, this is mostly
  403. * for smart cards. */
  404. if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
  405. (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
  406. RSA_METHOD_FLAG_NO_CHECK))
  407. ok=1;
  408. else
  409. #endif
  410. {
  411. if (!X509_check_private_key(x,c->pkeys[i].privatekey))
  412. {
  413. if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
  414. {
  415. i=(i == SSL_PKEY_DH_RSA)?
  416. SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
  417. if (c->pkeys[i].privatekey == NULL)
  418. ok=1;
  419. else
  420. {
  421. if (!X509_check_private_key(x,
  422. c->pkeys[i].privatekey))
  423. bad=1;
  424. else
  425. ok=1;
  426. }
  427. }
  428. else
  429. bad=1;
  430. }
  431. else
  432. ok=1;
  433. } /* NO_RSA */
  434. }
  435. else
  436. ok=1;
  437. EVP_PKEY_free(pkey);
  438. if (bad)
  439. {
  440. EVP_PKEY_free(c->pkeys[i].privatekey);
  441. c->pkeys[i].privatekey=NULL;
  442. }
  443. if (c->pkeys[i].x509 != NULL)
  444. X509_free(c->pkeys[i].x509);
  445. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  446. c->pkeys[i].x509=x;
  447. c->key= &(c->pkeys[i]);
  448. c->valid=0;
  449. return(1);
  450. }
  451. #ifndef NO_STDIO
  452. int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
  453. {
  454. int j;
  455. BIO *in;
  456. int ret=0;
  457. X509 *x=NULL;
  458. in=BIO_new(BIO_s_file_internal());
  459. if (in == NULL)
  460. {
  461. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
  462. goto end;
  463. }
  464. if (BIO_read_filename(in,file) <= 0)
  465. {
  466. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
  467. goto end;
  468. }
  469. if (type == SSL_FILETYPE_ASN1)
  470. {
  471. j=ERR_R_ASN1_LIB;
  472. x=d2i_X509_bio(in,NULL);
  473. }
  474. else if (type == SSL_FILETYPE_PEM)
  475. {
  476. j=ERR_R_PEM_LIB;
  477. x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
  478. }
  479. else
  480. {
  481. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
  482. goto end;
  483. }
  484. if (x == NULL)
  485. {
  486. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,j);
  487. goto end;
  488. }
  489. ret=SSL_CTX_use_certificate(ctx,x);
  490. end:
  491. if (x != NULL) X509_free(x);
  492. if (in != NULL) BIO_free(in);
  493. return(ret);
  494. }
  495. #endif
  496. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, unsigned char *d)
  497. {
  498. X509 *x;
  499. int ret;
  500. x=d2i_X509(NULL,&d,(long)len);
  501. if (x == NULL)
  502. {
  503. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
  504. return(0);
  505. }
  506. ret=SSL_CTX_use_certificate(ctx,x);
  507. X509_free(x);
  508. return(ret);
  509. }
  510. #ifndef NO_RSA
  511. int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
  512. {
  513. int ret;
  514. EVP_PKEY *pkey;
  515. if (rsa == NULL)
  516. {
  517. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
  518. return(0);
  519. }
  520. if (!ssl_cert_inst(&ctx->cert))
  521. {
  522. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
  523. return(0);
  524. }
  525. if ((pkey=EVP_PKEY_new()) == NULL)
  526. {
  527. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
  528. return(0);
  529. }
  530. CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
  531. EVP_PKEY_assign_RSA(pkey,rsa);
  532. ret=ssl_set_pkey(ctx->cert, pkey);
  533. EVP_PKEY_free(pkey);
  534. return(ret);
  535. }
  536. #ifndef NO_STDIO
  537. int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
  538. {
  539. int j,ret=0;
  540. BIO *in;
  541. RSA *rsa=NULL;
  542. in=BIO_new(BIO_s_file_internal());
  543. if (in == NULL)
  544. {
  545. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
  546. goto end;
  547. }
  548. if (BIO_read_filename(in,file) <= 0)
  549. {
  550. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
  551. goto end;
  552. }
  553. if (type == SSL_FILETYPE_ASN1)
  554. {
  555. j=ERR_R_ASN1_LIB;
  556. rsa=d2i_RSAPrivateKey_bio(in,NULL);
  557. }
  558. else if (type == SSL_FILETYPE_PEM)
  559. {
  560. j=ERR_R_PEM_LIB;
  561. rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
  562. ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
  563. }
  564. else
  565. {
  566. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
  567. goto end;
  568. }
  569. if (rsa == NULL)
  570. {
  571. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,j);
  572. goto end;
  573. }
  574. ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
  575. RSA_free(rsa);
  576. end:
  577. if (in != NULL) BIO_free(in);
  578. return(ret);
  579. }
  580. #endif
  581. int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, unsigned char *d, long len)
  582. {
  583. int ret;
  584. unsigned char *p;
  585. RSA *rsa;
  586. p=d;
  587. if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
  588. {
  589. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
  590. return(0);
  591. }
  592. ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
  593. RSA_free(rsa);
  594. return(ret);
  595. }
  596. #endif /* !NO_RSA */
  597. int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
  598. {
  599. if (pkey == NULL)
  600. {
  601. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
  602. return(0);
  603. }
  604. if (!ssl_cert_inst(&ctx->cert))
  605. {
  606. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
  607. return(0);
  608. }
  609. return(ssl_set_pkey(ctx->cert,pkey));
  610. }
  611. #ifndef NO_STDIO
  612. int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
  613. {
  614. int j,ret=0;
  615. BIO *in;
  616. EVP_PKEY *pkey=NULL;
  617. in=BIO_new(BIO_s_file_internal());
  618. if (in == NULL)
  619. {
  620. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
  621. goto end;
  622. }
  623. if (BIO_read_filename(in,file) <= 0)
  624. {
  625. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
  626. goto end;
  627. }
  628. if (type == SSL_FILETYPE_PEM)
  629. {
  630. j=ERR_R_PEM_LIB;
  631. pkey=PEM_read_bio_PrivateKey(in,NULL,
  632. ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
  633. }
  634. else
  635. {
  636. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
  637. goto end;
  638. }
  639. if (pkey == NULL)
  640. {
  641. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,j);
  642. goto end;
  643. }
  644. ret=SSL_CTX_use_PrivateKey(ctx,pkey);
  645. EVP_PKEY_free(pkey);
  646. end:
  647. if (in != NULL) BIO_free(in);
  648. return(ret);
  649. }
  650. #endif
  651. int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, unsigned char *d,
  652. long len)
  653. {
  654. int ret;
  655. unsigned char *p;
  656. EVP_PKEY *pkey;
  657. p=d;
  658. if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
  659. {
  660. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
  661. return(0);
  662. }
  663. ret=SSL_CTX_use_PrivateKey(ctx,pkey);
  664. EVP_PKEY_free(pkey);
  665. return(ret);
  666. }
  667. #ifndef NO_STDIO
  668. /* Read a file that contains our certificate in "PEM" format,
  669. * possibly followed by a sequence of CA certificates that should be
  670. * sent to the peer in the Certificate message.
  671. */
  672. int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
  673. {
  674. BIO *in;
  675. int ret=0;
  676. X509 *x=NULL;
  677. in=BIO_new(BIO_s_file_internal());
  678. if (in == NULL)
  679. {
  680. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_BUF_LIB);
  681. goto end;
  682. }
  683. if (BIO_read_filename(in,file) <= 0)
  684. {
  685. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_SYS_LIB);
  686. goto end;
  687. }
  688. x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
  689. if (x == NULL)
  690. {
  691. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_PEM_LIB);
  692. goto end;
  693. }
  694. ret=SSL_CTX_use_certificate(ctx,x);
  695. if (ERR_peek_error() != 0)
  696. ret = 0; /* Key/certificate mismatch doesn't imply ret==0 ... */
  697. if (ret)
  698. {
  699. /* If we could set up our certificate, now proceed to
  700. * the CA certificates.
  701. */
  702. X509 *ca;
  703. int r;
  704. unsigned long err;
  705. if (ctx->extra_certs != NULL)
  706. {
  707. sk_X509_pop_free(ctx->extra_certs, X509_free);
  708. ctx->extra_certs = NULL;
  709. }
  710. while ((ca = PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata))
  711. != NULL)
  712. {
  713. r = SSL_CTX_add_extra_chain_cert(ctx, ca);
  714. if (!r)
  715. {
  716. X509_free(ca);
  717. ret = 0;
  718. goto end;
  719. }
  720. /* Note that we must not free r if it was successfully
  721. * added to the chain (while we must free the main
  722. * certificate, since its reference count is increased
  723. * by SSL_CTX_use_certificate). */
  724. }
  725. /* When the while loop ends, it's usually just EOF. */
  726. err = ERR_peek_error();
  727. if (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
  728. (void) ERR_get_error();
  729. else
  730. ret = 0; /* some real error */
  731. }
  732. end:
  733. if (x != NULL) X509_free(x);
  734. if (in != NULL) BIO_free(in);
  735. return(ret);
  736. }
  737. #endif