|
33 | 33 |
|
34 | 34 | int php_openssl_apply_verification_policy(SSL *ssl, X509 *peer, php_stream *stream TSRMLS_DC);
|
35 | 35 | SSL *php_SSL_new_from_context(SSL_CTX *ctx, php_stream *stream TSRMLS_DC);
|
| 36 | +int php_openssl_get_x509_list_id(void); |
36 | 37 |
|
37 | 38 | /* This implementation is very closely tied to the that of the native
|
38 | 39 | * sockets implemented in the core.
|
@@ -414,9 +415,63 @@ static inline int php_openssl_enable_crypto(php_stream *stream,
|
414 | 415 | SSL_shutdown(sslsock->ssl_handle);
|
415 | 416 | } else {
|
416 | 417 | 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 | + } |
417 | 470 | }
|
418 | 471 |
|
419 |
| - X509_free(peer_cert); |
| 472 | + if (peer_cert) { |
| 473 | + X509_free(peer_cert); |
| 474 | + } |
420 | 475 | } else {
|
421 | 476 | n = errno == EAGAIN ? 0 : -1;
|
422 | 477 | }
|
|
0 commit comments