Skip to content

Commit fc2f4ac

Browse files
committed
Few refactorings
1 parent 4148257 commit fc2f4ac

File tree

2 files changed

+54
-61
lines changed

2 files changed

+54
-61
lines changed

ext/openssl/openssl.c

Lines changed: 46 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -713,15 +713,15 @@ static int X509_get_signature_nid(const X509 *x)
713713
do { \
714714
if (_cond) { \
715715
php_error_docref(NULL, E_WARNING, #_name" is too long"); \
716-
RETURN_FALSE; \
716+
return; \
717717
} \
718718
} while(0)
719719
/* number conversion flags checks */
720720
#define PHP_OPENSSL_CHECK_NUMBER_CONVERSION_NORET(_cond, _name) \
721721
do { \
722722
if (_cond) { \
723723
php_error_docref(NULL, E_WARNING, #_name" is too long"); \
724-
return FAILURE; \
724+
return NULL; \
725725
} \
726726
} while(0)
727727
/* check if size_t can be safely casted to int */
@@ -6640,15 +6640,14 @@ static int php_openssl_cipher_update(const EVP_CIPHER *cipher_type,
66406640
/* }}} */
66416641

66426642

6643-
PHP_OPENSSL_API int php_openssl_encrypt(char *data, size_t data_len, char *method, size_t method_len, char *password, size_t password_len, zend_long options, char *iv, size_t iv_len, zval *tag, zend_long tag_len, char *aad, size_t aad_len, zend_string **outbuf)
6643+
PHP_OPENSSL_API zend_string * php_openssl_encrypt(char *data, size_t data_len, char *method, size_t method_len, char *password, size_t password_len, zend_long options, char *iv, size_t iv_len, zval *tag, zend_long tag_len, char *aad, size_t aad_len)
66446644
{
66456645
const EVP_CIPHER *cipher_type;
66466646
EVP_CIPHER_CTX *cipher_ctx;
66476647
struct php_openssl_cipher_mode mode;
6648-
int i = 0, ret = SUCCESS, outlen;
6648+
int i = 0, outlen;
66496649
zend_bool free_iv = 0, free_password = 0;
6650-
6651-
*outbuf = 0;
6650+
zend_string *outbuf = NULL;
66526651

66536652
PHP_OPENSSL_CHECK_SIZE_T_TO_INT_NORET(data_len, data);
66546653
PHP_OPENSSL_CHECK_SIZE_T_TO_INT_NORET(password_len, password);
@@ -6659,33 +6658,32 @@ PHP_OPENSSL_API int php_openssl_encrypt(char *data, size_t data_len, char *metho
66596658
cipher_type = EVP_get_cipherbyname(method);
66606659
if (!cipher_type) {
66616660
php_error_docref(NULL, E_WARNING, "Unknown cipher algorithm");
6662-
return FAILURE;
6661+
return NULL;
66636662
}
66646663

66656664
cipher_ctx = EVP_CIPHER_CTX_new();
66666665
if (!cipher_ctx) {
66676666
php_error_docref(NULL, E_WARNING, "Failed to create cipher context");
6668-
return FAILURE;
6667+
return NULL;
66696668
}
66706669
php_openssl_load_cipher_mode(&mode, cipher_type);
66716670

66726671
if (php_openssl_cipher_init(cipher_type, cipher_ctx, &mode,
66736672
&password, &password_len, &free_password,
66746673
&iv, &iv_len, &free_iv, NULL, tag_len, options, 1) == FAILURE ||
6675-
php_openssl_cipher_update(cipher_type, cipher_ctx, &mode, outbuf, &outlen,
6674+
php_openssl_cipher_update(cipher_type, cipher_ctx, &mode, &outbuf, &outlen,
66766675
data, data_len, aad, aad_len, 1) == FAILURE) {
6677-
ret = FAILURE;
6678-
} else if (EVP_EncryptFinal(cipher_ctx, (unsigned char *)ZSTR_VAL(*outbuf) + outlen, &i)) {
6676+
} else if (EVP_EncryptFinal(cipher_ctx, (unsigned char *)ZSTR_VAL(outbuf) + outlen, &i)) {
66796677
outlen += i;
66806678
if (options & OPENSSL_RAW_DATA) {
6681-
ZSTR_VAL(*outbuf)[outlen] = '\0';
6682-
ZSTR_LEN(*outbuf) = outlen;
6679+
ZSTR_VAL(outbuf)[outlen] = '\0';
6680+
ZSTR_LEN(outbuf) = outlen;
66836681
} else {
66846682
zend_string *base64_str;
66856683

6686-
base64_str = php_base64_encode((unsigned char*)ZSTR_VAL(*outbuf), outlen);
6687-
zend_string_release_ex(*outbuf, 0);
6688-
*outbuf = base64_str;
6684+
base64_str = php_base64_encode((unsigned char*)ZSTR_VAL(outbuf), outlen);
6685+
zend_string_release_ex(outbuf, 0);
6686+
outbuf = base64_str;
66896687
}
66906688
if (mode.is_aead && tag) {
66916689
zend_string *tag_str = zend_string_alloc(tag_len, 0);
@@ -6697,22 +6695,19 @@ PHP_OPENSSL_API int php_openssl_encrypt(char *data, size_t data_len, char *metho
66976695
} else {
66986696
php_error_docref(NULL, E_WARNING, "Retrieving verification tag failed");
66996697
zend_string_release_ex(tag_str, 0);
6700-
zend_string_release_ex(*outbuf, 0);
6701-
ret = FAILURE;
6698+
zend_string_release_ex(outbuf, 0);
67026699
}
67036700
} else if (tag) {
67046701
ZEND_TRY_ASSIGN_NULL(tag);
67056702
php_error_docref(NULL, E_WARNING,
67066703
"The authenticated tag cannot be provided for cipher that doesn not support AEAD");
67076704
} else if (mode.is_aead) {
67086705
php_error_docref(NULL, E_WARNING, "A tag should be provided when using AEAD mode");
6709-
zend_string_release_ex(*outbuf, 0);
6710-
ret = FAILURE;
6706+
zend_string_release_ex(outbuf, 0);
67116707
}
67126708
} else {
67136709
php_openssl_store_errors();
6714-
zend_string_release_ex(*outbuf, 0);
6715-
ret = FAILURE;
6710+
zend_string_release_ex(outbuf, 0);
67166711
}
67176712

67186713
if (free_password) {
@@ -6723,7 +6718,7 @@ PHP_OPENSSL_API int php_openssl_encrypt(char *data, size_t data_len, char *metho
67236718
}
67246719
EVP_CIPHER_CTX_cleanup(cipher_ctx);
67256720
EVP_CIPHER_CTX_free(cipher_ctx);
6726-
return ret;
6721+
return outbuf;
67276722
}
67286723

67296724
/* {{{ proto string openssl_encrypt(string data, string method, string password [, int options=0 [, string $iv=''[, string &$tag = ''[, string $aad = ''[, int $tag_length = 16]]]]])
@@ -6741,24 +6736,23 @@ PHP_FUNCTION(openssl_encrypt)
67416736
return;
67426737
}
67436738

6744-
if (php_openssl_encrypt(data, data_len, method, method_len, password, password_len, options, iv, iv_len, tag, tag_len, aad, aad_len, &ret) == SUCCESS) {
6739+
if ((ret = php_openssl_encrypt(data, data_len, method, method_len, password, password_len, options, iv, iv_len, tag, tag_len, aad, aad_len))) {
67456740
RETVAL_STR(ret);
67466741
} else {
67476742
RETVAL_FALSE;
67486743
}
67496744
}
67506745
/* }}} */
67516746

6752-
PHP_OPENSSL_API int php_openssl_decrypt(char *data, size_t data_len, char *method, size_t method_len, char *password, size_t password_len, zend_long options, char *iv, size_t iv_len, char *tag, zend_long tag_len, char *aad, size_t aad_len, zend_string **outbuf)
6747+
PHP_OPENSSL_API zend_string * php_openssl_decrypt(char *data, size_t data_len, char *method, size_t method_len, char *password, size_t password_len, zend_long options, char *iv, size_t iv_len, char *tag, zend_long tag_len, char *aad, size_t aad_len)
67536748
{
67546749
const EVP_CIPHER *cipher_type;
67556750
EVP_CIPHER_CTX *cipher_ctx;
67566751
struct php_openssl_cipher_mode mode;
6757-
int i = 0, ret = SUCCESS, outlen;
6752+
int i = 0, outlen;
67586753
zend_string *base64_str = NULL;
67596754
zend_bool free_iv = 0, free_password = 0;
6760-
6761-
*outbuf = 0;
6755+
zend_string *outbuf = NULL;
67626756

67636757
PHP_OPENSSL_CHECK_SIZE_T_TO_INT_NORET(data_len, data);
67646758
PHP_OPENSSL_CHECK_SIZE_T_TO_INT_NORET(password_len, password);
@@ -6769,13 +6763,13 @@ PHP_OPENSSL_API int php_openssl_decrypt(char *data, size_t data_len, char *metho
67696763
cipher_type = EVP_get_cipherbyname(method);
67706764
if (!cipher_type) {
67716765
php_error_docref(NULL, E_WARNING, "Unknown cipher algorithm");
6772-
return FAILURE;
6766+
return NULL;
67736767
}
67746768

67756769
cipher_ctx = EVP_CIPHER_CTX_new();
67766770
if (!cipher_ctx) {
67776771
php_error_docref(NULL, E_WARNING, "Failed to create cipher context");
6778-
return FAILURE;
6772+
return NULL;
67796773
}
67806774

67816775
php_openssl_load_cipher_mode(&mode, cipher_type);
@@ -6785,7 +6779,7 @@ PHP_OPENSSL_API int php_openssl_decrypt(char *data, size_t data_len, char *metho
67856779
if (!base64_str) {
67866780
php_error_docref(NULL, E_WARNING, "Failed to base64 decode the input");
67876781
EVP_CIPHER_CTX_free(cipher_ctx);
6788-
return FAILURE;
6782+
return NULL;
67896783
}
67906784
data_len = ZSTR_LEN(base64_str);
67916785
data = ZSTR_VAL(base64_str);
@@ -6794,18 +6788,16 @@ PHP_OPENSSL_API int php_openssl_decrypt(char *data, size_t data_len, char *metho
67946788
if (php_openssl_cipher_init(cipher_type, cipher_ctx, &mode,
67956789
&password, &password_len, &free_password,
67966790
&iv, &iv_len, &free_iv, tag, tag_len, options, 0) == FAILURE ||
6797-
php_openssl_cipher_update(cipher_type, cipher_ctx, &mode, outbuf, &outlen,
6791+
php_openssl_cipher_update(cipher_type, cipher_ctx, &mode, &outbuf, &outlen,
67986792
data, data_len, aad, aad_len, 0) == FAILURE) {
6799-
ret = FAILURE;
68006793
} else if (mode.is_single_run_aead ||
6801-
EVP_DecryptFinal(cipher_ctx, (unsigned char *)ZSTR_VAL(*outbuf) + outlen, &i)) {
6794+
EVP_DecryptFinal(cipher_ctx, (unsigned char *)ZSTR_VAL(outbuf) + outlen, &i)) {
68026795
outlen += i;
6803-
ZSTR_VAL(*outbuf)[outlen] = '\0';
6804-
ZSTR_LEN(*outbuf) = outlen;
6796+
ZSTR_VAL(outbuf)[outlen] = '\0';
6797+
ZSTR_LEN(outbuf) = outlen;
68056798
} else {
68066799
php_openssl_store_errors();
6807-
zend_string_release_ex(*outbuf, 0);
6808-
ret = FAILURE;
6800+
zend_string_release_ex(outbuf, 0);
68096801
}
68106802

68116803
if (free_password) {
@@ -6819,7 +6811,7 @@ PHP_OPENSSL_API int php_openssl_decrypt(char *data, size_t data_len, char *metho
68196811
}
68206812
EVP_CIPHER_CTX_cleanup(cipher_ctx);
68216813
EVP_CIPHER_CTX_free(cipher_ctx);
6822-
return ret;
6814+
return outbuf;
68236815
}
68246816

68256817
/* {{{ proto string openssl_decrypt(string data, string method, string password [, int options=0 [, string $iv = ''[, string $tag = ''[, string $aad = '']]]])
@@ -6841,27 +6833,25 @@ PHP_FUNCTION(openssl_decrypt)
68416833
RETURN_FALSE;
68426834
}
68436835

6844-
if (php_openssl_decrypt(data, data_len, method, method_len, password, password_len, options, iv, iv_len, tag, tag_len, aad, aad_len, &ret) == SUCCESS) {
6836+
if ((ret = php_openssl_decrypt(data, data_len, method, method_len, password, password_len, options, iv, iv_len, tag, tag_len, aad, aad_len))) {
68456837
RETVAL_STR(ret);
68466838
} else {
68476839
RETVAL_FALSE;
68486840
}
68496841
}
68506842
/* }}} */
68516843

6852-
PHP_OPENSSL_API int php_openssl_cipher_iv_length(char *method, zend_long *ret)
6844+
PHP_OPENSSL_API zend_long php_openssl_cipher_iv_length(char *method)
68536845
{
68546846
const EVP_CIPHER *cipher_type;
6855-
*ret = 0;
68566847

68576848
cipher_type = EVP_get_cipherbyname(method);
68586849
if (!cipher_type) {
68596850
php_error_docref(NULL, E_WARNING, "Unknown cipher algorithm");
68606851
return -1;
68616852
}
68626853

6863-
*ret = EVP_CIPHER_iv_length(cipher_type);
6864-
return 0;
6854+
return EVP_CIPHER_iv_length(cipher_type);
68656855
}
68666856

68676857
/* {{{ proto int openssl_cipher_iv_length(string $method) */
@@ -6880,7 +6870,7 @@ PHP_FUNCTION(openssl_cipher_iv_length)
68806870
RETURN_FALSE;
68816871
}
68826872

6883-
if (php_openssl_cipher_iv_length(method, &ret) == -1) {
6873+
if ((ret = php_openssl_cipher_iv_length(method)) == -1) {
68846874
RETURN_FALSE;
68856875
}
68866876

@@ -6889,39 +6879,40 @@ PHP_FUNCTION(openssl_cipher_iv_length)
68896879
/* }}} */
68906880

68916881

6892-
PHP_OPENSSL_API int php_openssl_random_pseudo_bytes(zend_string **buffer, zend_long buffer_length)
6882+
PHP_OPENSSL_API zend_string * php_openssl_random_pseudo_bytes(zend_long buffer_length)
68936883
{
6884+
zend_string *buffer = NULL;
68946885
if (buffer_length <= 0
68956886
#ifndef PHP_WIN32
68966887
|| ZEND_LONG_INT_OVFL(buffer_length)
68976888
#endif
68986889
) {
68996890
zend_throw_exception(zend_ce_error, "Length must be greater than 0", 0);
6900-
return -1;
6891+
return NULL;
69016892
}
6902-
*buffer = zend_string_alloc(buffer_length, 0);
6893+
buffer = zend_string_alloc(buffer_length, 0);
69036894

69046895
#ifdef PHP_WIN32
69056896
/* random/urandom equivalent on Windows */
6906-
if (php_win32_get_random_bytes((unsigned char*)(*buffer)->val, (size_t) buffer_length) == FAILURE){
6897+
if (php_win32_get_random_bytes((unsigned char*)(buffer)->val, (size_t) buffer_length) == FAILURE){
69076898
zend_string_release_ex(buffer, 0);
69086899
zend_throw_exception(zend_ce_exception, "Error reading from source device", 0);
6909-
return -1;
6900+
return NULL;
69106901
}
69116902
#else
69126903

69136904
PHP_OPENSSL_CHECK_LONG_TO_INT_NORET(buffer_length, length);
69146905
PHP_OPENSSL_RAND_ADD_TIME();
69156906
/* FIXME loop if requested size > INT_MAX */
6916-
if (RAND_bytes((unsigned char*)ZSTR_VAL(*buffer), (int)buffer_length) <= 0) {
6917-
zend_string_release_ex(*buffer, 0);
6907+
if (RAND_bytes((unsigned char*)ZSTR_VAL(buffer), (int)buffer_length) <= 0) {
6908+
zend_string_release_ex(buffer, 0);
69186909
zend_throw_exception(zend_ce_exception, "Error reading from source device", 0);
6919-
return -1;
6910+
return NULL;
69206911
} else {
69216912
php_openssl_store_errors();
69226913
}
69236914
#endif
6924-
return 0;
6915+
return buffer;
69256916
}
69266917

69276918
/* {{{ proto string openssl_random_pseudo_bytes(int length [, &bool returned_strong_result])
@@ -6943,7 +6934,7 @@ PHP_FUNCTION(openssl_random_pseudo_bytes)
69436934
ZVAL_FALSE(zstrong_result_returned);
69446935
}
69456936

6946-
if ((ret = php_openssl_random_pseudo_bytes(&buffer, buffer_length)) == 0) {
6937+
if ((buffer = php_openssl_random_pseudo_bytes(buffer_length))) {
69476938
ZSTR_VAL(buffer)[buffer_length] = 0;
69486939
RETVAL_NEW_STR(buffer);
69496940
}

ext/openssl/php_openssl.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,14 @@ php_stream_transport_factory_func php_openssl_ssl_socket_factory;
9494

9595
void php_openssl_store_errors();
9696

97-
PHP_OPENSSL_API int php_openssl_cipher_iv_length(char *method, zend_long *ret);
98-
PHP_OPENSSL_API int php_openssl_random_pseudo_bytes(zend_string **buffer, zend_long length);
99-
PHP_OPENSSL_API int php_openssl_encrypt(char *data, size_t data_len, char *method, size_t method_len, char *password,
100-
size_t password_len, zend_long options, char *iv, size_t iv_len, zval *tag, zend_long tag_len, char *aad, size_t add_len, zend_string **outbuf);
101-
PHP_OPENSSL_API int php_openssl_decrypt(char *data, size_t data_len, char *method, size_t method_len, char *password,
102-
size_t password_len, zend_long options, char *iv, size_t iv_len, char *tag, zend_long tag_len, char *aad, size_t add_len, zend_string **outbuf);
97+
PHP_OPENSSL_API zend_long php_openssl_cipher_iv_length(char *method);
98+
PHP_OPENSSL_API zend_string * php_openssl_random_pseudo_bytes(zend_long length);
99+
PHP_OPENSSL_API zend_string * php_openssl_encrypt(char *data, size_t data_len,
100+
char *method, size_t method_len, char *password, size_t password_len,
101+
zend_long options, char *iv, size_t iv_len, zval *tag, zend_long tag_len, char *aad, size_t add_len);
102+
PHP_OPENSSL_API zend_string * php_openssl_decrypt(char *data, size_t data_len,
103+
char *method, size_t method_len, char *password, size_t password_len,
104+
zend_long options, char *iv, size_t iv_len, char *tag, zend_long tag_len, char *aad, size_t add_len);
103105

104106
PHP_MINIT_FUNCTION(openssl);
105107
PHP_MSHUTDOWN_FUNCTION(openssl);

0 commit comments

Comments
 (0)