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