@@ -669,6 +669,11 @@ int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
669
669
return 1 ;
670
670
}
671
671
672
+ static const unsigned char * ASN1_STRING_get0_data (const ASN1_STRING * asn1 )
673
+ {
674
+ return M_ASN1_STRING_data (asn1 );
675
+ }
676
+
672
677
#if OPENSSL_VERSION_NUMBER < 0x10002000L || defined (LIBRESSL_VERSION_NUMBER )
673
678
674
679
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
838
843
}
839
844
840
845
for (i = 0 ; i < X509_NAME_entry_count (name ); i ++ ) {
841
- unsigned char * to_add = NULL ;
846
+ const unsigned char * to_add = NULL ;
842
847
int to_add_len = 0 ;
843
- int needs_free = 0 ;
848
+ unsigned char * to_add_buf = NULL ;
844
849
845
850
ne = X509_NAME_get_entry (name , i );
846
851
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
855
860
str = X509_NAME_ENTRY_get_data (ne );
856
861
if (ASN1_STRING_type (str ) != V_ASN1_UTF8STRING ) {
857
862
/* 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 ;
860
865
} 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 );
863
868
to_add_len = ASN1_STRING_length (str );
864
869
}
865
870
866
871
if (to_add_len != -1 ) {
867
872
if ((data = zend_hash_str_find (Z_ARRVAL (subitem ), sname , strlen (sname ))) != NULL ) {
868
873
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 );
870
875
} else if (Z_TYPE_P (data ) == IS_STRING ) {
871
876
array_init (& tmp );
872
877
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 );
874
879
zend_hash_str_update (Z_ARRVAL (subitem ), sname , strlen (sname ), & tmp );
875
880
}
876
881
} 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 */
877
885
add_assoc_stringl (& subitem , sname , (char * )to_add , to_add_len );
878
886
}
879
887
} else {
880
888
php_openssl_store_errors ();
881
889
}
882
890
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 );
886
893
}
887
894
}
888
895
@@ -921,7 +928,7 @@ static time_t asn1_time_to_time_t(ASN1_UTCTIME * timestr) /* {{{ */
921
928
922
929
timestr_len = (size_t )ASN1_STRING_length (timestr );
923
930
924
- if (timestr_len != strlen ((const char * ) ASN1_STRING_data (timestr ))) {
931
+ if (timestr_len != strlen ((const char * ) ASN1_STRING_get0_data (timestr ))) {
925
932
php_error_docref (NULL , E_WARNING , "illegal length in timestamp" );
926
933
return (time_t )- 1 ;
927
934
}
@@ -936,7 +943,7 @@ static time_t asn1_time_to_time_t(ASN1_UTCTIME * timestr) /* {{{ */
936
943
return (time_t )- 1 ;
937
944
}
938
945
939
- strbuf = estrdup ((char * )ASN1_STRING_data (timestr ));
946
+ strbuf = estrdup ((const char * )ASN1_STRING_get0_data (timestr ));
940
947
941
948
memset (& thetime , 0 , sizeof (thetime ));
942
949
@@ -2046,7 +2053,7 @@ PHP_FUNCTION(openssl_spki_export_challenge)
2046
2053
goto cleanup ;
2047
2054
}
2048
2055
2049
- RETVAL_STRING ((char * ) ASN1_STRING_data (spki -> spkac -> challenge ));
2056
+ RETVAL_STRING ((const char * )ASN1_STRING_get0_data (spki -> spkac -> challenge ));
2050
2057
goto cleanup ;
2051
2058
2052
2059
cleanup :
@@ -2237,19 +2244,19 @@ static int openssl_x509v3_subjectAltName(BIO *bio, X509_EXTENSION *extension)
2237
2244
case GEN_EMAIL :
2238
2245
BIO_puts (bio , "email:" );
2239
2246
as = name -> d .rfc822Name ;
2240
- BIO_write (bio , ASN1_STRING_data (as ),
2247
+ BIO_write (bio , ASN1_STRING_get0_data (as ),
2241
2248
ASN1_STRING_length (as ));
2242
2249
break ;
2243
2250
case GEN_DNS :
2244
2251
BIO_puts (bio , "DNS:" );
2245
2252
as = name -> d .dNSName ;
2246
- BIO_write (bio , ASN1_STRING_data (as ),
2253
+ BIO_write (bio , ASN1_STRING_get0_data (as ),
2247
2254
ASN1_STRING_length (as ));
2248
2255
break ;
2249
2256
case GEN_URI :
2250
2257
BIO_puts (bio , "URI:" );
2251
2258
as = name -> d .uniformResourceIdentifier ;
2252
- BIO_write (bio , ASN1_STRING_data (as ),
2259
+ BIO_write (bio , ASN1_STRING_get0_data (as ),
2253
2260
ASN1_STRING_length (as ));
2254
2261
break ;
2255
2262
default :
0 commit comments