Skip to content

Commit 8baf12b

Browse files
committed
Merge branch 'PHP-7.1'
2 parents d162147 + ff93f74 commit 8baf12b

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

ext/openssl/openssl.c

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,11 @@ int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
668668
return 1;
669669
}
670670

671+
static const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *asn1)
672+
{
673+
return M_ASN1_STRING_data(asn1);
674+
}
675+
671676
#if OPENSSL_VERSION_NUMBER < 0x10002000L || defined (LIBRESSL_VERSION_NUMBER)
672677

673678
static int X509_get_signature_nid(const X509 *x)
@@ -837,9 +842,9 @@ static void add_assoc_name_entry(zval * val, char * key, X509_NAME * name, int s
837842
}
838843

839844
for (i = 0; i < X509_NAME_entry_count(name); i++) {
840-
unsigned char *to_add = NULL;
845+
const unsigned char *to_add = NULL;
841846
int to_add_len = 0;
842-
int needs_free = 0;
847+
unsigned char *to_add_buf = NULL;
843848

844849
ne = X509_NAME_get_entry(name, i);
845850
obj = X509_NAME_ENTRY_get_object(ne);
@@ -854,34 +859,36 @@ static void add_assoc_name_entry(zval * val, char * key, X509_NAME * name, int s
854859
str = X509_NAME_ENTRY_get_data(ne);
855860
if (ASN1_STRING_type(str) != V_ASN1_UTF8STRING) {
856861
/* ASN1_STRING_to_UTF8(3): The converted data is copied into a newly allocated buffer */
857-
to_add_len = ASN1_STRING_to_UTF8(&to_add, str);
858-
needs_free = 1;
862+
to_add_len = ASN1_STRING_to_UTF8(&to_add_buf, str);
863+
to_add = to_add_buf;
859864
} else {
860-
/* ASN1_STRING_data(3): Since this is an internal pointer it should not be freed or modified in any way */
861-
to_add = ASN1_STRING_data(str);
865+
/* ASN1_STRING_get0_data(3): Since this is an internal pointer it should not be freed or modified in any way */
866+
to_add = ASN1_STRING_get0_data(str);
862867
to_add_len = ASN1_STRING_length(str);
863868
}
864869

865870
if (to_add_len != -1) {
866871
if ((data = zend_hash_str_find(Z_ARRVAL(subitem), sname, strlen(sname))) != NULL) {
867872
if (Z_TYPE_P(data) == IS_ARRAY) {
868-
add_next_index_stringl(data, (char *)to_add, to_add_len);
873+
add_next_index_stringl(data, (const char *)to_add, to_add_len);
869874
} else if (Z_TYPE_P(data) == IS_STRING) {
870875
array_init(&tmp);
871876
add_next_index_str(&tmp, zend_string_copy(Z_STR_P(data)));
872-
add_next_index_stringl(&tmp, (char *)to_add, to_add_len);
877+
add_next_index_stringl(&tmp, (const char *)to_add, to_add_len);
873878
zend_hash_str_update(Z_ARRVAL(subitem), sname, strlen(sname), &tmp);
874879
}
875880
} else {
881+
/* it might be better to expand it and pass zval from ZVAL_STRING
882+
* to zend_symtable_str_update so we do not silently drop const
883+
* but we need a test to cover this part first */
876884
add_assoc_stringl(&subitem, sname, (char *)to_add, to_add_len);
877885
}
878886
} else {
879887
php_openssl_store_errors();
880888
}
881889

882-
if (needs_free) {
883-
/* ASN1_STRING_to_UTF8(3): The buffer out should be freed using free(3) */
884-
OPENSSL_free(to_add);
890+
if (to_add_buf != NULL) {
891+
OPENSSL_free(to_add_buf);
885892
}
886893
}
887894

@@ -920,7 +927,7 @@ static time_t asn1_time_to_time_t(ASN1_UTCTIME * timestr) /* {{{ */
920927

921928
timestr_len = (size_t)ASN1_STRING_length(timestr);
922929

923-
if (timestr_len != strlen((const char*)ASN1_STRING_data(timestr))) {
930+
if (timestr_len != strlen((const char *)ASN1_STRING_get0_data(timestr))) {
924931
php_error_docref(NULL, E_WARNING, "illegal length in timestamp");
925932
return (time_t)-1;
926933
}
@@ -935,7 +942,7 @@ static time_t asn1_time_to_time_t(ASN1_UTCTIME * timestr) /* {{{ */
935942
return (time_t)-1;
936943
}
937944

938-
strbuf = estrdup((char *)ASN1_STRING_data(timestr));
945+
strbuf = estrdup((const char *)ASN1_STRING_get0_data(timestr));
939946

940947
memset(&thetime, 0, sizeof(thetime));
941948

@@ -2045,7 +2052,7 @@ PHP_FUNCTION(openssl_spki_export_challenge)
20452052
goto cleanup;
20462053
}
20472054

2048-
RETVAL_STRING((char *) ASN1_STRING_data(spki->spkac->challenge));
2055+
RETVAL_STRING((const char *)ASN1_STRING_get0_data(spki->spkac->challenge));
20492056
goto cleanup;
20502057

20512058
cleanup:
@@ -2236,19 +2243,19 @@ static int openssl_x509v3_subjectAltName(BIO *bio, X509_EXTENSION *extension)
22362243
case GEN_EMAIL:
22372244
BIO_puts(bio, "email:");
22382245
as = name->d.rfc822Name;
2239-
BIO_write(bio, ASN1_STRING_data(as),
2246+
BIO_write(bio, ASN1_STRING_get0_data(as),
22402247
ASN1_STRING_length(as));
22412248
break;
22422249
case GEN_DNS:
22432250
BIO_puts(bio, "DNS:");
22442251
as = name->d.dNSName;
2245-
BIO_write(bio, ASN1_STRING_data(as),
2252+
BIO_write(bio, ASN1_STRING_get0_data(as),
22462253
ASN1_STRING_length(as));
22472254
break;
22482255
case GEN_URI:
22492256
BIO_puts(bio, "URI:");
22502257
as = name->d.uniformResourceIdentifier;
2251-
BIO_write(bio, ASN1_STRING_data(as),
2258+
BIO_write(bio, ASN1_STRING_get0_data(as),
22522259
ASN1_STRING_length(as));
22532260
break;
22542261
default:

0 commit comments

Comments
 (0)