Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Dev: tab to space in LSSodium
  • Loading branch information
Shnoulle committed May 20, 2020
1 parent ae8027a commit 97a1019
Showing 1 changed file with 72 additions and 75 deletions.
147 changes: 72 additions & 75 deletions application/core/LSSodium.php
Expand Up @@ -19,25 +19,24 @@ public function init(){
} else {
$this->checkIfKeyExists();
}

}

/**
* Check if Sodium library is installed
* @return bool
*/
public function checkIfLibraryExists(){
}

/**
* Check if Sodium library is installed
* @return bool
*/
public function checkIfLibraryExists(){
if (function_exists('sodium_crypto_sign_open')){
$this->bLibraryExists = true;
}
}

/**
*
* Check if encryption key exists in configuration
* @return bool Return decrypted value (string or unsezialized object) if suceeded. Return FALSE if an error occurs (bad password/salt given) or inpyt encryptedString
*/
protected function checkIfKeyExists(){
/**
*
* Check if encryption key exists in configuration
* @return bool Return decrypted value (string or unsezialized object) if suceeded. Return FALSE if an error occurs (bad password/salt given) or inpyt encryptedString
*/
protected function checkIfKeyExists(){
if (empty(Yii::app()->getConfig('encryptionkeypair'))){
$this->generateEncryptionKeys(); //return false;
}
Expand All @@ -53,38 +52,38 @@ protected function checkIfKeyExists(){
}

/**
*
* Get encryption key from version.php config file
* @return string Return encryption key string
*/
protected function getEncryptionKey(){
*
* Get encryption key from version.php config file
* @return string Return encryption key string
*/
protected function getEncryptionKey(){
return ParagonIE_Sodium_Compat::hex2bin(Yii::app()->getConfig('encryptionkeypair'));
}

/**
*
* Get encryption key from version.php config file
* @return string Return encryption key string
*/
protected function getEncryptionPublicKey(){
*
* Get encryption key from version.php config file
* @return string Return encryption key string
*/
protected function getEncryptionPublicKey(){
return ParagonIE_Sodium_Compat::hex2bin(Yii::app()->getConfig('encryptionpublickey'));
}

/**
*
* Get encryption key from version.php config file
* @return string Return encryption key string
*/
protected function getEncryptionSecretKey(){
*
* Get encryption key from version.php config file
* @return string Return encryption key string
*/
protected function getEncryptionSecretKey(){
return ParagonIE_Sodium_Compat::hex2bin(Yii::app()->getConfig('encryptionsecretkey'));
}
}

/**
* Encrypt input data using AES256 CBC encryption
* @param unknown_type $sDataToEncrypt Data to encrypt. Could be a string or a serializable PHP object
* @return string Return encrypted AES256 CBC value
*/
public function encrypt($sDataToEncrypt){
* Encrypt input data using AES256 CBC encryption
* @param unknown_type $sDataToEncrypt Data to encrypt. Could be a string or a serializable PHP object
* @return string Return encrypted AES256 CBC value
*/
public function encrypt($sDataToEncrypt){
if ($this->bLibraryExists === true){
if (!empty($sDataToEncrypt)){
$sEncrypted = base64_encode(ParagonIE_Sodium_Compat::crypto_sign((string) $sDataToEncrypt, $this->sEncryptionSecretKey));
Expand All @@ -95,16 +94,16 @@ public function encrypt($sDataToEncrypt){
} else {
return $sDataToEncrypt;
}
}
}

/**
*
* Decrypt encrypted string.
* @param string $sEncryptedString Encrypted string to decrypt
* @param bool $bReturnFalseIfError false by default. If TRUE, return false in case of error (bad decryption). Else, return given $encryptedInput value
* @return string Return decrypted value (string or unsezialized object) if suceeded. Return FALSE if an error occurs (bad password/salt given) or inpyt encryptedString
*/
public function decrypt($sEncryptedString, $bReturnFalseIfError=false){
/**
*
* Decrypt encrypted string.
* @param string $sEncryptedString Encrypted string to decrypt
* @param bool $bReturnFalseIfError false by default. If TRUE, return false in case of error (bad decryption). Else, return given $encryptedInput value
* @return string Return decrypted value (string or unsezialized object) if suceeded. Return FALSE if an error occurs (bad password/salt given) or inpyt encryptedString
*/
public function decrypt($sEncryptedString, $bReturnFalseIfError=false){
if ($this->bLibraryExists === true){
if (!empty($sEncryptedString) && $sEncryptedString != 'null'){
$plaintext = ParagonIE_Sodium_Compat::crypto_sign_open(base64_decode($sEncryptedString), $this->sEncryptionPublicKey);
Expand All @@ -117,14 +116,13 @@ public function decrypt($sEncryptedString, $bReturnFalseIfError=false){
} else {
return $sEncryptedString;
}

}
}

/**
*
* Write encryption key to version.php config file
*/
protected function generateEncryptionKeys(){
/**
*
* Write encryption key to version.php config file
*/
protected function generateEncryptionKeys(){
if (is_file(APPPATH.'config/security.php')) {
// Never replace an existing file
throw new CException(500, gT("Configuration file already exist"));
Expand All @@ -139,29 +137,29 @@ protected function generateEncryptionKeys(){
}

$sConfig = "<?php if (!defined('BASEPATH')) exit('No direct script access allowed');"."\n"
."/*"."\n"
." * LimeSurvey"."\n"
." * Copyright (C) 2007-2019 The LimeSurvey Project Team / Carsten Schmitz"."\n"
." * All rights reserved."."\n"
." * License: GNU/GPL License v3 or later, see LICENSE.php"."\n"
." * LimeSurvey is free software. This version may have been modified pursuant"."\n"
." * to the GNU General Public License, and as distributed it includes or"."\n"
." * is derivative of works licensed under the GNU General Public License or"."\n"
." * other free or open source software licenses."."\n"
." * See COPYRIGHT.php for copyright notices and details."."\n"
." */"."\n"
."\n"
."/* "."\n"
."WARNING!!!"."\n"
."ONCE SET, ENCRYPTION KEYS SHOULD NEVER BE CHANGED, OTHERWISE ALL ENCRYPTED DATA COULD BE LOST !!!"."\n"
."\n"
."*/"."\n"
."\n"
."\$config = array();"."\n"
."\$config['encryptionkeypair'] = '".$sEncryptionKeypair."';"."\n"
."\$config['encryptionpublickey'] = '".$sEncryptionPublicKey."';"."\n"
."\$config['encryptionsecretkey'] = '".$sEncryptionSecretKey."';"."\n"
."return \$config;";
."/*"."\n"
." * LimeSurvey"."\n"
." * Copyright (C) 2007-2019 The LimeSurvey Project Team / Carsten Schmitz"."\n"
." * All rights reserved."."\n"
." * License: GNU/GPL License v3 or later, see LICENSE.php"."\n"
." * LimeSurvey is free software. This version may have been modified pursuant"."\n"
." * to the GNU General Public License, and as distributed it includes or"."\n"
." * is derivative of works licensed under the GNU General Public License or"."\n"
." * other free or open source software licenses."."\n"
." * See COPYRIGHT.php for copyright notices and details."."\n"
." */"."\n"
."\n"
."/* "."\n"
."WARNING!!!"."\n"
."ONCE SET, ENCRYPTION KEYS SHOULD NEVER BE CHANGED, OTHERWISE ALL ENCRYPTED DATA COULD BE LOST !!!"."\n"
."\n"
."*/"."\n"
."\n"
."\$config = array();"."\n"
."\$config['encryptionkeypair'] = '".$sEncryptionKeypair."';"."\n"
."\$config['encryptionpublickey'] = '".$sEncryptionPublicKey."';"."\n"
."\$config['encryptionsecretkey'] = '".$sEncryptionSecretKey."';"."\n"
."return \$config;";

Yii::app()->setConfig("encryptionkeypair", $sEncryptionKeypair);
Yii::app()->setConfig("encryptionpublickey", $sEncryptionPublicKey);
Expand All @@ -170,7 +168,6 @@ protected function generateEncryptionKeys(){
file_put_contents(APPPATH.'config/security.php', $sConfig);
} else {
throw new CHttpException(500, gT("Configuration directory is not writable"));

}
}
}

0 comments on commit 97a1019

Please sign in to comment.