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

Skip to content

Commit 824f7f3

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).
1 parent 29c3fc5 commit 824f7f3

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+
hkiG9w0BCQEWFX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X4vPo6iW
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
@@ -55,6 +55,7 @@
5555
WRONGCERT = data_file("XXXnonexisting.pem")
5656
BADKEY = data_file("badkey.pem")
5757
NOKIACERT = data_file("nokia.pem")
58+
NULLBYTECERT = data_file("nullbytecert.pem")
5859

5960
DHFILE = data_file("dh512.pem")
6061
BYTES_DHFILE = os.fsencode(DHFILE)
@@ -162,6 +163,27 @@ def test_parse_cert(self):
162163
('DNS', 'projects.forum.nokia.com'))
163164
)
164165

166+
def test_parse_cert_CVE_2013_4238(self):
167+
p = ssl._ssl._test_decode_cert(NULLBYTECERT)
168+
if support.verbose:
169+
sys.stdout.write("\n" + pprint.pformat(p) + "\n")
170+
subject = ((('countryName', 'US'),),
171+
(('stateOrProvinceName', 'Oregon'),),
172+
(('localityName', 'Beaverton'),),
173+
(('organizationName', 'Python Software Foundation'),),
174+
(('organizationalUnitName', 'Python Core Development'),),
175+
(('commonName', 'null.python.org\x00example.org'),),
176+
(('emailAddress', '[email protected]'),))
177+
self.assertEqual(p['subject'], subject)
178+
self.assertEqual(p['issuer'], subject)
179+
self.assertEqual(p['subjectAltName'],
180+
(('DNS', 'altnull.python.org\x00example.com'),
181+
182+
('URI', 'http://null.python.org\x00http://example.org'),
183+
('IP Address', '192.0.2.1'),
184+
('IP Address', '2001:DB8:0:0:0:0:0:1\n'))
185+
)
186+
165187
def test_DER_to_PEM(self):
166188
with open(SVN_PYTHON_ORG_ROOT_CERT, 'r') as f:
167189
pem = f.read()
@@ -294,6 +316,13 @@ def fail(cert, hostname):
294316
fail(cert, 'foo.a.com')
295317
fail(cert, 'bar.foo.com')
296318

319+
# NULL bytes are bad, CVE-2013-4073
320+
cert = {'subject': ((('commonName',
321+
'null.python.org\x00example.org'),),)}
322+
ok(cert, 'null.python.org\x00example.org') # or raise an error?
323+
fail(cert, 'example.org')
324+
fail(cert, 'null.python.org')
325+
297326
# Slightly fake real-world example
298327
cert = {'notAfter': 'Jun 26 21:41:46 2011 GMT',
299328
'subject': ((('commonName', 'linuxfrz.org'),),),

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ Core and Builtins
6666
Library
6767
-------
6868

69+
- Issue #18709: Fix CVE-2013-4238. The SSL module now handles NULL bytes
70+
inside subjectAltName correctly. Formerly the module has used OpenSSL's
71+
GENERAL_NAME_print() function to get the string represention of ASN.1
72+
strings for ``rfc822Name`` (email), ``dNSName`` (DNS) and
73+
``uniformResourceIdentifier`` (URI).
74+
6975
- Issue #18405: Improve the entropy of crypt.mksalt().
7076

7177
- Issue #18676: Change 'positive' to 'non-negative' in queue.py put and get

Modules/_ssl.c

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

773773
for(j = 0; j < sk_GENERAL_NAME_num(names); j++) {
774-
775774
/* get a rendering of each name in the set of names */
775+
int gntype;
776+
ASN1_STRING *as = NULL;
776777

777778
name = sk_GENERAL_NAME_value(names, j);
778-
if (name->type == GEN_DIRNAME) {
779-
779+
gntype = name-> type;
780+
switch (gntype) {
781+
case GEN_DIRNAME:
780782
/* we special-case DirName as a tuple of
781783
tuples of attributes */
782784

@@ -798,11 +800,62 @@ _get_peer_alt_names (X509 *certificate) {
798800
goto fail;
799801
}
800802
PyTuple_SET_ITEM(t, 1, v);
803+
break;
801804

802-
} else {
805+
case GEN_EMAIL:
806+
case GEN_DNS:
807+
case GEN_URI:
808+
/* GENERAL_NAME_print() doesn't handle NULL bytes in ASN1_string
809+
correctly, CVE-2013-4238 */
810+
t = PyTuple_New(2);
811+
if (t == NULL)
812+
goto fail;
813+
switch (gntype) {
814+
case GEN_EMAIL:
815+
v = PyUnicode_FromString("email");
816+
as = name->d.rfc822Name;
817+
break;
818+
case GEN_DNS:
819+
v = PyUnicode_FromString("DNS");
820+
as = name->d.dNSName;
821+
break;
822+
case GEN_URI:
823+
v = PyUnicode_FromString("URI");
824+
as = name->d.uniformResourceIdentifier;
825+
break;
826+
}
827+
if (v == NULL) {
828+
Py_DECREF(t);
829+
goto fail;
830+
}
831+
PyTuple_SET_ITEM(t, 0, v);
832+
v = PyUnicode_FromStringAndSize((char *)ASN1_STRING_data(as),
833+
ASN1_STRING_length(as));
834+
if (v == NULL) {
835+
Py_DECREF(t);
836+
goto fail;
837+
}
838+
PyTuple_SET_ITEM(t, 1, v);
839+
break;
803840

841+
default:
804842
/* for everything else, we use the OpenSSL print form */
805-
843+
switch (gntype) {
844+
/* check for new general name type */
845+
case GEN_OTHERNAME:
846+
case GEN_X400:
847+
case GEN_EDIPARTY:
848+
case GEN_IPADD:
849+
case GEN_RID:
850+
break;
851+
default:
852+
if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
853+
"Unknown general name type %d",
854+
gntype) == -1) {
855+
goto fail;
856+
}
857+
break;
858+
}
806859
(void) BIO_reset(biobuf);
807860
GENERAL_NAME_print(biobuf, name);
808861
len = BIO_gets(biobuf, buf, sizeof(buf)-1);
@@ -829,6 +882,7 @@ _get_peer_alt_names (X509 *certificate) {
829882
goto fail;
830883
}
831884
PyTuple_SET_ITEM(t, 1, v);
885+
break;
832886
}
833887

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

0 commit comments

Comments
 (0)