main.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * lws-api-test-secure-streams
  3. *
  4. * Written in 2010-2020 by Andy Green <[email protected]>
  5. *
  6. * This file is made available under the Creative Commons CC0 1.0
  7. * Universal Public Domain Dedication.
  8. *
  9. * Let's exercise some basic SS / h1 functionality against httpbin.org
  10. */
  11. #include <libwebsockets.h>
  12. #include <string.h>
  13. #include <signal.h>
  14. static int interrupted, bad = 1;
  15. static lws_state_notify_link_t nl;
  16. static struct lws_context *context;
  17. static const char * const default_ss_policy =
  18. "{"
  19. "\"release\":" "\"01234567\","
  20. "\"product\":" "\"myproduct\","
  21. "\"schema-version\":" "1,"
  22. #if defined(VIA_LOCALHOST_SOCKS)
  23. "\"via-socks5\":" "\"127.0.0.1:1080\","
  24. #endif
  25. "\"retry\": [" /* named backoff / retry strategies */
  26. "{\"default\": {"
  27. "\"backoff\": [" "1000,"
  28. "2000,"
  29. "3000,"
  30. "5000,"
  31. "10000"
  32. "],"
  33. "\"conceal\":" "5,"
  34. "\"jitterpc\":" "20,"
  35. "\"svalidping\":" "30,"
  36. "\"svalidhup\":" "35"
  37. "}}"
  38. "],"
  39. "\"certs\": [" /* named individual certificates in BASE64 DER */
  40. /*
  41. * Let's Encrypt certs for warmcat.com / libwebsockets.org
  42. *
  43. * We fetch the real policy from there using SS and switch to
  44. * using that.
  45. */
  46. "{\"amz_root_ca1\": \""
  47. "MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF"
  48. "ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6"
  49. "b24gUm9vdCBDQSAxMB4XDTE1MDUyNjAwMDAwMFoXDTM4MDExNzAwMDAwMFowOTEL"
  50. "MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv"
  51. "b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj"
  52. "ca9HgFB0fW7Y14h29Jlo91ghYPl0hAEvrAIthtOgQ3pOsqTQNroBvo3bSMgHFzZM"
  53. "9O6II8c+6zf1tRn4SWiw3te5djgdYZ6k/oI2peVKVuRF4fn9tBb6dNqcmzU5L/qw"
  54. "IFAGbHrQgLKm+a/sRxmPUDgH3KKHOVj4utWp+UhnMJbulHheb4mjUcAwhmahRWa6"
  55. "VOujw5H5SNz/0egwLX0tdHA114gk957EWW67c4cX8jJGKLhD+rcdqsq08p8kDi1L"
  56. "93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm"
  57. "jgSubJrIqg0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC"
  58. "AYYwHQYDVR0OBBYEFIQYzIU07LwMlJQuCFmcx7IQTgoIMA0GCSqGSIb3DQEBCwUA"
  59. "A4IBAQCY8jdaQZChGsV2USggNiMOruYou6r4lK5IpDB/G/wkjUu0yKGX9rbxenDI"
  60. "U5PMCCjjmCXPI6T53iHTfIUJrU6adTrCC2qJeHZERxhlbI1Bjjt/msv0tadQ1wUs"
  61. "N+gDS63pYaACbvXy8MWy7Vu33PqUXHeeE6V/Uq2V8viTO96LXFvKWlJbYK8U90vv"
  62. "o/ufQJVtMVT8QtPHRh8jrdkPSHCa2XV4cdFyQzR1bldZwgJcJmApzyMZFo6IQ6XU"
  63. "5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy"
  64. "rqXRfboQnoZsG4q5WTP468SQvvG5"
  65. "\"}"
  66. "],"
  67. "\"trust_stores\": [" /* named cert chains */
  68. "{"
  69. "\"name\": \"amz\","
  70. "\"stack\": ["
  71. "\"amz_root_ca1\""
  72. "]"
  73. "}"
  74. "],"
  75. "\"s\": ["
  76. /*
  77. * "fetch_policy" decides from where the real policy
  78. * will be fetched, if present. Otherwise the initial
  79. * policy is treated as the whole, hardcoded, policy.
  80. */
  81. "{\"httpbin_get\": {"
  82. "\"endpoint\":" "\"httpbin.org\","
  83. "\"port\":" "443,"
  84. "\"protocol\":" "\"h1\","
  85. "\"http_method\":" "\"GET\","
  86. "\"http_url\":" "\"/get\","
  87. "\"tls\":" "true,"
  88. "\"opportunistic\":" "true,"
  89. "\"retry\":" "\"default\","
  90. "\"tls_trust_store\":" "\"amz\""
  91. "}},"
  92. "{\"httpbin_get404\": {"
  93. "\"endpoint\":" "\"httpbin.org\","
  94. "\"port\":" "443,"
  95. "\"protocol\":" "\"h1\","
  96. "\"http_method\":" "\"GET\","
  97. "\"http_url\":" "\"/status/404\","
  98. "\"tls\":" "true,"
  99. "\"opportunistic\":" "true,"
  100. "\"retry\":" "\"default\","
  101. "\"tls_trust_store\":" "\"amz\""
  102. "}},"
  103. "{\"httpbin_post\": {"
  104. "\"endpoint\":" "\"httpbin.org\","
  105. "\"port\":" "443,"
  106. "\"protocol\":" "\"h1\","
  107. "\"http_method\":" "\"POST\","
  108. "\"http_url\":" "\"/post\","
  109. "\"tls\":" "true,"
  110. "\"opportunistic\":" "true,"
  111. "\"retry\":" "\"default\","
  112. "\"tls_trust_store\":" "\"amz\""
  113. "}}"
  114. "}"
  115. "]}"
  116. ;
  117. typedef struct atss {
  118. const lws_ss_info_t *ssi;
  119. size_t send;
  120. char expect_nack;
  121. } atss_t;
  122. static const atss_t *next_test;
  123. typedef struct myss {
  124. struct lws_ss_handle *ss;
  125. void *opaque_data;
  126. /* ... application specific state ... */
  127. lws_sorted_usec_list_t sul;
  128. size_t payload;
  129. size_t sent;
  130. char seen_eom;
  131. char ended_well;
  132. } myss_t;
  133. /* secure streams payload interface */
  134. static int
  135. myss_rx(void *userobj, const uint8_t *buf, size_t len, int flags)
  136. {
  137. myss_t *m = (myss_t *)userobj;
  138. lwsl_hexdump_info(buf, len);
  139. m->payload += len;
  140. if (!(flags & LWSSS_FLAG_EOM))
  141. m->seen_eom = 1;
  142. return 0;
  143. }
  144. static int
  145. myss_tx_get(void *userobj, lws_ss_tx_ordinal_t ord, uint8_t *buf, size_t *len,
  146. int *flags)
  147. {
  148. return 1; /* nothing to send */
  149. }
  150. static int
  151. myss_tx_post(void *userobj, lws_ss_tx_ordinal_t ord, uint8_t *buf, size_t *len,
  152. int *flags)
  153. {
  154. myss_t *m = (myss_t *)userobj;
  155. size_t budget = (next_test->send - m->sent);
  156. if (!budget)
  157. return 1;
  158. if (*len < budget)
  159. budget = *len;
  160. if (!m->sent)
  161. *flags |= LWSSS_FLAG_SOM;
  162. memset(buf, 0x55, budget);
  163. *len = budget;
  164. m->sent += budget;
  165. if (m->sent != next_test->send)
  166. lws_ss_request_tx(m->ss);
  167. else
  168. *flags |= LWSSS_FLAG_EOM;
  169. return 0;
  170. }
  171. static int
  172. myss_state(void *userobj, void *sh, lws_ss_constate_t state,
  173. lws_ss_tx_ordinal_t ack)
  174. {
  175. myss_t *m = (myss_t *)userobj;
  176. lwsl_notice("%s: %s, ord 0x%x\n", __func__, lws_ss_state_name(state),
  177. (unsigned int)ack);
  178. switch (state) {
  179. case LWSSSCS_CREATING:
  180. lws_ss_client_connect(m->ss);
  181. if (next_test->send)
  182. lws_ss_request_tx_len(m->ss, (unsigned long)next_test->send);
  183. break;
  184. case LWSSSCS_ALL_RETRIES_FAILED:
  185. lwsl_notice("%s: Connection failed\n", __func__);
  186. interrupted = 1;
  187. break;
  188. case LWSSSCS_QOS_NACK_REMOTE:
  189. if (next_test->expect_nack)
  190. goto happy;
  191. lwsl_notice("%s: remote NACK\n", __func__);
  192. interrupted = 1;
  193. break;
  194. case LWSSSCS_QOS_ACK_REMOTE:
  195. /*
  196. * To be satisfied, we want to see the ACK_REMOTE indicating
  197. * that the transaction went through; that we had the payload
  198. * EOM; and that we saw at least 200 + posted bytes response
  199. */
  200. if (!m->seen_eom || m->payload < 200 + next_test->send) {
  201. lwsl_warn("%s: ACK_REMOTE but eom %d, payload %d\n",
  202. __func__, m->seen_eom, (int)m->payload);
  203. interrupted = 1;
  204. return -1;
  205. }
  206. happy:
  207. /* when we disconnect, we can go happily */
  208. m->ended_well = 1;
  209. if (!(++next_test)->ssi) {
  210. lwsl_notice("%s: completed all tests\n", __func__);
  211. bad = 0;
  212. interrupted = 1;
  213. break;
  214. }
  215. if (lws_ss_create(context, 0, next_test->ssi,
  216. NULL, NULL, NULL, NULL)) {
  217. lwsl_err("%s: failed to create secure stream\n",
  218. __func__);
  219. return -1;
  220. }
  221. break;
  222. case LWSSSCS_DISCONNECTED:
  223. if (!m->ended_well) {
  224. lwsl_warn("%s: DISCONNECTED without good end\n",
  225. __func__);
  226. interrupted = 1;
  227. }
  228. break;
  229. default:
  230. break;
  231. }
  232. return 0;
  233. }
  234. static const lws_ss_info_t ssi_get = {
  235. .handle_offset = offsetof(myss_t, ss),
  236. .opaque_user_data_offset = offsetof(myss_t, opaque_data),
  237. .rx = myss_rx,
  238. .tx = myss_tx_get,
  239. .state = myss_state,
  240. .user_alloc = sizeof(myss_t),
  241. .streamtype = "httpbin_get"
  242. }, ssi_get404 = {
  243. .handle_offset = offsetof(myss_t, ss),
  244. .opaque_user_data_offset = offsetof(myss_t, opaque_data),
  245. .rx = myss_rx,
  246. .tx = myss_tx_get,
  247. .state = myss_state,
  248. .user_alloc = sizeof(myss_t),
  249. .streamtype = "httpbin_get404"
  250. }, ssi_post = {
  251. .handle_offset = offsetof(myss_t, ss),
  252. .opaque_user_data_offset = offsetof(myss_t, opaque_data),
  253. .rx = myss_rx,
  254. .tx = myss_tx_post,
  255. .state = myss_state,
  256. .user_alloc = sizeof(myss_t),
  257. .streamtype = "httpbin_post"
  258. };
  259. static const atss_t test_list[] = {
  260. { .ssi = &ssi_get },
  261. { .ssi = &ssi_get404, .expect_nack = 1 },
  262. { .ssi = &ssi_post, .send = 4096 },
  263. { .ssi = NULL }
  264. };
  265. static int
  266. app_system_state_nf(lws_state_manager_t *mgr, lws_state_notify_link_t *link,
  267. int current, int target)
  268. {
  269. struct lws_context *context = lws_system_context_from_system_mgr(mgr);
  270. /*
  271. * For the things we care about, let's notice if we are trying to get
  272. * past them when we haven't solved them yet, and make the system
  273. * state wait while we trigger the dependent action.
  274. */
  275. switch (target) {
  276. case LWS_SYSTATE_OPERATIONAL:
  277. if (current == LWS_SYSTATE_OPERATIONAL) {
  278. next_test = &test_list[0];
  279. if (lws_ss_create(context, 0, next_test->ssi,
  280. NULL, NULL, NULL, NULL)) {
  281. lwsl_err("%s: failed to create secure stream\n",
  282. __func__);
  283. return -1;
  284. }
  285. }
  286. break;
  287. }
  288. return 0;
  289. }
  290. static lws_state_notify_link_t * const app_notifier_list[] = {
  291. &nl, NULL
  292. };
  293. static void
  294. sigint_handler(int sig)
  295. {
  296. interrupted = 1;
  297. }
  298. int main(int argc, const char **argv)
  299. {
  300. struct lws_context_creation_info info;
  301. int n = 0;
  302. signal(SIGINT, sigint_handler);
  303. memset(&info, 0, sizeof info);
  304. lws_cmdline_option_handle_builtin(argc, argv, &info);
  305. lwsl_user("LWS secure streams test client [-d<verb>]\n");
  306. /* these options are mutually exclusive if given */
  307. info.fd_limit_per_thread = 1 + 6 + 1;
  308. info.port = CONTEXT_PORT_NO_LISTEN;
  309. info.pss_policies_json = default_ss_policy;
  310. info.options = LWS_SERVER_OPTION_EXPLICIT_VHOSTS |
  311. LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT |
  312. LWS_SERVER_OPTION_H2_JUST_FIX_WINDOW_UPDATE_OVERFLOW;
  313. /* integrate us with lws system state management when context created */
  314. nl.name = "app";
  315. nl.notify_cb = app_system_state_nf;
  316. info.register_notifier_list = app_notifier_list;
  317. /* create the context */
  318. context = lws_create_context(&info);
  319. if (!context) {
  320. lwsl_err("lws init failed\n");
  321. return 1;
  322. }
  323. /* the event loop */
  324. while (n >= 0 && !interrupted)
  325. n = lws_service(context, 0);
  326. lws_context_destroy(context);
  327. lwsl_user("Completed: %s\n", bad ? "failed" : "OK");
  328. return bad;
  329. }