iodine 0.8.4 → 0.8.6

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.
@@ -1247,13 +1247,23 @@ See also: [Compiler Attributes](./001 compiler attributes.md).
1247
1247
 
1248
1248
  Enabled by `FIO_LEAK_COUNTER` (default `1`). `FIO_NO_LOG` and `FIO_LEAK_COUNTER` are mutually exclusive because leak reports are printed through the log.
1249
1249
 
1250
+ ### `FIO_LEAK_COUNTER_DEC`
1251
+
1252
+ ```c
1253
+ #define FIO_LEAK_COUNTER_DEC(name)
1254
+ ```
1255
+
1256
+ Declares a named counter and its cleanup function without defining them. Use it in declarations-only `FIO_EXTERN` translation units that reference the counter but must not emit a definition.
1257
+
1250
1258
  ### `FIO_LEAK_COUNTER_DEF`
1251
1259
 
1252
1260
  ```c
1253
1261
  #define FIO_LEAK_COUNTER_DEF(name)
1254
1262
  ```
1255
1263
 
1256
- Defines a named counter and a cleanup function that reports remaining allocations at process exit.
1264
+ Defines a named counter, a cleanup function that reports remaining allocations at process exit, and a constructor that registers the cleanup with the `FIO_CALL_AFTER_EXIT` state event (skipped when `FIO_LEAK_COUNTER_SKIP_EXIT` is set).
1265
+
1266
+ Emits non-`static` symbols, so it belongs in exactly one translation unit: the `FIO_EXTERN_COMPLETE` unit when the header is split, or every unit in a header-only build. Declarations-only units use `FIO_LEAK_COUNTER_DEC` instead.
1257
1267
 
1258
1268
  ### `FIO_LEAK_COUNTER_ON_ALLOC`
1259
1269
 
@@ -3482,53 +3492,54 @@ Most defaults are guarded with `#ifndef`; define them before including the heade
3482
3492
 
3483
3493
  ### Memory and Safety
3484
3494
 
3485
- - **`FIO_LEAK_COUNTER`** — Enables memory-leak counting. When enabled, the leak counter tracks allocations made through facil.io allocators and prints a summary at exit.
3486
- Default: `1` (enabled).
3487
- Set to `0` to disable counting and the exit report.
3495
+ - **`FIO_LEAK_COUNTER`** — Enables memory-leak counting. When enabled, the leak counter tracks allocations made through facil.io allocators and prints a summary at exit.
3496
+ Default: `1` (enabled).
3497
+ Set to `0` to disable counting and the exit report.
3488
3498
  Note: this conflicts with `FIO_NO_LOG`, because the leak report uses the logging layer.
3489
- - **`FIO_LEAK_COUNTER_SKIP_EXIT`** — Skip the automatic leak report at exit when leak counting is enabled.
3499
+ - **`FIO_LEAK_COUNTER_SKIP_EXIT`** — Skip the automatic leak report at exit when leak counting is enabled.
3490
3500
  Default: `0` (report at exit).
3491
- - **`FIO_LEAK_COUNTER_DEF(name)`** — Declares a named leak counter.
3501
+ - **`FIO_LEAK_COUNTER_DEC(name)`** — Declares a named leak counter without defining it; use in declarations-only `FIO_EXTERN` units.
3502
+ - **`FIO_LEAK_COUNTER_DEF(name)`** — Defines a named leak counter, its cleanup and the `FIO_CALL_AFTER_EXIT` registration; only in the single `FIO_EXTERN_COMPLETE` unit (or every unit in a header-only build).
3492
3503
  - **`FIO_LEAK_COUNTER_ON_ALLOC(name)`** — Records an allocation against the named counter.
3493
3504
  - **`FIO_LEAK_COUNTER_ON_FREE(name)`** — Records a free against the named counter.
3494
3505
  - **`FIO_LEAK_COUNTER_COUNT(name)`** — Returns the current value of the named counter.
3495
- - **`FIO_MEMORY_INITIALIZE_ALLOCATIONS_DEFAULT`** — When non-zero, facil.io allocators zero-initialize returned memory by default.
3496
- Default: `1` (secure by default).
3506
+ - **`FIO_MEMORY_INITIALIZE_ALLOCATIONS_DEFAULT`** — When non-zero, facil.io allocators zero-initialize returned memory by default.
3507
+ Default: `1` (secure by default).
3497
3508
  Set to `0` if you want raw allocations and prefer to do your own initialization.
3498
- - **`FIO_MEM_PAGE_SIZE_LOG`** — Log₂ of the OS memory page size. The memory allocator uses this to round allocations and map memory efficiently.
3499
- Default: `12` (4096-byte pages).
3509
+ - **`FIO_MEM_PAGE_SIZE_LOG`** — Log₂ of the OS memory page size. The memory allocator uses this to round allocations and map memory efficiently.
3510
+ Default: `12` (4096-byte pages).
3500
3511
  Change it if you are targeting a system with a different page size.
3501
3512
  - **`FIO_STATIC_ALLOC_SAFE_CONCURRENCY_MAX`** — Default slot count for selected internal `FIO_STATIC_ALLOC_DEF` invocations (default: `256`). The macro does not use this setting implicitly; pass it explicitly as the fourth argument where appropriate.
3502
3513
 
3503
3514
  ### Concurrency
3504
3515
 
3505
- - **`FIO_USE_THREAD_MUTEX`** — Choose the locking primitive used by thread-safe modules.
3506
- Default: `0` (use facil.io spinlocks).
3507
- Set to `1` to use OS native mutexes (`pthread_mutex_t` on POSIX) instead.
3516
+ - **`FIO_USE_THREAD_MUTEX`** — Choose the locking primitive used by thread-safe modules.
3517
+ Default: `0` (use facil.io spinlocks).
3518
+ Set to `1` to use OS native mutexes (`pthread_mutex_t` on POSIX) instead.
3508
3519
  If you need this decision to apply to only a single `#include`, use `FIO_USE_THREAD_MUTEX_TMP` in [`./001 header.h`](./001%20header.h) instead.
3509
3520
 
3510
3521
  ### Performance and Alignment
3511
3522
 
3512
- - **`FIO_UNALIGNED_ACCESS`** — Allows facil.io to attempt unaligned memory access on CPUs that support it. When enabled, the library may use faster paths in helpers such as `fio_buf2uXX`.
3513
- Default: `1` (attempt detection).
3523
+ - **`FIO_UNALIGNED_ACCESS`** — Allows facil.io to attempt unaligned memory access on CPUs that support it. When enabled, the library may use faster paths in helpers such as `fio_buf2uXX`.
3524
+ Default: `1` (attempt detection).
3514
3525
  Set to `0` on strict-alignment architectures or if you want to forbid unaligned access entirely. The actual capability is reflected by `FIO_UNALIGNED_MEMORY_ACCESS_ENABLED`, which is derived at compile time.
3515
- - **`FIO_LIMIT_INTRINSIC_BUFFER`** — Limits register pressure in some pseudo-intrinsic loops by processing smaller batches and looping more.
3516
- Default: `1`.
3526
+ - **`FIO_LIMIT_INTRINSIC_BUFFER`** — Limits register pressure in some pseudo-intrinsic loops by processing smaller batches and looping more.
3527
+ Default: `1`.
3517
3528
  Set to `0` if you prefer larger unrolled buffers and do not mind the extra register use.
3518
3529
 
3519
3530
  ### Maps
3520
3531
 
3521
- - **`FIO_MAP_WARNING_BITSIZE`** — Bit size at which hash-map allocations trigger an internal warning threshold in the `imap`/`map` templates.
3522
- Default: `24`.
3532
+ - **`FIO_MAP_WARNING_BITSIZE`** — Bit size at which hash-map allocations trigger an internal warning threshold in the `imap`/`map` templates.
3533
+ Default: `24`.
3523
3534
  Raise or lower it to tune when the library starts worrying about map size during growth.
3524
3535
 
3525
3536
  ### Logging
3526
3537
 
3527
3538
  - **`FIO_NO_LOG`** — Define this to remove all logging output. Cannot be combined with `FIO_LEAK_COUNTER`; the two are mutually exclusive.
3528
- - **`FIO_LOG2STDERR`** — Low-level sink used by the logging macros.
3529
- Default: no-op.
3539
+ - **`FIO_LOG2STDERR`** — Low-level sink used by the logging macros.
3540
+ Default: no-op.
3530
3541
  Override to print to `stderr` or route messages to a custom destination.
3531
- - **`FIO_LOG_LENGTH_LIMIT`** — Approximate cap on a single formatted log message, limiting stack memory use.
3542
+ - **`FIO_LOG_LENGTH_LIMIT`** — Approximate cap on a single formatted log message, limiting stack memory use.
3532
3543
  Default: `1024`.
3533
3544
  - **`DEBUG`** — Define this to enable debug-only log macros (`FIO_LOG_D*`) and active `FIO_ASSERT_DEBUG` checks. Also disables SIMD/Neon intrinsics in [`./000 core.h`](./000%20core.h).
3534
3545
 
@@ -5327,6 +5338,8 @@ typedef struct {
5327
5338
  fio_buf_info_s cert;
5328
5339
  fio_buf_info_s trust;
5329
5340
  fio_buf_info_s pass;
5341
+ /** The URL query without the TLS hints at its head / tail. */
5342
+ fio_buf_info_s query;
5330
5343
  bool tls;
5331
5344
  } fio_url_tls_info_s;
5332
5345
  ```
@@ -5376,6 +5389,12 @@ Detects TLS from scheme or query. Implicit TLS schemes include `wss`, `https`, `
5376
5389
 
5377
5390
  Query keys `tls` or `ssl` enable TLS. A nonempty value other than `1` or `true` supplies both key and certificate. A nonempty `key` and `cert` pair also enables TLS; `pass` or `password` supplies its optional passphrase. A nonempty `trust` value enables TLS independently. `key`, `cert`, or passphrase parameters alone do not enable TLS.
5378
5391
 
5392
+ TLS hints are read from the whole query. The returned `query` is the URL query with the hints at its head / tail omitted, a slice of the original (no copy), so it can be forwarded to a peer: `u.query = fio_url_is_tls(u).query;` (`fio_http_connect` does this for the request query). Hints in the middle of the query are read **and** kept, a pass-through for application keys with colliding names. Only hints actually used are omitted: `tls` / `ssl` always, `trust` when nonempty, `key` / `cert` only when both are set, and `pass` / `password` only alongside that pair. i.e.:
5393
+
5394
+ - `?tls&page=1&trust=sys` => `page=1`
5395
+ - `?page=1&tls&sort=a` => `page=1&tls&sort=a` (TLS enabled)
5396
+ - `?key=API_KEY` => `key=API_KEY` (no `cert`, not a hint)
5397
+
5379
5398
  Input slices need no trailing NUL or padding. Returned slices borrow the input buffer. Debug diagnostics report only TLS status and material-presence flags, never material contents.
5380
5399
 
5381
5400
  ### Macros
@@ -17122,6 +17141,19 @@ void example(void) {
17122
17141
 
17123
17142
  ---
17124
17143
 
17144
+ ## Split (`FIO_EXTERN`) builds
17145
+
17146
+ When the header is split into a declarations-only `FIO_EXTERN` unit and one `FIO_EXTERN_COMPLETE` implementation unit, the leak counter generated for `FIO_REF_NAME` is emitted in two steps:
17147
+
17148
+ - `FIO_LEAK_COUNTER_DEF(FIO_REF_NAME)` — definitions (counter, cleanup, exit registration), placed under the complete-TU gate only.
17149
+ - `FIO_LEAK_COUNTER_DEC(FIO_REF_NAME)` — declarations only, placed under the gate that also enables the `static` helpers in declarations-only units.
17150
+
17151
+ The declarations are needed because the internal helper types are `static` in declarations-only translation units (see [`001 header.h`](./001%20header.h)), so those units still compile their own copies of the inline wrappers and therefore reference the counter symbols. `_DEC` makes the symbols visible without defining them, so the linker resolves them against the single `_DEF` in the implementation unit.
17152
+
17153
+ `FIO_WEAK` is not used for these symbols: a duplicate definition is a real error and is reported by the linker instead of being silently merged.
17154
+
17155
+ ---
17156
+
17125
17157
  ## Memory Layout
17126
17158
 
17127
17159
  ```
@@ -21975,8 +22007,11 @@ fio_http_connect("https://example.com/", NULL, .on_http = on_response,
21975
22007
  .verify = 1);
21976
22008
  ```
21977
22009
 
21978
- The `trust=sys` / `trust=<ca-file>` URL query also works, but the URL query
21979
- is sent to the server as the request query unless `h` already has one.
22010
+ The `trust=sys` / `trust=<ca-file>` URL query also works. The URL query is
22011
+ sent to the server as the request query (unless `h` already has one), with
22012
+ the TLS hints at its head / tail omitted (see `fio_url_is_tls`). Hints in the
22013
+ middle of the query are sent as-is, so place TLS hints first or last, e.g.,
22014
+ `https://example.com/?page=1&trust=sys` sends `?page=1`.
21980
22015
 
21981
22016
  **Note**: interim `1xx` responses other than `101` (e.g., `100 Continue`,
21982
22017
  `103 Early Hints`) are skipped (RFC 9110 §15.2). `on_http` runs once, for
@@ -50,7 +50,7 @@ static void *iodine_handle_exception(void *ignr) {
50
50
  (int)RSTRING_LEN(msg),
51
51
  RSTRING_PTR(msg));
52
52
  } else {
53
- FIO_LOG_ERROR("exposed exception message: %.*s: %.*s\n %s\n"
53
+ FIO_LOG_ERROR("exposed exception message: %.*s: %.*s\n"
54
54
  "BACKTRACE UNAVAILABLE!\n",
55
55
  (int)RSTRING_LEN(exc_class),
56
56
  RSTRING_PTR(exc_class),
@@ -203,6 +203,10 @@ static VALUE iodine_cli_parse(VALUE self, VALUE required) {
203
203
  FIO_CLI_INT("--max-body -maxbd (" FIO_MACRO2STR(
204
204
  FIO_HTTP_DEFAULT_MAX_BODY_SIZE) ") total message payload limit per "
205
205
  "request, in bytes."),
206
+ FIO_CLI_INT("--max-params -maxpr (" FIO_MACRO2STR(
207
+ FIO_HTTP_PARAMS_MAX_FIELDS) ") request params field limit (query "
208
+ "pairs, form fields and multipart "
209
+ "parts)."),
206
210
  FIO_CLI_INT("--keep-alive -k (" FIO_MACRO2STR(
207
211
  FIO_HTTP_DEFAULT_TIMEOUT) ") HTTP keep-alive timeout in seconds "
208
212
  "(0..255)"),
@@ -51,7 +51,8 @@ This module is the heart of Iodine's connection handling. It manages:
51
51
  - `length` - Body length in bytes
52
52
  - `read([maxlen], [out_string])` - Read body data
53
53
  - `gets([limit])` - Read line from body
54
- - `seek(pos)` / `rewind` - Seek in body
54
+ - `seek([pos])` - Seek in body (no argument returns the current position)
55
+ - `rewind` - Move back to the start of the body
55
56
  - `close_body` - Release body resources (frees buffer / temporary file)
56
57
 
57
58
  ### Network Information
@@ -840,10 +841,20 @@ FIO_SFUNC VALUE iodine_connection_body_seek(int argc, VALUE *argv, VALUE o) {
840
841
  return Qnil;
841
842
  int64_t pos = SSIZE_MAX;
842
843
  iodine_rb2c_arg(argc, argv, IODINE_ARG_NUM(pos, 0, "pos", 0));
844
+ if (pos == SSIZE_MAX) /* no position given: report the current one */
845
+ return ULL2NUM(fio_http_body_pos(c->http));
843
846
  pos = (long long)fio_http_body_seek(c->http, (ssize_t)pos);
844
847
  return ULL2NUM(pos);
845
848
  }
846
849
 
850
+ /* Rack `rack.input#rewind`: moves the read position back to the start. */
851
+ FIO_SFUNC VALUE iodine_connection_body_rewind(VALUE o) {
852
+ iodine_connection_s *c = iodine_connection_ptr(o);
853
+ if (!c->http)
854
+ return Qnil;
855
+ return ULL2NUM(fio_http_body_seek(c->http, 0));
856
+ }
857
+
847
858
  FIO_SFUNC VALUE iodine_connection_body_close(VALUE o) {
848
859
  VALUE r = Qtrue;
849
860
  iodine_connection_s *c = iodine_connection_ptr(o);
@@ -1760,20 +1771,8 @@ static void iodine_io_raw_on_attach(fio_io_s *io) {
1760
1771
 
1761
1772
  /** Tests whether a nonblocking client socket completed its connection. */
1762
1773
  static int iodine_io_raw_client_connect_failed(fio_io_s *io) {
1763
- int error = 0;
1764
- #if FIO_OS_WIN
1765
- int error_len = sizeof(error);
1766
- return fio___winsock_fn.getsockopt(fio_io_fd(io),
1767
- SOL_SOCKET,
1768
- SO_ERROR,
1769
- (char *)&error,
1770
- &error_len) ||
1771
- error;
1772
- #else
1773
- socklen_t error_len = sizeof(error);
1774
- return getsockopt(fio_io_fd(io), SOL_SOCKET, SO_ERROR, &error, &error_len) ||
1775
- error;
1776
- #endif
1774
+ /* pending SO_ERROR, or -1 if the query failed */
1775
+ return fio_sock_error(fio_io_fd(io)) != 0;
1777
1776
  }
1778
1777
 
1779
1778
  typedef struct {
@@ -2255,6 +2254,7 @@ FIO_IFUNC iodine_connection_args_s iodine_connection_parse_args(int argc,
2255
2254
  .max_header_size = (uint32_t)fio_cli_get_i("-maxhd"),
2256
2255
  .max_line_len = (uint32_t)fio_cli_get_i("-maxln"),
2257
2256
  .max_body_size = (size_t)fio_cli_get_i("-maxbd"),
2257
+ .max_params = (size_t)fio_cli_get_i("-maxpr"),
2258
2258
  .ws_max_msg_size = (size_t)fio_cli_get_i("-maxms"),
2259
2259
  .timeout = iodine_connection___cli_timeout("-k"),
2260
2260
  .ws_timeout = iodine_connection___cli_timeout("-ping"),
@@ -2266,6 +2266,7 @@ FIO_IFUNC iodine_connection_args_s iodine_connection_parse_args(int argc,
2266
2266
  },
2267
2267
  };
2268
2268
  VALUE proc = Qnil, handler_tmp = Qnil, tls_io_rb = Qnil, timeout = Qnil;
2269
+ VALUE verify_rb = Qnil;
2269
2270
  iodine_rb2c_arg(
2270
2271
  argc,
2271
2272
  argv,
@@ -2283,6 +2284,7 @@ FIO_IFUNC iodine_connection_args_s iodine_connection_parse_args(int argc,
2283
2284
  IODINE_ARG_U8(r.settings.timeout, 0, "timeout", 0),
2284
2285
  IODINE_ARG_U8(r.settings.ws_timeout, 0, "ping", 0),
2285
2286
  IODINE_ARG_BOOL(r.settings.log, 0, "log", 0),
2287
+ IODINE_ARG_RB(verify_rb, 0, "verify", 0),
2286
2288
  IODINE_ARG_BOOL(r.settings.compress_static, 0, "dynamic_deflate", 0),
2287
2289
  IODINE_ARG_BOOL(r.settings.compress_ws, 0, "websocket_deflate", 0),
2288
2290
  IODINE_ARG_BUF(r.method, 0, "method", 0),
@@ -2291,6 +2293,8 @@ FIO_IFUNC iodine_connection_args_s iodine_connection_parse_args(int argc,
2291
2293
  IODINE_ARG_RB(r.cookies, 0, "cookies", 0),
2292
2294
  IODINE_ARG_PROC(proc, 0, "block", 0));
2293
2295
  r.settings.compress_dynamic = r.settings.compress_static;
2296
+ /* client mode only: TLS peer verification (system trust if none set) */
2297
+ r.settings.verify = (verify_rb == Qtrue);
2294
2298
  r.settings.udata = (void *)handler_tmp;
2295
2299
  /* test for errors before allocating or protecting data */
2296
2300
 
@@ -2624,6 +2628,7 @@ static VALUE iodine_connection_initialize(int argc, VALUE *argv, VALUE self) {
2624
2628
  .protocol = protocol,
2625
2629
  .udata = (void *)self,
2626
2630
  .tls = args.settings.tls,
2631
+ .verify = args.settings.verify,
2627
2632
  .timeout = protocol->timeout,
2628
2633
  .on_failed = iodine_io_raw_client_on_failed);
2629
2634
  }
@@ -2939,6 +2944,17 @@ static VALUE iodine_connection_peer_addr(VALUE self) {
2939
2944
  return rb_str_new(buf.buf, buf.len);
2940
2945
  }
2941
2946
 
2947
+ /** Returns the local (server side) network address or `nil`. */
2948
+ static VALUE iodine_connection_local_addr(VALUE self) {
2949
+ fio_buf_info_s buf = FIO_BUF_INFO0;
2950
+ iodine_connection_s *c = iodine_connection_ptr(self);
2951
+ if (c->io)
2952
+ buf = fio_sock_addr(fio_io_fd(c->io), false);
2953
+ if (!buf.len)
2954
+ return Qnil;
2955
+ return rb_str_new(buf.buf, buf.len);
2956
+ }
2957
+
2942
2958
  /** Returns the published peer's address. If unknown, returns {#peer_addr}. */
2943
2959
  static VALUE iodine_connection_from(VALUE self) {
2944
2960
  FIO_STR_INFO_TMP_VAR(addr, 128);
@@ -3535,7 +3551,7 @@ static void Init_Iodine_Connection(void) {
3535
3551
  val = STORE.frozen_str(FIO_STR_INFO1((char *)str)); \
3536
3552
  STORE.hold(val)
3537
3553
 
3538
- IODINE_RACK_CONTENT_LENGTH = STORE.header_name(FIO_STR_INFO1((char *)"CONTENT_LENGTH"));
3554
+ IODINE_RACK_CONTENT_LENGTH = STORE.header_name(FIO_STR_INFO1((char *)"content-length"));
3539
3555
  STORE.hold(IODINE_RACK_CONTENT_LENGTH);
3540
3556
 
3541
3557
  IODINE_CONST_RACK_STORE(IODINE_RACK_INPUT, "rack.input");
@@ -3593,7 +3609,7 @@ static void Init_Iodine_Connection(void) {
3593
3609
  rb_define_method(m, "gets", iodine_connection_body_gets, -1);
3594
3610
  rb_define_method(m, "read", iodine_connection_body_read, -1);
3595
3611
  rb_define_method(m, "seek", iodine_connection_body_seek, -1);
3596
- rb_define_method(m, "rewind", iodine_connection_body_seek, -1);
3612
+ rb_define_method(m, "rewind", iodine_connection_body_rewind, 0);
3597
3613
  rb_define_method(m, "close_body", iodine_connection_body_close, 0);
3598
3614
 
3599
3615
  rb_define_method(m, "open?", iodine_connection_is_open, 0);
@@ -3611,6 +3627,7 @@ static void Init_Iodine_Connection(void) {
3611
3627
  rb_define_method(m, "tls?", iodine_connection_is_tls, 0);
3612
3628
 
3613
3629
  rb_define_method(m, "peer_addr", iodine_connection_peer_addr, 0);
3630
+ rb_define_method(m, "local_addr", iodine_connection_local_addr, 0);
3614
3631
  rb_define_method(m, "from", iodine_connection_from, 0);
3615
3632
  rb_define_method(m, "certificate", iodine_connection_certificate, 0);
3616
3633
  rb_define_method(m,
@@ -11,6 +11,7 @@ Provides access to modern cryptographic primitives:
11
11
  - AES-128-GCM: AEAD symmetric encryption (16-byte key, 12-byte nonce)
12
12
  - AES-256-GCM: AEAD symmetric encryption (32-byte key, 12-byte nonce)
13
13
  - Ed25519: Digital signatures
14
+ - P256 / P384: ECDSA digital signatures (ES256 / ES384, DER signatures)
14
15
  - X25519: Key exchange and public-key encryption (ECIES with ChaCha20/AES)
15
16
  - HKDF: Key derivation (RFC 5869)
16
17
  ***************************************************************************** */
@@ -21,6 +22,8 @@ static VALUE iodine_rb_XCHACHA20POLY1305;
21
22
  static VALUE iodine_rb_AES128GCM;
22
23
  static VALUE iodine_rb_AES256GCM;
23
24
  static VALUE iodine_rb_ED25519;
25
+ static VALUE iodine_rb_P256;
26
+ static VALUE iodine_rb_P384;
24
27
  static VALUE iodine_rb_X25519;
25
28
  static VALUE iodine_rb_HKDF;
26
29
  static VALUE iodine_rb_X25519MLKEM768;
@@ -617,6 +620,105 @@ FIO_SFUNC VALUE iodine_crypto_ed25519_to_x25519_public(int argc,
617
620
  (void)self;
618
621
  }
619
622
 
623
+ /* *****************************************************************************
624
+ ECDSA P-256 / P-384 Digital Signatures (ES256 / ES384)
625
+ ***************************************************************************** */
626
+
627
+ /*
628
+ * Defines keypair / public_key / sign / verify for one NIST curve.
629
+ *
630
+ * Secret keys are big-endian scalars (`bytes_`), public keys are uncompressed
631
+ * SEC 1 points (0x04 || x || y; verification also accepts compressed points),
632
+ * signatures are DER encoded and messages are hashed with `hash_fn_`.
633
+ */
634
+ #define IODINE_CRYPTO_ECDSA_DEF(curve, bytes_, sig_max_, hash_fn_) \
635
+ /** Generates a new key pair: [secret_key, public_key]. */ \
636
+ FIO_SFUNC VALUE iodine_crypto_##curve##_keypair(VALUE self) { \
637
+ uint8_t sk[bytes_], pk[(bytes_ * 2) + 1]; \
638
+ if (fio_##curve##_keypair(sk, pk)) \
639
+ rb_raise(rb_eRuntimeError, "key generation failed"); \
640
+ VALUE secret = rb_str_new((const char *)sk, bytes_); \
641
+ VALUE public = rb_str_new((const char *)pk, (bytes_ * 2) + 1); \
642
+ fio_memset(sk, 0, bytes_); \
643
+ return rb_ary_new_from_args(2, secret, public); \
644
+ (void)self; \
645
+ } \
646
+ /** Derives the (uncompressed) public key from a secret key. */ \
647
+ FIO_SFUNC VALUE iodine_crypto_##curve##_public_key(int argc, \
648
+ VALUE *argv, \
649
+ VALUE self) { \
650
+ fio_buf_info_s sk = FIO_BUF_INFO0; \
651
+ iodine_rb2c_arg(argc, argv, IODINE_ARG_BUF(sk, 0, "secret_key", 1)); \
652
+ if (sk.len != bytes_) \
653
+ rb_raise(rb_eArgError, \
654
+ "secret_key must be " #bytes_ " bytes (got %zu)", \
655
+ sk.len); \
656
+ uint8_t pk[(bytes_ * 2) + 1]; \
657
+ if (fio_##curve##_public_key(pk, (const uint8_t *)sk.buf)) \
658
+ rb_raise(rb_eArgError, "secret_key is out of range"); \
659
+ return rb_str_new((const char *)pk, (bytes_ * 2) + 1); \
660
+ (void)self; \
661
+ } \
662
+ /** Signs `data` (hashed internally), returning a DER signature. */ \
663
+ FIO_SFUNC VALUE iodine_crypto_##curve##_sign(int argc, \
664
+ VALUE *argv, \
665
+ VALUE self) { \
666
+ fio_buf_info_s data = FIO_BUF_INFO0; \
667
+ fio_buf_info_s sk = FIO_BUF_INFO0; \
668
+ iodine_rb2c_arg(argc, \
669
+ argv, \
670
+ IODINE_ARG_BUF(data, 0, "data", 1), \
671
+ IODINE_ARG_BUF(sk, 0, "secret_key", 1)); \
672
+ if (sk.len != bytes_) \
673
+ rb_raise(rb_eArgError, \
674
+ "secret_key must be " #bytes_ " bytes (got %zu)", \
675
+ sk.len); \
676
+ fio_u512 h = hash_fn_(data.buf, data.len); \
677
+ uint8_t sig[sig_max_]; \
678
+ size_t sig_len = 0; \
679
+ if (fio_ecdsa_##curve##_sign(sig, \
680
+ &sig_len, \
681
+ sizeof(sig), \
682
+ h.u8, \
683
+ (const uint8_t *)sk.buf)) \
684
+ rb_raise(rb_eArgError, "signing failed (invalid secret_key?)"); \
685
+ return rb_str_new((const char *)sig, (long)sig_len); \
686
+ (void)self; \
687
+ } \
688
+ /** Verifies a DER signature over `data` (hashed internally). */ \
689
+ FIO_SFUNC VALUE iodine_crypto_##curve##_verify(int argc, \
690
+ VALUE *argv, \
691
+ VALUE self) { \
692
+ fio_buf_info_s data = FIO_BUF_INFO0; \
693
+ fio_buf_info_s sig = FIO_BUF_INFO0; \
694
+ fio_buf_info_s pk = FIO_BUF_INFO0; \
695
+ iodine_rb2c_arg(argc, \
696
+ argv, \
697
+ IODINE_ARG_BUF(data, 0, "data", 1), \
698
+ IODINE_ARG_BUF(sig, 0, "signature", 1), \
699
+ IODINE_ARG_BUF(pk, 0, "public_key", 1)); \
700
+ fio_u512 h = hash_fn_(data.buf, data.len); \
701
+ return fio_ecdsa_##curve##_verify((const uint8_t *)sig.buf, \
702
+ sig.len, \
703
+ h.u8, \
704
+ (const uint8_t *)pk.buf, \
705
+ pk.len) \
706
+ ? Qfalse \
707
+ : Qtrue; \
708
+ (void)self; \
709
+ }
710
+
711
+ /* SHA-256 widened to the shared fio_u512 return type. */
712
+ FIO_IFUNC fio_u512 iodine___crypto_sha256_512(const void *data, uint64_t len) {
713
+ fio_u512 r = {0};
714
+ r.u256[0] = fio_sha256(data, len);
715
+ return r;
716
+ }
717
+
718
+ IODINE_CRYPTO_ECDSA_DEF(p256, 32, 72, iodine___crypto_sha256_512)
719
+ IODINE_CRYPTO_ECDSA_DEF(p384, 48, 104, fio_sha384)
720
+ #undef IODINE_CRYPTO_ECDSA_DEF
721
+
620
722
  /* *****************************************************************************
621
723
  X25519 Key Exchange
622
724
  ***************************************************************************** */
@@ -1361,6 +1463,21 @@ static void Init_Iodine_Crypto(void) {
1361
1463
  iodine_crypto_ed25519_to_x25519_public,
1362
1464
  -1);
1363
1465
 
1466
+ /* Iodine::Base::Crypto::P256 / P384 (ECDSA) */
1467
+ #define IODINE_CRYPTO_ECDSA_REG(var, name, curve) \
1468
+ var = rb_define_module_under(iodine_rb_CRYPTO, name); \
1469
+ STORE.hold(var); \
1470
+ rb_define_module_function(var, "keypair", iodine_crypto_##curve##_keypair, 0); \
1471
+ rb_define_module_function(var, \
1472
+ "public_key", \
1473
+ iodine_crypto_##curve##_public_key, \
1474
+ -1); \
1475
+ rb_define_module_function(var, "sign", iodine_crypto_##curve##_sign, -1); \
1476
+ rb_define_module_function(var, "verify", iodine_crypto_##curve##_verify, -1);
1477
+ IODINE_CRYPTO_ECDSA_REG(iodine_rb_P256, "P256", p256)
1478
+ IODINE_CRYPTO_ECDSA_REG(iodine_rb_P384, "P384", p384)
1479
+ #undef IODINE_CRYPTO_ECDSA_REG
1480
+
1364
1481
  /* Iodine::Base::Crypto::X25519 */
1365
1482
  iodine_rb_X25519 = rb_define_module_under(iodine_rb_CRYPTO, "X25519");
1366
1483
  STORE.hold(iodine_rb_X25519);
@@ -213,7 +213,8 @@ static VALUE iodine_listener_map(int argc, VALUE *argv, VALUE o) {
213
213
  settings.public_folder = FIO_STR_INFO0;
214
214
  // TODO: test for a handler's public folder property?
215
215
  fio_http_route FIO_NOOP((fio_http_listener_s *)l->listener,
216
- RSTRING_PTR(url),
216
+ IODINE_STORE_IS_SKIP(url) ? "/"
217
+ : RSTRING_PTR(url),
217
218
  settings);
218
219
  }
219
220
  } else { /* not HTTP, URLs are invalid. */
@@ -44,8 +44,8 @@ static uint64_t iodinde_hmap_hash(VALUE k) {
44
44
  fio_buf_info_s buf = (fio_buf_info_s)IODINE_RSTR_INFO(k);
45
45
  return fio_risky_hash(buf.buf, buf.len, (uint64_t)&iodinde_hmap_hash);
46
46
  }
47
- if (RB_SYMBOL_P(k) || RB_FIXNUM_P(k) || k == RUBY_T_TRUE ||
48
- k == RUBY_T_FALSE || k == RUBY_T_NIL)
47
+ if (RB_SYMBOL_P(k) || RB_FIXNUM_P(k) || k == Qtrue || k == Qfalse ||
48
+ k == Qnil)
49
49
  return fio_risky_num((uint64_t)k, (uint64_t)&iodinde_hmap_hash);
50
50
  rb_raise(rb_eTypeError, "key MUST be either a String or a Symbol.");
51
51
  return 0;
@@ -76,8 +76,8 @@ static _Bool iodinde_hmap_object_cmp(VALUE a, VALUE b) {
76
76
  * used during key duplication to prevent GC issues.
77
77
  */
78
78
  typedef struct {
79
- iodine_hmap_s map; /**< The underlying facil.io hash map */
80
- VALUE tmp; /**< Temporary holder during key operations */
79
+ iodine_hmap_s map; /**< The underlying facil.io hash map */
80
+ VALUE tmp; /**< Temporary holder during key operations */
81
81
  } iodine_minimap_s;
82
82
 
83
83
  static int iodine_minimap_gc_mark_task(iodine_hmap_each_s *e) {
@@ -428,11 +428,11 @@ static VALUE iodine_minimap_benchmark_c(VALUE klass, VALUE object_count) {
428
428
  insert = fio_time_micro() - start; \
429
429
  if (klass##_count(&m) != OBJECTS) \
430
430
  FIO_LOG_ERROR("map counter error (%zu != %zu)!", \
431
- klass##_count(&m), \
431
+ (size_t)klass##_count(&m), \
432
432
  OBJECTS); \
433
433
  if (klass##_capa(&m) < OBJECTS) \
434
434
  FIO_LOG_ERROR("map capacity error (%zu < %zu)!", \
435
- klass##_capa(&m), \
435
+ (size_t)klass##_capa(&m), \
436
436
  OBJECTS); \
437
437
  start = fio_time_micro(); \
438
438
  for (size_t j = 0; j < CYCLES; ++j) { \
@@ -442,11 +442,11 @@ static VALUE iodine_minimap_benchmark_c(VALUE klass, VALUE object_count) {
442
442
  overwrite = fio_time_micro() - start; \
443
443
  if (klass##_count(&m) != OBJECTS) \
444
444
  FIO_LOG_ERROR("map counter error (%zu != %zu)!", \
445
- klass##_count(&m), \
445
+ (size_t)klass##_count(&m), \
446
446
  OBJECTS); \
447
447
  if (klass##_capa(&m) < OBJECTS) \
448
448
  FIO_LOG_ERROR("map capacity error (%zu < %zu)!", \
449
- klass##_capa(&m), \
449
+ (size_t)klass##_capa(&m), \
450
450
  OBJECTS); \
451
451
  start = fio_time_micro(); \
452
452
  for (size_t sc = 0; sc < FIND_CYCLES; ++sc) \
@@ -454,8 +454,8 @@ static VALUE iodine_minimap_benchmark_c(VALUE klass, VALUE object_count) {
454
454
  if (klass##_get(&m, numbers[i]) != numbers[i]) \
455
455
  FIO_LOG_ERROR(FIO_MACRO2STR(klass) "_get error ([%zu] %zu != %zu)!", \
456
456
  i, \
457
- numbers[i], \
458
- klass##_get(&m, i)); \
457
+ (size_t)numbers[i], \
458
+ (size_t)klass##_get(&m, i)); \
459
459
  } \
460
460
  find = fio_time_micro() - start; \
461
461
  start = fio_time_micro(); \
@@ -466,8 +466,8 @@ static VALUE iodine_minimap_benchmark_c(VALUE klass, VALUE object_count) {
466
466
  FIO_MACRO2STR(klass) "_get error" \
467
467
  "([%zu] %zu shouldn't exist but == %zu)!", \
468
468
  i, \
469
- numbers[i], \
470
- klass##_get(&m, numbers[i])); \
469
+ (size_t)numbers[i], \
470
+ (size_t)klass##_get(&m, numbers[i])); \
471
471
  } \
472
472
  find_missing = fio_time_micro() - start; \
473
473
  start = fio_time_micro(); \
@@ -484,11 +484,11 @@ static VALUE iodine_minimap_benchmark_c(VALUE klass, VALUE object_count) {
484
484
  FIO_MACRO2STR(klass), \
485
485
  (size_t)klass##_count(&m), \
486
486
  (size_t)klass##_capa(&m), \
487
- insert, \
488
- overwrite, \
489
- find, \
490
- find_missing, \
491
- loop); \
487
+ (size_t)insert, \
488
+ (size_t)overwrite, \
489
+ (size_t)find, \
490
+ (size_t)find_missing, \
491
+ (size_t)loop); \
492
492
  klass##_destroy(&m); \
493
493
  } while (0);
494
494
  for (size_t tst = 0; tst < TESTS; ++tst) {
@@ -218,13 +218,27 @@ Wrapper API
218
218
  Assigns the TLS context a public certificate, allowing remote parties to
219
219
  validate the connection's identity.
220
220
 
221
- A self signed certificate is automatically created if the `name` argument
222
- is specified and either (or both) of the `cert` (public certificate) or `key`
223
- (private key) arguments are missing.
224
-
225
- Some implementations allow servers to have more than a single certificate, which
226
- will be selected using the SNI extension. I believe the existing OpenSSL
227
- implementation supports this option (untested).
221
+ On servers, a self signed certificate (ECDSA P-256 on the `:iodine` backend)
222
+ is created for `name` if either (or both) of the `cert` (public certificate) or
223
+ `key` (private key) arguments are missing. This is fine for development, but
224
+ browsers will warn - use a CA issued certificate in production.
225
+
226
+ Servers may hold several certificates; both TLS backends select one per
227
+ handshake using the client's SNI host name:
228
+
229
+ 1. a case-insensitive exact match of `name`;
230
+ 2. else a single-label wildcard (`*.example.com` matches `www.example.com`,
231
+ but neither `example.com` nor `a.b.example.com`);
232
+ 3. else the default certificate: the one added with an empty `name`, else the
233
+ first one added.
234
+
235
+ Re-adding a `name` replaces its certificate. An omitted `name` defaults to
236
+ `"localhost"`.
237
+
238
+ On clients, the (default) certificate is presented when the server requests
239
+ one (mutual TLS / client certificate authentication), with its whole chain.
240
+ Use client certificates with PEM files: a name-only client entry presents no
241
+ certificate on the `:iodine` backend.
228
242
 
229
243
  Iodine::TLS#add_cert(name = nil,
230
244
  cert = nil,