Merge pull request #1060 from nghttp2/nghttpx-add-client-serial

Nghttpx add client serial
This commit is contained in:
Tatsuhiro Tsujikawa 2017-11-16 23:26:35 +09:00 committed by GitHub
commit c479f6122f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 108 additions and 0 deletions

View File

@ -384,6 +384,11 @@ respectively.
Return the subject name of a client certificate. Return the subject name of a client certificate.
.. rb:attr_reader:: tls_client_serial
Return the serial number of a client certificate in a format
like "ff:ff:ff:ff:ff:ff:ff:ff".
.. rb:attr_reader:: tls_cipher .. rb:attr_reader:: tls_cipher
Return a TLS cipher negotiated in this connection. Return a TLS cipher negotiated in this connection.

View File

@ -195,6 +195,7 @@ LOGVARS = [
"tls_client_fingerprint_sha1", "tls_client_fingerprint_sha1",
"tls_client_subject_name", "tls_client_subject_name",
"tls_client_issuer_name", "tls_client_issuer_name",
"tls_client_serial",
"backend_host", "backend_host",
"backend_port", "backend_port",
] ]

View File

@ -2502,6 +2502,8 @@ Logging:
certificate. certificate.
* $tls_client_issuer_name: issuer name in client * $tls_client_issuer_name: issuer name in client
certificate. certificate.
* $tls_client_serial: serial number in client
certificate.
* $tls_protocol: protocol for SSL/TLS connection. * $tls_protocol: protocol for SSL/TLS connection.
* $tls_session_id: session ID for SSL/TLS connection. * $tls_session_id: session ID for SSL/TLS connection.
* $tls_session_reused: "r" if SSL/TLS session was * $tls_session_reused: "r" if SSL/TLS session was

View File

@ -498,6 +498,15 @@ LogFragmentType log_var_lookup_token(const char *name, size_t namelen) {
break; break;
} }
break; break;
case 17:
switch (name[16]) {
case 'l':
if (util::strieq_l("tls_client_seria", name, 16)) {
return SHRPX_LOGF_TLS_CLIENT_SERIAL;
}
break;
}
break;
case 18: case 18:
switch (name[17]) { switch (name[17]) {
case 'd': case 'd':

View File

@ -579,6 +579,25 @@ void upstream_accesslog(const std::vector<LogFragment> &lfv,
std::tie(p, last) = copy(name, p, last); std::tie(p, last) = copy(name, p, last);
break; break;
} }
case SHRPX_LOGF_TLS_CLIENT_SERIAL: {
if (!lgsp.ssl) {
std::tie(p, last) = copy('-', p, last);
break;
}
auto x = SSL_get_peer_certificate(lgsp.ssl);
if (!x) {
std::tie(p, last) = copy('-', p, last);
break;
}
auto sn = tls::get_x509_serial(balloc, x);
X509_free(x);
if (sn.empty()) {
std::tie(p, last) = copy('-', p, last);
break;
}
std::tie(p, last) = copy(sn, p, last);
break;
}
case SHRPX_LOGF_BACKEND_HOST: case SHRPX_LOGF_BACKEND_HOST:
if (!downstream_addr) { if (!downstream_addr) {
std::tie(p, last) = copy('-', p, last); std::tie(p, last) = copy('-', p, last);

View File

@ -141,6 +141,7 @@ enum LogFragmentType {
SHRPX_LOGF_TLS_CLIENT_FINGERPRINT_SHA1, SHRPX_LOGF_TLS_CLIENT_FINGERPRINT_SHA1,
SHRPX_LOGF_TLS_CLIENT_FINGERPRINT_SHA256, SHRPX_LOGF_TLS_CLIENT_FINGERPRINT_SHA256,
SHRPX_LOGF_TLS_CLIENT_ISSUER_NAME, SHRPX_LOGF_TLS_CLIENT_ISSUER_NAME,
SHRPX_LOGF_TLS_CLIENT_SERIAL,
SHRPX_LOGF_TLS_CLIENT_SUBJECT_NAME, SHRPX_LOGF_TLS_CLIENT_SUBJECT_NAME,
SHRPX_LOGF_BACKEND_HOST, SHRPX_LOGF_BACKEND_HOST,
SHRPX_LOGF_BACKEND_PORT, SHRPX_LOGF_BACKEND_PORT,

View File

@ -235,6 +235,30 @@ mrb_value env_get_tls_client_issuer_name(mrb_state *mrb, mrb_value self) {
} }
} // namespace } // 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);
}
auto x = SSL_get_peer_certificate(ssl);
if (!x) {
return mrb_str_new_static(mrb, "", 0);
}
auto &balloc = downstream->get_block_allocator();
auto sn = tls::get_x509_serial(balloc, x);
X509_free(x);
return mrb_str_new(mrb, sn.c_str(), sn.size());
}
} // namespace
namespace { namespace {
mrb_value env_get_tls_cipher(mrb_state *mrb, mrb_value self) { mrb_value env_get_tls_cipher(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud); auto data = static_cast<MRubyAssocData *>(mrb->ud);
@ -348,6 +372,8 @@ void init_env_class(mrb_state *mrb, RClass *module) {
env_get_tls_client_issuer_name, MRB_ARGS_NONE()); env_get_tls_client_issuer_name, MRB_ARGS_NONE());
mrb_define_method(mrb, env_class, "tls_client_subject_name", mrb_define_method(mrb, env_class, "tls_client_subject_name",
env_get_tls_client_subject_name, MRB_ARGS_NONE()); 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_cipher", env_get_tls_cipher, mrb_define_method(mrb, env_class, "tls_cipher", env_get_tls_cipher,
MRB_ARGS_NONE()); MRB_ARGS_NONE());
mrb_define_method(mrb, env_class, "tls_protocol", env_get_tls_protocol, mrb_define_method(mrb, env_class, "tls_protocol", env_get_tls_protocol,

View File

@ -1959,6 +1959,46 @@ StringRef get_x509_issuer_name(BlockAllocator &balloc, X509 *x) {
return get_x509_name(balloc, X509_get_issuer_name(x)); return get_x509_name(balloc, X509_get_issuer_name(x));
} }
StringRef get_x509_serial(BlockAllocator &balloc, X509 *x) {
#if OPENSSL_1_1_API
auto sn = X509_get0_serialNumber(x);
uint64_t r;
if (ASN1_INTEGER_get_uint64(&r, sn) != 1) {
return StringRef{};
}
auto iov = make_byte_ref(balloc, 16 + 7 + 1);
auto p = iov.base;
for (int i = 56; i >= 0; i -= 8) {
auto a = r >> i;
*p++ = util::LOWER_XDIGITS[(a >> 4) & 0xf];
*p++ = util::LOWER_XDIGITS[a & 0xf];
*p++ = ':';
}
#else // !OPENSSL_1_1_API
auto sn = X509_get_serialNumber(x);
auto bn = BN_new();
auto bn_d = defer(BN_free, bn);
if (!ASN1_INTEGER_to_BN(sn, bn)) {
return StringRef{};
}
std::array<uint8_t, 8> b;
auto n = BN_bn2bin(bn, b.data());
assert(n == b.size());
auto iov = make_byte_ref(balloc, 16 + 7 + 1);
auto p = iov.base;
for (auto c : b) {
*p++ = util::LOWER_XDIGITS[c >> 4];
*p++ = util::LOWER_XDIGITS[c & 0xf];
*p++ = ':';
}
#endif // !OPENSSL_1_1_API
*--p = '\0';
return StringRef{iov.base, p};
}
} // namespace tls } // namespace tls
} // namespace shrpx } // namespace shrpx

View File

@ -284,6 +284,11 @@ StringRef get_x509_subject_name(BlockAllocator &balloc, X509 *x);
// name, it returns an empty string. // name, it returns an empty string.
StringRef get_x509_issuer_name(BlockAllocator &balloc, X509 *x); StringRef get_x509_issuer_name(BlockAllocator &balloc, X509 *x);
// Returns serial number of |x| in format like
// "ff:ff:ff:ff:ff:ff:ff:ff". If this function fails to get serial
// number, it returns an empty string. number
StringRef get_x509_serial(BlockAllocator &balloc, X509 *x);
} // namespace tls } // namespace tls
} // namespace shrpx } // namespace shrpx