Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ssl_rsa.c 31 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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. #ifndef OPENSSL_NO_TLSEXT
  68. static int ssl_set_authz(CERT *c, unsigned char *authz,
  69. size_t authz_length);
  70. #endif
  71. int SSL_use_certificate(SSL *ssl, X509 *x)
  72. {
  73. if (x == NULL)
  74. {
  75. OPENSSL_PUT_ERROR(SSL, SSL_use_certificate, ERR_R_PASSED_NULL_PARAMETER);
  76. return(0);
  77. }
  78. if (!ssl_cert_inst(&ssl->cert))
  79. {
  80. OPENSSL_PUT_ERROR(SSL, SSL_use_certificate, ERR_R_MALLOC_FAILURE);
  81. return(0);
  82. }
  83. return(ssl_set_cert(ssl->cert,x));
  84. }
  85. #ifndef OPENSSL_NO_STDIO
  86. int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
  87. {
  88. int reason_code;
  89. BIO *in;
  90. int ret=0;
  91. X509 *x=NULL;
  92. in=BIO_new(BIO_s_file());
  93. if (in == NULL)
  94. {
  95. OPENSSL_PUT_ERROR(SSL, SSL_use_certificate_file, ERR_R_BUF_LIB);
  96. goto end;
  97. }
  98. if (BIO_read_filename(in,file) <= 0)
  99. {
  100. OPENSSL_PUT_ERROR(SSL, SSL_use_certificate_file, ERR_R_SYS_LIB);
  101. goto end;
  102. }
  103. if (type == SSL_FILETYPE_ASN1)
  104. {
  105. reason_code =ERR_R_ASN1_LIB;
  106. x=d2i_X509_bio(in,NULL);
  107. }
  108. else if (type == SSL_FILETYPE_PEM)
  109. {
  110. reason_code=ERR_R_PEM_LIB;
  111. x=PEM_read_bio_X509(in,NULL,ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
  112. }
  113. else
  114. {
  115. OPENSSL_PUT_ERROR(SSL, SSL_use_certificate_file, SSL_R_BAD_SSL_FILETYPE);
  116. goto end;
  117. }
  118. if (x == NULL)
  119. {
  120. OPENSSL_PUT_ERROR(SSL, SSL_use_certificate_file,reason_code);
  121. goto end;
  122. }
  123. ret=SSL_use_certificate(ssl,x);
  124. end:
  125. if (x != NULL) X509_free(x);
  126. if (in != NULL) BIO_free(in);
  127. return(ret);
  128. }
  129. #endif
  130. int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
  131. {
  132. X509 *x;
  133. int ret;
  134. x=d2i_X509(NULL,&d,(long)len);
  135. if (x == NULL)
  136. {
  137. OPENSSL_PUT_ERROR(SSL, SSL_use_certificate_ASN1, ERR_R_ASN1_LIB);
  138. return(0);
  139. }
  140. ret=SSL_use_certificate(ssl,x);
  141. X509_free(x);
  142. return(ret);
  143. }
  144. #ifndef OPENSSL_NO_RSA
  145. int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
  146. {
  147. EVP_PKEY *pkey;
  148. int ret;
  149. if (rsa == NULL)
  150. {
  151. OPENSSL_PUT_ERROR(SSL, SSL_use_RSAPrivateKey, ERR_R_PASSED_NULL_PARAMETER);
  152. return(0);
  153. }
  154. if (!ssl_cert_inst(&ssl->cert))
  155. {
  156. OPENSSL_PUT_ERROR(SSL, SSL_use_RSAPrivateKey, ERR_R_MALLOC_FAILURE);
  157. return(0);
  158. }
  159. if ((pkey=EVP_PKEY_new()) == NULL)
  160. {
  161. OPENSSL_PUT_ERROR(SSL, SSL_use_RSAPrivateKey, ERR_R_EVP_LIB);
  162. return(0);
  163. }
  164. RSA_up_ref(rsa);
  165. EVP_PKEY_assign_RSA(pkey,rsa);
  166. ret=ssl_set_pkey(ssl->cert,pkey);
  167. EVP_PKEY_free(pkey);
  168. return(ret);
  169. }
  170. #endif
  171. static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
  172. {
  173. int i;
  174. /* Special case for DH: check two DH certificate types for a match.
  175. * This means for DH certificates we must set the certificate first.
  176. */
  177. if (pkey->type == EVP_PKEY_DH)
  178. {
  179. X509 *x;
  180. i = -1;
  181. x = c->pkeys[SSL_PKEY_DH_RSA].x509;
  182. if (x && X509_check_private_key(x, pkey))
  183. i = SSL_PKEY_DH_RSA;
  184. x = c->pkeys[SSL_PKEY_DH_DSA].x509;
  185. if (i == -1 && x && X509_check_private_key(x, pkey))
  186. i = SSL_PKEY_DH_DSA;
  187. ERR_clear_error();
  188. }
  189. else
  190. i=ssl_cert_type(NULL,pkey);
  191. if (i < 0)
  192. {
  193. OPENSSL_PUT_ERROR(SSL, ssl_set_pkey, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  194. return(0);
  195. }
  196. if (c->pkeys[i].x509 != NULL)
  197. {
  198. EVP_PKEY *pktmp;
  199. pktmp = X509_get_pubkey(c->pkeys[i].x509);
  200. EVP_PKEY_copy_parameters(pktmp,pkey);
  201. EVP_PKEY_free(pktmp);
  202. ERR_clear_error();
  203. /* TODO(fork): remove this? */
  204. #if 0
  205. #ifndef OPENSSL_NO_RSA
  206. /* Don't check the public/private key, this is mostly
  207. * for smart cards. */
  208. if ((pkey->type == EVP_PKEY_RSA) &&
  209. (RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK))
  210. ;
  211. else
  212. #endif
  213. #endif
  214. if (!X509_check_private_key(c->pkeys[i].x509,pkey))
  215. {
  216. X509_free(c->pkeys[i].x509);
  217. c->pkeys[i].x509 = NULL;
  218. return 0;
  219. }
  220. }
  221. if (c->pkeys[i].privatekey != NULL)
  222. EVP_PKEY_free(c->pkeys[i].privatekey);
  223. CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
  224. c->pkeys[i].privatekey=pkey;
  225. c->key= &(c->pkeys[i]);
  226. c->valid=0;
  227. return(1);
  228. }
  229. #ifndef OPENSSL_NO_RSA
  230. #ifndef OPENSSL_NO_STDIO
  231. int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
  232. {
  233. int reason_code,ret=0;
  234. BIO *in;
  235. RSA *rsa=NULL;
  236. in=BIO_new(BIO_s_file());
  237. if (in == NULL)
  238. {
  239. OPENSSL_PUT_ERROR(SSL, SSL_use_RSAPrivateKey_file, ERR_R_BUF_LIB);
  240. goto end;
  241. }
  242. if (BIO_read_filename(in,file) <= 0)
  243. {
  244. OPENSSL_PUT_ERROR(SSL, SSL_use_RSAPrivateKey_file, ERR_R_SYS_LIB);
  245. goto end;
  246. }
  247. if (type == SSL_FILETYPE_ASN1)
  248. {
  249. reason_code=ERR_R_ASN1_LIB;
  250. rsa=d2i_RSAPrivateKey_bio(in,NULL);
  251. }
  252. else if (type == SSL_FILETYPE_PEM)
  253. {
  254. reason_code=ERR_R_PEM_LIB;
  255. rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
  256. ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
  257. }
  258. else
  259. {
  260. OPENSSL_PUT_ERROR(SSL, SSL_use_RSAPrivateKey_file, SSL_R_BAD_SSL_FILETYPE);
  261. goto end;
  262. }
  263. if (rsa == NULL)
  264. {
  265. OPENSSL_PUT_ERROR(SSL, SSL_use_RSAPrivateKey_file,reason_code);
  266. goto end;
  267. }
  268. ret=SSL_use_RSAPrivateKey(ssl,rsa);
  269. RSA_free(rsa);
  270. end:
  271. if (in != NULL) BIO_free(in);
  272. return(ret);
  273. }
  274. #endif
  275. int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, unsigned char *d, long len)
  276. {
  277. int ret;
  278. const unsigned char *p;
  279. RSA *rsa;
  280. p=d;
  281. if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
  282. {
  283. OPENSSL_PUT_ERROR(SSL, SSL_use_RSAPrivateKey_ASN1, ERR_R_ASN1_LIB);
  284. return(0);
  285. }
  286. ret=SSL_use_RSAPrivateKey(ssl,rsa);
  287. RSA_free(rsa);
  288. return(ret);
  289. }
  290. #endif /* !OPENSSL_NO_RSA */
  291. int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
  292. {
  293. int ret;
  294. if (pkey == NULL)
  295. {
  296. OPENSSL_PUT_ERROR(SSL, SSL_use_PrivateKey, ERR_R_PASSED_NULL_PARAMETER);
  297. return(0);
  298. }
  299. if (!ssl_cert_inst(&ssl->cert))
  300. {
  301. OPENSSL_PUT_ERROR(SSL, SSL_use_PrivateKey, ERR_R_MALLOC_FAILURE);
  302. return(0);
  303. }
  304. ret=ssl_set_pkey(ssl->cert,pkey);
  305. return(ret);
  306. }
  307. #ifndef OPENSSL_NO_STDIO
  308. int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
  309. {
  310. int reason_code,ret=0;
  311. BIO *in;
  312. EVP_PKEY *pkey=NULL;
  313. in=BIO_new(BIO_s_file());
  314. if (in == NULL)
  315. {
  316. OPENSSL_PUT_ERROR(SSL, SSL_use_PrivateKey_file, ERR_R_BUF_LIB);
  317. goto end;
  318. }
  319. if (BIO_read_filename(in,file) <= 0)
  320. {
  321. OPENSSL_PUT_ERROR(SSL, SSL_use_PrivateKey_file, ERR_R_SYS_LIB);
  322. goto end;
  323. }
  324. if (type == SSL_FILETYPE_PEM)
  325. {
  326. reason_code=ERR_R_PEM_LIB;
  327. pkey=PEM_read_bio_PrivateKey(in,NULL,
  328. ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
  329. }
  330. else if (type == SSL_FILETYPE_ASN1)
  331. {
  332. reason_code = ERR_R_ASN1_LIB;
  333. pkey = d2i_PrivateKey_bio(in,NULL);
  334. }
  335. else
  336. {
  337. OPENSSL_PUT_ERROR(SSL, SSL_use_PrivateKey_file, SSL_R_BAD_SSL_FILETYPE);
  338. goto end;
  339. }
  340. if (pkey == NULL)
  341. {
  342. OPENSSL_PUT_ERROR(SSL, SSL_use_PrivateKey_file,reason_code);
  343. goto end;
  344. }
  345. ret=SSL_use_PrivateKey(ssl,pkey);
  346. EVP_PKEY_free(pkey);
  347. end:
  348. if (in != NULL) BIO_free(in);
  349. return(ret);
  350. }
  351. #endif
  352. int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d, long len)
  353. {
  354. int ret;
  355. const unsigned char *p;
  356. EVP_PKEY *pkey;
  357. p=d;
  358. if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
  359. {
  360. OPENSSL_PUT_ERROR(SSL, SSL_use_PrivateKey_ASN1, ERR_R_ASN1_LIB);
  361. return(0);
  362. }
  363. ret=SSL_use_PrivateKey(ssl,pkey);
  364. EVP_PKEY_free(pkey);
  365. return(ret);
  366. }
  367. int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
  368. {
  369. if (x == NULL)
  370. {
  371. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_certificate, ERR_R_PASSED_NULL_PARAMETER);
  372. return(0);
  373. }
  374. if (!ssl_cert_inst(&ctx->cert))
  375. {
  376. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_certificate, ERR_R_MALLOC_FAILURE);
  377. return(0);
  378. }
  379. return(ssl_set_cert(ctx->cert, x));
  380. }
  381. static int ssl_set_cert(CERT *c, X509 *x)
  382. {
  383. EVP_PKEY *pkey;
  384. int i;
  385. pkey=X509_get_pubkey(x);
  386. if (pkey == NULL)
  387. {
  388. OPENSSL_PUT_ERROR(SSL, ssl_set_cert, SSL_R_X509_LIB);
  389. return(0);
  390. }
  391. i=ssl_cert_type(x,pkey);
  392. if (i < 0)
  393. {
  394. OPENSSL_PUT_ERROR(SSL, ssl_set_cert, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  395. EVP_PKEY_free(pkey);
  396. return(0);
  397. }
  398. if (c->pkeys[i].privatekey != NULL)
  399. {
  400. EVP_PKEY_copy_parameters(pkey,c->pkeys[i].privatekey);
  401. ERR_clear_error();
  402. /* TODO(fork): remove this? */
  403. #if 0
  404. #ifndef OPENSSL_NO_RSA
  405. /* Don't check the public/private key, this is mostly
  406. * for smart cards. */
  407. if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
  408. (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
  409. RSA_METHOD_FLAG_NO_CHECK))
  410. ;
  411. else
  412. #endif /* OPENSSL_NO_RSA */
  413. #endif
  414. if (!X509_check_private_key(x,c->pkeys[i].privatekey))
  415. {
  416. /* don't fail for a cert/key mismatch, just free
  417. * current private key (when switching to a different
  418. * cert & key, first this function should be used,
  419. * then ssl_set_pkey */
  420. EVP_PKEY_free(c->pkeys[i].privatekey);
  421. c->pkeys[i].privatekey=NULL;
  422. /* clear error queue */
  423. ERR_clear_error();
  424. }
  425. }
  426. EVP_PKEY_free(pkey);
  427. if (c->pkeys[i].x509 != NULL)
  428. X509_free(c->pkeys[i].x509);
  429. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  430. c->pkeys[i].x509=x;
  431. c->key= &(c->pkeys[i]);
  432. c->valid=0;
  433. return(1);
  434. }
  435. #ifndef OPENSSL_NO_STDIO
  436. int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
  437. {
  438. int reason_code;
  439. BIO *in;
  440. int ret=0;
  441. X509 *x=NULL;
  442. in=BIO_new(BIO_s_file());
  443. if (in == NULL)
  444. {
  445. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_certificate_file, ERR_R_BUF_LIB);
  446. goto end;
  447. }
  448. if (BIO_read_filename(in,file) <= 0)
  449. {
  450. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_certificate_file, ERR_R_SYS_LIB);
  451. goto end;
  452. }
  453. if (type == SSL_FILETYPE_ASN1)
  454. {
  455. reason_code=ERR_R_ASN1_LIB;
  456. x=d2i_X509_bio(in,NULL);
  457. }
  458. else if (type == SSL_FILETYPE_PEM)
  459. {
  460. reason_code=ERR_R_PEM_LIB;
  461. x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
  462. }
  463. else
  464. {
  465. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_certificate_file, SSL_R_BAD_SSL_FILETYPE);
  466. goto end;
  467. }
  468. if (x == NULL)
  469. {
  470. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_certificate_file,reason_code);
  471. goto end;
  472. }
  473. ret=SSL_CTX_use_certificate(ctx,x);
  474. end:
  475. if (x != NULL) X509_free(x);
  476. if (in != NULL) BIO_free(in);
  477. return(ret);
  478. }
  479. #endif
  480. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
  481. {
  482. X509 *x;
  483. int ret;
  484. x=d2i_X509(NULL,&d,(long)len);
  485. if (x == NULL)
  486. {
  487. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_certificate_ASN1, ERR_R_ASN1_LIB);
  488. return(0);
  489. }
  490. ret=SSL_CTX_use_certificate(ctx,x);
  491. X509_free(x);
  492. return(ret);
  493. }
  494. #ifndef OPENSSL_NO_RSA
  495. int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
  496. {
  497. int ret;
  498. EVP_PKEY *pkey;
  499. if (rsa == NULL)
  500. {
  501. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_RSAPrivateKey, ERR_R_PASSED_NULL_PARAMETER);
  502. return(0);
  503. }
  504. if (!ssl_cert_inst(&ctx->cert))
  505. {
  506. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_RSAPrivateKey, ERR_R_MALLOC_FAILURE);
  507. return(0);
  508. }
  509. if ((pkey=EVP_PKEY_new()) == NULL)
  510. {
  511. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_RSAPrivateKey, ERR_R_EVP_LIB);
  512. return(0);
  513. }
  514. RSA_up_ref(rsa);
  515. EVP_PKEY_assign_RSA(pkey,rsa);
  516. ret=ssl_set_pkey(ctx->cert, pkey);
  517. EVP_PKEY_free(pkey);
  518. return(ret);
  519. }
  520. #ifndef OPENSSL_NO_STDIO
  521. int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
  522. {
  523. int reason_code,ret=0;
  524. BIO *in;
  525. RSA *rsa=NULL;
  526. in=BIO_new(BIO_s_file());
  527. if (in == NULL)
  528. {
  529. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_RSAPrivateKey_file, ERR_R_BUF_LIB);
  530. goto end;
  531. }
  532. if (BIO_read_filename(in,file) <= 0)
  533. {
  534. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_RSAPrivateKey_file, ERR_R_SYS_LIB);
  535. goto end;
  536. }
  537. if (type == SSL_FILETYPE_ASN1)
  538. {
  539. reason_code=ERR_R_ASN1_LIB;
  540. rsa=d2i_RSAPrivateKey_bio(in,NULL);
  541. }
  542. else if (type == SSL_FILETYPE_PEM)
  543. {
  544. reason_code=ERR_R_PEM_LIB;
  545. rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
  546. ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
  547. }
  548. else
  549. {
  550. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_RSAPrivateKey_file, SSL_R_BAD_SSL_FILETYPE);
  551. goto end;
  552. }
  553. if (rsa == NULL)
  554. {
  555. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_RSAPrivateKey_file,reason_code);
  556. goto end;
  557. }
  558. ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
  559. RSA_free(rsa);
  560. end:
  561. if (in != NULL) BIO_free(in);
  562. return(ret);
  563. }
  564. #endif
  565. int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d, long len)
  566. {
  567. int ret;
  568. const unsigned char *p;
  569. RSA *rsa;
  570. p=d;
  571. if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
  572. {
  573. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_RSAPrivateKey_ASN1, ERR_R_ASN1_LIB);
  574. return(0);
  575. }
  576. ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
  577. RSA_free(rsa);
  578. return(ret);
  579. }
  580. #endif /* !OPENSSL_NO_RSA */
  581. int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
  582. {
  583. if (pkey == NULL)
  584. {
  585. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_PrivateKey, ERR_R_PASSED_NULL_PARAMETER);
  586. return(0);
  587. }
  588. if (!ssl_cert_inst(&ctx->cert))
  589. {
  590. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_PrivateKey, ERR_R_MALLOC_FAILURE);
  591. return(0);
  592. }
  593. return(ssl_set_pkey(ctx->cert,pkey));
  594. }
  595. #ifndef OPENSSL_NO_STDIO
  596. int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
  597. {
  598. int reason_code,ret=0;
  599. BIO *in;
  600. EVP_PKEY *pkey=NULL;
  601. in=BIO_new(BIO_s_file());
  602. if (in == NULL)
  603. {
  604. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_PrivateKey_file, ERR_R_BUF_LIB);
  605. goto end;
  606. }
  607. if (BIO_read_filename(in,file) <= 0)
  608. {
  609. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_PrivateKey_file, ERR_R_SYS_LIB);
  610. goto end;
  611. }
  612. if (type == SSL_FILETYPE_PEM)
  613. {
  614. reason_code=ERR_R_PEM_LIB;
  615. pkey=PEM_read_bio_PrivateKey(in,NULL,
  616. ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
  617. }
  618. else if (type == SSL_FILETYPE_ASN1)
  619. {
  620. reason_code = ERR_R_ASN1_LIB;
  621. pkey = d2i_PrivateKey_bio(in,NULL);
  622. }
  623. else
  624. {
  625. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_PrivateKey_file, SSL_R_BAD_SSL_FILETYPE);
  626. goto end;
  627. }
  628. if (pkey == NULL)
  629. {
  630. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_PrivateKey_file,reason_code);
  631. goto end;
  632. }
  633. ret=SSL_CTX_use_PrivateKey(ctx,pkey);
  634. EVP_PKEY_free(pkey);
  635. end:
  636. if (in != NULL) BIO_free(in);
  637. return(ret);
  638. }
  639. #endif
  640. int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const unsigned char *d,
  641. long len)
  642. {
  643. int ret;
  644. const unsigned char *p;
  645. EVP_PKEY *pkey;
  646. p=d;
  647. if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
  648. {
  649. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_PrivateKey_ASN1, ERR_R_ASN1_LIB);
  650. return(0);
  651. }
  652. ret=SSL_CTX_use_PrivateKey(ctx,pkey);
  653. EVP_PKEY_free(pkey);
  654. return(ret);
  655. }
  656. #ifndef OPENSSL_NO_STDIO
  657. /* Read a file that contains our certificate in "PEM" format,
  658. * possibly followed by a sequence of CA certificates that should be
  659. * sent to the peer in the Certificate message.
  660. */
  661. int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
  662. {
  663. BIO *in;
  664. int ret=0;
  665. X509 *x=NULL;
  666. ERR_clear_error(); /* clear error stack for SSL_CTX_use_certificate() */
  667. in = BIO_new(BIO_s_file());
  668. if (in == NULL)
  669. {
  670. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_certificate_chain_file, ERR_R_BUF_LIB);
  671. goto end;
  672. }
  673. if (BIO_read_filename(in,file) <= 0)
  674. {
  675. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_certificate_chain_file, ERR_R_SYS_LIB);
  676. goto end;
  677. }
  678. x=PEM_read_bio_X509_AUX(in,NULL,ctx->default_passwd_callback,
  679. ctx->default_passwd_callback_userdata);
  680. if (x == NULL)
  681. {
  682. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_certificate_chain_file, ERR_R_PEM_LIB);
  683. goto end;
  684. }
  685. ret = SSL_CTX_use_certificate(ctx, x);
  686. if (ERR_peek_error() != 0)
  687. ret = 0; /* Key/certificate mismatch doesn't imply ret==0 ... */
  688. if (ret)
  689. {
  690. /* If we could set up our certificate, now proceed to
  691. * the CA certificates.
  692. */
  693. X509 *ca;
  694. int r;
  695. unsigned long err;
  696. SSL_CTX_clear_chain_certs(ctx);
  697. while ((ca = PEM_read_bio_X509(in, NULL,
  698. ctx->default_passwd_callback,
  699. ctx->default_passwd_callback_userdata))
  700. != NULL)
  701. {
  702. r = SSL_CTX_add0_chain_cert(ctx, ca);
  703. if (!r)
  704. {
  705. X509_free(ca);
  706. ret = 0;
  707. goto end;
  708. }
  709. /* Note that we must not free r if it was successfully
  710. * added to the chain (while we must free the main
  711. * certificate, since its reference count is increased
  712. * by SSL_CTX_use_certificate). */
  713. }
  714. /* When the while loop ends, it's usually just EOF. */
  715. err = ERR_peek_last_error();
  716. if (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
  717. ERR_clear_error();
  718. else
  719. ret = 0; /* some real error */
  720. }
  721. end:
  722. if (x != NULL) X509_free(x);
  723. if (in != NULL) BIO_free(in);
  724. return(ret);
  725. }
  726. #endif
  727. #ifndef OPENSSL_NO_TLSEXT
  728. /* authz_validate returns true iff authz is well formed, i.e. that it meets the
  729. * wire format as documented in the CERT_PKEY structure and that there are no
  730. * duplicate entries. */
  731. static char authz_validate(const unsigned char *authz, size_t length)
  732. {
  733. unsigned char types_seen_bitmap[32];
  734. if (!authz)
  735. return 1;
  736. memset(types_seen_bitmap, 0, sizeof(types_seen_bitmap));
  737. for (;;)
  738. {
  739. unsigned char type, byte, bit;
  740. unsigned short len;
  741. if (!length)
  742. return 1;
  743. type = *(authz++);
  744. length--;
  745. byte = type / 8;
  746. bit = type & 7;
  747. if (types_seen_bitmap[byte] & (1 << bit))
  748. return 0;
  749. types_seen_bitmap[byte] |= (1 << bit);
  750. if (length < 2)
  751. return 0;
  752. len = ((unsigned short) authz[0]) << 8 |
  753. ((unsigned short) authz[1]);
  754. authz += 2;
  755. length -= 2;
  756. if (length < len)
  757. return 0;
  758. authz += len;
  759. length -= len;
  760. }
  761. }
  762. static int serverinfo_find_extension(const unsigned char *serverinfo,
  763. size_t serverinfo_length,
  764. unsigned short extension_type,
  765. const unsigned char **extension_data,
  766. unsigned short *extension_length)
  767. {
  768. *extension_data = NULL;
  769. *extension_length = 0;
  770. if (serverinfo == NULL || serverinfo_length == 0)
  771. return 0;
  772. for (;;)
  773. {
  774. unsigned short type = 0; /* uint16 */
  775. unsigned short len = 0; /* uint16 */
  776. /* end of serverinfo */
  777. if (serverinfo_length == 0)
  778. return -1; /* Extension not found */
  779. /* read 2-byte type field */
  780. if (serverinfo_length < 2)
  781. return 0; /* Error */
  782. type = (serverinfo[0] << 8) + serverinfo[1];
  783. serverinfo += 2;
  784. serverinfo_length -= 2;
  785. /* read 2-byte len field */
  786. if (serverinfo_length < 2)
  787. return 0; /* Error */
  788. len = (serverinfo[0] << 8) + serverinfo[1];
  789. serverinfo += 2;
  790. serverinfo_length -= 2;
  791. if (len > serverinfo_length)
  792. return 0; /* Error */
  793. if (type == extension_type)
  794. {
  795. *extension_data = serverinfo;
  796. *extension_length = len;
  797. return 1; /* Success */
  798. }
  799. serverinfo += len;
  800. serverinfo_length -= len;
  801. }
  802. return 0; /* Error */
  803. }
  804. static int serverinfo_srv_first_cb(SSL *s, unsigned short ext_type,
  805. const unsigned char *in,
  806. unsigned short inlen, int *al,
  807. void *arg)
  808. {
  809. if (inlen != 0)
  810. {
  811. *al = SSL_AD_DECODE_ERROR;
  812. return 0;
  813. }
  814. return 1;
  815. }
  816. static int serverinfo_srv_second_cb(SSL *s, unsigned short ext_type,
  817. const unsigned char **out, unsigned short *outlen,
  818. void *arg)
  819. {
  820. const unsigned char *serverinfo = NULL;
  821. size_t serverinfo_length = 0;
  822. /* Is there serverinfo data for the chosen server cert? */
  823. if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
  824. &serverinfo_length)) != 0)
  825. {
  826. /* Find the relevant extension from the serverinfo */
  827. int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
  828. ext_type, out, outlen);
  829. if (retval == 0)
  830. return 0; /* Error */
  831. if (retval == -1)
  832. return -1; /* No extension found, don't send extension */
  833. return 1; /* Send extension */
  834. }
  835. return -1; /* No serverinfo data found, don't send extension */
  836. }
  837. /* With a NULL context, this function just checks that the serverinfo data
  838. parses correctly. With a non-NULL context, it registers callbacks for
  839. the included extensions. */
  840. static int serverinfo_process_buffer(const unsigned char *serverinfo,
  841. size_t serverinfo_length, SSL_CTX *ctx)
  842. {
  843. if (serverinfo == NULL || serverinfo_length == 0)
  844. return 0;
  845. for (;;)
  846. {
  847. unsigned short ext_type = 0; /* uint16 */
  848. unsigned short len = 0; /* uint16 */
  849. /* end of serverinfo */
  850. if (serverinfo_length == 0)
  851. return 1;
  852. /* read 2-byte type field */
  853. if (serverinfo_length < 2)
  854. return 0;
  855. /* FIXME: check for types we understand explicitly? */
  856. /* Register callbacks for extensions */
  857. ext_type = (serverinfo[0] << 8) + serverinfo[1];
  858. if (ctx && !SSL_CTX_set_custom_srv_ext(ctx, ext_type,
  859. serverinfo_srv_first_cb,
  860. serverinfo_srv_second_cb, NULL))
  861. return 0;
  862. serverinfo += 2;
  863. serverinfo_length -= 2;
  864. /* read 2-byte len field */
  865. if (serverinfo_length < 2)
  866. return 0;
  867. len = (serverinfo[0] << 8) + serverinfo[1];
  868. serverinfo += 2;
  869. serverinfo_length -= 2;
  870. if (len > serverinfo_length)
  871. return 0;
  872. serverinfo += len;
  873. serverinfo_length -= len;
  874. }
  875. }
  876. static const unsigned char *authz_find_data(const unsigned char *authz,
  877. size_t authz_length,
  878. unsigned char data_type,
  879. size_t *data_length)
  880. {
  881. if (authz == NULL) return NULL;
  882. if (!authz_validate(authz, authz_length))
  883. {
  884. OPENSSL_PUT_ERROR(SSL, authz_find_data, SSL_R_INVALID_AUTHZ_DATA);
  885. return NULL;
  886. }
  887. for (;;)
  888. {
  889. unsigned char type;
  890. unsigned short len;
  891. if (!authz_length)
  892. return NULL;
  893. type = *(authz++);
  894. authz_length--;
  895. /* We've validated the authz data, so we don't have to
  896. * check again that we have enough bytes left. */
  897. len = ((unsigned short) authz[0]) << 8 |
  898. ((unsigned short) authz[1]);
  899. authz += 2;
  900. authz_length -= 2;
  901. if (type == data_type)
  902. {
  903. *data_length = len;
  904. return authz;
  905. }
  906. authz += len;
  907. authz_length -= len;
  908. }
  909. /* No match */
  910. return NULL;
  911. }
  912. static int ssl_set_authz(CERT *c, unsigned char *authz, size_t authz_length)
  913. {
  914. CERT_PKEY *current_key = c->key;
  915. if (current_key == NULL)
  916. return 0;
  917. if (!authz_validate(authz, authz_length))
  918. {
  919. OPENSSL_PUT_ERROR(SSL, ssl_set_authz, SSL_R_INVALID_AUTHZ_DATA);
  920. return(0);
  921. }
  922. current_key->authz = OPENSSL_realloc(current_key->authz, authz_length);
  923. if (current_key->authz == NULL)
  924. {
  925. OPENSSL_PUT_ERROR(SSL, ssl_set_authz, ERR_R_MALLOC_FAILURE);
  926. return 0;
  927. }
  928. current_key->authz_length = authz_length;
  929. memcpy(current_key->authz, authz, authz_length);
  930. return 1;
  931. }
  932. int SSL_CTX_use_authz(SSL_CTX *ctx, unsigned char *authz,
  933. size_t authz_length)
  934. {
  935. if (authz == NULL)
  936. {
  937. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_authz, ERR_R_PASSED_NULL_PARAMETER);
  938. return 0;
  939. }
  940. if (!ssl_cert_inst(&ctx->cert))
  941. {
  942. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_authz, ERR_R_MALLOC_FAILURE);
  943. return 0;
  944. }
  945. return ssl_set_authz(ctx->cert, authz, authz_length);
  946. }
  947. int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
  948. size_t serverinfo_length)
  949. {
  950. if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0)
  951. {
  952. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_serverinfo, ERR_R_PASSED_NULL_PARAMETER);
  953. return 0;
  954. }
  955. if (!serverinfo_process_buffer(serverinfo, serverinfo_length, NULL))
  956. {
  957. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_serverinfo, SSL_R_INVALID_SERVERINFO_DATA);
  958. return 0;
  959. }
  960. if (!ssl_cert_inst(&ctx->cert))
  961. {
  962. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_serverinfo, ERR_R_MALLOC_FAILURE);
  963. return 0;
  964. }
  965. if (ctx->cert->key == NULL)
  966. {
  967. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_serverinfo, ERR_R_INTERNAL_ERROR);
  968. return 0;
  969. }
  970. ctx->cert->key->serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
  971. serverinfo_length);
  972. if (ctx->cert->key->serverinfo == NULL)
  973. {
  974. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_serverinfo, ERR_R_MALLOC_FAILURE);
  975. return 0;
  976. }
  977. memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
  978. ctx->cert->key->serverinfo_length = serverinfo_length;
  979. /* Now that the serverinfo is validated and stored, go ahead and
  980. * register callbacks. */
  981. if (!serverinfo_process_buffer(serverinfo, serverinfo_length, ctx))
  982. {
  983. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_serverinfo, SSL_R_INVALID_SERVERINFO_DATA);
  984. return 0;
  985. }
  986. return 1;
  987. }
  988. int SSL_use_authz(SSL *ssl, unsigned char *authz, size_t authz_length)
  989. {
  990. if (authz == NULL)
  991. {
  992. OPENSSL_PUT_ERROR(SSL, SSL_use_authz, ERR_R_PASSED_NULL_PARAMETER);
  993. return 0;
  994. }
  995. if (!ssl_cert_inst(&ssl->cert))
  996. {
  997. OPENSSL_PUT_ERROR(SSL, SSL_use_authz, ERR_R_MALLOC_FAILURE);
  998. return 0;
  999. }
  1000. return ssl_set_authz(ssl->cert, authz, authz_length);
  1001. }
  1002. const unsigned char *SSL_CTX_get_authz_data(SSL_CTX *ctx, unsigned char type,
  1003. size_t *data_length)
  1004. {
  1005. CERT_PKEY *current_key;
  1006. if (ctx->cert == NULL)
  1007. return NULL;
  1008. current_key = ctx->cert->key;
  1009. if (current_key->authz == NULL)
  1010. return NULL;
  1011. return authz_find_data(current_key->authz,
  1012. current_key->authz_length, type, data_length);
  1013. }
  1014. #ifndef OPENSSL_NO_STDIO
  1015. /* read_authz returns a newly allocated buffer with authz data */
  1016. static unsigned char *read_authz(const char *file, size_t *authz_length)
  1017. {
  1018. BIO *authz_in = NULL;
  1019. unsigned char *authz = NULL;
  1020. /* Allow authzs up to 64KB. */
  1021. static const size_t authz_limit = 65536;
  1022. size_t read_length;
  1023. unsigned char *ret = NULL;
  1024. authz_in = BIO_new(BIO_s_file());
  1025. if (authz_in == NULL)
  1026. {
  1027. OPENSSL_PUT_ERROR(SSL, read_authz, ERR_R_BUF_LIB);
  1028. goto end;
  1029. }
  1030. if (BIO_read_filename(authz_in,file) <= 0)
  1031. {
  1032. OPENSSL_PUT_ERROR(SSL, read_authz, ERR_R_SYS_LIB);
  1033. goto end;
  1034. }
  1035. authz = OPENSSL_malloc(authz_limit);
  1036. read_length = BIO_read(authz_in, authz, authz_limit);
  1037. if (read_length == authz_limit || read_length <= 0)
  1038. {
  1039. OPENSSL_PUT_ERROR(SSL, read_authz, SSL_R_AUTHZ_DATA_TOO_LARGE);
  1040. OPENSSL_free(authz);
  1041. goto end;
  1042. }
  1043. *authz_length = read_length;
  1044. ret = authz;
  1045. end:
  1046. if (authz_in != NULL) BIO_free(authz_in);
  1047. return ret;
  1048. }
  1049. int SSL_CTX_use_authz_file(SSL_CTX *ctx, const char *file)
  1050. {
  1051. unsigned char *authz = NULL;
  1052. size_t authz_length = 0;
  1053. int ret;
  1054. authz = read_authz(file, &authz_length);
  1055. if (authz == NULL)
  1056. return 0;
  1057. ret = SSL_CTX_use_authz(ctx, authz, authz_length);
  1058. /* SSL_CTX_use_authz makes a local copy of the authz. */
  1059. OPENSSL_free(authz);
  1060. return ret;
  1061. }
  1062. int SSL_use_authz_file(SSL *ssl, const char *file)
  1063. {
  1064. unsigned char *authz = NULL;
  1065. size_t authz_length = 0;
  1066. int ret;
  1067. authz = read_authz(file, &authz_length);
  1068. if (authz == NULL)
  1069. return 0;
  1070. ret = SSL_use_authz(ssl, authz, authz_length);
  1071. /* SSL_use_authz makes a local copy of the authz. */
  1072. OPENSSL_free(authz);
  1073. return ret;
  1074. }
  1075. int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
  1076. {
  1077. unsigned char *serverinfo = NULL;
  1078. size_t serverinfo_length = 0;
  1079. unsigned char* extension = 0;
  1080. long extension_length = 0;
  1081. char* name = NULL;
  1082. char* header = NULL;
  1083. char namePrefix[] = "SERVERINFO FOR ";
  1084. int ret = 0;
  1085. BIO *bin = NULL;
  1086. size_t num_extensions = 0;
  1087. if (ctx == NULL || file == NULL)
  1088. {
  1089. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_serverinfo_file, ERR_R_PASSED_NULL_PARAMETER);
  1090. goto end;
  1091. }
  1092. bin = BIO_new(BIO_s_file());
  1093. if (bin == NULL)
  1094. {
  1095. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_serverinfo_file, ERR_R_BUF_LIB);
  1096. goto end;
  1097. }
  1098. if (BIO_read_filename(bin, file) <= 0)
  1099. {
  1100. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_serverinfo_file, ERR_R_SYS_LIB);
  1101. goto end;
  1102. }
  1103. for (num_extensions=0;; num_extensions++)
  1104. {
  1105. if (PEM_read_bio(bin, &name, &header, &extension, &extension_length) == 0)
  1106. {
  1107. /* There must be at least one extension in this file */
  1108. if (num_extensions == 0)
  1109. {
  1110. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_serverinfo_file, SSL_R_NO_PEM_EXTENSIONS);
  1111. goto end;
  1112. }
  1113. else /* End of file, we're done */
  1114. break;
  1115. }
  1116. /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
  1117. if (strlen(name) < strlen(namePrefix))
  1118. {
  1119. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_serverinfo_file, SSL_R_PEM_NAME_TOO_SHORT);
  1120. goto end;
  1121. }
  1122. if (strncmp(name, namePrefix, strlen(namePrefix)) != 0)
  1123. {
  1124. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_serverinfo_file, SSL_R_PEM_NAME_BAD_PREFIX);
  1125. goto end;
  1126. }
  1127. /* Check that the decoded PEM data is plausible (valid length field) */
  1128. if (extension_length < 4 || (extension[2] << 8) + extension[3] != extension_length - 4)
  1129. {
  1130. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_serverinfo_file, SSL_R_BAD_DATA);
  1131. goto end;
  1132. }
  1133. /* Append the decoded extension to the serverinfo buffer */
  1134. serverinfo = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length);
  1135. if (serverinfo == NULL)
  1136. {
  1137. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_serverinfo_file, ERR_R_MALLOC_FAILURE);
  1138. goto end;
  1139. }
  1140. memcpy(serverinfo + serverinfo_length, extension, extension_length);
  1141. serverinfo_length += extension_length;
  1142. OPENSSL_free(name); name = NULL;
  1143. OPENSSL_free(header); header = NULL;
  1144. OPENSSL_free(extension); extension = NULL;
  1145. }
  1146. ret = SSL_CTX_use_serverinfo(ctx, serverinfo, serverinfo_length);
  1147. end:
  1148. /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
  1149. OPENSSL_free(name);
  1150. OPENSSL_free(header);
  1151. OPENSSL_free(extension);
  1152. OPENSSL_free(serverinfo);
  1153. if (bin != NULL)
  1154. BIO_free(bin);
  1155. return ret;
  1156. }
  1157. #endif /* OPENSSL_NO_STDIO */
  1158. #endif /* OPENSSL_NO_TLSEXT */