samedi 3 février 2018

Laravel 5 / Nginx / Setting up gpg using gnupg / Encrypt Secret Key for Login

I am trying now since days to bring gpg working on Laravel using Debian Server + Nginx and PHP5. I have an 2FA Login page using Gnupg.

Controller:

use gnupg;

    public function showDecryptionForm()
    {
      if(session()->has('user_name'))
      {
        $user_name = session()->get('user_name');
        $user_password = session()->get('user_password');
      }
      else
      {
        return redirect()->route('login');
      }
      $user_details = User::where('username',$user_name)->first();

      $secret = $this->generateSecretKey();

      $secret_hash = password_hash($secret, PASSWORD_BCRYPT);

      $this->secret_code = $secret_hash;

      //echo $user_details->pgp;

      putenv("GNUPGHOME =/home/user/.gnupg");
      $gpg = new gnupg();
      $key = $gpg->import($user_details->pgp);
      $gpg->addencryptkey($key['fingerprint']);
      $enc = $gpg->encrypt($secret);
      $gpg->clearencryptkeys();
      //echo '<textarea rows="30" class="form-control" name="pgp-msg">'.$enc.'</textarea>';

      session()->flash('secret_hash',$secret_hash);

      return view('auth.decryptionform',compact('enc','user_name','user_password'));
    }


    public function postDecryptionForm(Request $request)
    {
      if (Hash::check($request->decrypt_code,session()->get('secret_hash'))) 
      {
        if (Auth::attempt(['username' => $request->user_name, 'password' => $request->user_password]))
        { 
          $user = Auth::user();
          $user->last_seen = date('Y-m-d H:i:s', time());
          $user->save();
          return redirect()->route('home');
        }
      }
      else
      { 
        session()->flash('errormessage','Your PGP Decryption Code is Wrong!!!');
        return redirect('login');
      }  
    }
    public function generateSecretKey($length = 15)
    {
      $secret = '';
      for($i = 0; $length > $i; $i++)
      {
        $secret = $secret.rand(0,9);
      }
      return $secret;
    }
}

Blade:

   <label for="message">Public Key:</label>
   <textarea name="message" id="message" class="form-control" rows="15" cols="40" readonly></textarea>

I have installed GPGME,GnuPG and Pecl PHP extension and all looks fine.

I have double check that all folders and files have the correct permission:

sudo mkdir -p /home/www-data/.gnupg
sudo chown -R www-data:www-data /home/www-data/.gnupg
sudo chmod 700 /home/www-data/.gnupg

or

chown -R $(whoami) ~/.gnupg/
chmod 600 ~/.gnupg/*
chmod 700 ~/.gnupg

Now i have create an new key Pair using GPG and import the Secret and Public Key to my pubring.gpg

I see the Keys with:

sudo -u www-data gpg --homedir /home/www-data/.gnupg --list-keys
sudo -u www-data gpg --homedir /home/www-data/.gnupg --list-secret-keys

I can sign and Verify files with key.

All files have now -rwx------ 1 Permission. Works also not with other Permission... I know GPG is very sensitive about Permission.

I have also set the default key id from my generated key pair to gpg.conf file.

My Blade in Laravel 5 shows only always an empty field instead of the encrypted Key . I have also added instead of $gpg->addencryptkey($key['fingerprint']); my Keys fingerprint manually.

Now i have added this line to my Controller:

echo $gpg->geterror()

Now i get in Browser this message: no key set for encryption

Anyone an Solution why its not work for me?? I have really try anything , maybe someone here they work with gpg.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire