summaryrefslogtreecommitdiff
path: root/ext/openssl/openssl.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/openssl/openssl.c')
-rw-r--r--ext/openssl/openssl.c113
1 files changed, 54 insertions, 59 deletions
diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c
index e97cd8fda6..115801a380 100644
--- a/ext/openssl/openssl.c
+++ b/ext/openssl/openssl.c
@@ -2101,7 +2101,7 @@ PHP_FUNCTION(openssl_x509_export)
zend_bool notext = 1;
BIO * bio_out;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz/|b", &zcert, &zout, &notext) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz|b", &zcert, &zout, &notext) == FAILURE) {
return;
}
RETVAL_FALSE;
@@ -2123,9 +2123,8 @@ PHP_FUNCTION(openssl_x509_export)
if (PEM_write_bio_X509(bio_out, cert)) {
BUF_MEM *bio_buf;
- zval_ptr_dtor(zout);
BIO_get_mem_ptr(bio_out, &bio_buf);
- ZVAL_STRINGL(zout, bio_buf->data, bio_buf->length);
+ ZEND_TRY_ASSIGN_STRINGL(zout, bio_buf->data, bio_buf->length);
RETVAL_TRUE;
} else {
@@ -2925,7 +2924,7 @@ PHP_FUNCTION(openssl_pkcs12_export)
zval * item;
STACK_OF(X509) *ca = NULL;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz/zs|a", &zcert, &zout, &zpkey, &pass, &pass_len, &args) == FAILURE)
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "zzzs|a", &zcert, &zout, &zpkey, &pass, &pass_len, &args) == FAILURE)
return;
RETVAL_FALSE;
@@ -2965,9 +2964,8 @@ PHP_FUNCTION(openssl_pkcs12_export)
if (i2d_PKCS12_bio(bio_out, p12)) {
BUF_MEM *bio_buf;
- zval_ptr_dtor(zout);
BIO_get_mem_ptr(bio_out, &bio_buf);
- ZVAL_STRINGL(zout, bio_buf->data, bio_buf->length);
+ ZEND_TRY_ASSIGN_STRINGL(zout, bio_buf->data, bio_buf->length);
RETVAL_TRUE;
} else {
@@ -3006,7 +3004,7 @@ PHP_FUNCTION(openssl_pkcs12_read)
BIO * bio_in = NULL;
int i;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz/s", &zp12, &zp12_len, &zout, &pass, &pass_len) == FAILURE)
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "szs", &zp12, &zp12_len, &zout, &pass, &pass_len) == FAILURE)
return;
RETVAL_FALSE;
@@ -3024,8 +3022,10 @@ PHP_FUNCTION(openssl_pkcs12_read)
BIO * bio_out;
int cert_num;
- zval_ptr_dtor(zout);
- array_init(zout);
+ zout = zend_try_array_init(zout);
+ if (!zout) {
+ goto cleanup;
+ }
if (cert) {
bio_out = BIO_new(BIO_s_mem());
@@ -3380,7 +3380,7 @@ PHP_FUNCTION(openssl_csr_export)
BIO * bio_out;
zend_resource *csr_resource;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz/|b", &zcsr, &zout, &notext) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz|b", &zcsr, &zout, &notext) == FAILURE) {
return;
}
@@ -3403,8 +3403,7 @@ PHP_FUNCTION(openssl_csr_export)
BUF_MEM *bio_buf;
BIO_get_mem_ptr(bio_out, &bio_buf);
- zval_ptr_dtor(zout);
- ZVAL_STRINGL(zout, bio_buf->data, bio_buf->length);
+ ZEND_TRY_ASSIGN_STRINGL(zout, bio_buf->data, bio_buf->length);
RETVAL_TRUE;
} else {
@@ -3573,7 +3572,7 @@ PHP_FUNCTION(openssl_csr_new)
int we_made_the_key = 1;
zend_resource *key_resource;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "az/|a!a!", &dn, &out_pkey, &args, &attribs) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "az|a!a!", &dn, &out_pkey, &args, &attribs) == FAILURE) {
return;
}
RETVAL_FALSE;
@@ -3581,9 +3580,12 @@ PHP_FUNCTION(openssl_csr_new)
PHP_SSL_REQ_INIT(&req);
if (PHP_SSL_REQ_PARSE(&req, args) == SUCCESS) {
+ zval *out_pkey_val = out_pkey;
+ ZVAL_DEREF(out_pkey_val);
+
/* Generate or use a private key */
- if (Z_TYPE_P(out_pkey) != IS_NULL) {
- req.priv_key = php_openssl_evp_from_zval(out_pkey, 0, NULL, 0, 0, &key_resource);
+ if (Z_TYPE_P(out_pkey_val) != IS_NULL) {
+ req.priv_key = php_openssl_evp_from_zval(out_pkey_val, 0, NULL, 0, 0, &key_resource);
if (req.priv_key != NULL) {
we_made_the_key = 0;
}
@@ -3621,8 +3623,7 @@ PHP_FUNCTION(openssl_csr_new)
if (we_made_the_key) {
/* and a resource for the private key */
- zval_ptr_dtor(out_pkey);
- ZVAL_RES(out_pkey, zend_register_resource(req.priv_key, le_key));
+ ZEND_TRY_ASSIGN_RES(out_pkey, zend_register_resource(req.priv_key, le_key));
req.priv_key = NULL; /* make sure the cleanup code doesn't zap it! */
} else if (key_resource != NULL) {
req.priv_key = NULL; /* make sure the cleanup code doesn't zap it! */
@@ -4633,7 +4634,7 @@ PHP_FUNCTION(openssl_pkey_export)
BIO * bio_out = NULL;
const EVP_CIPHER * cipher;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz/|s!a!", &zpkey, &out, &passphrase, &passphrase_len, &args) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz|s!a!", &zpkey, &out, &passphrase, &passphrase_len, &args) == FAILURE) {
return;
}
RETVAL_FALSE;
@@ -4685,8 +4686,7 @@ PHP_FUNCTION(openssl_pkey_export)
RETVAL_TRUE;
bio_mem_len = BIO_get_mem_data(bio_out, &bio_mem_ptr);
- zval_ptr_dtor(out);
- ZVAL_STRINGL(out, bio_mem_ptr, bio_mem_len);
+ ZEND_TRY_ASSIGN_STRINGL(out, bio_mem_ptr, bio_mem_len);
} else {
php_openssl_store_errors();
}
@@ -5389,7 +5389,7 @@ PHP_FUNCTION(openssl_pkcs7_read)
PKCS7 * p7 = NULL;
int i;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz/", &p7b, &p7b_len,
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz", &p7b, &p7b_len,
&zout) == FAILURE) {
return;
}
@@ -5431,8 +5431,10 @@ PHP_FUNCTION(openssl_pkcs7_read)
break;
}
- zval_ptr_dtor(zout);
- array_init(zout);
+ zout = zend_try_array_init(zout);
+ if (!zout) {
+ goto clean_exit;
+ }
if (certs != NULL) {
for (i = 0; i < sk_X509_num(certs); i++) {
@@ -5686,7 +5688,7 @@ PHP_FUNCTION(openssl_private_encrypt)
size_t data_len;
zend_long padding = RSA_PKCS1_PADDING;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz/z|l", &data, &data_len, &crypted, &key, &padding) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "szz|l", &data, &data_len, &crypted, &key, &padding) == FAILURE) {
return;
}
RETVAL_FALSE;
@@ -5717,9 +5719,8 @@ PHP_FUNCTION(openssl_private_encrypt)
}
if (successful) {
- zval_ptr_dtor(crypted);
ZSTR_VAL(cryptedbuf)[cryptedlen] = '\0';
- ZVAL_NEW_STR(crypted, cryptedbuf);
+ ZEND_TRY_ASSIGN_NEW_STR(crypted, cryptedbuf);
cryptedbuf = NULL;
RETVAL_TRUE;
} else {
@@ -5749,7 +5750,7 @@ PHP_FUNCTION(openssl_private_decrypt)
char * data;
size_t data_len;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz/z|l", &data, &data_len, &crypted, &key, &padding) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "szz|l", &data, &data_len, &crypted, &key, &padding) == FAILURE) {
return;
}
RETVAL_FALSE;
@@ -5786,9 +5787,8 @@ PHP_FUNCTION(openssl_private_decrypt)
efree(crypttemp);
if (successful) {
- zval_ptr_dtor(crypted);
ZSTR_VAL(cryptedbuf)[cryptedlen] = '\0';
- ZVAL_NEW_STR(crypted, cryptedbuf);
+ ZEND_TRY_ASSIGN_NEW_STR(crypted, cryptedbuf);
cryptedbuf = NULL;
RETVAL_TRUE;
} else {
@@ -5818,7 +5818,7 @@ PHP_FUNCTION(openssl_public_encrypt)
char * data;
size_t data_len;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz/z|l", &data, &data_len, &crypted, &key, &padding) == FAILURE)
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "szz|l", &data, &data_len, &crypted, &key, &padding) == FAILURE)
return;
RETVAL_FALSE;
@@ -5848,9 +5848,8 @@ PHP_FUNCTION(openssl_public_encrypt)
}
if (successful) {
- zval_ptr_dtor(crypted);
ZSTR_VAL(cryptedbuf)[cryptedlen] = '\0';
- ZVAL_NEW_STR(crypted, cryptedbuf);
+ ZEND_TRY_ASSIGN_NEW_STR(crypted, cryptedbuf);
cryptedbuf = NULL;
RETVAL_TRUE;
} else {
@@ -5880,7 +5879,7 @@ PHP_FUNCTION(openssl_public_decrypt)
char * data;
size_t data_len;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz/z|l", &data, &data_len, &crypted, &key, &padding) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "szz|l", &data, &data_len, &crypted, &key, &padding) == FAILURE) {
return;
}
RETVAL_FALSE;
@@ -5919,9 +5918,8 @@ PHP_FUNCTION(openssl_public_decrypt)
efree(crypttemp);
if (successful) {
- zval_ptr_dtor(crypted);
ZSTR_VAL(cryptedbuf)[cryptedlen] = '\0';
- ZVAL_NEW_STR(crypted, cryptedbuf);
+ ZEND_TRY_ASSIGN_NEW_STR(crypted, cryptedbuf);
cryptedbuf = NULL;
RETVAL_TRUE;
} else {
@@ -5982,7 +5980,7 @@ PHP_FUNCTION(openssl_sign)
zend_long signature_algo = OPENSSL_ALGO_SHA1;
const EVP_MD *mdtype;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz/z|z", &data, &data_len, &signature, &key, &method) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "szz|z", &data, &data_len, &signature, &key, &method) == FAILURE) {
return;
}
pkey = php_openssl_evp_from_zval(key, 0, "", 0, 0, &keyresource);
@@ -6015,10 +6013,9 @@ PHP_FUNCTION(openssl_sign)
EVP_SignInit(md_ctx, mdtype) &&
EVP_SignUpdate(md_ctx, data, data_len) &&
EVP_SignFinal(md_ctx, (unsigned char*)ZSTR_VAL(sigbuf), &siglen, pkey)) {
- zval_ptr_dtor(signature);
ZSTR_VAL(sigbuf)[siglen] = '\0';
ZSTR_LEN(sigbuf) = siglen;
- ZVAL_NEW_STR(signature, sigbuf);
+ ZEND_TRY_ASSIGN_NEW_STR(signature, sigbuf);
RETVAL_TRUE;
} else {
php_openssl_store_errors();
@@ -6110,7 +6107,7 @@ PHP_FUNCTION(openssl_seal)
const EVP_CIPHER *cipher;
EVP_CIPHER_CTX *ctx;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz/z/a|sz/", &data, &data_len,
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "szza|sz", &data, &data_len,
&sealdata, &ekeys, &pubkeys, &method, &method_len, &iv) == FAILURE) {
return;
}
@@ -6184,12 +6181,15 @@ PHP_FUNCTION(openssl_seal)
}
if (len1 + len2 > 0) {
- zval_ptr_dtor(sealdata);
- ZVAL_NEW_STR(sealdata, zend_string_init((char*)buf, len1 + len2, 0));
+ ZEND_TRY_ASSIGN_NEW_STR(sealdata, zend_string_init((char*)buf, len1 + len2, 0));
efree(buf);
- zval_ptr_dtor(ekeys);
- array_init(ekeys);
+ ekeys = zend_try_array_init(ekeys);
+ if (!ekeys) {
+ EVP_CIPHER_CTX_free(ctx);
+ goto clean_exit;
+ }
+
for (i=0; i<nkeys; i++) {
eks[i][eksl[i]] = '\0';
add_next_index_stringl(ekeys, (const char*)eks[i], eksl[i]);
@@ -6198,9 +6198,8 @@ PHP_FUNCTION(openssl_seal)
}
if (iv) {
- zval_ptr_dtor(iv);
iv_buf[iv_len] = '\0';
- ZVAL_NEW_STR(iv, zend_string_init((char*)iv_buf, iv_len, 0));
+ ZEND_TRY_ASSIGN_NEW_STR(iv, zend_string_init((char*)iv_buf, iv_len, 0));
}
} else {
efree(buf);
@@ -6242,7 +6241,7 @@ PHP_FUNCTION(openssl_open)
size_t method_len = 0, iv_len = 0;
const EVP_CIPHER *cipher;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz/sz|ss", &data, &data_len, &opendata,
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "szsz|ss", &data, &data_len, &opendata,
&ekey, &ekey_len, &privkey, &method, &method_len, &iv, &iv_len) == FAILURE) {
return;
}
@@ -6288,9 +6287,8 @@ PHP_FUNCTION(openssl_open)
if (ctx != NULL && EVP_OpenInit(ctx, cipher, (unsigned char *)ekey, (int)ekey_len, iv_buf, pkey) &&
EVP_OpenUpdate(ctx, buf, &len1, (unsigned char *)data, (int)data_len) &&
EVP_OpenFinal(ctx, buf + len1, &len2) && (len1 + len2 > 0)) {
- zval_ptr_dtor(opendata);
buf[len1 + len2] = '\0';
- ZVAL_NEW_STR(opendata, zend_string_init((char*)buf, len1 + len2, 0));
+ ZEND_TRY_ASSIGN_NEW_STR(opendata, zend_string_init((char*)buf, len1 + len2, 0));
RETVAL_TRUE;
} else {
php_openssl_store_errors();
@@ -6642,7 +6640,7 @@ PHP_FUNCTION(openssl_encrypt)
zend_string *outbuf;
zend_bool free_iv = 0, free_password = 0;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss|lsz/sl", &data, &data_len, &method, &method_len,
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss|lszsl", &data, &data_len, &method, &method_len,
&password, &password_len, &options, &iv, &iv_len, &tag, &aad, &aad_len, &tag_len) == FAILURE) {
return;
}
@@ -6689,10 +6687,9 @@ PHP_FUNCTION(openssl_encrypt)
zend_string *tag_str = zend_string_alloc(tag_len, 0);
if (EVP_CIPHER_CTX_ctrl(cipher_ctx, mode.aead_get_tag_flag, tag_len, ZSTR_VAL(tag_str)) == 1) {
- zval_ptr_dtor(tag);
ZSTR_VAL(tag_str)[tag_len] = '\0';
ZSTR_LEN(tag_str) = tag_len;
- ZVAL_NEW_STR(tag, tag_str);
+ ZEND_TRY_ASSIGN_NEW_STR(tag, tag_str);
} else {
php_error_docref(NULL, E_WARNING, "Retrieving verification tag failed");
zend_string_release_ex(tag_str, 0);
@@ -6700,8 +6697,7 @@ PHP_FUNCTION(openssl_encrypt)
RETVAL_FALSE;
}
} else if (tag) {
- zval_ptr_dtor(tag);
- ZVAL_NULL(tag);
+ ZEND_TRY_ASSIGN_NULL(tag);
php_error_docref(NULL, E_WARNING,
"The authenticated tag cannot be provided for cipher that doesn not support AEAD");
} else if (mode.is_aead) {
@@ -6848,13 +6844,12 @@ PHP_FUNCTION(openssl_random_pseudo_bytes)
zend_string *buffer = NULL;
zval *zstrong_result_returned = NULL;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|z/", &buffer_length, &zstrong_result_returned) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|z", &buffer_length, &zstrong_result_returned) == FAILURE) {
return;
}
if (zstrong_result_returned) {
- zval_ptr_dtor(zstrong_result_returned);
- ZVAL_FALSE(zstrong_result_returned);
+ ZEND_TRY_ASSIGN_FALSE(zstrong_result_returned);
}
if (buffer_length <= 0
@@ -6872,7 +6867,7 @@ PHP_FUNCTION(openssl_random_pseudo_bytes)
if (php_win32_get_random_bytes((unsigned char*)buffer->val, (size_t) buffer_length) == FAILURE){
zend_string_release_ex(buffer, 0);
if (zstrong_result_returned) {
- ZVAL_FALSE(zstrong_result_returned);
+ ZEND_TRY_ASSIGN_FALSE(zstrong_result_returned);
}
zend_throw_exception(zend_ce_exception, "Error reading from source device", 0);
return;
@@ -6885,7 +6880,7 @@ PHP_FUNCTION(openssl_random_pseudo_bytes)
if (RAND_bytes((unsigned char*)ZSTR_VAL(buffer), (int)buffer_length) <= 0) {
zend_string_release_ex(buffer, 0);
if (zstrong_result_returned) {
- ZVAL_FALSE(zstrong_result_returned);
+ ZEND_TRY_ASSIGN_FALSE(zstrong_result_returned);
}
zend_throw_exception(zend_ce_exception, "Error reading from source device", 0);
return;
@@ -6898,7 +6893,7 @@ PHP_FUNCTION(openssl_random_pseudo_bytes)
RETVAL_NEW_STR(buffer);
if (zstrong_result_returned) {
- ZVAL_BOOL(zstrong_result_returned, 1);
+ ZEND_TRY_ASSIGN_TRUE(zstrong_result_returned);
}
}
/* }}} */