summaryrefslogtreecommitdiff
path: root/src/sp_crypt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sp_crypt.c')
-rw-r--r--src/sp_crypt.c65
1 files changed, 36 insertions, 29 deletions
diff --git a/src/sp_crypt.c b/src/sp_crypt.c
index c57ac0b..c1d9403 100644
--- a/src/sp_crypt.c
+++ b/src/sp_crypt.c
@@ -3,13 +3,10 @@
3void generate_key(unsigned char *key) { 3void generate_key(unsigned char *key) {
4 PHP_SHA256_CTX ctx; 4 PHP_SHA256_CTX ctx;
5 const char *user_agent = getenv("HTTP_USER_AGENT"); 5 const char *user_agent = getenv("HTTP_USER_AGENT");
6 const zend_string *env_var_zend = 6 const zend_string *env_var_zend = SPCFG(cookies_env_var);
7 SNUFFLEUPAGUS_G(config).config_snuffleupagus->cookies_env_var; 7 const zend_string *encryption_key_zend = SPCFG(encryption_key);
8 const zend_string *encryption_key_zend =
9 SNUFFLEUPAGUS_G(config).config_snuffleupagus->encryption_key;
10 const char *env_var = (env_var_zend ? getenv(ZSTR_VAL(env_var_zend)) : NULL); 8 const char *env_var = (env_var_zend ? getenv(ZSTR_VAL(env_var_zend)) : NULL);
11 const char *encryption_key = 9 const char *encryption_key = (encryption_key_zend ? ZSTR_VAL(encryption_key_zend) : NULL);
12 (encryption_key_zend ? ZSTR_VAL(encryption_key_zend) : NULL);
13 10
14 assert(32 == crypto_secretbox_KEYBYTES); // 32 is the size of a SHA256. 11 assert(32 == crypto_secretbox_KEYBYTES); // 32 is the size of a SHA256.
15 assert(encryption_key); // Encryption key can't be NULL 12 assert(encryption_key); // Encryption key can't be NULL
@@ -40,27 +37,25 @@ void generate_key(unsigned char *key) {
40// This function return 0 upon success , non-zero otherwise 37// This function return 0 upon success , non-zero otherwise
41int decrypt_zval(zval *pDest, bool simulation, zend_hash_key *hash_key) { 38int decrypt_zval(zval *pDest, bool simulation, zend_hash_key *hash_key) {
42 unsigned char key[crypto_secretbox_KEYBYTES] = {0}; 39 unsigned char key[crypto_secretbox_KEYBYTES] = {0};
43 unsigned char *decrypted; 40 unsigned char *decrypted = NULL, *backup = NULL;
44 zend_string *debase64;
45 int ret = 0; 41 int ret = 0;
46 42
47 debase64 = php_base64_decode((unsigned char *)(Z_STRVAL_P(pDest)), 43 zend_string *debase64 = php_base64_decode((unsigned char *)(Z_STRVAL_P(pDest)), Z_STRLEN_P(pDest));
48 Z_STRLEN_P(pDest));
49 44
50 if (ZSTR_LEN(debase64) < crypto_secretbox_NONCEBYTES) { 45 if (ZSTR_LEN(debase64) < crypto_secretbox_NONCEBYTES) {
51 if (true == simulation) { 46 if (true == simulation) {
52 sp_log_simulation( 47 sp_log_simulation(
53 "cookie_encryption", 48 "cookie_encryption",
54 "Buffer underflow tentative detected in cookie encryption handling " 49 "Buffer underflow tentative detected in cookie encryption handling "
55 "for %s. Using the cookie 'as it' instead of decrypting it", 50 "for %s. Using the cookie 'as is' instead of decrypting it",
56 hash_key ? ZSTR_VAL(hash_key->key) : "the session"); 51 hash_key ? ZSTR_VAL(hash_key->key) : "the session");
57 return ZEND_HASH_APPLY_KEEP; 52 ret = ZEND_HASH_APPLY_KEEP; goto out;
58 } else { 53 } else {
59 // LCOV_EXCL_START 54 // LCOV_EXCL_START
60 sp_log_drop( 55 sp_log_drop(
61 "cookie_encryption", 56 "cookie_encryption",
62 "Buffer underflow tentative detected in cookie encryption handling"); 57 "Buffer underflow (tentative) detected in cookie encryption handling");
63 return ZEND_HASH_APPLY_REMOVE; 58 ret = ZEND_HASH_APPLY_REMOVE; goto out;
64 // LCOV_EXCL_STOP 59 // LCOV_EXCL_STOP
65 } 60 }
66 } 61 }
@@ -71,15 +66,15 @@ int decrypt_zval(zval *pDest, bool simulation, zend_hash_key *hash_key) {
71 if (true == simulation) { 66 if (true == simulation) {
72 sp_log_simulation( 67 sp_log_simulation(
73 "cookie_encryption", 68 "cookie_encryption",
74 "Integer overflow tentative detected in cookie encryption handling " 69 "Integer overflow (tentative) detected in cookie encryption handling "
75 "for %s. Using the cookie 'as it' instead of decrypting it.", 70 "for %s. Using the cookie 'as it' instead of decrypting it.",
76 hash_key ? ZSTR_VAL(hash_key->key) : "the session"); 71 hash_key ? ZSTR_VAL(hash_key->key) : "the session");
77 return ZEND_HASH_APPLY_KEEP; 72 ret = ZEND_HASH_APPLY_KEEP; goto out;
78 } else { 73 } else {
79 sp_log_drop( 74 sp_log_drop(
80 "cookie_encryption", 75 "cookie_encryption",
81 "Integer overflow tentative detected in cookie encryption handling."); 76 "Integer overflow (tentative) detected in cookie encryption handling.");
82 return ZEND_HASH_APPLY_REMOVE; 77 ret = ZEND_HASH_APPLY_REMOVE; goto out;
83 } 78 }
84 } 79 }
85 // LCOV_EXCL_STOP 80 // LCOV_EXCL_STOP
@@ -87,7 +82,7 @@ int decrypt_zval(zval *pDest, bool simulation, zend_hash_key *hash_key) {
87 generate_key(key); 82 generate_key(key);
88 83
89 decrypted = ecalloc(ZSTR_LEN(debase64) + crypto_secretbox_ZEROBYTES, 1); 84 decrypted = ecalloc(ZSTR_LEN(debase64) + crypto_secretbox_ZEROBYTES, 1);
90 char *backup = ecalloc(ZSTR_LEN(debase64), 1); 85 backup = ecalloc(ZSTR_LEN(debase64), 1);
91 memcpy(backup, ZSTR_VAL(debase64), ZSTR_LEN(debase64)); 86 memcpy(backup, ZSTR_VAL(debase64), ZSTR_LEN(debase64));
92 87
93 ret = crypto_secretbox_open( 88 ret = crypto_secretbox_open(
@@ -101,28 +96,31 @@ int decrypt_zval(zval *pDest, bool simulation, zend_hash_key *hash_key) {
101 sp_log_simulation( 96 sp_log_simulation(
102 "cookie_encryption", 97 "cookie_encryption",
103 "Something went wrong with the decryption of %s. Using the cookie " 98 "Something went wrong with the decryption of %s. Using the cookie "
104 "'as it' instead of decrypting it", 99 "'as is' instead of decrypting it",
105 hash_key ? ZSTR_VAL(hash_key->key) : "the session"); 100 hash_key ? ZSTR_VAL(hash_key->key) : "the session");
106 memcpy(ZSTR_VAL(debase64), backup, ZSTR_LEN(debase64)); 101 memcpy(ZSTR_VAL(debase64), backup, ZSTR_LEN(debase64));
107 efree(backup); 102 ret = ZEND_HASH_APPLY_KEEP; goto out;
108 return ZEND_HASH_APPLY_KEEP;
109 } else { 103 } else {
110 sp_log_warn("cookie_encryption", 104 sp_log_warn("cookie_encryption",
111 "Something went wrong with the decryption of %s", 105 "Something went wrong with the decryption of %s",
112 hash_key ? ZSTR_VAL(hash_key->key) : "the session"); 106 hash_key ? ZSTR_VAL(hash_key->key) : "the session");
113 efree(backup); 107 ret = ZEND_HASH_APPLY_REMOVE; goto out;
114 return ZEND_HASH_APPLY_REMOVE;
115 } 108 }
116 } 109 }
117 efree(backup);
118 110
119 ZVAL_STRINGL(pDest, (char *)(decrypted + crypto_secretbox_ZEROBYTES), 111 ZVAL_STRINGL(pDest, (char *)(decrypted + crypto_secretbox_ZEROBYTES),
120 ZSTR_LEN(debase64) - crypto_secretbox_NONCEBYTES - 1 - 112 ZSTR_LEN(debase64) - crypto_secretbox_NONCEBYTES - 1 -
121 crypto_secretbox_ZEROBYTES); 113 crypto_secretbox_ZEROBYTES);
122 114
123 efree(decrypted); 115 ret = ZEND_HASH_APPLY_KEEP;
124 116
125 return ZEND_HASH_APPLY_KEEP; 117out:
118
119 if (debase64) { zend_string_efree(debase64); }
120 if (decrypted) { efree(decrypted); }
121 if (backup) { efree(backup); }
122
123 return ret;
126} 124}
127 125
128/* 126/*
@@ -156,10 +154,19 @@ zend_string *encrypt_zval(zend_string *data) {
156 154
157 memcpy(encrypted_data, nonce, crypto_secretbox_NONCEBYTES); 155 memcpy(encrypted_data, nonce, crypto_secretbox_NONCEBYTES);
158 156
159 crypto_secretbox(encrypted_data + crypto_secretbox_NONCEBYTES, 157 int err = crypto_secretbox(encrypted_data + crypto_secretbox_NONCEBYTES,
160 data_to_encrypt, encrypted_msg_len, nonce, key); 158 data_to_encrypt, encrypted_msg_len, nonce, key);
161 159
162 zend_string *z = php_base64_encode(encrypted_data, emsg_and_nonce_len); 160 zend_string *z = NULL;
161 if (err) {
162 sp_log_err("cookie_encryption", "something went wrong during encryption");
163 z = zend_string_init("<sp_encryption_error>", 21, 0);
164 } else {
165 z = php_base64_encode(encrypted_data, emsg_and_nonce_len);
166 }
167
168 efree(data_to_encrypt);
169 efree(encrypted_data);
163 170
164 return z; 171 return z;
165} 172}