Skip to content

Commit 676a505

Browse files
committed
Add crypto info to stream_get_meta_data() result on encrypted streams
A "crypto" key is added to the output of stream_get_meta_data() calls when invoked using a stream on which crypto is currently active. The new key's associated array contains the following keys: - protocol (string e.g. TLSv1.2, TLSv1.1, etc) - cipher_name (string) - cipher_bits (int) - cipher_version (string) If the TLS ALPN extension was used to successfully negotiate an application protocol that protocol's identifier is stored in the following key: - alpn_protocol If no ALPN protocol was negotiated the "alpn_protocol" key is not present in the crypto meta data array. More meta information concerning the stream's active encryption state may be added in the future.
1 parent f678c14 commit 676a505

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

ext/openssl/xp_ssl.c

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,6 @@ int php_openssl_setup_crypto(php_stream *stream,
16011601
sslsock->ctx = NULL;
16021602
return FAILURE;
16031603
}
1604-
16051604
if (sslsock->is_client) {
16061605
SSL_CTX_set_alpn_protos(sslsock->ctx, alpn, alpn_len);
16071606
} else {
@@ -2359,6 +2358,58 @@ static int php_openssl_sockop_set_option(php_stream *stream, int option, int val
23592358
php_stream_xport_param *xparam = (php_stream_xport_param *)ptrparam;
23602359

23612360
switch (option) {
2361+
case PHP_STREAM_OPTION_META_DATA_API:
2362+
if (sslsock->ssl_active) {
2363+
zval tmp;
2364+
char *proto_str;
2365+
const SSL_CIPHER *cipher;
2366+
2367+
array_init(&tmp);
2368+
2369+
switch (SSL_version(sslsock->ssl_handle)) {
2370+
#ifdef HAVE_TLS12
2371+
case TLS1_2_VERSION: proto_str = "TLSv1.2"; break;
2372+
#endif
2373+
#ifdef HAVE_TLS11
2374+
case TLS1_1_VERSION: proto_str = "TLSv1.1"; break;
2375+
#endif
2376+
case TLS1_VERSION: proto_str = "TLSv1"; break;
2377+
#ifdef HAVE_SSL3
2378+
case SSL3_VERSION: proto_str = "SSLv3"; break;
2379+
#endif
2380+
#ifdef HAVE_SSL2
2381+
case SSL2_VERSION: proto_str = "SSLv2"; break;
2382+
#endif
2383+
default: proto_str = "UNKNOWN";
2384+
}
2385+
2386+
cipher = SSL_get_current_cipher(sslsock->ssl_handle);
2387+
2388+
add_assoc_string(&tmp, "protocol", proto_str);
2389+
add_assoc_string(&tmp, "cipher_name", (char *) SSL_CIPHER_get_name(cipher));
2390+
add_assoc_long(&tmp, "cipher_bits", SSL_CIPHER_get_bits(cipher, NULL));
2391+
add_assoc_string(&tmp, "cipher_version", SSL_CIPHER_get_version(cipher));
2392+
2393+
#ifdef HAVE_TLS_ALPN
2394+
{
2395+
const unsigned char *alpn_proto = NULL;
2396+
unsigned int alpn_proto_len = 0;
2397+
2398+
SSL_get0_alpn_selected(sslsock->ssl_handle, &alpn_proto, &alpn_proto_len);
2399+
if (alpn_proto) {
2400+
add_assoc_stringl(&tmp, "alpn_protocol", (char *)alpn_proto, alpn_proto_len);
2401+
}
2402+
}
2403+
#endif
2404+
add_assoc_zval((zval *)ptrparam, "crypto", &tmp);
2405+
}
2406+
2407+
add_assoc_bool((zval *)ptrparam, "timed_out", sslsock->s.timeout_event);
2408+
add_assoc_bool((zval *)ptrparam, "blocked", sslsock->s.is_blocked);
2409+
add_assoc_bool((zval *)ptrparam, "eof", stream->eof);
2410+
2411+
return PHP_STREAM_OPTION_RETURN_OK;
2412+
23622413
case PHP_STREAM_OPTION_CHECK_LIVENESS:
23632414
{
23642415
struct timeval tv;

0 commit comments

Comments
 (0)