PWSafe3.pm

filter: Statementstmt 47.3% 222 / 469 Branchbran 32.5% 43 / 132 Conditioncond 25.0% 3 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 64.1% 43 / 67 Podpod 43.4% 10 / 23 totaltotal 45.2% 322 / 711 CC 78 SCAR 37.4
CC
78
Cov
44%
save
272.0
readfield
90.0
read
32.9
CRAP 42.0
« PWSafe3/Field.pm ↑ Summary PWSafe3/PasswordPolicy.pm »
1 #
2 # Copyright (c) 2011-2016 T.v.Dein <tlinden |AT| cpan.org>.
3 #
4 # Licensed under the terms of the Artistic License 2.0
5 # see: http://www.perlfoundation.org/artistic_license_2_0
6 #
7 # Implements:
8 # http://passwordsafe.svn.sourceforge.net/viewvc/passwordsafe/trunk/pwsafe/pwsafe/docs/formatV3.txt?revision=2139
9  
10 package Crypt::PWSafe3;
11  
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use Carp::Heavy;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use Crypt::CBC;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Crypt::ECB;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Crypt::Twofish;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Digest::HMAC;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Crypt::Random qw( makerandom );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use File::Copy qw(copy move);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use vars qw(@ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 1 $Crypt::PWSafe3::VERSION = '1.22';
35  
36 1 ▶use Crypt::PWSafe3::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use Crypt::PWSafe3::HeaderField;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use Crypt::PWSafe3::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use Crypt::PWSafe3::SHA256;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use Crypt::PWSafe3::PasswordPolicy;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 1 require 5.10.0;
43  
44 #
45 # check which random source to use.
46 # install a wrapper closure around the
47 # one we found.
48 1 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=14.6
49 1 eval {
50 1 require Bytes::Random::Secure;
51 0 Bytes::Random::Secure->import("random_bytes");
52 };
53 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 1 0 1
54 # well, didn' work, use slow function
55 1 eval { require Crypt::Random; };# qw( makerandom ); };
56 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 1 1
57 0 croak "Could not find either Crypt::Random or Bytes::Random::Secure. Install one of them and retry!";
58 }
59 else {
60 0 ▶ *Crypt::PWSafe3::random = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 0 my($this, $len) = @_;
62 0 my $bits = makerandom(Size => 256, Strength => 1);
63 0 return substr($bits, 0, $len);
64 1 };
65 }
66 }
67 else {
68 # good. use the faster one
69 0 ▶ *Crypt::PWSafe3::random = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 0 my($this, $len) = @_;
71 0 return random_bytes($len);
72 0 };
73 }
74 }
75  
76 1 my @fields = qw(tag salt iter shaps b1 b2 b3 b4 keyk file program
77 keyl iv hmac header strechedpw password whoami);
78 1 foreach my $field (@fields) {
79 0 ▶ eval qq(
Subroutine __ANON__: called CC=2 SCAR=7.3
Subroutine __ANON__: called CC=2 SCAR=7.3
Subroutine __ANON__: called CC=2 SCAR=7.3
Subroutine __ANON__: called CC=2 SCAR=7.3
Subroutine __ANON__: called CC=2 SCAR=7.3
Subroutine __ANON__: called CC=2 SCAR=7.3
Subroutine __ANON__: called CC=2 SCAR=7.3
Subroutine __ANON__: called CC=2 SCAR=7.3
Subroutine __ANON__: called CC=2 SCAR=7.3
Subroutine __ANON__: called CC=2 SCAR=7.3
Subroutine __ANON__: not called CC=2 SCAR=7.3
Subroutine __ANON__: called CC=2 SCAR=7.3
Subroutine __ANON__: called CC=2 SCAR=7.3
Subroutine __ANON__: not called CC=2 SCAR=7.3
Subroutine __ANON__: not called CC=2 SCAR=7.3
Subroutine __ANON__: called CC=2 SCAR=7.3
Subroutine __ANON__: called CC=2 SCAR=7.3
Subroutine __ANON__: not called CC=2 SCAR=7.3
Branch: 18 branches
BranchTrueFalseTotal
if ($arg) { } 4 3 7
if ($arg) { } 4 4 8
if ($arg) { } 4 4 8
if ($arg) { } 4 3 7
if ($arg) { } 4 4 8
if ($arg) { } 4 4 8
if ($arg) { } 4 4 8
if ($arg) { } 4 4 8
if ($arg) { } 4 4 8
if ($arg) { } 0 3 3
if ($arg) { } 0 0 0
if ($arg) { } 4 4 8
if ($arg) { } 4 4 8
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 4 8 12
if ($arg) { } 0 4 4
if ($arg) { } 0 0 0
80 *Crypt::PWSafe3::$field = sub {
81 my(\$this, \$arg) = \@_;
82 if (\$arg) {
83 return \$this->{$field} = \$arg;
84 }
85 else {
86 return \$this->{$field};
87 }
88 }
89 );
90 }
91  
92 5 ▶sub new {
Subroutine new: called CC=10 SCAR=25.1
Pod documented
93 #
94 # new vault object
95 5 my($this, %param) = @_;
96 5 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
97 5 my $self = \%param; # file, password, whoami, program, create
98 5 bless($self, $class);
99  
100 # sanity checks
101 5 ▶ if (! exists $self->{whoami}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"whoami"}) 5 0 5
102 5 $self->{whoami} = $ENV{USER};
103 }
104  
105 5 ▶ if (! exists $self->{program}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"program"}) 5 0 5
106 5 $self->{program} = $0;
107 }
108  
109 5 ▶ if (! exists $self->{password}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"password"}) 0 5 5
110 0 croak 'Parameter password is required';
111 }
112  
113 5 ▶ if (! exists $self->{create}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"create"}) 1 4 5
114 1 $self->{create} = 1;
115 }
116  
117 5 ▶ if (! exists $self->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{'file'}) { } 0 5 5
118 0 $self->{file} = '';
119 0 $self->create();
120 }
121 else {
122 5 ▶ if (! -s $self->{file} || ! -r $self->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -s $self->{'file'} && -r $self->{'file'}) { } 2 3 5
Condition66%2/3
-s $self->{'file'} && -r $self->{'file'}
!ll&&!rl&&r
203
MC/DC50%1/2
-s $self->{'file'} && -r $self->{'file'}
-s $self->{'file'} -r $self->{'file'}
Truth table66%2/3
A && B
A= -s $self->{'file'}B= -r $self->{'file'}
ABresult
0X0
100
111
123 2 ▶ if ($self->{create}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'create'}) { } 1 1 2
124 1 $self->create();
125 }
126 else {
127 1 croak "PWSafe3 file $self->{file} does not exist or is not readable";
128 }
129 }
130 else {
131 3 $self->read();
132 }
133 }
134  
135 0 $self->{modified} = 0;
136  
137 0 return $self;
138 }
139  
140 4 ▶sub stretchpw {
Subroutine stretchpw: called CC=2 SCAR=6.9
Pod undocumented
141 #
142 # generate the streched password hash
143 #
144 # algorithm is described here:
145 # [KEYSTRETCH Section 4.1] http://www.schneier.com/paper-low-entropy.pdf
146 4 my ($this, $passwd) = @_;
147 4 my $sha = Digest::SHA->new('SHA-256');
148 4 $sha->reset();
149 4 $sha->add( ( $passwd, $this->salt) );
150 4 my $stretched = $sha->digest();
151 4 foreach (1 .. $this->iter) {
152 8192 $sha->reset();
153 8192 $sha->add( ( $stretched) );
154 8192 $stretched = $sha->digest();
155 }
156 4 $passwd = 0 x 64;
157 4 return $stretched;
158 }
159  
160 1 ▶sub create {
Subroutine create: called CC=1 SCAR=0.0
Pod documented
161 #
162 # create an empty vault without writing to disk
163 1 my($this) = @_;
164  
165 # default header fields
166 1 $this->tag('PWS3');
167 1 $this->salt($this->random(32));
168 1 $this->iter(2048);
169  
170 # the streched pw
171 1 $this->strechedpw($this->stretchpw($this->password()));
172  
173 # generate hash of the streched pw
174 1 my $sha = Digest::SHA->new('SHA-256');
175 1 $sha->reset();
176 1 $sha->add( ( $this->strechedpw() ) );
177 1 $this->shaps( $sha->digest() );
178  
179 # encrypt b1 .. b4
180 1 my $crypt = Crypt::ECB->new;
181 1 $crypt->padding('none');
182 1 $crypt->cipher('Twofish');
183 1 $crypt->key( $this->strechedpw() );
184 1 $this->b1( $crypt->encrypt( $this->random(16) ) );
185 1 $this->b2( $crypt->encrypt( $this->random(16) ) );
186 1 $this->b3( $crypt->encrypt( $this->random(16) ) );
187 1 $this->b4( $crypt->encrypt( $this->random(16) ) );
188  
189 # create key k + l
190 1 $this->keyk( $crypt->decrypt( $this->b1() ) . $crypt->decrypt( $this->b2() ));
191 1 $this->keyl( $crypt->decrypt( $this->b3() ) . $crypt->decrypt( $this->b4() ));
192  
193 # create IV
194 1 $this->iv( $this->random(16) );
195  
196 # create hmac'er and cipher for actual encryption
197 1 $this->{hmacer} = Digest::HMAC->new($this->keyl, "Crypt::PWSafe3::SHA256");
198 1 $this->{cipher} = Crypt::CBC->new(
199 -key => $this->keyk,
200 -iv => $this->iv,
201 -cipher => 'Twofish',
202 -header => 'none',
203 -padding => 'null',
204 -literal_key => 1,
205 -keysize => 32,
206 -blocksize => 16
207 );
208  
209 # empty for now
210 0 $this->hmac( $this->{hmacer}->digest() );
211 }
212  
213 3 ▶sub read {
Subroutine read: called CC=11 SCAR=34.9
Pod undocumented
214 #
215 # read and decrypt an existing vault file
216 3 my($this) = @_;
217  
218 3 my $file = $this->file();
219 3 ▶ my $fd = FileHandle->new($file, 'r') or croak "Could not open $file for reading: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $fd = "FileHandle"->new($file, "r") or croak("Could not open $file for reading: $!") 3 0 3
220 3 $fd->binmode();
221 3 $this->{fd} = $fd;
222  
223 3 $this->tag( $this->readbytes(4) );
224 3 ▶ if ($this->tag ne 'PWS3') {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->tag ne "PWS3") 0 3 3
225 0 croak "Not a PasswordSave V3 file!";
226 }
227  
228 3 $this->salt( $this->readbytes(32) );
229 3 $this->iter( unpack("L<", $this->readbytes(4) ) );
230  
231 3 $this->strechedpw($this->stretchpw($this->password()));
232  
233 3 my $sha = Digest::SHA->new(256);
234 3 $sha->reset();
235 3 $sha->add( ( $this->strechedpw() ) );
236 3 $this->shaps( $sha->digest() );
237  
238 3 my $fileshaps = $this->readbytes(32);
239 3 ▶ if ($fileshaps ne $this->shaps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fileshaps ne $this->shaps) 0 3 3
240 0 croak "Wrong password!";
241 }
242  
243 3 $this->b1( $this->readbytes(16) );
244 3 $this->b2( $this->readbytes(16) );
245 3 $this->b3( $this->readbytes(16) );
246 3 $this->b4( $this->readbytes(16) );
247  
248 3 my $crypt = Crypt::ECB->new;
249 3 $crypt->padding('none');
250 3 ▶ $crypt->cipher('Twofish') || die $crypt->errstring;
Branch: 1 branch
BranchTrueFalseTotal
$crypt->cipher("Twofish") or die $crypt->errstring 3 0 3
251 3 $crypt->key( $this->strechedpw() );
252  
253 3 $this->keyk($crypt->decrypt($this->b1) . $crypt->decrypt($this->b2));
254 3 $this->keyl($crypt->decrypt($this->b3) . $crypt->decrypt($this->b4));
255  
256 3 $this->iv( $this->readbytes(16) );
257  
258 # create hmac'er and cipher for actual encryption
259 3 $this->{hmacer} = Digest::HMAC->new($this->keyl, "Crypt::PWSafe3::SHA256");
260 3 $this->{cipher} = Crypt::CBC->new(
261 -key => $this->keyk,
262 -iv => $this->iv,
263 -cipher => 'Twofish',
264 -header => 'none',
265 -padding => 'null',
266 -literal_key => 1,
267 -keysize => 32,
268 -blocksize => 16
269 );
270  
271 # read db header fields
272 0 $this->{header} = {};
273 0 while (1) {
274 0 my $field = $this->readfield('header');
275 0 ▶ if (! $field) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field) 0 0 0
276 0 last;
277 }
278 0 ▶ if ($field->type == 0xff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->type == 255) 0 0 0
279 0 last;
280 }
281 0 $this->addheader($field);
282 0 $this->hmacer($field->raw);
283 }
284  
285 # read db records
286 0 my $record = Crypt::PWSafe3::Record->new(super => $this);
287 0 $this->{record} = {};
288 0 while (1) {
289 0 my $field = $this->readfield();
290 0 ▶ if (! $field) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field) 0 0 0
291 0 last;
292 }
293 0 ▶ if ($field->type == 0xff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->type == 255) { } 0 0 0
294 0 $this->addrecord($record);
295 0 $record = Crypt::PWSafe3::Record->new(super => $this);
296 }
297 else {
298 0 $record->addfield($field);
299 0 $this->hmacer($field->raw);
300 }
301 }
302  
303 # read and check file hmac
304 0 $this->hmac( $this->readbytes(32) );
305 0 my $calcmac = $this->{hmacer}->digest();
306 0 ▶ if ($calcmac ne $this->hmac) {
Branch: 1 branch
BranchTrueFalseTotal
if ($calcmac ne $this->hmac) 0 0 0
307 0 croak "File integrity check failed, invalid HMAC";
308 }
309  
310 0 ▶ $this->{fd}->close() or croak "Could not close $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$this->{"fd"}->close or croak("Could not close ${file}: $!") 0 0 0
311 }
312  
313 0 ▶sub untaint {
Subroutine untaint: not called CC=2 SCAR=17.9
Pod undocumented
314 #
315 # untaint path's
316 0 my ($this, $path) = @_;
317 0 ▶ if($path =~ /([\w\-\/\\\.:]+\z)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[([\w\-/\\\.:]+\z)]) { } 0 0 0
318 0 return $1;
319 }
320 else {
321 # fail, return unchanged
322 0 return $path;
323 }
324 }
325  
326 0 ▶sub save {
Subroutine save: not called CC=16 SCAR=56.1
Pod documented
327 #
328 # write data to the vault file
329 0 my($this, %param) = @_;
330 0 my($file, $passwd);
331  
332 0 ▶ if (! exists $param{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $param{'file'}) { } 0 0 0
333 0 $file = $this->file;
334 }
335 else {
336 $file = $param{file}
337 0 }
338 0 ▶ if (! exists $param{passwd}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $param{'passwd'}) { } 0 0 0
339 0 $passwd = $this->password;
340 }
341 else {
342 $passwd = $param{passwd}
343 0 }
344  
345 0 ▶ if (! $this->{modified}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"modified"}) 0 0 0
346 0 return;
347 }
348  
349 0 my $lastsave = Crypt::PWSafe3::HeaderField->new(type => 0x04, value => time);
350 0 my $whatsaved = Crypt::PWSafe3::HeaderField->new(type => 0x06, value => $this->{program});
351 0 my $whosaved = Crypt::PWSafe3::HeaderField->new(type => 0x05, value => $this->{whoami});
352 0 $this->addheader($lastsave);
353 0 $this->addheader($whatsaved);
354 0 $this->addheader($whosaved);
355  
356 # open a temp vault file, first we try it in the same directory as the target vault file
357 0 my $tmpdir;
358 0 ▶ if (File::Spec->file_name_is_absolute($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if ('File::Spec'->file_name_is_absolute($file)) { } 0 0 0
359 0 my ($volume, $directories, undef) = File::Spec->splitpath($file);
360 0 $tmpdir = File::Spec->catdir($volume, $directories);
361 }
362 else {
363 0 my ($volume, $directories, undef) = File::Spec->splitpath(File::Spec->rel2abs($file));
364 0 $tmpdir = File::Spec->abs2rel(File::Spec->catdir($volume, $directories));
365 }
366  
367 0 my $fd;
368 0 ▶ if (-w $tmpdir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-w $tmpdir) { } 0 0 0
369 0 ▶ $fd = File::Temp->new(TEMPLATE => '.vaultXXXXXXXX', DIR => $tmpdir, EXLOCK => 0) or croak "Could not open tmpfile: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$fd = "File::Temp"->new("TEMPLATE", ".vaultXXXXXXXX", "DIR", $tmpdir, "EXLOCK", 0) or croak("Could not open tmpfile: $!\n") 0 0 0
370 }
371 else {
372 # well, then we'll use one in the tmp dir of the system
373 0 ▶ $fd = File::Temp->new(TEMPLATE => '.vaultXXXXXXXX', TMPDIR => 1, EXLOCK => 0) or croak "Could not open tmpfile: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$fd = "File::Temp"->new("TEMPLATE", ".vaultXXXXXXXX", "TMPDIR", 1, "EXLOCK", 0) or croak("Could not open tmpfile: $!\n") 0 0 0
374 }
375 0 my $tmpfile = "$fd";
376  
377 0 $this->{fd} = $fd;
378  
379 0 $this->writebytes($this->tag);
380 0 $this->writebytes($this->salt);
381 0 $this->writebytes(pack("L<", $this->iter));
382  
383 0 $this->strechedpw($this->stretchpw($passwd));
384  
385 # line 472
386 0 my $sha = Digest::SHA->new(256);
387 0 $sha->reset();
388 0 $sha->add( ( $this->strechedpw() ) );
389 0 $this->shaps( $sha->digest() );
390  
391 0 $this->writebytes($this->shaps);
392 0 $this->writebytes($this->b1);
393 0 $this->writebytes($this->b2);
394 0 $this->writebytes($this->b3);
395 0 $this->writebytes($this->b4);
396  
397 0 my $crypt = Crypt::ECB->new;
398 0 $crypt->padding('none');
399 0 $crypt->cipher('Twofish');
400 0 $crypt->key( $this->strechedpw() );
401  
402 0 $this->keyk($crypt->decrypt($this->b1) . $crypt->decrypt($this->b2));
403 0 $this->keyl($crypt->decrypt($this->b3) . $crypt->decrypt($this->b4));
404  
405 0 $this->writebytes($this->iv);
406  
407 0 $this->{hmacer} = Digest::HMAC->new($this->keyl, "Crypt::PWSafe3::SHA256");
408 0 $this->{cipher} = Crypt::CBC->new(
409 -key => $this->keyk,
410 -iv => $this->iv,
411 -cipher => 'Twofish',
412 -header => 'none',
413 -padding => 'null',
414 -literal_key => 1,
415 -keysize => 32,
416 -blocksize => 16
417 );
418  
419 0 my $eof = Crypt::PWSafe3::HeaderField->new(type => 0xff, value => '');
420  
421 0 foreach my $type (keys %{$this->{header}}) {
422 0 $this->writefield($this->{header}->{$type});
423 0 $this->hmacer($this->{header}->{$type}->{raw});
424 }
425 0 $this->writefield($eof);
426 0 $this->hmacer($eof->{raw});
427  
428 0 $eof = Crypt::PWSafe3::Field->new(type => 0xff, value => '');
429  
430 0 foreach my $uuid (keys %{$this->{record}}) {
431 0 my $record = $this->{record}->{$uuid};
432 0 foreach my $type (keys %{$record->{field}}) {
433 0 $this->writefield($record->{field}->{$type});
434 0 $this->hmacer($record->{field}->{$type}->{raw});
435 }
436 0 $this->writefield($eof);
437 0 $this->hmacer($eof->{raw});
438 }
439  
440 0 $this->writefield(Crypt::PWSafe3::Field->new(type => 'none', raw => 0));
441  
442 0 $this->hmac( $this->{hmacer}->digest() );
443 0 $this->writebytes($this->hmac);
444 0 ▶ if ($Config{d_fsync}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Crypt::PWSafe3::Config{"d_fsync"}) 0 0 0
445 0 ▶ $this->{fd}->sync() or croak "Could not fsync: $!";
Branch: 1 branch
BranchTrueFalseTotal
$this->{"fd"}->sync or croak("Could not fsync: $!") 0 0 0
446 }
447 0 ▶ $this->{fd}->close() or croak "Could not close tmpfile: $!";
Branch: 1 branch
BranchTrueFalseTotal
$this->{"fd"}->close or croak("Could not close tmpfile: $!") 0 0 0
448  
449 # now try to read it in again to check if it
450 # is valid what we created
451 0 eval {
452 0 my $vault = Crypt::PWSafe3->new(file => $tmpfile, create => 0, password => $passwd);
453 };
454 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
455 0 unlink $tmpfile;
456 0 croak "File integrity check failed ($@)";
457 }
458 else {
459 # well, seems to be ok :)
460 0 ▶ move($tmpfile, $file) or croak "Could not move $tmpfile to $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
move($tmpfile, $file) or croak("Could not move $tmpfile to ${file}: $!") 0 0 0
461 }
462 }
463  
464 0 ▶sub writefield {
Subroutine writefield: not called CC=5 SCAR=34.0
Pod undocumented
465 #
466 # write a field to vault file
467 0 my($this, $field) = @_;
468  
469 0 ▶ if ($field->type eq 'none') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->type eq "none") 0 0 0
470 0 $this->writebytes("PWS3-EOFPWS3-EOF");
471 0 return;
472 }
473  
474 0 my $len = pack("L<", $field->len);
475 0 my $type = pack("C", $field->type);
476 0 my $raw = $field->raw;
477  
478 # Assemble TLV block and pad to 16-byte boundary
479 0 my $data = $len . $type . $raw;
480  
481 0 ▶ if (length($data) % 16 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($data) % 16 != 0) 0 0 0
482 # too small or too large, padding required
483 0 my $padcount = 16 - (length($data) % 16);
484 0 $data .= $this->random($padcount);
485 }
486  
487 0 ▶ if (length($data) > 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data > 16) { } 0 0 0
488 0 my $crypt;
489 0 while (1) {
490 0 my $part = substr($data, 0, 16);
491 0 $crypt .= $this->encrypt($part);
492 0 ▶ if (length($data) <= 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data <= 16) { } 0 0 0
493 0 last;
494 }
495 else {
496 0 $data = substr($data, 16);
497 }
498 }
499 0 $this->writebytes($crypt);
500 }
501 else {
502 0 $this->writebytes($this->encrypt($data));
503 }
504 }
505  
506 0 ▶sub getrecord {
Subroutine getrecord: not called CC=2 SCAR=17.9
Pod undocumented
507 #
508 # return the given record
509 0 my($this, $uuid) = @_;
510 0 ▶ if (exists $this->{record}->{$uuid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{'record'}{$uuid}) { } 0 0 0
511 0 return $this->{record}->{$uuid};
512 }
513 else {
514 0 return 0;
515 }
516 }
517  
518 0 ▶sub getrecords {
Subroutine getrecords: not called CC=1 SCAR=6.9
Pod documented
519 #
520 # return all records we've got as a copy
521 0 my ($this) = @_;
522 0 return map { $this->{record}->{$_} } keys %{$this->{record}};
523 }
524  
525 0 ▶sub looprecord {
Subroutine looprecord: not called CC=1 SCAR=6.9
Pod documented
526 #
527 # return a list of uuid's of all records
528 0 my ($this) = @_;
529 0 return keys %{$this->{record}};
530 }
531  
532 0 ▶sub modifyrecord {
Subroutine modifyrecord: not called CC=3 SCAR=24.8
Pod documented
533 #
534 # modify a record identified by the given uuid
535 0 my($this, $uuid, %fields) = @_;
536  
537 0 ▶ if (! exists $this->{record}->{$uuid}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $this->{"record"}{$uuid}) 0 0 0
538 0 croak "No record with uuid $uuid found!";
539 }
540  
541 0 foreach my $field (keys %fields) {
542 0 $this->{record}->{$uuid}->modifyfield($field, $fields{$field});
543 }
544  
545 # mark vault as modified
546 0 $this->markmodified();
547 }
548  
549 0 ▶sub deleterecord {
Subroutine deleterecord: not called CC=2 SCAR=17.9
Pod documented
550 #
551 # delete a record identified by the given uuid, if present
552 # returns 1 if record was actually removed, 0 if it was not present
553 0 my($this, $uuid) = @_;
554  
555 0 ▶ if (! exists $this->{record}->{$uuid}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $this->{"record"}{$uuid}) 0 0 0
556 0 return 0;
557 }
558  
559 0 delete $this->{record}->{$uuid};
560  
561 # mark vault as modified
562 0 $this->markmodified();
563  
564 0 return 1;
565 }
566  
567  
568 0 ▶sub markmodified {
Subroutine markmodified: not called CC=1 SCAR=6.9
Pod undocumented
569 #
570 # mark the vault as modified by setting the appropriate header fields
571 0 my($this) = @_;
572 0 my $lastmod = Crypt::PWSafe3::HeaderField->new(
573 name => "lastsavetime",
574 value => time
575 );
576 my $who = Crypt::PWSafe3::HeaderField->new(
577 name => "wholastsaved",
578 value => $this->{whoami}
579 0 );
580 0 $this->addheader($lastmod);
581 0 $this->addheader($who);
582 0 $this->{modified} = 1;
583 }
584  
585 0 ▶sub newrecord {
Subroutine newrecord: not called CC=2 SCAR=17.9
Pod documented
586 #
587 # add a new record to an existing vault
588 0 my($this, %fields) = @_;
589 0 my $record = Crypt::PWSafe3::Record->new();
590 0 foreach my $field (keys %fields) {
591 0 $record->modifyfield($field, $fields{$field});
592 }
593 0 $this->markmodified();
594 0 $this->addrecord($record);
595 0 $this->{records}->{$record->uuid}->{super} = $this;
596 0 return $record->uuid;
597 }
598  
599 0 ▶sub addrecord {
Subroutine addrecord: not called CC=1 SCAR=6.9
Pod undocumented
600 #
601 # add a record object to record hash
602 0 my($this, $record) = @_;
603 0 $this->{record}->{ $record->uuid } = $record;
604 }
605  
606 0 ▶sub addheader {
Subroutine addheader: not called CC=1 SCAR=6.9
Pod documented
607 #
608 # add a header field to header hash
609 0 my($this, $field) = @_;
610 0 $this->{header}->{ $field->name } = $field;
611 }
612  
613  
614 0 ▶sub readfield {
Subroutine readfield: not called CC=9 SCAR=45.0
Pod undocumented
615 #
616 # read and return a field object of the vault
617 0 my($this, $header) = @_;
618 0 my $data = $this->readbytes(16);
619 0 ▶ if (! $data or length($data) < 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $data or length $data < 16) 0 0 0
Condition0%0/3
not $data or length $data < 16
l!l&&r!l&&!r
000
MC/DC0%0/2
not $data or length $data < 16
not $data length $data < 16
Truth table0%0/3
not(A) or B
A= not $dataB= length $data < 16
ABresult
1X1
011
000
620 0 croak "EOF encountered when parsing record field";
621 }
622 0 ▶ if ($data eq "PWS3-EOFPWS3-EOF") {
Branch: 1 branch
BranchTrueFalseTotal
if ($data eq "PWS3-EOFPWS3-EOF") 0 0 0
623 0 return 0;
624 }
625  
626 0 $data = $this->decrypt($data);
627  
628 0 my $len = unpack("L<", substr($data, 0, 4));
629 0 my $type = unpack("C", substr($data, 4, 1));
630 0 my $raw = substr($data, 5);
631  
632 0 ▶ if ($len > 11) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 11) 0 0 0
633 0 my $step = int(($len+4) / 16);
634 0 for (1 .. $step) {
635 0 my $data = $this->readbytes(16);
636 0 ▶ if (! $data or length($data) < 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $data or length $data < 16) 0 0 0
Condition0%0/3
not $data or length $data < 16
l!l&&r!l&&!r
000
MC/DC0%0/2
not $data or length $data < 16
not $data length $data < 16
Truth table0%0/3
not(A) or B
A= not $dataB= length $data < 16
ABresult
1X1
011
000
637 0 croak "EOF encountered when parsing record field";
638 }
639 0 $raw .= $this->decrypt($data);
640 }
641 }
642 0 $raw = substr($raw, 0, $len);
643 0 ▶ if ($header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header) { } 0 0 0
644 0 return Crypt::PWSafe3::HeaderField->new(type => $type, raw => $raw);
645 }
646 else {
647 0 return Crypt::PWSafe3::Field->new(type => $type, raw => $raw);
648 }
649 }
650  
651 0 ▶sub decrypt {
Subroutine decrypt: not called CC=1 SCAR=6.9
Pod undocumented
652 #
653 # helper, decrypt a string
654 0 my ($this, $data) = @_;
655 0 my $clear = $this->{cipher}->decrypt($data);
656 0 $this->{cipher}->iv($data);
657 0 return $clear;
658 }
659  
660 0 ▶sub encrypt {
Subroutine encrypt: not called CC=2 SCAR=17.9
Pod undocumented
661 #
662 # helper, encrypt a string
663 0 my ($this, $data) = @_;
664 0 my $raw = $this->{cipher}->encrypt($data);
665 0 ▶ if (length($raw) > 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $raw > 16) 0 0 0
666 # we use only the last 16byte block as next iv
667 # if data is more than 1 blocks then Crypt::CBC
668 # has already updated the iv for the inner blocks
669 0 $raw = substr($raw, -16, 16);
670 }
671 0 $this->{cipher}->iv($raw);
672 0 return $raw;
673 }
674  
675 0 ▶sub hmacer {
Subroutine hmacer: not called CC=1 SCAR=6.9
Pod undocumented
676 #
677 # helper, hmac generator
678 0 my($this, $data) = @_;
679  
680 0 $this->{hmacer}->add($data);
681 }
682  
683 27 ▶sub readbytes {
Subroutine readbytes: called CC=2 SCAR=7.1
Pod undocumented
684 #
685 # helper, reads number of bytes
686 27 my ($this, $size) = @_;
687 27 my $buffer;
688 27 my ($package, $filename, $line) = caller;
689  
690 27 my $got = $this->{fd}->sysread($buffer, $size);
691 27 ▶ if ($got == $size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got == $size) { } 27 0 27
692 27 $this->{sum} += $got;
693 27 return $buffer;
694 }
695 else {
696 0 return 0;
697 }
698 }
699  
700 0 ▶sub writebytes {
Subroutine writebytes: not called CC=2 SCAR=17.9
Pod undocumented
701 #
702 # helper, reads number of bytes
703 0 my ($this, $bytes) = @_;
704 0 my $got = $this->{fd}->syswrite($bytes);
705 0 ▶ if ($got) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got) { } 0 0 0
706 0 return $got;
707 }
708 else {
709 0 croak "Could not write to $this->{file}: $!";
710 }
711 }
712  
713  
714 0 ▶sub getheader {
Subroutine getheader: not called CC=2 SCAR=17.9
Pod documented
715 #
716 # return a header object
717 0 my($this, $name) = @_;
718 0 ▶ if (exists $this->{header}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{'header'}{$name}) { } 0 0 0
719 0 return $this->{header}->{$name};
720 }
721 else {
722 0 croak "Unknown header $name";
723 }
724 }
725  
726  
727  
728  
729 =head1 NAME
730  
731 Crypt::PWSafe3 - Read and write Passwordsafe v3 files
732  
733 =head1 SYNOPSIS
734  
735 use Crypt::PWSafe3;
736 my $vault = Crypt::PWSafe3->new(file => 'filename.psafe3', password => 'somesecret');
737
738 # fetch all database records
739 my @records = $vault->getrecords();
740 foreach my $record (@records) {
741 print $record->uuid;
742 print $record->title;
743 print $record->passwd;
744 # see Crypt::PWSafe3::Record for more details on accessing records
745 }
746  
747 # same as above but don't detach records from vault
748 foreach my $uuid ($vault->looprecord) {
749 # either change a record
750 $vault->modifyrecord($uuid, passwd => 'p1');
751  
752 # or just access it directly
753 print $vault->{record}->{$uuid}->title;
754 }
755  
756 # add a new record
757 $vault->newrecord(user => 'u1', passwd => 'p1', title => 't1');
758  
759 # modify an existing record
760 $vault->modifyrecord($uuid, passwd => 'p1');
761  
762 # replace a record (aka edit it)
763 my $record = $vault->getrecord($uuid);
764 $record->title('t2');
765 $record->passwd('foobar');
766 $vault->addrecord($record);
767  
768 # mark the vault as modified (not required if
769 # changes were done with ::modifyrecord()
770 $vault->markmodified();
771  
772 # save the vault
773 $vault->save();
774  
775 # save it under another name using another password
776 $vault->save(file => 'another.pwsafe3', passwd => 'blah');
777  
778 # access database headers
779 print $vault->getheader('wholastsaved')->value();
780 print scalar localtime($vault->getheader('lastsavetime')->value());
781  
782 # add/replace a database header
783 my $h = Crypt::PWSafe3::HeaderField->new(name => 'savedonhost', value => 'localhost');
784 $vault->addheader($h);
785  
786 =head1 DESCRIPTION
787  
788 Crypt::PWSafe3 provides read and write access to password
789 database files created by Password Safe V3 (and up) available at
790 http://passwordsafe.sf.net.
791  
792 =head1 METHODS
793  
794 =head2 B<new()>
795  
796 The new() method creates a new Crypt::PWSafe3 object. Any parameters
797 must be given as hash parameters.
798  
799 my $vault = Crypt::PWSafe3->new(
800 file => 'vault.psafe3',
801 password => 'secret',
802 whoami => 'user1',
803 program => 'mypwtool v1',
804 create => 0, # or 1
805 );
806  
807 Mandatory parameters:
808  
809 =over
810  
811 =item B<file>
812  
813 Specifies the password safe (v3) file. If it exists
814 it will be read in. Otherwise it will be created
815 if you call B<save()>.
816  
817 =item B<password>
818  
819 The password required to decrypt the password safe file.
820  
821 =back
822  
823 Optional parameters:
824  
825 =over
826  
827 =item B<whoami>
828  
829 Specifies the user who saves the password safe file.
830 If omitted the environment variable USER will be used
831 when calling B<save()>.
832  
833 =item B<program>
834  
835 Specifies which program saved the password safe file.
836 If omitted, the content of the perl variable $0 will
837 be used, which contains the name of the current running
838 script.
839  
840 =item B<create>
841  
842 If set to 0, B<new()> will fail if the file doesn't exist,
843 otherwise it will try to create it. Enabled by default.
844  
845 =back
846  
847 The optional parameters will become header fields of
848 the password safe file. You can manually set/override
849 more headers. See section L<addheader()> for
850 more details.
851  
852 =head2 B<getrecords()>
853  
854 Returns a list of all records found in the password
855 safe file. Each element is an B<Crypt::PWSafe3::Record>
856 object.
857  
858 A record object is identified by its B<UUID4> value,
859 which is a unique identifier. You can access the uuid by:
860  
861 $record->uuid
862  
863 Accessing other record properties works the same. For
864 more details, refer to L<Crypt::PWSafe3::Record>.
865  
866 B<Note>: record objects returned by getrecords() are
867 still associated with the L<Crypt::PWSafe3> object. So,
868 if you modify a field of such a record, the change will
869 be populated back into the vault. Of course you'd still
870 need to save it.
871  
872 Sample:
873  
874 foreach my $rec ($vault->getrecords) {
875 $rec->note('blah fasel');
876 }
877 $vault->save();
878  
879 However, it's also possible to use the B<modifyrecord()>
880 method, see below.
881  
882 =head2 B<looprecord()>
883  
884 Returns a list of UUIDs of all known records. You can
885 use this list to iterate over the records without
886 copying them and optionally changing them in place.
887  
888 Example:
889  
890 foreach my $uuid ($vault->looprecord) {
891 # either change a record
892 $vault->modifyrecord($uuid, passwd => 'p1');
893  
894 # or just access it directly
895 print $vault->{record}->{$uuid}->title;
896 }
897  
898  
899 =head2 B<modifyrecord(uuid, parameter-hash)>
900  
901 Modifies the record identified by the given UUID using
902 the values of the supplied parameter hash.
903  
904 Example:
905  
906 $vault->modifyrecord($uuid, passwd => 'p1');
907  
908 The parameter hash may contain any valid record field
909 type with according values. Refer to L<Crypt::PWSafe3::Record>
910 for details about available fields.
911  
912 =head2 B<newrecord(parameter-hash)>
913  
914 Create a new record. The UUID of the record will be generated
915 automatically. Refer to L<Crypt::PWSafe3::Record>
916 for details about available fields.
917  
918 =head2 B<addrecord(Crypt::PWSafe3::Record object)>
919  
920 Add a record to the vault. The record must be an
921 L<Crypt::PWSafe3::Record> object.
922  
923 =head2 B<deleterecord(uuid)>
924  
925 Delete the record identified by the given UUID.
926  
927 =head2 B<save([parameter-hash])>
928  
929 Save the current password safe vault back to disk.
930  
931 If not otherwise specified, use the same file and
932 password as we used to open it initially. If the
933 file doesn't exist it will be created.
934  
935 You may specify another filename and password here
936 by using a parameter hash.
937  
938 Example:
939  
940 $vault->save(file => 'anotherfile.psafe3', passwd => 'foo');
941  
942 Please note, that the vault will be written to a
943 temporary file first, then this temporary file
944 will be read in and if that works, it will be
945 moved over the destination file. This way the original
946 file persists if the written database gets corrupted
947 by some unknown reason (a bug for instance).
948  
949  
950 =head2 B<getheader(name)>
951  
952 Returns a raw B<Crypt::PWSafe3::HeaderField> object.
953 Refer to L<Crypt::PWSafe3::HeaderField> for details
954 how to access it.
955  
956 =head2 B<addheader(object)>
957  
958 Adds a header field to the password safe database. The
959 object parameter must be an B<Crypt::PWSafe3::HeaderField>
960 object.
961  
962 If the header already exists it will be replaced.
963  
964 Refer to L<Crypt::PWSafe3::HeaderField> for details
965 how to create new ones
966 .
967  
968 =head1 AUTHOR
969  
970 T.v.Dein <tlinden@cpan.org>
971  
972 =head1 BUGS
973  
974 Report bugs to
975 http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Crypt-PWSafe3.
976  
977  
978 =head1 SEE ALSO
979  
980 Subclasses:
981  
982 L<Crypt::PWSafe3::Record>
983 L<Crypt::PWSafe3::Field>
984 L<Crypt::PWSafe3::HeaderField>
985  
986 Password Safe Homepage:
987 L<http://passwordsafe.sourceforge.net/>
988  
989 Another (read-only) perl module:
990 L<Crypt::Pwsafe>
991  
992 A python port of Password Safe:
993 L<http://www.christoph-sommer.de/loxodo/>
994 Many thanks to Christoph Sommer, his python library
995 inspired me a lot and in fact most of the concepts
996 in this module are his ideas ported to perl.
997  
998 =head1 COPYRIGHT
999  
1000 Copyright (c) 2011-2016 by T.v.Dein <tlinden@cpan.org>.
1001  
1002 =head1 LICENSE
1003  
1004 This program is free software; you can redistribute it
1005 and/or modify it under the same terms of the Artistic
1006 License 2.0, see: L<http://www.perlfoundation.org/artistic_license_2_0>
1007  
1008 =head1 VERSION
1009  
1010 Crypt::PWSafe3 Version 1.22.
1011  
1012 =cut
1013  
1014  
1015  
1016  
1017 1 1;
1018  
« PWSafe3/Field.pm ↑ Summary PWSafe3/PasswordPolicy.pm »