8000 bpo-46232: Fix parsing of certs with bit string in DN (GH-30351) · python/cpython@633d0f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 633d0f9

Browse files
bpo-46232: Fix parsing of certs with bit string in DN (GH-30351)
(cherry picked from commit be095f6) Co-authored-by: Christian Heimes <christian@python.org>
1 parent 95d6271 commit 633d0f9

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The :mod:`ssl` module now handles certificates with bit strings in DN
2+
correctly.

Modules/_ssl.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,17 +1053,29 @@ _create_tuple_for_attribute(_sslmodulestate *state,
10531053
ASN1_OBJECT *name, ASN1_STRING *value)
10541054
{
10551055
Py_ssize_t buflen;
1056-
unsigned char *valuebuf = NULL;
1057-
PyObject *attr;
1056+
PyObject *pyattr;
1057+
PyObject *pyname = _asn1obj2py(state, name, 0);
10581058

1059-
buflen = ASN1_STRING_to_UTF8(&valuebuf, value);
1060-
if (buflen < 0) {
1059+
if (pyname == NULL) {
10611060
_setSSLError(state, NULL, 0, __FILE__, __LINE__);
10621061
return NULL;
10631062
}
1064-
attr = Py_BuildValue("Ns#", _asn1obj2py(state, name, 0), valuebuf, buflen);
1065-
OPENSSL_free(valuebuf);
1066-
return attr;
1063+
1064+
if (ASN1_STRING_type(value) == V_ASN1_BIT_STRING) {
1065+
buflen = ASN1_STRING_length(value);
1066+
pyattr = Py_BuildValue("Ny#", pyname, ASN1_STRING_get0_data(value), buflen);
1067+
} else {
1068+
unsigned char *valuebuf = NULL;
1069+
buflen = ASN1_STRING_to_UTF8(&valuebuf, value);
1070+
if (buflen < 0) {
1071+
_setSSLError(state, NULL, 0, __FILE__, __LINE__);
1072+
Py_DECREF(pyname);
1073+
return NULL;
1074+
}
1075+
pyattr = Py_BuildValue("Ns#", pyname, valuebuf, buflen);
1076+
OPENSSL_free(valuebuf);
1077+
}
1078+
return pyattr;
10671079
}
10681080

10691081
static PyObject *

0 commit comments

Comments
 (0)
0