Skip to content

Commit 021e1a3

Browse files
committed
merge from branch: peer certificate capture context options.
1 parent 3ab9e65 commit 021e1a3

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

ext/openssl/openssl.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ static int le_x509;
156156
static int le_csr;
157157
static int ssl_stream_data_index;
158158

159+
int php_openssl_get_x509_list_id(void)
160+
{
161+
return le_x509;
162+
}
163+
159164
/* {{{ resource destructors */
160165
static void php_pkey_free(zend_rsrc_list_entry *rsrc TSRMLS_DC)
161166
{

ext/openssl/xp_ssl.c

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
int php_openssl_apply_verification_policy(SSL *ssl, X509 *peer, php_stream *stream TSRMLS_DC);
3535
SSL *php_SSL_new_from_context(SSL_CTX *ctx, php_stream *stream TSRMLS_DC);
36+
int php_openssl_get_x509_list_id(void);
3637

3738
/* This implementation is very closely tied to the that of the native
3839
* sockets implemented in the core.
@@ -414,9 +415,63 @@ static inline int php_openssl_enable_crypto(php_stream *stream,
414415
SSL_shutdown(sslsock->ssl_handle);
415416
} else {
416417
sslsock->ssl_active = 1;
418+
419+
/* allow the script to capture the peer cert
420+
* and/or the certificate chain */
421+
if (stream->context) {
422+
zval **val, *zcert;
423+
424+
if (SUCCESS == php_stream_context_get_option(
425+
stream->context, "ssl",
426+
"capture_peer_cert", &val) &&
427+
zval_is_true(*val)) {
428+
MAKE_STD_ZVAL(zcert);
429+
ZVAL_RESOURCE(zcert, zend_list_insert(peer_cert,
430+
php_openssl_get_x509_list_id()));
431+
php_stream_context_set_option(stream->context,
432+
"ssl", "peer_certificate",
433+
zcert);
434+
peer_cert = NULL;
435+
}
436+
437+
if (SUCCESS == php_stream_context_get_option(
438+
stream->context, "ssl",
439+
"capture_peer_cert_chain", &val) &&
440+
zval_is_true(*val)) {
441+
zval *arr;
442+
STACK_OF(X509) *chain;
443+
444+
MAKE_STD_ZVAL(arr);
445+
chain = SSL_get_peer_cert_chain(
446+
sslsock->ssl_handle);
447+
448+
if (chain) {
449+
int i;
450+
array_init(arr);
451+
452+
for (i = 0; i < sk_X509_num(chain); i++) {
453+
X509 *mycert = X509_dup(
454+
sk_X509_value(chain, i));
455+
MAKE_STD_ZVAL(zcert);
456+
ZVAL_RESOURCE(zcert,
457+
zend_list_insert(mycert,
458+
php_openssl_get_x509_list_id()));
459+
add_next_index_zval(arr, zcert);
460+
}
461+
} else {
462+
ZVAL_NULL(arr);
463+
}
464+
465+
php_stream_context_set_option(stream->context,
466+
"ssl", "peer_certificate_chain",
467+
arr);
468+
}
469+
}
417470
}
418471

419-
X509_free(peer_cert);
472+
if (peer_cert) {
473+
X509_free(peer_cert);
474+
}
420475
} else {
421476
n = errno == EAGAIN ? 0 : -1;
422477
}

0 commit comments

Comments
 (0)