Securely store user data in a database using Symfony

I want to store data from users so that they become useless, even if the database somehow leaked. I also do not want to be able to encrypt data, so I encrypt all my data with `openssl_encrypt 'as follows:

$passCode = base64_encode($this->get('session')->get('_pk')); if (strlen($passCode) <= 16) { $iv = str_pad($passCode, 16, '0'); } else { $iv = substr($passCode, 0, 16); } $ciphertext = openssl_encrypt('whatevervalue', 'AES-256-CBC', $passCode, 0, $iv); $test = new Test(); $test->setValue($ciphertext); ... $em->persist($test); $em->flush(); ... 

$passCode is actually their password, which I entered into the var session as follows:

SecurityListener.php

 <?php namespace AppBundle\Listener; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; class SecurityListener { public function __construct($security, Session $session) { $this->security = $security; $this->session = $session; } public function onSecurityInteractiveLogin(InteractiveLoginEvent $event) { $this->session->set('_pk', base64_encode($event->getRequest()->get('_password'))); } } 

2 Problems:

  • Saving $passCode (really don't know about sessions) is perhaps a security issue?

  • What happens if the user changes the password. In the current case, I will need to decrypt and re-encrypt all of its database data with a new password, so that this does not seem to be the right solution. What if he loses his password?

It may be easier to understand what I want here:

I want to encrypt all the data in my database that the user himself enters there. I want this to be a β€œfunction” that even admin (s) cannot read data without a key. I know that the latter is not 100% possible (since there will be ways to intercept passwords / keys when entering through the web interface, but at least this causes some code changes). Is anything like that possible? Any open source files I can take a look at?

Thanks!

+5
source share
1 answer
  • There are some invariants for each user that will be used to create the private key: user ID, account creation date, some random generated salt ...
  • Write a hash function that will generate the private key.
  • Obfuscate the function code.

This is not ideal, since people with access to the code and the database will be able to decrypt the data, but it should be safe for database leaks and you should not have the problems you are talking about.

The only safe solution I can think of is to force users to generate their private keys and encrypt data on the client side, and then send the encrypted data to the server. Otherwise, if the server has the tools necessary for decryption, there will always be an opportunity for someone who has full access to the system to decrypt sensitive data.

I am not an expert in this matter and please tell me if I am wrong.

0
source

Source: https://habr.com/ru/post/1258709/


All Articles