summaryrefslogtreecommitdiffstats
path: root/src/shrpx_mruby_module_env.cc
blob: 9b63c5189bc306216c522b33a034412cfd86fc92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
 * nghttp2 - HTTP/2 C Library
 *
 * Copyright (c) 2015 Tatsuhiro Tsujikawa
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "shrpx_mruby_module_env.h"

#include <mruby/variable.h>
#include <mruby/string.h>
#include <mruby/hash.h>

#include "shrpx_downstream.h"
#include "shrpx_upstream.h"
#include "shrpx_client_handler.h"
#include "shrpx_mruby.h"
#include "shrpx_mruby_module.h"
#include "shrpx_log.h"
#include "shrpx_tls.h"

namespace shrpx {

namespace mruby {

namespace {
mrb_value env_init(mrb_state *mrb, mrb_value self) { return self; }
} // namespace

namespace {
mrb_value env_get_req(mrb_state *mrb, mrb_value self) {
  return mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "req"));
}
} // namespace

namespace {
mrb_value env_get_resp(mrb_state *mrb, mrb_value self) {
  return mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "resp"));
}
} // namespace

namespace {
mrb_value env_get_ctx(mrb_state *mrb, mrb_value self) {
  auto data = reinterpret_cast<MRubyAssocData *>(mrb->ud);
  auto downstream = data->downstream;

  auto dsym = intern_ptr(mrb, downstream);

  auto ctx = mrb_iv_get(mrb, self, dsym);
  if (mrb_nil_p(ctx)) {
    ctx = mrb_hash_new(mrb);
    mrb_iv_set(mrb, self, dsym, ctx);
  }

  return ctx;
}
} // namespace

namespace {
mrb_value env_get_phase(mrb_state *mrb, mrb_value self) {
  auto data = static_cast<MRubyAssocData *>(mrb->ud);

  return mrb_fixnum_value(data->phase);
}
} // namespace

namespace {
mrb_value env_get_remote_addr(mrb_state *mrb, mrb_value self) {
  auto data = static_cast<MRubyAssocData *>(mrb->ud);
  auto downstream = data->downstream;
  auto upstream = downstream->get_upstream();
  auto handler = upstream->get_client_handler();

  auto &ipaddr = handler->get_ipaddr();

  return mrb_str_new(mrb, ipaddr.data(), ipaddr.size());
}
} // namespace

namespace {
mrb_value env_get_server_port(mrb_state *mrb, mrb_value self) {
  auto data = static_cast<MRubyAssocData *>(mrb->ud);
  auto downstream = data->downstream;
  auto upstream = downstream->get_upstream();
  auto handler = upstream->get_client_handler();
  auto faddr = handler->get_upstream_addr();

  return mrb_fixnum_value(faddr->port);
}
} // namespace

namespace {
mrb_value env_get_server_addr(mrb_state *mrb, mrb_value self) {
  auto data = static_cast<MRubyAssocData *>(mrb->ud);
  auto downstream = data->downstream;
  auto upstream = downstream->get_upstream();
  auto handler = upstream->get_client_handler();
  auto faddr = handler->get_upstream_addr();

  return mrb_str_new(mrb, faddr->host.data(), faddr->host.size());
}
} // namespace

namespace {
mrb_value env_get_tls_used(mrb_state *mrb, mrb_value self) {
  auto data = static_cast<MRubyAssocData *>(mrb->ud);
  auto downstream = data->downstream;
  auto upstream = downstream->get_upstream();
  auto handler = upstream->get_client_handler();

  return handler->get_ssl() ? mrb_true_value() : mrb_false_value();
}
} // namespace

namespace {
mrb_value env_get_tls_sni(mrb_state *mrb, mrb_value self) {
  auto data = static_cast<MRubyAssocData *>(mrb->ud);
  auto downstream = data->downstream;
  auto upstream = downstream->get_upstream();
  auto handler = upstream->get_client_handler();
  auto sni = handler->get_tls_sni();

  return mrb_str_new(mrb, sni.data(), sni.size());
}
} // namespace

namespace {
mrb_value env_get_tls_client_fingerprint_md(mrb_state *mrb, const EVP_MD *md) {
  auto data = static_cast<MRubyAssocData *>(mrb->ud);
  auto downstream = data->downstream;
  auto upstream = downstream->get_upstream();
  auto handler = upstream->get_client_handler();
  auto ssl = handler->get_ssl();

  if (!ssl) {
    return mrb_str_new_static(mrb, "", 0);
  }

#if OPENSSL_3_0_0_API
  auto x = SSL_get0_peer_certificate(ssl);
#else  // !OPENSSL_3_0_0_API
  auto x = SSL_get_peer_certificate(ssl);
#endif // !OPENSSL_3_0_0_API
  if (!x) {
    return mrb_str_new_static(mrb, "", 0);
  }

  // Currently the largest hash value is SHA-256, which is 32 bytes.
  std::array<uint8_t, 32> buf;
  auto slen = tls::get_x509_fingerprint(buf.data(), buf.size(), x, md);
#if !OPENSSL_3_0_0_API
  X509_free(x);
#endif // !OPENSSL_3_0_0_API
  if (slen == -1) {
    mrb_raise(mrb, E_RUNTIME_ERROR, "could not compute client fingerprint");
  }

  auto &balloc = downstream->get_block_allocator();
  auto f = util::format_hex(balloc,
                            std::span{buf.data(), static_cast<size_t>(slen)});
  return mrb_str_new(mrb, f.data(), f.size());
}
} // namespace

namespace {
mrb_value env_get_tls_client_fingerprint_sha256(mrb_state *mrb,
                                                mrb_value self) {
  return env_get_tls_client_fingerprint_md(mrb, EVP_sha256());
}
} // namespace

namespace {
mrb_value env_get_tls_client_fingerprint_sha1(mrb_state *mrb, mrb_value self) {
  return env_get_tls_client_fingerprint_md(mrb, EVP_sha1());
}
} // namespace

namespace {
mrb_value env_get_tls_client_subject_name(mrb_state *mrb, mrb_value self) {
  auto data = static_cast<MRubyAssocData *>(mrb->ud);
  auto downstream = data->downstream;
  auto upstream = downstream->get_upstream();
  auto handler = upstream->get_client_handler();
  auto ssl = handler->get_ssl();

  if (!ssl) {
    return mrb_str_new_static(mrb, "", 0);
  }

#if OPENSSL_3_0_0_API
  auto x = SSL_get0_peer_certificate(ssl);
#else  // !OPENSSL_3_0_0_API
  auto x = SSL_get_peer_certificate(ssl);
#endif // !OPENSSL_3_0_0_API
  if (!x) {
    return mrb_str_new_static(mrb, "", 0);
  }

  auto &balloc = downstream->get_block_allocator();
  auto name = tls::get_x509_subject_name(balloc, x);
#if !OPENSSL_3_0_0_API
  X509_free(x);
#endif // !OPENSSL_3_0_0_API
  return mrb_str_new(mrb, name.data(), name.size());
}
} // namespace

namespace {
mrb_value env_get_tls_client_issuer_name(mrb_state *mrb, mrb_value self) {
  auto data = static_cast<MRubyAssocData *>(mrb->ud);
  auto downstream = data->downstream;
  auto upstream = downstream->get_upstream();
  auto handler = upstream->get_client_handler();
  auto ssl = handler->get_ssl();

  if (!ssl) {
    return mrb_str_new_static(mrb, "", 0);
  }

#if OPENSSL_3_0_0_API
  auto x = SSL_get0_peer_certificate(ssl);
#else  // !OPENSSL_3_0_0_API
  auto x = SSL_get_peer_certificate(ssl);
#endif // !OPENSSL_3_0_0_API
  if (!x) {
    return mrb_str_new_static(mrb, "", 0);
  }

  auto &balloc = downstream->get_block_allocator();
  auto name = tls::get_x509_issuer_name(balloc, x);
#if !OPENSSL_3_0_0_API
  X509_free(x);
#endif // !OPENSSL_3_0_0_API
  return mrb_str_new(mrb, name.data(), name.size());
}
} // namespace

namespace {
mrb_value env_get_tls_client_serial(mrb_state *mrb, mrb_value self) {
  auto data = static_cast<MRubyAssocData *>(mrb->ud);
  auto downstream = data->downstream;
  auto upstream = downstream->get_upstream();
  auto handler = upstream->get_client_handler();
  auto ssl = handler->get_ssl();

  if (!ssl) {
    return mrb_str_new_static(mrb, "", 0);
  }

#if OPENSSL_3_0_0_API
  auto x = SSL_get0_peer_certificate(ssl);
#else  // !OPENSSL_3_0_0_API
  auto x = SSL_get_peer_certificate(ssl);
#endif // !OPENSSL_3_0_0_API
  if (!x) {
    return mrb_str_new_static(mrb, "", 0);
  }

  auto &balloc = downstream->get_block_allocator();
  auto sn = tls::get_x509_serial(balloc, x);
#if !OPENSSL_3_0_0_API
  X509_free(x);
#endif // !OPENSSL_3_0_0_API
  return mrb_str_new(mrb, sn.data(), sn.size());
}
} // namespace

namespace {
mrb_value env_get_tls_client_not_before(mrb_state *mrb, mrb_value self) {
  auto data = static_cast<MRubyAssocData *>(mrb->ud);
  auto downstream = data->downstream;
  auto upstream = downstream->get_upstream();
  auto handler = upstream->get_client_handler();
  auto ssl = handler->get_ssl();

  if (!ssl) {
    return mrb_fixnum_value(0);
  }

#if OPENSSL_3_0_0_API
  auto x = SSL_get0_peer_certificate(ssl);
#else  // !OPENSSL_3_0_0_API
  auto x = SSL_get_peer_certificate(ssl);
#endif // !OPENSSL_3_0_0_API
  if (!x) {
    return mrb_fixnum_value(0);
  }

  time_t t;
  if (tls::get_x509_not_before(t, x) != 0) {
    t = 0;
  }

#if !OPENSSL_3_0_0_API
  X509_free(x);
#endif // !OPENSSL_3_0_0_API

  return mrb_fixnum_value(t);
}
} // namespace

namespace {
mrb_value env_get_tls_client_not_after(mrb_state *mrb, mrb_value self) {
  auto data = static_cast<MRubyAssocData *>(mrb->ud);
  auto downstream = data->downstream;
  auto upstream = downstream->get_upstream();
  auto handler = upstream->get_client_handler();
  auto ssl = handler->get_ssl();

  if (!ssl) {
    return mrb_fixnum_value(0);
  }

#if OPENSSL_3_0_0_API
  auto x = SSL_get0_peer_certificate(ssl);
#else  // !OPENSSL_3_0_0_API
  auto x = SSL_get_peer_certificate(ssl);
#endif // !OPENSSL_3_0_0_API
  if (!x) {
    return mrb_fixnum_value(0);
  }

  time_t t;
  if (tls::get_x509_not_after(t, x) != 0) {
    t = 0;
  }

#if !OPENSSL_3_0_0_API
  X509_free(x);
#endif // !OPENSSL_3_0_0_API

  return mrb_fixnum_value(t);
}
} // namespace

namespace {
mrb_value env_get_tls_cipher(mrb_state *mrb, mrb_value self) {
  auto data = static_cast<MRubyAssocData *>(mrb->ud);
  auto downstream = data->downstream;
  auto upstream = downstream->get_upstream();
  auto handler = upstream->get_client_handler();
  auto ssl = handler->get_ssl();

  if (!ssl) {
    return mrb_str_new_static(mrb, "", 0);
  }

  return mrb_str_new_cstr(mrb, SSL_get_cipher_name(ssl));
}
} // namespace

namespace {
mrb_value env_get_tls_protocol(mrb_state *mrb, mrb_value self) {
  auto data = static_cast<MRubyAssocData *>(mrb->ud);
  auto downstream = data->downstream;
  auto upstream = downstream->get_upstream();
  auto handler = upstream->get_client_handler();
  auto ssl = handler->get_ssl();

  if (!ssl) {
    return mrb_str_new_static(mrb, "", 0);
  }

  return mrb_str_new_cstr(mrb, nghttp2::tls::get_tls_protocol(ssl));
}
} // namespace

namespace {
mrb_value env_get_tls_session_id(mrb_state *mrb, mrb_value self) {
  auto data = static_cast<MRubyAssocData *>(mrb->ud);
  auto downstream = data->downstream;
  auto upstream = downstream->get_upstream();
  auto handler = upstream->get_client_handler();
  auto ssl = handler->get_ssl();

  if (!ssl) {
    return mrb_str_new_static(mrb, "", 0);
  }

  auto session = SSL_get_session(ssl);
  if (!session) {
    return mrb_str_new_static(mrb, "", 0);
  }

  unsigned int session_id_length = 0;
  auto session_id = SSL_SESSION_get_id(session, &session_id_length);

  auto &balloc = downstream->get_block_allocator();
  auto id = util::format_hex(balloc, std::span{session_id, session_id_length});
  return mrb_str_new(mrb, id.data(), id.size());
}
} // namespace

namespace {
mrb_value env_get_tls_session_reused(mrb_state *mrb, mrb_value self) {
  auto data = static_cast<MRubyAssocData *>(mrb->ud);
  auto downstream = data->downstream;
  auto upstream = downstream->get_upstream();
  auto handler = upstream->get_client_handler();
  auto ssl = handler->get_ssl();

  if (!ssl) {
    return mrb_false_value();
  }

  return SSL_session_reused(ssl) ? mrb_true_value() : mrb_false_value();
}
} // namespace

namespace {
mrb_value env_get_alpn(mrb_state *mrb, mrb_value self) {
  auto data = static_cast<MRubyAssocData *>(mrb->ud);
  auto downstream = data->downstream;
  auto upstream = downstream->get_upstream();
  auto handler = upstream->get_client_handler();
  auto alpn = handler->get_alpn();
  return mrb_str_new(mrb, alpn.data(), alpn.size());
}
} // namespace

namespace {
mrb_value env_get_tls_handshake_finished(mrb_state *mrb, mrb_value self) {
  auto data = static_cast<MRubyAssocData *>(mrb->ud);
  auto downstream = data->downstream;
  auto upstream = downstream->get_upstream();
  auto handler = upstream->get_client_handler();
  auto conn = handler->get_connection();
  return SSL_is_init_finished(conn->tls.ssl) ? mrb_true_value()
                                             : mrb_false_value();
}
} // namespace

void init_env_class(mrb_state *mrb, RClass *module) {
  auto env_class =
      mrb_define_class_under(mrb, module, "Env", mrb->object_class);

  mrb_define_method(mrb, env_class, "initialize", env_init, MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "req", env_get_req, MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "resp", env_get_resp, MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "ctx", env_get_ctx, MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "phase", env_get_phase, MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "remote_addr", env_get_remote_addr,
                    MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "server_addr", env_get_server_addr,
                    MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "server_port", env_get_server_port,
                    MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "tls_used", env_get_tls_used,
                    MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "tls_sni", env_get_tls_sni,
                    MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "tls_client_fingerprint_sha256",
                    env_get_tls_client_fingerprint_sha256, MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "tls_client_fingerprint_sha1",
                    env_get_tls_client_fingerprint_sha1, MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "tls_client_issuer_name",
                    env_get_tls_client_issuer_name, MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "tls_client_subject_name",
                    env_get_tls_client_subject_name, MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "tls_client_serial",
                    env_get_tls_client_serial, MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "tls_client_not_before",
                    env_get_tls_client_not_before, MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "tls_client_not_after",
                    env_get_tls_client_not_after, MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "tls_cipher", env_get_tls_cipher,
                    MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "tls_protocol", env_get_tls_protocol,
                    MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "tls_session_id", env_get_tls_session_id,
                    MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "tls_session_reused",
                    env_get_tls_session_reused, MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "alpn", env_get_alpn, MRB_ARGS_NONE());
  mrb_define_method(mrb, env_class, "tls_handshake_finished",
                    env_get_tls_handshake_finished, MRB_ARGS_NONE());
}

} // namespace mruby

} // namespace shrpx