@@ -653,6 +653,11 @@ int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
653
653
return 1 ;
654
654
}
655
655
656
+ static const unsigned char * ASN1_STRING_get0_data (const ASN1_STRING * asn1 )
657
+ {
658
+ return M_ASN1_STRING_data (asn1 );
659
+ }
660
+
656
661
#if OPENSSL_VERSION_NUMBER < 0x10002000L || defined (LIBRESSL_VERSION_NUMBER )
657
662
658
663
static int X509_get_signature_nid (const X509 * x )
@@ -811,9 +816,9 @@ static void add_assoc_name_entry(zval * val, char * key, X509_NAME * name, int s
811
816
}
812
817
813
818
for (i = 0 ; i < X509_NAME_entry_count (name ); i ++ ) {
814
- unsigned char * to_add = NULL ;
819
+ const unsigned char * to_add = NULL ;
815
820
int to_add_len = 0 ;
816
- int needs_free = 0 ;
821
+ unsigned char * to_add_buf = NULL ;
817
822
818
823
ne = X509_NAME_get_entry (name , i );
819
824
obj = X509_NAME_ENTRY_get_object (ne );
@@ -828,32 +833,34 @@ static void add_assoc_name_entry(zval * val, char * key, X509_NAME * name, int s
828
833
str = X509_NAME_ENTRY_get_data (ne );
829
834
if (ASN1_STRING_type (str ) != V_ASN1_UTF8STRING ) {
830
835
/* ASN1_STRING_to_UTF8(3): The converted data is copied into a newly allocated buffer */
831
- to_add_len = ASN1_STRING_to_UTF8 (& to_add , str );
832
- needs_free = 1 ;
836
+ to_add_len = ASN1_STRING_to_UTF8 (& to_add_buf , str );
837
+ to_add = to_add_buf ;
833
838
} else {
834
- /* ASN1_STRING_data (3): Since this is an internal pointer it should not be freed or modified in any way */
835
- to_add = ASN1_STRING_data (str );
839
+ /* ASN1_STRING_get0_data (3): Since this is an internal pointer it should not be freed or modified in any way */
840
+ to_add = ASN1_STRING_get0_data (str );
836
841
to_add_len = ASN1_STRING_length (str );
837
842
}
838
843
839
844
if (to_add_len != -1 ) {
840
845
if ((data = zend_hash_str_find (Z_ARRVAL (subitem ), sname , strlen (sname ))) != NULL ) {
841
846
if (Z_TYPE_P (data ) == IS_ARRAY ) {
842
- add_next_index_stringl (data , (char * )to_add , to_add_len );
847
+ add_next_index_stringl (data , (const char * )to_add , to_add_len );
843
848
} else if (Z_TYPE_P (data ) == IS_STRING ) {
844
849
array_init (& tmp );
845
850
add_next_index_str (& tmp , zend_string_copy (Z_STR_P (data )));
846
- add_next_index_stringl (& tmp , (char * )to_add , to_add_len );
851
+ add_next_index_stringl (& tmp , (const char * )to_add , to_add_len );
847
852
zend_hash_str_update (Z_ARRVAL (subitem ), sname , strlen (sname ), & tmp );
848
853
}
849
854
} else {
855
+ /* it might be better to expand it and pass zval from ZVAL_STRING
856
+ * to zend_symtable_str_update so we do not silently drop const
857
+ * but we need a test to cover this part first */
850
858
add_assoc_stringl (& subitem , sname , (char * )to_add , to_add_len );
851
859
}
852
860
}
853
861
854
- if (needs_free ) {
855
- /* ASN1_STRING_to_UTF8(3): The buffer out should be freed using free(3) */
856
- OPENSSL_free (to_add );
862
+ if (to_add_buf != NULL ) {
863
+ OPENSSL_free (to_add_buf );
857
864
}
858
865
}
859
866
@@ -892,7 +899,7 @@ static time_t asn1_time_to_time_t(ASN1_UTCTIME * timestr) /* {{{ */
892
899
893
900
timestr_len = (size_t )ASN1_STRING_length (timestr );
894
901
895
- if (timestr_len != strlen ((const char * ) ASN1_STRING_data (timestr ))) {
902
+ if (timestr_len != strlen ((const char * ) ASN1_STRING_get0_data (timestr ))) {
896
903
php_error_docref (NULL , E_WARNING , "illegal length in timestamp" );
897
904
return (time_t )- 1 ;
898
905
}
@@ -907,7 +914,7 @@ static time_t asn1_time_to_time_t(ASN1_UTCTIME * timestr) /* {{{ */
907
914
return (time_t )- 1 ;
908
915
}
909
916
910
- strbuf = estrdup ((char * )ASN1_STRING_data (timestr ));
917
+ strbuf = estrdup ((const char * )ASN1_STRING_get0_data (timestr ));
911
918
912
919
memset (& thetime , 0 , sizeof (thetime ));
913
920
@@ -1945,7 +1952,7 @@ PHP_FUNCTION(openssl_spki_export_challenge)
1945
1952
goto cleanup ;
1946
1953
}
1947
1954
1948
- RETVAL_STRING ((char * ) ASN1_STRING_data (spki -> spkac -> challenge ));
1955
+ RETVAL_STRING ((const char * )ASN1_STRING_get0_data (spki -> spkac -> challenge ));
1949
1956
goto cleanup ;
1950
1957
1951
1958
cleanup :
@@ -2126,19 +2133,19 @@ static int openssl_x509v3_subjectAltName(BIO *bio, X509_EXTENSION *extension)
2126
2133
case GEN_EMAIL :
2127
2134
BIO_puts (bio , "email:" );
2128
2135
as = name -> d .rfc822Name ;
2129
- BIO_write (bio , ASN1_STRING_data (as ),
2136
+ BIO_write (bio , ASN1_STRING_get0_data (as ),
2130
2137
ASN1_STRING_length (as ));
2131
2138
break ;
2132
2139
case GEN_DNS :
2133
2140
BIO_puts (bio , "DNS:" );
2134
2141
as = name -> d .dNSName ;
2135
- BIO_write (bio , ASN1_STRING_data (as ),
2142
+ BIO_write (bio , ASN1_STRING_get0_data (as ),
2136
2143
ASN1_STRING_length (as ));
2137
2144
break ;
2138
2145
case GEN_URI :
2139
2146
BIO_puts (bio , "URI:" );
2140
2147
as = name -> d .uniformResourceIdentifier ;
2141
- BIO_write (bio , ASN1_STRING_data (as ),
2148
+ BIO_write (bio , ASN1_STRING_get0_data (as ),
2142
2149
ASN1_STRING_length (as ));
2143
2150
break ;
2144
2151
default :
0 commit comments