You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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