Copilot commented on code in PR #12683: URL: https://github.com/apache/trafficserver/pull/12683#discussion_r2548864848
########## plugins/lua/ts_lua_client_cert_helpers.h: ########## @@ -0,0 +1,247 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +// Helper functions for certificate data extraction +static std::string +get_x509_name_string(X509_NAME *name) +{ + if (!name) { + return ""; + } + + BIO *bio = BIO_new(BIO_s_mem()); + if (!bio) { + return ""; + } + + if (X509_NAME_print_ex(bio, name, 0, XN_FLAG_RFC2253) <= 0) { + BIO_free(bio); + return ""; + } + + char *data = nullptr; + long length = BIO_get_mem_data(bio, &data); + std::string result; + + if (data && length > 0) { + result.assign(data, length); + } + + BIO_free(bio); + return result; +} + +static std::string +get_x509_serial_string(X509 *cert) +{ + if (!cert) { + return ""; + } + + ASN1_INTEGER *serial = X509_get_serialNumber(cert); + if (!serial) { + return ""; + } + + BIO *bio = BIO_new(BIO_s_mem()); + if (!bio) { + return ""; + } + + if (i2a_ASN1_INTEGER(bio, serial) <= 0) { + BIO_free(bio); + return ""; + } + + char *data = nullptr; + long length = BIO_get_mem_data(bio, &data); + std::string result; + + if (data && length > 0) { + result.assign(data, length); + } + + BIO_free(bio); + return result; +} + +static std::string +get_x509_time_string(ASN1_TIME *time) +{ + if (!time) { + return ""; + } + + BIO *bio = BIO_new(BIO_s_mem()); + if (!bio) { + return ""; + } + + if (ASN1_TIME_print(bio, time) <= 0) { + BIO_free(bio); + return ""; + } + + char *data = nullptr; + long length = BIO_get_mem_data(bio, &data); + std::string result; + + if (data && length > 0) { + result.assign(data, length); + } + + BIO_free(bio); + return result; +} + +static std::string +get_x509_pem_string(X509 *cert) +{ + if (!cert) { + return ""; + } + + BIO *bio = BIO_new(BIO_s_mem()); + if (!bio) { + return ""; + } + + if (PEM_write_bio_X509(bio, cert) <= 0) { + BIO_free(bio); + return ""; + } + + char *data = nullptr; + long length = BIO_get_mem_data(bio, &data); + std::string result; + + if (data && length > 0) { + result.assign(data, length); + } + + BIO_free(bio); + return result; +} + +static std::string +get_x509_signature_string(X509 *cert) +{ + if (!cert) { + return ""; + } + + const ASN1_BIT_STRING *sig = nullptr; +#if OPENSSL_VERSION_NUMBER >= 0x10100000L Review Comment: Inconsistent OpenSSL version check. The helper file uses `OPENSSL_VERSION_NUMBER >= 0x10100000L` (OpenSSL 1.1.0+) while the main file `ts_lua_client_request.cc` uses `OPENSSL_IS_OPENSSL3` for similar OpenSSL API compatibility checks. This inconsistency could lead to build issues or runtime incompatibilities. Consider using `OPENSSL_IS_OPENSSL3` consistently across both files, or document why different version checks are needed for different APIs. ```suggestion #if defined(OPENSSL_IS_OPENSSL3) ``` ########## plugins/lua/ts_lua_client_request.cc: ########## @@ -1169,6 +1203,927 @@ ts_lua_client_request_get_ssl_curve(lua_State *L) return 1; } +// Certificate API Functions +static void +ts_lua_inject_client_request_cert_api(lua_State *L) +{ + // Client certificate functions + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_pem); + lua_setfield(L, -2, "client_cert_get_pem"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_subject); + lua_setfield(L, -2, "client_cert_get_subject"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_issuer); + lua_setfield(L, -2, "client_cert_get_issuer"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_serial); + lua_setfield(L, -2, "client_cert_get_serial"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_signature); + lua_setfield(L, -2, "client_cert_get_signature"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_not_before); + lua_setfield(L, -2, "client_cert_get_not_before"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_not_after); + lua_setfield(L, -2, "client_cert_get_not_after"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_version); + lua_setfield(L, -2, "client_cert_get_version"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_san_dns); + lua_setfield(L, -2, "client_cert_get_san_dns"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_san_ip); + lua_setfield(L, -2, "client_cert_get_san_ip"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_san_email); + lua_setfield(L, -2, "client_cert_get_san_email"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_san_uri); + lua_setfield(L, -2, "client_cert_get_san_uri"); + + // Server certificate functions + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_pem); + lua_setfield(L, -2, "server_cert_get_pem"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_subject); + lua_setfield(L, -2, "server_cert_get_subject"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_issuer); + lua_setfield(L, -2, "server_cert_get_issuer"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_serial); + lua_setfield(L, -2, "server_cert_get_serial"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_signature); + lua_setfield(L, -2, "server_cert_get_signature"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_not_before); + lua_setfield(L, -2, "server_cert_get_not_before"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_not_after); + lua_setfield(L, -2, "server_cert_get_not_after"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_version); + lua_setfield(L, -2, "server_cert_get_version"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_san_dns); + lua_setfield(L, -2, "server_cert_get_san_dns"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_san_ip); + lua_setfield(L, -2, "server_cert_get_san_ip"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_san_email); + lua_setfield(L, -2, "server_cert_get_san_email"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_san_uri); + lua_setfield(L, -2, "server_cert_get_san_uri"); +} + +// Client Certificate Functions +static int +ts_lua_client_request_client_cert_get_pem(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::string pem = get_x509_pem_string(cert); + X509_free(cert); + if (!pem.empty()) { + lua_pushlstring(L, pem.c_str(), pem.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_subject(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::string subject = get_x509_name_string(X509_get_subject_name(cert)); + X509_free(cert); + if (!subject.empty()) { + lua_pushlstring(L, subject.c_str(), subject.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_issuer(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::string issuer = get_x509_name_string(X509_get_issuer_name(cert)); + X509_free(cert); + if (!issuer.empty()) { + lua_pushlstring(L, issuer.c_str(), issuer.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_serial(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::string serial = get_x509_serial_string(cert); + X509_free(cert); + if (!serial.empty()) { + lua_pushlstring(L, serial.c_str(), serial.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_signature(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::string sig = get_x509_signature_string(cert); + X509_free(cert); + if (!sig.empty()) { + lua_pushlstring(L, sig.c_str(), sig.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_not_before(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::string not_before = get_x509_time_string(X509_get_notBefore(cert)); + X509_free(cert); + if (!not_before.empty()) { + lua_pushlstring(L, not_before.c_str(), not_before.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_not_after(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::string not_after = get_x509_time_string(X509_get_notAfter(cert)); + X509_free(cert); + if (!not_after.empty()) { + lua_pushlstring(L, not_after.c_str(), not_after.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_version(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + long version = X509_get_version(cert); + X509_free(cert); + lua_pushinteger(L, version); + return 1; + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_san_dns(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::vector<std::string> dns_names = get_x509_san_strings(cert, GEN_DNS); + X509_free(cert); + + if (!dns_names.empty()) { + lua_newtable(L); + for (size_t i = 0; i < dns_names.size(); i++) { + lua_pushlstring(L, dns_names[i].c_str(), dns_names[i].length()); + lua_rawseti(L, -2, i + 1); + } + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_san_ip(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::vector<std::string> ip_addrs = get_x509_san_strings(cert, GEN_IPADD); + X509_free(cert); + + if (!ip_addrs.empty()) { + lua_newtable(L); + for (size_t i = 0; i < ip_addrs.size(); i++) { + lua_pushlstring(L, ip_addrs[i].c_str(), ip_addrs[i].length()); + lua_rawseti(L, -2, i + 1); + } + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_san_email(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::vector<std::string> emails = get_x509_san_strings(cert, GEN_EMAIL); + X509_free(cert); + + if (!emails.empty()) { + lua_newtable(L); + for (size_t i = 0; i < emails.size(); i++) { + lua_pushlstring(L, emails[i].c_str(), emails[i].length()); + lua_rawseti(L, -2, i + 1); + } + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_san_uri(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::vector<std::string> uris = get_x509_san_strings(cert, GEN_URI); + X509_free(cert); + + if (!uris.empty()) { + lua_newtable(L); + for (size_t i = 0; i < uris.size(); i++) { + lua_pushlstring(L, uris[i].c_str(), uris[i].length()); + lua_rawseti(L, -2, i + 1); + } + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} Review Comment: [nitpick] Repeated certificate retrieval causes unnecessary overhead. Each function retrieves the certificate independently using `SSL_get1_peer_certificate`/`SSL_get_peer_certificate`, which increments reference counts and requires parsing. If a Lua script calls multiple certificate getter functions (e.g., subject, issuer, serial), the same certificate will be retrieved and freed multiple times. Consider caching the certificate in the Lua context or providing a single function that returns all certificate fields as a Lua table to reduce overhead for common use cases. ########## plugins/lua/ts_lua_client_request.cc: ########## @@ -1169,6 +1203,927 @@ ts_lua_client_request_get_ssl_curve(lua_State *L) return 1; } +// Certificate API Functions +static void +ts_lua_inject_client_request_cert_api(lua_State *L) +{ + // Client certificate functions + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_pem); + lua_setfield(L, -2, "client_cert_get_pem"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_subject); + lua_setfield(L, -2, "client_cert_get_subject"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_issuer); + lua_setfield(L, -2, "client_cert_get_issuer"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_serial); + lua_setfield(L, -2, "client_cert_get_serial"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_signature); + lua_setfield(L, -2, "client_cert_get_signature"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_not_before); + lua_setfield(L, -2, "client_cert_get_not_before"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_not_after); + lua_setfield(L, -2, "client_cert_get_not_after"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_version); + lua_setfield(L, -2, "client_cert_get_version"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_san_dns); + lua_setfield(L, -2, "client_cert_get_san_dns"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_san_ip); + lua_setfield(L, -2, "client_cert_get_san_ip"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_san_email); + lua_setfield(L, -2, "client_cert_get_san_email"); + + lua_pushcfunction(L, ts_lua_client_request_client_cert_get_san_uri); + lua_setfield(L, -2, "client_cert_get_san_uri"); + + // Server certificate functions + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_pem); + lua_setfield(L, -2, "server_cert_get_pem"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_subject); + lua_setfield(L, -2, "server_cert_get_subject"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_issuer); + lua_setfield(L, -2, "server_cert_get_issuer"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_serial); + lua_setfield(L, -2, "server_cert_get_serial"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_signature); + lua_setfield(L, -2, "server_cert_get_signature"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_not_before); + lua_setfield(L, -2, "server_cert_get_not_before"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_not_after); + lua_setfield(L, -2, "server_cert_get_not_after"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_version); + lua_setfield(L, -2, "server_cert_get_version"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_san_dns); + lua_setfield(L, -2, "server_cert_get_san_dns"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_san_ip); + lua_setfield(L, -2, "server_cert_get_san_ip"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_san_email); + lua_setfield(L, -2, "server_cert_get_san_email"); + + lua_pushcfunction(L, ts_lua_client_request_server_cert_get_san_uri); + lua_setfield(L, -2, "server_cert_get_san_uri"); +} + +// Client Certificate Functions +static int +ts_lua_client_request_client_cert_get_pem(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::string pem = get_x509_pem_string(cert); + X509_free(cert); + if (!pem.empty()) { + lua_pushlstring(L, pem.c_str(), pem.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_subject(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::string subject = get_x509_name_string(X509_get_subject_name(cert)); + X509_free(cert); + if (!subject.empty()) { + lua_pushlstring(L, subject.c_str(), subject.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_issuer(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::string issuer = get_x509_name_string(X509_get_issuer_name(cert)); + X509_free(cert); + if (!issuer.empty()) { + lua_pushlstring(L, issuer.c_str(), issuer.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_serial(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::string serial = get_x509_serial_string(cert); + X509_free(cert); + if (!serial.empty()) { + lua_pushlstring(L, serial.c_str(), serial.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_signature(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::string sig = get_x509_signature_string(cert); + X509_free(cert); + if (!sig.empty()) { + lua_pushlstring(L, sig.c_str(), sig.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_not_before(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::string not_before = get_x509_time_string(X509_get_notBefore(cert)); + X509_free(cert); + if (!not_before.empty()) { + lua_pushlstring(L, not_before.c_str(), not_before.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_not_after(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::string not_after = get_x509_time_string(X509_get_notAfter(cert)); + X509_free(cert); + if (!not_after.empty()) { + lua_pushlstring(L, not_after.c_str(), not_after.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_version(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + long version = X509_get_version(cert); + X509_free(cert); + lua_pushinteger(L, version); + return 1; + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_san_dns(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::vector<std::string> dns_names = get_x509_san_strings(cert, GEN_DNS); + X509_free(cert); + + if (!dns_names.empty()) { + lua_newtable(L); + for (size_t i = 0; i < dns_names.size(); i++) { + lua_pushlstring(L, dns_names[i].c_str(), dns_names[i].length()); + lua_rawseti(L, -2, i + 1); + } + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_san_ip(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::vector<std::string> ip_addrs = get_x509_san_strings(cert, GEN_IPADD); + X509_free(cert); + + if (!ip_addrs.empty()) { + lua_newtable(L); + for (size_t i = 0; i < ip_addrs.size(); i++) { + lua_pushlstring(L, ip_addrs[i].c_str(), ip_addrs[i].length()); + lua_rawseti(L, -2, i + 1); + } + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_san_email(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::vector<std::string> emails = get_x509_san_strings(cert, GEN_EMAIL); + X509_free(cert); + + if (!emails.empty()) { + lua_newtable(L); + for (size_t i = 0; i < emails.size(); i++) { + lua_pushlstring(L, emails[i].c_str(), emails[i].length()); + lua_rawseti(L, -2, i + 1); + } + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_client_cert_get_san_uri(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); +#ifdef OPENSSL_IS_OPENSSL3 + X509 *cert = SSL_get1_peer_certificate(ssl); +#else + X509 *cert = SSL_get_peer_certificate(ssl); +#endif + if (cert) { + std::vector<std::string> uris = get_x509_san_strings(cert, GEN_URI); + X509_free(cert); + + if (!uris.empty()) { + lua_newtable(L); + for (size_t i = 0; i < uris.size(); i++) { + lua_pushlstring(L, uris[i].c_str(), uris[i].length()); + lua_rawseti(L, -2, i + 1); + } + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +// Server Certificate Functions +static int +ts_lua_client_request_server_cert_get_pem(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); + X509 *cert = SSL_get_certificate(ssl); + if (cert) { + std::string pem = get_x509_pem_string(cert); + if (!pem.empty()) { + lua_pushlstring(L, pem.c_str(), pem.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_server_cert_get_subject(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); + X509 *cert = SSL_get_certificate(ssl); + if (cert) { + std::string subject = get_x509_name_string(X509_get_subject_name(cert)); + if (!subject.empty()) { + lua_pushlstring(L, subject.c_str(), subject.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_server_cert_get_issuer(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); + X509 *cert = SSL_get_certificate(ssl); + if (cert) { + std::string issuer = get_x509_name_string(X509_get_issuer_name(cert)); + if (!issuer.empty()) { + lua_pushlstring(L, issuer.c_str(), issuer.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_server_cert_get_serial(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); + X509 *cert = SSL_get_certificate(ssl); + if (cert) { + std::string serial = get_x509_serial_string(cert); + if (!serial.empty()) { + lua_pushlstring(L, serial.c_str(), serial.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_server_cert_get_signature(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); + X509 *cert = SSL_get_certificate(ssl); + if (cert) { + std::string sig = get_x509_signature_string(cert); + if (!sig.empty()) { + lua_pushlstring(L, sig.c_str(), sig.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_server_cert_get_not_before(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); + X509 *cert = SSL_get_certificate(ssl); + if (cert) { + std::string not_before = get_x509_time_string(X509_get_notBefore(cert)); + if (!not_before.empty()) { + lua_pushlstring(L, not_before.c_str(), not_before.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_server_cert_get_not_after(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); + X509 *cert = SSL_get_certificate(ssl); + if (cert) { + std::string not_after = get_x509_time_string(X509_get_notAfter(cert)); + if (!not_after.empty()) { + lua_pushlstring(L, not_after.c_str(), not_after.length()); + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_server_cert_get_version(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); + X509 *cert = SSL_get_certificate(ssl); + if (cert) { + long version = X509_get_version(cert); + lua_pushinteger(L, version); + return 1; + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_server_cert_get_san_dns(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); + X509 *cert = SSL_get_certificate(ssl); + if (cert) { + std::vector<std::string> dns_names = get_x509_san_strings(cert, GEN_DNS); + + if (!dns_names.empty()) { + lua_newtable(L); + for (size_t i = 0; i < dns_names.size(); i++) { + lua_pushlstring(L, dns_names[i].c_str(), dns_names[i].length()); + lua_rawseti(L, -2, i + 1); + } + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_server_cert_get_san_ip(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); + X509 *cert = SSL_get_certificate(ssl); + if (cert) { + std::vector<std::string> ip_addrs = get_x509_san_strings(cert, GEN_IPADD); + + if (!ip_addrs.empty()) { + lua_newtable(L); + for (size_t i = 0; i < ip_addrs.size(); i++) { + lua_pushlstring(L, ip_addrs[i].c_str(), ip_addrs[i].length()); + lua_rawseti(L, -2, i + 1); + } + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_server_cert_get_san_email(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); + X509 *cert = SSL_get_certificate(ssl); + if (cert) { + std::vector<std::string> emails = get_x509_san_strings(cert, GEN_EMAIL); + + if (!emails.empty()) { + lua_newtable(L); + for (size_t i = 0; i < emails.size(); i++) { + lua_pushlstring(L, emails[i].c_str(), emails[i].length()); + lua_rawseti(L, -2, i + 1); + } + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} + +static int +ts_lua_client_request_server_cert_get_san_uri(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + TSHttpSsn ssnp; + TSVConn client_conn; + + GET_HTTP_CONTEXT(http_ctx, L); + + ssnp = TSHttpTxnSsnGet(http_ctx->txnp); + client_conn = TSHttpSsnClientVConnGet(ssnp); + + if (TSVConnIsSsl(client_conn)) { + TSSslConnection ssl_conn = TSVConnSslConnectionGet(client_conn); + if (ssl_conn) { + SSL *ssl = reinterpret_cast<SSL *>(ssl_conn); + X509 *cert = SSL_get_certificate(ssl); + if (cert) { + std::vector<std::string> uris = get_x509_san_strings(cert, GEN_URI); + + if (!uris.empty()) { + lua_newtable(L); + for (size_t i = 0; i < uris.size(); i++) { + lua_pushlstring(L, uris[i].c_str(), uris[i].length()); + lua_rawseti(L, -2, i + 1); + } + return 1; + } + } + } + } + + lua_pushnil(L); + return 1; +} Review Comment: [nitpick] Significant code duplication between client and server certificate functions. Each pair of functions (e.g., `client_cert_get_pem` vs `server_cert_get_pem`) differs only in how the certificate is retrieved: client certificates use `SSL_get1_peer_certificate`/`SSL_get_peer_certificate` with `X509_free`, while server certificates use `SSL_get_certificate` without freeing. Consider refactoring to use a helper function that takes a certificate type parameter to reduce the ~900 lines of duplicated code and improve maintainability. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
