ssl_lib.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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. #define SSL_SEND_DATA_MAX_LENGTH 1460
  20. /**
  21. * @brief Discover whether the current connection is in the error state
  22. */
  23. int ossl_statem_in_error(const SSL *ssl)
  24. {
  25. if (ssl->statem.state == MSG_FLOW_ERROR)
  26. return 1;
  27. return 0;
  28. }
  29. /**
  30. * @brief get the SSL specifical statement
  31. */
  32. int SSL_want(const SSL *ssl)
  33. {
  34. return ssl->rwstate;
  35. }
  36. /**
  37. * @brief check if SSL want nothing
  38. */
  39. int SSL_want_nothing(const SSL *ssl)
  40. {
  41. return (SSL_want(ssl) == SSL_NOTHING);
  42. }
  43. /**
  44. * @brief check if SSL want to read
  45. */
  46. int SSL_want_read(const SSL *ssl)
  47. {
  48. return (SSL_want(ssl) == SSL_READING);
  49. }
  50. /**
  51. * @brief check if SSL want to write
  52. */
  53. int SSL_want_write(const SSL *ssl)
  54. {
  55. return (SSL_want(ssl) == SSL_WRITING);
  56. }
  57. /**
  58. * @brief check if SSL want to lookup X509 certification
  59. */
  60. int SSL_want_x509_lookup(const SSL *ssl)
  61. {
  62. return (SSL_want(ssl) == SSL_WRITING);
  63. }
  64. /**
  65. * @brief get SSL error code
  66. */
  67. int SSL_get_error(const SSL *ssl, int ret_code)
  68. {
  69. int ret = SSL_ERROR_SYSCALL;
  70. SSL_ASSERT(ssl);
  71. if (ret_code > 0)
  72. ret = SSL_ERROR_NONE;
  73. else if (ret_code < 0)
  74. {
  75. if (SSL_want_read(ssl))
  76. ret = SSL_ERROR_WANT_READ;
  77. else if (SSL_want_write(ssl))
  78. ret = SSL_ERROR_WANT_WRITE;
  79. else
  80. ret = SSL_ERROR_SYSCALL; //unknown
  81. }
  82. else // ret_code == 0
  83. {
  84. if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN)
  85. ret = SSL_ERROR_ZERO_RETURN;
  86. else
  87. ret = SSL_ERROR_SYSCALL;
  88. }
  89. return ret;
  90. }
  91. /**
  92. * @brief get the SSL state
  93. */
  94. OSSL_HANDSHAKE_STATE SSL_get_state(const SSL *ssl)
  95. {
  96. OSSL_HANDSHAKE_STATE state;
  97. SSL_ASSERT(ssl);
  98. state = SSL_METHOD_CALL(get_state, ssl);
  99. return state;
  100. }
  101. /**
  102. * @brief create a new SSL session object
  103. */
  104. SSL_SESSION* SSL_SESSION_new(void)
  105. {
  106. SSL_SESSION *session;
  107. session = ssl_mem_zalloc(sizeof(SSL_SESSION));
  108. if (!session)
  109. SSL_RET(failed1, "ssl_mem_zalloc\n");
  110. session->peer = X509_new();
  111. if (!session->peer)
  112. SSL_RET(failed2, "X509_new\n");
  113. return session;
  114. failed2:
  115. ssl_mem_free(session);
  116. failed1:
  117. return NULL;
  118. }
  119. /**
  120. * @brief free a new SSL session object
  121. */
  122. void SSL_SESSION_free(SSL_SESSION *session)
  123. {
  124. X509_free(session->peer);
  125. ssl_mem_free(session);
  126. }
  127. /**
  128. * @brief create a SSL context
  129. */
  130. SSL_CTX* SSL_CTX_new(const SSL_METHOD *method)
  131. {
  132. SSL_CTX *ctx;
  133. CERT *cert;
  134. X509 *client_ca;
  135. if (!method) SSL_RET(go_failed1, "method:NULL\n");
  136. client_ca = X509_new();
  137. if (!client_ca)
  138. SSL_RET(go_failed1, "X509_new\n");
  139. cert = ssl_cert_new();
  140. if (!cert)
  141. SSL_RET(go_failed2, "ssl_cert_new\n");
  142. ctx = (SSL_CTX *)ssl_mem_zalloc(sizeof(SSL_CTX));
  143. if (!ctx)
  144. SSL_RET(go_failed3, "ssl_mem_zalloc:ctx\n");
  145. ctx->method = method;
  146. ctx->client_CA = client_ca;
  147. ctx->cert = cert;
  148. ctx->version = method->version;
  149. return ctx;
  150. go_failed3:
  151. ssl_cert_free(cert);
  152. go_failed2:
  153. X509_free(client_ca);
  154. go_failed1:
  155. return NULL;
  156. }
  157. /**
  158. * @brief free a SSL context
  159. */
  160. void SSL_CTX_free(SSL_CTX* ctx)
  161. {
  162. SSL_ASSERT(ctx);
  163. ssl_cert_free(ctx->cert);
  164. X509_free(ctx->client_CA);
  165. ssl_mem_free(ctx);
  166. }
  167. /**
  168. * @brief set the SSL context version
  169. */
  170. int SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth)
  171. {
  172. SSL_ASSERT(ctx);
  173. SSL_ASSERT(meth);
  174. ctx->method = meth;
  175. ctx->version = meth->version;
  176. return 1;
  177. }
  178. /**
  179. * @brief get the SSL context current method
  180. */
  181. const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx)
  182. {
  183. SSL_ASSERT(ctx);
  184. return ctx->method;
  185. }
  186. /**
  187. * @brief create a SSL
  188. */
  189. SSL *SSL_new(SSL_CTX *ctx)
  190. {
  191. int ret = 0;
  192. SSL *ssl;
  193. if (!ctx)
  194. SSL_RET(failed1, "ctx:NULL\n");
  195. ssl = (SSL *)ssl_mem_zalloc(sizeof(SSL));
  196. if (!ssl)
  197. SSL_RET(failed1, "ssl_mem_zalloc\n");
  198. ssl->session = SSL_SESSION_new();
  199. if (!ssl->session)
  200. SSL_RET(failed2, "SSL_SESSION_new\n");
  201. ssl->cert = __ssl_cert_new(ctx->cert);
  202. if (!ssl->cert)
  203. SSL_RET(failed3, "__ssl_cert_new\n");
  204. ssl->client_CA = __X509_new(ctx->client_CA);
  205. if (!ssl->client_CA)
  206. SSL_RET(failed4, "__X509_new\n");
  207. ssl->ctx = ctx;
  208. ssl->method = ctx->method;
  209. ssl->version = ctx->version;
  210. ssl->options = ctx->options;
  211. ssl->verify_mode = ctx->verify_mode;
  212. ret = SSL_METHOD_CALL(new, ssl);
  213. if (ret)
  214. SSL_RET(failed5, "ssl_new\n");
  215. ssl->rwstate = SSL_NOTHING;
  216. return ssl;
  217. failed5:
  218. X509_free(ssl->client_CA);
  219. failed4:
  220. ssl_cert_free(ssl->cert);
  221. failed3:
  222. SSL_SESSION_free(ssl->session);
  223. failed2:
  224. ssl_mem_free(ssl);
  225. failed1:
  226. return NULL;
  227. }
  228. /**
  229. * @brief free the SSL
  230. */
  231. void SSL_free(SSL *ssl)
  232. {
  233. SSL_ASSERT(ssl);
  234. SSL_METHOD_CALL(free, ssl);
  235. X509_free(ssl->client_CA);
  236. ssl_cert_free(ssl->cert);
  237. SSL_SESSION_free(ssl->session);
  238. ssl_mem_free(ssl);
  239. }
  240. /**
  241. * @brief perform the SSL handshake
  242. */
  243. int SSL_do_handshake(SSL *ssl)
  244. {
  245. int ret;
  246. SSL_ASSERT(ssl);
  247. ret = SSL_METHOD_CALL(handshake, ssl);
  248. return ret;
  249. }
  250. /**
  251. * @brief connect to the remote SSL server
  252. */
  253. int SSL_connect(SSL *ssl)
  254. {
  255. SSL_ASSERT(ssl);
  256. return SSL_do_handshake(ssl);
  257. }
  258. /**
  259. * @brief accept the remote connection
  260. */
  261. int SSL_accept(SSL *ssl)
  262. {
  263. SSL_ASSERT(ssl);
  264. return SSL_do_handshake(ssl);
  265. }
  266. /**
  267. * @brief shutdown the connection
  268. */
  269. int SSL_shutdown(SSL *ssl)
  270. {
  271. int ret;
  272. SSL_ASSERT(ssl);
  273. if (SSL_get_state(ssl) != TLS_ST_OK) return 1;
  274. ret = SSL_METHOD_CALL(shutdown, ssl);
  275. return ret;
  276. }
  277. /**
  278. * @brief reset the SSL
  279. */
  280. int SSL_clear(SSL *ssl)
  281. {
  282. int ret;
  283. SSL_ASSERT(ssl);
  284. ret = SSL_shutdown(ssl);
  285. if (1 != ret)
  286. SSL_ERR(0, go_failed1, "SSL_shutdown\n");
  287. SSL_METHOD_CALL(free, ssl);
  288. ret = SSL_METHOD_CALL(new, ssl);
  289. if (!ret)
  290. SSL_ERR(0, go_failed1, "ssl_new\n");
  291. return 1;
  292. go_failed1:
  293. return ret;
  294. }
  295. /**
  296. * @brief read data from to remote
  297. */
  298. int SSL_read(SSL *ssl, void *buffer, int len)
  299. {
  300. int ret;
  301. SSL_ASSERT(ssl);
  302. SSL_ASSERT(buffer);
  303. SSL_ASSERT(len);
  304. ssl->rwstate = SSL_READING;
  305. ret = SSL_METHOD_CALL(read, ssl, buffer, len);
  306. if (ret == len)
  307. ssl->rwstate = SSL_NOTHING;
  308. return ret;
  309. }
  310. /**
  311. * @brief send the data to remote
  312. */
  313. int SSL_write(SSL *ssl, const void *buffer, int len)
  314. {
  315. int ret;
  316. int send_bytes;
  317. const unsigned char *pbuf;
  318. SSL_ASSERT(ssl);
  319. SSL_ASSERT(buffer);
  320. SSL_ASSERT(len);
  321. ssl->rwstate = SSL_WRITING;
  322. send_bytes = len;
  323. pbuf = (const unsigned char *)buffer;
  324. do {
  325. int bytes;
  326. if (send_bytes > SSL_SEND_DATA_MAX_LENGTH)
  327. bytes = SSL_SEND_DATA_MAX_LENGTH;
  328. else
  329. bytes = send_bytes;
  330. ret = SSL_METHOD_CALL(send, ssl, buffer, bytes);
  331. if (ret > 0) {
  332. pbuf += ret;
  333. send_bytes -= ret;
  334. }
  335. } while (ret > 0 && send_bytes);
  336. if (ret >= 0) {
  337. ret = len - send_bytes;
  338. ssl->rwstate = SSL_NOTHING;
  339. } else
  340. ret = -1;
  341. return ret;
  342. }
  343. /**
  344. * @brief get SSL context of the SSL
  345. */
  346. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
  347. {
  348. SSL_ASSERT(ssl);
  349. return ssl->ctx;
  350. }
  351. /**
  352. * @brief get the SSL current method
  353. */
  354. const SSL_METHOD *SSL_get_ssl_method(SSL *ssl)
  355. {
  356. SSL_ASSERT(ssl);
  357. return ssl->method;
  358. }
  359. /**
  360. * @brief set the SSL method
  361. */
  362. int SSL_set_ssl_method(SSL *ssl, const SSL_METHOD *method)
  363. {
  364. int ret;
  365. SSL_ASSERT(ssl);
  366. SSL_ASSERT(method);
  367. if (ssl->version != method->version) {
  368. ret = SSL_shutdown(ssl);
  369. if (1 != ret)
  370. SSL_ERR(0, go_failed1, "SSL_shutdown\n");
  371. SSL_METHOD_CALL(free, ssl);
  372. ssl->method = method;
  373. ret = SSL_METHOD_CALL(new, ssl);
  374. if (!ret)
  375. SSL_ERR(0, go_failed1, "ssl_new\n");
  376. } else {
  377. ssl->method = method;
  378. }
  379. return 1;
  380. go_failed1:
  381. return ret;
  382. }
  383. /**
  384. * @brief get SSL shutdown mode
  385. */
  386. int SSL_get_shutdown(const SSL *ssl)
  387. {
  388. SSL_ASSERT(ssl);
  389. return ssl->shutdown;
  390. }
  391. /**
  392. * @brief set SSL shutdown mode
  393. */
  394. void SSL_set_shutdown(SSL *ssl, int mode)
  395. {
  396. SSL_ASSERT(ssl);
  397. ssl->shutdown = mode;
  398. }
  399. /**
  400. * @brief get the number of the bytes to be read
  401. */
  402. int SSL_pending(const SSL *ssl)
  403. {
  404. int ret;
  405. SSL_ASSERT(ssl);
  406. ret = SSL_METHOD_CALL(pending, ssl);
  407. return ret;
  408. }
  409. /**
  410. * @brief check if some data can be read
  411. */
  412. int SSL_has_pending(const SSL *ssl)
  413. {
  414. int ret;
  415. SSL_ASSERT(ssl);
  416. if (SSL_pending(ssl))
  417. ret = 1;
  418. else
  419. ret = 0;
  420. return ret;
  421. }
  422. /**
  423. * @brief clear the SSL context option bit of "op"
  424. */
  425. unsigned long SSL_CTX_clear_options(SSL_CTX *ctx, unsigned long op)
  426. {
  427. return ctx->options &= ~op;
  428. }
  429. /**
  430. * @brief get the SSL context option
  431. */
  432. unsigned long SSL_CTX_get_options(SSL_CTX *ctx)
  433. {
  434. return ctx->options;
  435. }
  436. /**
  437. * @brief set the option of the SSL context
  438. */
  439. unsigned long SSL_CTX_set_options(SSL_CTX *ctx, unsigned long opt)
  440. {
  441. return ctx->options |= opt;
  442. }
  443. /**
  444. * @brief clear SSL option
  445. */
  446. unsigned long SSL_clear_options(SSL *ssl, unsigned long op)
  447. {
  448. SSL_ASSERT(ssl);
  449. return ssl->options & ~op;
  450. }
  451. /**
  452. * @brief get SSL option
  453. */
  454. unsigned long SSL_get_options(SSL *ssl)
  455. {
  456. SSL_ASSERT(ssl);
  457. return ssl->options;
  458. }
  459. /**
  460. * @brief clear SSL option
  461. */
  462. unsigned long SSL_set_options(SSL *ssl, unsigned long op)
  463. {
  464. SSL_ASSERT(ssl);
  465. return ssl->options |= op;
  466. }
  467. /**
  468. * @brief get the socket handle of the SSL
  469. */
  470. int SSL_get_fd(const SSL *ssl)
  471. {
  472. int ret;
  473. SSL_ASSERT(ssl);
  474. ret = SSL_METHOD_CALL(get_fd, ssl, 0);
  475. return ret;
  476. }
  477. /**
  478. * @brief get the read only socket handle of the SSL
  479. */
  480. int SSL_get_rfd(const SSL *ssl)
  481. {
  482. int ret;
  483. SSL_ASSERT(ssl);
  484. ret = SSL_METHOD_CALL(get_fd, ssl, 0);
  485. return ret;
  486. }
  487. /**
  488. * @brief get the write only socket handle of the SSL
  489. */
  490. int SSL_get_wfd(const SSL *ssl)
  491. {
  492. int ret;
  493. SSL_ASSERT(ssl);
  494. ret = SSL_METHOD_CALL(get_fd, ssl, 0);
  495. return ret;
  496. }
  497. /**
  498. * @brief bind the socket file description into the SSL
  499. */
  500. int SSL_set_fd(SSL *ssl, int fd)
  501. {
  502. SSL_ASSERT(ssl);
  503. SSL_ASSERT(fd >= 0);
  504. SSL_METHOD_CALL(set_fd, ssl, fd, 0);
  505. return 1;
  506. }
  507. /**
  508. * @brief bind the read only socket file description into the SSL
  509. */
  510. int SSL_set_rfd(SSL *ssl, int fd)
  511. {
  512. SSL_ASSERT(ssl);
  513. SSL_ASSERT(fd >= 0);
  514. SSL_METHOD_CALL(set_fd, ssl, fd, 0);
  515. return 1;
  516. }
  517. /**
  518. * @brief bind the write only socket file description into the SSL
  519. */
  520. int SSL_set_wfd(SSL *ssl, int fd)
  521. {
  522. SSL_ASSERT(ssl);
  523. SSL_ASSERT(fd >= 0);
  524. SSL_METHOD_CALL(set_fd, ssl, fd, 0);
  525. return 1;
  526. }
  527. /**
  528. * @brief get SSL version
  529. */
  530. int SSL_version(const SSL *ssl)
  531. {
  532. SSL_ASSERT(ssl);
  533. return ssl->version;
  534. }
  535. /**
  536. * @brief get the SSL version string
  537. */
  538. static const char* ssl_protocol_to_string(int version)
  539. {
  540. const char *str;
  541. if (version == TLS1_2_VERSION)
  542. str = "TLSv1.2";
  543. else if (version == TLS1_1_VERSION)
  544. str = "TLSv1.1";
  545. else if (version == TLS1_VERSION)
  546. str = "TLSv1";
  547. else if (version == SSL3_VERSION)
  548. str = "SSLv3";
  549. else
  550. str = "unknown";
  551. return str;
  552. }
  553. /**
  554. * @brief get the SSL current version
  555. */
  556. const char *SSL_get_version(const SSL *ssl)
  557. {
  558. SSL_ASSERT(ssl);
  559. return ssl_protocol_to_string(SSL_version(ssl));
  560. }
  561. /**
  562. * @brief get alert description string
  563. */
  564. const char* SSL_alert_desc_string(int value)
  565. {
  566. const char *str;
  567. switch (value & 0xff)
  568. {
  569. case SSL3_AD_CLOSE_NOTIFY:
  570. str = "CN";
  571. break;
  572. case SSL3_AD_UNEXPECTED_MESSAGE:
  573. str = "UM";
  574. break;
  575. case SSL3_AD_BAD_RECORD_MAC:
  576. str = "BM";
  577. break;
  578. case SSL3_AD_DECOMPRESSION_FAILURE:
  579. str = "DF";
  580. break;
  581. case SSL3_AD_HANDSHAKE_FAILURE:
  582. str = "HF";
  583. break;
  584. case SSL3_AD_NO_CERTIFICATE:
  585. str = "NC";
  586. break;
  587. case SSL3_AD_BAD_CERTIFICATE:
  588. str = "BC";
  589. break;
  590. case SSL3_AD_UNSUPPORTED_CERTIFICATE:
  591. str = "UC";
  592. break;
  593. case SSL3_AD_CERTIFICATE_REVOKED:
  594. str = "CR";
  595. break;
  596. case SSL3_AD_CERTIFICATE_EXPIRED:
  597. str = "CE";
  598. break;
  599. case SSL3_AD_CERTIFICATE_UNKNOWN:
  600. str = "CU";
  601. break;
  602. case SSL3_AD_ILLEGAL_PARAMETER:
  603. str = "IP";
  604. break;
  605. case TLS1_AD_DECRYPTION_FAILED:
  606. str = "DC";
  607. break;
  608. case TLS1_AD_RECORD_OVERFLOW:
  609. str = "RO";
  610. break;
  611. case TLS1_AD_UNKNOWN_CA:
  612. str = "CA";
  613. break;
  614. case TLS1_AD_ACCESS_DENIED:
  615. str = "AD";
  616. break;
  617. case TLS1_AD_DECODE_ERROR:
  618. str = "DE";
  619. break;
  620. case TLS1_AD_DECRYPT_ERROR:
  621. str = "CY";
  622. break;
  623. case TLS1_AD_EXPORT_RESTRICTION:
  624. str = "ER";
  625. break;
  626. case TLS1_AD_PROTOCOL_VERSION:
  627. str = "PV";
  628. break;
  629. case TLS1_AD_INSUFFICIENT_SECURITY:
  630. str = "IS";
  631. break;
  632. case TLS1_AD_INTERNAL_ERROR:
  633. str = "IE";
  634. break;
  635. case TLS1_AD_USER_CANCELLED:
  636. str = "US";
  637. break;
  638. case TLS1_AD_NO_RENEGOTIATION:
  639. str = "NR";
  640. break;
  641. case TLS1_AD_UNSUPPORTED_EXTENSION:
  642. str = "UE";
  643. break;
  644. case TLS1_AD_CERTIFICATE_UNOBTAINABLE:
  645. str = "CO";
  646. break;
  647. case TLS1_AD_UNRECOGNIZED_NAME:
  648. str = "UN";
  649. break;
  650. case TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
  651. str = "BR";
  652. break;
  653. case TLS1_AD_BAD_CERTIFICATE_HASH_VALUE:
  654. str = "BH";
  655. break;
  656. case TLS1_AD_UNKNOWN_PSK_IDENTITY:
  657. str = "UP";
  658. break;
  659. default:
  660. str = "UK";
  661. break;
  662. }
  663. return str;
  664. }
  665. /**
  666. * @brief get alert description long string
  667. */
  668. const char* SSL_alert_desc_string_long(int value)
  669. {
  670. const char *str;
  671. switch (value & 0xff)
  672. {
  673. case SSL3_AD_CLOSE_NOTIFY:
  674. str = "close notify";
  675. break;
  676. case SSL3_AD_UNEXPECTED_MESSAGE:
  677. str = "unexpected_message";
  678. break;
  679. case SSL3_AD_BAD_RECORD_MAC:
  680. str = "bad record mac";
  681. break;
  682. case SSL3_AD_DECOMPRESSION_FAILURE:
  683. str = "decompression failure";
  684. break;
  685. case SSL3_AD_HANDSHAKE_FAILURE:
  686. str = "handshake failure";
  687. break;
  688. case SSL3_AD_NO_CERTIFICATE:
  689. str = "no certificate";
  690. break;
  691. case SSL3_AD_BAD_CERTIFICATE:
  692. str = "bad certificate";
  693. break;
  694. case SSL3_AD_UNSUPPORTED_CERTIFICATE:
  695. str = "unsupported certificate";
  696. break;
  697. case SSL3_AD_CERTIFICATE_REVOKED:
  698. str = "certificate revoked";
  699. break;
  700. case SSL3_AD_CERTIFICATE_EXPIRED:
  701. str = "certificate expired";
  702. break;
  703. case SSL3_AD_CERTIFICATE_UNKNOWN:
  704. str = "certificate unknown";
  705. break;
  706. case SSL3_AD_ILLEGAL_PARAMETER:
  707. str = "illegal parameter";
  708. break;
  709. case TLS1_AD_DECRYPTION_FAILED:
  710. str = "decryption failed";
  711. break;
  712. case TLS1_AD_RECORD_OVERFLOW:
  713. str = "record overflow";
  714. break;
  715. case TLS1_AD_UNKNOWN_CA:
  716. str = "unknown CA";
  717. break;
  718. case TLS1_AD_ACCESS_DENIED:
  719. str = "access denied";
  720. break;
  721. case TLS1_AD_DECODE_ERROR:
  722. str = "decode error";
  723. break;
  724. case TLS1_AD_DECRYPT_ERROR:
  725. str = "decrypt error";
  726. break;
  727. case TLS1_AD_EXPORT_RESTRICTION:
  728. str = "export restriction";
  729. break;
  730. case TLS1_AD_PROTOCOL_VERSION:
  731. str = "protocol version";
  732. break;
  733. case TLS1_AD_INSUFFICIENT_SECURITY:
  734. str = "insufficient security";
  735. break;
  736. case TLS1_AD_INTERNAL_ERROR:
  737. str = "internal error";
  738. break;
  739. case TLS1_AD_USER_CANCELLED:
  740. str = "user canceled";
  741. break;
  742. case TLS1_AD_NO_RENEGOTIATION:
  743. str = "no renegotiation";
  744. break;
  745. case TLS1_AD_UNSUPPORTED_EXTENSION:
  746. str = "unsupported extension";
  747. break;
  748. case TLS1_AD_CERTIFICATE_UNOBTAINABLE:
  749. str = "certificate unobtainable";
  750. break;
  751. case TLS1_AD_UNRECOGNIZED_NAME:
  752. str = "unrecognized name";
  753. break;
  754. case TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
  755. str = "bad certificate status response";
  756. break;
  757. case TLS1_AD_BAD_CERTIFICATE_HASH_VALUE:
  758. str = "bad certificate hash value";
  759. break;
  760. case TLS1_AD_UNKNOWN_PSK_IDENTITY:
  761. str = "unknown PSK identity";
  762. break;
  763. default:
  764. str = "unknown";
  765. break;
  766. }
  767. return str;
  768. }
  769. /**
  770. * @brief get alert type string
  771. */
  772. const char *SSL_alert_type_string(int value)
  773. {
  774. const char *str;
  775. switch (value >> 8)
  776. {
  777. case SSL3_AL_WARNING:
  778. str = "W";
  779. break;
  780. case SSL3_AL_FATAL:
  781. str = "F";
  782. break;
  783. default:
  784. str = "U";
  785. break;
  786. }
  787. return str;
  788. }
  789. /**
  790. * @brief get alert type long string
  791. */
  792. const char *SSL_alert_type_string_long(int value)
  793. {
  794. const char *str;
  795. switch (value >> 8)
  796. {
  797. case SSL3_AL_WARNING:
  798. str = "warning";
  799. break;
  800. case SSL3_AL_FATAL:
  801. str = "fatal";
  802. break;
  803. default:
  804. str = "unknown";
  805. break;
  806. }
  807. return str;
  808. }
  809. /**
  810. * @brief get the state string where SSL is reading
  811. */
  812. const char *SSL_rstate_string(SSL *ssl)
  813. {
  814. const char *str;
  815. SSL_ASSERT(ssl);
  816. switch (ssl->rlayer.rstate)
  817. {
  818. case SSL_ST_READ_HEADER:
  819. str = "RH";
  820. break;
  821. case SSL_ST_READ_BODY:
  822. str = "RB";
  823. break;
  824. case SSL_ST_READ_DONE:
  825. str = "RD";
  826. break;
  827. default:
  828. str = "unknown";
  829. break;
  830. }
  831. return str;
  832. }
  833. /**
  834. * @brief get the statement long string where SSL is reading
  835. */
  836. const char *SSL_rstate_string_long(SSL *ssl)
  837. {
  838. const char *str = "unknown";
  839. SSL_ASSERT(ssl);
  840. switch (ssl->rlayer.rstate)
  841. {
  842. case SSL_ST_READ_HEADER:
  843. str = "read header";
  844. break;
  845. case SSL_ST_READ_BODY:
  846. str = "read body";
  847. break;
  848. case SSL_ST_READ_DONE:
  849. str = "read done";
  850. break;
  851. default:
  852. break;
  853. }
  854. return str;
  855. }
  856. /**
  857. * @brief get SSL statement string
  858. */
  859. char *SSL_state_string(const SSL *ssl)
  860. {
  861. char *str = "UNKWN ";
  862. SSL_ASSERT(ssl);
  863. if (ossl_statem_in_error(ssl))
  864. str = "SSLERR";
  865. else
  866. {
  867. switch (SSL_get_state(ssl))
  868. {
  869. case TLS_ST_BEFORE:
  870. str = "PINIT ";
  871. break;
  872. case TLS_ST_OK:
  873. str = "SSLOK ";
  874. break;
  875. case TLS_ST_CW_CLNT_HELLO:
  876. str = "TWCH";
  877. break;
  878. case TLS_ST_CR_SRVR_HELLO:
  879. str = "TRSH";
  880. break;
  881. case TLS_ST_CR_CERT:
  882. str = "TRSC";
  883. break;
  884. case TLS_ST_CR_KEY_EXCH:
  885. str = "TRSKE";
  886. break;
  887. case TLS_ST_CR_CERT_REQ:
  888. str = "TRCR";
  889. break;
  890. case TLS_ST_CR_SRVR_DONE:
  891. str = "TRSD";
  892. break;
  893. case TLS_ST_CW_CERT:
  894. str = "TWCC";
  895. break;
  896. case TLS_ST_CW_KEY_EXCH:
  897. str = "TWCKE";
  898. break;
  899. case TLS_ST_CW_CERT_VRFY:
  900. str = "TWCV";
  901. break;
  902. case TLS_ST_SW_CHANGE:
  903. case TLS_ST_CW_CHANGE:
  904. str = "TWCCS";
  905. break;
  906. case TLS_ST_SW_FINISHED:
  907. case TLS_ST_CW_FINISHED:
  908. str = "TWFIN";
  909. break;
  910. case TLS_ST_SR_CHANGE:
  911. case TLS_ST_CR_CHANGE:
  912. str = "TRCCS";
  913. break;
  914. case TLS_ST_SR_FINISHED:
  915. case TLS_ST_CR_FINISHED:
  916. str = "TRFIN";
  917. break;
  918. case TLS_ST_SW_HELLO_REQ:
  919. str = "TWHR";
  920. break;
  921. case TLS_ST_SR_CLNT_HELLO:
  922. str = "TRCH";
  923. break;
  924. case TLS_ST_SW_SRVR_HELLO:
  925. str = "TWSH";
  926. break;
  927. case TLS_ST_SW_CERT:
  928. str = "TWSC";
  929. break;
  930. case TLS_ST_SW_KEY_EXCH:
  931. str = "TWSKE";
  932. break;
  933. case TLS_ST_SW_CERT_REQ:
  934. str = "TWCR";
  935. break;
  936. case TLS_ST_SW_SRVR_DONE:
  937. str = "TWSD";
  938. break;
  939. case TLS_ST_SR_CERT:
  940. str = "TRCC";
  941. break;
  942. case TLS_ST_SR_KEY_EXCH:
  943. str = "TRCKE";
  944. break;
  945. case TLS_ST_SR_CERT_VRFY:
  946. str = "TRCV";
  947. break;
  948. case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
  949. str = "DRCHV";
  950. break;
  951. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  952. str = "DWCHV";
  953. break;
  954. default:
  955. break;
  956. }
  957. }
  958. return str;
  959. }
  960. /**
  961. * @brief get SSL statement long string
  962. */
  963. char *SSL_state_string_long(const SSL *ssl)
  964. {
  965. char *str = "UNKWN ";
  966. SSL_ASSERT(ssl);
  967. if (ossl_statem_in_error(ssl))
  968. str = "SSLERR";
  969. else
  970. {
  971. switch (SSL_get_state(ssl))
  972. {
  973. case TLS_ST_BEFORE:
  974. str = "before SSL initialization";
  975. break;
  976. case TLS_ST_OK:
  977. str = "SSL negotiation finished successfully";
  978. break;
  979. case TLS_ST_CW_CLNT_HELLO:
  980. str = "SSLv3/TLS write client hello";
  981. break;
  982. case TLS_ST_CR_SRVR_HELLO:
  983. str = "SSLv3/TLS read server hello";
  984. break;
  985. case TLS_ST_CR_CERT:
  986. str = "SSLv3/TLS read server certificate";
  987. break;
  988. case TLS_ST_CR_KEY_EXCH:
  989. str = "SSLv3/TLS read server key exchange";
  990. break;
  991. case TLS_ST_CR_CERT_REQ:
  992. str = "SSLv3/TLS read server certificate request";
  993. break;
  994. case TLS_ST_CR_SESSION_TICKET:
  995. str = "SSLv3/TLS read server session ticket";
  996. break;
  997. case TLS_ST_CR_SRVR_DONE:
  998. str = "SSLv3/TLS read server done";
  999. break;
  1000. case TLS_ST_CW_CERT:
  1001. str = "SSLv3/TLS write client certificate";
  1002. break;
  1003. case TLS_ST_CW_KEY_EXCH:
  1004. str = "SSLv3/TLS write client key exchange";
  1005. break;
  1006. case TLS_ST_CW_CERT_VRFY:
  1007. str = "SSLv3/TLS write certificate verify";
  1008. break;
  1009. case TLS_ST_CW_CHANGE:
  1010. case TLS_ST_SW_CHANGE:
  1011. str = "SSLv3/TLS write change cipher spec";
  1012. break;
  1013. case TLS_ST_CW_FINISHED:
  1014. case TLS_ST_SW_FINISHED:
  1015. str = "SSLv3/TLS write finished";
  1016. break;
  1017. case TLS_ST_CR_CHANGE:
  1018. case TLS_ST_SR_CHANGE:
  1019. str = "SSLv3/TLS read change cipher spec";
  1020. break;
  1021. case TLS_ST_CR_FINISHED:
  1022. case TLS_ST_SR_FINISHED:
  1023. str = "SSLv3/TLS read finished";
  1024. break;
  1025. case TLS_ST_SR_CLNT_HELLO:
  1026. str = "SSLv3/TLS read client hello";
  1027. break;
  1028. case TLS_ST_SW_HELLO_REQ:
  1029. str = "SSLv3/TLS write hello request";
  1030. break;
  1031. case TLS_ST_SW_SRVR_HELLO:
  1032. str = "SSLv3/TLS write server hello";
  1033. break;
  1034. case TLS_ST_SW_CERT:
  1035. str = "SSLv3/TLS write certificate";
  1036. break;
  1037. case TLS_ST_SW_KEY_EXCH:
  1038. str = "SSLv3/TLS write key exchange";
  1039. break;
  1040. case TLS_ST_SW_CERT_REQ:
  1041. str = "SSLv3/TLS write certificate request";
  1042. break;
  1043. case TLS_ST_SW_SESSION_TICKET:
  1044. str = "SSLv3/TLS write session ticket";
  1045. break;
  1046. case TLS_ST_SW_SRVR_DONE:
  1047. str = "SSLv3/TLS write server done";
  1048. break;
  1049. case TLS_ST_SR_CERT:
  1050. str = "SSLv3/TLS read client certificate";
  1051. break;
  1052. case TLS_ST_SR_KEY_EXCH:
  1053. str = "SSLv3/TLS read client key exchange";
  1054. break;
  1055. case TLS_ST_SR_CERT_VRFY:
  1056. str = "SSLv3/TLS read certificate verify";
  1057. break;
  1058. case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
  1059. str = "DTLS1 read hello verify request";
  1060. break;
  1061. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  1062. str = "DTLS1 write hello verify request";
  1063. break;
  1064. default:
  1065. break;
  1066. }
  1067. }
  1068. return str;
  1069. }
  1070. /**
  1071. * @brief set the SSL context read buffer length
  1072. */
  1073. void SSL_CTX_set_default_read_buffer_len(SSL_CTX *ctx, size_t len)
  1074. {
  1075. SSL_ASSERT(ctx);
  1076. SSL_ASSERT(len);
  1077. ctx->read_buffer_len = len;
  1078. }
  1079. /**
  1080. * @brief set the SSL read buffer length
  1081. */
  1082. void SSL_set_default_read_buffer_len(SSL *ssl, size_t len)
  1083. {
  1084. SSL_ASSERT(ssl);
  1085. SSL_ASSERT(len);
  1086. SSL_METHOD_CALL(set_bufflen, ssl, len);
  1087. }
  1088. /**
  1089. * @brief set the SSL information callback function
  1090. */
  1091. void SSL_set_info_callback(SSL *ssl, void (*cb) (const SSL *ssl, int type, int val))
  1092. {
  1093. SSL_ASSERT(ssl);
  1094. ssl->info_callback = cb;
  1095. }
  1096. /**
  1097. * @brief add SSL context reference count by '1'
  1098. */
  1099. int SSL_CTX_up_ref(SSL_CTX *ctx)
  1100. {
  1101. SSL_ASSERT(ctx);
  1102. /**
  1103. * no support multi-thread SSL here
  1104. */
  1105. ctx->references++;
  1106. return 1;
  1107. }
  1108. /**
  1109. * @brief set the SSL security level
  1110. */
  1111. void SSL_set_security_level(SSL *ssl, int level)
  1112. {
  1113. SSL_ASSERT(ssl);
  1114. ssl->cert->sec_level = level;
  1115. }
  1116. /**
  1117. * @brief get the SSL security level
  1118. */
  1119. int SSL_get_security_level(const SSL *ssl)
  1120. {
  1121. SSL_ASSERT(ssl);
  1122. return ssl->cert->sec_level;
  1123. }
  1124. /**
  1125. * @brief get the SSL verifying mode of the SSL context
  1126. */
  1127. int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
  1128. {
  1129. SSL_ASSERT(ctx);
  1130. return ctx->verify_mode;
  1131. }
  1132. /**
  1133. * @brief set the session timeout time
  1134. */
  1135. long SSL_CTX_set_timeout(SSL_CTX *ctx, long t)
  1136. {
  1137. long l;
  1138. SSL_ASSERT(ctx);
  1139. l = ctx->session_timeout;
  1140. ctx->session_timeout = t;
  1141. return l;
  1142. }
  1143. /**
  1144. * @brief get the session timeout time
  1145. */
  1146. long SSL_CTX_get_timeout(const SSL_CTX *ctx)
  1147. {
  1148. SSL_ASSERT(ctx);
  1149. return ctx->session_timeout;
  1150. }
  1151. /**
  1152. * @brief set the SSL if we can read as many as data
  1153. */
  1154. void SSL_set_read_ahead(SSL *ssl, int yes)
  1155. {
  1156. SSL_ASSERT(ssl);
  1157. ssl->rlayer.read_ahead = yes;
  1158. }
  1159. /**
  1160. * @brief set the SSL context if we can read as many as data
  1161. */
  1162. void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes)
  1163. {
  1164. SSL_ASSERT(ctx);
  1165. ctx->read_ahead = yes;
  1166. }
  1167. /**
  1168. * @brief get the SSL ahead signal if we can read as many as data
  1169. */
  1170. int SSL_get_read_ahead(const SSL *ssl)
  1171. {
  1172. SSL_ASSERT(ssl);
  1173. return ssl->rlayer.read_ahead;
  1174. }
  1175. /**
  1176. * @brief get the SSL context ahead signal if we can read as many as data
  1177. */
  1178. long SSL_CTX_get_read_ahead(SSL_CTX *ctx)
  1179. {
  1180. SSL_ASSERT(ctx);
  1181. return ctx->read_ahead;
  1182. }
  1183. /**
  1184. * @brief check if the SSL context can read as many as data
  1185. */
  1186. long SSL_CTX_get_default_read_ahead(SSL_CTX *ctx)
  1187. {
  1188. SSL_ASSERT(ctx);
  1189. return ctx->read_ahead;
  1190. }
  1191. /**
  1192. * @brief set SSL session time
  1193. */
  1194. long SSL_set_time(SSL *ssl, long t)
  1195. {
  1196. SSL_ASSERT(ssl);
  1197. ssl->session->time = t;
  1198. return t;
  1199. }
  1200. /**
  1201. * @brief set SSL session timeout time
  1202. */
  1203. long SSL_set_timeout(SSL *ssl, long t)
  1204. {
  1205. SSL_ASSERT(ssl);
  1206. ssl->session->timeout = t;
  1207. return t;
  1208. }
  1209. /**
  1210. * @brief get the verifying result of the SSL certification
  1211. */
  1212. long SSL_get_verify_result(const SSL *ssl)
  1213. {
  1214. SSL_ASSERT(ssl);
  1215. return SSL_METHOD_CALL(get_verify_result, ssl);
  1216. }
  1217. /**
  1218. * @brief get the SSL verifying depth of the SSL context
  1219. */
  1220. int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
  1221. {
  1222. SSL_ASSERT(ctx);
  1223. return ctx->param.depth;
  1224. }
  1225. /**
  1226. * @brief set the SSL verify depth of the SSL context
  1227. */
  1228. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
  1229. {
  1230. SSL_ASSERT(ctx);
  1231. ctx->param.depth = depth;
  1232. }
  1233. /**
  1234. * @brief get the SSL verifying depth of the SSL
  1235. */
  1236. int SSL_get_verify_depth(const SSL *ssl)
  1237. {
  1238. SSL_ASSERT(ssl);
  1239. return ssl->param.depth;
  1240. }
  1241. /**
  1242. * @brief set the SSL verify depth of the SSL
  1243. */
  1244. void SSL_set_verify_depth(SSL *ssl, int depth)
  1245. {
  1246. SSL_ASSERT(ssl);
  1247. ssl->param.depth = depth;
  1248. }
  1249. /**
  1250. * @brief set the SSL context verifying of the SSL context
  1251. */
  1252. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode, int (*verify_callback)(int, X509_STORE_CTX *))
  1253. {
  1254. SSL_ASSERT(ctx);
  1255. ctx->verify_mode = mode;
  1256. ctx->default_verify_callback = verify_callback;
  1257. }
  1258. /**
  1259. * @brief set the SSL verifying of the SSL context
  1260. */
  1261. void SSL_set_verify(SSL *ssl, int mode, int (*verify_callback)(int, X509_STORE_CTX *))
  1262. {
  1263. SSL_ASSERT(ssl);
  1264. ssl->verify_mode = mode;
  1265. ssl->verify_callback = verify_callback;
  1266. }