diff options
| author | Ben Fuhrmannek | 2016-03-01 14:55:46 +0100 |
|---|---|---|
| committer | Ben Fuhrmannek | 2016-03-01 14:55:46 +0100 |
| commit | c180da6de0851521cae98a1b385e03a120d5cf61 (patch) | |
| tree | ddd49425cf08f6b70b6cb88858fbe0d1f50d2456 /secureconfig.c | |
| parent | 7060d15d6d2624f81cb3a57ee319fa61ba06ad89 (diff) | |
added secure configuration loader (#28)secureconfig
Diffstat (limited to 'secureconfig.c')
| -rw-r--r-- | secureconfig.c | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/secureconfig.c b/secureconfig.c new file mode 100644 index 0000000..075baf0 --- /dev/null +++ b/secureconfig.c | |||
| @@ -0,0 +1,135 @@ | |||
| 1 | /* | ||
| 2 | +----------------------------------------------------------------------+ | ||
| 3 | | Suhosin Version 1 | | ||
| 4 | +----------------------------------------------------------------------+ | ||
| 5 | | Copyright (c) 2006-2007 The Hardened-PHP Project | | ||
| 6 | | Copyright (c) 2007-2010 SektionEins GmbH | | ||
| 7 | +----------------------------------------------------------------------+ | ||
| 8 | | This source file is subject to version 3.01 of the PHP license, | | ||
| 9 | | that is bundled with this package in the file LICENSE, and is | | ||
| 10 | | available through the world-wide-web at the following url: | | ||
| 11 | | http://www.php.net/license/3_01.txt | | ||
| 12 | | If you did not receive a copy of the PHP license and are unable to | | ||
| 13 | | obtain it through the world-wide-web, please send a note to | | ||
| 14 | | license@php.net so we can mail you a copy immediately. | | ||
| 15 | +----------------------------------------------------------------------+ | ||
| 16 | | Author: Juergen Pabel <jpabel@akkaya.de> | | ||
| 17 | +----------------------------------------------------------------------+ | ||
| 18 | */ | ||
| 19 | |||
| 20 | #ifdef SUHOSIN_EXPERIMENTAL | ||
| 21 | #include <stdio.h> | ||
| 22 | #include "php.h" | ||
| 23 | #include "php_suhosin.h" | ||
| 24 | #include "sha256.h" | ||
| 25 | |||
| 26 | static char cryptkey[32]; | ||
| 27 | |||
| 28 | /* {{{ proto string secureconfig_encrypt(string plaintext) | ||
| 29 | Encrypt a configuration value using the configured cryptographic key */ | ||
| 30 | static PHP_FUNCTION(suhosin_secureconfig_encrypt) | ||
| 31 | { | ||
| 32 | char *plaintext, *ciphertext; | ||
| 33 | int plaintext_len, ciphertext_len; | ||
| 34 | int i; | ||
| 35 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &plaintext, &plaintext_len) == FAILURE) { | ||
| 36 | return; | ||
| 37 | } | ||
| 38 | ciphertext = suhosin_encrypt_string(plaintext, plaintext_len, "", 0, cryptkey TSRMLS_CC); | ||
| 39 | if(ciphertext == NULL) { | ||
| 40 | return; | ||
| 41 | } | ||
| 42 | ciphertext_len = strlen(ciphertext); | ||
| 43 | /* undo suhosin_encrypt_string()'s base64 alphabet transformation */ | ||
| 44 | for (i=0; i<ciphertext_len; i++) { | ||
| 45 | switch (ciphertext[i]) { | ||
| 46 | case '-': ciphertext[i]='/'; break; | ||
| 47 | case '.': ciphertext[i]='='; break; | ||
| 48 | case '_': ciphertext[i]='+'; break; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | RETURN_STRINGL((char *)ciphertext, ciphertext_len, 1); | ||
| 52 | } | ||
| 53 | |||
| 54 | /* }}} */ | ||
| 55 | |||
| 56 | |||
| 57 | /* {{{ proto string secureconfig_decrypt(string ciphertext) | ||
| 58 | Decrypt a configuration value using the configured cryptographic key */ | ||
| 59 | static PHP_FUNCTION(suhosin_secureconfig_decrypt) | ||
| 60 | { | ||
| 61 | char *plaintext, *ciphertext; | ||
| 62 | int plaintext_len, ciphertext_len; | ||
| 63 | int i; | ||
| 64 | |||
| 65 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ciphertext, &ciphertext_len) == FAILURE) { | ||
| 66 | return; | ||
| 67 | } | ||
| 68 | |||
| 69 | /* redo suhosin_encrypt_string()'s base64 alphabet transformation */ | ||
| 70 | for (i=0; i<ciphertext_len; i++) { | ||
| 71 | switch (ciphertext[i]) { | ||
| 72 | case '/': ciphertext[i]='-'; break; | ||
| 73 | case '=': ciphertext[i]='.'; break; | ||
| 74 | case '+': ciphertext[i]='_'; break; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | plaintext = suhosin_decrypt_string(ciphertext, ciphertext_len, "", 0, cryptkey, &plaintext_len, 0 TSRMLS_CC); | ||
| 78 | if(plaintext == NULL || plaintext_len <= 0) { | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | RETURN_STRINGL((char *)plaintext, plaintext_len, 1); | ||
| 82 | } | ||
| 83 | |||
| 84 | /* }}} */ | ||
| 85 | |||
| 86 | |||
| 87 | /* {{{ suhosin_secureconfig_functions[] | ||
| 88 | */ | ||
| 89 | static function_entry suhosin_secureconfig_functions[] = { | ||
| 90 | PHP_NAMED_FE(secureconfig_encrypt, PHP_FN(suhosin_secureconfig_encrypt), NULL) | ||
| 91 | PHP_NAMED_FE(secureconfig_decrypt, PHP_FN(suhosin_secureconfig_decrypt), NULL) | ||
| 92 | {NULL, NULL, NULL} | ||
| 93 | }; | ||
| 94 | /* }}} */ | ||
| 95 | |||
| 96 | |||
| 97 | void suhosin_hook_secureconfig(TSRMLS_D) | ||
| 98 | { | ||
| 99 | char* key; | ||
| 100 | suhosin_SHA256_CTX ctx; | ||
| 101 | |||
| 102 | // TSRMLS_FETCH(); | ||
| 103 | |||
| 104 | /* check if we already have secureconfig support */ | ||
| 105 | if (zend_hash_exists(CG(function_table), "secureconfig_encrypt", sizeof("secureconfig_encrypt"))) { | ||
| 106 | return; | ||
| 107 | } | ||
| 108 | |||
| 109 | key = SUHOSIN_G(secureconfig_cryptkey); | ||
| 110 | if (key != NULL) { | ||
| 111 | suhosin_SHA256Init(&ctx); | ||
| 112 | suhosin_SHA256Update(&ctx, (unsigned char*)key, strlen(key)); | ||
| 113 | suhosin_SHA256Final((unsigned char *)cryptkey, &ctx); | ||
| 114 | } else { | ||
| 115 | memset(cryptkey, 0x55 /*fallback key with alternating bits*/, 32); | ||
| 116 | } | ||
| 117 | |||
| 118 | /* add the secureconfig functions */ | ||
| 119 | #ifndef ZEND_ENGINE_2 | ||
| 120 | zend_register_functions(suhosin_secureconfig_functions, NULL, MODULE_PERSISTENT TSRMLS_CC); | ||
| 121 | #else | ||
| 122 | zend_register_functions(NULL, suhosin_secureconfig_functions, NULL, MODULE_PERSISTENT TSRMLS_CC); | ||
| 123 | #endif | ||
| 124 | } | ||
| 125 | |||
| 126 | #endif /* SUHOSIN_EXPERIMENTAL */ | ||
| 127 | |||
| 128 | /* | ||
| 129 | * Local variables: | ||
| 130 | * tab-width: 4 | ||
| 131 | * c-basic-offset: 4 | ||
| 132 | * End: | ||
| 133 | * vim600: sw=4 ts=4 fdm=marker | ||
| 134 | * vim<600: sw=4 ts=4 | ||
| 135 | */ | ||
