Skip to content

ext/openssl: various arrays optimisations. #18377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Zend/zend_API.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,16 @@ ZEND_API const char *zend_get_type_by_const(int type);

#define array_init(arg) ZVAL_ARR((arg), zend_new_array(0))
#define array_init_size(arg, size) ZVAL_ARR((arg), zend_new_array(size))
#define array_init_packed(arg) \
do { \
array_init(arg); \
zend_hash_real_init_packed(Z_ARRVAL_P(arg)); \
} while (0)
#define array_init_packed_size(arg, size) \
do { \
array_init_size(arg, size); \
zend_hash_real_init_packed(Z_ARRVAL_P(arg)); \
} while (0)
ZEND_API void object_init(zval *arg);
ZEND_API zend_result object_init_ex(zval *arg, zend_class_entry *ce);
ZEND_API zend_result object_init_with_constructor(zval *arg, zend_class_entry *class_type, uint32_t param_count, zval *params, HashTable *named_params);
Expand Down
14 changes: 9 additions & 5 deletions ext/openssl/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1063,17 +1063,19 @@ PHP_FUNCTION(openssl_x509_parse)
add_assoc_string(return_value, "signatureTypeSN", (char*)OBJ_nid2sn(sig_nid));
add_assoc_string(return_value, "signatureTypeLN", (char*)OBJ_nid2ln(sig_nid));
add_assoc_long(return_value, "signatureTypeNID", sig_nid);
array_init(&subitem);

int x509_count = X509_PURPOSE_get_count();
array_init_size(&subitem, x509_count);

/* NOTE: the purposes are added as integer keys - the keys match up to the X509_PURPOSE_SSL_XXX defines
in x509v3.h */
for (i = 0; i < X509_PURPOSE_get_count(); i++) {
for (i = 0; i < x509_count; i++) {
int id, purpset;
char * pname;
X509_PURPOSE * purp;
zval subsub;

array_init(&subsub);
array_init_packed_size(&subsub, 3);

purp = X509_PURPOSE_get0(i);
id = X509_PURPOSE_get_id(purp);
Expand Down Expand Up @@ -4138,15 +4140,16 @@ PHP_FUNCTION(openssl_seal)
ZEND_TRY_ASSIGN_REF_NEW_STR(sealdata, zend_string_init((char*)buf, len1 + len2, 0));
efree(buf);

ekeys = zend_try_array_init(ekeys);
ekeys = zend_try_array_init_size(ekeys, nkeys);
if (!ekeys) {
EVP_CIPHER_CTX_free(ctx);
goto clean_exit;
}
zend_hash_real_init_packed(Z_ARRVAL_P(ekeys));

for (i=0; i<nkeys; i++) {
eks[i][eksl[i]] = '\0';
add_next_index_stringl(ekeys, (const char*)eks[i], eksl[i]);
add_index_stringl(ekeys, i, (const char*)eks[i], eksl[i]);
efree(eks[i]);
eks[i] = NULL;
}
Expand Down Expand Up @@ -4292,6 +4295,7 @@ PHP_FUNCTION(openssl_get_curve_names)
}

array_init(return_value);
zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
for (i = 0; i < len; i++) {
sname = OBJ_nid2sn(curves[i].nid);
if (sname != NULL) {
Expand Down
6 changes: 3 additions & 3 deletions ext/openssl/openssl_backend_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ void php_openssl_add_assoc_name_entry(zval * val, char * key, X509_NAME * name,
if (Z_TYPE_P(data) == IS_ARRAY) {
add_next_index_stringl(data, (const char *)to_add, to_add_len);
} else if (Z_TYPE_P(data) == IS_STRING) {
array_init(&tmp);
add_next_index_str(&tmp, zend_string_copy(Z_STR_P(data)));
add_next_index_stringl(&tmp, (const char *)to_add, to_add_len);
array_init_packed_size(&tmp, 2);
add_index_str(&tmp, 0, zend_string_copy(Z_STR_P(data)));
add_index_stringl(&tmp, 1, (const char *)to_add, to_add_len);
zend_hash_str_update(Z_ARRVAL(subitem), sname, strlen(sname), &tmp);
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions ext/openssl/xp_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1752,16 +1752,16 @@ static int php_openssl_capture_peer_certs(php_stream *stream,
chain = SSL_get_peer_cert_chain(sslsock->ssl_handle);

if (chain && sk_X509_num(chain) > 0) {
int i;
array_init(&arr);
int i, num_chains = sk_X509_num(chain);
array_init_packed_size(&arr, num_chains);

for (i = 0; i < sk_X509_num(chain); i++) {
X509 *mycert = X509_dup(sk_X509_value(chain, i));

object_init_ex(&zcert, php_openssl_certificate_ce);
cert_object = Z_OPENSSL_CERTIFICATE_P(&zcert);
cert_object->x509 = mycert;
add_next_index_zval(&arr, &zcert);
add_index_zval(&arr, i, &zcert);
}

} else {
Expand Down
Loading