File: | Dpkg/OpenPGP/Backend.pm |
Coverage: | 59.0% |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | # Copyright © 2017, 2022 Guillem Jover <guillem@debian.org> | ||||||
2 | # | ||||||
3 | # This program is free software; you can redistribute it and/or modify | ||||||
4 | # it under the terms of the GNU General Public License as published by | ||||||
5 | # the Free Software Foundation; either version 2 of the License, or | ||||||
6 | # (at your option) any later version. | ||||||
7 | # | ||||||
8 | # This program is distributed in the hope that it will be useful, | ||||||
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||
11 | # GNU General Public License for more details. | ||||||
12 | # | ||||||
13 | # You should have received a copy of the GNU General Public License | ||||||
14 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||
15 | |||||||
16 | package Dpkg::OpenPGP::Backend; | ||||||
17 | |||||||
18 | 44 44 44 | 8060 3 481 | use strict; | ||||
19 | 44 44 44 | 47 41 968 | use warnings; | ||||
20 | |||||||
21 | our $VERSION = '0.01'; | ||||||
22 | |||||||
23 | 44 44 44 | 44 44 842 | use List::Util qw(first); | ||||
24 | |||||||
25 | 44 44 44 | 88 3 730 | use Dpkg::Path qw(find_command); | ||||
26 | 44 44 44 | 47 50 9815 | use Dpkg::OpenPGP::ErrorCodes; | ||||
27 | |||||||
28 | sub DEFAULT_CMDV { | ||||||
29 | 79 | 0 | 117 | return []; | |||
30 | } | ||||||
31 | |||||||
32 | sub DEFAULT_CMDSTORE { | ||||||
33 | 79 | 0 | 79 | return []; | |||
34 | } | ||||||
35 | |||||||
36 | sub DEFAULT_CMD { | ||||||
37 | 0 | 0 | 0 | return []; | |||
38 | } | ||||||
39 | |||||||
40 | sub _detect_cmd { | ||||||
41 | 327 | 636 | my ($cmd, $default) = @_; | ||||
42 | |||||||
43 | 327 | 1172 | if (! defined $cmd || $cmd eq 'auto') { | ||||
44 | 262 104 262 | 899 1154 859 | return first { find_command($_) } @{$default}; | ||||
45 | } else { | ||||||
46 | 65 | 290 | return find_command($cmd); | ||||
47 | } | ||||||
48 | } | ||||||
49 | |||||||
50 | sub new { | ||||||
51 | 109 | 0 | 368 | my ($this, %opts) = @_; | |||
52 | 109 | 628 | my $class = ref($this) || $this; | ||||
53 | |||||||
54 | my $self = { | ||||||
55 | 109 | 581 | strict_verify => $opts{strict_verify} // 1, | ||||
56 | }; | ||||||
57 | 109 | 128 | bless $self, $class; | ||||
58 | |||||||
59 | 109 | 411 | $self->{cmdv} = _detect_cmd($opts{cmdv}, $self->DEFAULT_CMDV()); | ||||
60 | 109 | 550 | $self->{cmdstore} = _detect_cmd($opts{cmdstore}, $self->DEFAULT_CMDSTORE()); | ||||
61 | 109 | 372 | $self->{cmd} = _detect_cmd($opts{cmd}, $self->DEFAULT_CMD()); | ||||
62 | |||||||
63 | 109 | 538 | return $self; | ||||
64 | } | ||||||
65 | |||||||
66 | sub has_backend_cmd { | ||||||
67 | 90 | 0 | 90 | my $self = shift; | |||
68 | |||||||
69 | 90 | 139 | return defined $self->{cmd}; | ||||
70 | } | ||||||
71 | |||||||
72 | sub has_verify_cmd { | ||||||
73 | 3 | 0 | 3 | my $self = shift; | |||
74 | |||||||
75 | 3 | 6 | return defined $self->{cmd}; | ||||
76 | } | ||||||
77 | |||||||
78 | sub has_keystore { | ||||||
79 | 0 | 0 | 0 | my $self = shift; | |||
80 | |||||||
81 | 0 | 0 | return 0; | ||||
82 | } | ||||||
83 | |||||||
84 | sub can_use_key { | ||||||
85 | 49 | 0 | 49 | my ($self, $key) = @_; | |||
86 | |||||||
87 | 49 | 115 | return $self->has_keystore() if $key->needs_keystore(); | ||||
88 | 49 | 130 | return 1; | ||||
89 | } | ||||||
90 | |||||||
91 | sub get_trusted_keyrings { | ||||||
92 | 0 | 0 | my $self = shift; | ||||
93 | |||||||
94 | 0 | return (); | |||||
95 | } | ||||||
96 | |||||||
97 | sub armor { | ||||||
98 | 0 | 0 | my ($self, $type, $in, $out) = @_; | ||||
99 | |||||||
100 | 0 | return OPENPGP_UNSUPPORTED_SUBCMD; | |||||
101 | } | ||||||
102 | |||||||
103 | sub dearmor { | ||||||
104 | 0 | 0 | my ($self, $type, $in, $out) = @_; | ||||
105 | |||||||
106 | 0 | return OPENPGP_UNSUPPORTED_SUBCMD; | |||||
107 | } | ||||||
108 | |||||||
109 | sub inline_verify { | ||||||
110 | 0 | 0 | my ($self, $inlinesigned, $data, @certs) = @_; | ||||
111 | |||||||
112 | 0 | return OPENPGP_UNSUPPORTED_SUBCMD; | |||||
113 | } | ||||||
114 | |||||||
115 | sub verify { | ||||||
116 | 0 | 0 | my ($self, $data, $sig, @certs) = @_; | ||||
117 | |||||||
118 | 0 | return OPENPGP_UNSUPPORTED_SUBCMD; | |||||
119 | } | ||||||
120 | |||||||
121 | sub inline_sign { | ||||||
122 | 0 | 0 | my ($self, $data, $inlinesigned, $key) = @_; | ||||
123 | |||||||
124 | 0 | return OPENPGP_UNSUPPORTED_SUBCMD; | |||||
125 | } | ||||||
126 | |||||||
127 | 1; |