summaryrefslogtreecommitdiff
path: root/src/sp_crypt.c
diff options
context:
space:
mode:
authorjvoisin2018-12-15 14:30:32 +0000
committerGitHub2018-12-15 14:30:32 +0000
commit0988660cc2f5d194468f81fab48160c0f9b253dc (patch)
tree3fd9cbae8641abc34e56d476f2eaa39b679c9e46 /src/sp_crypt.c
parent244f3da227d07b94602a3b517fcb8aefeb9f459d (diff)
Improve simulation mode for session cookies (#259)
Since decrypt_zval doesn't provide a way to tell apart failed and successful decryption when used in simulation mode, we'll have to restore the original value if something goes wrong, because crypto_secretbox_open might modify the value.
Diffstat (limited to 'src/sp_crypt.c')
-rw-r--r--src/sp_crypt.c8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/sp_crypt.c b/src/sp_crypt.c
index b6eaa59..525da56 100644
--- a/src/sp_crypt.c
+++ b/src/sp_crypt.c
@@ -91,6 +91,8 @@ int decrypt_zval(zval *pDest, bool simulation, zend_hash_key *hash_key) {
91 generate_key(key); 91 generate_key(key);
92 92
93 decrypted = ecalloc(ZSTR_LEN(debase64) + crypto_secretbox_ZEROBYTES, 1); 93 decrypted = ecalloc(ZSTR_LEN(debase64) + crypto_secretbox_ZEROBYTES, 1);
94 char *backup = ecalloc(ZSTR_LEN(debase64), 1);
95 memcpy(backup, ZSTR_VAL(debase64), ZSTR_LEN(debase64));
94 96
95 ret = crypto_secretbox_open( 97 ret = crypto_secretbox_open(
96 decrypted, 98 decrypted,
@@ -105,19 +107,25 @@ int decrypt_zval(zval *pDest, bool simulation, zend_hash_key *hash_key) {
105 "Something went wrong with the decryption of %s. Using the cookie " 107 "Something went wrong with the decryption of %s. Using the cookie "
106 "'as it' instead of decrypting it", 108 "'as it' instead of decrypting it",
107 hash_key ? ZSTR_VAL(hash_key->key) : "the session"); 109 hash_key ? ZSTR_VAL(hash_key->key) : "the session");
110 memcpy(ZSTR_VAL(debase64), backup, ZSTR_LEN(debase64));
111 efree(backup);
108 return ZEND_HASH_APPLY_KEEP; 112 return ZEND_HASH_APPLY_KEEP;
109 } else { 113 } else {
110 sp_log_msg("cookie_encryption", SP_LOG_WARN, 114 sp_log_msg("cookie_encryption", SP_LOG_WARN,
111 "Something went wrong with the decryption of %s", 115 "Something went wrong with the decryption of %s",
112 hash_key ? ZSTR_VAL(hash_key->key) : "the session"); 116 hash_key ? ZSTR_VAL(hash_key->key) : "the session");
117 efree(backup);
113 return ZEND_HASH_APPLY_REMOVE; 118 return ZEND_HASH_APPLY_REMOVE;
114 } 119 }
115 } 120 }
121 efree(backup);
116 122
117 ZVAL_STRINGL(pDest, (char *)(decrypted + crypto_secretbox_ZEROBYTES), 123 ZVAL_STRINGL(pDest, (char *)(decrypted + crypto_secretbox_ZEROBYTES),
118 ZSTR_LEN(debase64) - crypto_secretbox_NONCEBYTES - 1 - 124 ZSTR_LEN(debase64) - crypto_secretbox_NONCEBYTES - 1 -
119 crypto_secretbox_ZEROBYTES); 125 crypto_secretbox_ZEROBYTES);
120 126
127 zend_string_release(decrypted);
128
121 return ZEND_HASH_APPLY_KEEP; 129 return ZEND_HASH_APPLY_KEEP;
122} 130}
123 131