iodine 0.8.3 → 0.8.5

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.
@@ -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)"),
@@ -1760,20 +1760,8 @@ static void iodine_io_raw_on_attach(fio_io_s *io) {
1760
1760
 
1761
1761
  /** Tests whether a nonblocking client socket completed its connection. */
1762
1762
  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
1763
+ /* pending SO_ERROR, or -1 if the query failed */
1764
+ return fio_sock_error(fio_io_fd(io)) != 0;
1777
1765
  }
1778
1766
 
1779
1767
  typedef struct {
@@ -2255,6 +2243,7 @@ FIO_IFUNC iodine_connection_args_s iodine_connection_parse_args(int argc,
2255
2243
  .max_header_size = (uint32_t)fio_cli_get_i("-maxhd"),
2256
2244
  .max_line_len = (uint32_t)fio_cli_get_i("-maxln"),
2257
2245
  .max_body_size = (size_t)fio_cli_get_i("-maxbd"),
2246
+ .max_params = (size_t)fio_cli_get_i("-maxpr"),
2258
2247
  .ws_max_msg_size = (size_t)fio_cli_get_i("-maxms"),
2259
2248
  .timeout = iodine_connection___cli_timeout("-k"),
2260
2249
  .ws_timeout = iodine_connection___cli_timeout("-ping"),
@@ -2266,6 +2255,7 @@ FIO_IFUNC iodine_connection_args_s iodine_connection_parse_args(int argc,
2266
2255
  },
2267
2256
  };
2268
2257
  VALUE proc = Qnil, handler_tmp = Qnil, tls_io_rb = Qnil, timeout = Qnil;
2258
+ VALUE verify_rb = Qnil;
2269
2259
  iodine_rb2c_arg(
2270
2260
  argc,
2271
2261
  argv,
@@ -2283,6 +2273,7 @@ FIO_IFUNC iodine_connection_args_s iodine_connection_parse_args(int argc,
2283
2273
  IODINE_ARG_U8(r.settings.timeout, 0, "timeout", 0),
2284
2274
  IODINE_ARG_U8(r.settings.ws_timeout, 0, "ping", 0),
2285
2275
  IODINE_ARG_BOOL(r.settings.log, 0, "log", 0),
2276
+ IODINE_ARG_RB(verify_rb, 0, "verify", 0),
2286
2277
  IODINE_ARG_BOOL(r.settings.compress_static, 0, "dynamic_deflate", 0),
2287
2278
  IODINE_ARG_BOOL(r.settings.compress_ws, 0, "websocket_deflate", 0),
2288
2279
  IODINE_ARG_BUF(r.method, 0, "method", 0),
@@ -2291,6 +2282,8 @@ FIO_IFUNC iodine_connection_args_s iodine_connection_parse_args(int argc,
2291
2282
  IODINE_ARG_RB(r.cookies, 0, "cookies", 0),
2292
2283
  IODINE_ARG_PROC(proc, 0, "block", 0));
2293
2284
  r.settings.compress_dynamic = r.settings.compress_static;
2285
+ /* client mode only: TLS peer verification (system trust if none set) */
2286
+ r.settings.verify = (verify_rb == Qtrue);
2294
2287
  r.settings.udata = (void *)handler_tmp;
2295
2288
  /* test for errors before allocating or protecting data */
2296
2289
 
@@ -2607,6 +2600,8 @@ static VALUE iodine_connection_initialize(int argc, VALUE *argv, VALUE self) {
2607
2600
  (VALUE)h);
2608
2601
  // args.settings.on_finish =
2609
2602
  c->io = fio_http_connect FIO_NOOP(args.url.buf, h, args.settings);
2603
+ /* fio_http_connect duplicates its TLS reference; release our own. */
2604
+ fio_io_tls_free(args.settings.tls);
2610
2605
  c->http = h;
2611
2606
  fio_http_udata2_set(h, (void *)self);
2612
2607
 
@@ -2622,6 +2617,7 @@ static VALUE iodine_connection_initialize(int argc, VALUE *argv, VALUE self) {
2622
2617
  .protocol = protocol,
2623
2618
  .udata = (void *)self,
2624
2619
  .tls = args.settings.tls,
2620
+ .verify = args.settings.verify,
2625
2621
  .timeout = protocol->timeout,
2626
2622
  .on_failed = iodine_io_raw_client_on_failed);
2627
2623
  }
@@ -2931,7 +2927,18 @@ static VALUE iodine_connection_peer_addr(VALUE self) {
2931
2927
  fio_buf_info_s buf = FIO_BUF_INFO0;
2932
2928
  iodine_connection_s *c = iodine_connection_ptr(self);
2933
2929
  if (c->io)
2934
- buf = fio_sock_peer_addr(fio_io_fd(c->io));
2930
+ buf = fio_sock_peer_addr(fio_io_fd(c->io), false);
2931
+ if (!buf.len)
2932
+ return Qnil;
2933
+ return rb_str_new(buf.buf, buf.len);
2934
+ }
2935
+
2936
+ /** Returns the local (server side) network address or `nil`. */
2937
+ static VALUE iodine_connection_local_addr(VALUE self) {
2938
+ fio_buf_info_s buf = FIO_BUF_INFO0;
2939
+ iodine_connection_s *c = iodine_connection_ptr(self);
2940
+ if (c->io)
2941
+ buf = fio_sock_addr(fio_io_fd(c->io), false);
2935
2942
  if (!buf.len)
2936
2943
  return Qnil;
2937
2944
  return rb_str_new(buf.buf, buf.len);
@@ -2946,7 +2953,7 @@ static VALUE iodine_connection_from(VALUE self) {
2946
2953
  fio_http_from(&addr, c->http);
2947
2954
  buf = FIO_STR2BUF_INFO(addr);
2948
2955
  } else if (c->io)
2949
- buf = fio_sock_peer_addr(fio_io_fd(c->io));
2956
+ buf = fio_sock_peer_addr(fio_io_fd(c->io), false);
2950
2957
  if (!buf.len)
2951
2958
  return Qnil;
2952
2959
  return rb_str_new(buf.buf, buf.len);
@@ -3311,7 +3318,7 @@ static void *iodine_tcp_listen(iodine_connection_args_s args) {
3311
3318
  return fio_io_listen(.url = args.url.buf,
3312
3319
  .protocol = protocol,
3313
3320
  .udata = args.settings.udata,
3314
- .tls = args.settings.tls,
3321
+ .tls = fio_io_tls_dup(args.settings.tls),
3315
3322
  .on_stop = iodine_tcp_on_stop,
3316
3323
  .queue_for_accept = 0);
3317
3324
  }
@@ -3609,6 +3616,7 @@ static void Init_Iodine_Connection(void) {
3609
3616
  rb_define_method(m, "tls?", iodine_connection_is_tls, 0);
3610
3617
 
3611
3618
  rb_define_method(m, "peer_addr", iodine_connection_peer_addr, 0);
3619
+ rb_define_method(m, "local_addr", iodine_connection_local_addr, 0);
3612
3620
  rb_define_method(m, "from", iodine_connection_from, 0);
3613
3621
  rb_define_method(m, "certificate", iodine_connection_certificate, 0);
3614
3622
  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;
@@ -64,13 +67,14 @@ FIO_SFUNC VALUE iodine_crypto_chacha_encrypt(int argc,
64
67
  VALUE ciphertext = rb_str_new(data.buf, data.len);
65
68
  uint8_t mac[16];
66
69
 
67
- fio_chacha20_poly1305_enc(mac,
68
- RSTRING_PTR(ciphertext),
69
- data.len,
70
- ad.buf,
71
- ad.len,
72
- (void *)key.buf,
73
- (void *)nonce.buf);
70
+ if (fio_chacha20_poly1305_enc(mac,
71
+ RSTRING_PTR(ciphertext),
72
+ data.len,
73
+ ad.buf,
74
+ ad.len,
75
+ (void *)key.buf,
76
+ (void *)nonce.buf))
77
+ rb_raise(rb_eRuntimeError, "Encryption failed");
74
78
 
75
79
  VALUE mac_str = rb_str_new((const char *)mac, 16);
76
80
  return rb_ary_new_from_args(2, ciphertext, mac_str);
@@ -172,13 +176,14 @@ FIO_SFUNC VALUE iodine_crypto_xchacha_encrypt(int argc,
172
176
  VALUE ciphertext = rb_str_new(data.buf, data.len);
173
177
  uint8_t mac[16];
174
178
 
175
- fio_xchacha20_poly1305_enc(mac,
176
- RSTRING_PTR(ciphertext),
177
- data.len,
178
- ad.buf,
179
- ad.len,
180
- (void *)key.buf,
181
- (void *)nonce.buf);
179
+ if (fio_xchacha20_poly1305_enc(mac,
180
+ RSTRING_PTR(ciphertext),
181
+ data.len,
182
+ ad.buf,
183
+ ad.len,
184
+ (void *)key.buf,
185
+ (void *)nonce.buf))
186
+ rb_raise(rb_eRuntimeError, "Encryption failed");
182
187
 
183
188
  VALUE mac_str = rb_str_new((const char *)mac, 16);
184
189
  return rb_ary_new_from_args(2, ciphertext, mac_str);
@@ -277,13 +282,14 @@ FIO_SFUNC VALUE iodine_crypto_aes128gcm_encrypt(int argc,
277
282
  VALUE ciphertext = rb_str_new(data.buf, data.len);
278
283
  uint8_t mac[16];
279
284
 
280
- fio_aes128_gcm_enc(mac,
281
- RSTRING_PTR(ciphertext),
282
- data.len,
283
- ad.buf,
284
- ad.len,
285
- (void *)key.buf,
286
- (void *)nonce.buf);
285
+ if (fio_aes128_gcm_enc(mac,
286
+ RSTRING_PTR(ciphertext),
287
+ data.len,
288
+ ad.buf,
289
+ ad.len,
290
+ (void *)key.buf,
291
+ (void *)nonce.buf))
292
+ rb_raise(rb_eRuntimeError, "Encryption failed");
287
293
 
288
294
  VALUE mac_str = rb_str_new((const char *)mac, 16);
289
295
  return rb_ary_new_from_args(2, ciphertext, mac_str);
@@ -382,13 +388,14 @@ FIO_SFUNC VALUE iodine_crypto_aes256gcm_encrypt(int argc,
382
388
  VALUE ciphertext = rb_str_new(data.buf, data.len);
383
389
  uint8_t mac[16];
384
390
 
385
- fio_aes256_gcm_enc(mac,
386
- RSTRING_PTR(ciphertext),
387
- data.len,
388
- ad.buf,
389
- ad.len,
390
- (void *)key.buf,
391
- (void *)nonce.buf);
391
+ if (fio_aes256_gcm_enc(mac,
392
+ RSTRING_PTR(ciphertext),
393
+ data.len,
394
+ ad.buf,
395
+ ad.len,
396
+ (void *)key.buf,
397
+ (void *)nonce.buf))
398
+ rb_raise(rb_eRuntimeError, "Encryption failed");
392
399
 
393
400
  VALUE mac_str = rb_str_new((const char *)mac, 16);
394
401
  return rb_ary_new_from_args(2, ciphertext, mac_str);
@@ -613,6 +620,105 @@ FIO_SFUNC VALUE iodine_crypto_ed25519_to_x25519_public(int argc,
613
620
  (void)self;
614
621
  }
615
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
+
616
722
  /* *****************************************************************************
617
723
  X25519 Key Exchange
618
724
  ***************************************************************************** */
@@ -1004,15 +1110,16 @@ FIO_SFUNC VALUE iodine_crypto_hkdf_derive(int argc, VALUE *argv, VALUE self) {
1004
1110
  VALUE okm = rb_str_buf_new(length);
1005
1111
  rb_str_set_len(okm, length);
1006
1112
 
1007
- fio_hkdf(RSTRING_PTR(okm),
1008
- (size_t)length,
1009
- salt.buf,
1010
- salt.len,
1011
- ikm.buf,
1012
- ikm.len,
1013
- info.buf,
1014
- info.len,
1015
- (int)sha384);
1113
+ if (fio_hkdf(RSTRING_PTR(okm),
1114
+ (size_t)length,
1115
+ salt.buf,
1116
+ salt.len,
1117
+ ikm.buf,
1118
+ ikm.len,
1119
+ info.buf,
1120
+ info.len,
1121
+ (int)sha384))
1122
+ rb_raise(rb_eRuntimeError, "HKDF derivation failed");
1016
1123
 
1017
1124
  return okm;
1018
1125
  (void)self;
@@ -1356,6 +1463,21 @@ static void Init_Iodine_Crypto(void) {
1356
1463
  iodine_crypto_ed25519_to_x25519_public,
1357
1464
  -1);
1358
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
+
1359
1481
  /* Iodine::Base::Crypto::X25519 */
1360
1482
  iodine_rb_X25519 = rb_define_module_under(iodine_rb_CRYPTO, "X25519");
1361
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;
@@ -795,6 +795,7 @@ typedef struct {
795
795
  fio_str_info_s filename;
796
796
  fio_str_info_s content_type;
797
797
  fio_str_info_s headers;
798
+ fio_buf_info_s data;
798
799
  } iodine___params_callback_args_s;
799
800
 
800
801
  FIO_SFUNC VALUE iodine___params_callback_dispatch(VALUE args_) {
@@ -848,6 +849,10 @@ FIO_SFUNC VALUE iodine___params_callback_dispatch(VALUE args_) {
848
849
  a->filename,
849
850
  a->content_type,
850
851
  a->headers);
852
+ if (iodine___params_on_file_data(a->u, a->result, a->data)) {
853
+ a->result = NULL;
854
+ a->status = -1;
855
+ }
851
856
  break;
852
857
  }
853
858
  return Qnil;
@@ -963,11 +968,13 @@ FIO_SFUNC int iodine___params_cb_array_push(void *udata_,
963
968
  return a.status;
964
969
  }
965
970
 
966
- FIO_SFUNC void *iodine___params_cb_on_file(void *udata_,
967
- fio_str_info_s name,
968
- fio_str_info_s filename,
969
- fio_str_info_s content_type,
970
- fio_str_info_s headers) {
971
+ FIO_SFUNC fio_json_obj_result_s iodine___params_cb_on_file(
972
+ void *udata_,
973
+ fio_str_info_s name,
974
+ fio_str_info_s filename,
975
+ fio_str_info_s content_type,
976
+ fio_str_info_s headers,
977
+ fio_buf_info_s data) {
971
978
  iodine___params_callback_args_s a = {
972
979
  .u = (iodine_params_udata_s *)udata_,
973
980
  .op = IODINE_PARAMS_CB_FILE,
@@ -975,9 +982,10 @@ FIO_SFUNC void *iodine___params_cb_on_file(void *udata_,
975
982
  .filename = filename,
976
983
  .content_type = content_type,
977
984
  .headers = headers,
985
+ .data = data,
978
986
  };
979
987
  iodine___params_callback_protect(&a);
980
- return a.result;
988
+ return (fio_json_obj_result_s){.obj = a.result, .err = a.status};
981
989
  }
982
990
 
983
991
  /* The STL treats this callback table as read-only. */
@@ -997,7 +1005,6 @@ static fio_http_params_parse_callbacks_s IODINE_PARAMS_CALLBACKS = {
997
1005
  .is_map = iodine___params_is_map,
998
1006
  .free_unused_object = iodine___params_free_unused_object,
999
1007
  .on_file = iodine___params_cb_on_file,
1000
- .on_file_data = iodine___params_on_file_data,
1001
1008
  };
1002
1009
 
1003
1010
  /* *****************************************************************************
@@ -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,
@@ -247,8 +247,6 @@ FIO_SFUNC VALUE iodine_utils_is_eq(VALUE mod, VALUE a, VALUE b) { // clang-forma
247
247
  Randomness and Friends
248
248
  ***************************************************************************** */
249
249
 
250
- FIO_DEFINE_RANDOM128_FN(static, iodine_random, 11, 0)
251
-
252
250
  FIO_SFUNC VALUE iodine_utils_hmac512(VALUE self, VALUE secret, VALUE massage) {
253
251
  rb_check_type(secret, RUBY_T_STRING);
254
252
  rb_check_type(massage, RUBY_T_STRING);
@@ -420,12 +418,20 @@ FIO_SFUNC VALUE iodine_utils_risky_hash(int argc, VALUE *argv, VALUE self) {
420
418
  /**
421
419
  * Computes facil.io Risky256 Hash (non-cryptographic, 256-bit).
422
420
  *
421
+ * When `key:` is given, computes the keyed (single pass) variant instead.
422
+ *
423
423
  * @param data [String] Input data to hash
424
+ * @param key: [String] Optional secret key (keyed hashing)
424
425
  * @return [String] 32-byte binary hash
425
426
  */
426
- FIO_SFUNC VALUE iodine_utils_risky256(VALUE self, VALUE data) {
427
- rb_check_type(data, RUBY_T_STRING);
428
- fio_u256 h = fio_risky256(RSTRING_PTR(data), (uint64_t)RSTRING_LEN(data));
427
+ FIO_SFUNC VALUE iodine_utils_risky256(int argc, VALUE *argv, VALUE self) {
428
+ fio_buf_info_s data = FIO_BUF_INFO0, key = FIO_BUF_INFO0;
429
+ iodine_rb2c_arg(argc,
430
+ argv,
431
+ IODINE_ARG_BUF(data, 0, "data", 1),
432
+ IODINE_ARG_BUF(key, 0, "key", 0));
433
+ fio_u256 h = key.buf ? fio_risky256_keyed(key.buf, key.len, data.buf, data.len)
434
+ : fio_risky256(data.buf, data.len);
429
435
  return rb_str_new((const char *)h.u8, 32);
430
436
  (void)self;
431
437
  }
@@ -433,14 +439,21 @@ FIO_SFUNC VALUE iodine_utils_risky256(VALUE self, VALUE data) {
433
439
  /**
434
440
  * Computes facil.io Risky512 Hash (non-cryptographic, 512-bit).
435
441
  *
436
- * First 256 bits are identical to risky256 (SHAKE-style extension).
442
+ * First 256 bits are identical to risky256 (SHAKE-style extension), also when
443
+ * the same `key:` is given to both.
437
444
  *
438
445
  * @param data [String] Input data to hash
446
+ * @param key: [String] Optional secret key (keyed hashing)
439
447
  * @return [String] 64-byte binary hash
440
448
  */
441
- FIO_SFUNC VALUE iodine_utils_risky512(VALUE self, VALUE data) {
442
- rb_check_type(data, RUBY_T_STRING);
443
- fio_u512 h = fio_risky512(RSTRING_PTR(data), (uint64_t)RSTRING_LEN(data));
449
+ FIO_SFUNC VALUE iodine_utils_risky512(int argc, VALUE *argv, VALUE self) {
450
+ fio_buf_info_s data = FIO_BUF_INFO0, key = FIO_BUF_INFO0;
451
+ iodine_rb2c_arg(argc,
452
+ argv,
453
+ IODINE_ARG_BUF(data, 0, "data", 1),
454
+ IODINE_ARG_BUF(key, 0, "key", 0));
455
+ fio_u512 h = key.buf ? fio_risky512_keyed(key.buf, key.len, data.buf, data.len)
456
+ : fio_risky512(data.buf, data.len);
444
457
  return rb_str_new((const char *)h.u8, 64);
445
458
  (void)self;
446
459
  }
@@ -573,7 +586,7 @@ FIO_SFUNC VALUE iodine_utils_hmac_poly(VALUE self,
573
586
 
574
587
  FIO_SFUNC VALUE iodine_utils_uuid(int argc, VALUE *argv, VALUE self) {
575
588
  VALUE r = Qnil;
576
- fio_u128 rand = iodine_random128();
589
+ fio_u128 rand = fio_rand128();
577
590
  fio_buf_info_s secret = {0};
578
591
  fio_buf_info_s info = {0};
579
592
  FIO_STR_INFO_TMP_VAR(str, 128);
@@ -641,7 +654,7 @@ FIO_SFUNC VALUE iodine_utils_random(int argc, VALUE *argv, VALUE self) {
641
654
  if ((size - 1) > 0x0FFFFFFF)
642
655
  rb_raise(rb_eRangeError, "`bytes` count is out of range.");
643
656
  r = rb_str_buf_new(size);
644
- iodine_random_bytes(RSTRING_PTR(r), size);
657
+ fio_rand_bytes(RSTRING_PTR(r), size);
645
658
  rb_str_set_len(r, size);
646
659
  return r;
647
660
  (void)self;
@@ -684,6 +697,8 @@ FIO_SFUNC VALUE iodine_utils_totp(int argc, VALUE *argv, VALUE self) {
684
697
  interval = 30;
685
698
 
686
699
  uint32_t otp = fio_otp(secret, .offset = offset, .interval = interval);
700
+ if (otp == FIO_OTP_ERROR)
701
+ otp = fio_otp(secret, .offset = offset, .interval = interval, .is_raw = 1);
687
702
  return UINT2NUM(otp);
688
703
  (void)self;
689
704
  }
@@ -772,10 +787,15 @@ FIO_SFUNC VALUE iodine_utils_totp_verify(int argc, VALUE *argv, VALUE self) {
772
787
  if (window < 0 || window > 10)
773
788
  rb_raise(rb_eArgError, "window must be between 0 and 10");
774
789
 
775
- /* Check code against each offset in the window */
790
+ /* FIO_OTP_ERROR is not a valid code (including a caller's -1). */
791
+ if (code < 0 || code > 999999)
792
+ return Qfalse;
776
793
  for (int64_t offset = -window; offset <= window; ++offset) {
777
794
  uint32_t expected = fio_otp(secret, .offset = offset, .interval = interval);
778
- if (expected == (uint32_t)code)
795
+ if (expected == FIO_OTP_ERROR)
796
+ expected = fio_otp(secret, .offset = offset, .interval = interval,
797
+ .is_raw = 1);
798
+ if (expected != FIO_OTP_ERROR && expected == (uint32_t)code)
779
799
  return Qtrue;
780
800
  }
781
801
 
@@ -908,16 +928,12 @@ static void Init_Iodine_Utils(void) {
908
928
  rb_define_singleton_method(m, "sha1", iodine_utils_sha1, 1);
909
929
  rb_define_singleton_method(m, "crc32", iodine_utils_crc32, -1);
910
930
  rb_define_singleton_method(m, "risky_hash", iodine_utils_risky_hash, -1);
911
- rb_define_singleton_method(m, "risky256", iodine_utils_risky256, 1);
912
- rb_define_singleton_method(m, "risky512", iodine_utils_risky512, 1);
931
+ rb_define_singleton_method(m, "risky256", iodine_utils_risky256, -1);
932
+ rb_define_singleton_method(m, "risky512", iodine_utils_risky512, -1);
913
933
  rb_define_singleton_method(m, "risky256_hmac", iodine_utils_risky256_hmac, 2);
914
934
  rb_define_singleton_method(m, "risky512_hmac", iodine_utils_risky512_hmac, 2);
915
935
  rb_define_singleton_method(m, "secure_random", iodine_utils_secure_random, -1);
916
936
  rb_define_singleton_method(m, "blake2b", iodine_utils_blake2b, -1);
917
937
  rb_define_singleton_method(m, "blake2s", iodine_utils_blake2s, -1);
918
-
919
-
920
-
921
- fio_state_callback_add(FIO_CALL_IN_CHILD, iodine_random_on_fork, NULL);
922
938
  } // clang-format on
923
939
  #endif /* H___IODINE_UTILS___H */