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

Skip to content

Commit f06628b

Browse files
committed
Make read() and certificate() return bytes instead of bytearray instances.
1 parent 254348e commit f06628b

1 file changed

Lines changed: 23 additions & 31 deletions

File tree

Modules/_ssl.c

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ _create_tuple_for_attribute (ASN1_OBJECT *name, ASN1_STRING *value) {
504504
name_obj = PyUnicode_FromStringAndSize(namebuf, buflen);
505505
if (name_obj == NULL)
506506
goto fail;
507-
507+
508508
buflen = ASN1_STRING_to_UTF8(&valuebuf, value);
509509
if (buflen < 0) {
510510
_setSSLError(NULL, 0, __FILE__, __LINE__);
@@ -590,7 +590,7 @@ _create_tuple_for_X509_NAME (X509_NAME *xname)
590590
fprintf(stderr, "RDN level %d, attribute %s: %s\n",
591591
entry->set,
592592
PyString_AS_STRING(PyTuple_GET_ITEM(attr, 0)),
593-
PyString_AS_STRING(PyTuple_GET_ITEM(attr, 1)));
593+
PyString_AS_STRING(PyTuple_GET_ITEM(attr, 1)));
594594
*/
595595
if (attr == NULL)
596596
goto fail1;
@@ -628,7 +628,7 @@ _create_tuple_for_X509_NAME (X509_NAME *xname)
628628

629629
static PyObject *
630630
_get_peer_alt_names (X509 *certificate) {
631-
631+
632632
/* this code follows the procedure outlined in
633633
OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print()
634634
function to extract the STACK_OF(GENERAL_NAME),
@@ -641,7 +641,7 @@ _get_peer_alt_names (X509 *certificate) {
641641
X509_EXTENSION *ext = NULL;
642642
GENERAL_NAMES *names = NULL;
643643
GENERAL_NAME *name;
644-
X509V3_EXT_METHOD *method;
644+
X509V3_EXT_METHOD *method;
645645
BIO *biobuf = NULL;
646646
char buf[2048];
647647
char *vptr;
@@ -663,7 +663,7 @@ _get_peer_alt_names (X509 *certificate) {
663663
if (peer_alt_names == NULL)
664664
goto fail;
665665
}
666-
666+
667667
/* now decode the altName */
668668
ext = X509_get_ext(certificate, i);
669669
if(!(method = X509V3_EXT_get(ext))) {
@@ -714,7 +714,7 @@ _get_peer_alt_names (X509 *certificate) {
714714
goto fail;
715715
}
716716
PyTuple_SET_ITEM(t, 1, v);
717-
717+
718718
} else {
719719

720720
/* for everything else, we use the OpenSSL print form */
@@ -764,7 +764,7 @@ _get_peer_alt_names (X509 *certificate) {
764764
} else {
765765
return peer_alt_names;
766766
}
767-
767+
768768

769769
fail:
770770
if (biobuf != NULL)
@@ -817,18 +817,18 @@ _decode_certificate (X509 *certificate, int verbose) {
817817
goto fail0;
818818
}
819819
Py_DECREF(issuer);
820-
820+
821821
version = PyInt_FromLong(X509_get_version(certificate) + 1);
822822
if (PyDict_SetItemString(retval, "version", version) < 0) {
823823
Py_DECREF(version);
824824
goto fail0;
825825
}
826826
Py_DECREF(version);
827827
}
828-
828+
829829
/* get a memory buffer */
830830
biobuf = BIO_new(BIO_s_mem());
831-
831+
832832
if (verbose) {
833833

834834
(void) BIO_reset(biobuf);
@@ -897,7 +897,7 @@ _decode_certificate (X509 *certificate, int verbose) {
897897
}
898898
Py_DECREF(peer_alt_names);
899899
}
900-
900+
901901
BIO_free(biobuf);
902902
return retval;
903903

@@ -945,7 +945,7 @@ PySSL_test_decode_certificate (PyObject *mod, PyObject *args) {
945945
retval = _decode_certificate(x, verbose);
946946

947947
fail0:
948-
948+
949949
if (cert != NULL) BIO_free(cert);
950950
return retval;
951951
}
@@ -977,7 +977,7 @@ PySSL_peercert(PySSLObject *self, PyObject *args)
977977
return NULL;
978978
}
979979
/* this is actually an immutable bytes sequence */
980-
retval = PyBytes_FromStringAndSize
980+
retval = PyString_FromStringAndSize
981981
((const char *) bytes_buf, len);
982982
OPENSSL_free(bytes_buf);
983983
return retval;
@@ -1044,7 +1044,7 @@ static PyObject *PySSL_cipher (PySSLObject *self) {
10441044
goto fail0;
10451045
PyTuple_SET_ITEM(retval, 2, v);
10461046
return retval;
1047-
1047+
10481048
fail0:
10491049
Py_DECREF(retval);
10501050
return NULL;
@@ -1281,13 +1281,8 @@ static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args)
12811281
Py_DECREF(buf);
12821282
return NULL;
12831283
} else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
1284-
/* should contain a zero-length string */
1285-
if (!buf_passed) {
1286-
PyBytes_Resize(buf, 0);
1287-
return buf;
1288-
} else {
1289-
return PyInt_FromLong(0);
1290-
}
1284+
count = 0;
1285+
goto done;
12911286
}
12921287
}
12931288
do {
@@ -1312,12 +1307,8 @@ static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args)
13121307
(SSL_get_shutdown(self->ssl) ==
13131308
SSL_RECEIVED_SHUTDOWN))
13141309
{
1315-
if (!buf_passed) {
1316-
PyBytes_Resize(buf, 0);
1317-
return buf;
1318-
} else {
1319-
return PyInt_FromLong(0);
1320-
}
1310+
count = 0;
1311+
goto done;
13211312
} else {
13221313
sockstate = SOCKET_OPERATION_OK;
13231314
}
@@ -1338,11 +1329,12 @@ static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args)
13381329
}
13391330
return PySSL_SetError(self, count, __FILE__, __LINE__);
13401331
}
1332+
done:
13411333
if (!buf_passed) {
1342-
if (count != len) {
1343-
PyBytes_Resize(buf, count);
1344-
}
1345-
return buf;
1334+
PyObject *res = PyString_FromStringAndSize(
1335+
PyBytes_AS_STRING(buf), count);
1336+
Py_DECREF(buf);
1337+
return res;
13461338
} else {
13471339
return PyInt_FromLong(count);
13481340
}

0 commit comments

Comments
 (0)