… | |
… | |
130 | |
130 | |
131 | { |
131 | { |
132 | Scalar::Util::weaken (my $self = $self); |
132 | Scalar::Util::weaken (my $self = $self); |
133 | |
133 | |
134 | if (exists $arg{connect}) { |
134 | if (exists $arg{connect}) { |
135 | $arg{tls} ||= "connect"; |
|
|
136 | $arg{tls_ctx} ||= { sslv2 => 0, sslv3 => 0, tlsv1 => 1, verify => 1 }; |
135 | $arg{tls_ctx} ||= { sslv2 => 0, sslv3 => 0, tlsv1 => 1, verify => 1 }; |
137 | } |
136 | } |
138 | |
137 | |
139 | $arg{secret} = AnyEvent::MP::Base::default_secret () |
138 | $arg{secret} = AnyEvent::MP::Base::default_secret () |
140 | unless exists $arg{secret}; |
139 | unless exists $arg{secret}; |
… | |
… | |
150 | peername => delete $arg{peername}, |
149 | peername => delete $arg{peername}, |
151 | ; |
150 | ; |
152 | |
151 | |
153 | my $secret = $arg{secret}; |
152 | my $secret = $arg{secret}; |
154 | my $greeting_kv = $self->{greeting} ||= {}; |
153 | my $greeting_kv = $self->{greeting} ||= {}; |
155 | $greeting_kv->{"tls1.0"} ||= $arg{tls} |
154 | $greeting_kv->{"tls"} = "1.0" |
156 | if exists $arg{tls} && $arg{tls_ctx}; |
155 | if $arg{tls_ctx}; |
157 | $greeting_kv->{provider} = "AE-$VERSION"; |
156 | $greeting_kv->{provider} = "AE-$VERSION"; |
158 | $greeting_kv->{peeraddr} = AnyEvent::Socket::format_hostport $self->{peerhost}, $self->{peerport}; |
157 | $greeting_kv->{peeraddr} = AnyEvent::Socket::format_hostport $self->{peerhost}, $self->{peerport}; |
159 | |
158 | |
160 | # send greeting |
159 | # send greeting |
161 | my $lgreeting1 = "aemp;$PROTOCOL_VERSION;$PROTOCOL_VERSION" # version, min |
160 | my $lgreeting1 = "aemp;$PROTOCOL_VERSION;$PROTOCOL_VERSION" # version, min |
… | |
… | |
210 | $self->{remote_greeting} = { |
209 | $self->{remote_greeting} = { |
211 | map /^([^=]+)(?:=(.*))?/ ? ($1 => $2) : (), |
210 | map /^([^=]+)(?:=(.*))?/ ? ($1 => $2) : (), |
212 | @kv |
211 | @kv |
213 | }; |
212 | }; |
214 | |
213 | |
215 | if (exists $self->{tls} and $self->{tls_ctx} and exists $self->{remote_greeting}{"tls1.0"}) { |
|
|
216 | if ($self->{tls} ne $self->{remote_greeting}{"tls1.0"}) { |
|
|
217 | return $self->error ("TLS server/client mismatch"); |
|
|
218 | } |
|
|
219 | $self->{hdl}->starttls ($self->{tls}, $self->{tls_ctx}); |
|
|
220 | } |
|
|
221 | |
|
|
222 | # read nonce |
214 | # read nonce |
223 | $self->{hdl}->push_read (line => sub { |
215 | $self->{hdl}->push_read (line => sub { |
224 | my $rgreeting2 = $_[1]; |
216 | my $rgreeting2 = $_[1]; |
225 | |
217 | |
|
|
218 | if ($self->{tls_ctx} and 1 == int $self->{remote_greeting}{"tls"}) { |
|
|
219 | $self->{tls} = $lgreeting2 lt $rgreeting2 ? "connect" : "accept"; |
|
|
220 | $self->{hdl}->starttls ($self->{tls}, $self->{tls_ctx}); |
|
|
221 | } |
|
|
222 | |
226 | # auth |
223 | # auth |
227 | require Digest::MD6; |
224 | require Digest::MD6; |
228 | require Digest::HMAC_MD6; |
225 | require Digest::HMAC_MD6; |
229 | |
226 | |
230 | my $key = Digest::MD6::md6_hex ($secret); |
227 | my $key = Digest::MD6::md6_hex ($secret); |
… | |
… | |
322 | The greeting consists of two text lines that are ended by either an ASCII |
319 | The greeting consists of two text lines that are ended by either an ASCII |
323 | CR LF pair, or a single ASCII LF (recommended). |
320 | CR LF pair, or a single ASCII LF (recommended). |
324 | |
321 | |
325 | =head2 GREETING |
322 | =head2 GREETING |
326 | |
323 | |
327 | The first line contains strings seperated (not ended) by C<;> |
324 | The first line contains strings separated (not ended) by C<;> |
328 | characters. The first seven strings are fixed by the protocol, the |
325 | characters. The first seven strings are fixed by the protocol, the |
329 | remaining strings are C<KEY=VALUE> pairs. None of them may contain C<;> |
326 | remaining strings are C<KEY=VALUE> pairs. None of them may contain C<;> |
330 | characters themselves. |
327 | characters themselves. |
331 | |
328 | |
332 | The seven fixed strings are: |
329 | The seven fixed strings are: |
… | |
… | |
369 | |
366 | |
370 | A comma-separated list of packet encoding/framign formats understood. The |
367 | A comma-separated list of packet encoding/framign formats understood. The |
371 | receiving side should choose the first framing format it supports for |
368 | receiving side should choose the first framing format it supports for |
372 | sending packets (which might be different from the format it has to accept). |
369 | sending packets (which might be different from the format it has to accept). |
373 | |
370 | |
374 | . ";$self->{peerhost};$self->{peerport}" |
371 | =cut |
375 | . (join "", map ";$_=$greeting_kv->{$_}", keys %$greeting_kv); |
372 | |
376 | my $lgreeting2 = MIME::Base64::encode_base64 AnyEvent::MP::Base::nonce (33), ""; |
373 | The remaining arguments are C<KEY=VALUE> pairs. The following key-value |
|
|
374 | pairs are known at this time: |
|
|
375 | |
|
|
376 | =over 4 |
|
|
377 | |
|
|
378 | =item provider=<module-version> |
|
|
379 | |
|
|
380 | The software provider for this implementation. For AnyEvent::MP, this is |
|
|
381 | C<AE-0.0> or whatever version it currently is at. |
|
|
382 | |
|
|
383 | =item peeraddr=<host>:<port> |
|
|
384 | |
|
|
385 | The peer address (socket address of the other side) as seen locally, in the same format |
|
|
386 | as noderef endpoints. |
|
|
387 | |
|
|
388 | =item tls=<major>.<minor> |
|
|
389 | |
|
|
390 | Indicates that the other side supports TLS (version should be 1.0) and |
|
|
391 | wishes to do a TLS handshake. |
|
|
392 | |
|
|
393 | =back |
|
|
394 | |
|
|
395 | After this greeting line there will be a second line containing a |
|
|
396 | cryptographic nonce, i.e. random data of high quality. To keep the |
|
|
397 | protocol text-only, these are usually 32 base64-encoded octets, but |
|
|
398 | it could be anything that doesn't contain any ASCII CR or ASCII LF |
|
|
399 | characters. |
|
|
400 | |
|
|
401 | Example of the two lines of greeting: |
|
|
402 | |
|
|
403 | aemp;0;0;e7d.4a76f48f;10.0.0.1:4040;hmac_md6_64_256,hex_secret;json,storable;provider=AE-0.0;peeraddr=127.0.0.1:1235 |
|
|
404 | XntegV2Guvss0qNn7phCPnoU87xqxV+4Mqm/5y4iQm6a |
|
|
405 | |
|
|
406 | =head2 TLS handshake |
|
|
407 | |
|
|
408 | If, after the handshake, both sides indicate interest in TLS, then the |
|
|
409 | connection I<must> use TLS, or fail. |
|
|
410 | |
|
|
411 | Both sides compare their nonces, and the side who sent the lower nonce |
|
|
412 | value ("string" comparison on the raw octet values) becomes the client, |
|
|
413 | and the one with the higher nonce the server. |
|
|
414 | |
|
|
415 | =head2 AUTHENTICATION PHASE |
|
|
416 | |
|
|
417 | After the greeting is received (and the optional TLS handshake), |
|
|
418 | the authentication phase begins, which consists of sending a single |
|
|
419 | C<;>-separated line with three fixed strings and any number of |
|
|
420 | C<KEY=VALUE> pairs. |
|
|
421 | |
|
|
422 | The three fixed strings are: |
|
|
423 | |
|
|
424 | =over 4 |
|
|
425 | |
|
|
426 | =item the authentication method chosen |
|
|
427 | |
|
|
428 | This must be one of the methods offered by the other side in the greeting. |
|
|
429 | |
|
|
430 | =item the authentication data |
|
|
431 | |
|
|
432 | The authentication data itself, usually base64 or hex-encoded data. |
|
|
433 | |
|
|
434 | =item the framing protocol chosen |
|
|
435 | |
|
|
436 | This must be one of the framing protocols offered by the other side in the |
|
|
437 | greeting. Each side must accept the choice of the other side. |
|
|
438 | |
|
|
439 | =back |
|
|
440 | |
|
|
441 | =head2 DATA PHASE |
|
|
442 | |
|
|
443 | After this, packets get exchanged using the chosen framing protocol. It is |
|
|
444 | quite possible that both sides use a different framing protocol. |
|
|
445 | |
377 | =head1 SEE ALSO |
446 | =head1 SEE ALSO |
378 | |
447 | |
379 | L<AnyEvent>. |
448 | L<AnyEvent>. |
380 | |
449 | |
381 | =head1 AUTHOR |
450 | =head1 AUTHOR |