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