ssl_lib.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "ssl_lib.h"
  14. #include "ssl_pkey.h"
  15. #include "ssl_x509.h"
  16. #include "ssl_cert.h"
  17. #include "ssl_dbg.h"
  18. #include "ssl_port.h"
  19. #include "private-lib-core.h"
  20. char *
  21. lws_strncpy(char *dest, const char *src, size_t size);
  22. #define SSL_SEND_DATA_MAX_LENGTH 1460
  23. /**
  24. * @brief create a new SSL session object
  25. */
  26. static SSL_SESSION* SSL_SESSION_new(void)
  27. {
  28. SSL_SESSION *session;
  29. session = ssl_mem_zalloc(sizeof(SSL_SESSION));
  30. if (!session) {
  31. SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "no enough memory > (session)");
  32. goto failed1;
  33. }
  34. session->peer = X509_new();
  35. if (!session->peer) {
  36. SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "X509_new() return NULL");
  37. goto failed2;
  38. }
  39. return session;
  40. failed2:
  41. ssl_mem_free(session);
  42. failed1:
  43. return NULL;
  44. }
  45. /**
  46. * @brief free a new SSL session object
  47. */
  48. static void SSL_SESSION_free(SSL_SESSION *session)
  49. {
  50. X509_free(session->peer);
  51. ssl_mem_free(session);
  52. }
  53. /**
  54. * @brief Discover whether the current connection is in the error state
  55. */
  56. int ossl_statem_in_error(const SSL *ssl)
  57. {
  58. SSL_ASSERT1(ssl);
  59. if (ssl->statem.state == MSG_FLOW_ERROR)
  60. return 1;
  61. return 0;
  62. }
  63. /**
  64. * @brief get the SSL specifical statement
  65. */
  66. int SSL_want(const SSL *ssl)
  67. {
  68. SSL_ASSERT1(ssl);
  69. return ssl->rwstate;
  70. }
  71. /**
  72. * @brief check if SSL want nothing
  73. */
  74. int SSL_want_nothing(const SSL *ssl)
  75. {
  76. SSL_ASSERT1(ssl);
  77. if (ssl->err)
  78. return 1;
  79. return (SSL_want(ssl) == SSL_NOTHING);
  80. }
  81. /**
  82. * @brief check if SSL want to read
  83. */
  84. int SSL_want_read(const SSL *ssl)
  85. {
  86. SSL_ASSERT1(ssl);
  87. if (ssl->err)
  88. return 0;
  89. return (SSL_want(ssl) == SSL_READING);
  90. }
  91. /**
  92. * @brief check if SSL want to write
  93. */
  94. int SSL_want_write(const SSL *ssl)
  95. {
  96. SSL_ASSERT1(ssl);
  97. if (ssl->err)
  98. return 0;
  99. return (SSL_want(ssl) == SSL_WRITING);
  100. }
  101. /**
  102. * @brief check if SSL want to lookup X509 certification
  103. */
  104. int SSL_want_x509_lookup(const SSL *ssl)
  105. {
  106. SSL_ASSERT1(ssl);
  107. return (SSL_want(ssl) == SSL_WRITING);
  108. }
  109. /**
  110. * @brief get SSL error code
  111. */
  112. int SSL_get_error(const SSL *ssl, int ret_code)
  113. {
  114. int ret = SSL_ERROR_SYSCALL;
  115. SSL_ASSERT1(ssl);
  116. if (ret_code > 0)
  117. ret = SSL_ERROR_NONE;
  118. else if (ret_code < 0)
  119. {
  120. if (ssl->err == SSL_ERROR_WANT_READ || SSL_want_read(ssl))
  121. ret = SSL_ERROR_WANT_READ;
  122. else if (ssl->err == SSL_ERROR_WANT_WRITE || SSL_want_write(ssl))
  123. ret = SSL_ERROR_WANT_WRITE;
  124. else
  125. ret = SSL_ERROR_SYSCALL; //unknown
  126. }
  127. else // ret_code == 0
  128. {
  129. if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN)
  130. ret = SSL_ERROR_ZERO_RETURN;
  131. else
  132. ret = SSL_ERROR_SYSCALL;
  133. }
  134. return ret;
  135. }
  136. /**
  137. * @brief get the SSL state
  138. */
  139. OSSL_HANDSHAKE_STATE SSL_get_state(const SSL *ssl)
  140. {
  141. OSSL_HANDSHAKE_STATE state;
  142. SSL_ASSERT1(ssl);
  143. state = SSL_METHOD_CALL(get_state, ssl);
  144. return state;
  145. }
  146. /**
  147. * @brief create a SSL context
  148. */
  149. SSL_CTX* SSL_CTX_new(const SSL_METHOD *method)
  150. {
  151. SSL_CTX *ctx;
  152. CERT *cert;
  153. X509 *client_ca;
  154. if (!method) {
  155. SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "no no_method");
  156. return NULL;
  157. }
  158. client_ca = X509_new();
  159. if (!client_ca) {
  160. SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "X509_new() return NULL");
  161. goto failed1;
  162. }
  163. cert = ssl_cert_new();
  164. if (!cert) {
  165. SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "ssl_cert_new() return NULL");
  166. goto failed2;
  167. }
  168. ctx = (SSL_CTX *)ssl_mem_zalloc(sizeof(SSL_CTX));
  169. if (!ctx) {
  170. SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "no enough memory > (ctx)");
  171. goto failed3;
  172. }
  173. ctx->method = method;
  174. ctx->client_CA = client_ca;
  175. ctx->cert = cert;
  176. ctx->version = method->version;
  177. return ctx;
  178. failed3:
  179. ssl_cert_free(cert);
  180. failed2:
  181. X509_free(client_ca);
  182. failed1:
  183. return NULL;
  184. }
  185. /**
  186. * @brief free a SSL context
  187. */
  188. void SSL_CTX_free(SSL_CTX* ctx)
  189. {
  190. SSL_ASSERT3(ctx);
  191. ssl_cert_free(ctx->cert);
  192. X509_free(ctx->client_CA);
  193. if (ctx->alpn_protos)
  194. ssl_mem_free(ctx->alpn_protos);
  195. ssl_mem_free(ctx);
  196. }
  197. /**
  198. * @brief set the SSL context version
  199. */
  200. int SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth)
  201. {
  202. SSL_ASSERT1(ctx);
  203. SSL_ASSERT1(meth);
  204. ctx->method = meth;
  205. ctx->version = meth->version;
  206. return 1;
  207. }
  208. /**
  209. * @brief get the SSL context current method
  210. */
  211. const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx)
  212. {
  213. SSL_ASSERT2(ctx);
  214. return ctx->method;
  215. }
  216. /**
  217. * @brief create a SSL
  218. */
  219. SSL *SSL_new(SSL_CTX *ctx)
  220. {
  221. int ret = 0;
  222. SSL *ssl;
  223. if (!ctx) {
  224. SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "no ctx");
  225. return NULL;
  226. }
  227. ssl = (SSL *)ssl_mem_zalloc(sizeof(SSL));
  228. if (!ssl) {
  229. SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "no enough memory > (ssl)");
  230. goto failed1;
  231. }
  232. ssl->session = SSL_SESSION_new();
  233. if (!ssl->session) {
  234. SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "SSL_SESSION_new() return NULL");
  235. goto failed2;
  236. }
  237. ssl->cert = __ssl_cert_new(ctx->cert);
  238. if (!ssl->cert) {
  239. SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "__ssl_cert_new() return NULL");
  240. goto failed3;
  241. }
  242. ssl->client_CA = __X509_new(ctx->client_CA);
  243. if (!ssl->client_CA) {
  244. SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "__X509_new() return NULL");
  245. goto failed4;
  246. }
  247. ssl->ctx = ctx;
  248. ssl->method = ctx->method;
  249. ssl->version = ctx->version;
  250. ssl->options = ctx->options;
  251. ssl->verify_mode = ctx->verify_mode;
  252. ret = SSL_METHOD_CALL(new, ssl);
  253. if (ret) {
  254. SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "SSL_METHOD_CALL(new) return %d", ret);
  255. goto failed5;
  256. }
  257. _ssl_set_alpn_list(ssl);
  258. ssl->rwstate = SSL_NOTHING;
  259. return ssl;
  260. failed5:
  261. X509_free(ssl->client_CA);
  262. failed4:
  263. ssl_cert_free(ssl->cert);
  264. failed3:
  265. SSL_SESSION_free(ssl->session);
  266. failed2:
  267. ssl_mem_free(ssl);
  268. failed1:
  269. return NULL;
  270. }
  271. /**
  272. * @brief free the SSL
  273. */
  274. void SSL_free(SSL *ssl)
  275. {
  276. SSL_ASSERT3(ssl);
  277. SSL_METHOD_CALL(free, ssl);
  278. X509_free(ssl->client_CA);
  279. ssl_cert_free(ssl->cert);
  280. SSL_SESSION_free(ssl->session);
  281. if (ssl->alpn_protos)
  282. ssl_mem_free(ssl->alpn_protos);
  283. ssl_mem_free(ssl);
  284. }
  285. /**
  286. * @brief perform the SSL handshake
  287. */
  288. int SSL_do_handshake(SSL *ssl)
  289. {
  290. int ret;
  291. SSL_ASSERT1(ssl);
  292. ret = SSL_METHOD_CALL(handshake, ssl);
  293. return ret;
  294. }
  295. /**
  296. * @brief connect to the remote SSL server
  297. */
  298. int SSL_connect(SSL *ssl)
  299. {
  300. SSL_ASSERT1(ssl);
  301. return SSL_do_handshake(ssl);
  302. }
  303. /**
  304. * @brief accept the remote connection
  305. */
  306. int SSL_accept(SSL *ssl)
  307. {
  308. SSL_ASSERT1(ssl);
  309. return SSL_do_handshake(ssl);
  310. }
  311. /**
  312. * @brief shutdown the connection
  313. */
  314. int SSL_shutdown(SSL *ssl)
  315. {
  316. int ret;
  317. SSL_ASSERT1(ssl);
  318. if (SSL_get_state(ssl) != TLS_ST_OK) return 1;
  319. ret = SSL_METHOD_CALL(shutdown, ssl);
  320. return ret;
  321. }
  322. /**
  323. * @brief reset the SSL
  324. */
  325. int SSL_clear(SSL *ssl)
  326. {
  327. int ret;
  328. SSL_ASSERT1(ssl);
  329. ret = SSL_shutdown(ssl);
  330. if (1 != ret) {
  331. SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "SSL_shutdown return %d", ret);
  332. goto failed1;
  333. }
  334. SSL_METHOD_CALL(free, ssl);
  335. ret = SSL_METHOD_CALL(new, ssl);
  336. if (!ret) {
  337. SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "SSL_METHOD_CALL(new) return %d", ret);
  338. goto failed1;
  339. }
  340. return 1;
  341. failed1:
  342. return ret;
  343. }
  344. /**
  345. * @brief read data from to remote
  346. */
  347. int SSL_read(SSL *ssl, void *buffer, int len)
  348. {
  349. int ret;
  350. SSL_ASSERT1(ssl);
  351. SSL_ASSERT1(buffer);
  352. SSL_ASSERT1(len);
  353. ssl->rwstate = SSL_READING;
  354. ret = SSL_METHOD_CALL(read, ssl, buffer, len);
  355. if (ret == len)
  356. ssl->rwstate = SSL_NOTHING;
  357. return ret;
  358. }
  359. /**
  360. * @brief send the data to remote
  361. */
  362. int SSL_write(SSL *ssl, const void *buffer, int len)
  363. {
  364. int ret;
  365. int send_bytes, bytes;
  366. const unsigned char *pbuf;
  367. SSL_ASSERT1(ssl);
  368. SSL_ASSERT1(buffer);
  369. SSL_ASSERT1(len);
  370. ssl->rwstate = SSL_WRITING;
  371. send_bytes = len;
  372. pbuf = (const unsigned char *)buffer;
  373. do {
  374. if (send_bytes > SSL_SEND_DATA_MAX_LENGTH)
  375. bytes = SSL_SEND_DATA_MAX_LENGTH;
  376. else
  377. bytes = send_bytes;
  378. if (ssl->interrupted_remaining_write) {
  379. bytes = ssl->interrupted_remaining_write;
  380. ssl->interrupted_remaining_write = 0;
  381. }
  382. ret = SSL_METHOD_CALL(send, ssl, pbuf, bytes);
  383. //printf("%s: ssl_pm said %d for %d requested (cum %d)\n", __func__, ret, bytes, len -send_bytes);
  384. /* the return is a NEGATIVE OpenSSL error code, or the length sent */
  385. if (ret > 0) {
  386. pbuf += ret;
  387. send_bytes -= ret;
  388. } else
  389. ssl->interrupted_remaining_write = bytes;
  390. } while (ret > 0 && send_bytes && ret == bytes);
  391. if (ret >= 0) {
  392. ret = len - send_bytes;
  393. if (!ret)
  394. ssl->rwstate = SSL_NOTHING;
  395. } else {
  396. if (send_bytes == len)
  397. ret = -1;
  398. else
  399. ret = len - send_bytes;
  400. }
  401. return ret;
  402. }
  403. /**
  404. * @brief get SSL context of the SSL
  405. */
  406. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
  407. {
  408. SSL_ASSERT2(ssl);
  409. return ssl->ctx;
  410. }
  411. /**
  412. * @brief get the SSL current method
  413. */
  414. const SSL_METHOD *SSL_get_ssl_method(SSL *ssl)
  415. {
  416. SSL_ASSERT2(ssl);
  417. return ssl->method;
  418. }
  419. /**
  420. * @brief set the SSL method
  421. */
  422. int SSL_set_ssl_method(SSL *ssl, const SSL_METHOD *method)
  423. {
  424. int ret;
  425. SSL_ASSERT1(ssl);
  426. SSL_ASSERT1(method);
  427. if (ssl->version != method->version) {
  428. ret = SSL_shutdown(ssl);
  429. if (1 != ret) {
  430. SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "SSL_shutdown return %d", ret);
  431. goto failed1;
  432. }
  433. SSL_METHOD_CALL(free, ssl);
  434. ssl->method = method;
  435. ret = SSL_METHOD_CALL(new, ssl);
  436. if (!ret) {
  437. SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "SSL_METHOD_CALL(new) return %d", ret);
  438. goto failed1;
  439. }
  440. } else {
  441. ssl->method = method;
  442. }
  443. return 1;
  444. failed1:
  445. return ret;
  446. }
  447. /**
  448. * @brief get SSL shutdown mode
  449. */
  450. int SSL_get_shutdown(const SSL *ssl)
  451. {
  452. SSL_ASSERT1(ssl);
  453. return ssl->shutdown;
  454. }
  455. /**
  456. * @brief set SSL shutdown mode
  457. */
  458. void SSL_set_shutdown(SSL *ssl, int mode)
  459. {
  460. SSL_ASSERT3(ssl);
  461. ssl->shutdown = mode;
  462. }
  463. /**
  464. * @brief get the number of the bytes to be read
  465. */
  466. int SSL_pending(const SSL *ssl)
  467. {
  468. int ret;
  469. SSL_ASSERT1(ssl);
  470. ret = SSL_METHOD_CALL(pending, ssl);
  471. return ret;
  472. }
  473. /**
  474. * @brief check if some data can be read
  475. */
  476. int SSL_has_pending(const SSL *ssl)
  477. {
  478. int ret;
  479. SSL_ASSERT1(ssl);
  480. if (SSL_pending(ssl))
  481. ret = 1;
  482. else
  483. ret = 0;
  484. return ret;
  485. }
  486. /**
  487. * @brief clear the SSL context option bit of "op"
  488. */
  489. unsigned long SSL_CTX_clear_options(SSL_CTX *ctx, unsigned long op)
  490. {
  491. SSL_ASSERT1(ctx);
  492. return ctx->options &= ~op;
  493. }
  494. /**
  495. * @brief get the SSL context option
  496. */
  497. unsigned long SSL_CTX_get_options(SSL_CTX *ctx)
  498. {
  499. SSL_ASSERT1(ctx);
  500. return ctx->options;
  501. }
  502. /**
  503. * @brief set the option of the SSL context
  504. */
  505. unsigned long SSL_CTX_set_options(SSL_CTX *ctx, unsigned long opt)
  506. {
  507. SSL_ASSERT1(ctx);
  508. return ctx->options |= opt;
  509. }
  510. /**
  511. * @brief clear SSL option
  512. */
  513. unsigned long SSL_clear_options(SSL *ssl, unsigned long op)
  514. {
  515. SSL_ASSERT1(ssl);
  516. return ssl->options & ~op;
  517. }
  518. /**
  519. * @brief get SSL option
  520. */
  521. unsigned long SSL_get_options(SSL *ssl)
  522. {
  523. SSL_ASSERT1(ssl);
  524. return ssl->options;
  525. }
  526. /**
  527. * @brief clear SSL option
  528. */
  529. unsigned long SSL_set_options(SSL *ssl, unsigned long op)
  530. {
  531. SSL_ASSERT1(ssl);
  532. return ssl->options |= op;
  533. }
  534. /**
  535. * @brief get the socket handle of the SSL
  536. */
  537. int SSL_get_fd(const SSL *ssl)
  538. {
  539. int ret;
  540. SSL_ASSERT1(ssl);
  541. ret = SSL_METHOD_CALL(get_fd, ssl, 0);
  542. return ret;
  543. }
  544. /**
  545. * @brief get the read only socket handle of the SSL
  546. */
  547. int SSL_get_rfd(const SSL *ssl)
  548. {
  549. int ret;
  550. SSL_ASSERT1(ssl);
  551. ret = SSL_METHOD_CALL(get_fd, ssl, 0);
  552. return ret;
  553. }
  554. /**
  555. * @brief get the write only socket handle of the SSL
  556. */
  557. int SSL_get_wfd(const SSL *ssl)
  558. {
  559. int ret;
  560. SSL_ASSERT1(ssl);
  561. ret = SSL_METHOD_CALL(get_fd, ssl, 0);
  562. return ret;
  563. }
  564. /**
  565. * @brief bind the socket file description into the SSL
  566. */
  567. int SSL_set_fd(SSL *ssl, int fd)
  568. {
  569. SSL_ASSERT1(ssl);
  570. SSL_ASSERT1(fd >= 0);
  571. SSL_METHOD_CALL(set_fd, ssl, fd, 0);
  572. return 1;
  573. }
  574. /**
  575. * @brief bind the read only socket file description into the SSL
  576. */
  577. int SSL_set_rfd(SSL *ssl, int fd)
  578. {
  579. SSL_ASSERT1(ssl);
  580. SSL_ASSERT1(fd >= 0);
  581. SSL_METHOD_CALL(set_fd, ssl, fd, 0);
  582. return 1;
  583. }
  584. /**
  585. * @brief bind the write only socket file description into the SSL
  586. */
  587. int SSL_set_wfd(SSL *ssl, int fd)
  588. {
  589. SSL_ASSERT1(ssl);
  590. SSL_ASSERT1(fd >= 0);
  591. SSL_METHOD_CALL(set_fd, ssl, fd, 0);
  592. return 1;
  593. }
  594. /**
  595. * @brief get SSL version
  596. */
  597. int SSL_version(const SSL *ssl)
  598. {
  599. SSL_ASSERT1(ssl);
  600. return ssl->version;
  601. }
  602. /**
  603. * @brief get the SSL version string
  604. */
  605. static const char* ssl_protocol_to_string(int version)
  606. {
  607. const char *str;
  608. if (version == TLS1_2_VERSION)
  609. str = "TLSv1.2";
  610. else if (version == TLS1_1_VERSION)
  611. str = "TLSv1.1";
  612. else if (version == TLS1_VERSION)
  613. str = "TLSv1";
  614. else if (version == SSL3_VERSION)
  615. str = "SSLv3";
  616. else
  617. str = "unknown";
  618. return str;
  619. }
  620. /**
  621. * @brief get the SSL current version
  622. */
  623. const char *SSL_get_version(const SSL *ssl)
  624. {
  625. SSL_ASSERT2(ssl);
  626. return ssl_protocol_to_string(SSL_version(ssl));
  627. }
  628. /**
  629. * @brief get alert type string
  630. */
  631. const char *SSL_alert_type_string(int value)
  632. {
  633. const char *str;
  634. switch (value >> 8)
  635. {
  636. case SSL3_AL_WARNING:
  637. str = "W";
  638. break;
  639. case SSL3_AL_FATAL:
  640. str = "F";
  641. break;
  642. default:
  643. str = "U";
  644. break;
  645. }
  646. return str;
  647. }
  648. /**
  649. * @brief set the SSL context read buffer length
  650. */
  651. void SSL_CTX_set_default_read_buffer_len(SSL_CTX *ctx, size_t len)
  652. {
  653. SSL_ASSERT3(ctx);
  654. ctx->read_buffer_len = len;
  655. }
  656. /**
  657. * @brief set the SSL read buffer length
  658. */
  659. void SSL_set_default_read_buffer_len(SSL *ssl, size_t len)
  660. {
  661. SSL_ASSERT3(ssl);
  662. SSL_ASSERT3(len);
  663. SSL_METHOD_CALL(set_bufflen, ssl, len);
  664. }
  665. /**
  666. * @brief set the SSL information callback function
  667. */
  668. void SSL_set_info_callback(SSL *ssl, void (*cb) (const SSL *ssl, int type, int val))
  669. {
  670. SSL_ASSERT3(ssl);
  671. ssl->info_callback = cb;
  672. }
  673. /**
  674. * @brief add SSL context reference count by '1'
  675. */
  676. int SSL_CTX_up_ref(SSL_CTX *ctx)
  677. {
  678. SSL_ASSERT1(ctx);
  679. /**
  680. * no support multi-thread SSL here
  681. */
  682. ctx->references++;
  683. return 1;
  684. }
  685. /**
  686. * @brief set the SSL security level
  687. */
  688. void SSL_set_security_level(SSL *ssl, int level)
  689. {
  690. SSL_ASSERT3(ssl);
  691. ssl->cert->sec_level = level;
  692. }
  693. /**
  694. * @brief get the SSL security level
  695. */
  696. int SSL_get_security_level(const SSL *ssl)
  697. {
  698. SSL_ASSERT1(ssl);
  699. return ssl->cert->sec_level;
  700. }
  701. /**
  702. * @brief get the SSL verifying mode of the SSL context
  703. */
  704. int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
  705. {
  706. SSL_ASSERT1(ctx);
  707. return ctx->verify_mode;
  708. }
  709. /**
  710. * @brief set the session timeout time
  711. */
  712. long SSL_CTX_set_timeout(SSL_CTX *ctx, long t)
  713. {
  714. long l;
  715. SSL_ASSERT1(ctx);
  716. l = ctx->session_timeout;
  717. ctx->session_timeout = t;
  718. return l;
  719. }
  720. /**
  721. * @brief get the session timeout time
  722. */
  723. long SSL_CTX_get_timeout(const SSL_CTX *ctx)
  724. {
  725. SSL_ASSERT1(ctx);
  726. return ctx->session_timeout;
  727. }
  728. /**
  729. * @brief set the SSL if we can read as many as data
  730. */
  731. void SSL_set_read_ahead(SSL *ssl, int yes)
  732. {
  733. SSL_ASSERT3(ssl);
  734. ssl->rlayer.read_ahead = yes;
  735. }
  736. /**
  737. * @brief set the SSL context if we can read as many as data
  738. */
  739. void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes)
  740. {
  741. SSL_ASSERT3(ctx);
  742. ctx->read_ahead = yes;
  743. }
  744. /**
  745. * @brief get the SSL ahead signal if we can read as many as data
  746. */
  747. int SSL_get_read_ahead(const SSL *ssl)
  748. {
  749. SSL_ASSERT1(ssl);
  750. return ssl->rlayer.read_ahead;
  751. }
  752. /**
  753. * @brief get the SSL context ahead signal if we can read as many as data
  754. */
  755. long SSL_CTX_get_read_ahead(SSL_CTX *ctx)
  756. {
  757. SSL_ASSERT1(ctx);
  758. return ctx->read_ahead;
  759. }
  760. /**
  761. * @brief check if the SSL context can read as many as data
  762. */
  763. long SSL_CTX_get_default_read_ahead(SSL_CTX *ctx)
  764. {
  765. SSL_ASSERT1(ctx);
  766. return ctx->read_ahead;
  767. }
  768. /**
  769. * @brief set SSL session time
  770. */
  771. long SSL_set_time(SSL *ssl, long t)
  772. {
  773. SSL_ASSERT1(ssl);
  774. ssl->session->time = t;
  775. return t;
  776. }
  777. /**
  778. * @brief set SSL session timeout time
  779. */
  780. long SSL_set_timeout(SSL *ssl, long t)
  781. {
  782. SSL_ASSERT1(ssl);
  783. ssl->session->timeout = t;
  784. return t;
  785. }
  786. /**
  787. * @brief get the verifying result of the SSL certification
  788. */
  789. long SSL_get_verify_result(const SSL *ssl)
  790. {
  791. SSL_ASSERT1(ssl);
  792. return SSL_METHOD_CALL(get_verify_result, ssl);
  793. }
  794. /**
  795. * @brief get the SSL verifying depth of the SSL context
  796. */
  797. int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
  798. {
  799. SSL_ASSERT1(ctx);
  800. return ctx->param.depth;
  801. }
  802. /**
  803. * @brief set the SSL verify depth of the SSL context
  804. */
  805. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
  806. {
  807. SSL_ASSERT3(ctx);
  808. ctx->param.depth = depth;
  809. }
  810. /**
  811. * @brief get the SSL verifying depth of the SSL
  812. */
  813. int SSL_get_verify_depth(const SSL *ssl)
  814. {
  815. SSL_ASSERT1(ssl);
  816. return ssl->param.depth;
  817. }
  818. /**
  819. * @brief set the SSL verify depth of the SSL
  820. */
  821. void SSL_set_verify_depth(SSL *ssl, int depth)
  822. {
  823. SSL_ASSERT3(ssl);
  824. ssl->param.depth = depth;
  825. }
  826. /**
  827. * @brief set the SSL context verifying of the SSL context
  828. */
  829. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode, int (*verify_callback)(int, X509_STORE_CTX *))
  830. {
  831. SSL_ASSERT3(ctx);
  832. ctx->verify_mode = mode;
  833. ctx->default_verify_callback = verify_callback;
  834. }
  835. /**
  836. * @brief set the SSL verifying of the SSL context
  837. */
  838. void SSL_set_verify(SSL *ssl, int mode, int (*verify_callback)(int, X509_STORE_CTX *))
  839. {
  840. SSL_ASSERT3(ssl);
  841. ssl->verify_mode = mode;
  842. ssl->verify_callback = verify_callback;
  843. }
  844. void ERR_error_string_n(unsigned long e, char *buf, size_t len)
  845. {
  846. lws_strncpy(buf, "unknown", len);
  847. }
  848. void ERR_free_strings(void)
  849. {
  850. }
  851. char *ERR_error_string(unsigned long e, char *buf)
  852. {
  853. if (!buf)
  854. return "unknown";
  855. switch(e) {
  856. case X509_V_ERR_INVALID_CA:
  857. strcpy(buf, "CA is not trusted");
  858. break;
  859. case X509_V_ERR_HOSTNAME_MISMATCH:
  860. strcpy(buf, "Hostname mismatch");
  861. break;
  862. case X509_V_ERR_CA_KEY_TOO_SMALL:
  863. strcpy(buf, "CA key too small");
  864. break;
  865. case X509_V_ERR_CA_MD_TOO_WEAK:
  866. strcpy(buf, "MD key too weak");
  867. break;
  868. case X509_V_ERR_CERT_NOT_YET_VALID:
  869. strcpy(buf, "Cert from the future");
  870. break;
  871. case X509_V_ERR_CERT_HAS_EXPIRED:
  872. strcpy(buf, "Cert expired");
  873. break;
  874. default:
  875. strcpy(buf, "unknown");
  876. break;
  877. }
  878. return buf;
  879. }
  880. void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx)
  881. {
  882. return NULL;
  883. }
  884. /*
  885. * Openssl wants the valid protocol names supplied like this:
  886. *
  887. * (unsigned char *)"\x02h2\x08http/1.1", 6 + 9
  888. *
  889. * Mbedtls wants this:
  890. *
  891. * Pointer to a NULL-terminated list of supported protocols, in decreasing
  892. * preference order. The pointer to the list is recorded by the library for
  893. * later reference as required, so the lifetime of the table must be at least
  894. * as long as the lifetime of the SSL configuration structure.
  895. *
  896. * So accept the OpenSSL style and convert to mbedtls style
  897. */
  898. static void
  899. _openssl_alpn_to_mbedtls(struct alpn_ctx *ac, char ***palpn_protos)
  900. {
  901. unsigned char *p = ac->data, *q;
  902. unsigned char len;
  903. char **alpn_protos;
  904. int count = 0;
  905. /* find out how many entries he gave us */
  906. if (ac->len) {
  907. len = *p++;
  908. if (len)
  909. count++;
  910. while (p - ac->data < ac->len) {
  911. if (len--) {
  912. p++;
  913. continue;
  914. }
  915. len = *p++;
  916. if (!len)
  917. break;
  918. count++;
  919. }
  920. }
  921. if (!count)
  922. return;
  923. /* allocate space for count + 1 pointers and the data afterwards */
  924. alpn_protos = ssl_mem_zalloc((count + 1) * sizeof(char *) + ac->len + 1);
  925. if (!alpn_protos)
  926. return;
  927. *palpn_protos = alpn_protos;
  928. /* convert to mbedtls format */
  929. q = (unsigned char *)alpn_protos + (count + 1) * sizeof(char *);
  930. p = ac->data;
  931. count = 0;
  932. len = *p++;
  933. alpn_protos[count] = (char *)q;
  934. while (p - ac->data < ac->len) {
  935. if (len--) {
  936. *q++ = *p++;
  937. continue;
  938. }
  939. *q++ = '\0';
  940. count++;
  941. len = *p++;
  942. alpn_protos[count] = (char *)q;
  943. if (!len)
  944. break;
  945. }
  946. if (!len) {
  947. *q++ = '\0';
  948. count++;
  949. /* len = *p++; */
  950. alpn_protos[count] = (char *)q;
  951. }
  952. alpn_protos[count] = NULL; /* last pointer ends list with NULL */
  953. }
  954. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx, next_proto_cb cb, void *arg)
  955. {
  956. struct alpn_ctx *ac = arg;
  957. ctx->alpn_cb = cb;
  958. _openssl_alpn_to_mbedtls(ac, (char ***)&ctx->alpn_protos);
  959. }
  960. void SSL_set_alpn_select_cb(SSL *ssl, void *arg)
  961. {
  962. struct alpn_ctx *ac = arg;
  963. _openssl_alpn_to_mbedtls(ac, (char ***)&ssl->alpn_protos);
  964. _ssl_set_alpn_list(ssl);
  965. }