Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e06d47c

Browse files
committed
Issue #18709: Fix CVE-2013-4238. The SSL module now handles NULL bytes
inside subjectAltName correctly. Formerly the module has used OpenSSL's GENERAL_NAME_print() function to get the string represention of ASN.1 strings for rfc822Name (email), dNSName (DNS) and uniformResourceIdentifier (URI).
2 parents 01a513b + a3811e4 commit e06d47c

4 files changed

Lines changed: 184 additions & 5 deletions

File tree

Lib/test/nullbytecert.pem

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
Certificate:
2+
Data:
3+
Version: 3 (0x2)
4+
Serial Number: 0 (0x0)
5+
Signature Algorithm: sha1WithRSAEncryption
6+
Issuer: C=US, ST=Oregon, L=Beaverton, O=Python Software Foundation, OU=Python Core Development, CN=null.python.org\x00example.org/[email protected]
7+
Validity
8+
Not Before: Aug 7 13:11:52 2013 GMT
9+
Not After : Aug 7 13:12:52 2013 GMT
10+
Subject: C=US, ST=Oregon, L=Beaverton, O=Python Software Foundation, OU=Python Core Development, CN=null.python.org\x00example.org/[email protected]
11+
Subject Public Key Info:
12+
Public Key Algorithm: rsaEncryption
13+
Public-Key: (2048 bit)
14+
Modulus:
15+
00:b5:ea:ed:c9:fb:46:7d:6f:3b:76:80:dd:3a:f3:
16+
03:94:0b:a7:a6:db:ec:1d:df:ff:23:74:08:9d:97:
17+
16:3f:a3:a4:7b:3e:1b:0e:96:59:25:03:a7:26:e2:
18+
88:a9:cf:79:cd:f7:04:56:b0:ab:79:32:6e:59:c1:
19+
32:30:54:eb:58:a8:cb:91:f0:42:a5:64:27:cb:d4:
20+
56:31:88:52:ad:cf:bd:7f:f0:06:64:1f:cc:27:b8:
21+
a3:8b:8c:f3:d8:29:1f:25:0b:f5:46:06:1b:ca:02:
22+
45:ad:7b:76:0a:9c:bf:bb:b9:ae:0d:16:ab:60:75:
23+
ae:06:3e:9c:7c:31:dc:92:2f:29:1a:e0:4b:0c:91:
24+
90:6c:e9:37:c5:90:d7:2a:d7:97:15:a3:80:8f:5d:
25+
7b:49:8f:54:30:d4:97:2c:1c:5b:37:b5:ab:69:30:
26+
68:43:d3:33:78:4b:02:60:f5:3c:44:80:a1:8f:e7:
27+
f0:0f:d1:5e:87:9e:46:cf:62:fc:f9:bf:0c:65:12:
28+
f1:93:c8:35:79:3f:c8:ec:ec:47:f5:ef:be:44:d5:
29+
ae:82:1e:2d:9a:9f:98:5a:67:65:e1:74:70:7c:cb:
30+
d3:c2:ce:0e:45:49:27:dc:e3:2d:d4:fb:48:0e:2f:
31+
9e:77:b8:14:46:c0:c4:36:ca:02:ae:6a:91:8c:da:
32+
2f:85
33+
Exponent: 65537 (0x10001)
34+
X509v3 extensions:
35+
X509v3 Basic Constraints: critical
36+
CA:FALSE
37+
X509v3 Subject Key Identifier:
38+
88:5A:55:C0:52:FF:61:CD:52:A3:35:0F:EA:5A:9C:24:38:22:F7:5C
39+
X509v3 Key Usage:
40+
Digital Signature, Non Repudiation, Key Encipherment
41+
X509v3 Subject Alternative Name:
42+
*************************************************************
43+
WARNING: The values for DNS, email and URI are WRONG. OpenSSL
44+
doesn't print the text after a NULL byte.
45+
*************************************************************
46+
DNS:altnull.python.org, email:[email protected], URI:http://null.python.org, IP Address:192.0.2.1, IP Address:2001:DB8:0:0:0:0:0:1
47+
Signature Algorithm: sha1WithRSAEncryption
48+
ac:4f:45:ef:7d:49:a8:21:70:8e:88:59:3e:d4:36:42:70:f5:
49+
a3:bd:8b:d7:a8:d0:58:f6:31:4a:b1:a4:a6:dd:6f:d9:e8:44:
50+
3c:b6:0a:71:d6:7f:b1:08:61:9d:60:ce:75:cf:77:0c:d2:37:
51+
86:02:8d:5e:5d:f9:0f:71:b4:16:a8:c1:3d:23:1c:f1:11:b3:
52+
56:6e:ca:d0:8d:34:94:e6:87:2a:99:f2:ae:ae:cc:c2:e8:86:
53+
de:08:a8:7f:c5:05:fa:6f:81:a7:82:e6:d0:53:9d:34:f4:ac:
54+
3e:40:fe:89:57:7a:29:a4:91:7e:0b:c6:51:31:e5:10:2f:a4:
55+
60:76:cd:95:51:1a:be:8b:a1:b0:fd:ad:52:bd:d7:1b:87:60:
56+
d2:31:c7:17:c4:18:4f:2d:08:25:a3:a7:4f:b7:92:ca:e2:f5:
57+
25:f1:54:75:81:9d:b3:3d:61:a2:f7:da:ed:e1:c6:6f:2c:60:
58+
1f:d8:6f:c5:92:05:ab:c9:09:62:49:a9:14:ad:55:11:cc:d6:
59+
4a:19:94:99:97:37:1d:81:5f:8b:cf:a3:a8:96:44:51:08:3d:
60+
0b:05:65:12:eb:b6:70:80:88:48:72:4f:c6:c2:da:cf:cd:8e:
61+
5b:ba:97:2f:60:b4:96:56:49:5e:3a:43:76:63:04:be:2a:f6:
62+
c1:ca:a9:94
63+
-----BEGIN CERTIFICATE-----
64+
MIIE2DCCA8CgAwIBAgIBADANBgkqhkiG9w0BAQUFADCBxTELMAkGA1UEBhMCVVMx
65+
DzANBgNVBAgMBk9yZWdvbjESMBAGA1UEBwwJQmVhdmVydG9uMSMwIQYDVQQKDBpQ
66+
eXRob24gU29mdHdhcmUgRm91bmRhdGlvbjEgMB4GA1UECwwXUHl0aG9uIENvcmUg
67+
RGV2ZWxvcG1lbnQxJDAiBgNVBAMMG251bGwucHl0aG9uLm9yZwBleGFtcGxlLm9y
68+
ZzEkMCIGCSqGSIb3DQEJARYVcHl0aG9uLWRldkBweXRob24ub3JnMB4XDTEzMDgw
69+
NzEzMTE1MloXDTEzMDgwNzEzMTI1MlowgcUxCzAJBgNVBAYTAlVTMQ8wDQYDVQQI
70+
DAZPcmVnb24xEjAQBgNVBAcMCUJlYXZlcnRvbjEjMCEGA1UECgwaUHl0aG9uIFNv
71+
ZnR3YXJlIEZvdW5kYXRpb24xIDAeBgNVBAsMF1B5dGhvbiBDb3JlIERldmVsb3Bt
72+
ZW50MSQwIgYDVQQDDBtudWxsLnB5dGhvbi5vcmcAZXhhbXBsZS5vcmcxJDAiBgkq
73+
hkiG9w0BCQEW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2BX4vPo6iW
89+
RFEIPQsFZRLrtnCAiEhyT8bC2s/Njlu6ly9gtJZWSV46Q3ZjBL4q9sHKqZQ=
90+
-----END CERTIFICATE-----

Lib/test/test_ssl.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
WRONGCERT = data_file("XXXnonexisting.pem")
5757
BADKEY = data_file("badkey.pem")
5858
NOKIACERT = data_file("nokia.pem")
59+
NULLBYTECERT = data_file("nullbytecert.pem")
5960

6061
DHFILE = data_file("dh512.pem")
6162
BYTES_DHFILE = os.fsencode(DHFILE)
@@ -176,6 +177,27 @@ def test_parse_cert(self):
176177
('DNS', 'projects.forum.nokia.com'))
177178
)
178179

180+
def test_parse_cert_CVE_2013_4238(self):
181+
p = ssl._ssl._test_decode_cert(NULLBYTECERT)
182+
if support.verbose:
183+
sys.stdout.write("\n" + pprint.pformat(p) + "\n")
184+
subject = ((('countryName', 'US'),),
185+
(('stateOrProvinceName', 'Oregon'),),
186+
(('localityName', 'Beaverton'),),
187+
(('organizationName', 'Python Software Foundation'),),
188+
(('organizationalUnitName', 'Python Core Development'),),
189+
(('commonName', 'null.python.org\x00example.org'),),
190+
(('emailAddress', '[email protected]'),))
191+
self.assertEqual(p['subject'], subject)
192+
self.assertEqual(p['issuer'], subject)
193+
self.assertEqual(p['subjectAltName'],
194+
(('DNS', 'altnull.python.org\x00example.com'),
195+
196+
('URI', 'http://null.python.org\x00http://example.org'),
197+
('IP Address', '192.0.2.1'),
198+
('IP Address', '2001:DB8:0:0:0:0:0:1\n'))
199+
)
200+
179201
def test_DER_to_PEM(self):
180202
with open(SVN_PYTHON_ORG_ROOT_CERT, 'r') as f:
181203
pem = f.read()
@@ -308,6 +330,13 @@ def fail(cert, hostname):
308330
fail(cert, 'foo.a.com')
309331
fail(cert, 'bar.foo.com')
310332

333+
# NULL bytes are bad, CVE-2013-4073
334+
cert = {'subject': ((('commonName',
335+
'null.python.org\x00example.org'),),)}
336+
ok(cert, 'null.python.org\x00example.org') # or raise an error?
337+
fail(cert, 'example.org')
338+
fail(cert, 'null.python.org')
339+
311340
# Slightly fake real-world example
312341
cert = {'notAfter': 'Jun 26 21:41:46 2011 GMT',
313342
'subject': ((('commonName', 'linuxfrz.org'),),),

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ Core and Builtins
2828
Library
2929
-------
3030

31+
- Issue #18709: Fix CVE-2013-4238. The SSL module now handles NULL bytes
32+
inside subjectAltName correctly. Formerly the module has used OpenSSL's
33+
GENERAL_NAME_print() function to get the string represention of ASN.1
34+
strings for ``rfc822Name`` (email), ``dNSName`` (DNS) and
35+
``uniformResourceIdentifier`` (URI).
36+
3137
- Issue #18701: Remove support of old CPython versions (<3.0) from C code.
3238

3339
- Issue #18756: Improve error reporting in os.urandom() when the failure

Modules/_ssl.c

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -805,12 +805,14 @@ _get_peer_alt_names (X509 *certificate) {
805805
ext->value->length));
806806

807807
for(j = 0; j < sk_GENERAL_NAME_num(names); j++) {
808-
809808
/* get a rendering of each name in the set of names */
809+
int gntype;
810+
ASN1_STRING *as = NULL;
810811

811812
name = sk_GENERAL_NAME_value(names, j);
812-
if (name->type == GEN_DIRNAME) {
813-
813+
gntype = name-> type;
814+
switch (gntype) {
815+
case GEN_DIRNAME:
814816
/* we special-case DirName as a tuple of
815817
tuples of attributes */
816818

@@ -832,11 +834,62 @@ _get_peer_alt_names (X509 *certificate) {
832834
goto fail;
833835
}
834836
PyTuple_SET_ITEM(t, 1, v);
837+
break;
835838

836-
} else {
839+
case GEN_EMAIL:
840+
case GEN_DNS:
841+
case GEN_URI:
842+
/* GENERAL_NAME_print() doesn't handle NULL bytes in ASN1_string
843+
correctly, CVE-2013-4238 */
844+
t = PyTuple_New(2);
845+
if (t == NULL)
846+
goto fail;
847+
switch (gntype) {
848+
case GEN_EMAIL:
849+
v = PyUnicode_FromString("email");
850+
as = name->d.rfc822Name;
851+
break;
852+
case GEN_DNS:
853+
v = PyUnicode_FromString("DNS");
854+
as = name->d.dNSName;
855+
break;
856+
case GEN_URI:
857+
v = PyUnicode_FromString("URI");
858+
as = name->d.uniformResourceIdentifier;
859+
break;
860+
}
861+
if (v == NULL) {
862+
Py_DECREF(t);
863+
goto fail;
864+
}
865+
PyTuple_SET_ITEM(t, 0, v);
866+
v = PyUnicode_FromStringAndSize((char *)ASN1_STRING_data(as),
867+
ASN1_STRING_length(as));
868+
if (v == NULL) {
869+
Py_DECREF(t);
870+
goto fail;
871+
}
872+
PyTuple_SET_ITEM(t, 1, v);
873+
break;
837874

875+
default:
838876
/* for everything else, we use the OpenSSL print form */
839-
877+
switch (gntype) {
878+
/* check for new general name type */
879+
case GEN_OTHERNAME:
880+
case GEN_X400:
881+
case GEN_EDIPARTY:
882+
case GEN_IPADD:
883+
case GEN_RID:
884+
break;
885+
default:
886+
if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
887+
"Unknown general name type %d",
888+
gntype) == -1) {
889+
goto fail;
890+
}
891+
break;
892+
}
840893
(void) BIO_reset(biobuf);
841894
GENERAL_NAME_print(biobuf, name);
842895
len = BIO_gets(biobuf, buf, sizeof(buf)-1);
@@ -863,6 +916,7 @@ _get_peer_alt_names (X509 *certificate) {
863916
goto fail;
864917
}
865918
PyTuple_SET_ITEM(t, 1, v);
919+
break;
866920
}
867921

868922
/* and add that rendering to the list */

0 commit comments

Comments
 (0)