ssl_lib.c 34 KB

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