ssl_lib.c 31 KB

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