|
| 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 | +} |
0 commit comments