0d91aadd0bd8fbf77bb4efffe5be0bc753ab0007
[monkeysphere-validation-agent.git] / Crypt / Monkeysphere / MSVA / Client.pm
1 #----------------------------------------------------------------------
2 # Monkeysphere Validation Agent, Perl version
3 # Marginal User Interface for reasonable prompting
4 # Copyright © 2010 Daniel Kahn Gillmor <dkg@fifthhorseman.net>,
5 #                  Matthew James Goins <mjgoins@openflows.com>,
6 #                  Jameson Graef Rollins <jrollins@finestructure.net>,
7 #                  Elliot Winard <enw@caveteen.com>
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22 #----------------------------------------------------------------------
23
24 { package Crypt::Monkeysphere::MSVA::Client;
25
26   use strict;
27   use warnings;
28
29   BEGIN {
30     use Exporter   ();
31     our (@EXPORT_OK,@ISA);
32     @ISA = qw(Exporter);
33     @EXPORT_OK = qw( &create_apd );
34   }
35   our @EXPORT_OK;
36
37   use JSON;
38   use Crypt::Monkeysphere::MSVA qw( msvalog );
39
40   sub query_agent {
41     use LWP::UserAgent;
42     use HTTP::Request;
43
44     my $self = shift;
45     my $context = shift;
46     my $peer = shift;
47     my $pkctype = shift;
48     my $pkcdata = shift;
49     my $msvasocket = shift;
50
51     if (! defined $msvasocket or $msvasocket eq '') {
52       $msvasocket = 'http://localhost:8901';
53     }
54
55     my $apd = create_apd($context, $peer, $pkctype, $pkcdata);
56
57     my $apdjson = to_json($apd);
58
59     # create the user agent
60     my $ua = LWP::UserAgent->new;
61
62     my $headers = HTTP::Headers->new(
63         'Content-Type' => 'application/json',
64         'Content-Length' => length($apdjson),
65         'Connection' => 'close',
66         'Accept' => 'application/json',
67         );
68
69     my $requesturl = $msvasocket . '/reviewcert';
70
71     my $request = HTTP::Request->new(
72         'POST',
73         $requesturl,
74         $headers,
75         $apdjson,
76         );
77
78     msvalog('debug', "Contacting MSVA at %s\n", $requesturl);
79     my $response = $ua->request($request);
80
81     my $status = $response->status_line;
82     my $ret = from_json($response->content);
83
84     return $status, $ret;
85   }
86
87   sub create_apd {
88     my $context = shift;
89     my $peer = shift;
90     my $pkctype = shift;
91     my $pkcdata = shift;
92
93     msvalog('debug', "context: %s\n", $context);
94     msvalog('debug', "peer: %s\n", $peer);
95     msvalog('debug', "pkctype: %s\n", $pkctype);
96
97
98     if ($pkctype eq 'x509der') {
99       my $cert = Crypt::X509->new(cert => $pkcdata);
100       if ($cert->error) {
101         die;
102       };
103       msvalog('info', "x509der certificate loaded.\n");
104       msvalog('verbose', "cert subject: %s\n", $cert->subject_cn());
105       msvalog('verbose', "cert issuer: %s\n", $cert->issuer_cn());
106       msvalog('verbose', "cert pubkey algo: %s\n", $cert->PubKeyAlg());
107       msvalog('verbose', "cert pubkey: %s\n", unpack('H*', $cert->pubkey()));
108     } else {
109         msvalog('error', "unknown pkc type '%s'.\n", $pkctype);
110     };
111
112     return {
113             context => $context,
114             peer => $peer,
115             pkc => {
116                     type => $pkctype,
117                     # remap raw pkc data into numeric array
118                     data => [map(ord, split(//,$pkcdata))],
119                    },
120            };
121   }
122
123   1;
124 }