updates.
[silc.git] / apps / silcd / protocol.c
1 /*
2
3   protocol.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2001 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; either version 2 of the License, or
12   (at your option) any later version.
13   
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20 /*
21  * Server side of the protocols.
22  */
23 /* $Id$ */
24
25 #include "serverincludes.h"
26 #include "server_internal.h"
27
28 SILC_TASK_CALLBACK(silc_server_protocol_connection_auth);
29 SILC_TASK_CALLBACK(silc_server_protocol_key_exchange);
30 SILC_TASK_CALLBACK(silc_server_protocol_rekey);
31
32 extern char *silc_version_string;
33
34 /*
35  * Key Exhange protocol functions
36  */
37
38 /* Packet sending callback. This function is provided as packet sending
39    routine to the Key Exchange functions. */
40
41 static void silc_server_protocol_ke_send_packet(SilcSKE ske,
42                                                 SilcBuffer packet,
43                                                 SilcPacketType type,
44                                                 void *context)
45 {
46   SilcProtocol protocol = (SilcProtocol)context;
47   SilcServerKEInternalContext *ctx = 
48     (SilcServerKEInternalContext *)protocol->context;
49   SilcServer server = (SilcServer)ctx->server;
50
51   /* Send the packet immediately */
52   silc_server_packet_send(server, ske->sock,
53                           type, 0, packet->data, packet->len, TRUE);
54 }
55
56 /* Sets the negotiated key material into use for particular connection. */
57
58 int silc_server_protocol_ke_set_keys(SilcSKE ske,
59                                      SilcSocketConnection sock,
60                                      SilcSKEKeyMaterial *keymat,
61                                      SilcCipher cipher,
62                                      SilcPKCS pkcs,
63                                      SilcHash hash,
64                                      SilcHmac hmac,
65                                      SilcSKEDiffieHellmanGroup group,
66                                      bool is_responder)
67 {
68   SilcUnknownEntry conn_data;
69   SilcIDListData idata;
70
71   SILC_LOG_DEBUG(("Setting new key into use"));
72
73   conn_data = silc_calloc(1, sizeof(*conn_data));
74   idata = (SilcIDListData)conn_data;
75
76   /* Allocate cipher to be used in the communication */
77   if (!silc_cipher_alloc(cipher->cipher->name, &idata->send_key)) {
78     silc_free(conn_data);
79     return FALSE;
80   }
81   if (!silc_cipher_alloc(cipher->cipher->name, &idata->receive_key)) {
82     silc_free(conn_data);
83     return FALSE;
84   }
85   
86   if (is_responder == TRUE) {
87     silc_cipher_set_key(idata->send_key, keymat->receive_enc_key, 
88                         keymat->enc_key_len);
89     silc_cipher_set_iv(idata->send_key, keymat->receive_iv);
90     silc_cipher_set_key(idata->receive_key, keymat->send_enc_key, 
91                         keymat->enc_key_len);
92     silc_cipher_set_iv(idata->receive_key, keymat->send_iv);
93   } else {
94     silc_cipher_set_key(idata->send_key, keymat->send_enc_key, 
95                         keymat->enc_key_len);
96     silc_cipher_set_iv(idata->send_key, keymat->send_iv);
97     silc_cipher_set_key(idata->receive_key, keymat->receive_enc_key, 
98                         keymat->enc_key_len);
99     silc_cipher_set_iv(idata->receive_key, keymat->receive_iv);
100   }
101
102   idata->rekey = silc_calloc(1, sizeof(*idata->rekey));
103   idata->rekey->send_enc_key = 
104     silc_calloc(keymat->enc_key_len / 8,
105                 sizeof(*idata->rekey->send_enc_key));
106   memcpy(idata->rekey->send_enc_key, 
107          keymat->send_enc_key, keymat->enc_key_len / 8);
108   idata->rekey->enc_key_len = keymat->enc_key_len / 8;
109
110   if (ske->start_payload->flags & SILC_SKE_SP_FLAG_PFS)
111     idata->rekey->pfs = TRUE;
112   idata->rekey->ske_group = silc_ske_group_get_number(group);
113
114   /* Save the remote host's public key */
115   silc_pkcs_public_key_decode(ske->ke1_payload->pk_data, 
116                               ske->ke1_payload->pk_len, &idata->public_key);
117
118   /* Save the hash */
119   if (!silc_hash_alloc(hash->hash->name, &idata->hash)) {
120     silc_cipher_free(idata->send_key);
121     silc_cipher_free(idata->receive_key);
122     silc_free(conn_data);
123     return FALSE;
124   }
125
126   /* Save HMAC key to be used in the communication. */
127   if (!silc_hmac_alloc(hmac->hmac->name, NULL, &idata->hmac_send)) {
128     silc_cipher_free(idata->send_key);
129     silc_cipher_free(idata->receive_key);
130     silc_hash_free(idata->hash);
131     silc_free(conn_data);
132     return FALSE;
133   }
134   silc_hmac_set_key(idata->hmac_send, keymat->hmac_key, keymat->hmac_key_len);
135   idata->hmac_receive = idata->hmac_send;
136
137   sock->user_data = (void *)conn_data;
138
139   return TRUE;
140 }
141
142 /* Check remote host version string */
143
144 SilcSKEStatus silc_ske_check_version(SilcSKE ske, unsigned char *version,
145                                      uint32 len)
146 {
147   SilcSKEStatus status = SILC_SKE_STATUS_OK;
148   char *cp;
149   int maj = 0, min = 0, build = 0, maj2, min2, build2;
150
151   SILC_LOG_INFO(("%s (%s) is version %s", ske->sock->hostname,
152                  ske->sock->ip, version));
153
154   /* Check for initial version string */
155   if (!strstr(version, "SILC-1.0-"))
156     status = SILC_SKE_STATUS_BAD_VERSION;
157
158   /* Check software version */
159
160   cp = version + 9;
161   if (!cp)
162     status = SILC_SKE_STATUS_BAD_VERSION;
163
164   maj = atoi(cp);
165   cp = strchr(cp, '.');
166   if (cp) {
167     min = atoi(cp + 1);
168     cp++;
169   }
170   if (cp) {
171     cp = strchr(cp, '.');
172     if (cp)
173       build = atoi(cp + 1);
174   }
175
176   cp = silc_version_string + 9;
177   if (!cp)
178     status = SILC_SKE_STATUS_BAD_VERSION;
179
180   maj2 = atoi(cp);
181   cp = strchr(cp, '.');
182   if (cp) {
183     min2 = atoi(cp + 1);
184     cp++;
185   }
186   if (cp) {
187     cp = strchr(cp, '.');
188     if (cp)
189       build2 = atoi(cp + 1);
190   }
191
192   if (maj != maj2)
193     status = SILC_SKE_STATUS_BAD_VERSION;
194 #if 0
195   if (min < min2)
196     status = SILC_SKE_STATUS_BAD_VERSION;
197 #endif
198
199   return status;
200 }
201
202 /* Performs key exchange protocol. This is used for both initiator
203    and responder key exchange. This is performed always when accepting
204    new connection to the server. This may be called recursively. */
205
206 SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
207 {
208   SilcProtocol protocol = (SilcProtocol)context;
209   SilcServerKEInternalContext *ctx = 
210     (SilcServerKEInternalContext *)protocol->context;
211   SilcServer server = (SilcServer)ctx->server;
212   SilcSKEStatus status = 0;
213
214   SILC_LOG_DEBUG(("Start"));
215
216   if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
217     protocol->state = SILC_PROTOCOL_STATE_START;
218
219   SILC_LOG_DEBUG(("State=%d", protocol->state));
220
221   switch(protocol->state) {
222   case SILC_PROTOCOL_STATE_START:
223     {
224       /*
225        * Start protocol
226        */
227       SilcSKE ske;
228
229       /* Allocate Key Exchange object */
230       ske = silc_ske_alloc();
231       ctx->ske = ske;
232       ske->rng = server->rng;
233       
234       if (ctx->responder == TRUE) {
235         /* Start the key exchange by processing the received security
236            properties packet from initiator. */
237         status = silc_ske_responder_start(ske, ctx->rng, ctx->sock,
238                                           silc_version_string,
239                                           ctx->packet->buffer, FALSE,
240                                           NULL, NULL);
241       } else {
242         SilcSKEStartPayload *start_payload;
243
244         /* Assemble security properties. */
245         silc_ske_assemble_security_properties(ske, SILC_SKE_SP_FLAG_NONE, 
246                                               silc_version_string,
247                                               &start_payload);
248
249         /* Start the key exchange by sending our security properties
250            to the remote end. */
251         status = silc_ske_initiator_start(ske, ctx->rng, ctx->sock,
252                                           start_payload,
253                                           silc_server_protocol_ke_send_packet,
254                                           context);
255       }
256
257       if (status != SILC_SKE_STATUS_OK) {
258         SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
259                           status));
260         SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
261                         status));
262
263         protocol->state = SILC_PROTOCOL_STATE_ERROR;
264         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
265         return;
266       }
267
268       /* Advance protocol state and call the next state if we are responder */
269       protocol->state++;
270       if (ctx->responder == TRUE)
271         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 100000);
272     }
273     break;
274   case 2:
275     {
276       /* 
277        * Phase 1 
278        */
279       if (ctx->responder == TRUE) {
280         /* Sends the selected security properties to the initiator. */
281         status = 
282           silc_ske_responder_phase_1(ctx->ske, 
283                                      ctx->ske->start_payload,
284                                      silc_server_protocol_ke_send_packet,
285                                      context);
286       } else {
287         /* Call Phase-1 function. This processes the Key Exchange Start
288            paylaod reply we just got from the responder. The callback
289            function will receive the processed payload where we will
290            save it. */
291         status = silc_ske_initiator_phase_1(ctx->ske, ctx->packet->buffer,
292                                             NULL, NULL);
293       }
294
295       if (status != SILC_SKE_STATUS_OK) {
296         SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
297                           status));
298         SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
299                         status));
300
301         protocol->state = SILC_PROTOCOL_STATE_ERROR;
302         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
303         return;
304       }
305
306       /* Advance protocol state and call next state if we are initiator */
307       protocol->state++;
308       if (ctx->responder == FALSE)
309         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 100000);
310     }
311     break;
312   case 3:
313     {
314       /* 
315        * Phase 2 
316        */
317       if (ctx->responder == TRUE) {
318         /* Process the received Key Exchange 1 Payload packet from
319            the initiator. This also creates our parts of the Diffie
320            Hellman algorithm. */
321         status = silc_ske_responder_phase_2(ctx->ske, ctx->packet->buffer, 
322                                             NULL, NULL, NULL, NULL);
323       } else {
324         /* Call the Phase-2 function. This creates Diffie Hellman
325            key exchange parameters and sends our public part inside
326            Key Exhange 1 Payload to the responder. */
327         status = 
328           silc_ske_initiator_phase_2(ctx->ske,
329                                      server->public_key,
330                                      server->private_key,
331                                      silc_server_protocol_ke_send_packet,
332                                      context);
333       }
334
335       if (status != SILC_SKE_STATUS_OK) {
336         SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
337                           status));
338         SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
339                         status));
340
341         protocol->state = SILC_PROTOCOL_STATE_ERROR;
342         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
343         return;
344       }
345
346       /* Advance protocol state and call the next state if we are responder */
347       protocol->state++;
348       if (ctx->responder == TRUE)
349         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 100000);
350     }
351     break;
352   case 4:
353     {
354       /* 
355        * Finish protocol
356        */
357       if (ctx->responder == TRUE) {
358         /* This creates the key exchange material and sends our
359            public parts to the initiator inside Key Exchange 2 Payload. */
360         status = 
361           silc_ske_responder_finish(ctx->ske, 
362                                     server->public_key, server->private_key,
363                                     SILC_SKE_PK_TYPE_SILC,
364                                     silc_server_protocol_ke_send_packet,
365                                     context);
366       } else {
367         /* Finish the protocol. This verifies the Key Exchange 2 payload
368            sent by responder. */
369         status = silc_ske_initiator_finish(ctx->ske, ctx->packet->buffer, 
370                                            NULL, NULL, NULL, NULL);
371       }
372
373       if (status != SILC_SKE_STATUS_OK) {
374         SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
375                           status));
376         SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
377                         status));
378
379         protocol->state = SILC_PROTOCOL_STATE_ERROR;
380         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
381         return;
382       }
383
384       /* Send Ok to the other end. We will end the protocol as responder
385          sends Ok to us when we will take the new keys into use. */
386       if (ctx->responder == FALSE)
387         silc_ske_end(ctx->ske, silc_server_protocol_ke_send_packet, context);
388
389       /* End the protocol on the next round */
390       protocol->state = SILC_PROTOCOL_STATE_END;
391     }
392     break;
393
394   case SILC_PROTOCOL_STATE_END:
395     {
396       /* 
397        * End protocol
398        */
399       SilcSKEKeyMaterial *keymat;
400       int key_len = silc_cipher_get_key_len(ctx->ske->prop->cipher);
401       int hash_len = ctx->ske->prop->hash->hash->hash_len;
402
403       /* Process the key material */
404       keymat = silc_calloc(1, sizeof(*keymat));
405       status = silc_ske_process_key_material(ctx->ske, 16, key_len, hash_len,
406                                              keymat);
407       if (status != SILC_SKE_STATUS_OK) {
408         protocol->state = SILC_PROTOCOL_STATE_ERROR;
409         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
410         silc_ske_free_key_material(keymat);
411         return;
412       }
413       ctx->keymat = keymat;
414
415       /* Send Ok to the other end if we are responder. If we are initiator
416          we have sent this already. */
417       if (ctx->responder == TRUE)
418         silc_ske_end(ctx->ske, silc_server_protocol_ke_send_packet, context);
419
420       /* Unregister the timeout task since the protocol has ended. 
421          This was the timeout task to be executed if the protocol is
422          not completed fast enough. */
423       if (ctx->timeout_task)
424         silc_task_unregister(server->timeout_queue, ctx->timeout_task);
425
426       /* Call the final callback */
427       if (protocol->final_callback)
428         protocol->execute_final(server->timeout_queue, 0, protocol, fd);
429       else
430         silc_protocol_free(protocol);
431     }
432     break;
433
434   case SILC_PROTOCOL_STATE_ERROR:
435     /*
436      * Error occured
437      */
438
439     /* Send abort notification */
440     silc_ske_abort(ctx->ske, ctx->ske->status, 
441                    silc_server_protocol_ke_send_packet,
442                    context);
443
444     /* Unregister the timeout task since the protocol has ended. 
445        This was the timeout task to be executed if the protocol is
446        not completed fast enough. */
447     if (ctx->timeout_task)
448       silc_task_unregister(server->timeout_queue, ctx->timeout_task);
449
450     /* On error the final callback is always called. */
451     if (protocol->final_callback)
452       protocol->execute_final(server->timeout_queue, 0, protocol, fd);
453     else
454       silc_protocol_free(protocol);
455     break;
456
457   case SILC_PROTOCOL_STATE_FAILURE:
458     /*
459      * We have received failure from remote
460      */
461
462     /* Unregister the timeout task since the protocol has ended. 
463        This was the timeout task to be executed if the protocol is
464        not completed fast enough. */
465     if (ctx->timeout_task)
466       silc_task_unregister(server->timeout_queue, ctx->timeout_task);
467
468     /* On error the final callback is always called. */
469     if (protocol->final_callback)
470       protocol->execute_final(server->timeout_queue, 0, protocol, fd);
471     else
472       silc_protocol_free(protocol);
473     break;
474
475   case SILC_PROTOCOL_STATE_UNKNOWN:
476     break;
477   }
478 }
479
480 /*
481  * Connection Authentication protocol functions
482  */
483
484 static int 
485 silc_server_password_authentication(SilcServer server, char *auth1, 
486                                     char *auth2)
487 {
488   if (!auth1 || !auth2)
489     return FALSE;
490
491   if (!memcmp(auth1, auth2, strlen(auth1)))
492     return TRUE;
493
494   return FALSE;
495 }
496
497 static int
498 silc_server_public_key_authentication(SilcServer server,
499                                       SilcPublicKey pub_key,
500                                       unsigned char *sign,
501                                       uint32 sign_len,
502                                       SilcSKE ske)
503 {
504   SilcPKCS pkcs;
505   int len;
506   SilcBuffer auth;
507
508   if (!pub_key || !sign)
509     return FALSE;
510
511   silc_pkcs_alloc(pub_key->name, &pkcs);
512   if (!silc_pkcs_public_key_set(pkcs, pub_key)) {
513     silc_pkcs_free(pkcs);
514     return FALSE;
515   }
516
517   /* Make the authentication data. Protocol says it is HASH plus
518      KE Start Payload. */
519   len = ske->hash_len + ske->start_payload_copy->len;
520   auth = silc_buffer_alloc(len);
521   silc_buffer_pull_tail(auth, len);
522   silc_buffer_format(auth,
523                      SILC_STR_UI_XNSTRING(ske->hash, ske->hash_len),
524                      SILC_STR_UI_XNSTRING(ske->start_payload_copy->data,
525                                           ske->start_payload_copy->len),
526                      SILC_STR_END);
527
528   /* Verify signature */
529   if (silc_pkcs_verify(pkcs, sign, sign_len, auth->data, auth->len)) {
530     silc_pkcs_free(pkcs);
531     silc_buffer_free(auth);
532     return TRUE;
533   }
534
535   silc_pkcs_free(pkcs);
536   silc_buffer_free(auth);
537   return FALSE;
538 }
539
540 static int
541 silc_server_get_public_key_auth(SilcServer server,
542                                 SilcPublicKey pub_key,
543                                 unsigned char *auth_data,
544                                 uint32 *auth_data_len,
545                                 SilcSKE ske)
546 {
547   int len;
548   SilcPKCS pkcs;
549   SilcBuffer auth;
550
551   if (!pub_key)
552     return FALSE;
553
554   silc_pkcs_alloc(pub_key->name, &pkcs);
555   if (!silc_pkcs_public_key_set(pkcs, pub_key)) {
556     silc_pkcs_free(pkcs);
557     return FALSE;
558   }
559
560   /* Make the authentication data. Protocol says it is HASH plus
561      KE Start Payload. */
562   len = ske->hash_len + ske->start_payload_copy->len;
563   auth = silc_buffer_alloc(len);
564   silc_buffer_pull_tail(auth, len);
565   silc_buffer_format(auth,
566                      SILC_STR_UI_XNSTRING(ske->hash, ske->hash_len),
567                      SILC_STR_UI_XNSTRING(ske->start_payload_copy->data,
568                                           ske->start_payload_copy->len),
569                      SILC_STR_END);
570
571   if (silc_pkcs_sign(pkcs, auth->data, auth->len, auth_data, auth_data_len)) {
572     silc_pkcs_free(pkcs);
573     silc_buffer_free(auth);
574     return TRUE;
575   }
576
577   silc_pkcs_free(pkcs);
578   silc_buffer_free(auth);
579   return FALSE;
580 }
581
582 /* Performs connection authentication protocol. If responder, we 
583    authenticate the remote data received. If initiator, we will send
584    authentication data to the remote end. */
585
586 SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
587 {
588   SilcProtocol protocol = (SilcProtocol)context;
589   SilcServerConnAuthInternalContext *ctx = 
590     (SilcServerConnAuthInternalContext *)protocol->context;
591   SilcServer server = (SilcServer)ctx->server;
592
593   SILC_LOG_DEBUG(("Start"));
594
595   if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
596     protocol->state = SILC_PROTOCOL_STATE_START;
597
598   SILC_LOG_DEBUG(("State=%d", protocol->state));
599
600   switch(protocol->state) {
601   case SILC_PROTOCOL_STATE_START:
602     {
603       /* 
604        * Start protocol.
605        */
606
607       if (ctx->responder == TRUE) {
608         /*
609          * We are receiving party
610          */
611         int ret;
612         uint16 payload_len;
613         uint16 conn_type;
614         unsigned char *auth_data = NULL;
615
616         SILC_LOG_INFO(("Performing authentication protocol for %s (%s)",
617                        ctx->sock->hostname, ctx->sock->ip));
618
619         /* Parse the received authentication data packet. The received
620            payload is Connection Auth Payload. */
621         ret = silc_buffer_unformat(ctx->packet->buffer,
622                                    SILC_STR_UI_SHORT(&payload_len),
623                                    SILC_STR_UI_SHORT(&conn_type),
624                                    SILC_STR_END);
625         if (ret == -1) {
626           SILC_LOG_DEBUG(("Bad payload in authentication packet"));
627           protocol->state = SILC_PROTOCOL_STATE_ERROR;
628           protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
629           return;
630         }
631         
632         if (payload_len != ctx->packet->buffer->len) {
633           SILC_LOG_DEBUG(("Bad payload in authentication packet"));
634           protocol->state = SILC_PROTOCOL_STATE_ERROR;
635           protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
636           return;
637         }
638         
639         payload_len -= 4;
640         
641         if (conn_type < SILC_SOCKET_TYPE_CLIENT || 
642             conn_type > SILC_SOCKET_TYPE_ROUTER) {
643           SILC_LOG_ERROR(("Bad connection type %d", conn_type));
644           protocol->state = SILC_PROTOCOL_STATE_ERROR;
645           protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
646           return;
647         }
648         
649         if (payload_len > 0) {
650           /* Get authentication data */
651           silc_buffer_pull(ctx->packet->buffer, 4);
652           ret = silc_buffer_unformat(ctx->packet->buffer,
653                                      SILC_STR_UI_XNSTRING_ALLOC(&auth_data, 
654                                                                 payload_len),
655                                      SILC_STR_END);
656           if (ret == -1) {
657             SILC_LOG_DEBUG(("Bad payload in authentication packet"));
658             protocol->state = SILC_PROTOCOL_STATE_ERROR;
659             protocol->execute(server->timeout_queue, 0, 
660                               protocol, fd, 0, 300000);
661             return;
662           }
663         }
664
665         /* 
666          * Check the remote connection type and make sure that we have
667          * configured this connection. If we haven't allowed this connection
668          * the authentication must be failed.
669          */
670
671         SILC_LOG_DEBUG(("Remote connection type %d", conn_type));
672
673         /* Remote end is client */
674         if (conn_type == SILC_SOCKET_TYPE_CLIENT) {
675           SilcServerConfigSectionClientConnection *client = ctx->cconfig;
676           
677           if (client) {
678             switch(client->auth_meth) {
679             case SILC_AUTH_NONE:
680               /* No authentication required */
681               SILC_LOG_DEBUG(("No authentication required"));
682               break;
683               
684             case SILC_AUTH_PASSWORD:
685               /* Password authentication */
686               SILC_LOG_DEBUG(("Password authentication"));
687               ret = silc_server_password_authentication(server, auth_data,
688                                                         client->auth_data);
689
690               if (ret)
691                 break;
692
693               /* Authentication failed */
694               SILC_LOG_ERROR(("Authentication failed"));
695               SILC_LOG_DEBUG(("Authentication failed"));
696               silc_free(auth_data);
697               protocol->state = SILC_PROTOCOL_STATE_ERROR;
698               protocol->execute(server->timeout_queue, 0, 
699                                 protocol, fd, 0, 300000);
700               return;
701               break;
702               
703             case SILC_AUTH_PUBLIC_KEY:
704               /* Public key authentication */
705               SILC_LOG_DEBUG(("Public key authentication"));
706               ret = silc_server_public_key_authentication(server, 
707                                                           client->auth_data,
708                                                           auth_data,
709                                                           payload_len, 
710                                                           ctx->ske);
711
712               if (ret)
713                 break;
714
715               SILC_LOG_ERROR(("Authentication failed"));
716               SILC_LOG_DEBUG(("Authentication failed"));
717               silc_free(auth_data);
718               protocol->state = SILC_PROTOCOL_STATE_ERROR;
719               protocol->execute(server->timeout_queue, 0, 
720                                 protocol, fd, 0, 300000);
721               return;
722             }
723           } else {
724             SILC_LOG_DEBUG(("No configuration for remote connection"));
725             SILC_LOG_ERROR(("Remote connection not configured"));
726             SILC_LOG_ERROR(("Authentication failed"));
727             silc_free(auth_data);
728             protocol->state = SILC_PROTOCOL_STATE_ERROR;
729             protocol->execute(server->timeout_queue, 0, 
730                               protocol, fd, 0, 300000);
731             return;
732           }
733         }
734         
735         /* Remote end is server */
736         if (conn_type == SILC_SOCKET_TYPE_SERVER) {
737           SilcServerConfigSectionServerConnection *serv = ctx->sconfig;
738           
739           if (serv) {
740             switch(serv->auth_meth) {
741             case SILC_AUTH_NONE:
742               /* No authentication required */
743               SILC_LOG_DEBUG(("No authentication required"));
744               break;
745               
746             case SILC_AUTH_PASSWORD:
747               /* Password authentication */
748               SILC_LOG_DEBUG(("Password authentication"));
749               ret = silc_server_password_authentication(server, auth_data,
750                                                         serv->auth_data);
751
752               if (ret)
753                 break;
754               
755               /* Authentication failed */
756               SILC_LOG_ERROR(("Authentication failed"));
757               SILC_LOG_DEBUG(("Authentication failed"));
758               silc_free(auth_data);
759               protocol->state = SILC_PROTOCOL_STATE_ERROR;
760               protocol->execute(server->timeout_queue, 0, 
761                                 protocol, fd, 0, 300000);
762               return;
763               break;
764
765             case SILC_AUTH_PUBLIC_KEY:
766               /* Public key authentication */
767               SILC_LOG_DEBUG(("Public key authentication"));
768               ret = silc_server_public_key_authentication(server, 
769                                                           serv->auth_data,
770                                                           auth_data,
771                                                           payload_len, 
772                                                           ctx->ske);
773                                                           
774               if (ret)
775                 break;
776
777               SILC_LOG_ERROR(("Authentication failed"));
778               SILC_LOG_DEBUG(("Authentication failed"));
779               silc_free(auth_data);
780               protocol->state = SILC_PROTOCOL_STATE_ERROR;
781               protocol->execute(server->timeout_queue, 0, 
782                                 protocol, fd, 0, 300000);
783               return;
784             }
785           } else {
786             SILC_LOG_DEBUG(("No configuration for remote connection"));
787             SILC_LOG_ERROR(("Remote connection not configured"));
788             SILC_LOG_ERROR(("Authentication failed"));
789             protocol->state = SILC_PROTOCOL_STATE_ERROR;
790             protocol->execute(server->timeout_queue, 0, 
791                               protocol, fd, 0, 300000);
792             silc_free(auth_data);
793             return;
794           }
795         }
796         
797         /* Remote end is router */
798         if (conn_type == SILC_SOCKET_TYPE_ROUTER) {
799           SilcServerConfigSectionServerConnection *serv = ctx->rconfig;
800
801           if (serv) {
802             switch(serv->auth_meth) {
803             case SILC_AUTH_NONE:
804               /* No authentication required */
805               SILC_LOG_DEBUG(("No authentication required"));
806               break;
807               
808             case SILC_AUTH_PASSWORD:
809               /* Password authentication */
810               SILC_LOG_DEBUG(("Password authentication"));
811               ret = silc_server_password_authentication(server, auth_data,
812                                                         serv->auth_data);
813
814               if (ret)
815                 break;
816               
817               /* Authentication failed */
818               SILC_LOG_ERROR(("Authentication failed"));
819               SILC_LOG_DEBUG(("Authentication failed"));
820               silc_free(auth_data);
821               protocol->state = SILC_PROTOCOL_STATE_ERROR;
822               protocol->execute(server->timeout_queue, 0, 
823                                 protocol, fd, 0, 300000);
824               return;
825               break;
826               
827             case SILC_AUTH_PUBLIC_KEY:
828               /* Public key authentication */
829               SILC_LOG_DEBUG(("Public key authentication"));
830               ret = silc_server_public_key_authentication(server, 
831                                                           serv->auth_data,
832                                                           auth_data,
833                                                           payload_len, 
834                                                           ctx->ske);
835                                                           
836               if (ret)
837                 break;
838               
839               SILC_LOG_ERROR(("Authentication failed"));
840               SILC_LOG_DEBUG(("Authentication failed"));
841               silc_free(auth_data);
842               protocol->state = SILC_PROTOCOL_STATE_ERROR;
843               protocol->execute(server->timeout_queue, 0, 
844                                 protocol, fd, 0, 300000);
845               return;
846             }
847           } else {
848             SILC_LOG_DEBUG(("No configuration for remote connection"));
849             SILC_LOG_ERROR(("Remote connection not configured"));
850             SILC_LOG_ERROR(("Authentication failed"));
851             silc_free(auth_data);
852             protocol->state = SILC_PROTOCOL_STATE_ERROR;
853             protocol->execute(server->timeout_queue, 0, 
854                               protocol, fd, 0, 300000);
855             return;
856           }
857         }
858         
859         silc_free(auth_data);
860
861         /* Save connection type. This is later used to create the
862            ID for the connection. */
863         ctx->conn_type = conn_type;
864           
865         /* Advance protocol state. */
866         protocol->state = SILC_PROTOCOL_STATE_END;
867         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 0);
868
869       } else {
870         /* 
871          * We are initiator. We are authenticating ourselves to a
872          * remote server. We will send the authentication data to the
873          * other end for verify. 
874          */
875         SilcBuffer packet;
876         int payload_len = 0;
877         unsigned char *auth_data = NULL;
878         uint32 auth_data_len = 0;
879         
880         switch(ctx->auth_meth) {
881         case SILC_AUTH_NONE:
882           /* No authentication required */
883           break;
884           
885         case SILC_AUTH_PASSWORD:
886           /* Password authentication */
887           if (ctx->auth_data && ctx->auth_data_len) {
888             auth_data = strdup(ctx->auth_data);
889             auth_data_len = ctx->auth_data_len;
890             break;
891           }
892           break;
893           
894         case SILC_AUTH_PUBLIC_KEY:
895           {
896             unsigned char sign[1024];
897
898             /* Public key authentication */
899             silc_server_get_public_key_auth(server, ctx->auth_data,
900                                             sign, &auth_data_len,
901                                             ctx->ske);
902             auth_data = silc_calloc(auth_data_len, sizeof(*auth_data));
903             memcpy(auth_data, sign, auth_data_len);
904             break;
905           }
906         }
907         
908         payload_len = 4 + auth_data_len;
909         packet = silc_buffer_alloc(payload_len);
910         silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
911         silc_buffer_format(packet,
912                            SILC_STR_UI_SHORT(payload_len),
913                            SILC_STR_UI_SHORT(server->server_type 
914                                               == SILC_SERVER ?
915                                               SILC_SOCKET_TYPE_SERVER :
916                                               SILC_SOCKET_TYPE_ROUTER),
917                            SILC_STR_UI_XNSTRING(auth_data, auth_data_len),
918                            SILC_STR_END);
919         
920         /* Send the packet to server */
921         silc_server_packet_send(server, ctx->sock,
922                                 SILC_PACKET_CONNECTION_AUTH, 0, 
923                                 packet->data, packet->len, TRUE);
924         
925         if (auth_data) {
926           memset(auth_data, 0, auth_data_len);
927           silc_free(auth_data);
928         }
929         silc_buffer_free(packet);
930         
931         /* Next state is end of protocol */
932         protocol->state = SILC_PROTOCOL_STATE_END;
933       }
934     }
935     break;
936
937   case SILC_PROTOCOL_STATE_END:
938     {
939       /* 
940        * End protocol
941        */
942       unsigned char ok[4];
943
944       SILC_PUT32_MSB(SILC_AUTH_OK, ok);
945
946       /* Authentication successful */
947       silc_server_packet_send(server, ctx->sock, SILC_PACKET_SUCCESS,
948                               0, ok, 4, TRUE);
949
950       /* Unregister the timeout task since the protocol has ended. 
951          This was the timeout task to be executed if the protocol is
952          not completed fast enough. */
953       if (ctx->timeout_task)
954         silc_task_unregister(server->timeout_queue, ctx->timeout_task);
955
956       /* Protocol has ended, call the final callback */
957       if (protocol->final_callback)
958         protocol->execute_final(server->timeout_queue, 0, protocol, fd);
959       else
960         silc_protocol_free(protocol);
961     }
962     break;
963   case SILC_PROTOCOL_STATE_ERROR:
964     {
965       /*
966        * Error. Send notify to remote.
967        */
968       unsigned char error[4];
969
970       SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
971
972       /* Authentication failed */
973       silc_server_packet_send(server, ctx->sock, SILC_PACKET_FAILURE,
974                               0, error, 4, TRUE);
975
976       /* Unregister the timeout task since the protocol has ended. 
977          This was the timeout task to be executed if the protocol is
978          not completed fast enough. */
979       if (ctx->timeout_task)
980         silc_task_unregister(server->timeout_queue, ctx->timeout_task);
981
982       /* On error the final callback is always called. */
983       if (protocol->final_callback)
984         protocol->execute_final(server->timeout_queue, 0, protocol, fd);
985       else
986         silc_protocol_free(protocol);
987     }
988     break;
989
990   case SILC_PROTOCOL_STATE_FAILURE:
991     /*
992      * We have received failure from remote
993      */
994
995     /* Unregister the timeout task since the protocol has ended. 
996        This was the timeout task to be executed if the protocol is
997        not completed fast enough. */
998     if (ctx->timeout_task)
999       silc_task_unregister(server->timeout_queue, ctx->timeout_task);
1000
1001     /* On error the final callback is always called. */
1002     if (protocol->final_callback)
1003       protocol->execute_final(server->timeout_queue, 0, protocol, fd);
1004     else
1005       silc_protocol_free(protocol);
1006     break;
1007
1008   case SILC_PROTOCOL_STATE_UNKNOWN:
1009     break;
1010   }
1011 }
1012
1013 /*
1014  * Re-key protocol routines
1015  */
1016
1017 /* Actually takes the new keys into use. */
1018
1019 static void 
1020 silc_server_protocol_rekey_validate(SilcServer server,
1021                                     SilcServerRekeyInternalContext *ctx,
1022                                     SilcIDListData idata,
1023                                     SilcSKEKeyMaterial *keymat,
1024                                     bool send)
1025 {
1026   if (ctx->responder == TRUE) {
1027     if (send) {
1028       silc_cipher_set_key(idata->send_key, keymat->receive_enc_key, 
1029                           keymat->enc_key_len);
1030       silc_cipher_set_iv(idata->send_key, keymat->receive_iv);
1031     } else {
1032       silc_cipher_set_key(idata->receive_key, keymat->send_enc_key, 
1033                           keymat->enc_key_len);
1034       silc_cipher_set_iv(idata->receive_key, keymat->send_iv);
1035     }
1036   } else {
1037     if (send) {
1038       silc_cipher_set_key(idata->send_key, keymat->send_enc_key, 
1039                           keymat->enc_key_len);
1040       silc_cipher_set_iv(idata->send_key, keymat->send_iv);
1041     } else {
1042       silc_cipher_set_key(idata->receive_key, keymat->receive_enc_key, 
1043                           keymat->enc_key_len);
1044       silc_cipher_set_iv(idata->receive_key, keymat->receive_iv);
1045     }
1046   }
1047
1048   if (send) {
1049     silc_hmac_alloc(idata->hmac_send->hmac->name, NULL, &idata->hmac_send);
1050     silc_hmac_set_key(idata->hmac_send, keymat->hmac_key, 
1051                       keymat->hmac_key_len);
1052   } else {
1053     silc_hmac_free(idata->hmac_receive);
1054     idata->hmac_receive = idata->hmac_send;
1055   }
1056
1057   /* Save the current sending encryption key */
1058   if (!send) {
1059     memset(idata->rekey->send_enc_key, 0, idata->rekey->enc_key_len);
1060     silc_free(idata->rekey->send_enc_key);
1061     idata->rekey->send_enc_key = 
1062       silc_calloc(keymat->enc_key_len / 8,
1063                   sizeof(*idata->rekey->send_enc_key));
1064     memcpy(idata->rekey->send_enc_key, keymat->send_enc_key, 
1065            keymat->enc_key_len / 8);
1066     idata->rekey->enc_key_len = keymat->enc_key_len / 8;
1067   }
1068 }
1069
1070 /* This function actually re-generates (when not using PFS) the keys and
1071    takes them into use. */
1072
1073 void silc_server_protocol_rekey_generate(SilcServer server,
1074                                          SilcServerRekeyInternalContext *ctx,
1075                                          bool send)
1076 {
1077   SilcIDListData idata = (SilcIDListData)ctx->sock->user_data;
1078   SilcSKEKeyMaterial *keymat;
1079   uint32 key_len = silc_cipher_get_key_len(idata->send_key);
1080   uint32 hash_len = idata->hash->hash->hash_len;
1081
1082   SILC_LOG_DEBUG(("Generating new %s session keys (no PFS)",
1083                   send ? "sending" : "receiving"));
1084
1085   /* Generate the new key */
1086   keymat = silc_calloc(1, sizeof(*keymat));
1087   silc_ske_process_key_material_data(idata->rekey->send_enc_key,
1088                                      idata->rekey->enc_key_len,
1089                                      16, key_len, hash_len, 
1090                                      idata->hash, keymat);
1091
1092   /* Set the keys into use */
1093   silc_server_protocol_rekey_validate(server, ctx, idata, keymat, send);
1094
1095   silc_ske_free_key_material(keymat);
1096 }
1097
1098 /* This function actually re-generates (with PFS) the keys and
1099    takes them into use. */
1100
1101 void 
1102 silc_server_protocol_rekey_generate_pfs(SilcServer server,
1103                                         SilcServerRekeyInternalContext *ctx,
1104                                         bool send)
1105 {
1106   SilcIDListData idata = (SilcIDListData)ctx->sock->user_data;
1107   SilcSKEKeyMaterial *keymat;
1108   uint32 key_len = silc_cipher_get_key_len(idata->send_key);
1109   uint32 hash_len = idata->hash->hash->hash_len;
1110   unsigned char *tmpbuf;
1111   uint32 klen;
1112
1113   SILC_LOG_DEBUG(("Generating new %s session keys (with PFS)",
1114                   send ? "sending" : "receiving"));
1115
1116   /* Encode KEY to binary data */
1117   tmpbuf = silc_mp_mp2bin(ctx->ske->KEY, 0, &klen);
1118
1119   /* Generate the new key */
1120   keymat = silc_calloc(1, sizeof(*keymat));
1121   silc_ske_process_key_material_data(tmpbuf, klen, 16, key_len, hash_len, 
1122                                      idata->hash, keymat);
1123
1124   /* Set the keys into use */
1125   silc_server_protocol_rekey_validate(server, ctx, idata, keymat, send);
1126
1127   memset(tmpbuf, 0, klen);
1128   silc_free(tmpbuf);
1129   silc_ske_free_key_material(keymat);
1130 }
1131
1132 /* Packet sending callback. This function is provided as packet sending
1133    routine to the Key Exchange functions. */
1134
1135 static void 
1136 silc_server_protocol_rekey_send_packet(SilcSKE ske,
1137                                        SilcBuffer packet,
1138                                        SilcPacketType type,
1139                                        void *context)
1140 {
1141   SilcProtocol protocol = (SilcProtocol)context;
1142   SilcServerRekeyInternalContext *ctx = 
1143     (SilcServerRekeyInternalContext *)protocol->context;
1144   SilcServer server = (SilcServer)ctx->server;
1145
1146   /* Send the packet immediately */
1147   silc_server_packet_send(server, ctx->sock,
1148                           type, 0, packet->data, packet->len, FALSE);
1149 }
1150
1151 /* Performs re-key as defined in the SILC protocol specification. */
1152
1153 SILC_TASK_CALLBACK(silc_server_protocol_rekey)
1154 {
1155   SilcProtocol protocol = (SilcProtocol)context;
1156   SilcServerRekeyInternalContext *ctx = 
1157     (SilcServerRekeyInternalContext *)protocol->context;
1158   SilcServer server = (SilcServer)ctx->server;
1159   SilcIDListData idata = (SilcIDListData)ctx->sock->user_data;
1160   SilcSKEStatus status;
1161
1162   SILC_LOG_DEBUG(("Start"));
1163
1164   if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
1165     protocol->state = SILC_PROTOCOL_STATE_START;
1166
1167   SILC_LOG_DEBUG(("State=%d", protocol->state));
1168
1169   switch(protocol->state) {
1170   case SILC_PROTOCOL_STATE_START:
1171     {
1172       /* 
1173        * Start protocol.
1174        */
1175
1176       if (ctx->responder == TRUE) {
1177         /*
1178          * We are receiving party
1179          */
1180
1181         if (ctx->pfs == TRUE) {
1182           /* 
1183            * Use Perfect Forward Secrecy, ie. negotiate the key material
1184            * using the SKE protocol.
1185            */
1186
1187           if (ctx->packet->type != SILC_PACKET_KEY_EXCHANGE_1) {
1188             /* Error in protocol */
1189             protocol->state = SILC_PROTOCOL_STATE_ERROR;
1190             protocol->execute(server->timeout_queue, 0, protocol, fd, 
1191                               0, 300000);
1192           }
1193
1194           ctx->ske = silc_ske_alloc();
1195           ctx->ske->rng = server->rng;
1196           ctx->ske->prop = silc_calloc(1, sizeof(*ctx->ske->prop));
1197           silc_ske_get_group_by_number(idata->rekey->ske_group,
1198                                        &ctx->ske->prop->group);
1199
1200           status = silc_ske_responder_phase_2(ctx->ske, ctx->packet->buffer,
1201                                               NULL, NULL, NULL, NULL);
1202           if (status != SILC_SKE_STATUS_OK) {
1203             SILC_LOG_WARNING(("Error (type %d) during Re-key (PFS)",
1204                               status));
1205             
1206             protocol->state = SILC_PROTOCOL_STATE_ERROR;
1207             protocol->execute(server->timeout_queue, 0, 
1208                               protocol, fd, 0, 300000);
1209             return;
1210           }
1211
1212           /* Advance the protocol state */
1213           protocol->state++;
1214           protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 0);
1215         } else {
1216           /*
1217            * Do normal and simple re-key.
1218            */
1219
1220           /* Send the REKEY_DONE to indicate we will take new keys into use */
1221           silc_server_packet_send(server, ctx->sock, SILC_PACKET_REKEY_DONE,
1222                                   0, NULL, 0, FALSE);
1223
1224           /* After we send REKEY_DONE we must set the sending encryption
1225              key to the new key since all packets after this packet must
1226              encrypted with the new key. */
1227           silc_server_protocol_rekey_generate(server, ctx, TRUE);
1228
1229           /* The protocol ends in next stage. */
1230           protocol->state = SILC_PROTOCOL_STATE_END;
1231         }
1232       
1233       } else {
1234         /*
1235          * We are the initiator of this protocol
1236          */
1237
1238         /* Start the re-key by sending the REKEY packet */
1239         silc_server_packet_send(server, ctx->sock, SILC_PACKET_REKEY,
1240                                 0, NULL, 0, FALSE);
1241
1242         if (ctx->pfs == TRUE) {
1243           /* 
1244            * Use Perfect Forward Secrecy, ie. negotiate the key material
1245            * using the SKE protocol.
1246            */
1247           ctx->ske = silc_ske_alloc();
1248           ctx->ske->rng = server->rng;
1249           ctx->ske->prop = silc_calloc(1, sizeof(*ctx->ske->prop));
1250           silc_ske_get_group_by_number(idata->rekey->ske_group,
1251                                        &ctx->ske->prop->group);
1252
1253           status = 
1254             silc_ske_initiator_phase_2(ctx->ske, NULL, NULL,
1255                                        silc_server_protocol_rekey_send_packet,
1256                                        context);
1257
1258           if (status != SILC_SKE_STATUS_OK) {
1259             SILC_LOG_WARNING(("Error (type %d) during Re-key (PFS)",
1260                               status));
1261             
1262             protocol->state = SILC_PROTOCOL_STATE_ERROR;
1263             protocol->execute(server->timeout_queue, 0, 
1264                               protocol, fd, 0, 300000);
1265             return;
1266           }
1267
1268           /* Advance the protocol state */
1269           protocol->state++;
1270         } else {
1271           /*
1272            * Do normal and simple re-key.
1273            */
1274
1275           /* Send the REKEY_DONE to indicate we will take new keys into use 
1276              now. */ 
1277           silc_server_packet_send(server, ctx->sock, SILC_PACKET_REKEY_DONE,
1278                                   0, NULL, 0, FALSE);
1279
1280           /* After we send REKEY_DONE we must set the sending encryption
1281              key to the new key since all packets after this packet must
1282              encrypted with the new key. */
1283           silc_server_protocol_rekey_generate(server, ctx, TRUE);
1284
1285           /* The protocol ends in next stage. */
1286           protocol->state = SILC_PROTOCOL_STATE_END;
1287         }
1288       }
1289     }
1290     break;
1291
1292   case 2:
1293     /*
1294      * Second state, used only when oding re-key with PFS.
1295      */
1296     if (ctx->responder == TRUE) {
1297       if (ctx->pfs == TRUE) {
1298         /*
1299          * Send our KE packe to the initiator now that we've processed
1300          * the initiator's KE packet.
1301          */
1302         status = 
1303           silc_ske_responder_finish(ctx->ske, NULL, NULL, 
1304                                     SILC_SKE_PK_TYPE_SILC,
1305                                     silc_server_protocol_rekey_send_packet,
1306                                     context);
1307
1308           if (status != SILC_SKE_STATUS_OK) {
1309             SILC_LOG_WARNING(("Error (type %d) during Re-key (PFS)",
1310                               status));
1311             
1312             protocol->state = SILC_PROTOCOL_STATE_ERROR;
1313             protocol->execute(server->timeout_queue, 0, 
1314                               protocol, fd, 0, 300000);
1315             return;
1316           }
1317       }
1318
1319     } else {
1320       if (ctx->pfs == TRUE) {
1321         /*
1322          * The packet type must be KE packet
1323          */
1324         if (ctx->packet->type != SILC_PACKET_KEY_EXCHANGE_2) {
1325           /* Error in protocol */
1326           protocol->state = SILC_PROTOCOL_STATE_ERROR;
1327           protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
1328         }
1329         
1330         status = silc_ske_initiator_finish(ctx->ske, ctx->packet->buffer,
1331                                            NULL, NULL, NULL, NULL);
1332         if (status != SILC_SKE_STATUS_OK) {
1333           SILC_LOG_WARNING(("Error (type %d) during Re-key (PFS)",
1334                             status));
1335           
1336           protocol->state = SILC_PROTOCOL_STATE_ERROR;
1337           protocol->execute(server->timeout_queue, 0, 
1338                             protocol, fd, 0, 300000);
1339           return;
1340         }
1341       }
1342     }
1343
1344     /* Send the REKEY_DONE to indicate we will take new keys into use 
1345        now. */ 
1346     silc_server_packet_send(server, ctx->sock, SILC_PACKET_REKEY_DONE,
1347                             0, NULL, 0, FALSE);
1348     
1349     /* After we send REKEY_DONE we must set the sending encryption
1350        key to the new key since all packets after this packet must
1351        encrypted with the new key. */
1352     silc_server_protocol_rekey_generate_pfs(server, ctx, TRUE);
1353
1354     /* The protocol ends in next stage. */
1355     protocol->state = SILC_PROTOCOL_STATE_END;
1356     break;
1357
1358   case SILC_PROTOCOL_STATE_END:
1359     /* 
1360      * End protocol
1361      */
1362
1363     if (ctx->packet->type != SILC_PACKET_REKEY_DONE) {
1364       /* Error in protocol */
1365       protocol->state = SILC_PROTOCOL_STATE_ERROR;
1366       protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 0);
1367     }
1368
1369     /* We received the REKEY_DONE packet and all packets after this is
1370        encrypted with the new key so set the decryption key to the new key */
1371     silc_server_protocol_rekey_generate(server, ctx, FALSE);
1372
1373     /* Protocol has ended, call the final callback */
1374     if (protocol->final_callback)
1375       protocol->execute_final(server->timeout_queue, 0, protocol, fd);
1376     else
1377       silc_protocol_free(protocol);
1378     break;
1379
1380   case SILC_PROTOCOL_STATE_ERROR:
1381     /*
1382      * Error occured
1383      */
1384
1385     if (ctx->pfs == TRUE) {
1386       /* Send abort notification */
1387       silc_ske_abort(ctx->ske, ctx->ske->status, 
1388                      silc_server_protocol_ke_send_packet,
1389                      context);
1390     }
1391
1392     /* On error the final callback is always called. */
1393     if (protocol->final_callback)
1394       protocol->execute_final(server->timeout_queue, 0, protocol, fd);
1395     else
1396       silc_protocol_free(protocol);
1397     break;
1398
1399   case SILC_PROTOCOL_STATE_FAILURE:
1400     /*
1401      * We have received failure from remote
1402      */
1403
1404     /* On error the final callback is always called. */
1405     if (protocol->final_callback)
1406       protocol->execute_final(server->timeout_queue, 0, protocol, fd);
1407     else
1408       silc_protocol_free(protocol);
1409     break;
1410
1411   case SILC_PROTOCOL_STATE_UNKNOWN:
1412     break;
1413   }
1414
1415 }
1416
1417 /* Registers protocols used in server. */
1418
1419 void silc_server_protocols_register(void)
1420 {
1421   silc_protocol_register(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
1422                          silc_server_protocol_connection_auth);
1423   silc_protocol_register(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
1424                          silc_server_protocol_key_exchange);
1425   silc_protocol_register(SILC_PROTOCOL_SERVER_REKEY,
1426                          silc_server_protocol_rekey);
1427 }
1428
1429 /* Unregisters protocols */
1430
1431 void silc_server_protocols_unregister(void)
1432 {
1433   silc_protocol_unregister(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
1434                            silc_server_protocol_connection_auth);
1435   silc_protocol_unregister(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
1436                            silc_server_protocol_key_exchange);
1437   silc_protocol_unregister(SILC_PROTOCOL_SERVER_REKEY,
1438                            silc_server_protocol_rekey);
1439 }