--- AnyEvent-MP/MP/Transport.pm 2009/08/02 14:52:41 1.6 +++ AnyEvent-MP/MP/Transport.pm 2009/08/06 10:21:48 1.25 @@ -1,6 +1,6 @@ =head1 NAME -AnyEvent::MP::Transport - actual transport protocol +AnyEvent::MP::Transport - actual transport protocol handler =head1 SYNOPSIS @@ -8,10 +8,11 @@ =head1 DESCRIPTION -This is the superclass for MP transports, most of which is considered an -implementation detail. +This implements the actual transport protocol for MP (it represents a +single link), most of which is considered an implementation detail. -Future versions might document the actual protocol. +See the "PROTOCOL" section below if you want to write another client for +this protocol. =head1 FUNCTIONS/METHODS @@ -28,6 +29,9 @@ use Storable (); use JSON::XS (); +use Digest::MD6 (); +use Digest::HMAC_MD6 (); + use AE (); use AnyEvent::Socket (); use AnyEvent::Handle (); @@ -44,7 +48,7 @@ See C, below, for constructor arguments. -Defaults for peerhost, peerport, fh and tls are provided. +Defaults for peerhost, peerport and fh are provided. =cut @@ -59,7 +63,6 @@ fh => $fh, peerhost => $host, peerport => $port, - tls => "accept", @args, ); } @@ -83,7 +86,6 @@ peername => $host, peerhost => $nhost, peerport => $nport, - tls => "accept", @args, ); } @@ -106,14 +108,18 @@ greeting => { key => value }, # tls support - tls => "accept|connect", tls_ctx => AnyEvent::TLS, peername => $peername, # for verification ; =cut -our @FRAMING_WANT = qw(json storable);#d##TODO# +our @FRAMINGS = qw(json storable); # the framing types we accept and send, in order of preference +our @AUTH_SND = qw(hmac_md6_64_256); # auth types we send +our @AUTH_RCV = (@AUTH_SND, qw(cleartext)); # auth types we accept + +#AnyEvent::Handle::register_write_type mp_record => sub { +#}; sub new { my ($class, %arg) = @_; @@ -125,59 +131,98 @@ { Scalar::Util::weaken (my $self = $self); - if (exists $arg{connect}) { - $arg{tls} ||= "connect"; - $arg{tls_ctx} ||= { sslv2 => 0, sslv3 => 0, tlsv1 => 1, verify => 1 }; - } - $arg{secret} = AnyEvent::MP::Base::default_secret () unless exists $arg{secret}; + $arg{timeout} = 30 + unless exists $arg{timeout}; + + my $keepalive = (int $arg{timeout} * 0.75) || 1; + + my $secret = $arg{secret}; + + if ($secret =~ /-----BEGIN RSA PRIVATE KEY-----.*-----END RSA PRIVATE KEY-----.*-----BEGIN CERTIFICATE-----.*-----END CERTIFICATE-----/s) { + # assume TLS mode + $arg{tls_ctx} = { + sslv2 => 0, + sslv3 => 0, + tlsv1 => 1, + verify => 1, + cert => $secret, + ca_cert => $secret, + verify_require_client_cert => 1, + }; + } + $self->{hdl} = new AnyEvent::Handle fh => delete $arg{fh}, - rbuf_max => 64 * 1024, autocork => 1, no_delay => 1, on_error => sub { $self->error ($_[2]); }, + timeout => $AnyEvent::MP::Base::CONNECT_TIMEOUT, peername => delete $arg{peername}, ; - my $secret = $arg{secret}; my $greeting_kv = $self->{greeting} ||= {}; - $greeting_kv->{"tls1.0"} ||= $arg{tls} - if exists $arg{tls} && $arg{tls_ctx}; + + $self->{local_node} = $AnyEvent::MP::Base::NODE; + + $greeting_kv->{"tls"} = "1.0" if $arg{tls_ctx}; $greeting_kv->{provider} = "AE-$VERSION"; + $greeting_kv->{peeraddr} = AnyEvent::Socket::format_hostport $self->{peerhost}, $self->{peerport}; + $greeting_kv->{maxidle} = $keepalive; # send greeting - my $lgreeting = "aemp;$PROTOCOL_VERSION;$PROTOCOL_VERSION" # version, min - . ";$AnyEvent::MP::Base::UNIQ" - . ";$AnyEvent::MP::Base::NODE" - . ";" . (MIME::Base64::encode_base64 AnyEvent::MP::Base::nonce (33), "") - . ";hmac_md6_64_256" # hardcoded atm. - . ";json" # hardcoded atm. - . ";$self->{peerhost};$self->{peerport}" - . (join "", map ";$_=$greeting_kv->{$_}", keys %$greeting_kv); + my $lgreeting1 = "aemp;$PROTOCOL_VERSION" + . ";$AnyEvent::MP::Base::UNIQ" + . ";$self->{local_node}" + . ";" . (join ",", @AUTH_RCV) + . ";" . (join ",", @FRAMINGS) + . (join "", map ";$_=$greeting_kv->{$_}", keys %$greeting_kv); - $self->{hdl}->push_write ("$lgreeting\012"); + my $lgreeting2 = MIME::Base64::encode_base64 AnyEvent::MP::Base::nonce (33), ""; + + $self->{hdl}->push_write ("$lgreeting1\012$lgreeting2\012"); # expect greeting + $self->{hdl}->rbuf_max (4 * 1024); $self->{hdl}->push_read (line => sub { - my $rgreeting = $_[1]; + my $rgreeting1 = $_[1]; - my ($aemp, $version, $version_min, $uniq, $rnode, undef, $auth, $framing, $peerport, $peerhost, @kv) = split /;/, $rgreeting; + my ($aemp, $version, $uniq, $rnode, $auths, $framings, @kv) = split /;/, $rgreeting1; if ($aemp ne "aemp") { return $self->error ("unparsable greeting"); - } elsif ($version_min > $PROTOCOL_VERSION) { - return $self->error ("version mismatch (we: $PROTOCOL_VERSION, they: $version_min .. $version)"); - } elsif ($auth ne "hmac_md6_64_256") { - return $self->error ("unsupported auth method ($auth)"); - } elsif ($framing ne "json") { - return $self->error ("unsupported framing method ($auth)"); + } elsif ($version != $PROTOCOL_VERSION) { + return $self->error ("version mismatch (we: $PROTOCOL_VERSION, they: $version)"); } + my $s_auth; + for my $auth_ (split /,/, $auths) { + if (grep $auth_ eq $_, @AUTH_SND) { + $s_auth = $auth_; + last; + } + } + + defined $s_auth + or return $self->error ("$auths: no common auth type supported"); + + die unless $s_auth eq "hmac_md6_64_256"; # hardcoded atm. + + my $s_framing; + for my $framing_ (split /,/, $framings) { + if (grep $framing_ eq $_, @FRAMINGS) { + $s_framing = $framing_; + last; + } + } + + defined $s_framing + or return $self->error ("$framings: no common framing method supported"); + $self->{remote_uniq} = $uniq; $self->{remote_node} = $rnode; @@ -186,52 +231,67 @@ @kv }; - if (exists $self->{tls} and $self->{tls_ctx} and exists $self->{remote_greeting}{"tls1.0"}) { - if ($self->{tls} ne $self->{remote_greeting}{"tls1.0"}) { - return $self->error ("TLS server/client mismatch"); + # read nonce + $self->{hdl}->push_read (line => sub { + my $rgreeting2 = $_[1]; + + "$lgreeting1\012$lgreeting2" ne "$rgreeting1\012$rgreeting2" # echo attack? + or return $self->error ("authentication error, echo attack?"); + + my $key = Digest::MD6::md6 $secret; + my $lauth; + + if ($self->{tls_ctx} and 1 == int $self->{remote_greeting}{tls}) { + $self->{tls} = $lgreeting2 lt $rgreeting2 ? "connect" : "accept"; + $self->{hdl}->starttls ($self->{tls}, $self->{tls_ctx}); + $s_auth = "tls"; + $lauth = ""; + } else { + # we currently only support hmac_md6_64_256 + $lauth = Digest::HMAC_MD6::hmac_md6_hex $key, "$lgreeting1\012$lgreeting2\012$rgreeting1\012$rgreeting2\012", 64, 256; } - $self->{hdl}->starttls ($self->{tls}, $self->{tls_ctx}); - } - # auth - require Digest::MD6; - require Digest::HMAC_MD6; + $self->{hdl}->push_write ("$s_auth;$lauth;$s_framing\012"); - my $key = Digest::MD6::md6_hex ($secret); - my $lauth = Digest::HMAC_MD6::hmac_md6_base64 ($key, "$lgreeting\012$rgreeting", 64, 256); - my $rauth = Digest::HMAC_MD6::hmac_md6_base64 ($key, "$rgreeting\012$lgreeting", 64, 256); + # read the authentication response + $self->{hdl}->push_read (line => sub { + my ($hdl, $rline) = @_; - $lauth ne $rauth # echo attack? - or return $self->error ("authentication error"); + my ($auth_method, $rauth2, $r_framing) = split /;/, $rline; - $self->{hdl}->push_write ("$auth;$lauth;$framing\012"); + my $rauth = + $auth_method eq "hmac_md6_64_256" ? Digest::HMAC_MD6::hmac_md6_hex $key, "$rgreeting1\012$rgreeting2\012$lgreeting1\012$lgreeting2\012", 64, 256 + : $auth_method eq "cleartext" ? unpack "H*", $secret + : $auth_method eq "tls" ? ($self->{tls} ? "" : "\012\012") # \012\012 never matches + : return $self->error ("$auth_method: fatal, selected unsupported auth method"); - $self->{hdl}->rbuf_max (64); # enough for 44 reply bytes or so - $self->{hdl}->push_read (line => sub { - my ($hdl, $rline) = @_; + if ($rauth2 ne $rauth) { + return $self->error ("authentication failure/shared secret mismatch"); + } - my ($auth_method, $rauth2, $r_framing) = split /;/, $rline; + $self->{s_framing} = $s_framing; - if ($rauth2 ne $rauth) { - return $self->error ("authentication failure/shared secret mismatch"); - } + $hdl->rbuf_max (undef); + my $queue = delete $self->{queue}; # we are connected - $self->{s_framing} = "json";#d# + $self->{hdl}->timeout ($self->{remote_greeting}{keepalive} + 5) + if $self->{remote_greeting}{keepalive}; - $hdl->rbuf_max (undef); - my $queue = delete $self->{queue}; # we are connected + $self->connected; - $self->connected; + my $src_node = $self->{node}; - $hdl->push_write ($self->{s_framing} => $_) - for @$queue; + $self->send ($_) + for @$queue; - my $rmsg; $rmsg = sub { - $_[0]->push_read ($r_framing => $rmsg); + my $rmsg; $rmsg = sub { + $_[0]->push_read ($r_framing => $rmsg); - AnyEvent::MP::Base::_inject ($_[1]); - }; - $hdl->push_read ($r_framing => $rmsg); + local $AnyEvent::MP::Base::SRCNODE = $src_node; + AnyEvent::MP::Base::_inject (@{ $_[1] }); + }; + $hdl->push_read ($r_framing => $rmsg); + }); }); }); } @@ -243,15 +303,31 @@ my ($self, $msg) = @_; if ($self->{node} && $self->{node}{transport} == $self) { + #TODO: store error, but do not instantly fail + $self->{node}->fail (transport_error => $self->{node}{noderef}, $msg); $self->{node}->clr_transport; } -# $self->{on_error}($self, $msg); + $AnyEvent::MP::Base::WARN->("$self->{peerhost}:$self->{peerport}: $msg"); $self->destroy; } sub connected { my ($self) = @_; + if (ref $AnyEvent::MP::Base::SLAVE) { + # first connect with a master node + my $via = $self->{remote_node}; + $via =~ s/,/!/g; + $AnyEvent::MP::Base::NODE .= "\@$via"; + $AnyEvent::MP::Base::NODE{$AnyEvent::MP::Base::NODE} = $AnyEvent::MP::Base::NODE{""}; + $AnyEvent::MP::Base::SLAVE->(); + } + + if ($self->{local_node} ne $AnyEvent::MP::Base::NODE) { + # node changed its name since first greeting + $self->send (["", iam => $AnyEvent::MP::Base::NODE]); + } + my $node = AnyEvent::MP::Base::add_node ($self->{remote_node}); Scalar::Util::weaken ($self->{node} = $node); $node->set_transport ($self); @@ -276,6 +352,222 @@ =back +=head1 PROTOCOL + +The protocol is relatively simple, and consists of three phases which are +symmetrical for both sides: greeting (followed by optionally switching to +TLS mode), authentication and packet exchange. + +the protocol is designed to allow both full-text and binary streams. + +The greeting consists of two text lines that are ended by either an ASCII +CR LF pair, or a single ASCII LF (recommended). + +=head2 GREETING + +All the lines until after authentication must not exceed 4kb in length, +including delimiter. Afterwards there is no limit on the packet size that +can be received. + +=head3 First Greeting Line + +Example: + + aemp;0;fec.4a7720fc;127.0.0.1:1235,[::1]:1235;hmac_md6_64_256;json,storable;provider=AE-0.0 + +The first line contains strings separated (not ended) by C<;> +characters. The first even ixtrings are fixed by the protocol, the +remaining strings are C pairs. None of them may contain C<;> +characters themselves. + +The fixed strings are: + +=over 4 + +=item protocol identification + +The constant C to identify the protocol. + +=item protocol version + +The protocol version supported by this end, currently C<0>. If the +versions don't match then no communication is possible. Minor extensions +are supposed to be handled through additional key-value pairs. + +=item a token uniquely identifying the current node instance + +This is a string that must change between restarts. It usually contains +things like the current time, the (OS) process id or similar values, but +no meaning of the contents are assumed. + +=item the node endpoint descriptors + +for public nodes, this is a comma-separated list of protocol endpoints, +i.e., the noderef. For slave nodes, this is a unique identifier. + +=item the acceptable authentication methods + +A comma-separated list of authentication methods supported by the +node. Note that AnyEvent::MP supports a C authentication +method that accepts a cleartext password (hex-encoded), but will not use +this auth method itself. + +The receiving side should choose the first auth method it supports. + +=item the acceptable framing formats + +A comma-separated list of packet encoding/framign formats understood. The +receiving side should choose the first framing format it supports for +sending packets (which might be different from the format it has to accept). + +=back + +The remaining arguments are C pairs. The following key-value +pairs are known at this time: + +=over 4 + +=item provider= + +The software provider for this implementation. For AnyEvent::MP, this is +C or whatever version it currently is at. + +=item peeraddr=: + +The peer address (socket address of the other side) as seen locally, in the same format +as noderef endpoints. + +=item tls=. + +Indicates that the other side supports TLS (version should be 1.0) and +wishes to do a TLS handshake. + +=item maxidle= + +The maximum amount of time the node will not sent data, i.e., idle. This +can be used to close the conenction when no data has been received for a +too-long time (say, maxidle + 5 seconds). + +=back + +=head3 Second Greeting Line + +After this greeting line there will be a second line containing a +cryptographic nonce, i.e. random data of high quality. To keep the +protocol text-only, these are usually 32 base64-encoded octets, but +it could be anything that doesn't contain any ASCII CR or ASCII LF +characters. + +I<< The two nonces B be different, and an aemp implementation +B check and fail when they are identical >>. + +Example of a nonce line: + + p/I122ql7kJR8lumW3lXlXCeBnyDAvz8NQo3x5IFowE4 + +=head2 TLS handshake + +I<< If, after the handshake, both sides indicate interest in TLS, then the +connection B use TLS, or fail. >> + +Both sides compare their nonces, and the side who sent the lower nonce +value ("string" comparison on the raw octet values) becomes the client, +and the one with the higher nonce the server. + +=head2 AUTHENTICATION PHASE + +After the greeting is received (and the optional TLS handshake), +the authentication phase begins, which consists of sending a single +C<;>-separated line with three fixed strings and any number of +C pairs. + +The three fixed strings are: + +=over 4 + +=item the authentication method chosen + +This must be one of the methods offered by the other side in the greeting. + +The currently supported authentication methods are: + +=over 4 + +=item cleartext + +This is simply the shared secret, lowercase-hex-encoded. This method is of +course very insecure, unless TLS is used, which is why this module will +accept, but not generate, cleartext auth replies. + +=item hmac_md6_64_256 + +This method uses an MD6 HMAC with 64 bit blocksize and 256 bit hash. First, the shared secret +is hashed with MD6: + + key = MD6 (secret) + +This secret is then used to generate the "local auth reply", by taking +the two local greeting lines and the two remote greeting lines (without +line endings), appending \012 to all of them, concatenating them and +calculating the MD6 HMAC with the key. + + lauth = HMAC_MD6 key, "lgreeting1\012lgreeting2\012rgreeting1\012rgreeting2\012" + +This authentication token is then lowercase-hex-encoded and sent to the +other side. + +Then the remote auth reply is generated using the same method, but local +and remote greeting lines swapped: + + rauth = HMAC_MD6 key, "rgreeting1\012rgreeting2\012lgreeting1\012lgreeting2\012" + +This is the token that is expected from the other side. + +=item tls + +This type is only valid iff TLS was enabled and the TLS handshake +was successful. It has no authentication data, as the server/client +certificate was successfully verified. + +Implementations supporting TLS I accept this authentication type. + +=back + +=item the authentication data + +The authentication data itself, usually base64 or hex-encoded data, see +above. + +=item the framing protocol chosen + +This must be one of the framing protocols offered by the other side in the +greeting. Each side must accept the choice of the other side. + +=back + +Example of an authentication reply: + + hmac_md6_64_256;363d5175df38bd9eaddd3f6ca18aa1c0c4aa22f0da245ac638d048398c26b8d3;json + +=head2 DATA PHASE + +After this, packets get exchanged using the chosen framing protocol. It is +quite possible that both sides use a different framing protocol. + +=head2 FULL EXAMPLE + +This is an actual protocol dump of a handshake, followed by a single data +packet. The greater than/less than lines indicate the direction of the +transfer only. + + > aemp;0;nndKd+gn;10.0.0.1:4040;hmac_md6_64_256,cleartext;json,storable;provider=AE-0.0;peeraddr=127.0.0.1:1235 + > sRG8bbc4TDbkpvH8FTP4HBs87OhepH6VuApoZqXXskuG + < aemp;0;nmpKd+gh;127.0.0.1:1235,[::1]:1235;hmac_md6_64_256,cleartext;json,storable;provider=AE-0.0;peeraddr=127.0.0.1:58760 + < dCEUcL/LJVSTJcx8byEsOzrwhzJYOq+L3YcopA5T6EAo + > hmac_md6_64_256;9513d4b258975accfcb2ab7532b83690e9c119a502c612203332a591c7237788;json + < hmac_md6_64_256;0298d6ba2240faabb2b2e881cf86b97d70a113ca74a87dc006f9f1e9d3010f90;json + > ["","lookup","pinger","10.0.0.1:4040#nndKd+gn.a","resolved"] + =head1 SEE ALSO L.