Skip to content

Commit ff93f74

Browse files
committed
Merge branch 'PHP-7.0' into PHP-7.1
2 parents 6af1d7a + 3704947 commit ff93f74

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
@@ -669,6 +669,11 @@ int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
669669
return 1;
670670
}
671671

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

674679
static int X509_get_signature_nid(const X509 *x)
@@ -838,9 +843,9 @@ static void add_assoc_name_entry(zval * val, char * key, X509_NAME * name, int s
838843
}
839844

840845
for (i = 0; i < X509_NAME_entry_count(name); i++) {
841-
unsigned char *to_add = NULL;
846+
const unsigned char *to_add = NULL;
842847
int to_add_len = 0;
843-
int needs_free = 0;
848+
unsigned char *to_add_buf = NULL;
844849

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

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

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

@@ -921,7 +928,7 @@ static time_t asn1_time_to_time_t(ASN1_UTCTIME * timestr) /* {{{ */
921928

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

924-
if (timestr_len != strlen((const char*)ASN1_STRING_data(timestr))) {
931+
if (timestr_len != strlen((const char *)ASN1_STRING_get0_data(timestr))) {
925932
php_error_docref(NULL, E_WARNING, "illegal length in timestamp");
926933
return (time_t)-1;
927934
}
@@ -936,7 +943,7 @@ static time_t asn1_time_to_time_t(ASN1_UTCTIME * timestr) /* {{{ */
936943
return (time_t)-1;
937944
}
938945

939-
strbuf = estrdup((char *)ASN1_STRING_data(timestr));
946+
strbuf = estrdup((const char *)ASN1_STRING_get0_data(timestr));
940947

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

@@ -2046,7 +2053,7 @@ PHP_FUNCTION(openssl_spki_export_challenge)
20462053
goto cleanup;
20472054
}
20482055

2049-
RETVAL_STRING((char *) ASN1_STRING_data(spki->spkac->challenge));
2056+
RETVAL_STRING((const char *)ASN1_STRING_get0_data(spki->spkac->challenge));
20502057
goto cleanup;
20512058

20522059
cleanup:
@@ -2237,19 +2244,19 @@ static int openssl_x509v3_subjectAltName(BIO *bio, X509_EXTENSION *extension)
22372244
case GEN_EMAIL:
22382245
BIO_puts(bio, "email:");
22392246
as = name->d.rfc822Name;
2240-
BIO_write(bio, ASN1_STRING_data(as),
2247+
BIO_write(bio, ASN1_STRING_get0_data(as),
22412248
ASN1_STRING_length(as));
22422249
break;
22432250
case GEN_DNS:
22442251
BIO_puts(bio, "DNS:");
22452252
as = name->d.dNSName;
2246-
BIO_write(bio, ASN1_STRING_data(as),
2253+
BIO_write(bio, ASN1_STRING_get0_data(as),
22472254
ASN1_STRING_length(as));
22482255
break;
22492256
case GEN_URI:
22502257
BIO_puts(bio, "URI:");
22512258
as = name->d.uniformResourceIdentifier;
2252-
BIO_write(bio, ASN1_STRING_data(as),
2259+
BIO_write(bio, ASN1_STRING_get0_data(as),
22532260
ASN1_STRING_length(as));
22542261
break;
22552262
default:

0 commit comments

Comments
 (0)