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