xbintsc 0.6.20 → 0.6.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (224) hide show
  1. package/AGENTS.md +5 -0
  2. package/dist/src/cli/help.d.ts +1 -1
  3. package/dist/src/cli/main.js +1 -0
  4. package/dist/src/cli/main.js.map +1 -1
  5. package/dist/src/driver/build-key.d.ts +3 -0
  6. package/dist/src/driver/build-key.js +5 -2
  7. package/dist/src/driver/build-key.js.map +1 -1
  8. package/dist/src/driver/compiler.js +11 -0
  9. package/dist/src/driver/compiler.js.map +1 -1
  10. package/dist/src/driver/config.d.ts +12 -0
  11. package/dist/src/driver/config.js +22 -0
  12. package/dist/src/driver/config.js.map +1 -1
  13. package/dist/src/driver/fingerprint.d.ts +11 -0
  14. package/dist/src/driver/fingerprint.js +25 -0
  15. package/dist/src/driver/fingerprint.js.map +1 -1
  16. package/dist/src/driver/index.d.ts +2 -1
  17. package/dist/src/driver/index.js +1 -0
  18. package/dist/src/driver/index.js.map +1 -1
  19. package/dist/src/driver/link-features.js +36 -11
  20. package/dist/src/driver/link-features.js.map +1 -1
  21. package/dist/src/driver/options.d.ts +8 -1
  22. package/dist/src/driver/paths.d.ts +10 -0
  23. package/dist/src/driver/paths.js +18 -0
  24. package/dist/src/driver/paths.js.map +1 -1
  25. package/dist/src/driver/runtime-objects.d.ts +1 -1
  26. package/dist/src/driver/runtime-objects.js +22 -6
  27. package/dist/src/driver/runtime-objects.js.map +1 -1
  28. package/dist/src/driver/storage.d.ts +34 -0
  29. package/dist/src/driver/storage.js +67 -0
  30. package/dist/src/driver/storage.js.map +1 -0
  31. package/dist/src/driver/toolchain.d.ts +1 -1
  32. package/dist/src/driver/toolchain.js +10 -1
  33. package/dist/src/driver/toolchain.js.map +1 -1
  34. package/dist/src/driver/version.d.ts +1 -1
  35. package/dist/src/driver/version.js +1 -1
  36. package/dist/src/extensions/gui/features.d.ts +1 -1
  37. package/dist/src/extensions/gui/features.js +2 -2
  38. package/dist/src/extensions/gui/features.js.map +1 -1
  39. package/dist/src/extensions/gui/index.js +3 -1
  40. package/dist/src/extensions/gui/index.js.map +1 -1
  41. package/dist/src/extensions/node/http2/feature.d.ts +14 -0
  42. package/dist/src/extensions/node/http2/feature.js +28 -0
  43. package/dist/src/extensions/node/http2/feature.js.map +1 -0
  44. package/dist/src/extensions/node/http2/index.d.ts +5 -1
  45. package/dist/src/extensions/node/http2/index.js +6 -1
  46. package/dist/src/extensions/node/http2/index.js.map +1 -1
  47. package/dist/src/extensions/node/index.js +3 -1
  48. package/dist/src/extensions/node/index.js.map +1 -1
  49. package/dist/src/extensions/node/tls/feature.d.ts +12 -0
  50. package/dist/src/extensions/node/tls/feature.js +24 -0
  51. package/dist/src/extensions/node/tls/feature.js.map +1 -0
  52. package/dist/src/extensions/node/tls/index.d.ts +5 -1
  53. package/dist/src/extensions/node/tls/index.js +5 -1
  54. package/dist/src/extensions/node/tls/index.js.map +1 -1
  55. package/dist/src/extensions/node/zlib/compression.js +11 -6
  56. package/dist/src/extensions/node/zlib/compression.js.map +1 -1
  57. package/dist/src/extensions/registry.d.ts +4 -1
  58. package/dist/src/extensions/registry.js +9 -2
  59. package/dist/src/extensions/registry.js.map +1 -1
  60. package/dist/src/extensions/storage.d.ts +22 -0
  61. package/dist/src/extensions/storage.js +51 -0
  62. package/dist/src/extensions/storage.js.map +1 -0
  63. package/dist/src/extensions/tls.d.ts +26 -0
  64. package/dist/src/extensions/tls.js +46 -0
  65. package/dist/src/extensions/tls.js.map +1 -0
  66. package/dist/tests/driver/config.test.js +31 -0
  67. package/dist/tests/driver/config.test.js.map +1 -1
  68. package/dist/tests/driver/link-features.test.js +13 -2
  69. package/dist/tests/driver/link-features.test.js.map +1 -1
  70. package/dist/tests/driver/storage.test.d.ts +1 -0
  71. package/dist/tests/driver/storage.test.js +29 -0
  72. package/dist/tests/driver/storage.test.js.map +1 -0
  73. package/dist/tests/e2e/fetch.test.js +3 -1
  74. package/dist/tests/e2e/fetch.test.js.map +1 -1
  75. package/dist/tests/e2e/http2.test.d.ts +7 -0
  76. package/dist/tests/e2e/http2.test.js +201 -0
  77. package/dist/tests/e2e/http2.test.js.map +1 -0
  78. package/dist/tests/e2e/net-helpers.d.ts +28 -0
  79. package/dist/tests/e2e/net-helpers.js +59 -0
  80. package/dist/tests/e2e/net-helpers.js.map +1 -0
  81. package/dist/tests/e2e/node-modules.test.js +3 -5
  82. package/dist/tests/e2e/node-modules.test.js.map +1 -1
  83. package/dist/tests/e2e/storage.test.d.ts +7 -0
  84. package/dist/tests/e2e/storage.test.js +86 -0
  85. package/dist/tests/e2e/storage.test.js.map +1 -0
  86. package/dist/tests/e2e/tls.test.d.ts +10 -0
  87. package/dist/tests/e2e/tls.test.js +197 -0
  88. package/dist/tests/e2e/tls.test.js.map +1 -0
  89. package/dist/tests/extensions/gui-features.test.js +1 -1
  90. package/dist/tests/extensions/gui-features.test.js.map +1 -1
  91. package/dist/tests/extensions/storage.test.d.ts +1 -0
  92. package/dist/tests/extensions/storage.test.js +51 -0
  93. package/dist/tests/extensions/storage.test.js.map +1 -0
  94. package/doc/ai/cli.md +2 -0
  95. package/doc/ai/contributing.md +14 -0
  96. package/doc/ai/extensions.md +31 -3
  97. package/doc/ai/language-support.md +1 -1
  98. package/doc/ai/zh-CN/cli.md +2 -0
  99. package/doc/ai/zh-CN/contributing.md +12 -0
  100. package/doc/ai/zh-CN/extensions.md +27 -2
  101. package/doc/ai/zh-CN/language-support.md +1 -1
  102. package/doc/gui/api.md +1 -1
  103. package/doc/gui/build.md +7 -5
  104. package/doc/gui/media.md +4 -4
  105. package/doc/gui/rendering.md +1 -1
  106. package/doc/gui.md +3 -3
  107. package/doc/implemented/async.md +1 -1
  108. package/doc/implemented/extensions.md +2 -1
  109. package/doc/implemented/fetch.md +19 -4
  110. package/doc/implemented/node/http2.md +51 -0
  111. package/doc/implemented/node/tls.md +56 -0
  112. package/doc/implemented/node.md +8 -0
  113. package/doc/implemented/storage.md +65 -0
  114. package/doc/implemented.md +4 -2
  115. package/doc/unimplemented/node/modules.md +6 -6
  116. package/doc/unimplemented/toolchain.md +1 -1
  117. package/doc/xbintsc.config.schema.json +16 -1
  118. package/doc/zh-CN/gui/api.md +1 -1
  119. package/doc/zh-CN/gui/build.md +6 -4
  120. package/doc/zh-CN/gui/media.md +3 -3
  121. package/doc/zh-CN/gui/rendering.md +1 -1
  122. package/doc/zh-CN/gui.md +3 -3
  123. package/doc/zh-CN/implemented/async.md +1 -1
  124. package/doc/zh-CN/implemented/extensions.md +2 -1
  125. package/doc/zh-CN/implemented/fetch.md +15 -3
  126. package/doc/zh-CN/implemented/node/http2.md +44 -0
  127. package/doc/zh-CN/implemented/node/tls.md +47 -0
  128. package/doc/zh-CN/implemented/node.md +7 -0
  129. package/doc/zh-CN/implemented/storage.md +52 -0
  130. package/doc/zh-CN/implemented.md +4 -2
  131. package/doc/zh-CN/unimplemented/node/modules.md +3 -3
  132. package/doc/zh-CN/unimplemented/toolchain.md +1 -1
  133. package/package.json +16 -8
  134. package/runtime/ext_gui/dom_api_form.cpp +10 -0
  135. package/runtime/ext_gui/gui_media.cpp +1 -1
  136. package/runtime/ext_gui/media.cpp +1 -1
  137. package/runtime/ext_gui/media_api.h +2 -2
  138. package/runtime/ext_node/http2/http2.c +25 -11
  139. package/runtime/ext_node/http2/http2_backend.c +48 -0
  140. package/runtime/ext_node/http2/http2_bridge.c +25 -0
  141. package/runtime/ext_node/http2/parts/api.inc +40 -0
  142. package/runtime/ext_node/http2/parts/client.inc +215 -0
  143. package/runtime/ext_node/http2/parts/common.inc +330 -0
  144. package/runtime/ext_node/http2/parts/events.inc +139 -0
  145. package/runtime/ext_node/http2/parts/server.inc +301 -0
  146. package/runtime/ext_node/http2/parts/streams.inc +254 -0
  147. package/runtime/ext_node/tls/parts/api.inc +38 -0
  148. package/runtime/ext_node/tls/parts/connect.inc +107 -0
  149. package/runtime/ext_node/tls/parts/helpers.inc +121 -0
  150. package/runtime/ext_node/tls/parts/server.inc +101 -0
  151. package/runtime/ext_node/tls/parts/socket.inc +171 -0
  152. package/runtime/ext_node/tls/tls.c +21 -13
  153. package/runtime/ext_node/tls/tls_backend.c +50 -0
  154. package/runtime/ext_node/zlib/{miniz/compression_bridge.c → compression_bridge.c} +1 -1
  155. package/runtime/xt_fetch/async.inc +19 -12
  156. package/runtime/xt_fetch/socket.inc +55 -0
  157. package/runtime/xt_fetch.c +3 -0
  158. package/runtime/xt_http2/nghttp2_compat.h +19 -0
  159. package/runtime/xt_http2/nghttp2_static.h +18 -0
  160. package/runtime/xt_http2_api.h +38 -0
  161. package/runtime/xt_node_tls_api.h +37 -0
  162. package/runtime/xt_protocol_registry.c +22 -0
  163. package/runtime/xt_storage/indexed_db.c +408 -0
  164. package/runtime/xt_storage/indexed_db_internal.h +26 -0
  165. package/runtime/xt_storage/indexed_db_store.c +305 -0
  166. package/runtime/xt_storage/storage.c +209 -0
  167. package/runtime/xt_storage/storage_internal.h +66 -0
  168. package/runtime/xt_tls/parts/api.inc +21 -0
  169. package/runtime/xt_tls/parts/client.inc +112 -0
  170. package/runtime/xt_tls/parts/io.inc +157 -0
  171. package/runtime/xt_tls/parts/server.inc +102 -0
  172. package/runtime/xt_tls/parts/setup.inc +146 -0
  173. package/runtime/xt_tls/tls_bridge.c +27 -0
  174. package/runtime/xt_tls/tls_core.c +39 -0
  175. package/runtime/xt_tls/tls_internal.h +73 -0
  176. package/runtime/xt_tls_api.h +73 -0
  177. package/scripts/build-gui.ts +9 -8
  178. package/scripts/build-media.ts +2 -2
  179. package/scripts/build-tls.ts +248 -0
  180. package/scripts/check-file-length.ts +4 -21
  181. package/scripts/fetch-vendored.ts +244 -0
  182. package/scripts/package-release.ts +13 -3
  183. package/src/cli/main.ts +1 -0
  184. package/src/driver/build-key.ts +13 -2
  185. package/src/driver/compiler.ts +13 -0
  186. package/src/driver/config.ts +35 -0
  187. package/src/driver/fingerprint.ts +24 -0
  188. package/src/driver/index.ts +8 -0
  189. package/src/driver/link-features.ts +33 -11
  190. package/src/driver/options.ts +8 -1
  191. package/src/driver/paths.ts +20 -0
  192. package/src/driver/runtime-objects.ts +25 -6
  193. package/src/driver/storage.ts +84 -0
  194. package/src/driver/toolchain.ts +11 -2
  195. package/src/driver/version.ts +1 -1
  196. package/src/extensions/gui/features.ts +2 -2
  197. package/src/extensions/gui/index.ts +3 -1
  198. package/src/extensions/node/http2/feature.ts +33 -0
  199. package/src/extensions/node/http2/index.ts +6 -1
  200. package/src/extensions/node/index.ts +3 -1
  201. package/src/extensions/node/tls/feature.ts +29 -0
  202. package/src/extensions/node/tls/index.ts +5 -1
  203. package/src/extensions/node/zlib/compression.ts +11 -6
  204. package/src/extensions/registry.ts +9 -2
  205. package/src/extensions/storage.ts +58 -0
  206. package/src/extensions/tls.ts +52 -0
  207. package/vendor/sqlite3/sqlite3.c +271131 -0
  208. package/vendor/sqlite3/sqlite3.h +14447 -0
  209. package/{runtime/ext_gui → vendor/stb}/stb_image.h +17 -1
  210. /package/runtime/{ext_media → xt_media}/decode_audio.cpp +0 -0
  211. /package/runtime/{ext_media → xt_media}/media.cpp +0 -0
  212. /package/runtime/{ext_media → xt_media}/media_bridge.c +0 -0
  213. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz.c +0 -0
  214. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz.h +0 -0
  215. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_common.h +0 -0
  216. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_export.h +0 -0
  217. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tdef.c +0 -0
  218. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tdef.h +0 -0
  219. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tinfl.c +0 -0
  220. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tinfl.h +0 -0
  221. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_zip.h +0 -0
  222. /package/{runtime/ext_gui → vendor/nanosvg}/nanosvg.h +0 -0
  223. /package/{runtime/ext_gui → vendor/nanosvg}/nanosvgrast.h +0 -0
  224. /package/{runtime/ext_gui → vendor/stb}/stb_truetype.h +0 -0
@@ -0,0 +1,112 @@
1
+ /* -- client --------------------------------------------------------------- */
2
+
3
+ static int xt_tls_parse_identity(xt_tls_setup *setup, const char *cert_pem, const char *key_pem,
4
+ char *err, size_t errlen) {
5
+ if (cert_pem != NULL && cert_pem[0] != '\0') {
6
+ int ret = mbedtls_x509_crt_parse(&setup->cert, (const unsigned char *)cert_pem,
7
+ strlen(cert_pem) + 1);
8
+ if (ret != 0) {
9
+ snprintf(err, errlen, "cannot parse the client certificate (mbedtls %d)", ret);
10
+ return -1;
11
+ }
12
+ setup->has_cert = 1;
13
+ }
14
+ if (key_pem != NULL && key_pem[0] != '\0') {
15
+ int ret = mbedtls_pk_parse_key(&setup->key, (const unsigned char *)key_pem,
16
+ strlen(key_pem) + 1, NULL, 0, mbedtls_ctr_drbg_random,
17
+ &setup->ctr_drbg);
18
+ if (ret != 0) {
19
+ snprintf(err, errlen, "cannot parse the client private key (mbedtls %d)", ret);
20
+ return -1;
21
+ }
22
+ setup->has_key = 1;
23
+ }
24
+ if (setup->has_cert != setup->has_key) {
25
+ snprintf(err, errlen, "a client certificate and key must be provided together");
26
+ return -1;
27
+ }
28
+ return 0;
29
+ }
30
+
31
+ static int xt_tls_tcp_connect(const char *host, int port) {
32
+ char service[16];
33
+ snprintf(service, sizeof(service), "%d", port);
34
+ struct addrinfo hints;
35
+ memset(&hints, 0, sizeof(hints));
36
+ hints.ai_family = AF_UNSPEC;
37
+ hints.ai_socktype = SOCK_STREAM;
38
+ struct addrinfo *result = NULL;
39
+ if (getaddrinfo(host, service, &hints, &result) != 0) return -1;
40
+ int fd = -1;
41
+ for (struct addrinfo *info = result; info != NULL; info = info->ai_next) {
42
+ int candidate = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
43
+ if (candidate < 0) continue;
44
+ if (connect(candidate, info->ai_addr, (xt_tls_socklen_t)info->ai_addrlen) == 0) {
45
+ fd = candidate;
46
+ break;
47
+ }
48
+ xt_tls_closesocket(candidate);
49
+ }
50
+ freeaddrinfo(result);
51
+ return fd;
52
+ }
53
+
54
+ static xt_tls_conn *xt_tls_client_connect(const char *host, int port, const char *servername,
55
+ int verify, const char *ca_pem, const char *cert_pem,
56
+ const char *key_pem, const char *alpn, char *err,
57
+ size_t errlen) {
58
+ xt_tls_conn *conn = (xt_tls_conn *)calloc(1, sizeof(xt_tls_conn));
59
+ if (conn == NULL) {
60
+ snprintf(err, errlen, "out of memory");
61
+ return NULL;
62
+ }
63
+ conn->fd = -1;
64
+ conn->owns_setup = 1;
65
+ conn->setup = (xt_tls_setup *)calloc(1, sizeof(xt_tls_setup));
66
+ if (conn->setup == NULL) {
67
+ snprintf(err, errlen, "out of memory");
68
+ free(conn);
69
+ return NULL;
70
+ }
71
+ xt_tls_setup *setup = conn->setup;
72
+ mbedtls_ssl_init(&conn->ssl);
73
+ if (xt_tls_setup_crypto(setup, err, errlen) != 0) goto fail;
74
+ if (xt_tls_parse_identity(setup, cert_pem, key_pem, err, errlen) != 0) goto fail;
75
+ if (verify && xt_tls_setup_trust(setup, ca_pem, err, errlen) != 0) goto fail;
76
+ if (mbedtls_ssl_config_defaults(&setup->config, MBEDTLS_SSL_IS_CLIENT,
77
+ MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT) != 0) {
78
+ snprintf(err, errlen, "cannot initialise the TLS client configuration");
79
+ goto fail;
80
+ }
81
+ mbedtls_ssl_conf_rng(&setup->config, mbedtls_ctr_drbg_random, &setup->ctr_drbg);
82
+ if (verify) {
83
+ mbedtls_ssl_conf_authmode(&setup->config, MBEDTLS_SSL_VERIFY_REQUIRED);
84
+ mbedtls_ssl_conf_ca_chain(&setup->config, &setup->ca, NULL);
85
+ } else {
86
+ mbedtls_ssl_conf_authmode(&setup->config, MBEDTLS_SSL_VERIFY_NONE);
87
+ }
88
+ if (setup->has_cert) mbedtls_ssl_conf_own_cert(&setup->config, &setup->cert, &setup->key);
89
+ xt_tls_setup_alpn(setup, alpn);
90
+ if (setup->alpn_count > 0) mbedtls_ssl_conf_alpn_protocols(&setup->config, setup->alpn_list);
91
+ if (mbedtls_ssl_setup(&conn->ssl, &setup->config) != 0) {
92
+ snprintf(err, errlen, "cannot initialise the TLS session");
93
+ goto fail;
94
+ }
95
+ if (servername != NULL && servername[0] != '\0') mbedtls_ssl_set_hostname(&conn->ssl, servername);
96
+ conn->fd = xt_tls_tcp_connect(host, port);
97
+ if (conn->fd < 0) {
98
+ snprintf(err, errlen, "connect ECONNREFUSED %s:%d", host, port);
99
+ goto fail;
100
+ }
101
+ xt_tls_set_blocking(conn->fd);
102
+ mbedtls_ssl_set_bio(&conn->ssl, conn, xt_tls_send_bio, xt_tls_recv_bio, NULL);
103
+ if (xt_tls_finish_handshake(conn, verify) != 0) {
104
+ snprintf(err, errlen, "%s", conn->error);
105
+ goto fail;
106
+ }
107
+ return conn;
108
+
109
+ fail:
110
+ xt_tls_conn_close(conn);
111
+ return NULL;
112
+ }
@@ -0,0 +1,157 @@
1
+ /* -- blocking socket BIO and stream I/O ----------------------------------- */
2
+
3
+ /*
4
+ * Every connection this backend owns is switched to blocking mode: `fetch`
5
+ * runs its exchange on an async worker, and the Node sockets only call
6
+ * `read`/`write` after the reactor reported readiness. Blocking here keeps the
7
+ * SSL state machine simple and never busy-loops.
8
+ */
9
+
10
+ #if defined(_WIN32)
11
+ #include <winsock2.h>
12
+ typedef int xt_tls_socklen_t;
13
+ #define xt_tls_closesocket closesocket
14
+ #else
15
+ #include <fcntl.h>
16
+ #include <netdb.h>
17
+ #include <sys/socket.h>
18
+ #include <unistd.h>
19
+ typedef socklen_t xt_tls_socklen_t;
20
+ #define xt_tls_closesocket close
21
+ #endif
22
+
23
+ static void xt_tls_set_blocking(int fd) {
24
+ #if defined(_WIN32)
25
+ u_long mode = 0;
26
+ ioctlsocket(fd, FIONBIO, &mode);
27
+ #else
28
+ int flags = fcntl(fd, F_GETFL, 0);
29
+ if (flags >= 0) fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
30
+ #endif
31
+ }
32
+
33
+ int xt_tls_send_bio(void *ctx, const unsigned char *buf, size_t len) {
34
+ xt_tls_conn *conn = (xt_tls_conn *)ctx;
35
+ size_t sent = 0;
36
+ while (sent < len) {
37
+ #if defined(_WIN32)
38
+ int written = send(conn->fd, (const char *)buf + sent, (int)(len - sent), 0);
39
+ #else
40
+ ssize_t written = send(conn->fd, buf + sent, len - sent, 0);
41
+ #endif
42
+ if (written > 0) {
43
+ sent += (size_t)written;
44
+ continue;
45
+ }
46
+ if (written < 0) return MBEDTLS_ERR_NET_SEND_FAILED;
47
+ return MBEDTLS_ERR_SSL_WANT_WRITE;
48
+ }
49
+ return (int)sent;
50
+ }
51
+
52
+ int xt_tls_recv_bio(void *ctx, unsigned char *buf, size_t len) {
53
+ xt_tls_conn *conn = (xt_tls_conn *)ctx;
54
+ #if defined(_WIN32)
55
+ int count = recv(conn->fd, (char *)buf, (int)len, 0);
56
+ #else
57
+ ssize_t count = recv(conn->fd, buf, len, 0);
58
+ #endif
59
+ if (count < 0) return MBEDTLS_ERR_NET_RECV_FAILED;
60
+ return (int)count;
61
+ }
62
+
63
+ static int xt_tls_conn_read(xt_tls_conn *conn, unsigned char *buffer, size_t capacity) {
64
+ if (conn == NULL) return -1;
65
+ for (;;) {
66
+ int ret = mbedtls_ssl_read(&conn->ssl, buffer, capacity);
67
+ if (ret > 0) return ret;
68
+ if (ret == 0 || ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) return 0;
69
+ if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) continue;
70
+ xt_tls_conn_fail(conn, "TLS read failed", ret);
71
+ return -1;
72
+ }
73
+ }
74
+
75
+ static int xt_tls_conn_write(xt_tls_conn *conn, const unsigned char *data, size_t length) {
76
+ if (conn == NULL) return -1;
77
+ size_t sent = 0;
78
+ while (sent < length) {
79
+ int ret = mbedtls_ssl_write(&conn->ssl, data + sent, length - sent);
80
+ if (ret > 0) {
81
+ sent += (size_t)ret;
82
+ continue;
83
+ }
84
+ if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) continue;
85
+ xt_tls_conn_fail(conn, "TLS write failed", ret);
86
+ return -1;
87
+ }
88
+ return 0;
89
+ }
90
+
91
+ static void xt_tls_conn_shutdown(xt_tls_conn *conn) {
92
+ if (conn == NULL) return;
93
+ /* Best effort: the peer may already be gone. */
94
+ (void)mbedtls_ssl_close_notify(&conn->ssl);
95
+ }
96
+
97
+ void xt_tls_conn_free(xt_tls_conn *conn) {
98
+ if (conn == NULL) return;
99
+ mbedtls_ssl_free(&conn->ssl);
100
+ if (conn->owns_setup && conn->setup != NULL) {
101
+ xt_tls_setup_free(conn->setup);
102
+ free(conn->setup);
103
+ }
104
+ free(conn);
105
+ }
106
+
107
+ static void xt_tls_conn_close(xt_tls_conn *conn) {
108
+ if (conn == NULL) return;
109
+ xt_tls_conn_shutdown(conn);
110
+ if (conn->fd >= 0) xt_tls_closesocket(conn->fd);
111
+ conn->fd = -1;
112
+ xt_tls_conn_free(conn);
113
+ }
114
+
115
+ static const char *xt_tls_conn_alpn(xt_tls_conn *conn) {
116
+ if (conn == NULL) return NULL;
117
+ const char *protocol = mbedtls_ssl_get_alpn_protocol(&conn->ssl);
118
+ if (protocol == NULL) return NULL;
119
+ snprintf(conn->alpn, sizeof(conn->alpn), "%s", protocol);
120
+ return conn->alpn;
121
+ }
122
+
123
+ static const char *xt_tls_conn_error(xt_tls_conn *conn) {
124
+ return conn == NULL ? "TLS connection is not available" : conn->error;
125
+ }
126
+
127
+ static int xt_tls_conn_fd(xt_tls_conn *conn) { return conn == NULL ? -1 : conn->fd; }
128
+
129
+ static int xt_tls_finish_handshake(xt_tls_conn *conn, int verify) {
130
+ int ret;
131
+ while ((ret = mbedtls_ssl_handshake(&conn->ssl)) != 0) {
132
+ if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) continue;
133
+ /* A certificate failure aborts the handshake itself; report the specific
134
+ * reason (expired, wrong host, untrusted root, ...) instead of a generic
135
+ * X509 error. */
136
+ uint32_t flags = mbedtls_ssl_get_verify_result(&conn->ssl);
137
+ if (flags != 0) {
138
+ char info[256];
139
+ mbedtls_x509_crt_verify_info(info, sizeof(info), "", flags);
140
+ snprintf(conn->error, sizeof(conn->error), "certificate verification failed: %s", info);
141
+ } else {
142
+ xt_tls_conn_fail(conn, "TLS handshake failed", ret);
143
+ }
144
+ return -1;
145
+ }
146
+ if (verify) {
147
+ uint32_t flags = mbedtls_ssl_get_verify_result(&conn->ssl);
148
+ if (flags != 0) {
149
+ char info[256];
150
+ mbedtls_x509_crt_verify_info(info, sizeof(info), "", flags);
151
+ snprintf(conn->error, sizeof(conn->error), "certificate verification failed: %s", info);
152
+ return -1;
153
+ }
154
+ }
155
+ xt_tls_conn_alpn(conn);
156
+ return 0;
157
+ }
@@ -0,0 +1,102 @@
1
+ /* -- server --------------------------------------------------------------- */
2
+
3
+ static int xt_tls_server_identity(xt_tls_setup *setup, const char *cert_pem, const char *key_pem,
4
+ char *err, size_t errlen) {
5
+ if (cert_pem == NULL || cert_pem[0] == '\0' || key_pem == NULL || key_pem[0] == '\0') {
6
+ snprintf(err, errlen, "a TLS server requires both a certificate and a private key");
7
+ return -1;
8
+ }
9
+ int ret = mbedtls_x509_crt_parse(&setup->cert, (const unsigned char *)cert_pem,
10
+ strlen(cert_pem) + 1);
11
+ if (ret != 0) {
12
+ snprintf(err, errlen, "cannot parse the server certificate (mbedtls %d)", ret);
13
+ return -1;
14
+ }
15
+ setup->has_cert = 1;
16
+ ret = mbedtls_pk_parse_key(&setup->key, (const unsigned char *)key_pem, strlen(key_pem) + 1,
17
+ NULL, 0, mbedtls_ctr_drbg_random, &setup->ctr_drbg);
18
+ if (ret != 0) {
19
+ snprintf(err, errlen, "cannot parse the server private key (mbedtls %d)", ret);
20
+ return -1;
21
+ }
22
+ setup->has_key = 1;
23
+ return 0;
24
+ }
25
+
26
+ static xt_tls_server_config *xt_tls_server_config_new(const char *cert_pem, const char *key_pem,
27
+ const char *ca_pem, int request_cert,
28
+ int verify, const char *alpn, char *err,
29
+ size_t errlen) {
30
+ xt_tls_server_config *config = (xt_tls_server_config *)calloc(1, sizeof(xt_tls_server_config));
31
+ if (config == NULL) {
32
+ snprintf(err, errlen, "out of memory");
33
+ return NULL;
34
+ }
35
+ config->verify = verify;
36
+ if (xt_tls_setup_crypto(&config->setup, err, errlen) != 0) goto fail;
37
+ if (xt_tls_server_identity(&config->setup, cert_pem, key_pem, err, errlen) != 0) goto fail;
38
+ if (mbedtls_ssl_config_defaults(&config->setup.config, MBEDTLS_SSL_IS_SERVER,
39
+ MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT) != 0) {
40
+ snprintf(err, errlen, "cannot initialise the TLS server configuration");
41
+ goto fail;
42
+ }
43
+ mbedtls_ssl_conf_rng(&config->setup.config, mbedtls_ctr_drbg_random, &config->setup.ctr_drbg);
44
+ mbedtls_ssl_conf_own_cert(&config->setup.config, &config->setup.cert, &config->setup.key);
45
+ if (request_cert) {
46
+ if (ca_pem == NULL || ca_pem[0] == '\0') {
47
+ if (xt_tls_setup_trust(&config->setup, NULL, err, errlen) != 0) goto fail;
48
+ } else if (xt_tls_setup_trust(&config->setup, ca_pem, err, errlen) != 0) {
49
+ goto fail;
50
+ }
51
+ mbedtls_ssl_conf_ca_chain(&config->setup.config, &config->setup.ca, NULL);
52
+ mbedtls_ssl_conf_authmode(&config->setup.config, MBEDTLS_SSL_VERIFY_REQUIRED);
53
+ } else {
54
+ mbedtls_ssl_conf_authmode(&config->setup.config, MBEDTLS_SSL_VERIFY_NONE);
55
+ }
56
+ xt_tls_setup_alpn(&config->setup, alpn);
57
+ if (config->setup.alpn_count > 0) {
58
+ mbedtls_ssl_conf_alpn_protocols(&config->setup.config, config->setup.alpn_list);
59
+ }
60
+ return config;
61
+
62
+ fail:
63
+ xt_tls_setup_free(&config->setup);
64
+ free(config);
65
+ return NULL;
66
+ }
67
+
68
+ static void xt_tls_server_config_free(xt_tls_server_config *config) {
69
+ if (config == NULL) return;
70
+ xt_tls_setup_free(&config->setup);
71
+ free(config);
72
+ }
73
+
74
+ static xt_tls_conn *xt_tls_server_accept(xt_tls_server_config *config, int fd, char *err,
75
+ size_t errlen) {
76
+ if (config == NULL) {
77
+ snprintf(err, errlen, "the TLS server has no credentials");
78
+ return NULL;
79
+ }
80
+ xt_tls_conn *conn = (xt_tls_conn *)calloc(1, sizeof(xt_tls_conn));
81
+ if (conn == NULL) {
82
+ snprintf(err, errlen, "out of memory");
83
+ return NULL;
84
+ }
85
+ conn->fd = fd;
86
+ conn->owns_setup = 0;
87
+ conn->setup = &config->setup;
88
+ mbedtls_ssl_init(&conn->ssl);
89
+ xt_tls_set_blocking(fd);
90
+ if (mbedtls_ssl_setup(&conn->ssl, &config->setup.config) != 0) {
91
+ snprintf(err, errlen, "cannot initialise the TLS server session");
92
+ xt_tls_conn_free(conn);
93
+ return NULL;
94
+ }
95
+ mbedtls_ssl_set_bio(&conn->ssl, conn, xt_tls_send_bio, xt_tls_recv_bio, NULL);
96
+ if (xt_tls_finish_handshake(conn, config->verify) != 0) {
97
+ snprintf(err, errlen, "%s", conn->error);
98
+ xt_tls_conn_free(conn);
99
+ return NULL;
100
+ }
101
+ return conn;
102
+ }
@@ -0,0 +1,146 @@
1
+ /* -- configuration, credentials and trust store ---------------------------- */
2
+
3
+ /*
4
+ * mbedTLS ships no platform trust-store loader. Windows exposes its roots
5
+ * through the crypto API; the Unix hosts use the conventional CA bundle paths
6
+ * (macOS ships `/etc/ssl/cert.pem`, the BSDs use the same file, and Linux
7
+ * distributions use one of the two bundle locations). A caller-supplied `ca`
8
+ * (Node's `tls.connect({ ca })`) bypasses the platform store entirely.
9
+ */
10
+
11
+ #if defined(_WIN32)
12
+ #include <wincrypt.h>
13
+ #include <windows.h>
14
+ #else
15
+ #include <unistd.h>
16
+ #endif
17
+
18
+ #include <stdio.h>
19
+ #include <stdlib.h>
20
+ #include <string.h>
21
+
22
+ int xt_tls_platform_init(void) { return 0; }
23
+
24
+ void xt_tls_setup_alpn(xt_tls_setup *setup, const char *list) {
25
+ setup->alpn_count = 0;
26
+ setup->alpn_list[0] = NULL;
27
+ if (list == NULL) return;
28
+ const char *cursor = list;
29
+ while (*cursor != '\0' && setup->alpn_count < XT_TLS_MAX_ALPN) {
30
+ const char *comma = strchr(cursor, ',');
31
+ size_t length = comma ? (size_t)(comma - cursor) : strlen(cursor);
32
+ if (length > 0 && length < XT_TLS_ALPN_MAX) {
33
+ memcpy(setup->alpn[setup->alpn_count], cursor, length);
34
+ setup->alpn[setup->alpn_count][length] = '\0';
35
+ setup->alpn_list[setup->alpn_count] = setup->alpn[setup->alpn_count];
36
+ setup->alpn_count++;
37
+ }
38
+ if (!comma) break;
39
+ cursor = comma + 1;
40
+ }
41
+ setup->alpn_list[setup->alpn_count] = NULL;
42
+ }
43
+
44
+ #if defined(_WIN32)
45
+ static int xt_tls_load_system_roots(mbedtls_x509_crt *chain) {
46
+ HCERTSTORE store = CertOpenSystemStoreA(0, "ROOT");
47
+ if (store == NULL) return -1;
48
+ int loaded = 0;
49
+ PCCERT_CONTEXT certificate = NULL;
50
+ while ((certificate = CertEnumCertificatesInStore(store, certificate)) != NULL) {
51
+ if (mbedtls_x509_crt_parse_der(chain, certificate->pbCertEncoded,
52
+ certificate->cbCertEncoded) == 0) {
53
+ loaded++;
54
+ }
55
+ }
56
+ CertCloseStore(store, 0);
57
+ return loaded > 0 ? 0 : -1;
58
+ }
59
+ #else
60
+ static int xt_tls_load_system_roots(mbedtls_x509_crt *chain) {
61
+ static const char *bundles[] = {
62
+ "/etc/ssl/certs/ca-certificates.crt", /* Debian/Ubuntu/Alpine */
63
+ "/etc/pki/tls/certs/ca-bundle.crt", /* Fedora/RHEL */
64
+ "/etc/ssl/cert.pem", /* macOS/BSD */
65
+ "/etc/ssl/ca-bundle.pem",
66
+ NULL,
67
+ };
68
+ for (size_t i = 0; bundles[i] != NULL; i++) {
69
+ if (access(bundles[i], R_OK) != 0) continue;
70
+ if (mbedtls_x509_crt_parse_file(chain, bundles[i]) == 0) return 0;
71
+ }
72
+ /* Last resort: every hashed certificate in the directory. */
73
+ return mbedtls_x509_crt_parse_path(chain, "/etc/ssl/certs");
74
+ }
75
+ #endif
76
+
77
+ int xt_tls_setup_trust(xt_tls_setup *setup, const char *ca_pem, char *err, size_t errlen) {
78
+ int loaded = 0;
79
+ if (ca_pem != NULL && ca_pem[0] != '\0') {
80
+ if (mbedtls_x509_crt_parse(&setup->ca, (const unsigned char *)ca_pem, strlen(ca_pem) + 1) != 0) {
81
+ snprintf(err, errlen, "cannot parse the supplied CA certificates");
82
+ return -1;
83
+ }
84
+ loaded = 1;
85
+ } else if (xt_tls_load_system_roots(&setup->ca) == 0) {
86
+ loaded = 1;
87
+ }
88
+ /* Node-compatible escape hatch for a private CA (and the e2e tests): the
89
+ * named PEM bundle is trusted in addition to the platform roots. */
90
+ const char *extra = getenv("NODE_EXTRA_CA_CERTS");
91
+ if (extra != NULL && extra[0] != '\0') {
92
+ if (mbedtls_x509_crt_parse_file(&setup->ca, extra) != 0) {
93
+ snprintf(err, errlen, "cannot read NODE_EXTRA_CA_CERTS (%s)", extra);
94
+ return -1;
95
+ }
96
+ loaded = 1;
97
+ }
98
+ if (!loaded) {
99
+ snprintf(err, errlen, "no CA certificates are available");
100
+ return -1;
101
+ }
102
+ setup->has_ca = 1;
103
+ return 0;
104
+ }
105
+
106
+ static int xt_tls_setup_crypto(xt_tls_setup *setup, char *err, size_t errlen) {
107
+ mbedtls_ssl_config_init(&setup->config);
108
+ mbedtls_entropy_init(&setup->entropy);
109
+ mbedtls_ctr_drbg_init(&setup->ctr_drbg);
110
+ mbedtls_x509_crt_init(&setup->ca);
111
+ mbedtls_x509_crt_init(&setup->cert);
112
+ mbedtls_pk_init(&setup->key);
113
+ setup->has_ca = setup->has_cert = setup->has_key = 0;
114
+ setup->alpn_count = 0;
115
+ setup->alpn_list[0] = NULL;
116
+ static const char pers[] = "xbintsc-tls";
117
+ int ret = mbedtls_ctr_drbg_seed(&setup->ctr_drbg, mbedtls_entropy_func, &setup->entropy,
118
+ (const unsigned char *)pers, sizeof(pers) - 1);
119
+ if (ret != 0) {
120
+ snprintf(err, errlen, "cannot seed the TLS RNG (mbedtls %d)", ret);
121
+ return -1;
122
+ }
123
+ setup->initialized = 1;
124
+ return 0;
125
+ }
126
+
127
+ void xt_tls_setup_free(xt_tls_setup *setup) {
128
+ if (setup == NULL) return;
129
+ mbedtls_ssl_config_free(&setup->config);
130
+ mbedtls_ctr_drbg_free(&setup->ctr_drbg);
131
+ mbedtls_entropy_free(&setup->entropy);
132
+ mbedtls_x509_crt_free(&setup->ca);
133
+ mbedtls_x509_crt_free(&setup->cert);
134
+ mbedtls_pk_free(&setup->key);
135
+ setup->initialized = 0;
136
+ }
137
+
138
+ void xt_tls_conn_fail(xt_tls_conn *conn, const char *message, int code) {
139
+ char detail[128];
140
+ if (code != 0) {
141
+ mbedtls_strerror(code, detail, sizeof(detail));
142
+ snprintf(conn->error, sizeof(conn->error), "%s (mbedtls: %s)", message, detail);
143
+ } else {
144
+ snprintf(conn->error, sizeof(conn->error), "%s", message);
145
+ }
146
+ }
@@ -0,0 +1,27 @@
1
+ /*
2
+ * xbintsc `tls` link feature — registration bridge.
3
+ *
4
+ * The mbedTLS transport (and `tls_core.c`) lives in `tls.lib`, which is linked
5
+ * only when the feature is active. A static archive member with just a
6
+ * constructor is never extracted, so this tiny translation unit — the
7
+ * feature's `runtimeSources()` entry — references `xt_tls_backend_boot()`.
8
+ * That call pulls the implementation (and the mbedTLS members it uses) out of
9
+ * the archive and installs the dispatch table before `main`.
10
+ *
11
+ * It intentionally includes no mbedTLS header: the npm package ships the
12
+ * archive, not the vendored headers, so a program that enables TLS must be
13
+ * compilable without `vendor/`.
14
+ */
15
+
16
+ extern void xt_tls_backend_boot(void);
17
+
18
+ #if defined(__GNUC__) || defined(__clang__)
19
+ __attribute__((constructor)) static void xt_tls_bridge_boot(void) {
20
+ xt_tls_backend_boot();
21
+ }
22
+ #elif defined(_MSC_VER)
23
+ void xt_tls_bridge_boot(void);
24
+ #pragma section(".CRT$XCU", read)
25
+ __declspec(allocate(".CRT$XCU")) static void (*const xt_tls_bridge_boot_ctor)(void) = xt_tls_bridge_boot;
26
+ void xt_tls_bridge_boot(void) { xt_tls_backend_boot(); }
27
+ #endif
@@ -0,0 +1,39 @@
1
+ /*
2
+ * xbintsc `tls` link feature — the mbedTLS transport.
3
+ *
4
+ * This translation unit is compiled only when a program may use TLS (an
5
+ * `https://` URL, `node:tls`, or an explicit `--feature tls`). It installs the
6
+ * `XtTlsApi` table into the always-linked core registry from a constructor, so
7
+ * `fetch` and `node:tls` can fall back to a clear runtime error when the
8
+ * feature is not linked instead of failing at link time.
9
+ *
10
+ * The mbedTLS implementation itself lives in the `tls` archive
11
+ * (`runtime/lib/<os>-<arch>/tls.{a,lib}`), built by `npm run tls`. This file
12
+ * is the feature's `runtimeSources()` entry: linking it directly is what pulls
13
+ * the archive members it references out of the static library.
14
+ */
15
+
16
+ #if defined(_WIN32)
17
+ /* winsock2.h must precede windows.h; the configuration code below includes the
18
+ * latter for the certificate store API. */
19
+ #include <winsock2.h>
20
+ #include <ws2tcpip.h>
21
+ #endif
22
+
23
+ #include "../xt_tls_api.h"
24
+ #include "tls_internal.h"
25
+
26
+ #include <stdio.h>
27
+ #include <stdlib.h>
28
+ #include <string.h>
29
+
30
+ #include "mbedtls/error.h"
31
+
32
+ static int xt_tls_finish_handshake(xt_tls_conn *conn, int verify);
33
+ static void xt_tls_conn_close(xt_tls_conn *conn);
34
+
35
+ #include "parts/setup.inc"
36
+ #include "parts/io.inc"
37
+ #include "parts/client.inc"
38
+ #include "parts/server.inc"
39
+ #include "parts/api.inc"
@@ -0,0 +1,73 @@
1
+ /*
2
+ * Shared state for the mbedTLS-backed TLS backend (`tls_core.c`).
3
+ *
4
+ * One `xt_tls_setup` owns the configuration and credentials; a
5
+ * `xt_tls_conn` owns one handshake/stream. Servers build the setup once and
6
+ * borrow it for every accepted connection, clients own one per connection.
7
+ */
8
+
9
+ #ifndef XT_TLS_INTERNAL_H
10
+ #define XT_TLS_INTERNAL_H
11
+
12
+ #include <stddef.h>
13
+
14
+ #include "mbedtls/ctr_drbg.h"
15
+ #include "mbedtls/entropy.h"
16
+ #include "mbedtls/net_sockets.h"
17
+ #include "mbedtls/pk.h"
18
+ #include "mbedtls/ssl.h"
19
+ #include "mbedtls/x509_crt.h"
20
+
21
+ /** How many ALPN protocols a single configuration accepts. */
22
+ #define XT_TLS_MAX_ALPN 8
23
+ #define XT_TLS_ALPN_MAX 64
24
+
25
+ typedef struct xt_tls_setup {
26
+ mbedtls_ssl_config config;
27
+ mbedtls_entropy_context entropy;
28
+ mbedtls_ctr_drbg_context ctr_drbg;
29
+ mbedtls_x509_crt ca;
30
+ mbedtls_x509_crt cert;
31
+ mbedtls_pk_context key;
32
+ char alpn[XT_TLS_MAX_ALPN][XT_TLS_ALPN_MAX];
33
+ const char *alpn_list[XT_TLS_MAX_ALPN + 1];
34
+ int alpn_count;
35
+ int has_ca;
36
+ int has_cert;
37
+ int has_key;
38
+ int initialized;
39
+ } xt_tls_setup;
40
+
41
+ struct xt_tls_conn {
42
+ int fd;
43
+ mbedtls_ssl_context ssl;
44
+ /* Borrowed for accepted connections, owned for clients. */
45
+ xt_tls_setup *setup;
46
+ int owns_setup;
47
+ char error[256];
48
+ char alpn[XT_TLS_ALPN_MAX];
49
+ };
50
+
51
+ struct xt_tls_server_config {
52
+ xt_tls_setup setup;
53
+ int verify;
54
+ };
55
+
56
+ int xt_tls_platform_init(void);
57
+
58
+ /** Parse a comma-separated ALPN list into `setup` (empty list clears it). */
59
+ void xt_tls_setup_alpn(xt_tls_setup *setup, const char *list);
60
+
61
+ /** Load `ca_pem` or, when NULL, the host trust store. */
62
+ int xt_tls_setup_trust(xt_tls_setup *setup, const char *ca_pem, char *err, size_t errlen);
63
+
64
+ void xt_tls_setup_free(xt_tls_setup *setup);
65
+
66
+ int xt_tls_send_bio(void *ctx, const unsigned char *buf, size_t len);
67
+ int xt_tls_recv_bio(void *ctx, unsigned char *buf, size_t len);
68
+
69
+ /** Write a formatted error into `conn->error`. */
70
+ void xt_tls_conn_fail(xt_tls_conn *conn, const char *message, int code);
71
+ void xt_tls_conn_free(xt_tls_conn *conn);
72
+
73
+ #endif /* XT_TLS_INTERNAL_H */