765eba24d629094210fb6a3f756465ee5b702538
[silc.git] / lib / silcclient / client_connect.c
1 /*
2
3   client_st_connect.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2006 - 2007 Pekka Riikonen
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; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #include "silc.h"
21 #include "silcclient.h"
22 #include "client_internal.h"
23
24 /************************ Static utility functions **************************/
25
26 /* Callback called after connected to remote host */
27
28 static void silc_client_connect_callback(SilcNetStatus status,
29                                          SilcStream stream, void *context)
30 {
31   SilcFSMThread fsm = context;
32   SilcClientConnection conn = silc_fsm_get_context(fsm);
33   SilcClient client = conn->client;
34
35   conn->internal->op = NULL;
36   if (conn->internal->verbose) {
37     switch (status) {
38     case SILC_NET_OK:
39       break;
40     case SILC_NET_UNKNOWN_IP:
41       client->internal->ops->say(
42                    client, conn, SILC_CLIENT_MESSAGE_ERROR,
43                    "Could not connect to host %s: unknown IP address",
44                    conn->remote_host);
45       break;
46     case SILC_NET_UNKNOWN_HOST:
47       client->internal->ops->say(
48                    client, conn, SILC_CLIENT_MESSAGE_ERROR,
49                    "Could not connect to host %s: unknown host name",
50                    conn->remote_host);
51       break;
52     case SILC_NET_HOST_UNREACHABLE:
53       client->internal->ops->say(
54                    client, conn, SILC_CLIENT_MESSAGE_ERROR,
55                    "Could not connect to host %s: network unreachable",
56                    conn->remote_host);
57       break;
58     case SILC_NET_CONNECTION_REFUSED:
59       client->internal->ops->say(
60                    client, conn, SILC_CLIENT_MESSAGE_ERROR,
61                    "Could not connect to host %s: connection refused",
62                    conn->remote_host);
63       break;
64     case SILC_NET_CONNECTION_TIMEOUT:
65       client->internal->ops->say(
66                    client, conn, SILC_CLIENT_MESSAGE_ERROR,
67                    "Could not connect to host %s: connection timeout",
68                    conn->remote_host);
69       break;
70     default:
71       client->internal->ops->say(
72                    client, conn, SILC_CLIENT_MESSAGE_ERROR,
73                    "Could not connect to host %s",
74                    conn->remote_host);
75       break;
76     }
77   }
78
79   if (status != SILC_NET_OK) {
80     /* Notify application of failure */
81     SILC_LOG_DEBUG(("Connecting failed"));
82     conn->internal->status = SILC_CLIENT_CONN_ERROR;
83     silc_fsm_next(fsm, silc_client_st_connect_error);
84     SILC_FSM_CALL_CONTINUE(fsm);
85     return;
86   }
87
88   /* Connection created successfully */
89   SILC_LOG_DEBUG(("Connected"));
90   conn->internal->user_stream = stream;
91   SILC_FSM_CALL_CONTINUE(fsm);
92 }
93
94 /* Called after application has verified remote host's public key */
95
96 static void silc_client_ke_verify_key_cb(SilcBool success, void *context)
97 {
98   SilcVerifyKeyContext verify = context;
99
100   SILC_LOG_DEBUG(("Start"));
101
102   /* Call the completion callback back to the SKE */
103   verify->completion(verify->ske, success ? SILC_SKE_STATUS_OK :
104                      SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY,
105                      verify->completion_context);
106
107   silc_free(verify);
108 }
109
110 /* Verify remote host's public key */
111
112 static void silc_client_ke_verify_key(SilcSKE ske,
113                                       SilcPublicKey public_key,
114                                       void *context,
115                                       SilcSKEVerifyCbCompletion completion,
116                                       void *completion_context)
117 {
118   SilcFSMThread fsm = context;
119   SilcClientConnection conn = silc_fsm_get_context(fsm);
120   SilcClient client = conn->client;
121   SilcVerifyKeyContext verify;
122
123   /* If we provided repository for SKE and we got here the key was not
124      found from the repository. */
125   if (conn->internal->params.repository &&
126       !conn->internal->params.verify_notfound) {
127     completion(ske, SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY,
128                completion_context);
129     return;
130   }
131
132   SILC_LOG_DEBUG(("Verify remote public key"));
133
134   verify = silc_calloc(1, sizeof(*verify));
135   if (!verify) {
136     completion(ske, SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY,
137                completion_context);
138     return;
139   }
140   verify->ske = ske;
141   verify->completion = completion;
142   verify->completion_context = completion_context;
143
144   /* Verify public key in application */
145   client->internal->ops->verify_public_key(client, conn,
146                                            conn->type, public_key,
147                                            silc_client_ke_verify_key_cb,
148                                            verify);
149 }
150
151 /* Key exchange protocol completion callback */
152
153 static void silc_client_ke_completion(SilcSKE ske,
154                                       SilcSKEStatus status,
155                                       SilcSKESecurityProperties prop,
156                                       SilcSKEKeyMaterial keymat,
157                                       SilcSKERekeyMaterial rekey,
158                                       void *context)
159 {
160   SilcFSMThread fsm = context;
161   SilcClientConnection conn = silc_fsm_get_context(fsm);
162   SilcClient client = conn->client;
163   SilcCipher send_key, receive_key;
164   SilcHmac hmac_send, hmac_receive;
165
166   conn->internal->op = NULL;
167   if (status != SILC_SKE_STATUS_OK) {
168     /* Key exchange failed */
169     SILC_LOG_DEBUG(("Error during key exchange with %s: %s (%d)",
170                     conn->remote_host, silc_ske_map_status(status), status));
171
172     if (conn->internal->verbose)
173       client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
174                                  "Error during key exchange with %s: %s",
175                                  conn->remote_host,
176                                  silc_ske_map_status(status));
177
178     conn->internal->status = SILC_CLIENT_CONN_ERROR_KE;
179     conn->internal->error = status;
180     silc_ske_free_rekey_material(rekey);
181
182     silc_fsm_next(fsm, silc_client_st_connect_error);
183     SILC_FSM_CALL_CONTINUE(fsm);
184     return;
185   }
186
187   SILC_LOG_DEBUG(("Setting keys into use"));
188
189   /* Allocate the cipher and HMAC contexts */
190   if (!silc_ske_set_keys(ske, keymat, prop, &send_key, &receive_key,
191                          &hmac_send, &hmac_receive, &conn->internal->hash)) {
192     /* Error setting keys */
193     SILC_LOG_DEBUG(("Could not set keys into use"));
194
195     if (conn->internal->verbose)
196       client->internal->ops->say(
197                        client, conn, SILC_CLIENT_MESSAGE_ERROR,
198                        "Error during key exchange with %s: cannot use keys",
199                        conn->remote_host);
200
201     conn->internal->status = SILC_CLIENT_CONN_ERROR_KE;
202     silc_ske_free_rekey_material(rekey);
203
204     silc_fsm_next(fsm, silc_client_st_connect_error);
205     SILC_FSM_CALL_CONTINUE(fsm);
206     return;
207   }
208
209   /* Set the keys into the packet stream.  After this call packets will be
210      encrypted with these keys. */
211   if (!silc_packet_set_keys(conn->stream, send_key, receive_key, hmac_send,
212                             hmac_receive, FALSE)) {
213     /* Error setting keys */
214     SILC_LOG_DEBUG(("Could not set keys into use"));
215
216     if (conn->internal->verbose)
217       client->internal->ops->say(
218                        client, conn, SILC_CLIENT_MESSAGE_ERROR,
219                        "Error during key exchange with %s: cannot use keys",
220                        conn->remote_host);
221
222     conn->internal->status = SILC_CLIENT_CONN_ERROR_KE;
223     silc_ske_free_rekey_material(rekey);
224
225     silc_fsm_next(fsm, silc_client_st_connect_error);
226     SILC_FSM_CALL_CONTINUE(fsm);
227     return;
228   }
229
230   conn->internal->rekey = rekey;
231
232   SILC_LOG_DEBUG(("Key Exchange completed"));
233
234   /* Key exchange done */
235   SILC_FSM_CALL_CONTINUE_SYNC(fsm);
236 }
237
238 /* Rekey protocol completion callback */
239
240 static void silc_client_rekey_completion(SilcSKE ske,
241                                          SilcSKEStatus status,
242                                          SilcSKESecurityProperties prop,
243                                          SilcSKEKeyMaterial keymat,
244                                          SilcSKERekeyMaterial rekey,
245                                          void *context)
246 {
247   SilcFSMThread fsm = context;
248   SilcClientConnection conn = silc_fsm_get_context(fsm);
249   SilcClient client = conn->client;
250
251   conn->internal->op = NULL;
252   if (status != SILC_SKE_STATUS_OK) {
253     /* Rekey failed */
254     SILC_LOG_DEBUG(("Error during rekey with %s: %s (%d)",
255                     conn->remote_host, silc_ske_map_status(status), status));
256
257     if (conn->internal->verbose)
258       client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
259                                  "Error during rekey with %s: %s",
260                                  conn->remote_host,
261                                  silc_ske_map_status(status));
262
263     silc_fsm_finish(fsm);
264     return;
265   }
266
267   silc_ske_free_rekey_material(conn->internal->rekey);
268   conn->internal->rekey = rekey;
269
270   silc_ske_free(conn->internal->ske);
271   conn->internal->ske = NULL;
272
273   SILC_LOG_DEBUG(("Rekey completed conn %p", conn));
274
275   /* Rekey done */
276   silc_fsm_finish(fsm);
277 }
278
279 /* Callback called by application to return authentication data */
280
281 static void silc_client_connect_auth_method(SilcAuthMethod auth_meth,
282                                             void *auth, SilcUInt32 auth_len,
283                                             void *context)
284 {
285   SilcFSMThread fsm = context;
286   SilcClientConnection conn = silc_fsm_get_context(fsm);
287
288   conn->internal->params.auth_method = auth_meth;
289   conn->internal->params.auth = auth;
290   conn->internal->params.auth_len = auth_len;
291
292   SILC_FSM_CALL_CONTINUE(fsm);
293 }
294
295 /* Connection authentication completion callback */
296
297 static void silc_client_connect_auth_completion(SilcConnAuth connauth,
298                                                 SilcBool success,
299                                                 void *context)
300 {
301   SilcFSMThread fsm = context;
302   SilcClientConnection conn = silc_fsm_get_context(fsm);
303   SilcClient client = conn->client;
304
305   conn->internal->op = NULL;
306   silc_connauth_free(connauth);
307
308   if (!success) {
309     if (conn->internal->verbose)
310         client->internal->ops->say(
311                         client, conn, SILC_CLIENT_MESSAGE_ERROR,
312                         "Authentication failed");
313
314     conn->internal->status = SILC_CLIENT_CONN_ERROR_AUTH;
315     conn->internal->error = SILC_STATUS_ERR_AUTH_FAILED;
316     silc_fsm_next(fsm, silc_client_st_connect_error);
317   }
318
319   SILC_FSM_CALL_CONTINUE_SYNC(fsm);
320 }
321
322 /********************** CONNECTION_AUTH_REQUEST packet **********************/
323
324 /* Received connection authentication request packet.  We get the
325    required authentication method here. */
326
327 SILC_FSM_STATE(silc_client_connect_auth_request)
328 {
329   SilcClientConnection conn = fsm_context;
330   SilcPacket packet = state_context;
331   SilcUInt16 conn_type, auth_meth;
332
333   if (!conn->internal->auth_request) {
334     silc_packet_free(packet);
335     return SILC_FSM_FINISH;
336   }
337
338   /* Parse the payload */
339   if (silc_buffer_unformat(&packet->buffer,
340                            SILC_STR_UI_SHORT(&conn_type),
341                            SILC_STR_UI_SHORT(&auth_meth),
342                            SILC_STR_END) < 0)
343     auth_meth = SILC_AUTH_NONE;
344
345   silc_packet_free(packet);
346
347   SILC_LOG_DEBUG(("Resolved authentication method: %s",
348                   (auth_meth == SILC_AUTH_NONE ? "none" :
349                    auth_meth == SILC_AUTH_PASSWORD ? "passphrase" :
350                    "public key")));
351   conn->internal->params.auth_method = auth_meth;
352
353   /* Continue authentication */
354   silc_fsm_continue_sync(&conn->internal->event_thread);
355   return SILC_FSM_FINISH;
356 }
357
358 /*************************** Connect remote host ****************************/
359
360 /* Connection timeout callback */
361
362 SILC_TASK_CALLBACK(silc_client_connect_timeout)
363 {
364   SilcClientConnection conn = context;
365
366   SILC_LOG_DEBUG(("Connection timeout"));
367
368   conn->internal->status = SILC_CLIENT_CONN_ERROR_TIMEOUT;
369   conn->internal->error = SILC_STATUS_ERR_TIMEDOUT;
370
371   silc_fsm_next(&conn->internal->event_thread, silc_client_st_connect_error);
372   silc_fsm_continue_sync(&conn->internal->event_thread);
373 }
374
375 /* Creates a connection to remote host */
376
377 SILC_FSM_STATE(silc_client_st_connect)
378 {
379   SilcClientConnection conn = fsm_context;
380
381   SILC_LOG_DEBUG(("Connecting to %s:%d", conn->remote_host,
382                   conn->remote_port));
383
384   /** Connect */
385   silc_fsm_next(fsm, silc_client_st_connect_set_stream);
386
387   /* Add connection timeout */
388   if (conn->internal->params.timeout_secs)
389     silc_schedule_task_add_timeout(conn->internal->schedule,
390                                    silc_client_connect_timeout, conn,
391                                    conn->internal->params.timeout_secs, 0);
392
393   if (conn->internal->params.udp) {
394     SilcStream stream;
395
396     if (!conn->internal->params.local_ip) {
397       /** IP address not given */
398       SILC_LOG_ERROR(("Local UDP IP address not specified"));
399       conn->internal->status = SILC_CLIENT_CONN_ERROR;
400       silc_fsm_next(fsm, silc_client_st_connect_error);
401       return SILC_FSM_CONTINUE;
402     }
403
404     /* Connect (UDP) */
405     stream = silc_net_udp_connect(conn->internal->params.bind_ip ?
406                                   conn->internal->params.bind_ip :
407                                   conn->internal->params.local_ip,
408                                   conn->internal->params.local_port,
409                                   conn->remote_host, conn->remote_port,
410                                   conn->internal->schedule);
411
412     SILC_FSM_CALL(silc_client_connect_callback(stream ? SILC_NET_OK :
413                                                SILC_NET_HOST_UNREACHABLE,
414                                                stream, fsm));
415   } else {
416     /* Connect (TCP) */
417     SILC_FSM_CALL(conn->internal->op = silc_net_tcp_connect(
418                                        NULL, conn->remote_host,
419                                        conn->remote_port,
420                                        conn->internal->schedule,
421                                        silc_client_connect_callback, fsm));
422   }
423 }
424
425 /* Sets the new connection stream into use and creates packet stream */
426
427 SILC_FSM_STATE(silc_client_st_connect_set_stream)
428 {
429   SilcClientConnection conn = fsm_context;
430   SilcClient client = conn->client;
431
432   if (conn->internal->disconnected) {
433     /** Disconnected */
434     silc_fsm_next(fsm, silc_client_st_connect_error);
435     return SILC_FSM_CONTINUE;
436   }
437
438   /* Create packet stream */
439   conn->stream = silc_packet_stream_create(client->internal->packet_engine,
440                                            conn->internal->schedule,
441                                            conn->internal->user_stream);
442   if (!conn->stream) {
443     /** Cannot create packet stream */
444     SILC_LOG_DEBUG(("Could not create packet stream"));
445     conn->internal->status = SILC_CLIENT_CONN_ERROR;
446     silc_fsm_next(fsm, silc_client_st_connect_error);
447     return SILC_FSM_CONTINUE;
448   }
449
450   silc_packet_set_context(conn->stream, conn);
451
452   /** Start key exchange */
453   silc_fsm_next(fsm, silc_client_st_connect_key_exchange);
454   return SILC_FSM_CONTINUE;
455 }
456
457 /* Starts key exchange protocol with remote host */
458
459 SILC_FSM_STATE(silc_client_st_connect_key_exchange)
460 {
461   SilcClientConnection conn = fsm_context;
462   SilcClient client = conn->client;
463   SilcSKEParamsStruct params;
464
465   SILC_LOG_DEBUG(("Starting key exchange protocol"));
466
467   /* Allocate SKE */
468   conn->internal->ske =
469     silc_ske_alloc(client->rng, conn->internal->schedule,
470                    conn->internal->params.repository,
471                    conn->public_key, conn->private_key, fsm);
472   if (!conn->internal->ske) {
473     /** Out of memory */
474     conn->internal->status = SILC_CLIENT_CONN_ERROR_KE;
475     silc_fsm_next(fsm, silc_client_st_connect_error);
476     return SILC_FSM_CONTINUE;
477   }
478
479   /* Set SKE callbacks */
480   silc_ske_set_callbacks(conn->internal->ske, silc_client_ke_verify_key,
481                          silc_client_ke_completion, fsm);
482
483   /* Set up key exchange parameters */
484   params.version = client->internal->silc_client_version;
485   params.timeout_secs = conn->internal->params.timeout_secs;
486   params.flags = SILC_SKE_SP_FLAG_MUTUAL;
487   if (conn->internal->params.pfs)
488     params.flags |= SILC_SKE_SP_FLAG_PFS;
489   if (conn->internal->params.udp) {
490     params.flags |= SILC_SKE_SP_FLAG_IV_INCLUDED;
491     params.session_port = conn->internal->params.local_port;
492   }
493
494   if (conn->internal->params.no_authentication)
495     /** Run key exchange (no auth) */
496     silc_fsm_next(fsm, silc_client_st_connected);
497   else if (conn->internal->params.udp)
498     /** Run key exchange (UDP)*/
499     silc_fsm_next(fsm, silc_client_st_connect_setup_udp);
500   else
501     /** Run key exchange (TCP) */
502     silc_fsm_next(fsm, silc_client_st_connect_auth_resolve);
503
504   SILC_FSM_CALL(conn->internal->op = silc_ske_initiator(conn->internal->ske,
505                                                         conn->stream,
506                                                         &params, NULL));
507 }
508
509 /* For UDP/IP connections, set up the UDP session after successful key
510    exchange protocol */
511
512 SILC_FSM_STATE(silc_client_st_connect_setup_udp)
513 {
514   SilcClientConnection conn = fsm_context;
515   SilcStream stream, old;
516   SilcSKESecurityProperties prop;
517
518   SILC_LOG_DEBUG(("Setup UDP SILC session"));
519
520   if (conn->internal->disconnected) {
521     /** Disconnected */
522     silc_fsm_next(fsm, silc_client_st_connect_error);
523     return SILC_FSM_CONTINUE;
524   }
525
526   /* Create new UDP stream */
527   prop = silc_ske_get_security_properties(conn->internal->ske);
528   stream = silc_net_udp_connect(conn->internal->params.local_ip,
529                                 conn->internal->params.local_port,
530                                 conn->remote_host, prop->remote_port,
531                                 conn->internal->schedule);
532   if (!stream) {
533     /** Cannot create UDP stream */
534     conn->internal->status = SILC_CLIENT_CONN_ERROR;
535     silc_fsm_next(fsm, silc_client_st_connect_error);
536     return SILC_FSM_CONTINUE;
537   }
538
539   /* Set the new stream to packet stream */
540   old = silc_packet_stream_get_stream(conn->stream);
541   silc_packet_stream_set_stream(conn->stream, stream);
542   silc_packet_stream_set_iv_included(conn->stream);
543   silc_packet_set_sid(conn->stream, 0);
544
545   /* Delete the old stream */
546   silc_stream_destroy(old);
547
548   /** Start authentication */
549   silc_fsm_next(fsm, silc_client_st_connect_auth_resolve);
550   return SILC_FSM_CONTINUE;
551 }
552
553 /* Resolve authentication method to be used in authentication protocol */
554
555 SILC_FSM_STATE(silc_client_st_connect_auth_resolve)
556 {
557   SilcClientConnection conn = fsm_context;
558
559   SILC_LOG_DEBUG(("Resolve authentication method"));
560
561   if (conn->internal->disconnected) {
562     /** Disconnected */
563     silc_fsm_next(fsm, silc_client_st_connect_error);
564     return SILC_FSM_CONTINUE;
565   }
566
567   /* If authentication method and data is set, use them */
568   if (conn->internal->params.auth_set) {
569     /** Got authentication data */
570     silc_fsm_next(fsm, silc_client_st_connect_auth_start);
571     return SILC_FSM_CONTINUE;
572   }
573
574   /* Send connection authentication request packet */
575   silc_packet_send_va(conn->stream,
576                       SILC_PACKET_CONNECTION_AUTH_REQUEST, 0,
577                       SILC_STR_UI_SHORT(SILC_CONN_CLIENT),
578                       SILC_STR_UI_SHORT(SILC_AUTH_NONE),
579                       SILC_STR_END);
580
581   /** Wait for authentication method */
582   conn->internal->auth_request = TRUE;
583   conn->internal->params.auth_method = SILC_AUTH_NONE;
584   silc_fsm_next_later(fsm, silc_client_st_connect_auth_data, 2, 0);
585   return SILC_FSM_WAIT;
586 }
587
588 /* Get authentication data to be used in authentication protocol */
589
590 SILC_FSM_STATE(silc_client_st_connect_auth_data)
591 {
592   SilcClientConnection conn = fsm_context;
593   SilcClient client = conn->client;
594
595   SILC_LOG_DEBUG(("Get authentication data"));
596
597   if (conn->internal->disconnected) {
598     /** Disconnected */
599     silc_fsm_next(fsm, silc_client_st_connect_error);
600     return SILC_FSM_CONTINUE;
601   }
602
603   conn->internal->auth_request = FALSE;
604
605   /** Get authentication data */
606   silc_fsm_next(fsm, silc_client_st_connect_auth_start);
607   SILC_FSM_CALL(client->internal->ops->get_auth_method(
608                                     client, conn,
609                                     conn->remote_host,
610                                     conn->remote_port,
611                                     conn->internal->params.auth_method,
612                                     silc_client_connect_auth_method, fsm));
613 }
614
615 /* Start connection authentication with remote host */
616
617 SILC_FSM_STATE(silc_client_st_connect_auth_start)
618 {
619   SilcClientConnection conn = fsm_context;
620   SilcConnAuth connauth;
621
622   SILC_LOG_DEBUG(("Starting connection authentication protocol"));
623
624   if (conn->internal->disconnected) {
625     /** Disconnected */
626     silc_fsm_next(fsm, silc_client_st_connect_error);
627     return SILC_FSM_CONTINUE;
628   }
629
630   /* We always use the same key for connection authentication and SKE */
631   if (conn->internal->params.auth_method == SILC_AUTH_PUBLIC_KEY)
632     conn->internal->params.auth = conn->private_key;
633
634   /* Allocate connection authentication protocol */
635   connauth = silc_connauth_alloc(conn->internal->schedule,
636                                  conn->internal->ske,
637                                  conn->internal->params.rekey_secs);
638   if (!connauth) {
639     /** Out of memory */
640     conn->internal->status = SILC_CLIENT_CONN_ERROR_AUTH;
641     conn->internal->error = SILC_STATUS_ERR_AUTH_FAILED;
642     silc_fsm_next(fsm, silc_client_st_connect_error);
643     return SILC_FSM_CONTINUE;
644   }
645
646   /** Start connection authentication */
647   silc_fsm_next(fsm, silc_client_st_connected);
648   SILC_FSM_CALL(conn->internal->op = silc_connauth_initiator(
649                                         connauth, SILC_CONN_CLIENT,
650                                         conn->internal->params.auth_method,
651                                         conn->internal->params.auth,
652                                         conn->internal->params.auth_len,
653                                         silc_client_connect_auth_completion,
654                                         fsm));
655 }
656
657 /* Connection fully established */
658
659 SILC_FSM_STATE(silc_client_st_connected)
660 {
661   SilcClientConnection conn = fsm_context;
662   SilcClient client = conn->client;
663
664   /* Get SILC protocol version remote supports */
665   silc_ske_parse_version(conn->internal->ske, &conn->internal->remote_version,
666                          NULL, NULL, NULL, NULL);
667
668   silc_ske_free(conn->internal->ske);
669   conn->internal->ske = NULL;
670
671   if (conn->internal->disconnected) {
672     /** Disconnected */
673     silc_fsm_next(fsm, silc_client_st_connect_error);
674     return SILC_FSM_CONTINUE;
675   }
676
677   SILC_LOG_DEBUG(("Connection established"));
678
679   /* Install rekey timer */
680   if (conn->type != SILC_CONN_CLIENT)
681     silc_schedule_task_add_timeout(conn->internal->schedule,
682                                    silc_client_rekey_timer, conn,
683                                    conn->internal->params.rekey_secs, 0);
684
685   /* If we connected to server, now register to network. */
686   if (conn->type == SILC_CONN_SERVER &&
687       !conn->internal->params.no_authentication) {
688
689     /* If detach data is provided, resume the session. */
690     if (conn->internal->params.detach_data &&
691         conn->internal->params.detach_data_len) {
692       /** Resume detached session */
693       silc_fsm_next(fsm, silc_client_st_resume);
694     } else {
695       /** Register to network */
696       silc_fsm_next(fsm, silc_client_st_register);
697     }
698
699     return SILC_FSM_CONTINUE;
700   }
701
702   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
703                                 silc_client_connect_timeout, conn);
704
705   /* Call connection callback */
706   conn->callback(client, conn, SILC_CLIENT_CONN_SUCCESS, 0, NULL,
707                  conn->callback_context);
708
709   silc_async_free(conn->internal->cop);
710   conn->internal->cop = NULL;
711
712   return SILC_FSM_FINISH;
713 }
714
715 /* Error during connecting */
716
717 SILC_FSM_STATE(silc_client_st_connect_error)
718 {
719   SilcClientConnection conn = fsm_context;
720
721   if (conn->internal->ske) {
722     silc_ske_free(conn->internal->ske);
723     conn->internal->ske = NULL;
724   }
725
726   /* Signal to close connection */
727   if (!conn->internal->disconnected) {
728     conn->internal->disconnected = TRUE;
729     SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
730   }
731
732   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
733                                 silc_client_connect_timeout, conn);
734
735   return SILC_FSM_FINISH;
736 }
737
738 /****************************** Connect rekey *******************************/
739
740 /* Connection rekey timer callback */
741
742 SILC_TASK_CALLBACK(silc_client_rekey_timer)
743 {
744   SilcClientConnection conn = context;
745
746   /* Signal to start rekey */
747   if (!silc_fsm_is_started(&conn->internal->event_thread)) {
748     conn->internal->rekey_responder = FALSE;
749     conn->internal->rekeying = TRUE;
750     SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
751   }
752
753   /* Reinstall rekey timer */
754   silc_schedule_task_add_timeout(conn->internal->schedule,
755                                  silc_client_rekey_timer, conn,
756                                  conn->internal->params.rekey_secs, 0);
757 }
758
759 /* Performs rekey */
760
761 SILC_FSM_STATE(silc_client_st_rekey)
762 {
763   SilcClientConnection conn = fsm_context;
764   SilcClient client = conn->client;
765
766   SILC_LOG_DEBUG(("Rekey conn %p", conn));
767
768   if (conn->internal->disconnected)
769     return SILC_FSM_FINISH;
770
771   /* Allocate SKE */
772   conn->internal->ske =
773     silc_ske_alloc(client->rng, conn->internal->schedule,
774                    conn->internal->params.repository,
775                    conn->public_key, conn->private_key, fsm);
776   if (!conn->internal->ske)
777     return SILC_FSM_FINISH;
778
779   /* Set SKE callbacks */
780   silc_ske_set_callbacks(conn->internal->ske, NULL,
781                          silc_client_rekey_completion, fsm);
782
783   /** Perform rekey */
784   if (!conn->internal->rekey_responder)
785     SILC_FSM_CALL(conn->internal->op = silc_ske_rekey_initiator(
786                                                     conn->internal->ske,
787                                                     conn->stream,
788                                                     conn->internal->rekey));
789   else
790     SILC_FSM_CALL(conn->internal->op = silc_ske_rekey_responder(
791                                                     conn->internal->ske,
792                                                     conn->stream,
793                                                     conn->internal->rekey));
794 }