Allows formatted nicknames as input for silc_client_get_clients
[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     silc_ske_free_rekey_material(rekey);
180
181     silc_fsm_next(fsm, silc_client_st_connect_error);
182     SILC_FSM_CALL_CONTINUE_SYNC(fsm);
183     return;
184   }
185
186   SILC_LOG_DEBUG(("Setting keys into use"));
187
188   /* Allocate the cipher and HMAC contexts */
189   if (!silc_ske_set_keys(ske, keymat, prop, &send_key, &receive_key,
190                          &hmac_send, &hmac_receive, &conn->internal->hash)) {
191     /* Error setting keys */
192     SILC_LOG_DEBUG(("Could not set keys into use"));
193
194     if (conn->internal->verbose)
195       client->internal->ops->say(
196                        client, conn, SILC_CLIENT_MESSAGE_ERROR,
197                        "Error during key exchange with %s: cannot use keys",
198                        conn->remote_host);
199
200     conn->internal->status = SILC_CLIENT_CONN_ERROR_KE;
201     silc_ske_free_rekey_material(rekey);
202
203     silc_fsm_next(fsm, silc_client_st_connect_error);
204     SILC_FSM_CALL_CONTINUE_SYNC(fsm);
205     return;
206   }
207
208   /* Set the keys into the packet stream.  After this call packets will be
209      encrypted with these keys. */
210   if (!silc_packet_set_keys(conn->stream, send_key, receive_key, hmac_send,
211                             hmac_receive, FALSE)) {
212     /* Error setting keys */
213     SILC_LOG_DEBUG(("Could not set keys into use"));
214
215     if (conn->internal->verbose)
216       client->internal->ops->say(
217                        client, conn, SILC_CLIENT_MESSAGE_ERROR,
218                        "Error during key exchange with %s: cannot use keys",
219                        conn->remote_host);
220
221     conn->internal->status = SILC_CLIENT_CONN_ERROR_KE;
222     silc_ske_free_rekey_material(rekey);
223
224     silc_fsm_next(fsm, silc_client_st_connect_error);
225     SILC_FSM_CALL_CONTINUE_SYNC(fsm);
226     return;
227   }
228
229   conn->internal->rekey = rekey;
230
231   SILC_LOG_DEBUG(("Key Exchange completed"));
232
233   /* Key exchange done */
234   SILC_FSM_CALL_CONTINUE_SYNC(fsm);
235 }
236
237 /* Rekey protocol completion callback */
238
239 static void silc_client_rekey_completion(SilcSKE ske,
240                                          SilcSKEStatus status,
241                                          SilcSKESecurityProperties prop,
242                                          SilcSKEKeyMaterial keymat,
243                                          SilcSKERekeyMaterial rekey,
244                                          void *context)
245 {
246   SilcFSMThread fsm = context;
247   SilcClientConnection conn = silc_fsm_get_context(fsm);
248   SilcClient client = conn->client;
249
250   conn->internal->op = NULL;
251   if (status != SILC_SKE_STATUS_OK) {
252     /* Rekey failed */
253     SILC_LOG_DEBUG(("Error during rekey with %s: %s (%d)",
254                     conn->remote_host, silc_ske_map_status(status), status));
255
256     if (conn->internal->verbose)
257       client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
258                                  "Error during rekey with %s: %s",
259                                  conn->remote_host,
260                                  silc_ske_map_status(status));
261
262     silc_ske_free(conn->internal->ske);
263     conn->internal->ske = NULL;
264     silc_fsm_finish(fsm);
265     return;
266   }
267
268   silc_ske_free_rekey_material(conn->internal->rekey);
269   conn->internal->rekey = rekey;
270
271   silc_ske_free(conn->internal->ske);
272   conn->internal->ske = NULL;
273
274   SILC_LOG_DEBUG(("Rekey completed conn %p", conn));
275
276   /* Rekey done */
277   silc_fsm_finish(fsm);
278 }
279
280 /* Callback called by application to return authentication data */
281
282 static void silc_client_connect_auth_method(SilcAuthMethod auth_meth,
283                                             void *auth, SilcUInt32 auth_len,
284                                             void *context)
285 {
286   SilcFSMThread fsm = context;
287   SilcClientConnection conn = silc_fsm_get_context(fsm);
288
289   conn->internal->params.auth_method = auth_meth;
290   conn->internal->params.auth = auth;
291   conn->internal->params.auth_len = auth_len;
292
293   SILC_FSM_CALL_CONTINUE(fsm);
294 }
295
296 /* Connection authentication completion callback */
297
298 static void silc_client_connect_auth_completion(SilcConnAuth connauth,
299                                                 SilcBool success,
300                                                 void *context)
301 {
302   SilcFSMThread fsm = context;
303   SilcClientConnection conn = silc_fsm_get_context(fsm);
304   SilcClient client = conn->client;
305
306   conn->internal->op = NULL;
307   silc_connauth_free(connauth);
308
309   if (!success) {
310     if (conn->internal->verbose)
311         client->internal->ops->say(
312                         client, conn, SILC_CLIENT_MESSAGE_ERROR,
313                         "Authentication failed");
314
315     conn->internal->status = SILC_CLIENT_CONN_ERROR_AUTH;
316     conn->internal->error = SILC_STATUS_ERR_AUTH_FAILED;
317     silc_fsm_next(fsm, silc_client_st_connect_error);
318   }
319
320   SILC_FSM_CALL_CONTINUE_SYNC(fsm);
321 }
322
323 /********************** CONNECTION_AUTH_REQUEST packet **********************/
324
325 /* Received connection authentication request packet.  We get the
326    required authentication method here. */
327
328 SILC_FSM_STATE(silc_client_connect_auth_request)
329 {
330   SilcClientConnection conn = fsm_context;
331   SilcPacket packet = state_context;
332   SilcUInt16 conn_type, auth_meth;
333
334   if (!conn->internal->auth_request) {
335     silc_packet_free(packet);
336     return SILC_FSM_FINISH;
337   }
338
339   /* Parse the payload */
340   if (silc_buffer_unformat(&packet->buffer,
341                            SILC_STR_UI_SHORT(&conn_type),
342                            SILC_STR_UI_SHORT(&auth_meth),
343                            SILC_STR_END) < 0)
344     auth_meth = SILC_AUTH_NONE;
345
346   silc_packet_free(packet);
347
348   SILC_LOG_DEBUG(("Resolved authentication method: %s",
349                   (auth_meth == SILC_AUTH_NONE ? "none" :
350                    auth_meth == SILC_AUTH_PASSWORD ? "passphrase" :
351                    "public key")));
352   conn->internal->params.auth_method = auth_meth;
353
354   /* Continue authentication */
355   silc_fsm_continue_sync(&conn->internal->event_thread);
356   return SILC_FSM_FINISH;
357 }
358
359 /*************************** Connect remote host ****************************/
360
361 /* Connection timeout callback */
362
363 SILC_TASK_CALLBACK(silc_client_connect_timeout)
364 {
365   SilcClientConnection conn = context;
366
367   SILC_LOG_DEBUG(("Connection timeout"));
368
369   conn->internal->status = SILC_CLIENT_CONN_ERROR_TIMEOUT;
370   conn->internal->error = SILC_STATUS_ERR_TIMEDOUT;
371
372   silc_fsm_next(&conn->internal->event_thread, silc_client_st_connect_error);
373   silc_fsm_continue_sync(&conn->internal->event_thread);
374 }
375
376 /* Creates a connection to remote host */
377
378 SILC_FSM_STATE(silc_client_st_connect)
379 {
380   SilcClientConnection conn = fsm_context;
381
382   SILC_LOG_DEBUG(("Connecting to %s:%d", conn->remote_host,
383                   conn->remote_port));
384
385   /** Connect */
386   silc_fsm_next(fsm, silc_client_st_connect_set_stream);
387
388   /* Add connection timeout */
389   if (conn->internal->params.timeout_secs)
390     silc_schedule_task_add_timeout(conn->internal->schedule,
391                                    silc_client_connect_timeout, conn,
392                                    conn->internal->params.timeout_secs, 0);
393
394   if (conn->internal->params.udp) {
395     SilcStream stream;
396
397     if (!conn->internal->params.local_ip) {
398       /** IP address not given */
399       SILC_LOG_ERROR(("Local UDP IP address not specified"));
400       conn->internal->status = SILC_CLIENT_CONN_ERROR;
401       silc_fsm_next(fsm, silc_client_st_connect_error);
402       return SILC_FSM_CONTINUE;
403     }
404
405     /* Connect (UDP) */
406     stream = silc_net_udp_connect(conn->internal->params.bind_ip ?
407                                   conn->internal->params.bind_ip :
408                                   conn->internal->params.local_ip,
409                                   conn->internal->params.local_port,
410                                   conn->remote_host, conn->remote_port,
411                                   conn->internal->schedule);
412
413     SILC_FSM_CALL(silc_client_connect_callback(stream ? SILC_NET_OK :
414                                                SILC_NET_HOST_UNREACHABLE,
415                                                stream, fsm));
416   } else {
417     /* Connect (TCP) */
418     SILC_FSM_CALL(conn->internal->op = silc_net_tcp_connect(
419                                        NULL, conn->remote_host,
420                                        conn->remote_port,
421                                        conn->internal->schedule,
422                                        silc_client_connect_callback, fsm));
423   }
424 }
425
426 /* Sets the new connection stream into use and creates packet stream */
427
428 SILC_FSM_STATE(silc_client_st_connect_set_stream)
429 {
430   SilcClientConnection conn = fsm_context;
431   SilcClient client = conn->client;
432
433   if (conn->internal->disconnected) {
434     /** Disconnected */
435     silc_fsm_next(fsm, silc_client_st_connect_error);
436     return SILC_FSM_CONTINUE;
437   }
438
439   /* Create packet stream */
440   conn->stream = silc_packet_stream_create(client->internal->packet_engine,
441                                            conn->internal->schedule,
442                                            conn->internal->user_stream);
443   if (!conn->stream) {
444     /** Cannot create packet stream */
445     SILC_LOG_DEBUG(("Could not create packet stream"));
446     conn->internal->status = SILC_CLIENT_CONN_ERROR;
447     silc_fsm_next(fsm, silc_client_st_connect_error);
448     return SILC_FSM_CONTINUE;
449   }
450
451   silc_packet_set_context(conn->stream, conn);
452
453   /** Start key exchange */
454   silc_fsm_next(fsm, silc_client_st_connect_key_exchange);
455   return SILC_FSM_CONTINUE;
456 }
457
458 /* Starts key exchange protocol with remote host */
459
460 SILC_FSM_STATE(silc_client_st_connect_key_exchange)
461 {
462   SilcClientConnection conn = fsm_context;
463   SilcClient client = conn->client;
464   SilcSKEParamsStruct params;
465   SilcClientID cid;
466
467   SILC_LOG_DEBUG(("Starting key exchange protocol"));
468
469   /* Allocate SKE */
470   conn->internal->ske =
471     silc_ske_alloc(client->rng, conn->internal->schedule,
472                    conn->internal->params.repository,
473                    conn->public_key, conn->private_key, fsm);
474   if (!conn->internal->ske) {
475     /** Out of memory */
476     conn->internal->status = SILC_CLIENT_CONN_ERROR_KE;
477     silc_fsm_next(fsm, silc_client_st_connect_error);
478     return SILC_FSM_CONTINUE;
479   }
480
481   /* Set SKE callbacks */
482   silc_ske_set_callbacks(conn->internal->ske, silc_client_ke_verify_key,
483                          silc_client_ke_completion, fsm);
484
485   /* Set up key exchange parameters */
486   params.version = client->internal->silc_client_version;
487   params.timeout_secs = conn->internal->params.timeout_secs;
488   params.flags = SILC_SKE_SP_FLAG_MUTUAL;
489   if (conn->internal->params.pfs)
490     params.flags |= SILC_SKE_SP_FLAG_PFS;
491   if (conn->internal->params.udp) {
492     params.flags |= SILC_SKE_SP_FLAG_IV_INCLUDED;
493     params.session_port = conn->internal->params.local_port;
494   }
495
496   if (conn->internal->params.no_authentication)
497     /** Run key exchange (no auth) */
498     silc_fsm_next(fsm, silc_client_st_connected);
499   else if (conn->internal->params.udp)
500     /** Run key exchange (UDP)*/
501     silc_fsm_next(fsm, silc_client_st_connect_setup_udp);
502   else
503     /** Run key exchange (TCP) */
504     silc_fsm_next(fsm, silc_client_st_connect_auth_resolve);
505
506   /* Old server version requires empty Client ID in packets.  Remove this
507      backwards support somepoint after 1.1 server is released. */
508   memset(&cid, 0, sizeof(cid));
509   cid.ip.data_len = 4;
510   silc_packet_set_ids(conn->stream, SILC_ID_CLIENT, &cid, 0, NULL);
511
512   SILC_FSM_CALL(conn->internal->op = silc_ske_initiator(conn->internal->ske,
513                                                         conn->stream,
514                                                         &params, NULL));
515 }
516
517 /* For UDP/IP connections, set up the UDP session after successful key
518    exchange protocol */
519
520 SILC_FSM_STATE(silc_client_st_connect_setup_udp)
521 {
522   SilcClientConnection conn = fsm_context;
523   SilcStream stream, old;
524   SilcSKESecurityProperties prop;
525
526   SILC_LOG_DEBUG(("Setup UDP SILC session"));
527
528   if (conn->internal->disconnected) {
529     /** Disconnected */
530     silc_fsm_next(fsm, silc_client_st_connect_error);
531     return SILC_FSM_CONTINUE;
532   }
533
534   /* Create new UDP stream */
535   prop = silc_ske_get_security_properties(conn->internal->ske);
536   stream = silc_net_udp_connect(conn->internal->params.local_ip,
537                                 conn->internal->params.local_port,
538                                 conn->remote_host, prop->remote_port,
539                                 conn->internal->schedule);
540   if (!stream) {
541     /** Cannot create UDP stream */
542     conn->internal->status = SILC_CLIENT_CONN_ERROR;
543     silc_fsm_next(fsm, silc_client_st_connect_error);
544     return SILC_FSM_CONTINUE;
545   }
546
547   /* Set the new stream to packet stream */
548   old = silc_packet_stream_get_stream(conn->stream);
549   silc_packet_stream_set_stream(conn->stream, stream);
550   silc_packet_stream_set_iv_included(conn->stream);
551   silc_packet_set_sid(conn->stream, 0);
552
553   /* Delete the old stream */
554   silc_stream_destroy(old);
555
556   /** Start authentication */
557   silc_fsm_next(fsm, silc_client_st_connect_auth_resolve);
558   return SILC_FSM_CONTINUE;
559 }
560
561 /* Resolve authentication method to be used in authentication protocol */
562
563 SILC_FSM_STATE(silc_client_st_connect_auth_resolve)
564 {
565   SilcClientConnection conn = fsm_context;
566
567   SILC_LOG_DEBUG(("Resolve authentication method"));
568
569   if (conn->internal->disconnected) {
570     /** Disconnected */
571     silc_fsm_next(fsm, silc_client_st_connect_error);
572     return SILC_FSM_CONTINUE;
573   }
574
575   /* If authentication method and data is set, use them */
576   if (conn->internal->params.auth_set) {
577     /** Got authentication data */
578     silc_fsm_next(fsm, silc_client_st_connect_auth_start);
579     return SILC_FSM_CONTINUE;
580   }
581
582   /* Send connection authentication request packet */
583   silc_packet_send_va(conn->stream,
584                       SILC_PACKET_CONNECTION_AUTH_REQUEST, 0,
585                       SILC_STR_UI_SHORT(SILC_CONN_CLIENT),
586                       SILC_STR_UI_SHORT(SILC_AUTH_NONE),
587                       SILC_STR_END);
588
589   /** Wait for authentication method */
590   conn->internal->auth_request = TRUE;
591   conn->internal->params.auth_method = SILC_AUTH_NONE;
592   silc_fsm_next_later(fsm, silc_client_st_connect_auth_data, 2, 0);
593   return SILC_FSM_WAIT;
594 }
595
596 /* Get authentication data to be used in authentication protocol */
597
598 SILC_FSM_STATE(silc_client_st_connect_auth_data)
599 {
600   SilcClientConnection conn = fsm_context;
601   SilcClient client = conn->client;
602
603   SILC_LOG_DEBUG(("Get authentication data"));
604
605   if (conn->internal->disconnected) {
606     /** Disconnected */
607     silc_fsm_next(fsm, silc_client_st_connect_error);
608     return SILC_FSM_CONTINUE;
609   }
610
611   conn->internal->auth_request = FALSE;
612
613   /** Get authentication data */
614   silc_fsm_next(fsm, silc_client_st_connect_auth_start);
615   SILC_FSM_CALL(client->internal->ops->get_auth_method(
616                                     client, conn,
617                                     conn->remote_host,
618                                     conn->remote_port,
619                                     conn->internal->params.auth_method,
620                                     silc_client_connect_auth_method, fsm));
621 }
622
623 /* Start connection authentication with remote host */
624
625 SILC_FSM_STATE(silc_client_st_connect_auth_start)
626 {
627   SilcClientConnection conn = fsm_context;
628   SilcConnAuth connauth;
629
630   SILC_LOG_DEBUG(("Starting connection authentication protocol"));
631
632   if (conn->internal->disconnected) {
633     /** Disconnected */
634     silc_fsm_next(fsm, silc_client_st_connect_error);
635     return SILC_FSM_CONTINUE;
636   }
637
638   /* We always use the same key for connection authentication and SKE */
639   if (conn->internal->params.auth_method == SILC_AUTH_PUBLIC_KEY)
640     conn->internal->params.auth = conn->private_key;
641
642   /* Allocate connection authentication protocol */
643   connauth = silc_connauth_alloc(conn->internal->schedule,
644                                  conn->internal->ske,
645                                  conn->internal->params.rekey_secs);
646   if (!connauth) {
647     /** Out of memory */
648     conn->internal->status = SILC_CLIENT_CONN_ERROR_AUTH;
649     conn->internal->error = SILC_STATUS_ERR_AUTH_FAILED;
650     silc_fsm_next(fsm, silc_client_st_connect_error);
651     return SILC_FSM_CONTINUE;
652   }
653
654   /** Start connection authentication */
655   silc_fsm_next(fsm, silc_client_st_connected);
656   SILC_FSM_CALL(conn->internal->op = silc_connauth_initiator(
657                                         connauth, SILC_CONN_CLIENT,
658                                         conn->internal->params.auth_method,
659                                         conn->internal->params.auth,
660                                         conn->internal->params.auth_len,
661                                         silc_client_connect_auth_completion,
662                                         fsm));
663 }
664
665 /* Connection fully established */
666
667 SILC_FSM_STATE(silc_client_st_connected)
668 {
669   SilcClientConnection conn = fsm_context;
670   SilcClient client = conn->client;
671
672   /* Get SILC protocol version remote supports */
673   silc_ske_parse_version(conn->internal->ske, &conn->internal->remote_version,
674                          NULL, NULL, NULL, NULL);
675
676   silc_ske_free(conn->internal->ske);
677   conn->internal->ske = NULL;
678
679   if (conn->internal->disconnected) {
680     /** Disconnected */
681     silc_fsm_next(fsm, silc_client_st_connect_error);
682     return SILC_FSM_CONTINUE;
683   }
684
685   SILC_LOG_DEBUG(("Connection established"));
686
687   /* Install rekey timer */
688   if (conn->type != SILC_CONN_CLIENT)
689     silc_schedule_task_add_timeout(conn->internal->schedule,
690                                    silc_client_rekey_timer, conn,
691                                    conn->internal->params.rekey_secs, 0);
692
693   /* If we connected to server, now register to network. */
694   if (conn->type == SILC_CONN_SERVER &&
695       !conn->internal->params.no_authentication) {
696
697     /* If detach data is provided, resume the session. */
698     if (conn->internal->params.detach_data &&
699         conn->internal->params.detach_data_len) {
700       /** Resume detached session */
701       silc_fsm_next(fsm, silc_client_st_resume);
702     } else {
703       /** Register to network */
704       silc_fsm_next(fsm, silc_client_st_register);
705     }
706
707     return SILC_FSM_CONTINUE;
708   }
709
710   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
711                                 silc_client_connect_timeout, conn);
712
713   /* Call connection callback */
714   conn->callback(client, conn, SILC_CLIENT_CONN_SUCCESS, 0, NULL,
715                  conn->callback_context);
716
717   silc_async_free(conn->internal->cop);
718   conn->internal->cop = NULL;
719
720   return SILC_FSM_FINISH;
721 }
722
723 /* Error during connecting */
724
725 SILC_FSM_STATE(silc_client_st_connect_error)
726 {
727   SilcClientConnection conn = fsm_context;
728
729   if (conn->internal->ske) {
730     silc_ske_free(conn->internal->ske);
731     conn->internal->ske = NULL;
732   }
733
734   /* Signal to close connection */
735   if (!conn->internal->disconnected) {
736     conn->internal->disconnected = TRUE;
737     SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
738   }
739
740   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
741                                 silc_client_connect_timeout, conn);
742
743   return SILC_FSM_FINISH;
744 }
745
746 /****************************** Connect rekey *******************************/
747
748 /* Connection rekey timer callback */
749
750 SILC_TASK_CALLBACK(silc_client_rekey_timer)
751 {
752   SilcClientConnection conn = context;
753
754   /* Signal to start rekey */
755   if (!silc_fsm_is_started(&conn->internal->event_thread)) {
756     conn->internal->rekey_responder = FALSE;
757     conn->internal->rekeying = TRUE;
758     SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
759   }
760
761   /* Reinstall rekey timer */
762   silc_schedule_task_add_timeout(conn->internal->schedule,
763                                  silc_client_rekey_timer, conn,
764                                  conn->internal->params.rekey_secs, 0);
765 }
766
767 /* Performs rekey */
768
769 SILC_FSM_STATE(silc_client_st_rekey)
770 {
771   SilcClientConnection conn = fsm_context;
772   SilcClient client = conn->client;
773
774   SILC_LOG_DEBUG(("Rekey conn %p", conn));
775
776   if (conn->internal->disconnected)
777     return SILC_FSM_FINISH;
778
779   /* Allocate SKE */
780   conn->internal->ske =
781     silc_ske_alloc(client->rng, conn->internal->schedule,
782                    conn->internal->params.repository,
783                    conn->public_key, conn->private_key, fsm);
784   if (!conn->internal->ske)
785     return SILC_FSM_FINISH;
786
787   /* Set SKE callbacks */
788   silc_ske_set_callbacks(conn->internal->ske, NULL,
789                          silc_client_rekey_completion, fsm);
790
791   /** Perform rekey */
792   if (!conn->internal->rekey_responder)
793     SILC_FSM_CALL(conn->internal->op = silc_ske_rekey_initiator(
794                                                     conn->internal->ske,
795                                                     conn->stream,
796                                                     conn->internal->rekey));
797   else
798     SILC_FSM_CALL(conn->internal->op = silc_ske_rekey_responder(
799                                                     conn->internal->ske,
800                                                     conn->stream,
801                                                     conn->internal->rekey,
802                                                     NULL));
803 }