Skip to content

Commit

Permalink
Save any ObfuscatedText as encrypted
Browse files Browse the repository at this point in the history
  • Loading branch information
jrouzierinverse committed Oct 2, 2024
1 parent 6ff775a commit 482f9f9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
16 changes: 16 additions & 0 deletions html/pfappserver/lib/pfappserver/Form/Field/ObfuscatedText.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ extends 'HTML::FormHandler::Field::Text';

use pf::util;
use namespace::autoclean;
use pf::config::crypt;

has '+type_attr' => ( default => 'password' );
has '+inflate_default_method'=> ( default => sub { \&inflate } );
has '+deflate_value_method'=> ( default => sub { \&deflate } );

sub BUILD {
my ($self, @args) = @_;
Expand All @@ -42,6 +45,19 @@ sub element_attributes {
return $attr;
}


sub deflate {
my ($self, $value ) = @_;
$value = pf::config::crypt::pf_encrypt($value);
return $value;
}

sub inflate {
my ($self, $value ) = @_;
$value = pf::config::crypt::pf_decrypt($value);
return $value;
}

=head1 COPYRIGHT
Copyright (C) 2005-2024 Inverse inc.
Expand Down
7 changes: 7 additions & 0 deletions lib/pf/config/crypt.pm
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ sub decode_tags {

sub pf_encrypt {
my ($text) = @_;
if (rindex($text, $PREFIX, 0) == 0) {
return $text;
}

my $iv = random_bytes(12);
my $ad = '';
my ($ciphertext, $tag) = gcm_encrypt_authenticate('AES', $DERIVED_KEY, $iv, $ad, $text);
Expand All @@ -88,6 +92,9 @@ sub pf_encrypt {

sub pf_decrypt {
my ($data) = @_;
if (rindex($data, $PREFIX, 0) != 0) {
return $data;
}
my $tags = decode_tags($data);
return gcm_decrypt_verify('AES', $DERIVED_KEY, $tags->{iv}, $tags->{ad}, $tags->{data}, $tags->{tag});
}
Expand Down
1 change: 1 addition & 0 deletions lib/pfconfig/cached.pm
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use Sereal::Decoder qw(sereal_decode_with_object);
use Time::HiRes qw(stat time);
use pf::Sereal qw($DECODER);
use pfconfig::config;
use pf::config::crypt::object;
use bytes;

our $LAST_TOUCH_CACHE = 0;
Expand Down

0 comments on commit 482f9f9

Please sign in to comment.