xbintsc 0.6.22 → 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 (166) hide show
  1. package/AGENTS.md +5 -0
  2. package/dist/src/cli/help.d.ts +1 -1
  3. package/dist/src/driver/build-key.js +2 -1
  4. package/dist/src/driver/build-key.js.map +1 -1
  5. package/dist/src/driver/fingerprint.d.ts +11 -0
  6. package/dist/src/driver/fingerprint.js +25 -0
  7. package/dist/src/driver/fingerprint.js.map +1 -1
  8. package/dist/src/driver/link-features.js +36 -11
  9. package/dist/src/driver/link-features.js.map +1 -1
  10. package/dist/src/driver/paths.d.ts +10 -0
  11. package/dist/src/driver/paths.js +18 -0
  12. package/dist/src/driver/paths.js.map +1 -1
  13. package/dist/src/driver/runtime-objects.d.ts +1 -1
  14. package/dist/src/driver/runtime-objects.js +22 -6
  15. package/dist/src/driver/runtime-objects.js.map +1 -1
  16. package/dist/src/driver/toolchain.d.ts +1 -1
  17. package/dist/src/driver/toolchain.js +10 -1
  18. package/dist/src/driver/toolchain.js.map +1 -1
  19. package/dist/src/driver/version.d.ts +1 -1
  20. package/dist/src/driver/version.js +1 -1
  21. package/dist/src/extensions/node/http2/feature.d.ts +14 -0
  22. package/dist/src/extensions/node/http2/feature.js +28 -0
  23. package/dist/src/extensions/node/http2/feature.js.map +1 -0
  24. package/dist/src/extensions/node/http2/index.d.ts +5 -1
  25. package/dist/src/extensions/node/http2/index.js +6 -1
  26. package/dist/src/extensions/node/http2/index.js.map +1 -1
  27. package/dist/src/extensions/node/index.js +3 -1
  28. package/dist/src/extensions/node/index.js.map +1 -1
  29. package/dist/src/extensions/node/tls/feature.d.ts +12 -0
  30. package/dist/src/extensions/node/tls/feature.js +24 -0
  31. package/dist/src/extensions/node/tls/feature.js.map +1 -0
  32. package/dist/src/extensions/node/tls/index.d.ts +5 -1
  33. package/dist/src/extensions/node/tls/index.js +5 -1
  34. package/dist/src/extensions/node/tls/index.js.map +1 -1
  35. package/dist/src/extensions/node/zlib/compression.js +11 -6
  36. package/dist/src/extensions/node/zlib/compression.js.map +1 -1
  37. package/dist/src/extensions/registry.js +2 -1
  38. package/dist/src/extensions/registry.js.map +1 -1
  39. package/dist/src/extensions/storage.js +4 -3
  40. package/dist/src/extensions/storage.js.map +1 -1
  41. package/dist/src/extensions/tls.d.ts +26 -0
  42. package/dist/src/extensions/tls.js +46 -0
  43. package/dist/src/extensions/tls.js.map +1 -0
  44. package/dist/tests/driver/link-features.test.js +9 -0
  45. package/dist/tests/driver/link-features.test.js.map +1 -1
  46. package/dist/tests/e2e/fetch.test.js +3 -1
  47. package/dist/tests/e2e/fetch.test.js.map +1 -1
  48. package/dist/tests/e2e/http2.test.d.ts +7 -0
  49. package/dist/tests/e2e/http2.test.js +201 -0
  50. package/dist/tests/e2e/http2.test.js.map +1 -0
  51. package/dist/tests/e2e/net-helpers.d.ts +28 -0
  52. package/dist/tests/e2e/net-helpers.js +59 -0
  53. package/dist/tests/e2e/net-helpers.js.map +1 -0
  54. package/dist/tests/e2e/node-modules.test.js +3 -5
  55. package/dist/tests/e2e/node-modules.test.js.map +1 -1
  56. package/dist/tests/e2e/tls.test.d.ts +10 -0
  57. package/dist/tests/e2e/tls.test.js +197 -0
  58. package/dist/tests/e2e/tls.test.js.map +1 -0
  59. package/dist/tests/extensions/storage.test.js +3 -2
  60. package/dist/tests/extensions/storage.test.js.map +1 -1
  61. package/doc/ai/contributing.md +14 -0
  62. package/doc/ai/extensions.md +5 -3
  63. package/doc/ai/language-support.md +1 -1
  64. package/doc/ai/zh-CN/contributing.md +12 -0
  65. package/doc/ai/zh-CN/extensions.md +4 -2
  66. package/doc/ai/zh-CN/language-support.md +1 -1
  67. package/doc/gui/api.md +1 -1
  68. package/doc/gui/build.md +5 -3
  69. package/doc/gui/rendering.md +1 -1
  70. package/doc/gui.md +1 -1
  71. package/doc/implemented/async.md +1 -1
  72. package/doc/implemented/extensions.md +1 -0
  73. package/doc/implemented/fetch.md +19 -4
  74. package/doc/implemented/node/http2.md +51 -0
  75. package/doc/implemented/node/tls.md +56 -0
  76. package/doc/implemented/node.md +8 -0
  77. package/doc/implemented/storage.md +2 -1
  78. package/doc/implemented.md +2 -2
  79. package/doc/unimplemented/node/modules.md +6 -6
  80. package/doc/unimplemented/toolchain.md +1 -1
  81. package/doc/zh-CN/gui/api.md +1 -1
  82. package/doc/zh-CN/gui/build.md +4 -2
  83. package/doc/zh-CN/gui/rendering.md +1 -1
  84. package/doc/zh-CN/gui.md +1 -1
  85. package/doc/zh-CN/implemented/async.md +1 -1
  86. package/doc/zh-CN/implemented/extensions.md +1 -0
  87. package/doc/zh-CN/implemented/fetch.md +15 -3
  88. package/doc/zh-CN/implemented/node/http2.md +44 -0
  89. package/doc/zh-CN/implemented/node/tls.md +47 -0
  90. package/doc/zh-CN/implemented/node.md +7 -0
  91. package/doc/zh-CN/implemented/storage.md +2 -1
  92. package/doc/zh-CN/implemented.md +2 -2
  93. package/doc/zh-CN/unimplemented/node/modules.md +3 -3
  94. package/doc/zh-CN/unimplemented/toolchain.md +1 -1
  95. package/package.json +16 -8
  96. package/runtime/ext_node/http2/http2.c +25 -11
  97. package/runtime/ext_node/http2/http2_backend.c +48 -0
  98. package/runtime/ext_node/http2/http2_bridge.c +25 -0
  99. package/runtime/ext_node/http2/parts/api.inc +40 -0
  100. package/runtime/ext_node/http2/parts/client.inc +215 -0
  101. package/runtime/ext_node/http2/parts/common.inc +330 -0
  102. package/runtime/ext_node/http2/parts/events.inc +139 -0
  103. package/runtime/ext_node/http2/parts/server.inc +301 -0
  104. package/runtime/ext_node/http2/parts/streams.inc +254 -0
  105. package/runtime/ext_node/tls/parts/api.inc +38 -0
  106. package/runtime/ext_node/tls/parts/connect.inc +107 -0
  107. package/runtime/ext_node/tls/parts/helpers.inc +121 -0
  108. package/runtime/ext_node/tls/parts/server.inc +101 -0
  109. package/runtime/ext_node/tls/parts/socket.inc +171 -0
  110. package/runtime/ext_node/tls/tls.c +21 -13
  111. package/runtime/ext_node/tls/tls_backend.c +50 -0
  112. package/runtime/ext_node/zlib/{miniz/compression_bridge.c → compression_bridge.c} +1 -1
  113. package/runtime/xt_fetch/async.inc +19 -12
  114. package/runtime/xt_fetch/socket.inc +55 -0
  115. package/runtime/xt_fetch.c +3 -0
  116. package/runtime/xt_http2/nghttp2_compat.h +19 -0
  117. package/runtime/xt_http2/nghttp2_static.h +18 -0
  118. package/runtime/xt_http2_api.h +38 -0
  119. package/runtime/xt_node_tls_api.h +37 -0
  120. package/runtime/xt_protocol_registry.c +22 -0
  121. package/runtime/xt_storage/storage_internal.h +3 -3
  122. package/runtime/xt_tls/parts/api.inc +21 -0
  123. package/runtime/xt_tls/parts/client.inc +112 -0
  124. package/runtime/xt_tls/parts/io.inc +157 -0
  125. package/runtime/xt_tls/parts/server.inc +102 -0
  126. package/runtime/xt_tls/parts/setup.inc +146 -0
  127. package/runtime/xt_tls/tls_bridge.c +27 -0
  128. package/runtime/xt_tls/tls_core.c +39 -0
  129. package/runtime/xt_tls/tls_internal.h +73 -0
  130. package/runtime/xt_tls_api.h +73 -0
  131. package/scripts/build-gui.ts +8 -7
  132. package/scripts/build-tls.ts +248 -0
  133. package/scripts/check-file-length.ts +4 -25
  134. package/scripts/fetch-vendored.ts +244 -0
  135. package/scripts/package-release.ts +13 -3
  136. package/src/driver/build-key.ts +7 -1
  137. package/src/driver/fingerprint.ts +24 -0
  138. package/src/driver/link-features.ts +33 -11
  139. package/src/driver/paths.ts +20 -0
  140. package/src/driver/runtime-objects.ts +25 -6
  141. package/src/driver/toolchain.ts +11 -2
  142. package/src/driver/version.ts +1 -1
  143. package/src/extensions/node/http2/feature.ts +33 -0
  144. package/src/extensions/node/http2/index.ts +6 -1
  145. package/src/extensions/node/index.ts +3 -1
  146. package/src/extensions/node/tls/feature.ts +29 -0
  147. package/src/extensions/node/tls/index.ts +5 -1
  148. package/src/extensions/node/zlib/compression.ts +11 -6
  149. package/src/extensions/registry.ts +2 -1
  150. package/src/extensions/storage.ts +4 -3
  151. package/src/extensions/tls.ts +52 -0
  152. package/{runtime/ext_gui → vendor/stb}/stb_image.h +17 -1
  153. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz.c +0 -0
  154. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz.h +0 -0
  155. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_common.h +0 -0
  156. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_export.h +0 -0
  157. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tdef.c +0 -0
  158. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tdef.h +0 -0
  159. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tinfl.c +0 -0
  160. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tinfl.h +0 -0
  161. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_zip.h +0 -0
  162. /package/{runtime/ext_gui → vendor/nanosvg}/nanosvg.h +0 -0
  163. /package/{runtime/ext_gui → vendor/nanosvg}/nanosvgrast.h +0 -0
  164. /package/{runtime/xt_storage → vendor}/sqlite3/sqlite3.c +0 -0
  165. /package/{runtime/xt_storage → vendor}/sqlite3/sqlite3.h +0 -0
  166. /package/{runtime/ext_gui → vendor/stb}/stb_truetype.h +0 -0
@@ -71,3 +71,58 @@ static int xt_fetch_recv(int fd, char *buffer, size_t capacity) {
71
71
  return count > 2147483647 ? 2147483647 : (int)count;
72
72
  #endif
73
73
  }
74
+
75
+ /* -- transport abstraction (plain TCP or TLS) ------------------------------ */
76
+
77
+ /*
78
+ * The worker is the same for both schemes: only connect/write/read/close
79
+ * differ. TLS is reached through the optional `tls` link feature's dispatch
80
+ * table, which is NULL in a program that never enables it; an `https://`
81
+ * request then rejects instead of failing to link.
82
+ */
83
+ typedef struct {
84
+ int fd;
85
+ xt_tls_conn *tls;
86
+ } xt_fetch_stream;
87
+
88
+ static int xt_fetch_stream_open(const xt_fetch_url *url, xt_fetch_stream *stream, char *err,
89
+ size_t errlen) {
90
+ stream->fd = -1;
91
+ stream->tls = NULL;
92
+ if (!url->secure) {
93
+ stream->fd = xt_fetch_connect(url);
94
+ return stream->fd < 0 ? -1 : 0;
95
+ }
96
+ if (xt_tls_core_api == NULL) {
97
+ snprintf(err, errlen, "https is not supported (the tls link feature is not linked)");
98
+ return -1;
99
+ }
100
+ int port = url->port[0] != '\0' ? atoi(url->port) : 443;
101
+ stream->tls = xt_tls_core_api->client_connect(url->host, port, url->host, 1, NULL, NULL, NULL,
102
+ "http/1.1", err, errlen);
103
+ if (stream->tls == NULL) return -1;
104
+ stream->fd = xt_tls_core_api->fd(stream->tls);
105
+ return 0;
106
+ }
107
+
108
+ static int xt_fetch_stream_send(xt_fetch_stream *stream, const char *data, size_t length) {
109
+ if (stream->tls != NULL) return xt_tls_core_api->write(stream->tls, (const unsigned char *)data, length);
110
+ return xt_fetch_send_all(stream->fd, data, length);
111
+ }
112
+
113
+ static int xt_fetch_stream_recv(xt_fetch_stream *stream, char *buffer, size_t capacity) {
114
+ if (stream->tls != NULL) {
115
+ return xt_tls_core_api->read(stream->tls, (unsigned char *)buffer, capacity);
116
+ }
117
+ return xt_fetch_recv(stream->fd, buffer, capacity);
118
+ }
119
+
120
+ static void xt_fetch_stream_close(xt_fetch_stream *stream) {
121
+ if (stream->tls != NULL) {
122
+ xt_tls_core_api->close(stream->tls);
123
+ stream->tls = NULL;
124
+ } else if (stream->fd >= 0) {
125
+ xt_fetch_close_socket(stream->fd);
126
+ }
127
+ stream->fd = -1;
128
+ }
@@ -14,6 +14,9 @@
14
14
 
15
15
  #include "rt_internal.h"
16
16
 
17
+ /* The optional mbedTLS backend's dispatch table (`tls` link feature). */
18
+ #include "xt_tls_api.h"
19
+
17
20
  #include <stdio.h>
18
21
  #include <stdlib.h>
19
22
  #include <string.h>
@@ -0,0 +1,19 @@
1
+ /*
2
+ * Forced-include shim for the vendored nghttp2 build.
3
+ *
4
+ * nghttp2's public header uses POSIX `ssize_t`, which the MSVC/UCRT headers
5
+ * clang targets on Windows do not define. The xbintsc runtime header does
6
+ * define it (`rt.h`), but nghttp2's own translation units do not include that
7
+ * header, so the archive build passes this file with `-include`. The guards
8
+ * make it a no-op on POSIX and when `rt.h` was already seen.
9
+ */
10
+ #ifndef XT_NGHTTP2_COMPAT_H
11
+ #define XT_NGHTTP2_COMPAT_H
12
+
13
+ #if defined(_WIN32) && !defined(_SSIZE_T_DEFINED)
14
+ #include <stdint.h>
15
+ typedef intptr_t ssize_t;
16
+ #define _SSIZE_T_DEFINED
17
+ #endif
18
+
19
+ #endif /* XT_NGHTTP2_COMPAT_H */
@@ -0,0 +1,18 @@
1
+ /*
2
+ * Include nghttp2 as a static library.
3
+ *
4
+ * The archive is linked directly into the executable, so the public header must
5
+ * not decorate its declarations with `__declspec(dllimport)` on Windows. The
6
+ * vendored library is built with `NGHTTP2_STATICLIB`; consumers must define it
7
+ * before including `<nghttp2/nghttp2.h>` too.
8
+ */
9
+ #ifndef XT_NGHTTP2_STATIC_H
10
+ #define XT_NGHTTP2_STATIC_H
11
+
12
+ #ifndef NGHTTP2_STATICLIB
13
+ #define NGHTTP2_STATICLIB 1
14
+ #endif
15
+
16
+ #include <nghttp2/nghttp2.h>
17
+
18
+ #endif /* XT_NGHTTP2_STATIC_H */
@@ -0,0 +1,38 @@
1
+ /*
2
+ * High-level `node:http2` surface provided by the `http2` link feature.
3
+ *
4
+ * Same shape as `xt_node_tls_api.h`: the always-linked `http2` module keeps the
5
+ * importable bindings and forwards to this table, which the nghttp2-backed
6
+ * feature installs in a constructor.
7
+ */
8
+
9
+ #ifndef XT_HTTP2_API_H
10
+ #define XT_HTTP2_API_H
11
+
12
+ #include "rt.h"
13
+
14
+ #ifdef __cplusplus
15
+ extern "C" {
16
+ #endif
17
+
18
+ typedef struct xt_http2_api {
19
+ /** `http2.connect(authority[, options][, listener])`. */
20
+ xt_value (*connect)(int32_t argc, xt_value *argv);
21
+ /** `http2.createServer([options][, onRequest])` (cleartext h2c). */
22
+ xt_value (*create_server)(int32_t argc, xt_value *argv);
23
+ /** `http2.createSecureServer(options[, onRequest])` (TLS + ALPN h2). */
24
+ xt_value (*create_secure_server)(int32_t argc, xt_value *argv);
25
+ /** `http2.constants` (frame/error code numbers). */
26
+ xt_value (*constants)(void);
27
+ } XtHttp2Api;
28
+
29
+ /** The installed backend, or NULL when the `http2` feature is not linked. */
30
+ extern const XtHttp2Api *xt_http2_api;
31
+
32
+ void xt_http2_register(const XtHttp2Api *api);
33
+
34
+ #ifdef __cplusplus
35
+ }
36
+ #endif
37
+
38
+ #endif /* XT_HTTP2_API_H */
@@ -0,0 +1,37 @@
1
+ /*
2
+ * High-level `node:tls` surface provided by the `node-tls` link feature.
3
+ *
4
+ * The always-linked `tls` module keeps the importable bindings and falls back to
5
+ * a clear runtime error when the feature is absent. The feature's backend owns
6
+ * the JavaScript objects (`TLSSocket`, the server wrapper) and registers them
7
+ * here in a constructor, mirroring the `zlib` compression bridge.
8
+ */
9
+
10
+ #ifndef XT_NODE_TLS_API_H
11
+ #define XT_NODE_TLS_API_H
12
+
13
+ #include "rt.h"
14
+
15
+ #ifdef __cplusplus
16
+ extern "C" {
17
+ #endif
18
+
19
+ typedef struct xt_node_tls_api {
20
+ /** `tls.connect(options[, host][, callback])`. */
21
+ xt_value (*connect)(int32_t argc, xt_value *argv);
22
+ /** `tls.createServer(options[, secureConnectionListener])`. */
23
+ xt_value (*create_server)(int32_t argc, xt_value *argv);
24
+ /** `new tls.TLSSocket(...)`. */
25
+ xt_value (*socket_ctor)(int32_t argc, xt_value *argv);
26
+ } XtNodeTlsApi;
27
+
28
+ /** The installed backend, or NULL when the `node-tls` feature is not linked. */
29
+ extern const XtNodeTlsApi *xt_tls_node_api;
30
+
31
+ void xt_node_tls_register(const XtNodeTlsApi *api);
32
+
33
+ #ifdef __cplusplus
34
+ }
35
+ #endif
36
+
37
+ #endif /* XT_NODE_TLS_API_H */
@@ -0,0 +1,22 @@
1
+ /*
2
+ * Registration slots for the optional TLS and HTTP/2 backends.
3
+ *
4
+ * These globals live in the core runtime so a program that never enables the
5
+ * `tls`/`http2` link features still links: the fallbacks (rejecting `https://`,
6
+ * throwing from `node:tls`) only need the NULL check. The feature
7
+ * implementations install their tables from a constructor before `main`.
8
+ */
9
+
10
+ #include "xt_http2_api.h"
11
+ #include "xt_node_tls_api.h"
12
+ #include "xt_tls_api.h"
13
+
14
+ const XtTlsApi *xt_tls_core_api = NULL;
15
+ const XtNodeTlsApi *xt_tls_node_api = NULL;
16
+ const XtHttp2Api *xt_http2_api = NULL;
17
+
18
+ void xt_tls_core_register(const XtTlsApi *api) { xt_tls_core_api = api; }
19
+
20
+ void xt_node_tls_register(const XtNodeTlsApi *api) { xt_tls_node_api = api; }
21
+
22
+ void xt_http2_register(const XtHttp2Api *api) { xt_http2_api = api; }
@@ -2,9 +2,9 @@
2
2
  * Shared helpers for the xbintsc `storage` extension.
3
3
  *
4
4
  * localStorage / sessionStorage / indexedDB are implemented on top of a vendored
5
- * SQLite (runtime/xt_storage/sqlite3). This header owns the small value helpers
6
- * and the two database handles; the Web Storage surface lives in `storage.c`
7
- * and the IndexedDB surface in `indexed_db.c`.
5
+ * SQLite (vendor/sqlite3, fetched by `npm run vendor`). This header owns the
6
+ * small value helpers and the two database handles; the Web Storage surface
7
+ * lives in `storage.c` and the IndexedDB surface in `indexed_db.c`.
8
8
  */
9
9
  #ifndef XT_STORAGE_INTERNAL_H
10
10
  #define XT_STORAGE_INTERNAL_H
@@ -0,0 +1,21 @@
1
+ /* -- API table and registration ------------------------------------------- */
2
+
3
+ static const XtTlsApi kTlsApi = {
4
+ xt_tls_client_connect,
5
+ xt_tls_server_config_new,
6
+ xt_tls_server_accept,
7
+ xt_tls_conn_read,
8
+ xt_tls_conn_write,
9
+ xt_tls_conn_shutdown,
10
+ xt_tls_conn_close,
11
+ xt_tls_conn_free,
12
+ xt_tls_server_config_free,
13
+ xt_tls_conn_alpn,
14
+ xt_tls_conn_error,
15
+ xt_tls_conn_fd,
16
+ };
17
+
18
+ /* Called by `tls_bridge.c`, which is the feature's compiled `runtimeSources()`
19
+ * entry. The call is what makes the linker extract this object (and the mbedTLS
20
+ * members it references) from `tls.lib`. */
21
+ void xt_tls_backend_boot(void) { xt_tls_core_register(&kTlsApi); }
@@ -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
+ }