196963
From 2874696a5a8d46639d261571f915c493cd875897 Mon Sep 17 00:00:00 2001
196963
From: Stanislav Malyshev <stas@php.net>
196963
Date: Tue, 13 Aug 2013 22:20:33 -0700
196963
Subject: [PATCH] Fix CVE-2013-4073 - handling of certs with null bytes
196963
196963
---
196963
 NEWS                                |  4 ++
196963
 ext/openssl/openssl.c               | 86 ++++++++++++++++++++++++++++++++++++-
196963
 ext/openssl/tests/cve2013_4073.pem  | 28 ++++++++++++
196963
 ext/openssl/tests/cve2013_4073.phpt | 19 ++++++++
196963
 4 files changed, 135 insertions(+), 2 deletions(-)
196963
 create mode 100644 ext/openssl/tests/cve2013_4073.pem
196963
 create mode 100644 ext/openssl/tests/cve2013_4073.phpt
196963
196963
diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c
196963
index d7ac117..c32748c 100644
196963
--- a/ext/openssl/openssl.c
196963
+++ b/ext/openssl/openssl.c
196963
@@ -1398,6 +1398,74 @@ PHP_FUNCTION(openssl_x509_check_private_key)
196963
 }
196963
 /* }}} */
196963
 
196963
+/* Special handling of subjectAltName, see CVE-2013-4073
196963
+ * Christian Heimes
196963
+ */
196963
+
196963
+static int openssl_x509v3_subjectAltName(BIO *bio, X509_EXTENSION *extension)
196963
+{
196963
+	GENERAL_NAMES *names;
196963
+	const X509V3_EXT_METHOD *method = NULL;
196963
+	long i, length, num;
196963
+	const unsigned char *p;
196963
+
196963
+	method = X509V3_EXT_get(extension);
196963
+	if (method == NULL) {
196963
+		return -1;
196963
+	}
196963
+
196963
+	p = extension->value->data;
196963
+	length = extension->value->length;
196963
+	if (method->it) {
196963
+		names = (GENERAL_NAMES*)(ASN1_item_d2i(NULL, &p, length,
196963
+						       ASN1_ITEM_ptr(method->it)));
196963
+	} else {
196963
+		names = (GENERAL_NAMES*)(method->d2i(NULL, &p, length));
196963
+	}
196963
+	if (names == NULL) {
196963
+		return -1;
196963
+	}
196963
+
196963
+	num = sk_GENERAL_NAME_num(names);
196963
+	for (i = 0; i < num; i++) {
196963
+			GENERAL_NAME *name;
196963
+			ASN1_STRING *as;
196963
+			name = sk_GENERAL_NAME_value(names, i);
196963
+			switch (name->type) {
196963
+				case GEN_EMAIL:
196963
+					BIO_puts(bio, "email:");
196963
+					as = name->d.rfc822Name;
196963
+					BIO_write(bio, ASN1_STRING_data(as),
196963
+						  ASN1_STRING_length(as));
196963
+					break;
196963
+				case GEN_DNS:
196963
+					BIO_puts(bio, "DNS:");
196963
+					as = name->d.dNSName;
196963
+					BIO_write(bio, ASN1_STRING_data(as),
196963
+						  ASN1_STRING_length(as));
196963
+					break;
196963
+				case GEN_URI:
196963
+					BIO_puts(bio, "URI:");
196963
+					as = name->d.uniformResourceIdentifier;
196963
+					BIO_write(bio, ASN1_STRING_data(as),
196963
+						  ASN1_STRING_length(as));
196963
+					break;
196963
+				default:
196963
+					/* use builtin print for GEN_OTHERNAME, GEN_X400,
196963
+					 * GEN_EDIPARTY, GEN_DIRNAME, GEN_IPADD and GEN_RID
196963
+					 */
196963
+					GENERAL_NAME_print(bio, name);
196963
+			}
196963
+			/* trailing ', ' except for last element */
196963
+			if (i < (num - 1)) {
196963
+				BIO_puts(bio, ", ");
196963
+			}
196963
+	}
196963
+	sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
196963
+
196963
+	return 0;
196963
+}
196963
+
196963
 /* {{{ proto array openssl_x509_parse(mixed x509 [, bool shortnames=true])
196963
    Returns an array of the fields/values of the CERT */
196963
 PHP_FUNCTION(openssl_x509_parse)
196963
@@ -1494,15 +1562,29 @@ PHP_FUNCTION(openssl_x509_parse)
196963
 
196963
 
196963
 	for (i = 0; i < X509_get_ext_count(cert); i++) {
196963
+		int nid;
196963
 		extension = X509_get_ext(cert, i);
196963
-		if (OBJ_obj2nid(X509_EXTENSION_get_object(extension)) != NID_undef) {
196963
+		nid = OBJ_obj2nid(X509_EXTENSION_get_object(extension));
196963
+		if (nid != NID_undef) {
196963
 			extname = (char *)OBJ_nid2sn(OBJ_obj2nid(X509_EXTENSION_get_object(extension)));
196963
 		} else {
196963
 			OBJ_obj2txt(buf, sizeof(buf)-1, X509_EXTENSION_get_object(extension), 1);
196963
 			extname = buf;
196963
 		}
196963
 		bio_out = BIO_new(BIO_s_mem());
196963
-		if (X509V3_EXT_print(bio_out, extension, 0, 0)) {
196963
+		if (nid == NID_subject_alt_name) {
196963
+			if (openssl_x509v3_subjectAltName(bio_out, extension) == 0) {
196963
+				add_assoc_stringl(subitem, extname, bio_buf->data, bio_buf->length, 1);
196963
+			} else {
196963
+				zval_dtor(return_value);
196963
+				if (certresource == -1 && cert) {
196963
+					X509_free(cert);
196963
+				}
196963
+				BIO_free(bio_out);
196963
+				RETURN_FALSE;
196963
+			}
196963
+		}
196963
+		else if (X509V3_EXT_print(bio_out, extension, 0, 0)) {
196963
 			BIO_get_mem_ptr(bio_out, &bio_buf);
196963
 			add_assoc_stringl(subitem, extname, bio_buf->data, bio_buf->length, 1);
196963
 		} else {
196963
diff --git a/ext/openssl/tests/cve2013_4073.pem b/ext/openssl/tests/cve2013_4073.pem
196963
new file mode 100644
196963
index 0000000..7ebb994
196963
--- /dev/null
196963
+++ b/ext/openssl/tests/cve2013_4073.pem
196963
@@ -0,0 +1,28 @@
196963
+-----BEGIN CERTIFICATE-----
196963
+MIIE2DCCA8CgAwIBAgIBADANBgkqhkiG9w0BAQUFADCBxTELMAkGA1UEBhMCVVMx
196963
+DzANBgNVBAgMBk9yZWdvbjESMBAGA1UEBwwJQmVhdmVydG9uMSMwIQYDVQQKDBpQ
196963
+eXRob24gU29mdHdhcmUgRm91bmRhdGlvbjEgMB4GA1UECwwXUHl0aG9uIENvcmUg
196963
+RGV2ZWxvcG1lbnQxJDAiBgNVBAMMG251bGwucHl0aG9uLm9yZwBleGFtcGxlLm9y
196963
+ZzEkMCIGCSqGSIb3DQEJARYVcHl0aG9uLWRldkBweXRob24ub3JnMB4XDTEzMDgw
196963
+NzEzMTE1MloXDTEzMDgwNzEzMTI1MlowgcUxCzAJBgNVBAYTAlVTM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Fj2MUqxpKbdb9noRDy2CnHWf7EIYZ1gznXPdwzSN4YCjV5d+Q9xtBaowT0j
196963
+HPERs1ZuytCNNJTmhyqZ8q6uzMLoht4IqH/FBfpvgaeC5tBTnTT0rD5A/olXeimk
196963
+kX4LxlEx5RAvpGB2zZVRGr6LobD9rVK91xuHYNIxxxfEGE8tCCWjp0+3ksri9SXx
196963
+VHWBnbM9YaL32u3hxm8sYB/Yb8WSBavJCWJJqRStVRHM1koZlJmXNx2BX4vPo6iW
196963
+RFEIPQsFZRLrtnCAiEhyT8bC2s/Njlu6ly9gtJZWSV46Q3ZjBL4q9sHKqZQ=
196963
+-----END CERTIFICATE-----
196963
diff --git a/ext/openssl/tests/cve2013_4073.phpt b/ext/openssl/tests/cve2013_4073.phpt
196963
new file mode 100644
196963
index 0000000..e676ddf
196963
--- /dev/null
196963
+++ b/ext/openssl/tests/cve2013_4073.phpt
196963
@@ -0,0 +1,19 @@
196963
+--TEST--
196963
+CVE 2013-4073: Null-byte certificate handling
196963
+--SKIPIF--
196963
+
196963
+if (!extension_loaded("openssl")) die("skip");
196963
+--FILE--
196963
+
196963
+$cert = file_get_contents(__DIR__ . '/cve2013_4073.pem');
196963
+$info = openssl_x509_parse($cert);
196963
+var_export($info['extensions']);
196963
+
196963
+--EXPECTF--
196963
+array (
196963
+  'basicConstraints' => 'CA:FALSE',
196963
+  'subjectKeyIdentifier' => '88:5A:55:C0:52:FF:61:CD:52:A3:35:0F:EA:5A:9C:24:38:22:F7:5C',
196963
+  'keyUsage' => 'Digital Signature, Non Repudiation, Key Encipherment',
196963
+  'subjectAltName' => 'DNS:altnull.python.org' . "\0" . 'example.com, email:null@python.org' . "\0" . 'user@example.org, URI:http://null.python.org' . "\0" . 'http://example.org, IP Address:192.0.2.1, IP Address:2001:DB8:0:0:0:0:0:1
196963
+',
196963
+)
196963
-- 
196963
1.7.11.5
196963
196963
From c1c49d6e3983c9ce0b43ffe7bf6e03b809ed048b Mon Sep 17 00:00:00 2001
196963
From: Stanislav Malyshev <stas@php.net>
196963
Date: Mon, 19 Aug 2013 01:02:12 -0700
196963
Subject: [PATCH] fix using wrong buffer pointer
196963
196963
---
196963
 ext/openssl/openssl.c | 1 +
196963
 1 file changed, 1 insertion(+)
196963
196963
diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c
196963
index c7a9f5c..e7672e4 100644
196963
--- a/ext/openssl/openssl.c
196963
+++ b/ext/openssl/openssl.c
196963
@@ -1502,6 +1502,7 @@ PHP_FUNCTION(openssl_x509_parse)
196963
 		bio_out = BIO_new(BIO_s_mem());
196963
 		if (nid == NID_subject_alt_name) {
196963
 			if (openssl_x509v3_subjectAltName(bio_out, extension) == 0) {
196963
+				BIO_get_mem_ptr(bio_out, &bio_buf);
196963
 				add_assoc_stringl(subitem, extname, bio_buf->data, bio_buf->length, 1);
196963
 			} else {
196963
 				zval_dtor(return_value);
196963
-- 
196963
1.7.11.5
196963