Skip to content

Commit 3eb5acd

Browse files
committed
Add scipts to monitor nginx ssl session ticket keys and session ticket resumptions.
1 parent 99c5554 commit 3eb5acd

File tree

5 files changed

+184
-1
lines changed

5 files changed

+184
-1
lines changed

README.markdown

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Table of Contents
4848
* [ngx-orig-resp-body-len](#ngx-orig-resp-body-len)
4949
* [zlib-deflate-chunk-size](#zlib-deflate-chunk-size)
5050
* [lj-str-tab](#lj-str-tab)
51+
* [ngx-ssl-session-ticket-keys](#ngx-ssl-session-ticket-keys)
52+
* [ngx-ssl-session-resumption-stats](#ngx-ssl-session-resumption-stats)
5153
* [Installation](#installation)
5254
* [Author](#author)
5355
* [Copyright and License](#copyright-and-license)
@@ -1453,7 +1455,31 @@ value |-------------------------------------------------- count
14531455
lj-str-tab
14541456
----------
14551457

1456-
Analayzing the structure and various statistics of the global Lua string hash table in the LuaJIT v2.1 VM.
1458+
Analyzing the structure and various statistics of the global Lua string hash table in the LuaJIT v2.1 VM.
1459+
1460+
[Back to TOC](#table-of-contents)
1461+
1462+
ngx-ssl-session-ticket-keys
1463+
----------
1464+
1465+
Dumping ssl session ticket keys of a nginx worker.
1466+
1467+
```bash
1468+
# assuming one nginx worker process has the pid 3781.
1469+
$ ./samples/ngx-ssl-session-ticket-keys.sxx -I ./tapset -x 3781
1470+
```
1471+
1472+
[Back to TOC](#table-of-contents)
1473+
1474+
ngx-ssl-session-resumption-stats
1475+
----------
1476+
1477+
Analyzing the statistics of nginx SSL/TLS session ticket resumption.
1478+
1479+
```bash
1480+
# assuming one nginx worker process has the pid 3781.
1481+
$ ./samples/ngx-ssl-session-resumption-stats.sxx -x 3781
1482+
```
14571483

14581484
[Back to TOC](#table-of-contents)
14591485

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env stap++
2+
3+
# Capture ssl session resumption statistics.
4+
5+
global total
6+
global tickets
7+
global resumed
8+
global reencrypted
9+
10+
probe begin {
11+
printf("Start tracing NGX OPENSSL ticket key callback\n");
12+
}
13+
14+
probe @pfunc(ngx_ssl_session_ticket_key_callback).return {
15+
total++;
16+
# record client session ticket decryption calls
17+
if ($enc == 0) {
18+
tickets++;
19+
if ($return > 0) resumed++;
20+
if ($return > 1) reencrypted++;
21+
}
22+
}
23+
24+
probe end {
25+
printf("Stop tracing NGX OPENSSL ticket key callback\n");
26+
printf("Total sessions: %d\n", total);
27+
printf("Total session tickets: %d\n", tickets);
28+
printf("Total resumed session: %d\n", resumed);
29+
printf("Total re-encrypted session ticket: %d\n", reencrypted);
30+
31+
if (total > 0) {
32+
ratio1 = (tickets * 100) / total;
33+
34+
} else {
35+
ratio1 = 0;
36+
}
37+
38+
if (tickets > 0) {
39+
ratio2 = (resumed * 100) / tickets;
40+
41+
} else {
42+
ratio2 = 0;
43+
}
44+
printf("Session resumption attempts ratio: %d percent\n", ratio1)
45+
printf("Session resumption success ratio: %d percent\n", ratio2)
46+
exit();
47+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env stap++
2+
3+
# Capture ssl session tickets.
4+
5+
@use nginx.array
6+
@use nginx.openssl
7+
8+
probe begin {
9+
printf("Start tracing NGX OPENSSL ticket key callback\n")
10+
}
11+
12+
// print 16-byte key name
13+
function print_key_name(name) {
14+
printf("key name: ");
15+
$*n := @cast(name, "unsigned char", "$^exec_path")
16+
for (i=0; i<16; i++) {
17+
printf("%02x", $*n[i])
18+
}
19+
printf("\n")
20+
}
21+
22+
// print 16-byte aes state
23+
function print_key_aes(state) {
24+
printf("key aes state: ");
25+
$*s := @cast(state, "unsigned char", "$^exec_path")
26+
for (i=0; i<16; i++) {
27+
printf("%02x", $*s[i])
28+
}
29+
printf("\n")
30+
}
31+
32+
// print 16-byte hmac state
33+
function print_key_hmac(state) {
34+
printf("key hmac state: ");
35+
$*s := @cast(state, "unsigned char", "$^exec_path")
36+
for (i=0; i<16; i++) {
37+
printf("%02x", $*s[i])
38+
}
39+
printf("\n")
40+
}
41+
42+
// print session ticket content
43+
function print_session_ticket_key(key) {
44+
$*k := @cast(key, "ngx_ssl_session_ticket_key_t", "$^exec_path")
45+
print_key_name($*k->name)
46+
// should disable by default the two calls below to maintain key confidentiality.
47+
print_key_aes($*k->aes_key)
48+
print_key_hmac($*k->hmac_key)
49+
}
50+
51+
probe @pfunc(ngx_ssl_session_ticket_key_callback).return {
52+
keys_index = @var("ngx_ssl_session_ticket_keys_index@src/event/ngx_event_openssl.c")
53+
num = get_ssl_ex_data_len($ssl_conn->ctx)
54+
if (keys_index > num) {
55+
printf("Error: ticket key list is not supported")
56+
57+
} else {
58+
keys = get_ssl_ex_data_item($ssl_conn->ctx, keys_index)
59+
keys_len = get_ngx_array_len(keys)
60+
if (keys_len <= 0) {
61+
printf("Error: empty key list")
62+
63+
} else {
64+
key_ptr = get_ngx_array_elts(keys)
65+
enc_key = key_ptr
66+
last_key = &@cast(key_ptr, "ngx_ssl_session_ticket_key_t", "$^exec_path")[keys_len-1]
67+
printf("keys len %d\n", keys_len)
68+
printf("enc key:\n")
69+
print_session_ticket_key(enc_key)
70+
printf("last dec key:\n")
71+
print_session_ticket_key(last_key)
72+
}
73+
}
74+
}
75+
76+
probe end {
77+
printf("Stop tracing NGX OPENSSL ticket key callback\n")
78+
exit()
79+
}

tapset/nginx/array.sxx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// module nginx.array
2+
3+
function get_ngx_array_len(ngx_arr) {
4+
$*arr := @cast(ngx_arr, "ngx_array_t", "$^exec_path")
5+
return $*arr->nelts
6+
7+
}
8+
9+
function get_ngx_array_elts(ngx_arr) {
10+
$*arr := @cast(ngx_arr, "ngx_array_t", "$^exec_path")
11+
return $*arr->elts
12+
}

tapset/nginx/openssl.sxx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// module nginx.openssl
2+
3+
// extract ex_data pointer from openssl SSL_CTX
4+
function get_ssl_ex_data(ssl_ctx) {
5+
$*ctx := @cast(ssl_ctx, "SSL_CTX", "$^exec_path")
6+
return &$*ctx->ex_data
7+
}
8+
9+
// extract number of items in SSL_CTX ex_data
10+
function get_ssl_ex_data_len(ssl_ctx) {
11+
ex_data = get_ssl_ex_data(ssl_ctx)
12+
return ex_data->sk->stack->num
13+
}
14+
15+
// extract the item specified by idx in SSL_CTX ex_data
16+
function get_ssl_ex_data_item(ssl_ctx, idx) {
17+
ex_data = get_ssl_ex_data(ssl_ctx)
18+
return ex_data->sk->stack->data[idx]
19+
}

0 commit comments

Comments
 (0)