updates.
[silc.git] / lib / silcclient / 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  * Client side of the protocols.
22  */
23 /* $Id$ */
24
25 #include "clientlibincludes.h"
26 #include "client_internal.h"
27
28 SILC_TASK_CALLBACK(silc_client_protocol_connection_auth);
29 SILC_TASK_CALLBACK(silc_client_protocol_key_exchange);
30 SILC_TASK_CALLBACK(silc_client_protocol_rekey);
31
32 /*
33  * Key Exhange protocol functions
34  */
35
36 /* Function that is called when SKE protocol sends packets to network. */
37
38 void silc_client_protocol_ke_send_packet(SilcSKE ske,
39                                          SilcBuffer packet,
40                                          SilcPacketType type,
41                                          void *context)
42 {
43   SilcProtocol protocol = (SilcProtocol)context;
44   SilcClientKEInternalContext *ctx = 
45     (SilcClientKEInternalContext *)protocol->context;
46   SilcClient client = (SilcClient)ctx->client;
47
48   /* Send the packet immediately */
49   silc_client_packet_send(client, ske->sock, type, NULL, 0, NULL, NULL,
50                           packet->data, packet->len, TRUE);
51 }
52
53 /* Public key verification callback. Called by the application. */
54
55 typedef struct {
56   SilcSKE ske;
57   SilcSKEVerifyCbCompletion completion;
58   void *completion_context;
59 } *VerifyKeyContext;
60
61 static void silc_client_verify_key_cb(bool success, void *context)
62 {
63   VerifyKeyContext verify = (VerifyKeyContext)context;
64
65   SILC_LOG_DEBUG(("Start"));
66
67   /* Call the completion callback back to the SKE */
68   verify->completion(verify->ske, success ? SILC_SKE_STATUS_OK : 
69                      SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY, 
70                      verify->completion_context);
71
72   silc_free(verify);
73 }
74
75 /* Callback that is called when we have received KE payload from
76    responder. We try to verify the public key now. */
77
78 void silc_client_protocol_ke_verify_key(SilcSKE ske,
79                                         unsigned char *pk_data,
80                                         uint32 pk_len,
81                                         SilcSKEPKType pk_type,
82                                         void *context,
83                                         SilcSKEVerifyCbCompletion completion,
84                                         void *completion_context)
85 {
86   SilcProtocol protocol = (SilcProtocol)context;
87   SilcClientKEInternalContext *ctx = 
88     (SilcClientKEInternalContext *)protocol->context;
89   SilcClient client = (SilcClient)ctx->client;
90   VerifyKeyContext verify;
91
92   SILC_LOG_DEBUG(("Start"));
93
94   verify = silc_calloc(1, sizeof(*verify));
95   verify->ske = ske;
96   verify->completion = completion;
97   verify->completion_context = completion_context;
98
99   /* Verify public key from user. */
100   client->ops->verify_public_key(client, ctx->sock->user_data, 
101                                  ctx->sock->type,
102                                  pk_data, pk_len, pk_type,
103                                  silc_client_verify_key_cb, verify);
104 }
105
106 /* Sets the negotiated key material into use for particular connection. */
107
108 void silc_client_protocol_ke_set_keys(SilcSKE ske,
109                                       SilcSocketConnection sock,
110                                       SilcSKEKeyMaterial *keymat,
111                                       SilcCipher cipher,
112                                       SilcPKCS pkcs,
113                                       SilcHash hash,
114                                       SilcHmac hmac,
115                                       SilcSKEDiffieHellmanGroup group)
116 {
117   SilcClientConnection conn = (SilcClientConnection)sock->user_data;
118
119   SILC_LOG_DEBUG(("Setting new keys into use"));
120
121   /* Allocate cipher to be used in the communication */
122   silc_cipher_alloc(cipher->cipher->name, &conn->send_key);
123   silc_cipher_alloc(cipher->cipher->name, &conn->receive_key);
124
125   conn->send_key->cipher->set_key(conn->send_key->context, 
126                                  keymat->send_enc_key, 
127                                  keymat->enc_key_len);
128   conn->send_key->set_iv(conn->send_key, keymat->send_iv);
129   conn->receive_key->cipher->set_key(conn->receive_key->context, 
130                                     keymat->receive_enc_key, 
131                                     keymat->enc_key_len);
132   conn->receive_key->set_iv(conn->receive_key, keymat->receive_iv);
133
134   /* Rekey stuff */
135   conn->rekey = silc_calloc(1, sizeof(*conn->rekey));
136   conn->rekey->send_enc_key = 
137     silc_calloc(keymat->enc_key_len / 8,
138                 sizeof(*conn->rekey->send_enc_key));
139   memcpy(conn->rekey->send_enc_key, 
140          keymat->send_enc_key, keymat->enc_key_len / 8);
141   conn->rekey->enc_key_len = keymat->enc_key_len / 8;
142
143   if (ske->start_payload->flags & SILC_SKE_SP_FLAG_PFS)
144     conn->rekey->pfs = TRUE;
145   conn->rekey->ske_group = silc_ske_group_get_number(group);
146
147   /* Save HMAC key to be used in the communication. */
148   silc_hmac_alloc((char *)silc_hmac_get_name(hmac), NULL, &conn->hmac_send);
149   silc_hmac_set_key(conn->hmac_send, keymat->hmac_key, keymat->hmac_key_len);
150   conn->hmac_receive = conn->hmac_send;
151
152   /* Save the HASH function */
153   silc_hash_alloc(hash->hash->name, &conn->hash);
154 }
155
156 /* Checks the version string of the server. */
157
158 SilcSKEStatus silc_ske_check_version(SilcSKE ske, unsigned char *version,
159                                      uint32 len, void *context)
160 {
161   SilcClientConnection conn = (SilcClientConnection)ske->sock->user_data;
162   SilcClient client = (SilcClient)ske->user_data;
163   SilcSKEStatus status = SILC_SKE_STATUS_OK;
164   char *cp;
165   int maj = 0, min = 0, build = 0, maj2 = 0, min2 = 0, build2 = 0;
166
167   /* Check for initial version string */
168   if (!strstr(version, "SILC-1.0-"))
169     status = SILC_SKE_STATUS_BAD_VERSION;
170
171   /* Check software version */
172
173   cp = version + 9;
174   if (!cp)
175     status = SILC_SKE_STATUS_BAD_VERSION;
176
177   maj = atoi(cp);
178   cp = strchr(cp, '.');
179   if (cp) {
180     min = atoi(cp + 1);
181     cp++;
182   }
183   cp = strchr(cp, '.');
184   if (cp)
185     build = atoi(cp + 1);
186
187   cp = client->silc_client_version + 9;
188   if (!cp)
189     status = SILC_SKE_STATUS_BAD_VERSION;
190
191   maj2 = atoi(cp);
192   cp = strchr(cp, '.');
193   if (cp) {
194     min2 = atoi(cp + 1);
195     cp++;
196   }
197   cp = strchr(cp, '.');
198   if (cp)
199     build2 = atoi(cp + 1);
200
201   if (maj != maj2)
202     status = SILC_SKE_STATUS_BAD_VERSION;
203   if (min < min2)
204     status = SILC_SKE_STATUS_BAD_VERSION;
205
206   if (status != SILC_SKE_STATUS_OK)
207     client->ops->say(client, conn, SILC_CLIENT_MESSAGE_AUDIT,
208                      "We don't support server version `%s'", version);
209
210   return status;
211 }
212
213 /* Callback that is called by the SKE to indicate that it is safe to
214    continue the execution of the protocol. Is given as argument to the 
215    silc_ske_initiator_finish or silc_ske_responder_phase_2 functions. 
216    This is called due to the fact that the public key verification
217    process is asynchronous and we must not continue the protocl until
218    the public key has been verified and this callback is called. */
219
220 static void silc_client_protocol_ke_continue(SilcSKE ske,
221                                              void *context)
222 {
223   SilcProtocol protocol = (SilcProtocol)context;
224   SilcClientKEInternalContext *ctx = 
225     (SilcClientKEInternalContext *)protocol->context;
226   SilcClient client = (SilcClient)ctx->client;
227   SilcClientConnection conn = ctx->sock->user_data;
228
229   SILC_LOG_DEBUG(("Start"));
230
231   if (ske->status != SILC_SKE_STATUS_OK) {
232     if (ske->status == SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY) {
233       client->ops->say(client, conn, SILC_CLIENT_MESSAGE_AUDIT, 
234                        "Received unsupported server %s public key",
235                        ctx->sock->hostname);
236     } else if (ske->status == SILC_SKE_STATUS_PUBLIC_KEY_NOT_PROVIDED) {
237       client->ops->say(client, conn, SILC_CLIENT_MESSAGE_AUDIT,
238                        "Remote host did not send its public key, even though "
239                        "it must send it");
240     } else {
241       client->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
242                        "Error during key exchange protocol with server %s",
243                        ctx->sock->hostname);
244     }
245     
246     protocol->state = SILC_PROTOCOL_STATE_ERROR;
247     silc_protocol_execute(protocol, client->schedule, 0, 0);
248     return;
249   }
250
251   /* Send Ok to the other end. We will end the protocol as server
252      sends Ok to us when we will take the new keys into use. Do this
253      if we are initiator. This is happens when this callback was sent
254      to silc_ske_initiator_finish function. */
255   if (ctx->responder == FALSE) {
256     silc_ske_end(ctx->ske);
257
258     /* End the protocol on the next round */
259     protocol->state = SILC_PROTOCOL_STATE_END;
260   }
261
262   /* Advance protocol state and call the next state if we are responder. 
263      This happens when this callback was sent to silc_ske_responder_phase_2
264      function. */
265   if (ctx->responder == TRUE) {
266     protocol->state++;
267     silc_protocol_execute(protocol, client->schedule, 0, 100000);
268   }
269 }
270
271 /* Performs key exchange protocol. This is used for both initiator
272    and responder key exchange. This may be called recursively. */
273
274 SILC_TASK_CALLBACK(silc_client_protocol_key_exchange)
275 {
276   SilcProtocol protocol = (SilcProtocol)context;
277   SilcClientKEInternalContext *ctx = 
278     (SilcClientKEInternalContext *)protocol->context;
279   SilcClient client = (SilcClient)ctx->client;
280   SilcClientConnection conn = ctx->sock->user_data;
281   SilcSKEStatus status = SILC_SKE_STATUS_OK;
282
283   SILC_LOG_DEBUG(("Start"));
284
285   if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
286     protocol->state = SILC_PROTOCOL_STATE_START;
287
288   switch(protocol->state) {
289   case SILC_PROTOCOL_STATE_START:
290     {
291       /*
292        * Start Protocol
293        */
294       SilcSKE ske;
295
296       /* Allocate Key Exchange object */
297       ske = silc_ske_alloc();
298       ctx->ske = ske;
299       ske->rng = client->rng;
300       ske->user_data = (void *)client;
301
302       silc_ske_set_callbacks(ske, ctx->send_packet, NULL,
303                              ctx->verify,
304                              silc_client_protocol_ke_continue,
305                              silc_ske_check_version, 
306                              context);
307       
308       if (ctx->responder == TRUE) {
309         /* Start the key exchange by processing the received security
310            properties packet from initiator. */
311         status = silc_ske_responder_start(ske, ctx->rng, ctx->sock,
312                                           client->silc_client_version,
313                                           ctx->packet->buffer, TRUE);
314       } else {
315         SilcSKEStartPayload *start_payload;
316
317         /* Assemble security properties. */
318         silc_ske_assemble_security_properties(ske, SILC_SKE_SP_FLAG_NONE, 
319                                               client->silc_client_version,
320                                               &start_payload);
321
322         /* Start the key exchange by sending our security properties
323            to the remote end. */
324         status = silc_ske_initiator_start(ske, ctx->rng, ctx->sock,
325                                           start_payload);
326       }
327
328       /* Return now if the procedure is pending */
329       if (status == SILC_SKE_STATUS_PENDING)
330         return;
331
332       if (status != SILC_SKE_STATUS_OK) {
333         SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
334                           status));
335         SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
336                         status));
337
338         protocol->state = SILC_PROTOCOL_STATE_ERROR;
339         silc_protocol_execute(protocol, client->schedule, 0, 0);
340         return;
341       }
342
343       /* Advance protocol state and call the next state if we are responder */
344       protocol->state++;
345       if (ctx->responder == TRUE)
346         silc_protocol_execute(protocol, client->schedule, 0, 100000);
347     }
348     break;
349   case 2:
350     {
351       /* 
352        * Phase 1 
353        */
354       if (ctx->responder == TRUE) {
355         /* Sends the selected security properties to the initiator. */
356         status = 
357           silc_ske_responder_phase_1(ctx->ske, 
358                                      ctx->ske->start_payload);
359       } else {
360         /* Call Phase-1 function. This processes the Key Exchange Start
361            paylaod reply we just got from the responder. The callback
362            function will receive the processed payload where we will
363            save it. */
364         status = silc_ske_initiator_phase_1(ctx->ske, ctx->packet->buffer);
365       }
366
367       if (status != SILC_SKE_STATUS_OK) {
368         SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
369                           status));
370         SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
371                         status));
372
373         protocol->state = SILC_PROTOCOL_STATE_ERROR;
374         silc_protocol_execute(protocol, client->schedule, 0, 0);
375         return;
376       }
377
378       /* Advance protocol state and call next state if we are initiator */
379       protocol->state++;
380       if (ctx->responder == FALSE)
381         silc_protocol_execute(protocol, client->schedule, 0, 100000);
382     }
383     break;
384   case 3:
385     {
386       /* 
387        * Phase 2 
388        */
389       if (ctx->responder == TRUE) {
390         /* Process the received Key Exchange 1 Payload packet from
391            the initiator. This also creates our parts of the Diffie
392            Hellman algorithm. The silc_client_protocol_ke_continue will
393            be called after the public key has been verified. */
394         status = silc_ske_responder_phase_2(ctx->ske, ctx->packet->buffer);
395       } else {
396         /* Call the Phase-2 function. This creates Diffie Hellman
397            key exchange parameters and sends our public part inside
398            Key Exhange 1 Payload to the responder. */
399         status = silc_ske_initiator_phase_2(ctx->ske,
400                                             client->public_key,
401                                             client->private_key);
402         protocol->state++;
403       }
404
405       /* Return now if the procedure is pending */
406       if (status == SILC_SKE_STATUS_PENDING)
407         return;
408
409       if (status != SILC_SKE_STATUS_OK) {
410         SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
411                           status));
412         SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
413                         status));
414
415         protocol->state = SILC_PROTOCOL_STATE_ERROR;
416         silc_protocol_execute(protocol, client->schedule, 0, 0);
417         return;
418       }
419     }
420     break;
421   case 4:
422     {
423       /* 
424        * Finish protocol
425        */
426       if (ctx->responder == TRUE) {
427         /* This creates the key exchange material and sends our
428            public parts to the initiator inside Key Exchange 2 Payload. */
429         status = 
430           silc_ske_responder_finish(ctx->ske, 
431                                     client->public_key, client->private_key,
432                                     SILC_SKE_PK_TYPE_SILC);
433
434         /* End the protocol on the next round */
435         protocol->state = SILC_PROTOCOL_STATE_END;
436       } else {
437         /* Finish the protocol. This verifies the Key Exchange 2 payload
438            sent by responder. The silc_client_protocol_ke_continue will
439            be called after the public key has been verified. */
440         status = silc_ske_initiator_finish(ctx->ske, ctx->packet->buffer);
441       }
442
443       /* Return now if the procedure is pending */
444       if (status == SILC_SKE_STATUS_PENDING)
445         return;
446
447       if (status != SILC_SKE_STATUS_OK) {
448         if (status == SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY) {
449           client->ops->say(client, conn, SILC_CLIENT_MESSAGE_AUDIT, 
450                            "Received unsupported server %s public key",
451                            ctx->sock->hostname);
452         } else {
453           client->ops->say(client, conn, SILC_CLIENT_MESSAGE_AUDIT,
454                            "Error during key exchange protocol with server %s",
455                            ctx->sock->hostname);
456         }
457         protocol->state = SILC_PROTOCOL_STATE_ERROR;
458         silc_protocol_execute(protocol, client->schedule, 0, 0);
459         return;
460       }
461     }
462     break;
463
464   case SILC_PROTOCOL_STATE_END:
465     {
466       /* 
467        * End protocol
468        */
469       SilcSKEKeyMaterial *keymat;
470       int key_len = silc_cipher_get_key_len(ctx->ske->prop->cipher);
471       int hash_len = ctx->ske->prop->hash->hash->hash_len;
472
473       /* Process the key material */
474       keymat = silc_calloc(1, sizeof(*keymat));
475       status = silc_ske_process_key_material(ctx->ske, 16, key_len, hash_len,
476                                              keymat);
477       if (status != SILC_SKE_STATUS_OK) {
478         protocol->state = SILC_PROTOCOL_STATE_ERROR;
479         silc_protocol_execute(protocol, client->schedule, 0, 300000);
480         silc_ske_free_key_material(keymat);
481         return;
482       }
483       ctx->keymat = keymat;
484
485       /* Send Ok to the other end if we are responder. If we are initiator
486          we have sent this already. */
487       if (ctx->responder == TRUE)
488         silc_ske_end(ctx->ske);
489
490       /* Unregister the timeout task since the protocol has ended. 
491          This was the timeout task to be executed if the protocol is
492          not completed fast enough. */
493       if (ctx->timeout_task)
494         silc_schedule_task_del(client->schedule, ctx->timeout_task);
495
496       /* Protocol has ended, call the final callback */
497       if (protocol->final_callback)
498         silc_protocol_execute_final(protocol, client->schedule);
499       else
500         silc_protocol_free(protocol);
501     }
502     break;
503
504   case SILC_PROTOCOL_STATE_ERROR:
505     /*
506      * Error during protocol
507      */
508     
509     /* Send abort notification */
510     silc_ske_abort(ctx->ske, ctx->ske->status);
511
512     /* On error the final callback is always called. */
513     if (protocol->final_callback)
514       silc_protocol_execute_final(protocol, client->schedule);
515     else
516       silc_protocol_free(protocol);
517     break;
518
519   case SILC_PROTOCOL_STATE_FAILURE:
520     /*
521      * Received failure from remote.
522      */
523
524     /* Unregister the timeout task since the protocol has ended. 
525        This was the timeout task to be executed if the protocol is
526        not completed fast enough. */
527     if (ctx->timeout_task)
528       silc_schedule_task_del(client->schedule, ctx->timeout_task);
529
530     /* On error the final callback is always called. */
531     if (protocol->final_callback)
532       silc_protocol_execute_final(protocol, client->schedule);
533     else
534       silc_protocol_free(protocol);
535     break;
536   case SILC_PROTOCOL_STATE_UNKNOWN:
537     break;
538   }
539 }
540
541 /*
542  * Connection Authentication protocol functions
543  */
544
545 static int
546 silc_client_get_public_key_auth(SilcClient client,
547                                 SilcClientConnection conn,
548                                 unsigned char *auth_data,
549                                 uint32 *auth_data_len,
550                                 SilcSKE ske)
551 {
552   int len;
553   SilcPKCS pkcs;
554   SilcBuffer auth;
555
556   /* Use our default key */
557   pkcs = client->pkcs;
558
559   /* Make the authentication data. Protocol says it is HASH plus
560      KE Start Payload. */
561   len = ske->hash_len + ske->start_payload_copy->len;
562   auth = silc_buffer_alloc(len);
563   silc_buffer_pull_tail(auth, len);
564   silc_buffer_format(auth,
565                      SILC_STR_UI_XNSTRING(ske->hash, ske->hash_len),
566                      SILC_STR_UI_XNSTRING(ske->start_payload_copy->data,
567                                           ske->start_payload_copy->len),
568                      SILC_STR_END);
569
570   if (silc_pkcs_sign_with_hash(pkcs, ske->prop->hash, auth->data, 
571                                auth->len, auth_data, auth_data_len)) {
572     silc_buffer_free(auth);
573     return TRUE;
574   }
575
576   silc_buffer_free(auth);
577   return FALSE;
578 }
579
580 /* Continues the connection authentication protocol. This funtion may
581    be called directly or used as SilcAskPassphrase callback. */
582
583 static void 
584 silc_client_conn_auth_continue(unsigned char *auth_data,
585                                uint32 auth_data_len, void *context)
586 {
587   SilcProtocol protocol = (SilcProtocol)context;
588   SilcClientConnAuthInternalContext *ctx = 
589     (SilcClientConnAuthInternalContext *)protocol->context;
590   SilcClient client = (SilcClient)ctx->client;
591   SilcBuffer packet;
592   int payload_len = 0;
593
594   SILC_LOG_DEBUG(("Start"));
595
596   payload_len = 4 + auth_data_len;
597   packet = silc_buffer_alloc(payload_len);
598   silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
599   silc_buffer_format(packet,
600                      SILC_STR_UI_SHORT(payload_len),
601                      SILC_STR_UI_SHORT(SILC_SOCKET_TYPE_CLIENT),
602                      SILC_STR_UI_XNSTRING(auth_data, auth_data_len),
603                      SILC_STR_END);
604
605   /* Send the packet to server */
606   silc_client_packet_send(client, ctx->sock,
607                           SILC_PACKET_CONNECTION_AUTH,
608                           NULL, 0, NULL, NULL,
609                           packet->data, packet->len, TRUE);
610   silc_buffer_free(packet);
611       
612   /* Next state is end of protocol */
613   protocol->state = SILC_PROTOCOL_STATE_END;
614 }
615                                                     
616 SILC_TASK_CALLBACK(silc_client_protocol_connection_auth)
617 {
618   SilcProtocol protocol = (SilcProtocol)context;
619   SilcClientConnAuthInternalContext *ctx = 
620     (SilcClientConnAuthInternalContext *)protocol->context;
621   SilcClient client = (SilcClient)ctx->client;
622   SilcClientConnection conn = ctx->sock->user_data;
623
624   SILC_LOG_DEBUG(("Start"));
625
626   if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
627     protocol->state = SILC_PROTOCOL_STATE_START;
628
629   switch(protocol->state) {
630   case SILC_PROTOCOL_STATE_START:
631     {
632       /* 
633        * Start protocol. We send authentication data to the server
634        * to be authenticated.
635        */
636       unsigned char *auth_data = NULL;
637       uint32 auth_data_len = 0;
638       unsigned char sign[1024];
639
640       switch(ctx->auth_meth) {
641       case SILC_AUTH_NONE:
642         /* No authentication required */
643         break;
644
645       case SILC_AUTH_PASSWORD:
646         /* Password authentication */
647         if (ctx->auth_data && ctx->auth_data_len) {
648           auth_data = ctx->auth_data;
649           auth_data_len = ctx->auth_data_len;
650           break;
651         }
652
653         client->ops->say(client, conn, SILC_CLIENT_MESSAGE_INFO,
654                          "Password authentication required by server %s",
655                          ctx->sock->hostname);
656         client->ops->ask_passphrase(client, conn,
657                                     silc_client_conn_auth_continue,
658                                     protocol);
659         return;
660         break;
661
662       case SILC_AUTH_PUBLIC_KEY:
663         if (!ctx->auth_data) {
664           /* Public key authentication */
665           silc_client_get_public_key_auth(client, conn, sign, &auth_data_len, 
666                                           ctx->ske);
667           auth_data = sign;
668         } else {
669           auth_data = ctx->auth_data;
670           auth_data_len = ctx->auth_data_len;
671         }
672         
673         break;
674       }
675
676       silc_client_conn_auth_continue(auth_data,
677                                      auth_data_len, protocol);
678     }
679     break;
680
681   case SILC_PROTOCOL_STATE_END:
682     {
683       /* 
684        * End protocol. Nothing special to be done here.
685        */
686
687       /* Protocol has ended, call the final callback */
688       if (protocol->final_callback)
689         silc_protocol_execute_final(protocol, client->schedule);
690       else
691         silc_protocol_free(protocol);
692     }
693     break;
694
695   case SILC_PROTOCOL_STATE_ERROR:
696     {
697       /* 
698        * Error. Send notify to remote.
699        */
700       unsigned char error[4];
701
702       SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
703
704       /* Error in protocol. Send FAILURE packet. Although I don't think
705          this could ever happen on client side. */
706       silc_client_packet_send(client, ctx->sock, SILC_PACKET_FAILURE,
707                               NULL, 0, NULL, NULL, error, 4, TRUE);
708
709       /* On error the final callback is always called. */
710       if (protocol->final_callback)
711         silc_protocol_execute_final(protocol, client->schedule);
712       else
713         silc_protocol_free(protocol);
714     }
715
716   case SILC_PROTOCOL_STATE_FAILURE:
717     /*
718      * Received failure from remote.
719      */
720
721     /* On error the final callback is always called. */
722     if (protocol->final_callback)
723       silc_protocol_execute_final(protocol, client->schedule);
724     else
725       silc_protocol_free(protocol);
726     break;
727
728   case SILC_PROTOCOL_STATE_UNKNOWN:
729     break;
730   }
731 }
732
733 /*
734  * Re-key protocol routines
735  */
736
737 /* Actually takes the new keys into use. */
738
739 static void 
740 silc_client_protocol_rekey_validate(SilcClient client,
741                                     SilcClientRekeyInternalContext *ctx,
742                                     SilcSocketConnection sock,
743                                     SilcSKEKeyMaterial *keymat,
744                                     bool send)
745 {
746   SilcClientConnection conn = (SilcClientConnection)sock->user_data;
747
748   if (ctx->responder == TRUE) {
749     if (send) {
750       silc_cipher_set_key(conn->send_key, keymat->receive_enc_key, 
751                           keymat->enc_key_len);
752       silc_cipher_set_iv(conn->send_key, keymat->receive_iv);
753     } else {
754       silc_cipher_set_key(conn->receive_key, keymat->send_enc_key, 
755                           keymat->enc_key_len);
756       silc_cipher_set_iv(conn->receive_key, keymat->send_iv);
757     }
758   } else {
759     if (send) {
760       silc_cipher_set_key(conn->send_key, keymat->send_enc_key, 
761                           keymat->enc_key_len);
762       silc_cipher_set_iv(conn->send_key, keymat->send_iv);
763     } else {
764       silc_cipher_set_key(conn->receive_key, keymat->receive_enc_key, 
765                           keymat->enc_key_len);
766       silc_cipher_set_iv(conn->receive_key, keymat->receive_iv);
767     }
768   }
769
770   if (send) {
771     silc_hmac_alloc((char *)silc_hmac_get_name(conn->hmac_receive), NULL, 
772                     &conn->hmac_send);
773     silc_hmac_set_key(conn->hmac_send, keymat->hmac_key, 
774                       keymat->hmac_key_len);
775   } else {
776     silc_hmac_free(conn->hmac_receive);
777     conn->hmac_receive = conn->hmac_send;
778   }
779
780   /* Save the current sending encryption key */
781   if (!send) {
782     memset(conn->rekey->send_enc_key, 0, conn->rekey->enc_key_len);
783     silc_free(conn->rekey->send_enc_key);
784     conn->rekey->send_enc_key = 
785       silc_calloc(keymat->enc_key_len / 8,
786                   sizeof(*conn->rekey->send_enc_key));
787     memcpy(conn->rekey->send_enc_key, keymat->send_enc_key, 
788            keymat->enc_key_len / 8);
789     conn->rekey->enc_key_len = keymat->enc_key_len / 8;
790   }
791 }
792
793 /* This function actually re-generates (when not using PFS) the keys and
794    takes them into use. */
795
796 static void 
797 silc_client_protocol_rekey_generate(SilcClient client,
798                                     SilcClientRekeyInternalContext *ctx,
799                                     bool send)
800 {
801   SilcClientConnection conn = (SilcClientConnection)ctx->sock->user_data;
802   SilcSKEKeyMaterial *keymat;
803   uint32 key_len = silc_cipher_get_key_len(conn->send_key);
804   uint32 hash_len = conn->hash->hash->hash_len;
805
806   SILC_LOG_DEBUG(("Generating new %s session keys (no PFS)",
807                   send ? "sending" : "receiving"));
808
809   /* Generate the new key */
810   keymat = silc_calloc(1, sizeof(*keymat));
811   silc_ske_process_key_material_data(conn->rekey->send_enc_key,
812                                      conn->rekey->enc_key_len,
813                                      16, key_len, hash_len, 
814                                      conn->hash, keymat);
815
816   /* Set the keys into use */
817   silc_client_protocol_rekey_validate(client, ctx, ctx->sock, keymat, send);
818
819   silc_ske_free_key_material(keymat);
820 }
821
822 /* This function actually re-generates (with PFS) the keys and
823    takes them into use. */
824
825 static void 
826 silc_client_protocol_rekey_generate_pfs(SilcClient client,
827                                         SilcClientRekeyInternalContext *ctx,
828                                         bool send)
829 {
830   SilcClientConnection conn = (SilcClientConnection)ctx->sock->user_data;
831   SilcSKEKeyMaterial *keymat;
832   uint32 key_len = silc_cipher_get_key_len(conn->send_key);
833   uint32 hash_len = conn->hash->hash->hash_len;
834   unsigned char *tmpbuf;
835   uint32 klen;
836
837   SILC_LOG_DEBUG(("Generating new %s session keys (with PFS)",
838                   send ? "sending" : "receiving"));
839
840   /* Encode KEY to binary data */
841   tmpbuf = silc_mp_mp2bin(ctx->ske->KEY, 0, &klen);
842
843   /* Generate the new key */
844   keymat = silc_calloc(1, sizeof(*keymat));
845   silc_ske_process_key_material_data(tmpbuf, klen, 16, key_len, hash_len, 
846                                      conn->hash, keymat);
847
848   /* Set the keys into use */
849   silc_client_protocol_rekey_validate(client, ctx, ctx->sock, keymat, send);
850
851   memset(tmpbuf, 0, klen);
852   silc_free(tmpbuf);
853   silc_ske_free_key_material(keymat);
854 }
855
856 /* Packet sending callback. This function is provided as packet sending
857    routine to the Key Exchange functions. */
858
859 static void 
860 silc_client_protocol_rekey_send_packet(SilcSKE ske,
861                                        SilcBuffer packet,
862                                        SilcPacketType type,
863                                        void *context)
864 {
865   SilcProtocol protocol = (SilcProtocol)context;
866   SilcClientRekeyInternalContext *ctx = 
867     (SilcClientRekeyInternalContext *)protocol->context;
868   SilcClient client = (SilcClient)ctx->client;
869
870   /* Send the packet immediately */
871   silc_client_packet_send(client, ctx->sock, type, NULL, 0, NULL, NULL,
872                           packet->data, packet->len, FALSE);
873 }
874
875 /* Performs re-key as defined in the SILC protocol specification. */
876
877 SILC_TASK_CALLBACK(silc_client_protocol_rekey)
878 {
879   SilcProtocol protocol = (SilcProtocol)context;
880   SilcClientRekeyInternalContext *ctx = 
881     (SilcClientRekeyInternalContext *)protocol->context;
882   SilcClient client = (SilcClient)ctx->client;
883   SilcClientConnection conn = (SilcClientConnection)ctx->sock->user_data;
884   SilcSKEStatus status;
885
886   SILC_LOG_DEBUG(("Start"));
887
888   if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
889     protocol->state = SILC_PROTOCOL_STATE_START;
890
891   SILC_LOG_DEBUG(("State=%d", protocol->state));
892
893   switch(protocol->state) {
894   case SILC_PROTOCOL_STATE_START:
895     {
896       /* 
897        * Start protocol.
898        */
899
900       if (ctx->responder == TRUE) {
901         /*
902          * We are receiving party
903          */
904
905         if (ctx->pfs == TRUE) {
906           /* 
907            * Use Perfect Forward Secrecy, ie. negotiate the key material
908            * using the SKE protocol.
909            */
910
911           if (ctx->packet->type != SILC_PACKET_KEY_EXCHANGE_1) {
912             /* Error in protocol */
913             protocol->state = SILC_PROTOCOL_STATE_ERROR;
914             silc_protocol_execute(protocol, client->schedule, 0, 300000);
915           }
916
917           ctx->ske = silc_ske_alloc();
918           ctx->ske->rng = client->rng;
919           ctx->ske->prop = silc_calloc(1, sizeof(*ctx->ske->prop));
920           silc_ske_get_group_by_number(conn->rekey->ske_group,
921                                        &ctx->ske->prop->group);
922
923           silc_ske_set_callbacks(ctx->ske, 
924                                  silc_client_protocol_rekey_send_packet,
925                                  NULL,  NULL, NULL, silc_ske_check_version,
926                                  context);
927       
928           status = silc_ske_responder_phase_2(ctx->ske, ctx->packet->buffer);
929           if (status != SILC_SKE_STATUS_OK) {
930             SILC_LOG_WARNING(("Error (type %d) during Re-key (PFS)",
931                               status));
932             
933             protocol->state = SILC_PROTOCOL_STATE_ERROR;
934             silc_protocol_execute(protocol, client->schedule, 0, 300000);
935             return;
936           }
937
938           /* Advance the protocol state */
939           protocol->state++;
940           silc_protocol_execute(protocol, client->schedule, 0, 0);
941         } else {
942           /*
943            * Do normal and simple re-key.
944            */
945
946           /* Send the REKEY_DONE to indicate we will take new keys into use */
947           silc_client_packet_send(client, ctx->sock, 
948                                   SILC_PACKET_REKEY_DONE, 
949                                   NULL, 0, NULL, NULL, NULL, 0, FALSE);
950
951           /* After we send REKEY_DONE we must set the sending encryption
952              key to the new key since all packets after this packet must
953              encrypted with the new key. */
954           silc_client_protocol_rekey_generate(client, ctx, TRUE);
955
956           /* The protocol ends in next stage. */
957           protocol->state = SILC_PROTOCOL_STATE_END;
958         }
959       
960       } else {
961         /*
962          * We are the initiator of this protocol
963          */
964
965         /* Start the re-key by sending the REKEY packet */
966         silc_client_packet_send(client, ctx->sock, SILC_PACKET_REKEY, 
967                                 NULL, 0, NULL, NULL, NULL, 0, FALSE);
968
969         if (ctx->pfs == TRUE) {
970           /* 
971            * Use Perfect Forward Secrecy, ie. negotiate the key material
972            * using the SKE protocol.
973            */
974           ctx->ske = silc_ske_alloc();
975           ctx->ske->rng = client->rng;
976           ctx->ske->prop = silc_calloc(1, sizeof(*ctx->ske->prop));
977           silc_ske_get_group_by_number(conn->rekey->ske_group,
978                                        &ctx->ske->prop->group);
979
980           silc_ske_set_callbacks(ctx->ske, 
981                                  silc_client_protocol_rekey_send_packet,
982                                  NULL,  NULL, NULL, silc_ske_check_version,
983                                  context);
984       
985           status =  silc_ske_initiator_phase_2(ctx->ske, NULL, NULL);
986           if (status != SILC_SKE_STATUS_OK) {
987             SILC_LOG_WARNING(("Error (type %d) during Re-key (PFS)",
988                               status));
989             
990             protocol->state = SILC_PROTOCOL_STATE_ERROR;
991             silc_protocol_execute(protocol, client->schedule, 0, 300000);
992             return;
993           }
994
995           /* Advance the protocol state */
996           protocol->state++;
997         } else {
998           /*
999            * Do normal and simple re-key.
1000            */
1001
1002           /* Send the REKEY_DONE to indicate we will take new keys into use 
1003              now. */ 
1004           silc_client_packet_send(client, ctx->sock, 
1005                                   SILC_PACKET_REKEY_DONE, 
1006                                   NULL, 0, NULL, NULL, NULL, 0, FALSE);
1007
1008           /* After we send REKEY_DONE we must set the sending encryption
1009              key to the new key since all packets after this packet must
1010              encrypted with the new key. */
1011           silc_client_protocol_rekey_generate(client, ctx, TRUE);
1012
1013           /* The protocol ends in next stage. */
1014           protocol->state = SILC_PROTOCOL_STATE_END;
1015         }
1016       }
1017     }
1018     break;
1019
1020   case 2:
1021     /*
1022      * Second state, used only when oding re-key with PFS.
1023      */
1024     if (ctx->responder == TRUE) {
1025       if (ctx->pfs == TRUE) {
1026         /*
1027          * Send our KE packe to the initiator now that we've processed
1028          * the initiator's KE packet.
1029          */
1030         status = silc_ske_responder_finish(ctx->ske, NULL, NULL, 
1031                                            SILC_SKE_PK_TYPE_SILC);
1032
1033           if (status != SILC_SKE_STATUS_OK) {
1034             SILC_LOG_WARNING(("Error (type %d) during Re-key (PFS)",
1035                               status));
1036             
1037             protocol->state = SILC_PROTOCOL_STATE_ERROR;
1038             silc_protocol_execute(protocol, client->schedule, 0, 300000);
1039             return;
1040           }
1041       }
1042
1043     } else {
1044       if (ctx->pfs == TRUE) {
1045         /*
1046          * The packet type must be KE packet
1047          */
1048         if (ctx->packet->type != SILC_PACKET_KEY_EXCHANGE_2) {
1049           /* Error in protocol */
1050           protocol->state = SILC_PROTOCOL_STATE_ERROR;
1051           silc_protocol_execute(protocol, client->schedule, 0, 300000);
1052         }
1053         
1054         status = silc_ske_initiator_finish(ctx->ske, ctx->packet->buffer);
1055         if (status != SILC_SKE_STATUS_OK) {
1056           SILC_LOG_WARNING(("Error (type %d) during Re-key (PFS)",
1057                             status));
1058           
1059           protocol->state = SILC_PROTOCOL_STATE_ERROR;
1060           silc_protocol_execute(protocol, client->schedule, 0, 300000);
1061           return;
1062         }
1063       }
1064     }
1065
1066     /* Send the REKEY_DONE to indicate we will take new keys into use 
1067        now. */ 
1068     silc_client_packet_send(client, ctx->sock, SILC_PACKET_REKEY_DONE, 
1069                             NULL, 0, NULL, NULL, NULL, 0, FALSE);
1070     
1071     /* After we send REKEY_DONE we must set the sending encryption
1072        key to the new key since all packets after this packet must
1073        encrypted with the new key. */
1074     silc_client_protocol_rekey_generate_pfs(client, ctx, TRUE);
1075
1076     /* The protocol ends in next stage. */
1077     protocol->state = SILC_PROTOCOL_STATE_END;
1078     break;
1079
1080   case SILC_PROTOCOL_STATE_END:
1081     /* 
1082      * End protocol
1083      */
1084
1085     if (ctx->packet->type != SILC_PACKET_REKEY_DONE) {
1086       /* Error in protocol */
1087       protocol->state = SILC_PROTOCOL_STATE_ERROR;
1088       silc_protocol_execute(protocol, client->schedule, 0, 0);
1089     }
1090
1091     /* We received the REKEY_DONE packet and all packets after this is
1092        encrypted with the new key so set the decryption key to the new key */
1093     silc_client_protocol_rekey_generate(client, ctx, FALSE);
1094
1095     /* Protocol has ended, call the final callback */
1096     if (protocol->final_callback)
1097       silc_protocol_execute_final(protocol, client->schedule);
1098     else
1099       silc_protocol_free(protocol);
1100     break;
1101
1102   case SILC_PROTOCOL_STATE_ERROR:
1103     /*
1104      * Error occured
1105      */
1106
1107     if (ctx->pfs == TRUE) {
1108       /* Send abort notification */
1109       silc_ske_abort(ctx->ske, ctx->ske->status);
1110     }
1111
1112     /* On error the final callback is always called. */
1113     if (protocol->final_callback)
1114       silc_protocol_execute_final(protocol, client->schedule);
1115     else
1116       silc_protocol_free(protocol);
1117     break;
1118
1119   case SILC_PROTOCOL_STATE_FAILURE:
1120     /*
1121      * We have received failure from remote
1122      */
1123
1124     /* On error the final callback is always called. */
1125     if (protocol->final_callback)
1126       silc_protocol_execute_final(protocol, client->schedule);
1127     else
1128       silc_protocol_free(protocol);
1129     break;
1130
1131   case SILC_PROTOCOL_STATE_UNKNOWN:
1132     break;
1133   }
1134
1135 }
1136
1137 /* Registers protocols used in client */
1138
1139 void silc_client_protocols_register(void)
1140 {
1141   silc_protocol_register(SILC_PROTOCOL_CLIENT_CONNECTION_AUTH,
1142                          silc_client_protocol_connection_auth);
1143   silc_protocol_register(SILC_PROTOCOL_CLIENT_KEY_EXCHANGE,
1144                          silc_client_protocol_key_exchange);
1145   silc_protocol_register(SILC_PROTOCOL_CLIENT_REKEY,
1146                          silc_client_protocol_rekey);
1147 }
1148
1149 /* Unregisters protocols */
1150
1151 void silc_client_protocols_unregister(void)
1152 {
1153   silc_protocol_unregister(SILC_PROTOCOL_CLIENT_CONNECTION_AUTH,
1154                            silc_client_protocol_connection_auth);
1155   silc_protocol_unregister(SILC_PROTOCOL_CLIENT_KEY_EXCHANGE,
1156                            silc_client_protocol_key_exchange);
1157   silc_protocol_unregister(SILC_PROTOCOL_CLIENT_REKEY,
1158                            silc_client_protocol_rekey);
1159 }