summaryrefslogtreecommitdiff
path: root/ext/openssl/openssl.c
diff options
context:
space:
mode:
authorTjerk Meesters <tjerk@muvee.com>2013-09-21 16:45:20 +0800
committerTjerk Meesters <tjerk@muvee.com>2013-09-21 16:45:20 +0800
commit8915c3fb4fa40743bdddf23013a63e014d03d02c (patch)
tree81c79b7ea06d66205f99c648fa1aa9738bb91829 /ext/openssl/openssl.c
parent9e3bedcd73265acb3d190c894860bd9aa1015121 (diff)
downloadphp-git-8915c3fb4fa40743bdddf23013a63e014d03d02c.tar.gz
added better wildcard matching for CN
Diffstat (limited to 'ext/openssl/openssl.c')
-rw-r--r--ext/openssl/openssl.c35
1 files changed, 25 insertions, 10 deletions
diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c
index 4aac4e3137..5460f3a6e1 100644
--- a/ext/openssl/openssl.c
+++ b/ext/openssl/openssl.c
@@ -4829,6 +4829,30 @@ static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx) /* {{{ */
}
/* }}} */
+static int php_openssl_match_cn(const char *subjectname, const char *certname)
+{
+ int match = strcmp(subjectname, certname) == 0;
+
+ if (!match) {
+ char *wildcard = strchr(certname, '*');
+ int prefix_len = wildcard - certname;
+
+ /* 1) prefix, if not empty, must match */
+ if (wildcard && (prefix_len == 0 || strncmp(subjectname, certname, prefix_len) == 0)) {
+ const char *suffix = subjectname + strlen(subjectname) - strlen(wildcard + 1);
+
+ /*
+ * 2) suffix must match
+ * 3) no period between prefix and suffix
+ **/
+ match = strcmp(wildcard + 1, suffix) == 0 &&
+ memchr(subjectname + prefix_len, '.', suffix - subjectname - prefix_len) == NULL;
+ }
+ }
+
+ return match;
+}
+
int php_openssl_apply_verification_policy(SSL *ssl, X509 *peer, php_stream *stream TSRMLS_DC) /* {{{ */
{
zval **val = NULL;
@@ -4881,16 +4905,7 @@ int php_openssl_apply_verification_policy(SSL *ssl, X509 *peer, php_stream *stre
return FAILURE;
}
- 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, '.');
- }
- }
+ match = php_openssl_match_cn(cnmatch, buf);
if (!match) {
/* didn't match */