Nickname formatting/parsing rewrite.
[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 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->callback(client, conn, SILC_CLIENT_CONN_ERROR, 0,
83                    NULL, conn->callback_context);
84     silc_fsm_next(fsm, silc_client_st_connect_error);
85     SILC_FSM_CALL_CONTINUE(fsm);
86     return;
87   }
88
89   /* Connection created successfully */
90   SILC_LOG_DEBUG(("Connected"));
91   conn->stream = (void *)stream;
92   SILC_FSM_CALL_CONTINUE(fsm);
93 }
94
95 /* Called after application has verified remote host's public key */
96
97 static void silc_client_ke_verify_key_cb(SilcBool success, void *context)
98 {
99   VerifyKeyContext verify = (VerifyKeyContext)context;
100
101   SILC_LOG_DEBUG(("Start"));
102
103   /* Call the completion callback back to the SKE */
104   verify->completion(verify->ske, success ? SILC_SKE_STATUS_OK :
105                      SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY,
106                      verify->completion_context);
107
108   silc_free(verify);
109 }
110
111 /* Verify remote host's public key */
112
113 static void silc_client_ke_verify_key(SilcSKE ske,
114                                       SilcPublicKey public_key,
115                                       void *context,
116                                       SilcSKEVerifyCbCompletion completion,
117                                       void *completion_context)
118 {
119   SilcFSMThread fsm = context;
120   SilcClientConnection conn = silc_fsm_get_context(fsm);
121   SilcClient client = conn->client;
122   VerifyKeyContext verify;
123
124   /* If we provided repository for SKE and we got here the key was not
125      found from the repository. */
126   if (conn->internal->params.repository &&
127       !conn->internal->params.verify_notfound) {
128     completion(ske, SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY,
129                completion_context);
130     return;
131   }
132
133   SILC_LOG_DEBUG(("Verify remote public key"));
134
135   verify = silc_calloc(1, sizeof(*verify));
136   if (!verify) {
137     completion(ske, SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY,
138                completion_context);
139     return;
140   }
141   verify->ske = ske;
142   verify->completion = completion;
143   verify->completion_context = completion_context;
144
145   /* Verify public key in application */
146   client->internal->ops->verify_public_key(client, conn,
147                                            conn->type, public_key,
148                                            silc_client_ke_verify_key_cb,
149                                            verify);
150 }
151
152 /* Key exchange protocol completion callback */
153
154 static void silc_client_ke_completion(SilcSKE ske,
155                                       SilcSKEStatus status,
156                                       SilcSKESecurityProperties prop,
157                                       SilcSKEKeyMaterial keymat,
158                                       SilcSKERekeyMaterial rekey,
159                                       void *context)
160 {
161   SilcFSMThread fsm = context;
162   SilcClientConnection conn = silc_fsm_get_context(fsm);
163   SilcClient client = conn->client;
164   SilcCipher send_key, receive_key;
165   SilcHmac hmac_send, hmac_receive;
166
167   conn->internal->op = NULL;
168   if (status != SILC_SKE_STATUS_OK) {
169     /* Key exchange failed */
170     SILC_LOG_DEBUG(("Error during key exchange with %s: %s (%d)",
171                     conn->remote_host, silc_ske_map_status(status), status));
172
173     if (conn->internal->verbose)
174       client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
175                                  "Error during key exchange with %s: %s",
176                                  conn->remote_host,
177                                  silc_ske_map_status(status));
178
179     silc_ske_free_rekey_material(rekey);
180
181     silc_fsm_next(fsm, silc_client_st_connect_error);
182     SILC_FSM_CALL_CONTINUE(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->callback(client, conn, SILC_CLIENT_CONN_ERROR_KE, 0, NULL,
201                    conn->callback_context);
202
203     silc_ske_free_rekey_material(rekey);
204
205     silc_fsm_next(fsm, silc_client_st_connect_error);
206     SILC_FSM_CALL_CONTINUE(fsm);
207     return;
208   }
209
210   /* Set the keys into the packet stream.  After this call packets will be
211      encrypted with these keys. */
212   if (!silc_packet_set_keys(conn->stream, send_key, receive_key, hmac_send,
213                             hmac_receive, FALSE)) {
214     /* Error setting keys */
215     SILC_LOG_DEBUG(("Could not set keys into use"));
216
217     if (conn->internal->verbose)
218       client->internal->ops->say(
219                        client, conn, SILC_CLIENT_MESSAGE_ERROR,
220                        "Error during key exchange with %s: cannot use keys",
221                        conn->remote_host);
222
223     conn->callback(client, conn, SILC_CLIENT_CONN_ERROR_KE, 0, NULL,
224                    conn->callback_context);
225
226     silc_ske_free_rekey_material(rekey);
227
228     silc_fsm_next(fsm, silc_client_st_connect_error);
229     SILC_FSM_CALL_CONTINUE(fsm);
230     return;
231   }
232
233   conn->internal->rekey = rekey;
234
235   SILC_LOG_DEBUG(("Key Exchange completed"));
236
237   /* Key exchange done */
238   SILC_FSM_CALL_CONTINUE(fsm);
239 }
240
241 /* Rekey protocol completion callback */
242
243 static void silc_client_rekey_completion(SilcSKE ske,
244                                          SilcSKEStatus status,
245                                          SilcSKESecurityProperties prop,
246                                          SilcSKEKeyMaterial keymat,
247                                          SilcSKERekeyMaterial rekey,
248                                          void *context)
249 {
250   SilcFSMThread fsm = context;
251   SilcClientConnection conn = silc_fsm_get_context(fsm);
252   SilcClient client = conn->client;
253
254   conn->internal->op = NULL;
255   if (status != SILC_SKE_STATUS_OK) {
256     /* Rekey failed */
257     SILC_LOG_DEBUG(("Error during rekey with %s: %s (%d)",
258                     conn->remote_host, silc_ske_map_status(status), status));
259
260     if (conn->internal->verbose)
261       client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
262                                  "Error during rekey with %s: %s",
263                                  conn->remote_host,
264                                  silc_ske_map_status(status));
265
266     silc_fsm_finish(fsm);
267     return;
268   }
269
270   silc_ske_free_rekey_material(conn->internal->rekey);
271   conn->internal->rekey = rekey;
272
273   SILC_LOG_DEBUG(("Rekey completed"));
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(SilcBool success,
282                                             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 = SILC_AUTH_NONE;
290
291   if (success) {
292     conn->internal->params.auth_method = auth_meth;
293     conn->internal->params.auth = auth;
294     conn->internal->params.auth_len = auth_len;
295   }
296
297   SILC_FSM_CALL_CONTINUE(fsm);
298 }
299
300 /* Connection authentication completion callback */
301
302 static void silc_client_connect_auth_completion(SilcConnAuth connauth,
303                                                 SilcBool success,
304                                                 void *context)
305 {
306   SilcFSMThread fsm = context;
307   SilcClientConnection conn = silc_fsm_get_context(fsm);
308   SilcClient client = conn->client;
309
310   conn->internal->op = NULL;
311   silc_connauth_free(connauth);
312
313   if (!success) {
314     if (conn->internal->verbose)
315         client->internal->ops->say(
316                         client, conn, SILC_CLIENT_MESSAGE_ERROR,
317                         "Authentication failed");
318
319     conn->callback(client, conn, SILC_CLIENT_CONN_ERROR_AUTH, 0, NULL,
320                    conn->callback_context);
321     silc_fsm_next(fsm, silc_client_st_connect_error);
322   }
323
324   SILC_FSM_CALL_CONTINUE(fsm);
325 }
326
327 /*************************** Connect remote host ****************************/
328
329 /* Connection timeout callback */
330
331 SILC_TASK_CALLBACK(silc_client_connect_timeout)
332 {
333   SilcClientConnection conn = context;
334   SilcClient client = conn->client;
335
336   SILC_LOG_DEBUG(("Connection timeout"));
337
338   conn->callback(client, conn, SILC_CLIENT_CONN_ERROR_TIMEOUT, 0, NULL,
339                  conn->callback_context);
340
341   silc_fsm_next(&conn->internal->event_thread, silc_client_st_connect_error);
342   silc_fsm_continue_sync(&conn->internal->event_thread);
343 }
344
345 /* Creates a connection to remote host */
346
347 SILC_FSM_STATE(silc_client_st_connect)
348 {
349   SilcClientConnection conn = fsm_context;
350   SilcClient client = conn->client;
351
352   SILC_LOG_DEBUG(("Connecting to %s:%d", conn->remote_host,
353                   conn->remote_port));
354
355   /** Connect */
356   silc_fsm_next(fsm, silc_client_st_connect_set_stream);
357
358   /* Add connection timeout */
359   if (conn->internal->params.timeout_secs)
360     silc_schedule_task_add_timeout(conn->internal->schedule,
361                                    silc_client_connect_timeout, conn,
362                                    conn->internal->params.timeout_secs, 0);
363
364   if (conn->internal->params.udp) {
365     SilcStream stream;
366
367     if (!conn->internal->params.local_ip) {
368       /** IP address not given */
369       SILC_LOG_ERROR(("Local UDP IP address not specified"));
370       conn->callback(client, conn, SILC_CLIENT_CONN_ERROR, 0, NULL,
371                      conn->callback_context);
372       silc_fsm_next(fsm, silc_client_st_connect_error);
373       SILC_FSM_CONTINUE;
374     }
375
376     /* Connect (UDP) */
377     stream = silc_net_udp_connect(conn->internal->params.bind_ip ?
378                                   conn->internal->params.bind_ip :
379                                   conn->internal->params.local_ip,
380                                   conn->internal->params.local_port,
381                                   conn->remote_host, conn->remote_port,
382                                   conn->internal->schedule);
383
384     SILC_FSM_CALL(silc_client_connect_callback(stream ? SILC_NET_OK :
385                                                SILC_NET_HOST_UNREACHABLE,
386                                                stream, fsm));
387   } else {
388     /* Connect (TCP) */
389     SILC_FSM_CALL(conn->internal->op = silc_net_tcp_connect(
390                                        NULL, conn->remote_host,
391                                        conn->remote_port,
392                                        conn->internal->schedule,
393                                        silc_client_connect_callback, fsm));
394   }
395 }
396
397 /* Sets the new connection stream into use and creates packet stream */
398
399 SILC_FSM_STATE(silc_client_st_connect_set_stream)
400 {
401   SilcClientConnection conn = fsm_context;
402   SilcClient client = conn->client;
403
404   if (conn->internal->disconnected) {
405     /** Disconnected */
406     silc_fsm_next(fsm, silc_client_st_connect_error);
407     SILC_FSM_CONTINUE;
408   }
409
410   /* Create packet stream */
411   conn->stream = silc_packet_stream_create(client->internal->packet_engine,
412                                            conn->internal->schedule,
413                                            (SilcStream)conn->stream);
414   if (!conn->stream) {
415     /** Cannot create packet stream */
416     SILC_LOG_DEBUG(("Could not create packet stream"));
417     conn->callback(client, conn, SILC_CLIENT_CONN_ERROR, 0, NULL,
418                    conn->callback_context);
419     silc_fsm_next(fsm, silc_client_st_connect_error);
420     SILC_FSM_CONTINUE;
421   }
422
423   silc_packet_set_context(conn->stream, conn);
424
425   /** Start key exchange */
426   silc_fsm_next(fsm, silc_client_st_connect_key_exchange);
427   SILC_FSM_CONTINUE;
428 }
429
430 /* Starts key exchange protocol with remote host */
431
432 SILC_FSM_STATE(silc_client_st_connect_key_exchange)
433 {
434   SilcClientConnection conn = fsm_context;
435   SilcClient client = conn->client;
436   SilcSKEParamsStruct params;
437
438   SILC_LOG_DEBUG(("Starting key exchange protocol"));
439
440   /* Allocate SKE */
441   conn->internal->ske =
442     silc_ske_alloc(client->rng, conn->internal->schedule,
443                    conn->internal->params.repository,
444                    conn->public_key, conn->private_key, fsm);
445   if (!conn->internal->ske) {
446     /** Out of memory */
447     conn->callback(client, conn, SILC_CLIENT_CONN_ERROR_KE, 0, NULL,
448                    conn->callback_context);
449     silc_fsm_next(fsm, silc_client_st_connect_error);
450     SILC_FSM_CONTINUE;
451   }
452
453   /* Set SKE callbacks */
454   silc_ske_set_callbacks(conn->internal->ske, silc_client_ke_verify_key,
455                          silc_client_ke_completion, fsm);
456
457   /* Set up key exchange parameters */
458   params.version = client->internal->silc_client_version;
459   params.flags = SILC_SKE_SP_FLAG_MUTUAL;
460   if (conn->internal->params.pfs)
461     params.flags |= SILC_SKE_SP_FLAG_PFS;
462   if (conn->internal->params.udp) {
463     params.flags |= SILC_SKE_SP_FLAG_IV_INCLUDED;
464     params.session_port = conn->internal->params.local_port;
465   }
466
467   if (conn->internal->params.no_authentication)
468     /** Run key exchange (no auth) */
469     silc_fsm_next(fsm, silc_client_st_connected);
470   else if (conn->internal->params.udp)
471     /** Run key exchange (UDP)*/
472     silc_fsm_next(fsm, silc_client_st_connect_setup_udp);
473   else
474     /** Run key exchange (TCP) */
475     silc_fsm_next(fsm, silc_client_st_connect_auth);
476
477   SILC_FSM_CALL(conn->internal->op = silc_ske_initiator(conn->internal->ske,
478                                                         conn->stream,
479                                                         &params, NULL));
480 }
481
482 /* For UDP/IP connections, set up the UDP session after successful key
483    exchange protocol */
484
485 SILC_FSM_STATE(silc_client_st_connect_setup_udp)
486 {
487   SilcClientConnection conn = fsm_context;
488   SilcClient client = conn->client;
489   SilcStream stream, old;
490   SilcSKESecurityProperties prop;
491
492   SILC_LOG_DEBUG(("Setup UDP SILC session"));
493
494   if (conn->internal->disconnected) {
495     /** Disconnected */
496     silc_fsm_next(fsm, silc_client_st_connect_error);
497     SILC_FSM_CONTINUE;
498   }
499
500   /* Create new UDP stream */
501   prop = silc_ske_get_security_properties(conn->internal->ske);
502   stream = silc_net_udp_connect(conn->internal->params.local_ip,
503                                 conn->internal->params.local_port,
504                                 conn->remote_host, prop->remote_port,
505                                 conn->internal->schedule);
506   if (!stream) {
507     /** Cannot create UDP stream */
508     conn->callback(client, conn, SILC_CLIENT_CONN_ERROR, 0, NULL,
509                    conn->callback_context);
510     silc_fsm_next(fsm, silc_client_st_connect_error);
511     SILC_FSM_CONTINUE;
512   }
513
514   /* Set the new stream to packet stream */
515   old = silc_packet_stream_get_stream(conn->stream);
516   silc_packet_stream_set_stream(conn->stream, stream);
517   silc_packet_stream_set_iv_included(conn->stream);
518   silc_packet_set_sid(conn->stream, 0);
519
520   /* Delete the old stream */
521   silc_stream_destroy(old);
522
523   /** Start authentication */
524   silc_fsm_next(fsm, silc_client_st_connect_auth);
525   SILC_FSM_CONTINUE;
526 }
527
528 /* Get authentication method to be used in authentication protocol */
529
530 SILC_FSM_STATE(silc_client_st_connect_auth)
531 {
532   SilcClientConnection conn = fsm_context;
533   SilcClient client = conn->client;
534
535   SILC_LOG_DEBUG(("Get authentication data"));
536
537   if (conn->internal->disconnected) {
538     /** Disconnected */
539     silc_fsm_next(fsm, silc_client_st_connect_error);
540     SILC_FSM_CONTINUE;
541   }
542
543   silc_fsm_next(fsm, silc_client_st_connect_auth_start);
544
545   /* If authentication data not provided, ask from application */
546   if (!conn->internal->params.auth_set)
547     SILC_FSM_CALL(client->internal->ops->get_auth_method(
548                                     client, conn,
549                                     conn->remote_host,
550                                     conn->remote_port,
551                                     silc_client_connect_auth_method, fsm));
552
553   if (conn->internal->params.auth_method == SILC_AUTH_PUBLIC_KEY)
554     conn->internal->params.auth = conn->private_key;
555
556   /* We have authentication data */
557   SILC_FSM_CONTINUE;
558 }
559
560 /* Start connection authentication with remote host */
561
562 SILC_FSM_STATE(silc_client_st_connect_auth_start)
563 {
564   SilcClientConnection conn = fsm_context;
565   SilcClient client = conn->client;
566   SilcConnAuth connauth;
567
568   SILC_LOG_DEBUG(("Starting connection authentication protocol"));
569
570   if (conn->internal->disconnected) {
571     /** Disconnected */
572     silc_fsm_next(fsm, silc_client_st_connect_error);
573     SILC_FSM_CONTINUE;
574   }
575
576   /* Allocate connection authentication protocol */
577   connauth = silc_connauth_alloc(conn->internal->schedule,
578                                  conn->internal->ske,
579                                  conn->internal->params.rekey_secs);
580   if (!connauth) {
581     /** Out of memory */
582     conn->callback(client, conn, SILC_CLIENT_CONN_ERROR_AUTH, 0, NULL,
583                    conn->callback_context);
584     silc_fsm_next(fsm, silc_client_st_connect_error);
585     SILC_FSM_CONTINUE;
586   }
587
588   /** Start connection authentication */
589   silc_fsm_next(fsm, silc_client_st_connected);
590   SILC_FSM_CALL(conn->internal->op = silc_connauth_initiator(
591                                         connauth, SILC_CONN_CLIENT,
592                                         conn->internal->params.auth_method,
593                                         conn->internal->params.auth,
594                                         conn->internal->params.auth_len,
595                                         silc_client_connect_auth_completion,
596                                         fsm));
597 }
598
599 /* Connection fully established */
600
601 SILC_FSM_STATE(silc_client_st_connected)
602 {
603   SilcClientConnection conn = fsm_context;
604   SilcClient client = conn->client;
605
606   silc_ske_free(conn->internal->ske);
607   conn->internal->ske = NULL;
608
609   if (conn->internal->disconnected) {
610     /** Disconnected */
611     silc_fsm_next(fsm, silc_client_st_connect_error);
612     SILC_FSM_CONTINUE;
613   }
614
615   SILC_LOG_DEBUG(("Connection established"));
616
617   /* Install rekey timer */
618   silc_schedule_task_add_timeout(conn->internal->schedule,
619                                  silc_client_rekey_timer, conn,
620                                  conn->internal->params.rekey_secs, 0);
621
622   /* If we connected to server, now register to network. */
623   if (conn->type == SILC_CONN_SERVER &&
624       !conn->internal->params.no_authentication) {
625
626     /* If detach data is provided, resume the session. */
627     if (conn->internal->params.detach_data &&
628         conn->internal->params.detach_data_len) {
629       /** Resume detached session */
630       silc_fsm_next(fsm, silc_client_st_resume);
631     } else {
632       /** Register to network */
633       silc_fsm_next(fsm, silc_client_st_register);
634     }
635
636     SILC_FSM_CONTINUE;
637   }
638
639   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
640                                 silc_client_connect_timeout, conn);
641
642   /* Call connection callback */
643   conn->callback(client, conn, SILC_CLIENT_CONN_SUCCESS, 0, NULL,
644                  conn->callback_context);
645
646   SILC_FSM_FINISH;
647 }
648
649 /* Error during connecting */
650
651 SILC_FSM_STATE(silc_client_st_connect_error)
652 {
653   SilcClientConnection conn = fsm_context;
654
655   if (conn->internal->ske) {
656     silc_ske_free(conn->internal->ske);
657     conn->internal->ske = NULL;
658   }
659
660   /* Signal to close connection */
661   if (!conn->internal->disconnected) {
662     conn->internal->disconnected = TRUE;
663     SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
664   }
665
666   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
667                                 silc_client_connect_timeout, conn);
668
669   SILC_FSM_FINISH;
670 }
671
672 /****************************** Connect rekey *******************************/
673
674 /* Connection rekey timer callback */
675
676 SILC_TASK_CALLBACK(silc_client_rekey_timer)
677 {
678   SilcClientConnection conn = context;
679
680   /* Signal to start rekey */
681   conn->internal->rekey_responder = FALSE;
682   conn->internal->rekeying = TRUE;
683   SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
684
685   /* Reinstall rekey timer */
686   silc_schedule_task_add_timeout(conn->internal->schedule,
687                                  silc_client_rekey_timer, conn,
688                                  conn->internal->params.rekey_secs, 0);
689 }
690
691 /* Performs rekey */
692
693 SILC_FSM_STATE(silc_client_st_rekey)
694 {
695   SilcClientConnection conn = fsm_context;
696   SilcClient client = conn->client;
697
698   SILC_LOG_DEBUG(("Rekey"));
699
700   if (conn->internal->disconnected)
701     SILC_FSM_FINISH;
702
703   /* Allocate SKE */
704   conn->internal->ske =
705     silc_ske_alloc(client->rng, conn->internal->schedule,
706                    conn->internal->params.repository,
707                    conn->public_key, conn->private_key, fsm);
708   if (!conn->internal->ske)
709     SILC_FSM_FINISH;
710
711   /* Set SKE callbacks */
712   silc_ske_set_callbacks(conn->internal->ske, NULL,
713                          silc_client_rekey_completion, fsm);
714
715   /** Perform rekey */
716   if (!conn->internal->rekey_responder)
717     SILC_FSM_CALL(conn->internal->op = silc_ske_rekey_initiator(
718                                                     conn->internal->ske,
719                                                     conn->stream,
720                                                     conn->internal->rekey));
721   else
722     SILC_FSM_CALL(conn->internal->op = silc_ske_rekey_responder(
723                                                     conn->internal->ske,
724                                                     conn->stream,
725                                                     conn->internal->rekey));
726 }