SoftHSM/Server.pm

filter: Statementstmt 8.1% 28 / 342 Branchbran 0.0% 0 / 190 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 20.0% 8 / 40 Podpod 100% 14 / 14 totaltotal 7.7% 50 / 642 CC 114 SCAR 42.3
CC
114
Cov
5%
__ANON__
182.0
UpdateConfig
156.0
Init
132.0
CRAP 68.6
↑ Summary SoftHSM/CLI.pm »
1 package Lim::Plugin::SoftHSM::Server;
2  
3 1 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Fcntl qw(:seek);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Lim::Plugin::SoftHSM ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Lim::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use base qw(Lim::Component::Server);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =encoding utf8
17  
18 =head1 NAME
19  
20 Lim::Plugin::SoftHSM::Server - Server class for SoftHSM management plugin
21  
22 =head1 VERSION
23  
24 See L<Lim::Plugin::SoftHSM> for version.
25  
26 =cut
27  
28 1 our $VERSION = $Lim::Plugin::SoftHSM::VERSION;
29  
30 =head1 SYNOPSIS
31  
32 use Lim::Plugin::SoftHSM;
33  
34 # Create a Server object
35 $client = Lim::Plugin::SoftHSM->Server;
36  
37 =head1 CONFIGURATION
38  
39 TODO
40  
41 =over 4
42  
43 =item SOFTHSM_VERSION_MIN
44  
45 =item SOFTHSM_VERSION_MAX
46  
47 =back
48  
49 =cut
50  
51 1 our $VERSION = $Lim::Plugin::SoftHSM::VERSION;
52 1 our %ConfigFiles = (
53 'softhsm.conf' => [
54 '/etc/softhsm/softhsm.conf',
55 '/etc/softhsm.conf',
56 'softhsm.conf'
57 ]
58 );
59  
60 sub SOFTHSM_VERSION_MIN (){ 1003000 }
61 sub SOFTHSM_VERSION_MAX (){ 1003003 }
62  
63 =head1 INTERNAL METHODS
64  
65 These are only internal methods and should not be used externally.
66  
67 =over 4
68  
69 =item Init
70  
71 =cut
72  
73 0 ▶sub Init {
Subroutine Init: not called CC=11 SCAR=48.8
Pod documented
74 0 my $self = shift;
75 0 my %args = ( @_ );
76  
77 $self->{bin} = {
78 0 softhsm => 0
79 };
80 0 $self->{version} = {};
81
82 0 my ($stdout, $stderr);
83 0 my $cv = Lim::Util::run_cmd [ 'softhsm', '--version' ],
84 '<', '/dev/null',
85 '>', \$stdout,
86 '2>', \$stderr;
87 0 ▶ if ($cv->recv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cv->recv) { } 0 0 0
88 0 $self->{logger}->warn('Unable to find "softhsm" executable, module functions limited');
89 }
90 else {
91 0 ▶ if ($stdout =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stdout =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)/ou) { } 0 0 0
92 0 my ($major,$minor,$patch) = ($1, $2, $3);
93
94 0 ▶ if ($major > 0 and $major < 10 and $minor > -1 and $minor < 10 and $patch > -1 and $patch < 100) {
Branch: 1 branch
BranchTrueFalseTotal
if ($major > 0 and $major < 10 and $minor > -1 and $minor < 10 and $patch > -1 and $patch < 100) { } 0 0 0
Condition0%0/15
$major > 0 and $major < 10 and $minor > -1 and $minor < 10 and $patch > -1 and $patch < 100
!ll&&!rl&&r
000
$major > 0 and $major < 10 and $minor > -1 and $minor < 10 and $patch > -1
!ll&&!rl&&r
000
$major > 0 and $major < 10 and $minor > -1 and $minor < 10
!ll&&!rl&&r
000
$major > 0 and $major < 10 and $minor > -1
!ll&&!rl&&r
000
$major > 0 and $major < 10
!ll&&!rl&&r
000
MC/DC0%0/6
$major > 0 and $major < 10 and $minor > -1 and $minor < 10 and $patch > -1 and $patch < 100
$major > 0 $major < 10 $minor > -1 $minor < 10 $patch > -1 $patch < 100
Truth table0%0/7
A and B and C and D and E and F
A= $major > 0B= $major < 10C= $minor > -1D= $minor < 10E= $patch > -1F= $patch < 100
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
95 0 my $version = ($major * 1000000) + ($minor * 1000) + $patch;
96
97 0 ▶ unless ($version >= SOFTHSM_VERSION_MIN and $version <= SOFTHSM_VERSION_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version >= 1003000 and $version <= 1003003) { } 0 0 0
Condition0%0/3
$version >= 1003000 and $version <= 1003003
!ll&&!rl&&r
000
MC/DC0%0/2
$version >= 1003000 and $version <= 1003003
$version >= 1003000 $version <= 1003003
Truth table0%0/3
A and B
A= $version >= 1003000B= $version <= 1003003
ABresult
0X0
100
111
98 0 $self->{logger}->warn('Unsupported "softhsm" executable version, unable to continue');
99 }
100 else {
101 0 $self->{bin}->{softhsm} = $version;
102 0 $self->{version}->{softhsm} = $major.'.'.$minor.'.'.$patch;
103 }
104 }
105 else {
106 0 $self->{logger}->warn('Invalid "softhsm" version, module functions limited');
107 }
108 }
109 else {
110 0 $self->{logger}->warn('Unable to get "softhsm" version, module functions limited');
111 }
112 }
113 }
114  
115 =item Destroy
116  
117 =cut
118  
119 0 ▶sub Destroy {
Subroutine Destroy: not called CC=1 SCAR=0.0
Pod documented
120 }
121  
122 =item $server->_ScanConfig
123  
124 Scan for SoftHSM configuration files and return a hash reference.
125  
126 $hash_ref->{<full path file name>} = {
127 name => <full path file name>,
128 write => <true if writable>,
129 read => <true if readable>
130 };
131  
132 =cut
133  
134 0 ▶sub _ScanConfig {
Subroutine _ScanConfig: not called CC=7 SCAR=40.3
135 0 my ($self) = @_;
136 0 my %file;
137
138 0 foreach my $config (keys %ConfigFiles) {
139 0 foreach my $file (@{$ConfigFiles{$config}}) {
140 0 ▶ if (defined ($_ = Lim::Util::FileWritable($file))) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined($_ = Lim::Util::FileWritable($file))) { } 0 0 0
elsif (defined($_ = Lim::Util::FileReadable($file))) { } 0 0 0
141 0 ▶ if (exists $file{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $file{$_}) 0 0 0
142 0 $file{$_}->{write} = 1;
143 0 next;
144 }
145
146 0 $file{$_} = {
147 name => $_,
148 write => 1,
149 read => 1
150 };
151 }
152 elsif (defined ($_ = Lim::Util::FileReadable($file))) {
153 0 ▶ if (exists $file{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $file{$_}) 0 0 0
154 0 next;
155 }
156
157 0 $file{$_} = {
158 name => $_,
159 write => 0,
160 read => 1
161 };
162 }
163 }
164 }
165
166 0 return \%file;
167 }
168  
169 =back
170  
171 =head1 METHODS
172  
173 These methods are called from the Lim framework and should not be used else
174 where.
175  
176 Please see L<Lim::Plugin::SoftHSM> for full documentation of calls.
177  
178 =over 4
179  
180 =item $server->ReadVersion(...)
181  
182 Get the version of the plugin and version of SoftHSM found.
183  
184 =cut
185  
186 0 ▶sub ReadVersion {
Subroutine ReadVersion: not called CC=3 SCAR=24.8
Pod documented
187 0 my ($self, $cb) = @_;
188 0 my @program;
189
190 0 ▶ if ($self->{version}->{softhsm}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"version"}{"softhsm"}) 0 0 0
191 0 push(@program, { name => 'softhsm', version => $self->{version}->{softhsm} });
192 }
193  
194 0 ▶ if (scalar @program) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @program) { } 0 0 0
195 0 $self->Successful($cb, { version => $VERSION, program => \@program });
196 }
197 else {
198 0 $self->Successful($cb, { version => $VERSION });
199 }
200 }
201  
202 =item $server->ReadConfigs(...)
203  
204 Get a list of all config files that can be managed by this plugin.
205  
206 =cut
207  
208 0 ▶sub ReadConfigs {
Subroutine ReadConfigs: not called CC=1 SCAR=6.9
Pod documented
209 0 my ($self, $cb) = @_;
210 0 my $files = $self->_ScanConfig;
211
212 0 $self->Successful($cb, {
213 file => [ values %$files ]
214 });
215 }
216  
217 =item $server->CreateConfig(...)
218  
219 Create a new config file.
220  
221 =cut
222  
223 0 ▶sub CreateConfig {
Subroutine CreateConfig: not called CC=1 SCAR=6.9
Pod documented
224 0 my ($self, $cb) = @_;
225
226 0 $self->Error($cb, 'Not Implemented');
227 }
228  
229 =item $server->ReadConfig(...)
230  
231 Returns a config file as a content.
232  
233 =cut
234  
235 0 ▶sub ReadConfig {
Subroutine ReadConfig: not called CC=11 SCAR=48.8
Pod documented
236 0 my ($self, $cb, $q) = @_;
237 0 my $files = $self->_ScanConfig;
238 0 my $result = {};
239  
240 0 ▶ foreach my $read (ref($q->{file}) eq 'ARRAY' ? @{$q->{file}} : $q->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
ref $q->{'file'} eq 'ARRAY' ? : 0 0 0
241 0 ▶ unless (exists $files->{$read->{name}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$files{$read->{"name"}}) 0 0 0
242 $self->Error($cb, Lim::Error->new(
243 code => 500,
244 0 message => 'File "'.$read->{name}.'" not found in configuration files'
245 ));
246 0 return;
247 }
248 }
249
250 0 ▶ foreach my $read (ref($q->{file}) eq 'ARRAY' ? @{$q->{file}} : $q->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
ref $q->{'file'} eq 'ARRAY' ? : 0 0 0
251 0 my $file = $files->{$read->{name}};
252
253 0 ▶ if ($file->{read} and defined (my $fh = IO::File->new($file->{name}))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->{"read"} and defined(my $fh = "IO::File"->new($file->{"name"}))) 0 0 0
Condition0%0/3
$file->{"read"} and defined(my $fh = "IO::File"->new($file->{"name"}))
!ll&&!rl&&r
000
MC/DC0%0/2
$file->{"read"} and defined(my $fh = "IO::File"->new($file->{"name"}))
$file->{"read"} defined(my $fh = "IO::File"->new($file->{"name"}))
Truth table0%0/3
A and B
A= $file->{"read"}B= defined(my $fh = "IO::File"->new($file->{"name"}))
ABresult
0X0
100
111
254 0 my ($tell, $content);
255 0 $fh->seek(0, SEEK_END);
256 0 $tell = $fh->tell;
257 0 $fh->seek(0, SEEK_SET);
258 0 ▶ if ($fh->read($content, $tell) == $tell) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh->read($content, $tell) == $tell) 0 0 0
259 0 ▶ if (exists $result->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $result->{'file'}) { } 0 0 0
260 0 ▶ unless (ref($result->{file}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $result->{"file"} eq "ARRAY") 0 0 0
261 0 $result->{file} = [ $result->{file} ];
262 }
263 0 push(@{$result->{file}}, {
264 name => $file->{name},
265 0 content => $content
266 });
267 }
268 else {
269 $result->{file} = {
270 name => $file->{name},
271 0 content => $content
272 };
273 }
274 }
275 }
276 }
277 0 $self->Successful($cb, $result);
278 }
279  
280 =item $server->UpdateConfig(...)
281  
282 Update a config file, this will overwrite the file.
283  
284 =cut
285  
286 0 ▶sub UpdateConfig {
Subroutine UpdateConfig: not called CC=12 SCAR=50.5
Pod documented
287 0 my ($self, $cb, $q) = @_;
288 0 my $files = $self->_ScanConfig;
289 0 my $result = {};
290  
291 0 ▶ foreach my $read (ref($q->{file}) eq 'ARRAY' ? @{$q->{file}} : $q->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
ref $q->{'file'} eq 'ARRAY' ? : 0 0 0
292 0 ▶ unless (exists $files->{$read->{name}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$files{$read->{"name"}}) 0 0 0
293 $self->Error($cb, Lim::Error->new(
294 code => 500,
295 0 message => 'File "'.$read->{name}.'" not found in configuration files'
296 ));
297 0 return;
298 }
299 }
300  
301 0 ▶ foreach my $read (ref($q->{file}) eq 'ARRAY' ? @{$q->{file}} : $q->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
ref $q->{'file'} eq 'ARRAY' ? : 0 0 0
302 0 my $file = $files->{$read->{name}};
303  
304 0 ▶ if ($file->{write} and defined (my $tmp = Lim::Util::TempFileLikeThis($file->{name}))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->{"write"} and defined(my $tmp = Lim::Util::TempFileLikeThis($file->{"name"}))) 0 0 0
Condition0%0/3
$file->{"write"} and defined(my $tmp = Lim::Util::TempFileLikeThis($file->{"name"}))
!ll&&!rl&&r
000
MC/DC0%0/2
$file->{"write"} and defined(my $tmp = Lim::Util::TempFileLikeThis($file->{"name"}))
$file->{"write"} defined(my $tmp = Lim::Util::TempFileLikeThis($file->{"name"}))
Truth table0%0/3
A and B
A= $file->{"write"}B= defined(my $tmp = Lim::Util::TempFileLikeThis($file->{"name"}))
ABresult
0X0
100
111
305 0 print $tmp $read->{content};
306 0 $tmp->flush;
307 0 $tmp->close;
308
309 0 my $fh = IO::File->new;
310 0 ▶ if ($fh->open($tmp->filename)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh->open($tmp->filename)) 0 0 0
311 0 my ($tell, $content);
312 0 $fh->seek(0, SEEK_END);
313 0 $tell = $fh->tell;
314 0 $fh->seek(0, SEEK_SET);
315 0 ▶ unless ($fh->read($content, $tell) == $tell) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->read($content, $tell) == $tell) 0 0 0
316 $self->Error($cb, Lim::Error->new(
317 code => 500,
318 0 message => 'Failed to write "'.$read->{name}.'" to temporary file'
319 ));
320 0 return;
321 }
322 0 ▶ unless (Digest::SHA::sha1_base64($read->{content}) eq Digest::SHA::sha1_base64($content)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Digest::SHA::sha1_base64($read->{"content"}) eq &Digest::SHA::sha1_base64($content)) 0 0 0
323 $self->Error($cb, Lim::Error->new(
324 code => 500,
325 0 message => 'Checksum missmatch on "'.$read->{name}.'" after writing to temporary file'
326 ));
327 0 return;
328 }
329 0 ▶ unless (rename($tmp->filename, $file->{name}))
Branch: 1 branch
BranchTrueFalseTotal
unless (rename $tmp->filename, $file->{"name"}) 0 0 0
330 {
331 $self->Error($cb, Lim::Error->new(
332 code => 500,
333 0 message => 'Failed to rename "'.$read->{name}.'"'
334 ));
335 0 return;
336 }
337 }
338 }
339 }
340 0 $self->Successful($cb);
341 }
342  
343 =item $server->DeleteConfig(...)
344  
345 Delete a config file.
346  
347 =cut
348  
349 0 ▶sub DeleteConfig {
Subroutine DeleteConfig: not called CC=1 SCAR=6.9
Pod documented
350 0 my ($self, $cb) = @_;
351
352 0 $self->Error($cb, 'Not Implemented');
353 }
354  
355 =item $server->ReadShowSlots(...)
356  
357 Get a list of all SoftHSM slots that are available.
358  
359 =cut
360  
361 0 ▶sub ReadShowSlots {
Subroutine ReadShowSlots: not called CC=2 SCAR=17.9
Pod documented
362 0 my ($self, $cb) = @_;
363
364 0 ▶ unless ($self->{bin}->{softhsm}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"bin"}{"softhsm"}) 0 0 0
365 0 $self->Error($cb, 'No "softhsm" executable found or unsupported version, unable to continue');
366 0 return;
367 }
368
369 0 my ($stderr, @slots, $slot, $data);
370 Lim::Util::run_cmd
371 [
372 'softhsm',
373 '--show-slots'
374 ],
375 '<', '/dev/null',
376 0 ▶ '>', sub {
Subroutine __ANON__: not called CC=13 SCAR=52.0
377 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 0 0
378 0 $data .= $_[0];
379
380 0 $cb->reset_timeout;
381
382 0 ▶ while ($data =~ s/^([^\r\n]*)\r?\n//o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ s/^([^\r\n]*)\r?\n//ou) 0 0 0
383 0 my $line = $1;
384
385 0 ▶ if ($line =~ /^Slot\s+([0-9]+)/o) {
Branch: 6 branches
BranchTrueFalseTotal
if ($line =~ /^Slot\s+([0-9]+)/ou) { } 0 0 0
elsif (not defined $slot) { } 0 0 0
elsif ($line =~ /Token\s+present:\s+(\w+)/ou) { } 0 0 0
elsif ($line =~ /Token\s+initialized:\s+(\w+)/ou) { } 0 0 0
elsif ($line =~ /User\s+PIN\s+initialized:\s+(\w+)/ou) { } 0 0 0
elsif ($line =~ /Token\s+label:\s+(\w+)/ou) { } 0 0 0
386 0 ▶ if (defined $slot) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $slot) 0 0 0
387 0 push(@slots, $slot);
388 }
389 $slot = {
390 0 id => $1
391 };
392 }
393 elsif (!defined $slot) {
394 0 next;
395 }
396 elsif ($line =~ /Token\s+present:\s+(\w+)/o) {
397 0 ▶ if (lc($1) eq 'yes') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $1 eq 'yes') { } 0 0 0
398 0 $slot->{token_present} = 1;
399 }
400 else {
401 0 $slot->{token_present} = 0;
402 }
403 }
404 elsif ($line =~ /Token\s+initialized:\s+(\w+)/o) {
405 0 ▶ if (lc($1) eq 'yes') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $1 eq 'yes') { } 0 0 0
406 0 $slot->{token_initialized} = 1;
407 }
408 else {
409 0 $slot->{token_initialized} = 0;
410 }
411 }
412 elsif ($line =~ /User\s+PIN\s+initialized:\s+(\w+)/o) {
413 0 ▶ if (lc($1) eq 'yes') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $1 eq 'yes') { } 0 0 0
414 0 $slot->{user_pin_initialized} = 1;
415 }
416 else {
417 0 $slot->{user_pin_initialized} = 0;
418 }
419 }
420 elsif ($line =~ /Token\s+label:\s+(\w+)/o) {
421 # TODO spaces in token label??
422 0 $slot->{token_label} = $1;
423 }
424 }
425 }
426 },
427 '2>', \$stderr,
428 timeout => 15,
429 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
430 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
431 0 return;
432 }
433 0 ▶ if (shift->recv) {
Branch: 1 branch
BranchTrueFalseTotal
if ((shift())->recv) { } 0 0 0
434 0 $self->Error($cb, 'Unable to read slots');
435 }
436 else {
437 0 ▶ if (defined $slot) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $slot) 0 0 0
438 0 push(@slots, $slot);
439 }
440 0 ▶ if (scalar @slots == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @slots == 1) { } 0 0 0
elsif (scalar @slots) { } 0 0 0
441 0 $self->Successful($cb, { slot => $slots[0] });
442 }
443 elsif (scalar @slots) {
444 0 $self->Successful($cb, { slot => \@slots });
445 }
446 else {
447 0 $self->Successful($cb);
448 }
449 }
450 0 };
451 }
452  
453 =item $server->CreateInitToken(...)
454  
455 Initialize a slot.
456  
457 =cut
458  
459 0 ▶sub CreateInitToken {
Subroutine CreateInitToken: not called CC=4 SCAR=30.0
Pod documented
460 0 my ($self, $cb, $q) = @_;
461
462 0 ▶ unless ($self->{bin}->{softhsm}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"bin"}{"softhsm"}) 0 0 0
463 0 $self->Error($cb, 'No "softhsm" executable found or unsupported version, unable to continue');
464 0 return;
465 }
466
467 0 ▶ my @tokens = ref($q->{token}) eq 'ARRAY' ? @{$q->{token}} : ($q->{token});
Branch: 1 branch
BranchTrueFalseTotal
ref $q->{'token'} eq 'ARRAY' ? : 0 0 0
468 0 ▶ if (scalar @tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @tokens) 0 0 0
469 0 weaken($self);
470 0 ▶ my $cmd_cb; $cmd_cb = sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
471 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
472 0 undef($cmd_cb);
473 0 return;
474 }
475 0 ▶ if (my $token = shift(@tokens)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = shift @tokens) { } 0 0 0
476 0 my ($stdout, $stderr);
477 0 ▶ unless (length($token->{so_pin}) >= 4 and length($token->{so_pin}) <= 255) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $token->{"so_pin"} >= 4 and length $token->{"so_pin"} <= 255) 0 0 0
Condition0%0/3
length $token->{"so_pin"} >= 4 and length $token->{"so_pin"} <= 255
!ll&&!rl&&r
000
MC/DC0%0/2
length $token->{"so_pin"} >= 4 and length $token->{"so_pin"} <= 255
length $token->{"so_pin"} >= 4 length $token->{"so_pin"} <= 255
Truth table0%0/3
A and B
A= length $token->{"so_pin"} >= 4B= length $token->{"so_pin"} <= 255
ABresult
0X0
100
111
478 0 $self->Error($cb, 'Unable to create token ', $token->{label}, ': so_pin not between 4 and 255 characters');
479 0 undef($cmd_cb);
480 0 return;
481 }
482 0 ▶ unless (length($token->{pin}) >= 4 and length($token->{pin}) <= 255) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $token->{"pin"} >= 4 and length $token->{"pin"} <= 255) 0 0 0
Condition0%0/3
length $token->{"pin"} >= 4 and length $token->{"pin"} <= 255
!ll&&!rl&&r
000
MC/DC0%0/2
length $token->{"pin"} >= 4 and length $token->{"pin"} <= 255
length $token->{"pin"} >= 4 length $token->{"pin"} <= 255
Truth table0%0/3
A and B
A= length $token->{"pin"} >= 4B= length $token->{"pin"} <= 255
ABresult
0X0
100
111
483 0 $self->Error($cb, 'Unable to create token ', $token->{label}, ': pin not between 4 and 255 characters');
484 0 undef($cmd_cb);
485 0 return;
486 }
487 Lim::Util::run_cmd
488 [
489 'softhsm',
490 '--init-token',
491 '--slot', $token->{slot},
492 '--label', $token->{label},
493 '--so-pin', $token->{so_pin},
494 '--pin', $token->{pin}
495 ],
496 '<', '/dev/null',
497 0 ▶ '>', sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
498 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 0 0
499 0 $cb->reset_timeout;
500 0 $stdout .= $_[0];
501 }
502 },
503 '2>', \$stderr,
504 timeout => 10,
505 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
506 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
507 0 undef($cmd_cb);
508 0 return;
509 }
510 0 ▶ if (shift->recv) {
Branch: 1 branch
BranchTrueFalseTotal
if ((shift())->recv) 0 0 0
511 0 $self->Error($cb, 'Unable to create token ', $token->{label});
512 0 undef($cmd_cb);
513 0 return;
514 }
515 0 $cmd_cb->();
516 0 };
517 }
518 else {
519 0 $self->Successful($cb);
520 0 undef($cmd_cb);
521 }
522 0 };
523 0 $cmd_cb->();
524 0 return;
525 }
526 0 $self->Successful($cb);
527 }
528  
529 =item $server->CreateImport(...)
530  
531 Import a key into a slot.
532  
533 =cut
534  
535 0 ▶sub CreateImport {
Subroutine CreateImport: not called CC=3 SCAR=24.8
Pod documented
536 0 my ($self, $cb, $q) = @_;
537
538 0 ▶ unless ($self->{bin}->{softhsm}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"bin"}{"softhsm"}) 0 0 0
539 0 $self->Error($cb, 'No "softhsm" executable found or unsupported version, unable to continue');
540 0 return;
541 }
542
543 0 ▶ my @key_pairs = ref($q->{key_pair}) eq 'ARRAY' ? @{$q->{key_pair}} : ($q->{key_pair});
Branch: 1 branch
BranchTrueFalseTotal
ref $q->{'key_pair'} eq 'ARRAY' ? : 0 0 0
544  
545 0 weaken($self);
546 0 ▶ my $cmd_cb; $cmd_cb = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
547 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
548 0 undef($cmd_cb);
549 0 return;
550 }
551 0 ▶ if (my $key_pair = shift(@key_pairs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key_pair = shift @key_pairs) { } 0 0 0
552 0 my $tmp = Lim::Util::FileWriteContent($key_pair->{content});
553 0 ▶ unless (defined $tmp) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmp) 0 0 0
554 0 $self->Error($cb, 'Unable to write content key pair id ', $key_pair->{id}, ' to a file');
555 0 undef($cmd_cb);
556 0 return;
557 }
558 0 my ($stdout, $stderr);
559 Lim::Util::run_cmd
560 [
561 'softhsm',
562 '--import', $tmp->filename,
563 '--slot', $key_pair->{slot},
564 '--pin', $key_pair->{pin},
565 '--label', $key_pair->{label},
566 '--id', $key_pair->{id},
567 (exists $key_pair->{file_pin} ? ('--file-pin', $key_pair->{file_pin}) : ())
568 ],
569 '<', '/dev/null',
570 0 ▶ '>', sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
571 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 0 0
572 0 $cb->reset_timeout;
573 0 $stdout .= $_[0];
574 }
575 },
576 '2>', \$stderr,
577 timeout => 10,
578 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
579 0 undef($tmp);
580 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
581 0 undef($cmd_cb);
582 0 return;
583 }
584 0 ▶ if (shift->recv) {
Branch: 1 branch
BranchTrueFalseTotal
if ((shift())->recv) 0 0 0
585 0 $self->Error($cb, 'Unable to import key_pair id ', $key_pair->{id});
586 0 undef($cmd_cb);
587 0 return;
588 }
589 0 $cmd_cb->();
590 0 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
exists $key_pair->{'file_pin'} ? : 0 0 0
591 }
592 else {
593 0 $self->Successful($cb);
594 0 undef($cmd_cb);
595 }
596 0 };
597 0 $cmd_cb->();
598 }
599  
600 =item $server->ReadExport(...)
601  
602 Export a key from a slot.
603  
604 =cut
605  
606 0 ▶sub ReadExport {
Subroutine ReadExport: not called CC=3 SCAR=24.8
Pod documented
607 0 my ($self, $cb, $q) = @_;
608
609 0 ▶ unless ($self->{bin}->{softhsm}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"bin"}{"softhsm"}) 0 0 0
610 0 $self->Error($cb, 'No "softhsm" executable found or unsupported version, unable to continue');
611 0 return;
612 }
613
614 0 ▶ my @key_pairs = ref($q->{key_pair}) eq 'ARRAY' ? @{$q->{key_pair}} : ($q->{key_pair});
Branch: 1 branch
BranchTrueFalseTotal
ref $q->{'key_pair'} eq 'ARRAY' ? : 0 0 0
615 0 my @exports;
616  
617 0 weaken($self);
618 0 ▶ my $cmd_cb; $cmd_cb = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
619 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
620 0 undef($cmd_cb);
621 0 return;
622 }
623 0 ▶ if (my $key_pair = shift(@key_pairs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key_pair = shift @key_pairs) { } 0 0 0
624 0 my $tmp = Lim::Util::TempFile;
625 0 my ($stdout, $stderr);
626 Lim::Util::run_cmd
627 [
628 'softhsm',
629 '--export', $tmp->filename,
630 '--slot', $key_pair->{slot},
631 '--pin', $key_pair->{pin},
632 '--id', $key_pair->{id},
633 (exists $key_pair->{file_pin} ? ('--file-pin', $key_pair->{file_pin}) : ())
634 ],
635 '<', '/dev/null',
636 0 ▶ '>', sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
637 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 0 0
638 0 $cb->reset_timeout;
639 0 $stdout .= $_[0];
640 }
641 },
642 '2>', \$stderr,
643 timeout => 10,
644 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
645 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
646 0 undef($cmd_cb);
647 0 return;
648 }
649 0 ▶ if (shift->recv) {
Branch: 2 branches
BranchTrueFalseTotal
if ((shift())->recv) { } 0 0 0
elsif (defined(my $content = Lim::Util::FileReadContent($tmp->filename))) { } 0 0 0
650 0 $self->Error($cb, 'Unable to export key_pair id ', $key_pair->{id});
651 0 undef($cmd_cb);
652 }
653 elsif (defined (my $content = Lim::Util::FileReadContent($tmp->filename))) {
654 push(@exports, {
655 id => $key_pair->{id},
656 0 content => $content
657 });
658 0 $cmd_cb->();
659 }
660 else {
661 0 $self->Error($cb, 'Unable to read export key_pair id ', $key_pair->{id}, ' file ', $tmp->filename);
662 0 undef($cmd_cb);
663 }
664 0 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
exists $key_pair->{'file_pin'} ? : 0 0 0
665 }
666 else {
667 0 ▶ if (scalar @exports == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @exports == 1) { } 0 0 0
elsif (scalar @exports) { } 0 0 0
668 0 $self->Successful($cb, { key_pair => $exports[0] });
669 }
670 elsif (scalar @exports) {
671 0 $self->Successful($cb, { key_pair => \@exports });
672 }
673 else {
674 0 $self->Successful($cb);
675 }
676 0 undef($cmd_cb);
677 }
678 0 };
679 0 $cmd_cb->();
680 }
681  
682 =item $server->UpdateOptimize(...)
683  
684 Optimize the SoftHSM database.
685  
686 =cut
687  
688 0 ▶sub UpdateOptimize {
Subroutine UpdateOptimize: not called CC=3 SCAR=24.8
Pod documented
689 0 my ($self, $cb, $q) = @_;
690
691 0 ▶ unless ($self->{bin}->{softhsm}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"bin"}{"softhsm"}) 0 0 0
692 0 $self->Error($cb, 'No "softhsm" executable found or unsupported version, unable to continue');
693 0 return;
694 }
695
696 0 ▶ my @slots = ref($q->{slot}) eq 'ARRAY' ? @{$q->{slot}} : ($q->{slot});
Branch: 1 branch
BranchTrueFalseTotal
ref $q->{'slot'} eq 'ARRAY' ? : 0 0 0
697  
698 0 weaken($self);
699 0 ▶ my $cmd_cb; $cmd_cb = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
700 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
701 0 undef($cmd_cb);
702 0 return;
703 }
704 0 ▶ if (my $slot = shift(@slots)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $slot = shift @slots) { } 0 0 0
705 0 my ($stdout, $stderr);
706 Lim::Util::run_cmd
707 [
708 'softhsm',
709 '--optimize',
710 '--slot', $slot->{id},
711 '--pin', $slot->{pin}
712 ],
713 '<', '/dev/null',
714 0 ▶ '>', sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
715 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 0 0
716 0 $cb->reset_timeout;
717 0 $stdout .= $_[0];
718 }
719 },
720 '2>', \$stderr,
721 timeout => 10,
722 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
723 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
724 0 undef($cmd_cb);
725 0 return;
726 }
727 0 ▶ if (shift->recv) {
Branch: 1 branch
BranchTrueFalseTotal
if ((shift())->recv) 0 0 0
728 0 $self->Error($cb, 'Unable to optimize softhsm');
729 0 undef($cmd_cb);
730 0 return;
731 }
732 0 $cmd_cb->();
733 0 };
734 }
735 else {
736 0 $self->Successful($cb);
737 0 undef($cmd_cb);
738 }
739 0 };
740 0 $cmd_cb->();
741 }
742  
743 =item $server->UpdateTrusted(...)
744  
745 Update the trusted status of a key.
746  
747 =cut
748  
749 0 ▶sub UpdateTrusted {
Subroutine UpdateTrusted: not called CC=3 SCAR=24.8
Pod documented
750 0 my ($self, $cb, $q) = @_;
751
752 0 ▶ unless ($self->{bin}->{softhsm}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"bin"}{"softhsm"}) 0 0 0
753 0 $self->Error($cb, 'No "softhsm" executable found or unsupported version, unable to continue');
754 0 return;
755 }
756
757 0 ▶ my @key_pairs = ref($q->{key_pair}) eq 'ARRAY' ? @{$q->{key_pair}} : ($q->{key_pair});
Branch: 1 branch
BranchTrueFalseTotal
ref $q->{'key_pair'} eq 'ARRAY' ? : 0 0 0
758  
759 0 weaken($self);
760 0 ▶ my $cmd_cb; $cmd_cb = sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
761 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
762 0 undef($cmd_cb);
763 0 return;
764 }
765 0 ▶ if (my $key_pair = shift(@key_pairs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key_pair = shift @key_pairs) { } 0 0 0
766 0 ▶ unless (exists $key_pair->{id} or exists $key_pair->{label}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $key_pair->{"id"} or exists $key_pair->{"label"}) 0 0 0
Condition0%0/3
exists $key_pair->{"id"} or exists $key_pair->{"label"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $key_pair->{"id"} or exists $key_pair->{"label"}
exists $key_pair->{"id"} exists $key_pair->{"label"}
Truth table0%0/3
A or B
A= exists $key_pair->{"id"}B= exists $key_pair->{"label"}
ABresult
1X1
011
000
767 0 $self->Error($cb, 'Unable to mark key pair trusted, no id or label given');
768 0 undef($cmd_cb);
769 0 return;
770 }
771 0 ▶ if (exists $key_pair->{id} and exists $key_pair->{label}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $key_pair->{"id"} and exists $key_pair->{"label"}) 0 0 0
Condition0%0/3
exists $key_pair->{"id"} and exists $key_pair->{"label"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $key_pair->{"id"} and exists $key_pair->{"label"}
exists $key_pair->{"id"} exists $key_pair->{"label"}
Truth table0%0/3
A and B
A= exists $key_pair->{"id"}B= exists $key_pair->{"label"}
ABresult
0X0
100
111
772 0 $self->Error($cb, 'Unable to mark key pair trusted, both id and label given');
773 0 undef($cmd_cb);
774 0 return;
775 }
776
777 0 my ($stdout, $stderr);
778 Lim::Util::run_cmd
779 [
780 'softhsm',
781 '--trusted', $key_pair->{trusted} ? 'true' : 'false',
782 '--slot', $key_pair->{slot},
783 '--so-pin', $key_pair->{so_pin},
784 '--type', $key_pair->{type},
785 (exists $key_pair->{id} ? ('--id', $key_pair->{id}) : ()),
786 (exists $key_pair->{label} ? ('--label', $key_pair->{label}) : ())
787 ],
788 '<', '/dev/null',
789 0 ▶ '>', sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
790 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 0 0
791 0 $cb->reset_timeout;
792 0 $stdout .= $_[0];
793 }
794 },
795 '2>', \$stderr,
796 timeout => 10,
797 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
798 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
799 0 undef($cmd_cb);
800 0 return;
801 }
802 0 ▶ if (shift->recv) {
Branch: 1 branch
BranchTrueFalseTotal
if ((shift())->recv) 0 0 0
803 $self->Error($cb, 'Unable to mark key pair ',
804 (exists $key_pair->{id} ? ('id ', $key_pair->{id}) : ()),
805 (exists $key_pair->{label} ? ('label ', $key_pair->{label}) : ()),
806 ' trusted ',
807 0 ▶ $key_pair->{trusted} ? 'true' : 'false');
Branch: 3 branches
BranchTrueFalseTotal
exists $key_pair->{'id'} ? : 0 0 0
exists $key_pair->{'label'} ? : 0 0 0
$key_pair->{'trusted'} ? : 0 0 0
808 0 undef($cmd_cb);
809 0 return;
810 }
811 0 $cmd_cb->();
812 0 ▶ };
Branch: 3 branches
BranchTrueFalseTotal
$key_pair->{'trusted'} ? : 0 0 0
exists $key_pair->{'id'} ? : 0 0 0
exists $key_pair->{'label'} ? : 0 0 0
813 }
814 else {
815 0 $self->Successful($cb);
816 0 undef($cmd_cb);
817 }
818 0 };
819 0 $cmd_cb->();
820 }
821  
822 =back
823  
824 =head1 AUTHOR
825  
826 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
827  
828 =head1 BUGS
829  
830 Please report any bugs or feature requests to L<https://github.com/jelu/lim-plugin-softhsm/issues>.
831  
832 =head1 SUPPORT
833  
834 You can find documentation for this module with the perldoc command.
835  
836 perldoc Lim::Plugin::SoftHSM
837  
838 You can also look for information at:
839  
840 =over 4
841  
842 =item * Lim issue tracker (report bugs here)
843  
844 L<https://github.com/jelu/lim-plugin-softhsm/issues>
845  
846 =back
847  
848 =head1 ACKNOWLEDGEMENTS
849  
850 =head1 LICENSE AND COPYRIGHT
851  
852 Copyright 2012-2013 Jerry Lundström.
853  
854 This program is free software; you can redistribute it and/or modify it
855 under the terms of either: the GNU General Public License as published
856 by the Free Software Foundation; or the Artistic License.
857  
858 See http://dev.perl.org/licenses/ for more information.
859  
860  
861 =cut
862  
863 1 1; # End of Lim::Plugin::SoftHSM::Server
↑ Summary SoftHSM/CLI.pm »