Skip to content

Commit 85c2fc9

Browse files
feature: added new ffi function ngx_http_lua_ffi_ssl_ciphers.
1 parent 3f33dd8 commit 85c2fc9

File tree

3 files changed

+205
-0
lines changed

3 files changed

+205
-0
lines changed

src/ngx_http_lua_ssl_certby.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,109 @@ ngx_http_lua_ffi_ssl_raw_client_addr(ngx_http_request_t *r, char **addr,
961961
}
962962

963963

964+
int
965+
ngx_http_lua_ffi_ssl_ciphers(ngx_http_request_t *r, char **pciphers,
966+
size_t *cipherslen, char **err)
967+
{
968+
ngx_pool_t *pool;
969+
ngx_ssl_conn_t *ssl_conn;
970+
ngx_connection_t *c;
971+
972+
if (r->connection == NULL || r->connection->ssl == NULL) {
973+
*err = "bad request";
974+
return NGX_ERROR;
975+
}
976+
977+
ssl_conn = r->connection->ssl->connection;
978+
if (ssl_conn == NULL) {
979+
*err = "bad ssl conn";
980+
return NGX_ERROR;
981+
}
982+
983+
pool = r->pool;
984+
c = ngx_ssl_get_connection(ssl_conn);
985+
986+
#ifdef SSL_CTRL_GET_RAW_CIPHERLIST
987+
988+
int n, i, bytes;
989+
size_t len;
990+
u_char *ciphers, *p;
991+
const SSL_CIPHER *cipher;
992+
993+
bytes = SSL_get0_raw_cipherlist(c->ssl->connection, NULL);
994+
n = SSL_get0_raw_cipherlist(c->ssl->connection, &ciphers);
995+
996+
if (n <= 0) {
997+
*cipherslen = 0;
998+
return NGX_OK;
999+
}
1000+
1001+
len = 0;
1002+
n /= bytes;
1003+
1004+
for (i = 0; i < n; i++) {
1005+
cipher = SSL_CIPHER_find(c->ssl->connection, ciphers + i * bytes);
1006+
1007+
if (cipher) {
1008+
len += ngx_strlen(SSL_CIPHER_get_name(cipher));
1009+
1010+
} else {
1011+
len += sizeof("0x") - 1 + bytes * (sizeof("00") - 1);
1012+
}
1013+
1014+
len += sizeof(":") - 1;
1015+
}
1016+
1017+
*pciphers = ngx_pnalloc(pool, len);
1018+
if (*pciphers == NULL) {
1019+
return NGX_ERROR;
1020+
}
1021+
1022+
p = (u_char *) *pciphers;
1023+
1024+
for (i = 0; i < n; i++) {
1025+
cipher = SSL_CIPHER_find(c->ssl->connection, ciphers + i * bytes);
1026+
1027+
if (cipher) {
1028+
p = ngx_sprintf(p, "%s", SSL_CIPHER_get_name(cipher));
1029+
1030+
} else {
1031+
p = ngx_sprintf(p, "0x");
1032+
p = ngx_hex_dump(p, ciphers + i * bytes, bytes);
1033+
}
1034+
1035+
*p++ = ':';
1036+
}
1037+
1038+
p--;
1039+
1040+
*cipherslen = p - (u_char *) *pciphers;
1041+
1042+
#else
1043+
1044+
u_char buf[4096];
1045+
1046+
if (SSL_get_shared_ciphers(c->ssl->connection, (char *) buf, 4096)
1047+
== NULL)
1048+
{
1049+
*cipherslen = 0;
1050+
return NGX_OK;
1051+
}
1052+
1053+
*cipherslen = ngx_strlen(buf);
1054+
*pciphers = ngx_pnalloc(pool, *cipherslen);
1055+
if (*pciphers == NULL) {
1056+
return NGX_ERROR;
1057+
}
1058+
1059+
ngx_memcpy(*pciphers, buf, *cipherslen);
1060+
1061+
#endif
1062+
1063+
return NGX_OK;
1064+
}
1065+
1066+
9641067
int
9651068
ngx_http_lua_ffi_cert_pem_to_der(const u_char *pem, size_t pem_len, u_char *der,
9661069
char **err)

t/139-ssl-cert-by.t

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,3 +2321,104 @@ ssl handshake: userdata
23212321
uthread: hello from f()
23222322
uthread: killed
23232323
uthread: failed to kill: already waited or killed
2324+
2325+
2326+
2327+
=== TEST 27: get ciphers
2328+
--- http_config
2329+
lua_package_path "../lua-resty-core/lib/?.lua;;";
2330+
lua_ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384;
2331+
2332+
server {
2333+
listen 127.0.0.1:12345 ssl;
2334+
server_name test.com;
2335+
2336+
ssl_certificate_by_lua_block {
2337+
local ssl = require "ngx.ssl"
2338+
print("ciphers: ", ssl.ciphers())
2339+
}
2340+
ssl_certificate ../../cert/test.crt;
2341+
ssl_certificate_key ../../cert/test.key;
2342+
2343+
server_tokens off;
2344+
location /foo {
2345+
default_type 'text/plain';
2346+
content_by_lua_block { ngx.status = 201 ngx.say("foo") ngx.exit(201) }
2347+
more_clear_headers Date;
2348+
}
2349+
}
2350+
--- config
2351+
server_tokens off;
2352+
lua_ssl_trusted_certificate ../../cert/test.crt;
2353+
2354+
location /t {
2355+
content_by_lua_block {
2356+
do
2357+
local sock = ngx.socket.tcp()
2358+
2359+
sock:settimeout(2000)
2360+
2361+
local ok, err = sock:connect("127.0.0.1", 12345)
2362+
if not ok then
2363+
ngx.say("failed to connect: ", err)
2364+
return
2365+
end
2366+
2367+
ngx.say("connected: ", ok)
2368+
2369+
local sess, err = sock:sslhandshake(nil, "test.com", true)
2370+
if not sess then
2371+
ngx.say("failed to do SSL handshake: ", err)
2372+
return
2373+
end
2374+
2375+
ngx.say("ssl handshake: ", type(sess))
2376+
2377+
local req = "GET /foo HTTP/1.0\r\nHost: test.com\r\nConnection: close\r\n\r\n"
2378+
local bytes, err = sock:send(req)
2379+
if not bytes then
2380+
ngx.say("failed to send http request: ", err)
2381+
return
2382+
end
2383+
2384+
ngx.say("sent http request: ", bytes, " bytes.")
2385+
2386+
while true do
2387+
local line, err = sock:receive()
2388+
if not line then
2389+
-- ngx.say("failed to receive response status line: ", err)
2390+
break
2391+
end
2392+
2393+
ngx.say("received: ", line)
2394+
end
2395+
2396+
local ok, err = sock:close()
2397+
ngx.say("close: ", ok, " ", err)
2398+
end -- do
2399+
-- collectgarbage()
2400+
}
2401+
}
2402+
2403+
--- request
2404+
GET /t
2405+
--- response_body
2406+
connected: 1
2407+
ssl handshake: userdata
2408+
sent http request: 56 bytes.
2409+
received: HTTP/1.1 201 Created
2410+
received: Server: nginx
2411+
received: Content-Type: text/plain
2412+
received: Content-Length: 4
2413+
received: Connection: close
2414+
received:
2415+
received: foo
2416+
close: 1 nil
2417+
2418+
--- error_log
2419+
ciphers: ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:TLS_EMPTY_RENEGOTIATION_INFO_SCSV
2420+
2421+
--- no_error_log
2422+
[error]
2423+
[alert]
2424+
[crit]

t/166-ssl-client-hello.t

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,6 +2140,7 @@ ssl_client_hello_by_lua:1: ssl client hello by lua is running!
21402140
local ip = string.format("%d.%d.%d.%d", byte(addr, 1), byte(addr, 2),
21412141
byte(addr, 3), byte(addr, 4))
21422142
print("client ip: ", ip)
2143+
21432144
}
21442145
ssl_certificate ../../cert/test.crt;
21452146
ssl_certificate_key ../../cert/test.key;

0 commit comments

Comments
 (0)