ssl_demo.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. /*================================================================
  2. Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
  3. Quectel Wireless Solution Proprietary and Confidential.
  4. =================================================================*/
  5. /*=================================================================
  6. EDIT HISTORY FOR MODULE
  7. This section contains comments describing changes made to the module.
  8. Notice that changes are listed in reverse chronological order.
  9. WHEN WHO WHAT, WHERE, WHY
  10. ------------ ------- -------------------------------------------------------------------------------
  11. =================================================================*/
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include "ql_api_osi.h"
  16. #include "ql_api_nw.h"
  17. #include "ql_log.h"
  18. #include "ql_api_datacall.h"
  19. #include "sockets.h"
  20. #include "lwip/ip_addr.h"
  21. #include "lwip/ip6_addr.h"
  22. #include "lwip/netdb.h"
  23. #include "lwip/netif.h"
  24. #include "lwip/inet.h"
  25. #include "lwip/tcp.h"
  26. #include "ql_ssl.h"
  27. #define QL_SSL_LOG_LEVEL QL_LOG_LEVEL_INFO
  28. #define QL_SSL_LOG(msg, ...) QL_LOG(QL_SSL_LOG_LEVEL, "ssl_demo", msg, ##__VA_ARGS__)
  29. #define QL_SSL_LOG_PUSH(msg, ...) QL_LOG_PUSH("ssl_demo", msg, ##__VA_ARGS__)
  30. static ql_task_t ssl_task = NULL;
  31. #define TEST_HOST_ADDR "www.baidu.com"
  32. #define TEST_HOST_PORT 443
  33. #define TEST_SEND_DATA "GET /index.html HTTP/1.1\r\n\
  34. HOST: www.baidu.com\r\n\
  35. User-Agent: QuecOpen\r\n\
  36. Connection: Keep-Alive\r\n\r\n\0"
  37. #define DTLS_TEST_HOST_ADDR "220.180.239.212"
  38. #define DTLS_TEST_HOST_PORT 8252
  39. char *chain_path="UFS:cacert.pem";
  40. static void ssl_test_tls_with_noblocking(int profile_idx)
  41. {
  42. ql_data_call_info_s info;
  43. struct sockaddr_in local4, server_ipv4;
  44. struct sockaddr_in6 local6, server_ipv6;
  45. struct addrinfo *pres = NULL;
  46. char ip_addr_str[64] = {0};
  47. int socket_fd = -1;
  48. int flags = 0;
  49. int ret = 0;
  50. fd_set read_fds;
  51. fd_set write_fds;
  52. fd_set exp_fds;
  53. int fd_changed;
  54. int connected = 0;
  55. int ssl_hs_completed = 0;
  56. ql_ssl_config ssl_cfg;
  57. ql_ssl_context ssl_ctx;
  58. ql_get_data_call_info(0, profile_idx, &info);
  59. ret = getaddrinfo_with_pcid(TEST_HOST_ADDR, NULL, NULL, &pres, (uint32)profile_idx);
  60. if (ret < 0 || pres == NULL)
  61. {
  62. QL_SSL_LOG("DNS getaddrinfo failed! ret=%d; pres=%p!",ret,pres);
  63. return;
  64. }
  65. QL_SSL_LOG("local ip ver:%d", info.ip_version);
  66. if(info.ip_version == QL_PDP_TYPE_IPV6){
  67. struct addrinfo *temp = NULL;
  68. for(temp = pres; temp != NULL; temp = temp->ai_next){
  69. if(temp->ai_family == AF_INET6){
  70. socket_fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
  71. if(socket_fd < 0){
  72. break;
  73. }
  74. flags |= O_NONBLOCK;
  75. fcntl(socket_fd, F_SETFL,flags);
  76. local6.sin6_family = AF_INET6;
  77. local6.sin6_port = 0;
  78. local6.sin6_len = sizeof(struct sockaddr_in6);
  79. inet6_aton(ip6addr_ntoa(&info.v6.addr.ip), &local6.sin6_addr);
  80. ret = bind(socket_fd,(struct sockaddr *)&local6,sizeof(struct sockaddr));
  81. if(ret != 0){
  82. close(socket_fd);
  83. socket_fd = -1;
  84. break;
  85. }
  86. memcpy(&server_ipv6, (struct sockaddr_in6 *)temp->ai_addr, sizeof(struct sockaddr_in6));
  87. server_ipv6.sin6_family = AF_INET6;
  88. server_ipv6.sin6_port = htons(TEST_HOST_PORT);
  89. inet_ntop(AF_INET6, &server_ipv6.sin6_addr, ip_addr_str, sizeof(ip_addr_str));
  90. QL_SSL_LOG("By DNS,server_ipv6.sin_addr: %s", ip_addr_str);
  91. ret = connect(socket_fd, (struct sockaddr *)&server_ipv6, sizeof(server_ipv6));
  92. if((ret < 0 && lwip_get_error(socket_fd) == EINPROGRESS) || ret ==0){
  93. if(ret == 0)
  94. connected = 1;
  95. break;
  96. }else{
  97. close(socket_fd);
  98. socket_fd = -1;
  99. continue;
  100. }
  101. }
  102. }
  103. }else{
  104. struct addrinfo *temp = NULL;
  105. for(temp = pres; temp != NULL; temp = temp->ai_next){
  106. QL_SSL_LOG("temp->ai_family:%d", temp->ai_family);
  107. if(temp->ai_family == AF_INET){
  108. struct sockaddr_in * sin_res = (struct sockaddr_in *)temp->ai_addr;
  109. socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  110. QL_SSL_LOG("socket fd:%d",socket_fd);
  111. if(socket_fd < 0){
  112. break;
  113. }
  114. flags |= O_NONBLOCK;
  115. fcntl(socket_fd, F_SETFL,flags);
  116. local4.sin_family = AF_INET;
  117. local4.sin_port = 0;
  118. inet_aton(ip4addr_ntoa(&info.v4.addr.ip), &local4.sin_addr);
  119. ret = bind(socket_fd,(struct sockaddr *)&local4,sizeof(struct sockaddr));
  120. QL_SSL_LOG("bind ret:%d, errno:%d",ret, lwip_get_error(socket_fd));
  121. if(ret != 0){
  122. close(socket_fd);
  123. socket_fd = -1;
  124. break;
  125. }
  126. memset(&server_ipv4, 0x00, sizeof(struct sockaddr_in));
  127. server_ipv4.sin_addr = sin_res->sin_addr;
  128. server_ipv4.sin_family = AF_INET;
  129. server_ipv4.sin_port = htons(TEST_HOST_PORT);
  130. inet_ntop(AF_INET, &server_ipv4.sin_addr, ip_addr_str, sizeof(ip_addr_str));
  131. QL_SSL_LOG("By DNS,server_ipv4.sin_addr: %s", ip_addr_str);
  132. ret = connect(socket_fd, (struct sockaddr *)&server_ipv4, sizeof(server_ipv4));
  133. QL_SSL_LOG("connect ret:%d, errno:%d",ret, lwip_get_error(socket_fd));
  134. if((ret < 0 && lwip_get_error(socket_fd) == EINPROGRESS) || ret ==0){
  135. if(ret == 0)
  136. connected = 1;
  137. break;
  138. }else{
  139. close(socket_fd);
  140. socket_fd = -1;
  141. continue;
  142. }
  143. }
  144. }
  145. }
  146. freeaddrinfo(pres);
  147. QL_SSL_LOG("socket fd:%d",socket_fd);
  148. if(socket_fd < 0)
  149. return;
  150. FD_ZERO(&read_fds);
  151. FD_ZERO(&write_fds);
  152. FD_ZERO(&exp_fds);
  153. FD_SET(socket_fd, &write_fds);
  154. FD_SET(socket_fd, &exp_fds);
  155. while(1){
  156. fd_changed = select(socket_fd+1, &read_fds, &write_fds, &exp_fds, NULL);
  157. if(fd_changed > 0){
  158. if(FD_ISSET(socket_fd, &write_fds)){
  159. FD_CLR(socket_fd, &write_fds);
  160. if(connected== 0){
  161. int value = 0;
  162. int len = 0;
  163. len = sizeof(value);
  164. getsockopt(socket_fd, SOL_SOCKET, SO_ERROR, &value, &len);
  165. QL_SSL_LOG("errno: %d", value);
  166. if(value == 0 || value == EISCONN ){
  167. int *ciphersuit = NULL;
  168. connected = 1;
  169. QL_SSL_LOG("tcp socket connected and ready to write!!!!");
  170. do{
  171. ret = ql_ssl_conf_init(&ssl_cfg);
  172. ciphersuit = malloc(sizeof(int)*3);
  173. if(ciphersuit == NULL){
  174. ret = QL_SSL_ERROR_OUT_OF_MEM;
  175. break;
  176. }
  177. memset(ciphersuit, 0x00, sizeof(int)*3);
  178. ciphersuit[0] = QL_SSL_TLS_RSA_WITH_RC4_128_MD5;
  179. ciphersuit[1] = QL_SSL_TLS_RSA_WITH_RC4_128_SHA;
  180. //Sets the array of cipher groups for the mbedTLS_SSL_CONF_cipherSuites function
  181. //The entire array ends with the number 0
  182. ciphersuit[2] = 0;
  183. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_VERSION, QL_SSL_VERSION_ALL);
  184. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_TRANSPORT, QL_SSL_TLS_PROTOCOL);
  185. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_AUTHMODE, QL_SSL_VERIFY_NULL);
  186. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_HS_TIMEOUT, 60);
  187. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_CIPHERSUITE, ciphersuit);
  188. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_CACERT, &chain_path,1);
  189. //ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_IGNORE_CERT_ITEM, MBEDTLS_X509_BADCERT_KEY_USAGE|MBEDTLS_X509_BADCERT_CN_MISMATCH);
  190. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_SNI, 1);
  191. if((ret =ql_ssl_new(&ssl_ctx)) != QL_SSL_SUCCESS){
  192. break;
  193. }
  194. ql_ssl_set_socket_fd(&ssl_ctx, socket_fd);
  195. if((ret= ql_ssl_setup(&ssl_ctx, &ssl_cfg)) != QL_SSL_SUCCESS){
  196. break;
  197. }
  198. ret = ql_ssl_set_hostname(&ssl_ctx, TEST_HOST_ADDR);
  199. QL_SSL_LOG("set_hostname:%08x", ret);
  200. ret = ql_ssl_handshake(&ssl_ctx);
  201. }while(0);
  202. if(!(ret == QL_SSL_SUCCESS || ret == QL_SSL_ERROR_WANT_READ||ret == QL_SSL_ERROR_WANT_WRITE)){
  203. QL_SSL_LOG("ssl handshake failed!!!!");
  204. ql_ssl_conf_free(&ssl_cfg);
  205. ql_ssl_free(&ssl_ctx);
  206. shutdown(socket_fd, SHUT_WR);
  207. break;
  208. }else if(ret == QL_SSL_ERROR_WANT_READ){
  209. FD_SET(socket_fd, &read_fds);
  210. }else if(ret == QL_SSL_ERROR_WANT_WRITE){
  211. FD_SET(socket_fd, &write_fds);
  212. }else if(ret == QL_SSL_SUCCESS){
  213. ssl_hs_completed = 1;
  214. QL_SSL_LOG("SSL HandShake completed!!!!");
  215. ql_ssl_write(&ssl_ctx, (const unsigned char *)TEST_SEND_DATA, strlen(TEST_SEND_DATA));
  216. FD_SET(socket_fd, &read_fds);
  217. }
  218. }else{
  219. QL_SSL_LOG("tcp socket connected failure!!!!");
  220. break;
  221. }
  222. }else{
  223. QL_SSL_LOG("tcp socket ready to write!!!!");
  224. if(ssl_hs_completed == 0){
  225. ret = ql_ssl_handshake(&ssl_ctx);
  226. if(!(ret == QL_SSL_SUCCESS || ret == QL_SSL_ERROR_WANT_READ||ret == QL_SSL_ERROR_WANT_WRITE)){
  227. ql_ssl_conf_free(&ssl_cfg);
  228. ql_ssl_free(&ssl_ctx);
  229. shutdown(socket_fd, SHUT_WR);
  230. break;
  231. }else if(ret == QL_SSL_ERROR_WANT_READ){
  232. FD_SET(socket_fd, &read_fds);
  233. }else if(ret == QL_SSL_ERROR_WANT_WRITE){
  234. FD_SET(socket_fd, &write_fds);
  235. }else if(ret == QL_SSL_SUCCESS){
  236. ssl_hs_completed = 1;
  237. QL_SSL_LOG("SSL HandShake completed!!!!");
  238. ql_ssl_write(&ssl_ctx, (const unsigned char *)TEST_SEND_DATA, strlen(TEST_SEND_DATA));
  239. FD_SET(socket_fd, &read_fds);
  240. }
  241. }
  242. }
  243. }
  244. if(FD_ISSET(socket_fd, &read_fds)){
  245. FD_CLR(socket_fd, &read_fds);
  246. QL_SSL_LOG("tcp socket ready to read!!!!");
  247. if(ssl_hs_completed == 0){
  248. ret = ql_ssl_handshake(&ssl_ctx);
  249. if(!(ret == QL_SSL_SUCCESS || ret == QL_SSL_ERROR_WANT_READ||ret == QL_SSL_ERROR_WANT_WRITE)){
  250. ql_ssl_conf_free(&ssl_cfg);
  251. ql_ssl_free(&ssl_ctx);
  252. shutdown(socket_fd, SHUT_WR);
  253. break;
  254. }else if(ret == QL_SSL_ERROR_WANT_READ){
  255. FD_SET(socket_fd, &read_fds);
  256. }else if(ret == QL_SSL_ERROR_WANT_WRITE){
  257. FD_SET(socket_fd, &write_fds);
  258. }else if(ret == QL_SSL_SUCCESS){
  259. ssl_hs_completed = 1;
  260. QL_SSL_LOG("SSL HandShake completed!!!!");
  261. ql_ssl_write(&ssl_ctx, (const unsigned char *)TEST_SEND_DATA, strlen(TEST_SEND_DATA));
  262. FD_SET(socket_fd, &read_fds);
  263. }
  264. }else{
  265. unsigned char test_recv_buf[513] = {0};
  266. int end = 0;
  267. do{
  268. ret =ql_ssl_read(&ssl_ctx, test_recv_buf, 512);
  269. QL_SSL_LOG("ret:%d", ret);
  270. if(ret > 0){
  271. QL_SSL_LOG("recv: %s", test_recv_buf);
  272. if(ret < 512)
  273. {
  274. end = 1;
  275. break;
  276. }
  277. }else{
  278. if(ret == QL_SSL_ERROR_WANT_READ){
  279. FD_SET(socket_fd, &read_fds);
  280. }else{
  281. end = 1;
  282. }
  283. break;
  284. }
  285. }while(1);
  286. if(end == 1)
  287. break;
  288. }
  289. }
  290. if(FD_ISSET(socket_fd, &exp_fds)){
  291. FD_CLR(socket_fd, &exp_fds);
  292. QL_SSL_LOG("tcp socket occur exception!!!!");
  293. shutdown(socket_fd, SHUT_WR);
  294. break;
  295. }
  296. }
  297. }
  298. if(ssl_hs_completed == 1){
  299. ql_ssl_conf_free(&ssl_cfg);
  300. ql_ssl_free(&ssl_ctx);
  301. shutdown(socket_fd, SHUT_WR);
  302. }
  303. close(socket_fd);
  304. }
  305. static void ssl_test_tls_with_blocking(int profile_idx)
  306. {
  307. ql_data_call_info_s info;
  308. struct sockaddr_in local4, server_ipv4;
  309. struct sockaddr_in6 local6, server_ipv6;
  310. struct addrinfo *pres = NULL;
  311. char ip_addr_str[64] = {0};
  312. int socket_fd = -1;
  313. int socket_fd2 = -1;
  314. int ret = 0,ret2 = 0;
  315. ql_ssl_config ssl_cfg;
  316. ql_ssl_context ssl_ctx,ssl_ctx2;
  317. ql_get_data_call_info(0, profile_idx, &info);
  318. ret = getaddrinfo_with_pcid(TEST_HOST_ADDR, NULL, NULL, &pres, (uint32)profile_idx);
  319. if (ret < 0 || pres == NULL)
  320. {
  321. QL_SSL_LOG("DNS getaddrinfo failed! ret=%d; pres=%p!",ret,pres);
  322. return;
  323. }
  324. QL_SSL_LOG("local ip ver:%d", info.ip_version);
  325. if(info.ip_version == QL_PDP_TYPE_IPV6){
  326. struct addrinfo *temp = NULL;
  327. for(temp = pres; temp != NULL; temp = temp->ai_next){
  328. if(temp->ai_family == AF_INET6){
  329. socket_fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
  330. if(socket_fd < 0){
  331. break;
  332. }
  333. local6.sin6_family = AF_INET6;
  334. local6.sin6_port = 0;
  335. local6.sin6_len = sizeof(struct sockaddr_in6);
  336. inet6_aton(ip6addr_ntoa(&info.v6.addr.ip), &local6.sin6_addr);
  337. ret = bind(socket_fd,(struct sockaddr *)&local6,sizeof(struct sockaddr));
  338. if(ret != 0){
  339. close(socket_fd);
  340. socket_fd = -1;
  341. break;
  342. }
  343. memcpy(&server_ipv6, (struct sockaddr_in6 *)temp->ai_addr, sizeof(struct sockaddr_in6));
  344. server_ipv6.sin6_family = AF_INET6;
  345. server_ipv6.sin6_port = htons(TEST_HOST_PORT);
  346. inet_ntop(AF_INET6, &server_ipv6.sin6_addr, ip_addr_str, sizeof(ip_addr_str));
  347. QL_SSL_LOG("By DNS,server_ipv6.sin_addr: %s", ip_addr_str);
  348. ret = connect(socket_fd, (struct sockaddr *)&server_ipv6, sizeof(server_ipv6));
  349. if(ret ==0){
  350. break;
  351. }else{
  352. close(socket_fd);
  353. socket_fd = -1;
  354. continue;
  355. }
  356. }
  357. }
  358. }else{
  359. struct addrinfo *temp = NULL;
  360. for(temp = pres; temp != NULL; temp = temp->ai_next){
  361. QL_SSL_LOG("temp->ai_family:%d", temp->ai_family);
  362. if(temp->ai_family == AF_INET){
  363. struct sockaddr_in * sin_res = (struct sockaddr_in *)temp->ai_addr;
  364. socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  365. socket_fd2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  366. QL_SSL_LOG("socket fd:%d",socket_fd);
  367. QL_SSL_LOG("socket fd2:%d",socket_fd2);
  368. if((socket_fd < 0)||(socket_fd2 < 0)){
  369. break;
  370. }
  371. local4.sin_family = AF_INET;
  372. local4.sin_port = 0;
  373. inet_aton(ip4addr_ntoa(&info.v4.addr.ip), &local4.sin_addr);
  374. ret = bind(socket_fd,(struct sockaddr *)&local4,sizeof(struct sockaddr));
  375. QL_SSL_LOG("bind socket_fd ret:%d, errno:%d",ret, lwip_get_error(socket_fd));
  376. if(ret != 0){
  377. close(socket_fd);
  378. socket_fd = -1;
  379. break;
  380. }
  381. ret = bind(socket_fd2,(struct sockaddr *)&local4,sizeof(struct sockaddr));
  382. QL_SSL_LOG("bind socket_fd2 ret:%d, errno:%d",ret, lwip_get_error(socket_fd));
  383. if(ret != 0){
  384. close(socket_fd2);
  385. socket_fd2 = -1;
  386. break;
  387. }
  388. memset(&server_ipv4, 0x00, sizeof(struct sockaddr_in));
  389. server_ipv4.sin_addr = sin_res->sin_addr;
  390. server_ipv4.sin_family = AF_INET;
  391. server_ipv4.sin_port = htons(TEST_HOST_PORT);
  392. inet_ntop(AF_INET, &server_ipv4.sin_addr, ip_addr_str, sizeof(ip_addr_str));
  393. QL_SSL_LOG("By DNS,server_ipv4.sin_addr: %s", ip_addr_str);
  394. ret = connect(socket_fd, (struct sockaddr *)&server_ipv4, sizeof(server_ipv4));
  395. QL_SSL_LOG("socket_fd connect ret:%d, errno:%d",ret, lwip_get_error(socket_fd));
  396. ret2 = connect(socket_fd2, (struct sockaddr *)&server_ipv4, sizeof(server_ipv4));
  397. QL_SSL_LOG("socket_fd2 connect ret:%d, errno:%d",ret2, lwip_get_error(socket_fd));
  398. if((ret ==0)&&(ret2 ==0)){
  399. break;
  400. }else{
  401. close(socket_fd);
  402. socket_fd = -1;
  403. close(socket_fd2);
  404. socket_fd2 = -1;
  405. continue;
  406. }
  407. }
  408. }
  409. }
  410. freeaddrinfo(pres);
  411. QL_SSL_LOG("socket fd:%d,socket fd2:%d",socket_fd,socket_fd2);
  412. if((socket_fd < 0)||(socket_fd2 < 0))
  413. return;
  414. //ssl handshake
  415. do{
  416. int *ciphersuit = NULL;
  417. ret = ql_ssl_conf_init(&ssl_cfg);
  418. ciphersuit = malloc(sizeof(int)*3);
  419. if(ciphersuit == NULL){
  420. ret = QL_SSL_ERROR_OUT_OF_MEM;
  421. break;
  422. }
  423. memset(ciphersuit, 0x00, sizeof(int)*3);
  424. ciphersuit[0] = QL_SSL_TLS_RSA_WITH_RC4_128_MD5;
  425. ciphersuit[1] = QL_SSL_TLS_RSA_WITH_RC4_128_SHA;
  426. //Sets the array of cipher groups for the mbedTLS_SSL_CONF_cipherSuites function
  427. //The entire array ends with the number 0
  428. ciphersuit[2] = 0;
  429. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_VERSION, QL_SSL_VERSION_ALL);
  430. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_TRANSPORT, QL_SSL_TLS_PROTOCOL);
  431. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_AUTHMODE, QL_SSL_VERIFY_NULL);
  432. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_HS_TIMEOUT, 60);
  433. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_CIPHERSUITE, ciphersuit);
  434. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_CACERT, &chain_path,1);
  435. //ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_IGNORE_CERT_ITEM, MBEDTLS_X509_BADCERT_KEY_USAGE|MBEDTLS_X509_BADCERT_CN_MISMATCH);
  436. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_SNI, 1);
  437. if(((ret =ql_ssl_new(&ssl_ctx)) != QL_SSL_SUCCESS)||((ret =ql_ssl_new(&ssl_ctx2)) != QL_SSL_SUCCESS)){
  438. break;
  439. }
  440. ql_ssl_set_socket_fd(&ssl_ctx, socket_fd);
  441. ql_ssl_set_socket_fd(&ssl_ctx2, socket_fd2);
  442. if(((ret= ql_ssl_setup(&ssl_ctx, &ssl_cfg)) != QL_SSL_SUCCESS)||((ret2= ql_ssl_setup(&ssl_ctx2, &ssl_cfg)) != QL_SSL_SUCCESS)){
  443. break;
  444. }
  445. ql_ssl_set_hostname(&ssl_ctx, TEST_HOST_ADDR);
  446. QL_SSL_LOG("set_hostname:%08x", ret);
  447. ql_ssl_set_hostname(&ssl_ctx2, TEST_HOST_ADDR);
  448. QL_SSL_LOG("set_hostname:%08x", ret);
  449. ret = ql_ssl_handshake(&ssl_ctx);
  450. ret2 = ql_ssl_handshake(&ssl_ctx2);
  451. }while(0);
  452. if((ret != QL_SSL_SUCCESS)||(ret2 != QL_SSL_SUCCESS)){
  453. QL_SSL_LOG("ssl handshake failed!!!!");
  454. ql_ssl_conf_free(&ssl_cfg);
  455. ql_ssl_free(&ssl_ctx);
  456. ql_ssl_free(&ssl_ctx2);
  457. shutdown(socket_fd, SHUT_WR);
  458. shutdown(socket_fd2, SHUT_WR);
  459. close(socket_fd);
  460. close(socket_fd2);
  461. return ;
  462. }else{
  463. unsigned char test_recv_buf[120] = {0};
  464. QL_SSL_LOG("SSL HandShake completed!!!!");
  465. if(ql_ssl_write(&ssl_ctx, (const unsigned char *)TEST_SEND_DATA, strlen(TEST_SEND_DATA)) >0){
  466. while((ret = ql_ssl_read(&ssl_ctx, test_recv_buf,120)) > 0){
  467. QL_SSL_LOG("recv: %s", test_recv_buf);
  468. if(ret < 120)
  469. break;
  470. }
  471. }
  472. if(ql_ssl_write(&ssl_ctx2, (const unsigned char *)TEST_SEND_DATA, strlen(TEST_SEND_DATA)) >0){
  473. while((ret = ql_ssl_read(&ssl_ctx2, test_recv_buf,120)) > 0){
  474. QL_SSL_LOG("recv2: %s", test_recv_buf);
  475. if(ret < 120)
  476. break;
  477. }
  478. }
  479. ql_ssl_close_notify(&ssl_ctx);
  480. ql_ssl_conf_free(&ssl_cfg);
  481. ql_ssl_free(&ssl_ctx);
  482. shutdown(socket_fd, SHUT_RDWR);
  483. close(socket_fd);
  484. ql_ssl_close_notify(&ssl_ctx2);
  485. ql_ssl_free(&ssl_ctx2);
  486. shutdown(socket_fd2, SHUT_RDWR);
  487. close(socket_fd2);
  488. return ;
  489. }
  490. }
  491. #ifdef QL_SSL_TLS_SESSION_CACHE_FEATURE
  492. static void ssl_test_tls_with_session_cache(int profile_idx)
  493. {
  494. int loop_cnt = 5;
  495. ql_ssl_config ssl_cfg;
  496. ql_ssl_conf_init(&ssl_cfg);
  497. loop_cnt = 5;
  498. while(loop_cnt)
  499. {
  500. ql_data_call_info_s info;
  501. struct sockaddr_in local4, server_ipv4;
  502. struct sockaddr_in6 local6, server_ipv6;
  503. struct addrinfo *pres = NULL;
  504. char ip_addr_str[64] = {0};
  505. int socket_fd = -1;
  506. int ret = 0;
  507. ql_ssl_context ssl_ctx;
  508. QL_SSL_LOG("loop_cnt:%d", loop_cnt);
  509. loop_cnt --;
  510. ql_get_data_call_info(0, profile_idx, &info);
  511. ret = getaddrinfo_with_pcid(TEST_HOST_ADDR, NULL, NULL, &pres, (uint32)profile_idx);
  512. if (ret < 0 || pres == NULL)
  513. {
  514. QL_SSL_LOG("DNS getaddrinfo failed! ret=%d; pres=%p!",ret,pres);
  515. continue;
  516. }
  517. QL_SSL_LOG("local ip ver:%d", info.ip_version);
  518. if(info.ip_version == QL_PDP_TYPE_IPV6){
  519. struct addrinfo *temp = NULL;
  520. for(temp = pres; temp != NULL; temp = temp->ai_next){
  521. if(temp->ai_family == AF_INET6){
  522. socket_fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
  523. if(socket_fd < 0){
  524. break;
  525. }
  526. local6.sin6_family = AF_INET6;
  527. local6.sin6_port = 0;
  528. local6.sin6_len = sizeof(struct sockaddr_in6);
  529. inet6_aton(ip6addr_ntoa(&info.v6.addr.ip), &local6.sin6_addr);
  530. ret = bind(socket_fd,(struct sockaddr *)&local6,sizeof(struct sockaddr));
  531. if(ret != 0){
  532. close(socket_fd);
  533. socket_fd = -1;
  534. break;
  535. }
  536. memcpy(&server_ipv6, (struct sockaddr_in6 *)temp->ai_addr, sizeof(struct sockaddr_in6));
  537. server_ipv6.sin6_family = AF_INET6;
  538. server_ipv6.sin6_port = htons(TEST_HOST_PORT);
  539. inet_ntop(AF_INET6, &server_ipv6.sin6_addr, ip_addr_str, sizeof(ip_addr_str));
  540. QL_SSL_LOG("By DNS,server_ipv6.sin_addr: %s", ip_addr_str);
  541. ret = connect(socket_fd, (struct sockaddr *)&server_ipv6, sizeof(server_ipv6));
  542. if(ret ==0){
  543. break;
  544. }else{
  545. close(socket_fd);
  546. socket_fd = -1;
  547. continue;
  548. }
  549. }
  550. }
  551. }else{
  552. struct addrinfo *temp = NULL;
  553. for(temp = pres; temp != NULL; temp = temp->ai_next){
  554. QL_SSL_LOG("temp->ai_family:%d", temp->ai_family);
  555. if(temp->ai_family == AF_INET){
  556. struct sockaddr_in * sin_res = (struct sockaddr_in *)temp->ai_addr;
  557. socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  558. QL_SSL_LOG("socket fd:%d",socket_fd);
  559. if((socket_fd < 0)){
  560. break;
  561. }
  562. local4.sin_family = AF_INET;
  563. local4.sin_port = 0;
  564. inet_aton(ip4addr_ntoa(&info.v4.addr.ip), &local4.sin_addr);
  565. ret = bind(socket_fd,(struct sockaddr *)&local4,sizeof(struct sockaddr));
  566. QL_SSL_LOG("bind socket_fd ret:%d, errno:%d",ret, lwip_get_error(socket_fd));
  567. if(ret != 0){
  568. close(socket_fd);
  569. socket_fd = -1;
  570. break;
  571. }
  572. memset(&server_ipv4, 0x00, sizeof(struct sockaddr_in));
  573. server_ipv4.sin_addr = sin_res->sin_addr;
  574. server_ipv4.sin_family = AF_INET;
  575. server_ipv4.sin_port = htons(TEST_HOST_PORT);
  576. inet_ntop(AF_INET, &server_ipv4.sin_addr, ip_addr_str, sizeof(ip_addr_str));
  577. QL_SSL_LOG("By DNS,server_ipv4.sin_addr: %s", ip_addr_str);
  578. ret = connect(socket_fd, (struct sockaddr *)&server_ipv4, sizeof(server_ipv4));
  579. QL_SSL_LOG("socket_fd connect ret:%d, errno:%d",ret, lwip_get_error(socket_fd));
  580. if((ret ==0)){
  581. break;
  582. }else{
  583. close(socket_fd);
  584. socket_fd = -1;
  585. continue;
  586. }
  587. }
  588. }
  589. }
  590. freeaddrinfo(pres);
  591. QL_SSL_LOG("socket fd:%d",socket_fd);
  592. if((socket_fd < 0))
  593. continue;
  594. //ssl handshake
  595. do{
  596. int *ciphersuit = NULL;
  597. ciphersuit = malloc(sizeof(int)*3);
  598. if(ciphersuit == NULL){
  599. ret = QL_SSL_ERROR_OUT_OF_MEM;
  600. break;
  601. }
  602. ciphersuit[0] = QL_SSL_TLS_RSA_WITH_RC4_128_MD5;
  603. ciphersuit[1] = QL_SSL_TLS_RSA_WITH_RC4_128_SHA;
  604. ciphersuit[2] = 0;
  605. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_VERSION, QL_SSL_VERSION_ALL);
  606. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_TRANSPORT, QL_SSL_TLS_PROTOCOL);
  607. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_AUTHMODE, QL_SSL_VERIFY_NULL);
  608. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_HS_TIMEOUT, 60);
  609. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_CIPHERSUITE, ciphersuit);
  610. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_CACERT, &chain_path,1);
  611. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_SESSION_CACHE, 1);
  612. if(((ret =ql_ssl_new(&ssl_ctx)) != QL_SSL_SUCCESS)){
  613. break;
  614. }
  615. if((ret= ql_ssl_setup(&ssl_ctx, &ssl_cfg)) != QL_SSL_SUCCESS){
  616. break;
  617. }
  618. ql_ssl_set_hostname(&ssl_ctx, TEST_HOST_ADDR);
  619. ql_ssl_set_socket_fd(&ssl_ctx, socket_fd);
  620. ret = ql_ssl_handshake(&ssl_ctx);
  621. }while(0);
  622. if((ret != QL_SSL_SUCCESS)){
  623. QL_SSL_LOG("ssl handshake failed!!!!");
  624. ql_ssl_free(&ssl_ctx);
  625. shutdown(socket_fd, SHUT_WR);
  626. close(socket_fd);
  627. continue ;
  628. }else{
  629. unsigned char test_recv_buf[120] = {0};
  630. QL_SSL_LOG("SSL HandShake completed!!!!");
  631. if(ql_ssl_write(&ssl_ctx, (const unsigned char *)TEST_SEND_DATA, strlen(TEST_SEND_DATA)) >0){
  632. while((ret = ql_ssl_read(&ssl_ctx, test_recv_buf,120)) > 0){
  633. QL_SSL_LOG("recv: %s", test_recv_buf);
  634. if(ret < 120)
  635. break;
  636. }
  637. }
  638. ql_ssl_close_notify(&ssl_ctx);
  639. ql_ssl_free(&ssl_ctx);
  640. shutdown(socket_fd, SHUT_RDWR);
  641. close(socket_fd);
  642. continue ;
  643. }
  644. }
  645. ql_ssl_conf_free(&ssl_cfg);
  646. }
  647. #endif
  648. static void ssl_test_dtls(int profile_idx)
  649. {
  650. ql_data_call_info_s info;
  651. struct sockaddr_in local4, server_ipv4;
  652. struct sockaddr_in6 local6, server_ipv6;
  653. struct addrinfo *pres = NULL;
  654. char ip_addr_str[64] = {0};
  655. int socket_fd = -1;
  656. int ret = 0;
  657. int flags = 0;
  658. fd_set read_fds;
  659. fd_set write_fds;
  660. fd_set exp_fds;
  661. int fd_changed;
  662. ql_ssl_config ssl_cfg;
  663. ql_ssl_context ssl_ctx;
  664. int *ciphersuit = NULL;
  665. int ssl_hs_completed = 0;
  666. FD_ZERO(&read_fds);
  667. FD_ZERO(&write_fds);
  668. FD_ZERO(&exp_fds);
  669. ql_get_data_call_info(0, profile_idx, &info);
  670. ret = getaddrinfo_with_pcid(DTLS_TEST_HOST_ADDR, NULL, NULL, &pres, (uint32)profile_idx);
  671. if (ret < 0 || pres == NULL)
  672. {
  673. QL_SSL_LOG("DNS getaddrinfo failed! ret=%d; pres=%p!",ret,pres);
  674. return;
  675. }
  676. QL_SSL_LOG("local ip ver:%d", info.ip_version);
  677. if(info.ip_version == QL_PDP_TYPE_IPV6){
  678. struct addrinfo *temp = NULL;
  679. for(temp = pres; temp != NULL; temp = temp->ai_next){
  680. if(temp->ai_family == AF_INET6){
  681. socket_fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  682. if(socket_fd < 0){
  683. break;
  684. }
  685. flags |= O_NONBLOCK;
  686. fcntl(socket_fd, F_SETFL,flags);
  687. local6.sin6_family = AF_INET6;
  688. local6.sin6_port = 0;
  689. local6.sin6_len = sizeof(struct sockaddr_in6);
  690. inet6_aton(ip6addr_ntoa(&info.v6.addr.ip), &local6.sin6_addr);
  691. ret = bind(socket_fd,(struct sockaddr *)&local6,sizeof(struct sockaddr));
  692. if(ret != 0){
  693. close(socket_fd);
  694. socket_fd = -1;
  695. break;
  696. }
  697. memcpy(&server_ipv6, (struct sockaddr_in6 *)temp->ai_addr, sizeof(struct sockaddr_in6));
  698. server_ipv6.sin6_family = AF_INET6;
  699. server_ipv6.sin6_port = htons(DTLS_TEST_HOST_PORT);
  700. inet_ntop(AF_INET6, &server_ipv6.sin6_addr, ip_addr_str, sizeof(ip_addr_str));
  701. QL_SSL_LOG("By DNS,server_ipv6.sin_addr: %s", ip_addr_str);
  702. ret = connect(socket_fd, (struct sockaddr *)&server_ipv6, sizeof(server_ipv6));
  703. if(ret ==0){
  704. break;
  705. }else{
  706. close(socket_fd);
  707. socket_fd = -1;
  708. continue;
  709. }
  710. }
  711. }
  712. }else{
  713. struct addrinfo *temp = NULL;
  714. for(temp = pres; temp != NULL; temp = temp->ai_next){
  715. QL_SSL_LOG("temp->ai_family:%d", temp->ai_family);
  716. if(temp->ai_family == AF_INET){
  717. struct sockaddr_in * sin_res = (struct sockaddr_in *)temp->ai_addr;
  718. socket_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  719. QL_SSL_LOG("socket fd:%d",socket_fd);
  720. if(socket_fd < 0){
  721. break;
  722. }
  723. flags |= O_NONBLOCK;
  724. fcntl(socket_fd, F_SETFL,flags);
  725. local4.sin_family = AF_INET;
  726. local4.sin_port = 0;
  727. inet_aton(ip4addr_ntoa(&info.v4.addr.ip), &local4.sin_addr);
  728. ret = bind(socket_fd,(struct sockaddr *)&local4,sizeof(struct sockaddr));
  729. QL_SSL_LOG("bind ret:%d, errno:%d",ret, lwip_get_error(socket_fd));
  730. if(ret != 0){
  731. close(socket_fd);
  732. socket_fd = -1;
  733. break;
  734. }
  735. memset(&server_ipv4, 0x00, sizeof(struct sockaddr_in));
  736. server_ipv4.sin_addr = sin_res->sin_addr;
  737. server_ipv4.sin_family = AF_INET;
  738. server_ipv4.sin_port = htons(DTLS_TEST_HOST_PORT);
  739. inet_ntop(AF_INET, &server_ipv4.sin_addr, ip_addr_str, sizeof(ip_addr_str));
  740. QL_SSL_LOG("By DNS,server_ipv4.sin_addr: %s", ip_addr_str);
  741. ret = connect(socket_fd, (struct sockaddr *)&server_ipv4, sizeof(server_ipv4));
  742. QL_SSL_LOG("connect ret:%d, errno:%d",ret, lwip_get_error(socket_fd));
  743. if(ret ==0){
  744. break;
  745. }else{
  746. close(socket_fd);
  747. socket_fd = -1;
  748. continue;
  749. }
  750. }
  751. }
  752. }
  753. freeaddrinfo(pres);
  754. QL_SSL_LOG("socket fd:%d",socket_fd);
  755. if(socket_fd < 0)
  756. return;
  757. //ssl handshake
  758. ret = ql_ssl_conf_init(&ssl_cfg);
  759. ciphersuit = malloc(sizeof(int)*3);
  760. if(ciphersuit == NULL){
  761. close(socket_fd);
  762. return;
  763. }
  764. memset(ciphersuit, 0x00, sizeof(int)*3);
  765. ciphersuit[0] = QL_SSL_TLS_PSK_WITH_RC4_128_SHA;
  766. ciphersuit[1] = QL_SSL_TLS_PSK_WITH_AES_128_CBC_SHA;
  767. //Sets the array of cipher groups for the mbedTLS_SSL_CONF_cipherSuites function
  768. //The entire array ends with the number 0
  769. ciphersuit[2] = 0;
  770. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_VERSION, QL_SSL_VERSION_3);
  771. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_TRANSPORT, QL_SSL_DTLS_PROTOCOL);
  772. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_AUTHMODE, QL_SSL_VERIFY_NULL);
  773. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_HS_TIMEOUT, 5000);
  774. ql_ssl_conf_set(&ssl_cfg, QL_SSL_CONF_CIPHERSUITE, ciphersuit);
  775. if((ret =ql_ssl_new(&ssl_ctx)) != QL_SSL_SUCCESS){
  776. ql_ssl_conf_free(&ssl_cfg);
  777. close(socket_fd);
  778. return;
  779. }
  780. if((ret= ql_ssl_setup(&ssl_ctx, &ssl_cfg)) != QL_SSL_SUCCESS){
  781. ql_ssl_conf_free(&ssl_cfg);
  782. ql_ssl_free(&ssl_ctx);
  783. close(socket_fd);
  784. return;
  785. }
  786. ql_ssl_set_hostname(&ssl_ctx, DTLS_TEST_HOST_ADDR);
  787. ql_ssl_set_socket_fd(&ssl_ctx, socket_fd);
  788. ret = ql_ssl_handshake(&ssl_ctx);
  789. if(!(ret == QL_SSL_SUCCESS || ret == QL_SSL_ERROR_WANT_READ||ret == QL_SSL_ERROR_WANT_WRITE)){
  790. QL_SSL_LOG("ssl handshake failed!!!!");
  791. ql_ssl_conf_free(&ssl_cfg);
  792. ql_ssl_free(&ssl_ctx);
  793. close(socket_fd);
  794. return;
  795. }
  796. if(ret != QL_SSL_SUCCESS){
  797. if(ret == QL_SSL_ERROR_WANT_READ)
  798. FD_SET(socket_fd, &read_fds);
  799. else
  800. FD_SET(socket_fd, &write_fds);
  801. FD_SET(socket_fd, &exp_fds);
  802. while(1){
  803. fd_changed = select(socket_fd+1, &read_fds, &write_fds, &exp_fds, NULL);
  804. if(fd_changed > 0){
  805. if(FD_ISSET(socket_fd, &write_fds)){
  806. FD_CLR(socket_fd, &write_fds);
  807. if(ssl_hs_completed == 0){
  808. ret = ql_ssl_handshake(&ssl_ctx);
  809. if(!(ret == QL_SSL_SUCCESS || ret == QL_SSL_ERROR_WANT_READ||ret == QL_SSL_ERROR_WANT_WRITE)){
  810. QL_SSL_LOG("ssl handshake failed!!!!");
  811. break;
  812. }else if(ret == QL_SSL_SUCCESS){
  813. QL_SSL_LOG("ssl handshake completed!!!!");
  814. ssl_hs_completed = 1;
  815. break;
  816. }else if(ret == QL_SSL_ERROR_WANT_READ){
  817. FD_SET(socket_fd, &read_fds);
  818. }else if(ret == QL_SSL_ERROR_WANT_WRITE){
  819. FD_SET(socket_fd, &write_fds);
  820. }
  821. }
  822. }
  823. if(FD_ISSET(socket_fd, &read_fds)){
  824. FD_CLR(socket_fd, &read_fds);
  825. if(ssl_hs_completed == 0){
  826. ret = ql_ssl_handshake(&ssl_ctx);
  827. if(!(ret == QL_SSL_SUCCESS || ret == QL_SSL_ERROR_WANT_READ||ret == QL_SSL_ERROR_WANT_WRITE)){
  828. QL_SSL_LOG("ssl handshake failed!!!!");
  829. break;
  830. }else if(ret == QL_SSL_SUCCESS){
  831. QL_SSL_LOG("ssl handshake completed!!!!");
  832. ssl_hs_completed = 1;
  833. break;
  834. }else if(ret == QL_SSL_ERROR_WANT_READ){
  835. FD_SET(socket_fd, &read_fds);
  836. }else if(ret == QL_SSL_ERROR_WANT_WRITE){
  837. FD_SET(socket_fd, &write_fds);
  838. }
  839. }
  840. }
  841. if(FD_ISSET(socket_fd, &exp_fds)){
  842. FD_CLR(socket_fd, &exp_fds);
  843. break;
  844. }
  845. }
  846. }
  847. }
  848. ql_ssl_conf_free(&ssl_cfg);
  849. ql_ssl_free(&ssl_ctx);
  850. close(socket_fd);
  851. return;
  852. }
  853. static void ssl_app_thread(void * arg)
  854. {
  855. int ret = 0;
  856. int i = 0, run_num = 1;
  857. int profile_idx = 1;
  858. ql_data_call_info_s info;
  859. char ip4_addr_str[16] = {0};
  860. uint8_t nSim = 0;
  861. ql_rtos_task_sleep_s(10);
  862. QL_SSL_LOG("========== ssl demo start ==========");
  863. QL_SSL_LOG("wait for network register done");
  864. while((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10){
  865. i++;
  866. ql_rtos_task_sleep_s(1);
  867. }
  868. if(ret == 0){
  869. i = 0;
  870. QL_SSL_LOG("====network registered!!!!====");
  871. }else{
  872. QL_SSL_LOG("====network register failure!!!!!====");
  873. goto exit;
  874. }
  875. ql_set_data_call_asyn_mode(nSim, profile_idx, 0);
  876. QL_SSL_LOG("===start data call====");
  877. ret=ql_start_data_call(nSim, profile_idx, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  878. QL_SSL_LOG("===data call result:%d", ret);
  879. if(ret != 0){
  880. QL_SSL_LOG("====data call failure!!!!=====");
  881. }
  882. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  883. ret = ql_get_data_call_info(nSim, profile_idx, &info);
  884. if(ret != 0){
  885. QL_SSL_LOG("ql_get_data_call_info ret: %d", ret);
  886. ql_stop_data_call(nSim, profile_idx);
  887. goto exit;
  888. }
  889. QL_SSL_LOG("info->profile_idx: %d", info.profile_idx);
  890. QL_SSL_LOG("info->ip_version: %d", info.ip_version);
  891. QL_SSL_LOG("info->v4.state: %d", info.v4.state);
  892. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  893. QL_SSL_LOG("info.v4.addr.ip: %s", ip4_addr_str);
  894. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  895. QL_SSL_LOG("info.v4.addr.pri_dns: %s", ip4_addr_str);
  896. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  897. QL_SSL_LOG("info.v4.addr.sec_dns: %s", ip4_addr_str);
  898. while(run_num <= 100){
  899. int case_id = run_num%3;
  900. QL_SSL_LOG("==============ssl_demo_test_tls[%d]================",run_num);
  901. if(case_id == 0){
  902. ssl_test_tls_with_noblocking(profile_idx);
  903. }else if(case_id == 1){
  904. ssl_test_tls_with_blocking(profile_idx);
  905. }
  906. else if(case_id == 2)
  907. {
  908. #ifdef QL_SSL_TLS_SESSION_CACHE_FEATURE
  909. ssl_test_tls_with_session_cache(profile_idx);
  910. #endif
  911. }
  912. QL_SSL_LOG("==============ssl_demo_test_tls[%d] end================",run_num);
  913. run_num++;
  914. ql_rtos_task_sleep_s(1);
  915. }
  916. QL_SSL_LOG("==============ssl_demo_test_dtls================");
  917. ssl_test_dtls(profile_idx);
  918. QL_SSL_LOG("==============ssl_demo_test_dtls end================");
  919. exit:
  920. ql_rtos_task_delete(ssl_task);
  921. return;
  922. }
  923. int ql_ssl_app_init(void)
  924. {
  925. QlOSStatus err = QL_OSI_SUCCESS;
  926. err = ql_rtos_task_create(&ssl_task, 16*1024, APP_PRIORITY_ABOVE_NORMAL, "QsslApp", ssl_app_thread, NULL, 5);
  927. if(err != QL_OSI_SUCCESS)
  928. {
  929. QL_SSL_LOG("ssl_app init failed");
  930. }
  931. return err;
  932. }