cvs: php4(PHP_4_3) /ext/openssl openssl.c php_openssl.h /ext/standard file.c file.h fsock.c http_fopen_wrapper.c /main network.c
| From: | Wez Furlong | Date: | Sat, 26 Apr 2003 21:34:48 +0000 |
| Subject: | cvs: php4(PHP_4_3) /ext/openssl openssl.c php_openssl.h /ext/standard file.c file.h fsock.c http_fopen_wrapper.c /main network.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-20519@lists.php.net to get a copy of this message | ||
wez Sat Apr 26 17:34:48 2003 EDT
Modified files: (Branch: PHP_4_3)
/php4/ext/openssl openssl.c php_openssl.h
/php4/ext/standard file.c file.h fsock.c http_fopen_wrapper.c
/php4/main network.c
Log:
Fix the lack of SSL certificate verification support for ssl:// sockets and
https:// streams.
This code is essential for people writing secure applications in order to avoid
man-in-the-middle attacks, and is thus regarded as a bug fix.
It is, however, optional; you need to explicitly turn on the verification
functionality, as it depends on you to specify your trusted certificate chain.
This sample demonstrates a secured https:// request, making use of the CA
bundle provided by curl:
<?php
$ctx = stream_context_create();
// Turn on verification
stream_context_set_option($ctx, "ssl", "verify_peer", true);
// Set the CA bundle (trusted certificate chain)
stream_context_set_option($ctx, "ssl", "cafile",
"/usr/local/share/curl/curl-ca-bundle.crt");
$fp = fopen("https://www.zend.com",
"rb", false, $ctx);
?>
This sample demonstrates how to roll your own https:// request, and specify
a
certificate to use for authentication; the local_cert and passphrase options
will also work for fopen().
<?php
$ctx = stream_context_create();
stream_context_set_option($ctx, "ssl", "verify_peer", true);
stream_context_set_option($ctx, "ssl", "cafile",
"/usr/local/share/curl/curl-ca-bundle.crt");
// set local cert. it MUST be a PEM encoded file containing the certificate
// AND your private key. It can also contain the certificate chain of issuers.
stream_context_set_option($ctx, "ssl", "local_cert",
"/path/to/my/cert.pem");
stream_context_set_option($ctx, "ssl", "passphrase", "secret!");
// Set the common name that we are expecting; PHP will perform limited wildcard
// matching. If the CN does not match this, the connection attempt will fail.
// The value to specify will always be the same as the Host: header you specify.
stream_context_set_option($ctx, "ssl", "CN_match",
"secure.sample.domain");
$ssl = fsockopen("ssl://secure.sample.domain", 443, $errno, $errstr, 10, $ctx);
if ($ssl) {
fwrite($ssl, "GET / HTTP/1.0\r\nHost: secure.sample.domain\r\n\r\n");
fpassthru($ssl);
}
?>
Index: php4/ext/openssl/openssl.c diff -u php4/ext/openssl/openssl.c:1.52.2.8 php4/ext/openssl/openssl.c:1.52.2.9 --- php4/ext/openssl/openssl.c:1.52.2.8 Fri Mar 7 08:42:16 2003 +++ php4/ext/openssl/openssl.c Sat Apr 26 17:34:47 2003 @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: openssl.c,v 1.52.2.8 2003/03/07 13:42:16 ddhill Exp $ */ +/* $Id: openssl.c,v 1.52.2.9 2003/04/26 21:34:47 wez Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -135,6 +135,8 @@ static int le_x509; static int le_csr; +static int ssl_stream_data_index; + /* {{{ resource destructors */ static void php_pkey_free(zend_rsrc_list_entry *rsrc TSRMLS_DC) { @@ -539,9 +541,14 @@ OpenSSL_add_all_algorithms(); ERR_load_ERR_strings(); + ERR_load_SSL_strings(); ERR_load_crypto_strings(); ERR_load_EVP_strings(); + /* register a resource id number with openSSL so that we can map SSL -> stream structures in + * openSSL callbacks */ + ssl_stream_data_index = SSL_get_ex_new_index(0, "PHP stream index", NULL, NULL, NULL); + /* purposes for cert purpose checking */ REGISTER_LONG_CONSTANT("X509_PURPOSE_SSL_CLIENT", X509_PURPOSE_SSL_CLIENT, CONST_CS|CONST_PERSISTENT); REGISTER_LONG_CONSTANT("X509_PURPOSE_SSL_SERVER", X509_PURPOSE_SSL_SERVER, CONST_CS|CONST_PERSISTENT); @@ -2959,6 +2966,226 @@ RETURN_TRUE; } /* }}} */ + +/* SSL verification functions */ + +#define GET_VER_OPT(name) SUCCESS == php_stream_context_get_option(stream->context, "ssl", name, &val) +#define GET_VER_OPT_STRING(name, str) if (GET_VER_OPT(name)) { convert_to_string_ex(val); str = Z_STRVAL_PP(val); } + +static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx) +{ + php_stream *stream; + SSL *ssl; + X509 *err_cert; + int err, depth, ret; + zval **val; + TSRMLS_FETCH(); + + ret = preverify_ok; + + /* determine the status for the current cert */ + err_cert = X509_STORE_CTX_get_current_cert(ctx); + err = X509_STORE_CTX_get_error(ctx); + depth = X509_STORE_CTX_get_error_depth(ctx); + + /* conjure the stream & context to use */ + ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); + stream = (php_stream*)SSL_get_ex_data(ssl, ssl_stream_data_index); + + /* if allow_self_signed is set, make sure that verification succeeds */ + if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT && GET_VER_OPT("allow_self_signed") && zval_is_true(*val)) { + ret = 1; + } + + /* check the depth */ + if (GET_VER_OPT("verify_depth")) { + convert_to_long_ex(val); + + if (depth > Z_LVAL_PP(val)) { + ret = 0; + X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_CHAIN_TOO_LONG); + } + } + + return ret; + +} + +PHPAPI int php_openssl_apply_verification_policy(SSL *ssl, X509 *peer, php_stream *stream TSRMLS_DC) +{ + zval **val = NULL; + char *cnmatch = NULL; + X509_NAME *name; + char buf[1024]; + int err; + + /* verification is turned off */ + if (!(GET_VER_OPT("verify_peer") && zval_is_true(*val))) { + return SUCCESS; + } + + if (peer == NULL) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not get peer certificate"); + return FAILURE; + } + + err = SSL_get_verify_result(ssl); + switch (err) { + case X509_V_OK: + /* fine */ + break; + case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: + if (GET_VER_OPT("allow_self_signed") && zval_is_true(*val)) { + /* allowed */ + break; + } + /* not allowed, so fall through */ + default: + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not verify peer: code:%d %s", err, X509_verify_cert_error_string(err)); + return FAILURE; + } + + /* if the cert passed the usual checks, apply our own local policies now */ + + name = X509_get_subject_name(peer); + + /* Does the common name match ? (used primarily for https://) */ + GET_VER_OPT_STRING("CN_match", cnmatch); + if (cnmatch) { + int match = 0; + + X509_NAME_get_text_by_NID(name, NID_commonName, buf, sizeof(buf)); + + match = strcmp(cnmatch, buf) == 0; + + if (!match && strlen(buf) > 3 && buf[0] == '*' && buf[1] == '.') { + /* Try wildcard */ + + if (strchr(buf+2, '.')) { + char *tmp = strstr(cnmatch, buf+1); + + match = tmp && strcmp(tmp, buf+2) && tmp == strchr(cnmatch, '.'); + } + } + + if (!match) { + /* didn't match */ + php_error_docref(NULL TSRMLS_CC, E_WARNING, + "Peer certificate CN=`%s' did not match expected CN=`%s'", + buf, cnmatch); + + return FAILURE; + } + } + + return SUCCESS; +} + +static int passwd_callback(char *buf, int num, int verify, void *data) +{ + php_stream *stream = (php_stream *)data; + zval **val = NULL; + char *passphrase = NULL; + /* TODO: could expand this to make a callback into PHP user-space */ + + GET_VER_OPT_STRING("passphrase", passphrase); + + if (passphrase) { + if (Z_STRLEN_PP(val) < num - 1) { + memcpy(buf, Z_STRVAL_PP(val), Z_STRLEN_PP(val)+1); + return Z_STRLEN_PP(val); + } + } + return 0; +} + +PHPAPI SSL *php_SSL_new_from_context(SSL_CTX *ctx, php_stream *stream TSRMLS_DC) +{ + zval **val = NULL; + char *cafile = NULL; + char *capath = NULL; + char *certfile = NULL; + int ok = 1; + + + /* look at context options in the stream and set appropriate verification flags */ + if (GET_VER_OPT("verify_peer") && zval_is_true(*val)) { + + /* turn on verification callback */ + SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, verify_callback); + + /* CA stuff */ + GET_VER_OPT_STRING("cafile", cafile); + GET_VER_OPT_STRING("capath", capath); + + if (cafile || capath) { + if (!SSL_CTX_load_verify_locations(ctx, cafile, capath)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set verify locations `%s' `%s'\n", cafile, capath); + return NULL; + } + } + + if (GET_VER_OPT("verify_depth")) { + convert_to_long_ex(val); + SSL_CTX_set_verify_depth(ctx, Z_LVAL_PP(val)); + } + + } else { + SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL); + } + + /* callback for the passphrase (for localcert) */ + if (GET_VER_OPT("passphrase")) { + SSL_CTX_set_default_passwd_cb_userdata(ctx, stream); + SSL_CTX_set_default_passwd_cb(ctx, passwd_callback); + } + + GET_VER_OPT_STRING("local_cert", certfile); + if (certfile) { + X509 *cert = NULL; + EVP_PKEY *key = NULL; + SSL *tmpssl; + + /* a certificate to use for authentication */ + if (SSL_CTX_use_certificate_chain_file(ctx, certfile) != 1) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set local cert chain file `%s'; Check that your cafile/capath settings include details of your certificate and its issuer", certfile); + return NULL; + } + + if (SSL_CTX_use_PrivateKey_file(ctx, certfile, SSL_FILETYPE_PEM) != 1) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set private key file `%s'", certfile); + return NULL; + } + + tmpssl = SSL_new(ctx); + cert = SSL_get_certificate(tmpssl); + + if (cert) { + key = X509_get_pubkey(cert); + EVP_PKEY_copy_parameters(key, SSL_get_privatekey(tmpssl)); + EVP_PKEY_free(key); + } + SSL_free(tmpssl); + + if (!SSL_CTX_check_private_key(ctx)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Private key does not match certificate!"); + } + } + + if (ok) { + SSL *ssl = SSL_new(ctx); + + if (ssl) { + /* map SSL => stream */ + SSL_set_ex_data(ssl, ssl_stream_data_index, stream); + } + return ssl; + } + + return NULL; +} + + /* * Local variables: Index: php4/ext/openssl/php_openssl.h diff -u php4/ext/openssl/php_openssl.h:1.10.8.1 php4/ext/openssl/php_openssl.h:1.10.8.2 --- php4/ext/openssl/php_openssl.h:1.10.8.1 Tue Dec 31 11:35:06 2002 +++ php4/ext/openssl/php_openssl.h Sat Apr 26 17:34:47 2003 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_openssl.h,v 1.10.8.1 2002/12/31 16:35:06 sebastian Exp $ */ +/* $Id: php_openssl.h,v 1.10.8.2 2003/04/26 21:34:47 wez Exp $ */ #ifndef PHP_OPENSSL_H #define PHP_OPENSSL_H @@ -64,6 +64,10 @@ PHP_FUNCTION(openssl_csr_export); PHP_FUNCTION(openssl_csr_export_to_file); PHP_FUNCTION(openssl_csr_sign); + +#include <openssl/ssl.h> +PHPAPI int php_openssl_apply_verification_policy(SSL *ssl, X509 *peer, php_stream *stream TSRMLS_DC); +PHPAPI SSL *php_SSL_new_from_context(SSL_CTX *ctx, php_stream *stream TSRMLS_DC); #else Index: php4/ext/standard/file.c diff -u php4/ext/standard/file.c:1.279.2.16 php4/ext/standard/file.c:1.279.2.17 --- php4/ext/standard/file.c:1.279.2.16 Tue Apr 22 22:37:29 2003 +++ php4/ext/standard/file.c Sat Apr 26 17:34:47 2003 @@ -21,7 +21,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: file.c,v 1.279.2.16 2003/04/23 02:37:29 iliaa Exp $ */ +/* $Id: file.c,v 1.279.2.17 2003/04/26 21:34:47 wez Exp $ */ /* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */ @@ -124,6 +124,11 @@ /* sharing globals is *evil* */ static int le_stream_context = FAILURE; +PHPAPI int php_le_stream_context(void) +{ + return le_stream_context; +} + /* }}} */ /* {{{ Module-Stuff */ @@ -493,7 +498,7 @@ do { p++; - parse_eol: +parse_eol: if (PG(magic_quotes_runtime)) { /* s is in target_buf which is freed at the end of the function */ slashed = php_addslashes(s, (p-s), &len, 0 TSRMLS_CC); Index: php4/ext/standard/file.h diff -u php4/ext/standard/file.h:1.70.2.2 php4/ext/standard/file.h:1.70.2.3 --- php4/ext/standard/file.h:1.70.2.2 Tue Dec 31 11:35:27 2002 +++ php4/ext/standard/file.h Sat Apr 26 17:34:48 2003 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: file.h,v 1.70.2.2 2002/12/31 16:35:27 sebastian Exp $ */ +/* $Id: file.h,v 1.70.2.3 2003/04/26 21:34:48 wez Exp $ */ /* Synced with php 3.0 revision 1.30 1999-06-16 [ssb] */ @@ -126,6 +126,7 @@ extern php_file_globals file_globals; #endif +PHPAPI int php_le_stream_context(void); #endif /* FILE_H */ Index: php4/ext/standard/fsock.c diff -u php4/ext/standard/fsock.c:1.106.2.3 php4/ext/standard/fsock.c:1.106.2.4 --- php4/ext/standard/fsock.c:1.106.2.3 Fri Mar 7 08:42:19 2003 +++ php4/ext/standard/fsock.c Sat Apr 26 17:34:48 2003 @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: fsock.c,v 1.106.2.3 2003/03/07 13:42:19 ddhill Exp $ */ +/* $Id: fsock.c,v 1.106.2.4 2003/04/26 21:34:48 wez Exp $ */ /* converted to PHP Streams and moved much code to main/network.c [wez] */ @@ -137,20 +137,23 @@ char *host; int host_len; long port = -1; - zval *zerrno = NULL, *zerrstr = NULL; + zval *zerrno = NULL, *zerrstr = NULL, *zcontext = NULL; double timeout = FG(default_socket_timeout); unsigned long conv; struct timeval tv; char *hashkey = NULL; php_stream *stream = NULL; + php_stream_context *context = NULL; int err; RETVAL_FALSE; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lzzd", &host, &host_len, &port, &zerrno, &zerrstr, &timeout) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lzzdr", &host, &host_len, &port, &zerrno, &zerrstr, &timeout, &zcontext) == FAILURE) { RETURN_FALSE; } - + if (zcontext) { + ZEND_FETCH_RESOURCE(context, php_stream_context*, &zcontext, -1, "stream-context", php_le_stream_context()); + } if (persistent) { spprintf(&hashkey, 0, "pfsockopen__%s:%d", host, port); @@ -222,6 +225,8 @@ if (stream == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to connect to %s:%d", host, port); + } else if (context) { + php_stream_context_set(stream, context); } #if HAVE_OPENSSL_EXT @@ -238,8 +243,11 @@ /* unknown ?? */ break; } - if (ssl_ret == FAILURE) + if (ssl_ret == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to activate SSL mode %d", ssl_flags); + php_stream_close(stream); + stream = NULL; + } } #endif @@ -272,14 +280,14 @@ /* }}} */ -/* {{{ proto int fsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]]) +/* {{{ proto int fsockopen(string hostname, int port [, int errno [, string errstr [, float timeout [, resource context]]]]) Open Internet or Unix domain socket connection */ PHP_FUNCTION(fsockopen) { php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); } /* }}} */ -/* {{{ proto int pfsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]]) +/* {{{ proto int pfsockopen(string hostname, int port [, int errno [, string errstr [, float timeout [, resource context]]]]) Open persistent Internet or Unix domain socket connection */ PHP_FUNCTION(pfsockopen) { Index: php4/ext/standard/http_fopen_wrapper.c diff -u php4/ext/standard/http_fopen_wrapper.c:1.53.2.9 php4/ext/standard/http_fopen_wrapper.c:1.53.2.10 --- php4/ext/standard/http_fopen_wrapper.c:1.53.2.9 Mon Apr 14 09:55:53 2003 +++ php4/ext/standard/http_fopen_wrapper.c Sat Apr 26 17:34:48 2003 @@ -18,7 +18,7 @@ | Wez Furlong <wez@thebrainroom.com> | +----------------------------------------------------------------------+ */ -/* $Id: http_fopen_wrapper.c,v 1.53.2.9 2003/04/14 13:55:53 iliaa Exp $ */ +/* $Id: http_fopen_wrapper.c,v 1.53.2.10 2003/04/26 21:34:48 wez Exp $ */ #include "php.h" #include "php_globals.h" @@ -153,6 +153,18 @@ #if HAVE_OPENSSL_EXT if (use_ssl) { + + if (context) { + /* set the CN we expect to be on the remote cert. + * You still need to have enabled verification (verify_peer) in the context for + * this to have an effect */ + zval *cn; + + ALLOC_INIT_ZVAL(cn); + ZVAL_STRING(cn, resource->host, 1); + php_stream_context_set_option(context, "ssl", "CN_match", cn); + } + if (php_stream_sock_ssl_activate(stream, 1) == FAILURE) { php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Unable to activate SSL mode"); php_stream_close(stream); Index: php4/main/network.c diff -u php4/main/network.c:1.83.2.5 php4/main/network.c:1.83.2.6 --- php4/main/network.c:1.83.2.5 Sat Mar 15 08:25:36 2003 +++ php4/main/network.c Sat Apr 26 17:34:48 2003 @@ -16,7 +16,7 @@ | Streams work by Wez Furlong <wez@thebrainroom.com> | +----------------------------------------------------------------------+ */ -/* $Id: network.c,v 1.83.2.5 2003/03/15 13:25:36 wez Exp $ */ +/* $Id: network.c,v 1.83.2.6 2003/04/26 21:34:48 wez Exp $ */ /*#define DEBUG_MAIN_NETWORK 1*/ @@ -51,6 +51,7 @@ #ifdef HAVE_OPENSSL_EXT #include <openssl/err.h> +#include "ext/openssl/php_openssl.h" #endif #ifdef HAVE_SYS_SELECT_H @@ -705,22 +706,26 @@ return FAILURE; } - sock->ssl_handle = SSL_new(ctx); + sock->ssl_handle = php_SSL_new_from_context(ctx, stream TSRMLS_CC); + if (sock->ssl_handle == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "php_stream_sock_ssl_activate_with_method: failed to create an SSL handle"); SSL_CTX_free(ctx); return FAILURE; } + SSL_set_connect_state(sock->ssl_handle); SSL_set_fd(sock->ssl_handle, sock->socket); if (psock) { SSL_copy_session_id(sock->ssl_handle, psock->ssl_handle); } + } if (activate) { int err; + X509 *peer_cert; do { err = SSL_connect(sock->ssl_handle); @@ -731,6 +736,17 @@ SSL_shutdown(sock->ssl_handle); return FAILURE; } + + /* handshake was ok; did the verification go ok too ? */ + peer_cert = SSL_get_peer_certificate(sock->ssl_handle); + + if (FAILURE == php_openssl_apply_verification_policy(sock->ssl_handle, peer_cert, stream TSRMLS_CC)) { + SSL_shutdown(sock->ssl_handle); + return FAILURE; + } + + X509_free(peer_cert); + sock->ssl_active = activate; } else { SSL_shutdown(sock->ssl_handle); @@ -773,6 +789,22 @@ } #if HAVE_OPENSSL_EXT + +static void php_ERR_error_string_n(int code, char *buf, size_t size) +{ + switch (code) { + case 0x1407E086: /* SSL2 */ + case 0x14090086: /* SSL3 */ + /* There does not appear to be a symbolic constant for these two codes; + * they occur when certificate verification fails. The OpenSSL provided + * error message is not particularly useful, so we special case it here */ + strncpy(buf, "Failed to verify peer certificate. Check your `cafile' and/or `capath' context options", size); + break; + default: + ERR_error_string_n(code, buf, size); + } +} + static int handle_ssl_error(php_stream *stream, int nr_bytes TSRMLS_DC) { php_netstream_data_t *sock = (php_netstream_data_t*)stream->abstract; @@ -793,6 +825,7 @@ /* re-negotiation, or perhaps the SSL layer needs more * packets: retry in next iteration */ break; + case SSL_ERROR_SYSCALL: if (ERR_peek_error() == 0) { if (nr_bytes == 0) { @@ -819,10 +852,10 @@ if (ebuf) { esbuf[0] = '\n'; esbuf[1] = '\0'; - ERR_error_string_n(code, esbuf + 1, sizeof(esbuf) - 2); + php_ERR_error_string_n(code, esbuf + 1, sizeof(esbuf) - 2); } else { esbuf[0] = '\0'; - ERR_error_string_n(code, esbuf, sizeof(esbuf) - 1); + php_ERR_error_string_n(code, esbuf, sizeof(esbuf) - 1); } code = strlen(esbuf); esbuf[code] = '\0'; @@ -840,7 +873,7 @@ php_error_docref(NULL TSRMLS_CC, E_WARNING, "SSL operation failed with code %d.%s%s", err, - ebuf ? "OpenSSL Error messages:\n" : "", + ebuf ? " OpenSSL Error messages:\n" : "", ebuf ? ebuf : ""); retry = 0;
Index: php4/ext/openssl/openssl.c diff -u php4/ext/openssl/openssl.c:1.52.2.8 php4/ext/openssl/openssl.c:1.52.2.9 --- php4/ext/openssl/openssl.c:1.52.2.8 Fri Mar 7 08:42:16 2003 +++ php4/ext/openssl/openssl.c Sat Apr 26 17:34:47 2003 @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: openssl.c,v 1.52.2.8 2003/03/07 13:42:16 ddhill Exp $ */ +/* $Id: openssl.c,v 1.52.2.9 2003/04/26 21:34:47 wez Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -135,6 +135,8 @@ static int le_x509; static int le_csr; +static int ssl_stream_data_index; + /* {{{ resource destructors */ static void php_pkey_free(zend_rsrc_list_entry *rsrc TSRMLS_DC) { @@ -539,9 +541,14 @@ OpenSSL_add_all_algorithms(); ERR_load_ERR_strings(); + ERR_load_SSL_strings(); ERR_load_crypto_strings(); ERR_load_EVP_strings(); + /* register a resource id number with openSSL so that we can map SSL -> stream structures in + * openSSL callbacks */ + ssl_stream_data_index = SSL_get_ex_new_index(0, "PHP stream index", NULL, NULL, NULL); + /* purposes for cert purpose checking */ REGISTER_LONG_CONSTANT("X509_PURPOSE_SSL_CLIENT", X509_PURPOSE_SSL_CLIENT, CONST_CS|CONST_PERSISTENT); REGISTER_LONG_CONSTANT("X509_PURPOSE_SSL_SERVER", X509_PURPOSE_SSL_SERVER, CONST_CS|CONST_PERSISTENT); @@ -2959,6 +2966,226 @@ RETURN_TRUE; } /* }}} */ + +/* SSL verification functions */ + +#define GET_VER_OPT(name) SUCCESS == php_stream_context_get_option(stream->context, "ssl", name, &val) +#define GET_VER_OPT_STRING(name, str) if (GET_VER_OPT(name)) { convert_to_string_ex(val); str = Z_STRVAL_PP(val); } + +static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx) +{ + php_stream *stream; + SSL *ssl; + X509 *err_cert; + int err, depth, ret; + zval **val; + TSRMLS_FETCH(); + + ret = preverify_ok; + + /* determine the status for the current cert */ + err_cert = X509_STORE_CTX_get_current_cert(ctx); + err = X509_STORE_CTX_get_error(ctx); + depth = X509_STORE_CTX_get_error_depth(ctx); + + /* conjure the stream & context to use */ + ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); + stream = (php_stream*)SSL_get_ex_data(ssl, ssl_stream_data_index); + + /* if allow_self_signed is set, make sure that verification succeeds */ + if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT && GET_VER_OPT("allow_self_signed") && zval_is_true(*val)) { + ret = 1; + } + + /* check the depth */ + if (GET_VER_OPT("verify_depth")) { + convert_to_long_ex(val); + + if (depth > Z_LVAL_PP(val)) { + ret = 0; + X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_CHAIN_TOO_LONG); + } + } + + return ret; + +} + +PHPAPI int php_openssl_apply_verification_policy(SSL *ssl, X509 *peer, php_stream *stream TSRMLS_DC) +{ + zval **val = NULL; + char *cnmatch = NULL; + X509_NAME *name; + char buf[1024]; + int err; + + /* verification is turned off */ + if (!(GET_VER_OPT("verify_peer") && zval_is_true(*val))) { + return SUCCESS; + } + + if (peer == NULL) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not get peer certificate"); + return FAILURE; + } + + err = SSL_get_verify_result(ssl); + switch (err) { + case X509_V_OK: + /* fine */ + break; + case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: + if (GET_VER_OPT("allow_self_signed") && zval_is_true(*val)) { + /* allowed */ + break; + } + /* not allowed, so fall through */ + default: + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not verify peer: code:%d %s", err, X509_verify_cert_error_string(err)); + return FAILURE; + } + + /* if the cert passed the usual checks, apply our own local policies now */ + + name = X509_get_subject_name(peer); + + /* Does the common name match ? (used primarily for https://) */ + GET_VER_OPT_STRING("CN_match", cnmatch); + if (cnmatch) { + int match = 0; + + X509_NAME_get_text_by_NID(name, NID_commonName, buf, sizeof(buf)); + + match = strcmp(cnmatch, buf) == 0; + + if (!match && strlen(buf) > 3 && buf[0] == '*' && buf[1] == '.') { + /* Try wildcard */ + + if (strchr(buf+2, '.')) { + char *tmp = strstr(cnmatch, buf+1); + + match = tmp && strcmp(tmp, buf+2) && tmp == strchr(cnmatch, '.'); + } + } + + if (!match) { + /* didn't match */ + php_error_docref(NULL TSRMLS_CC, E_WARNING, + "Peer certificate CN=`%s' did not match expected CN=`%s'", + buf, cnmatch); + + return FAILURE; + } + } + + return SUCCESS; +} + +static int passwd_callback(char *buf, int num, int verify, void *data) +{ + php_stream *stream = (php_stream *)data; + zval **val = NULL; + char *passphrase = NULL; + /* TODO: could expand this to make a callback into PHP user-space */ + + GET_VER_OPT_STRING("passphrase", passphrase); + + if (passphrase) { + if (Z_STRLEN_PP(val) < num - 1) { + memcpy(buf, Z_STRVAL_PP(val), Z_STRLEN_PP(val)+1); + return Z_STRLEN_PP(val); + } + } + return 0; +} + +PHPAPI SSL *php_SSL_new_from_context(SSL_CTX *ctx, php_stream *stream TSRMLS_DC) +{ + zval **val = NULL; + char *cafile = NULL; + char *capath = NULL; + char *certfile = NULL; + int ok = 1; + + + /* look at context options in the stream and set appropriate verification flags */ + if (GET_VER_OPT("verify_peer") && zval_is_true(*val)) { + + /* turn on verification callback */ + SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, verify_callback); + + /* CA stuff */ + GET_VER_OPT_STRING("cafile", cafile); + GET_VER_OPT_STRING("capath", capath); + + if (cafile || capath) { + if (!SSL_CTX_load_verify_locations(ctx, cafile, capath)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set verify locations `%s' `%s'\n", cafile, capath); + return NULL; + } + } + + if (GET_VER_OPT("verify_depth")) { + convert_to_long_ex(val); + SSL_CTX_set_verify_depth(ctx, Z_LVAL_PP(val)); + } + + } else { + SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL); + } + + /* callback for the passphrase (for localcert) */ + if (GET_VER_OPT("passphrase")) { + SSL_CTX_set_default_passwd_cb_userdata(ctx, stream); + SSL_CTX_set_default_passwd_cb(ctx, passwd_callback); + } + + GET_VER_OPT_STRING("local_cert", certfile); + if (certfile) { + X509 *cert = NULL; + EVP_PKEY *key = NULL; + SSL *tmpssl; + + /* a certificate to use for authentication */ + if (SSL_CTX_use_certificate_chain_file(ctx, certfile) != 1) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set local cert chain file `%s'; Check that your cafile/capath settings include details of your certificate and its issuer", certfile); + return NULL; + } + + if (SSL_CTX_use_PrivateKey_file(ctx, certfile, SSL_FILETYPE_PEM) != 1) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set private key file `%s'", certfile); + return NULL; + } + + tmpssl = SSL_new(ctx); + cert = SSL_get_certificate(tmpssl); + + if (cert) { + key = X509_get_pubkey(cert); + EVP_PKEY_copy_parameters(key, SSL_get_privatekey(tmpssl)); + EVP_PKEY_free(key); + } + SSL_free(tmpssl); + + if (!SSL_CTX_check_private_key(ctx)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Private key does not match certificate!"); + } + } + + if (ok) { + SSL *ssl = SSL_new(ctx); + + if (ssl) { + /* map SSL => stream */ + SSL_set_ex_data(ssl, ssl_stream_data_index, stream); + } + return ssl; + } + + return NULL; +} + + /* * Local variables: Index: php4/ext/openssl/php_openssl.h diff -u php4/ext/openssl/php_openssl.h:1.10.8.1 php4/ext/openssl/php_openssl.h:1.10.8.2 --- php4/ext/openssl/php_openssl.h:1.10.8.1 Tue Dec 31 11:35:06 2002 +++ php4/ext/openssl/php_openssl.h Sat Apr 26 17:34:47 2003 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_openssl.h,v 1.10.8.1 2002/12/31 16:35:06 sebastian Exp $ */ +/* $Id: php_openssl.h,v 1.10.8.2 2003/04/26 21:34:47 wez Exp $ */ #ifndef PHP_OPENSSL_H #define PHP_OPENSSL_H @@ -64,6 +64,10 @@ PHP_FUNCTION(openssl_csr_export); PHP_FUNCTION(openssl_csr_export_to_file); PHP_FUNCTION(openssl_csr_sign); + +#include <openssl/ssl.h> +PHPAPI int php_openssl_apply_verification_policy(SSL *ssl, X509 *peer, php_stream *stream TSRMLS_DC); +PHPAPI SSL *php_SSL_new_from_context(SSL_CTX *ctx, php_stream *stream TSRMLS_DC); #else Index: php4/ext/standard/file.c diff -u php4/ext/standard/file.c:1.279.2.16 php4/ext/standard/file.c:1.279.2.17 --- php4/ext/standard/file.c:1.279.2.16 Tue Apr 22 22:37:29 2003 +++ php4/ext/standard/file.c Sat Apr 26 17:34:47 2003 @@ -21,7 +21,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: file.c,v 1.279.2.16 2003/04/23 02:37:29 iliaa Exp $ */ +/* $Id: file.c,v 1.279.2.17 2003/04/26 21:34:47 wez Exp $ */ /* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */ @@ -124,6 +124,11 @@ /* sharing globals is *evil* */ static int le_stream_context = FAILURE; +PHPAPI int php_le_stream_context(void) +{ + return le_stream_context; +} + /* }}} */ /* {{{ Module-Stuff */ @@ -493,7 +498,7 @@ do { p++; - parse_eol: +parse_eol: if (PG(magic_quotes_runtime)) { /* s is in target_buf which is freed at the end of the function */ slashed = php_addslashes(s, (p-s), &len, 0 TSRMLS_CC); Index: php4/ext/standard/file.h diff -u php4/ext/standard/file.h:1.70.2.2 php4/ext/standard/file.h:1.70.2.3 --- php4/ext/standard/file.h:1.70.2.2 Tue Dec 31 11:35:27 2002 +++ php4/ext/standard/file.h Sat Apr 26 17:34:48 2003 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: file.h,v 1.70.2.2 2002/12/31 16:35:27 sebastian Exp $ */ +/* $Id: file.h,v 1.70.2.3 2003/04/26 21:34:48 wez Exp $ */ /* Synced with php 3.0 revision 1.30 1999-06-16 [ssb] */ @@ -126,6 +126,7 @@ extern php_file_globals file_globals; #endif +PHPAPI int php_le_stream_context(void); #endif /* FILE_H */ Index: php4/ext/standard/fsock.c diff -u php4/ext/standard/fsock.c:1.106.2.3 php4/ext/standard/fsock.c:1.106.2.4 --- php4/ext/standard/fsock.c:1.106.2.3 Fri Mar 7 08:42:19 2003 +++ php4/ext/standard/fsock.c Sat Apr 26 17:34:48 2003 @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: fsock.c,v 1.106.2.3 2003/03/07 13:42:19 ddhill Exp $ */ +/* $Id: fsock.c,v 1.106.2.4 2003/04/26 21:34:48 wez Exp $ */ /* converted to PHP Streams and moved much code to main/network.c [wez] */ @@ -137,20 +137,23 @@ char *host; int host_len; long port = -1; - zval *zerrno = NULL, *zerrstr = NULL; + zval *zerrno = NULL, *zerrstr = NULL, *zcontext = NULL; double timeout = FG(default_socket_timeout); unsigned long conv; struct timeval tv; char *hashkey = NULL; php_stream *stream = NULL; + php_stream_context *context = NULL; int err; RETVAL_FALSE; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lzzd", &host, &host_len, &port, &zerrno, &zerrstr, &timeout) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lzzdr", &host, &host_len, &port, &zerrno, &zerrstr, &timeout, &zcontext) == FAILURE) { RETURN_FALSE; } - + if (zcontext) { + ZEND_FETCH_RESOURCE(context, php_stream_context*, &zcontext, -1, "stream-context", php_le_stream_context()); + } if (persistent) { spprintf(&hashkey, 0, "pfsockopen__%s:%d", host, port); @@ -222,6 +225,8 @@ if (stream == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to connect to %s:%d", host, port); + } else if (context) { + php_stream_context_set(stream, context); } #if HAVE_OPENSSL_EXT @@ -238,8 +243,11 @@ /* unknown ?? */ break; } - if (ssl_ret == FAILURE) + if (ssl_ret == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to activate SSL mode %d", ssl_flags); + php_stream_close(stream); + stream = NULL; + } } #endif @@ -272,14 +280,14 @@ /* }}} */ -/* {{{ proto int fsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]]) +/* {{{ proto int fsockopen(string hostname, int port [, int errno [, string errstr [, float timeout [, resource context]]]]) Open Internet or Unix domain socket connection */ PHP_FUNCTION(fsockopen) { php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); } /* }}} */ -/* {{{ proto int pfsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]]) +/* {{{ proto int pfsockopen(string hostname, int port [, int errno [, string errstr [, float timeout [, resource context]]]]) Open persistent Internet or Unix domain socket connection */ PHP_FUNCTION(pfsockopen) { Index: php4/ext/standard/http_fopen_wrapper.c diff -u php4/ext/standard/http_fopen_wrapper.c:1.53.2.9 php4/ext/standard/http_fopen_wrapper.c:1.53.2.10 --- php4/ext/standard/http_fopen_wrapper.c:1.53.2.9 Mon Apr 14 09:55:53 2003 +++ php4/ext/standard/http_fopen_wrapper.c Sat Apr 26 17:34:48 2003 @@ -18,7 +18,7 @@ | Wez Furlong <wez@thebrainroom.com> | +----------------------------------------------------------------------+ */ -/* $Id: http_fopen_wrapper.c,v 1.53.2.9 2003/04/14 13:55:53 iliaa Exp $ */ +/* $Id: http_fopen_wrapper.c,v 1.53.2.10 2003/04/26 21:34:48 wez Exp $ */ #include "php.h" #include "php_globals.h" @@ -153,6 +153,18 @@ #if HAVE_OPENSSL_EXT if (use_ssl) { + + if (context) { + /* set the CN we expect to be on the remote cert. + * You still need to have enabled verification (verify_peer) in the context for + * this to have an effect */ + zval *cn; + + ALLOC_INIT_ZVAL(cn); + ZVAL_STRING(cn, resource->host, 1); + php_stream_context_set_option(context, "ssl", "CN_match", cn); + } + if (php_stream_sock_ssl_activate(stream, 1) == FAILURE) { php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Unable to activate SSL mode"); php_stream_close(stream); Index: php4/main/network.c diff -u php4/main/network.c:1.83.2.5 php4/main/network.c:1.83.2.6 --- php4/main/network.c:1.83.2.5 Sat Mar 15 08:25:36 2003 +++ php4/main/network.c Sat Apr 26 17:34:48 2003 @@ -16,7 +16,7 @@ | Streams work by Wez Furlong <wez@thebrainroom.com> | +----------------------------------------------------------------------+ */ -/* $Id: network.c,v 1.83.2.5 2003/03/15 13:25:36 wez Exp $ */ +/* $Id: network.c,v 1.83.2.6 2003/04/26 21:34:48 wez Exp $ */ /*#define DEBUG_MAIN_NETWORK 1*/ @@ -51,6 +51,7 @@ #ifdef HAVE_OPENSSL_EXT #include <openssl/err.h> +#include "ext/openssl/php_openssl.h" #endif #ifdef HAVE_SYS_SELECT_H @@ -705,22 +706,26 @@ return FAILURE; } - sock->ssl_handle = SSL_new(ctx); + sock->ssl_handle = php_SSL_new_from_context(ctx, stream TSRMLS_CC); + if (sock->ssl_handle == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "php_stream_sock_ssl_activate_with_method: failed to create an SSL handle"); SSL_CTX_free(ctx); return FAILURE; } + SSL_set_connect_state(sock->ssl_handle); SSL_set_fd(sock->ssl_handle, sock->socket); if (psock) { SSL_copy_session_id(sock->ssl_handle, psock->ssl_handle); } + } if (activate) { int err; + X509 *peer_cert; do { err = SSL_connect(sock->ssl_handle); @@ -731,6 +736,17 @@ SSL_shutdown(sock->ssl_handle); return FAILURE; } + + /* handshake was ok; did the verification go ok too ? */ + peer_cert = SSL_get_peer_certificate(sock->ssl_handle); + + if (FAILURE == php_openssl_apply_verification_policy(sock->ssl_handle, peer_cert, stream TSRMLS_CC)) { + SSL_shutdown(sock->ssl_handle); + return FAILURE; + } + + X509_free(peer_cert); + sock->ssl_active = activate; } else { SSL_shutdown(sock->ssl_handle); @@ -773,6 +789,22 @@ } #if HAVE_OPENSSL_EXT + +static void php_ERR_error_string_n(int code, char *buf, size_t size) +{ + switch (code) { + case 0x1407E086: /* SSL2 */ + case 0x14090086: /* SSL3 */ + /* There does not appear to be a symbolic constant for these two codes; + * they occur when certificate verification fails. The OpenSSL provided + * error message is not particularly useful, so we special case it here */ + strncpy(buf, "Failed to verify peer certificate. Check your `cafile' and/or `capath' context options", size); + break; + default: + ERR_error_string_n(code, buf, size); + } +} + static int handle_ssl_error(php_stream *stream, int nr_bytes TSRMLS_DC) { php_netstream_data_t *sock = (php_netstream_data_t*)stream->abstract; @@ -793,6 +825,7 @@ /* re-negotiation, or perhaps the SSL layer needs more * packets: retry in next iteration */ break; + case SSL_ERROR_SYSCALL: if (ERR_peek_error() == 0) { if (nr_bytes == 0) { @@ -819,10 +852,10 @@ if (ebuf) { esbuf[0] = '\n'; esbuf[1] = '\0'; - ERR_error_string_n(code, esbuf + 1, sizeof(esbuf) - 2); + php_ERR_error_string_n(code, esbuf + 1, sizeof(esbuf) - 2); } else { esbuf[0] = '\0'; - ERR_error_string_n(code, esbuf, sizeof(esbuf) - 1); + php_ERR_error_string_n(code, esbuf, sizeof(esbuf) - 1); } code = strlen(esbuf); esbuf[code] = '\0'; @@ -840,7 +873,7 @@ php_error_docref(NULL TSRMLS_CC, E_WARNING, "SSL operation failed with code %d.%s%s", err, - ebuf ? "OpenSSL Error messages:\n" : "", + ebuf ? " OpenSSL Error messages:\n" : "", ebuf ? ebuf : ""); retry = 0;