Debian Codegraph @

gpgkeys_hkpms

raw
1 #!/usr/bin/perl -w
2
3 # hkpms transport -- HKP-over-TLS, authenticated by monkeysphere
4
5 use strict;
6 use warnings;
7
8
9
10 # Author: Daniel Kahn Gillmor <[email protected]>
11 # Copyright: 2010
12 # License: GPL v3+
13 # (you should have received a COPYING file with this distribution)
14
15
16
17
18 { package Crypt::Monkeysphere::MSVA::HKPMS;
19 use POSIX;
20 use Crypt::Monkeysphere::Logger;
21 use Crypt::Monkeysphere::MSVA::Client;
22 use Regexp::Common qw /net/;
23 use Module::Load::Conditional;
24
25 sub parse_input {
26 my $self = shift;
27 my $input = shift;
28
29 my $inheaders = 1;
30 foreach my $line (split(/\n/, $input)) {
31 if ($inheaders) {
32 if ($line eq '') {
33 $inheaders = 0;
34 } else {
35 next if ($line =~ /^#/);
36 my @args = split(/ /, $line);
37 my $cmd = shift @args;
38 $self->{config}->{lc($cmd)} = join(' ', @args);
39 if (lc($cmd) eq 'option') {
40 my $opt = lc($args[0]);
41 if ($opt eq 'debug') {
42 $self->{logger}->set_log_level('debug');
43 } elsif ($opt eq 'verbose') {
44 $self->{logger}->more_verbose();
45 } elsif ($opt eq 'no-check-cert') {
46 $self->{logger}->log('error', "Received no-check-cert option. Why are you bothering with hkpms if you aren't checking?\n");
47 $self->{actually_check} = 0;
48 } elsif ($opt eq 'check-cert') {
49 $self->{actually_check} = 1;
50 } elsif ($opt =~ /^http-proxy=(.*)/) {
51 my $hp = $1;
52 if ($hp =~ /^(socks|http|https):\/\/($RE{net}{domain}|$RE{net}{IPv4}):([[:digit:]]+)\/?$/) {
53 if ('socks' eq $1) {
54 if ( ! Module::Load::Conditional::check_install(module => 'LWP::Protocol::socks')) {
55 $self->{logger}->log('error', "Requesting a socks proxy for hkpms, but LWP::Protocol::socks is not installed.\nThis will likely fail.\n");
56 }
57 }
58 $self->{proxy} = sprintf('%s://%s:%s', $1, $2, $3);
59 } else {
60 $self->{logger}->log('error', "Failed to make sense of this http-proxy address: '%s'; ignoring.\n", $hp);
61 }
62 } else {
63 $self->{logger}->log('error', "Received '%s' as an option, but gpgkeys_hkpms does not implement it. Ignoring...\n", $opt);
64 }
65 # FIXME: consider other keyserver-options from gpg(1).
66 # in particular, the following might be interesting:
67 # timeout
68 # include-revoked
69 # include-disabled
70 # ca-cert-file
71 }
72 }
73 } else {
74 push(@{$self->{args}}, $line);
75 }
76 }
77 }
78
79 sub verify_cert {
80 my $self = shift;
81 my ($ok, $ctxstore, $certname, $error, $cert) = @_;
82 my $certpem = Net::SSLeay::PEM_get_string_X509($cert);
83 my ($status, $ret);
84
85 if (exists $self->{cache}->{$certpem}) {
86 ($status, $ret) = @{$self->{cache}->{$certpem}};
87 $self->{logger}->log('debug', "Found response in cache\n");
88 } else {
89 # use Crypt::Monkeysphere::MSVA::Client if available:
90 if (defined($self->{client})) {
91 # because we really don't want to create some sort of MSVA loop:
92 ($status, $ret) = $self->{client}->query_agent('https', $self->{config}->{host}, 'server', 'x509pem', $certpem, 'never');
93 } else {
94 use Crypt::Monkeysphere::MSVA;
95 $self->{logger}->log('verbose', "Could not find a running agent (MONKEYSPHERE_VALIDATION_AGENT_SOCKET env var).\nFalling back to in-process certificate checks.\n");
96 # If there is no running agent, we might want to be able to fall
97 # back here.
98
99 # FIXME: this is hackery! we're just calling daemon-internal code
100 # (and it's not a stable API):
101
102 my $data = {peer => { name => $self->{config}->{host}, type => 'server' },
103 context => 'https',
104 pkc => { type => 'x509pem', data => $certpem },
105 keyserverpolicy => 'never', # because we really don't want to create some sort of MSVA loop
106 };
107
108 my $clientinfo = { uid => POSIX::geteuid(), inode => undef };
109
110 ($status, $ret) = Crypt::Monkeysphere::MSVA::reviewcert($data, $clientinfo);
111 }
112
113 # make a cache of the cert if it verifies once, since this seems
114 # to get called 3 times by perl for some reason. (see
115 # https://bugs.debian.org/606249)
116 $self->{cache}->{$certpem} = [ $status, $ret ];
117 if (JSON::is_bool($ret->{valid}) && ($ret->{valid} eq 1)) {
118 $self->{logger}->log('verbose', "Monkeysphere HKPMS Certificate validation succeeded:\n %s\n", $ret->{message});
119 } else {
120 my $m = '[undefined]';
121 $m = $ret->{message} if (defined($ret->{message}));
122 $self->{logger}->log('error', "Monkeysphere HKPMS Certificate validation failed:\n %s\n", $m);
123 }
124 }
125
126 if (JSON::is_bool($ret->{valid}) && ($ret->{valid} eq 1)) {
127 return 1;
128 } else {
129 return 0;
130 }
131 }
132
133 sub query {
134 my $self = shift;
135
136 # FIXME: i'd like to pass this debug argument to IO::Socket::SSL,
137 # but i don't know how to do that.
138 # i get 'Variable "@iosslargs" will not stay shared' if i try to call
139 # use IO::Socket::SSL 1.37 @iosslargs;
140 my @iosslargs = ();
141 if ($self->{logger}->get_log_level() >= 4) {
142 push @iosslargs, sprintf("debug%d", int($self->{logger}->get_log_level() - 3));
143 }
144
145 # versions earlier than 1.35 can fail open: bad news!.
146 # 1.37 lets us set ca_path and ca_file to undef, which is what we want.
147 use IO::Socket::SSL 1.37;
148 use Net::SSLeay;
149 use LWP::UserAgent;
150 use URI;
151
152 IO::Socket::SSL::set_ctx_defaults(
153 verify_callback => sub { $self->verify_cert(@_); },
154 verify_mode => 0x03,
155 ca_path => undef,
156 ca_file => undef,
157 );
158
159 my $ua = LWP::UserAgent::->new();
160
161 if (exists($self->{proxy})) {
162 $self->{logger}->log('verbose', "Using http-proxy: %s\n", $self->{proxy});
163 $ua->proxy([qw(http https)] => $self->{proxy});
164 } else {
165 # if no proxy was explicitly set, use the environment:
166 $ua->env_proxy();
167 }
168
169 printf("VERSION 1\nPROGRAM %s gpgkeys_hkpms msva-perl/%s\n",
170 $self->{config}->{program}, # this is kind of cheating :/
171 $Crypt::Monkeysphere::MSVA::VERSION);
172
173
174 $self->{logger}->log('debug', "command: %s\n", $self->{config}->{command});
175 if (lc($self->{config}->{command}) eq 'search') {
176 # for COMMAND = SEARCH, we want op=index, and we want to rejoin all args with spaces.
177 my $uri = URI::->new(sprintf('https://%s/pks/lookup', $self->{config}->{host}));
178 my $arg = join(' ', @{$self->{args}});
179 $uri->query_form(op => 'index',
180 options => 'mr',
181 search => $arg,
182 );
183 $arg =~ s/\n/ /g ; # swap out newlines for spaces
184 printf("\n%s %s BEGIN\n", $self->{config}->{command}, $arg);
185 $self->{logger}->log('debug', "URI: %s\n", $uri);
186 my $resp = $ua->get($uri);
187 if ($resp->is_success) {
188 print($resp->decoded_content);
189 } else {
190 # FIXME: handle errors better
191 $self->{logger}->log('error', "HTTPS error: %s\n", $resp->status_line);
192 }
193 printf("\n%s %s END\n", $self->{config}->{command}, $arg);
194 } elsif (lc($self->{config}->{command}) eq 'get') {
195 # for COMMAND = GET, we want op=get, and we want to issue each query separately.
196 my $uri = URI::->new(sprintf('https://%s/pks/lookup', $self->{config}->{host}));
197 foreach my $arg (@{$self->{args}}) {
198 printf("\n%s %s BEGIN\n", $self->{config}->{command}, $arg);
199 $uri->query_form(op => 'get',
200 options => 'mr',
201 search => $arg,
202 );
203 my $resp = $ua->get($uri);
204 if ($resp->is_success) {
205 print($resp->decoded_content);
206 } else {
207 # FIXME: handle errors better
208 $self->{logger}->log('error', "HTTPS error: %s\n", $resp->status_line);
209 }
210 printf("\n%s %s END\n", $self->{config}->{command}, $arg);
211 }
212 } elsif (lc($self->{config}->{command}) eq 'send') {
213 $self->{logger}->log('debug', "Sending keys");
214 # walk the input looking for "KEY E403BC1A17856FB7 BEGIN" lines.
215 my @keydata;
216 my $keyid;
217 foreach my $arg (@{$self->{args}}) {
218 if ($arg =~ /^KEY ([a-fA-F0-9]+) BEGIN\s*$/) {
219 @keydata = ();
220 $keyid = $1;
221 $self->{logger}->log('debug', "Found KEY BEGIN line (%s)\n", $keyid);
222 } elsif (defined($keyid)) {
223 if ($arg eq sprintf('KEY %s END', $keyid)) {
224 $self->{logger}->log('debug', "Found KEY END line with %d lines of data elapsed\n", scalar(@keydata));
225 # for sending keys, we want to POST to /pks/add, with a keytext variable.
226 my $uri = URI::->new(sprintf('https://%s/pks/add', $self->{config}->{host}));
227 my $resp = $ua->post($uri, {keytext => join("\n", @keydata)});
228 if ($resp->is_success) {
229 printf("\n%s", $resp->decoded_content);
230 } else {
231 # FIXME: handle errors better
232 $self->{logger}->log('error', "HTTPS error: %s\n", $resp->status_line);
233 }
234 printf("\nKEY %s SENT\n", $keyid);
235 @keydata = ();
236 $keyid = undef;
237 } else {
238 push @keydata, $arg;
239 }
240 } else {
241 $self->{logger}->log('debug2', "Found garbage line\n");
242 }
243 }
244 if (defined($keyid)) {
245 $self->{logger}->log('error', "Never got a 'KEY %s END' line, discarding.\n", $keyid);
246 }
247 } else {
248 # are there other commands we might want?
249 $self->{logger}->log('error', "Unknown command %s\n", $self->{config}->{command});
250 }
251 }
252
253
254 sub new {
255 my $class = shift;
256
257 my $default_log_level = 'error';
258 my $client;
259 if (exists($ENV{MONKEYSPHERE_VALIDATION_AGENT_SOCKET})) {
260 $client = Crypt::Monkeysphere::MSVA::Client::->new(
261 socket => $ENV{MONKEYSPHERE_VALIDATION_AGENT_SOCKET},
262 log_level => $default_log_level,
263 );
264 }
265 my $self = { config => { },
266 args => [ ],
267 logger => (defined($client) ? $client->{logger} : Crypt::Monkeysphere::Logger::->new($default_log_level)),
268 cache => { },
269 client => $client,
270 actually_check => 1,
271 };
272
273 bless ($self, $class);
274 return $self;
275 }
276 1;
277 }
278
279
280 my $hkpms = Crypt::Monkeysphere::MSVA::HKPMS::->new();
281
282 my $input = # load gpg instructions from stdin:
283 do {
284 local $/; # slurp!
285 <STDIN>;
286 };
287
288
289 $hkpms->parse_input($input);
290 $hkpms->query();
291