1 /*
2 * lws-minimal-secure-streams-alexa
3 *
4 * This file is made available under the Creative Commons CC0 1.0
5 * Universal Public Domain Dedication.
6 */
7
8 #include <libwebsockets.h>
9 #include <string.h>
10 #include <signal.h>
11
12 extern int
13 avs_example_start(struct lws_context *context);
14
15 static int interrupted;
16 static lws_state_notify_link_t nl;
17
18 #if !defined(LWS_SS_USE_SSPC)
19
20 /*
21 * If not using the proxy, we need to bring our own policy
22 */
23
24 static const char * const default_ss_policy =
25 "{"
26 "\"release\":" "\"01234567\","
27 "\"product\":" "\"myproduct\","
28 "\"schema-version\":" "1,"
29 "\"retry\": [" /* named backoff / retry strategies */
30 "{\"default\": {"
31 "\"backoff\": [" "1000,"
32 "2000,"
33 "3000,"
34 "5000,"
35 "10000"
36 "],"
37 "\"conceal\":" "5,"
38 "\"jitterpc\":" "20,"
39 "\"svalidping\":" "60,"
40 "\"svalidhup\":" "64"
41 "}}"
42 "],"
43 "\"certs\": [" /* named individual certificates in BASE64 DER */
44 "{\"digicert_global_root_g2\": \"" /* api.amazon.com 2038-01 */
45 "MIIDjjCCAnagAwIBAgIQAzrx5qcRqaC7KGSxHQn65TANBgkqhkiG9w0BAQsFADBh"
46 "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3"
47 "d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBH"
48 "MjAeFw0xMzA4MDExMjAwMDBaFw0zODAxMTUxMjAwMDBaMGExCzAJBgNVBAYTAlVT"
49 "MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j"
50 "b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IEcyMIIBIjANBgkqhkiG"
51 "9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuzfNNNx7a8myaJCtSnX/RrohCgiN9RlUyfuI"
52 "2/Ou8jqJ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D+rcdqsq08p8kDi1L"
103 "93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm"
104 "jgSubJrIqg0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC"
105 "AYYwHQYDVR0OBBYEFIQYzIU07LwMlJQuCFmcx7IQTgoIMA0GCSqGSIb3DQEBCwUA"
106 "A4IBAQCY8jdaQZChGsV2USggNiMOruYou6r4lK5IpDB/G/wkjUu0yKGX9rbxenDI"
107 "U5PMCCjjmCXPI6T53iHTfIUJrU6adTrCC2qJeHZERxhlbI1Bjjt/msv0tadQ1wUs"
108 "N+gDS63pYaACbvXy8MWy7Vu33PqUXHeeE6V/Uq2V8viTO96LXFvKWlJbYK8U90vv"
109 "o/ufQJVtMVT8QtPHRh8jrdkPSHCa2XV4cdFyQzR1bldZwgJcJmApzyMZFo6IQ6XU"
110 "5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy"
111 "rqXRfboQnoZsG4q5WTP468SQvvG5"
112 "\"},"
113 "{\"starfield_services_root_ca\": \""
114 "MIID7zCCAtegAwIBAgIBADANBgkqhkiG9w0BAQsFADCBmDELMAkGA1UEBhMCVVMx"
115 "EDAOBgNVBAgTB0FyaXpvbmExEzARBgNVBAcTClNjb3R0c2RhbGUxJTAjBgNVBAoT"
116 "HFN0YXJmaWVsZCBUZWNobm9sb2dpZXMsIEluYy4xOzA5BgNVBAMTMlN0YXJmaWVs"
117 "ZCBTZXJ2aWNlcyBSb290IENlcnRpZmljYXRlIEF1dGhvcml0eSAtIEcyMB4XDTA5"
118 "MDkwMTAwMDAwMFoXDTM3MTIzMTIzNTk1OVowgZgxCzAJBgNVBAYTAlVTMRAwDgYD"
119 "VQQIEwdBcml6b25hMRMwEQYDVQQHEwpTY290dHNkYWxlMSUwIwYDVQQKExxTdGFy"
120 "ZmllbGQgVGVjaG5vbG9na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MAMBAf8w"
154 "DQYJKoZIhvcNAQEFBQADggEBAAWdP4id0ckaVaGsafPzWdqbAYcaT1epoXkJKtv3"
155 "L7IezMdeatiDh6GX70k1PncGQVhiv45YuApnP+yz3SFmH8lU+nLMPUxA2IGvd56D"
156 "eruix/U0F47ZEUD0/CwqTRV/p2JdLiXTAAsgGh1o+Re49L2L7ShZ3U0WixeDyLJl"
157 "xy16paq8U4Zt3VekyvggQQto8PT7dL5WXXp59fkdheMtlb71cZBDzI0fmgAKhynp"
158 "VSJYACPq4xJDKVtHCN2MQWplBqjlIapBtJUhlbl90TSrE9atvNziPTnNvT51cKEY"
159 "WQPJIrSPnNVeKtelttQKbfi3QBFGmh95DmK/D5fs4C8fF5Q="
160 "\"}"
161 "],"
162 "\"trust_stores\": [" /* named cert chains */
163 "{" /* chain for alexa.na.gateway.devices.a2z.com */
164 "\"name\": \"avs_via_starfield\","
165 "\"stack\": ["
166 "\"starfield_class_2_ca\","
167 "\"starfield_services_root_ca\""
168 "]"
169 "},"
170 "{" /* chain for api.amazon.com */
171 "\"name\": \"api_amazon_com\","
172 "\"stack\": ["
173 "\"digicert_global_ca_g2\","
174 "\"digicert_global_root_g2\""
175 "]"
176 "}"
177 "],"
178 "\"auth\": [" /* available auth type bindings */
179 "{"
180 "\"name\":" "\"lwa\","
181 "\"streamtype\":" "\"api_amazon_com_lwa\","
182 "\"blob\":" "0"
183 "}"
184 "],"
185 "\"s\": [" /* the supported stream types */
186 "{\"api_amazon_com_lwa\": {"
187 "\"endpoint\":" "\"api.amazon.com\","
188 "\"port\":" "443,"
189 "\"protocol\":" "\"h1\","
190 "\"http_method\":" "\"POST\","
191 "\"http_url\":" "\"auth/o2/token\","
192 "\"opportunistic\":" "true,"
193 "\"tls\":" "true,"
194 "\"h2q_oflow_txcr\":" "true,"
195 "\"http_www_form_urlencoded\":" "true,"
196 "\"http_no_content_length\":" "true,"
197 "\"retry\":" "\"default\","
198 "\"tls_trust_store\":" "\"api_amazon_com\""
199 "}},"
200 /*
201 * long poll event listener
202 */
203 "{\"avs_event\": {"
204 "\"endpoint\":" "\"alexa.na.gateway.devices.a2z.com\","
205 "\"port\":" "443,"
206 "\"protocol\":" "\"h2\","
207 "\"http_method\":" "\"GET\","
208 "\"http_url\":" "\"v20160207/directives\","
209 "\"use_auth\":" "\"lwa\","
210 "\"h2q_oflow_txcr\":" "true,"
211 "\"http_auth_header\":" "\"authorization:\","
212 "\"http_auth_preamble\":" "\"Bearer \","
213 "\"http_multipart_ss_in\":" "true,"
214 "\"nailed_up\":" "true,"
215 "\"long_poll\":" "true,"
216 "\"retry\":" "\"default\","
217 "\"tls\":" "true,"
218 "\"tls_trust_store\":" "\"avs_via_starfield\""
219 "}},"
220 /*
221 * Utterance metadata and audio send and reply processing.
222 *
223 * "Rideshare" and http_multipart_mime means these both go out
224 * in one multipart http transaction.
225 */
226 "{\"avs_metadata\": {"
227 "\"endpoint\":" "\"alexa.na.gateway.devices.a2z.com\","
228 "\"port\":" "443,"
229 "\"protocol\":" "\"h2\","
230 "\"http_method\":" "\"POST\","
231 "\"http_url\":" "\"v20160207/events\","
232 "\"use_auth\":" "\"lwa\","
233 "\"opportunistic\":" "true,"
234 "\"h2q_oflow_txcr\":" "true,"
235 "\"http_auth_header\":" "\"authorization:\","
236 "\"http_auth_preamble\":" "\"Bearer \","
237 "\"http_multipart_name\":" "\"metadata\","
238 "\"http_mime_content_type\":" "\"application/json; charset=UTF-8\","
239 "\"http_no_content_length\":" "true,"
240 "\"http_multipart_ss_in\":" "true,"
241 "\"rideshare\":" "\"avs_audio\","
242 "\"retry\":" "\"default\","
243 "\"tls\":" "true,"
244 "\"tls_trust_store\":" "\"avs_via_starfield\""
245 "}},"
246 "{\"avs_audio\": {"
247 "\"endpoint\":" "\"alexa.na.gateway.devices.a2z.com\","
248 "\"port\":" "443,"
249 "\"protocol\":" "\"h2\","
250 "\"http_method\":" "\"POST\","
251 "\"http_url\":" "\"v20160207/events\","
252 "\"use_auth\":" "\"lwa\","
253 "\"tls\":" "true,"
254 "\"h2q_oflow_txcr\":" "true,"
255 "\"http_auth_header\":" "\"authorization:\","
256 "\"http_auth_preamble\":" "\"Bearer \","
257 "\"http_multipart_ss_in\":" "true,"
258 "\"http_multipart_name\":" "\"audio\","
259 "\"http_mime_content_type\":" "\"application/octet-stream\","
260 "\"http_no_content_length\":" "true,"
261 "\"retry\":" "\"default\","
262 "\"tls_trust_store\":" "\"avs_via_starfield\""
263 "}}"
264 "]"
265 "}"
266 ;
267
268 #endif
269
270 static const char *canned_root_token_payload =
271 "grant_type=refresh_token"
272 "&refresh_token=Atzr|IwEBIJedGXjDqsU_vMxykqOMg"
273 "SHfYe3CPcedueWEMWSDMaDnEmiW8RlR1Kns7Cb4B-TOSnqp7ifVsY4BMY2B8tpHfO39XP"
274 "zfu9HapGjTR458IyHX44FE71pWJkGZ79uVBpljP4sazJuk8XS3Oe_yLnm_DIO6fU1nU3Y"
275 "0flYmsOiOAQE_gRk_pdlmEtHnpMA-9rLw3mkY5L89Ty9kUygBsiFaYatouROhbsTn8-jW"
276 "k1zZLUDpT6ICtBXSnrCIg0pUbZevPFhTwdXd6eX-u4rq0W-XaDvPWFO7au-iPb4Zk5eZE"
277 "iX6sissYrtNmuEXc2uHu7MnQO1hHCaTdIO2CANVumf-PHSD8xseamyh04sLV5JgFzY45S"
278 "KvKMajiUZuLkMokOx86rjC2Hdkx5DO7G-dbG1ufBDG-N79pFMSs7Ck5pc283IdLoJkCQc"
279 "AGvTX8o8I29QqkcGou-9TKhOJmpX8As94T61ok0UqqEKPJ7RhfQHHYdCtsdwxgvfVr9qI"
280 "xL_hDCcTho8opCVX-6QhJHl6SQFlTw13"
281 "&client_id="
282 "amzn1.application-oa2-client.4823334c434b4190a2b5a42c07938a2d";
283
284 /*
285 * Register the root token, and make the sticky AVS connection at the
286 * appropriate times during system startup
287 */
288
289 static int
app_system_state_nf(lws_state_manager_t * mgr,lws_state_notify_link_t * link,int current,int target)290 app_system_state_nf(lws_state_manager_t *mgr, lws_state_notify_link_t *link,
291 int current, int target)
292 {
293 struct lws_context *context = lws_system_context_from_system_mgr(mgr);
294 lws_system_blob_t *ab = lws_system_get_blob(context,
295 LWS_SYSBLOB_TYPE_AUTH, 1 /* AUTH_IDX_ROOT */);
296 size_t size;
297
298 /*
299 * For the things we care about, let's notice if we are trying to get
300 * past them when we haven't solved them yet, and make the system
301 * state wait while we trigger the dependent action.
302 */
303 switch (target) {
304 case LWS_SYSTATE_REGISTERED:
305 size = lws_system_blob_get_size(ab);
306 if (size)
307 break;
308
309 /* let's register our canned root token so auth can use it */
310 lws_system_blob_direct_set(ab,
311 (const uint8_t *)canned_root_token_payload,
312 strlen(canned_root_token_payload));
313 break;
314 case LWS_SYSTATE_OPERATIONAL:
315 if (current == target)
316 avs_example_start(context);
317 break;
318 case LWS_SYSTATE_POLICY_INVALID:
319 /*
320 * This is a NOP since we used direct set... but in a real
321 * system this could easily change to be done on the heap, then
322 * this would be important
323 */
324 lws_system_blob_destroy(lws_system_get_blob(context,
325 LWS_SYSBLOB_TYPE_AUTH,
326 1 /* AUTH_IDX_ROOT */));
327 break;
328 }
329
330 return 0;
331 }
332
333 static void
sigint_handler(int sig)334 sigint_handler(int sig)
335 {
336 interrupted = 1;
337 }
338
339 static lws_state_notify_link_t * const app_notifier_list[] = {
340 &nl, NULL
341 };
342
343 extern struct lws_protocols protocol_audio_test;
344 static const struct lws_protocols *protocols[] = {
345 &protocol_audio_test,
346 #if defined(LWS_SS_USE_SSPC)
347 lws_sspc_protocols,
348 #endif
349 NULL
350 };
351
main(int argc,const char ** argv)352 int main(int argc, const char **argv)
353 {
354 struct lws_context_creation_info info;
355 struct lws_context *context;
356 int n = 0;
357
358 signal(SIGINT, sigint_handler);
359 memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
360 lws_cmdline_option_handle_builtin(argc, argv, &info);
361
362 lwsl_user("LWS secure streams - Alexa voice test [-d<verb>]\n");
363
364 info.fd_limit_per_thread = 1 + 6 + 1;
365 #if !defined(LWS_SS_USE_SSPC)
366 info.options = LWS_SERVER_OPTION_EXPLICIT_VHOSTS |
367 LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
368 info.pss_policies_json = default_ss_policy;
369 #else
370 {
371 const char *p;
372
373 /* connect to ssproxy via UDS by default, else via
374 * tcp connection to this port */
375 if ((p = lws_cmdline_option(argc, argv, "-p")))
376 info.ss_proxy_port = atoi(p);
377
378 /* UDS "proxy.ss.lws" in abstract namespace, else this socket
379 * path; when -p given this can specify the network interface
380 * to bind to */
381 if ((p = lws_cmdline_option(argc, argv, "-i")))
382 info.ss_proxy_bind = p;
383
384 /* if -p given, -a specifies the proxy address to connect to */
385 if ((p = lws_cmdline_option(argc, argv, "-a")))
386 info.ss_proxy_address = p;
387 }
388 #endif
389 info.port = CONTEXT_PORT_NO_LISTEN;
390 info.pprotocols = protocols;
391
392 /* integrate us with lws system state management when context created */
393 nl.name = "app";
394 nl.notify_cb = app_system_state_nf;
395 info.register_notifier_list = app_notifier_list;
396
397 context = lws_create_context(&info);
398 if (!context) {
399 lwsl_err("lws init failed\n");
400 return 1;
401 }
402
403 /* create an explicit vhost so the sound protocol is initialized */
404
405 info.vhost_name = "asound";
406 if (!lws_create_vhost(context, &info)) {
407 lwsl_err("lws init failed\n");
408 goto bail;
409 }
410
411 /* the event loop */
412
413 while (n >= 0 && !interrupted)
414 n = lws_service(context, 0);
415
416 bail:
417 lws_context_destroy(context);
418 lwsl_user("Completed\n");
419
420 return 0;
421 }
422