com php-src: Merge branch 'openssl_error_store' into openssl_aead_with_error_store: ext/openssl/opens sl.c
| From: | Jakub Zelenka | Date: | Mon, 29 Feb 2016 19:34:39 +0000 |
| Subject: | com php-src: Merge branch 'openssl_error_store' into openssl_aead_with_error_store: ext/openssl/opens sl.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-92471@lists.php.net to get a copy of this message | ||
Commit: 7a4cc73641bb3eb878f7184bcbd026ee663cf2a9
Author: Jakub Zelenka <bukka@php.net> Mon, 29 Feb 2016 19:34:39 +0000
Parents: 70141093a731049ee9220e9e965f61ded56ed4d5 80015ba741fc857074050086db6c7b2a4716d6d5
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=7a4cc73641bb3eb878f7184bcbd026ee663cf2a9
Log:
Merge branch 'openssl_error_store' into openssl_aead_with_error_store
Changed paths:
MM ext/openssl/openssl.c
Diff:
diff --cc ext/openssl/openssl.c
index abe2284,3298efc..49a5f53
--- a/ext/openssl/openssl.c
+++ b/ext/openssl/openssl.c
@@@ -5426,21 -5561,42 +5736,22 @@@ PHP_FUNCTION(openssl_encrypt
RETURN_FALSE;
}
- PHP_OPENSSL_CHECK_SIZE_T_TO_INT(data_len, data);
-
- keylen = EVP_CIPHER_key_length(cipher_type);
- if (keylen > password_len) {
- key = emalloc(keylen);
- memset(key, 0, keylen);
- memcpy(key, password, password_len);
- } else {
- key = (unsigned char*)password;
+ cipher_ctx = EVP_CIPHER_CTX_new();
+ if (!cipher_ctx) {
+ php_error_docref(NULL, E_WARNING, "Failed to create cipher context");
+ RETURN_FALSE;
}
- max_iv_len = EVP_CIPHER_iv_length(cipher_type);
- if (iv_len == 0 && max_iv_len > 0) {
- php_error_docref(NULL, E_WARNING, "Using an empty Initialization Vector (iv) is potentially
insecure and not recommended");
- }
- free_iv = php_openssl_validate_iv(&iv, &iv_len, max_iv_len);
+ php_openssl_load_cipher_mode(&mode, cipher_type);
- outlen = (int)data_len + EVP_CIPHER_block_size(cipher_type);
- outbuf = zend_string_alloc(outlen, 0);
+
- EVP_EncryptInit(&cipher_ctx, cipher_type, NULL, NULL);
- if (password_len > keylen) {
- PHP_OPENSSL_CHECK_SIZE_T_TO_INT(password_len, password);
- if (!EVP_CIPHER_CTX_set_key_length(&cipher_ctx, (int)password_len)) {
- php_openssl_store_errors();
- }
- }
- EVP_EncryptInit_ex(&cipher_ctx, NULL, NULL, key, (unsigned char *)iv);
- if (options & OPENSSL_ZERO_PADDING) {
- EVP_CIPHER_CTX_set_padding(&cipher_ctx, 0);
- }
- if (data_len > 0) {
- EVP_EncryptUpdate(&cipher_ctx, (unsigned char*)ZSTR_VAL(outbuf), &i, (unsigned char
*)data, (int)data_len);
- }
- outlen = i;
- if (EVP_EncryptFinal(&cipher_ctx, (unsigned char *)ZSTR_VAL(outbuf) + i, &i)) {
+ if (php_openssl_cipher_init(cipher_type, cipher_ctx, &mode,
+ &password, &password_len, &free_password,
+ &iv, &iv_len, &free_iv, NULL, tag_len, options, 1) == FAILURE ||
+ php_openssl_cipher_update(cipher_type, cipher_ctx, &mode, &outbuf, &outlen,
+ data, data_len, aad, aad_len, 1) == FAILURE) {
+ RETVAL_FALSE;
+ } else if (EVP_EncryptFinal(cipher_ctx, (unsigned char *)ZSTR_VAL(outbuf) + outlen, &i)) {
outlen += i;
if (options & OPENSSL_RAW_DATA) {
ZSTR_VAL(outbuf)[outlen] = '\0';
@@@ -5453,25 -5609,8 +5764,26 @@@
zend_string_release(outbuf);
RETVAL_STR(base64_str);
}
+ if (mode.is_aead && tag) {
+ 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_dtor(tag);
+ ZSTR_VAL(tag_str)[tag_len] = '\0';
+ ZSTR_LEN(tag_str) = tag_len;
+ ZVAL_NEW_STR(tag, tag_str);
+ } else {
+ zend_string_release(tag_str);
+ php_error_docref(NULL, E_WARNING, "Retrieving verification tag failed");
+ }
+ } else if (tag) {
+ zval_dtor(tag);
+ ZVAL_NULL(tag);
+ php_error_docref(NULL, E_WARNING,
+ "The authenticated tag cannot be provided for cipher that doesn not support AEAD");
+ }
} else {
+ php_openssl_store_errors();
zend_string_release(outbuf);
RETVAL_FALSE;
}