5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2005 Pekka Riikonen
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.
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.
21 #include "silcconnauth.h"
23 /************************** Types and definitions ***************************/
25 static SilcBool silc_connauth_packet_receive(SilcPacketEngine engine,
26 SilcPacketStream stream,
28 void *callback_context,
31 /* Connection authentication context */
32 struct SilcConnAuthStruct {
35 SilcConnectionType conn_type;
36 SilcAuthMethod auth_method;
38 SilcUInt32 auth_data_len;
39 SilcConnAuthCompletion completion;
40 SilcConnAuthGetAuthData get_auth_data;
42 SilcDList public_keys;
43 SilcSKRStatus skr_status;
44 SilcUInt32 timeout_secs;
46 unsigned int aborted : 1;
47 unsigned int success : 1;
50 /* Packet stream callbacks */
51 static SilcPacketCallbacks silc_connauth_stream_cbs =
53 silc_connauth_packet_receive, NULL, NULL
57 /************************ Static utility functions **************************/
61 static SilcBool silc_connauth_packet_receive(SilcPacketEngine engine,
62 SilcPacketStream stream,
64 void *callback_context,
67 SilcConnAuth connauth = callback_context;
68 connauth->packet = packet;
69 silc_fsm_continue(&connauth->fsm);
73 /* Async operation abortion callback */
75 static void silc_connauth_abort(SilcAsyncOperation op, void *context)
77 SilcConnAuth connauth = context;
78 connauth->aborted = TRUE;
81 /* Generates signature for public key based authentication */
83 static SilcBool silc_connauth_get_signature(SilcConnAuth connauth,
84 unsigned char **auth_data,
85 SilcUInt32 *auth_data_len)
89 SilcPrivateKey private_key;
92 SILC_LOG_DEBUG(("Compute signature"));
95 private_key = connauth->auth_data;
97 /* Make the authentication data. Protocol says it is HASH plus
99 len = ske->hash_len + silc_buffer_len(ske->start_payload_copy);
100 auth = silc_buffer_alloc_size(len);
103 silc_buffer_format(auth,
104 SILC_STR_UI_XNSTRING(ske->hash, ske->hash_len),
105 SILC_STR_UI_XNSTRING(
106 ske->start_payload_copy->data,
107 silc_buffer_len(ske->start_payload_copy)),
110 len = ((silc_pkcs_private_key_get_len(private_key) + 7) / 8) + 1;
111 *auth_data = silc_calloc(len, sizeof(**auth_data));
112 if (*auth_data == NULL) {
113 silc_buffer_free(auth);
117 /* Compute signature */
118 if (!silc_pkcs_sign(private_key, auth->data, silc_buffer_len(auth),
119 *auth_data, len, auth_data_len, ske->prop->hash)) {
120 silc_free(*auth_data);
121 silc_buffer_free(auth);
125 silc_buffer_free(auth);
129 /* Verifies digital signature */
131 static SilcBool silc_connauth_verify_signature(SilcConnAuth connauth,
132 SilcPublicKey pub_key,
138 SilcSKE ske = connauth->ske;
140 if (!pub_key || !sign)
143 /* Make the authentication data. Protocol says it is HASH plus
145 len = ske->hash_len + silc_buffer_len(ske->start_payload_copy);
146 auth = silc_buffer_alloc_size(len);
149 silc_buffer_format(auth,
150 SILC_STR_UI_XNSTRING(ske->hash, ske->hash_len),
151 SILC_STR_UI_XNSTRING(
152 ske->start_payload_copy->data,
153 silc_buffer_len(ske->start_payload_copy)),
156 /* Verify signature */
157 if (!silc_pkcs_verify(pub_key, sign, sign_len, auth->data,
158 silc_buffer_len(auth), ske->prop->hash)) {
159 silc_buffer_free(auth);
163 silc_buffer_free(auth);
170 SILC_TASK_CALLBACK(silc_connauth_timeout)
172 SilcConnAuth connauth = context;
173 SILC_LOG_DEBUG(("Protocol timeout"));
174 connauth->aborted = TRUE;
175 silc_fsm_continue_sync(&connauth->fsm);
180 static void silc_connauth_skr_callback(SilcSKR skr, SilcSKRFind find,
181 SilcSKRStatus status,
182 SilcDList results, void *context)
184 SilcConnAuth connauth = context;
186 silc_skr_find_free(find);
188 connauth->public_keys = results;
189 connauth->skr_status = status;
191 SILC_FSM_CALL_CONTINUE(&connauth->fsm);
195 /******************************* Protocol API *******************************/
197 /* Allocate connection authentication context */
199 SilcConnAuth silc_connauth_alloc(SilcSchedule schedule,
201 SilcUInt32 timeout_secs)
203 SilcConnAuth connauth;
205 if (!schedule || !ske)
208 connauth = silc_calloc(1, sizeof(*connauth));
212 if (!silc_fsm_init(&connauth->fsm, connauth, NULL, NULL, schedule)) {
213 silc_connauth_free(connauth);
217 connauth->timeout_secs = timeout_secs;
223 /* Free connection authentication context */
225 void silc_connauth_free(SilcConnAuth connauth)
227 if (connauth->public_keys)
228 silc_dlist_uninit(connauth->public_keys);
232 /* Return associated SKE context */
234 SilcSKE silc_connauth_get_ske(SilcConnAuth connauth)
236 return connauth->ske;
240 /******************************** Initiator *********************************/
242 SILC_FSM_STATE(silc_connauth_st_initiator_start);
243 SILC_FSM_STATE(silc_connauth_st_initiator_result);
244 SILC_FSM_STATE(silc_connauth_st_initiator_failure);
246 SILC_FSM_STATE(silc_connauth_st_initiator_start)
248 SilcConnAuth connauth = fsm_context;
251 unsigned char *auth_data = NULL;
252 SilcUInt32 auth_data_len = 0;
254 SILC_LOG_DEBUG(("Start"));
256 if (connauth->aborted) {
258 silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
259 return SILC_FSM_CONTINUE;
263 if (connauth->timeout_secs)
264 silc_schedule_task_add_timeout(silc_fsm_get_schedule(fsm),
265 silc_connauth_timeout, connauth,
266 connauth->timeout_secs, 0);
268 switch (connauth->auth_method) {
270 /* No authentication required */
273 case SILC_AUTH_PASSWORD:
274 auth_data = silc_memdup(connauth->auth_data, connauth->auth_data_len);
277 silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
278 return SILC_FSM_CONTINUE;
280 auth_data_len = connauth->auth_data_len;
283 case SILC_AUTH_PUBLIC_KEY:
284 if (!silc_connauth_get_signature(connauth, &auth_data, &auth_data_len)) {
285 /** Error computing signature */
286 silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
287 return SILC_FSM_CONTINUE;
292 payload_len = 4 + auth_data_len;
293 packet = silc_buffer_alloc_size(payload_len);
296 silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
297 return SILC_FSM_CONTINUE;
300 silc_buffer_format(packet,
301 SILC_STR_UI_SHORT(payload_len),
302 SILC_STR_UI_SHORT(connauth->conn_type),
303 SILC_STR_UI_XNSTRING(auth_data, auth_data_len),
306 /* Send the packet */
307 if (!silc_packet_send(connauth->ske->stream, SILC_PACKET_CONNECTION_AUTH, 0,
308 packet->data, silc_buffer_len(packet))) {
309 /** Error sending packet */
310 silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
311 return SILC_FSM_CONTINUE;
315 memset(auth_data, 0, auth_data_len);
316 silc_free(auth_data);
318 silc_buffer_free(packet);
320 /** Wait for responder */
321 silc_fsm_next(fsm, silc_connauth_st_initiator_result);
322 return SILC_FSM_WAIT;
325 SILC_FSM_STATE(silc_connauth_st_initiator_result)
327 SilcConnAuth connauth = fsm_context;
329 SILC_LOG_DEBUG(("Start"));
331 if (connauth->aborted) {
333 silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
334 return SILC_FSM_CONTINUE;
337 /* Check the status of authentication */
338 if (connauth->packet->type == SILC_PACKET_SUCCESS) {
339 SILC_LOG_DEBUG(("Authentication successful"));
340 connauth->success = TRUE;
342 SILC_LOG_DEBUG(("Authentication failed"));
343 connauth->success = FALSE;
345 silc_packet_free(connauth->packet);
347 /* Call completion callback */
348 connauth->completion(connauth, connauth->success, connauth->context);
350 silc_packet_stream_unlink(connauth->ske->stream,
351 &silc_connauth_stream_cbs, connauth);
352 silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
354 return SILC_FSM_FINISH;
357 SILC_FSM_STATE(silc_connauth_st_initiator_failure)
359 SilcConnAuth connauth = fsm_context;
360 unsigned char error[4];
362 SILC_LOG_DEBUG(("Start"));
364 /* Send FAILURE packet */
365 SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
366 silc_packet_send(connauth->ske->stream, SILC_PACKET_FAILURE, 0, error, 4);
368 /* Call completion callback */
369 connauth->completion(connauth, FALSE, connauth->context);
371 silc_packet_stream_unlink(connauth->ske->stream,
372 &silc_connauth_stream_cbs, connauth);
373 silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
375 return SILC_FSM_FINISH;
379 silc_connauth_initiator(SilcConnAuth connauth,
380 SilcConnectionType conn_type,
381 SilcAuthMethod auth_method, void *auth_data,
382 SilcUInt32 auth_data_len,
383 SilcConnAuthCompletion completion,
386 SilcAsyncOperation op;
388 SILC_LOG_DEBUG(("Connection authentication as initiator"));
390 if (auth_method == SILC_AUTH_PASSWORD && !auth_data) {
391 completion(connauth, FALSE, context);
395 if (auth_method == SILC_AUTH_PUBLIC_KEY && !auth_data) {
396 completion(connauth, FALSE, context);
400 connauth->conn_type = conn_type;
401 connauth->auth_method = auth_method;
402 connauth->auth_data = auth_data;
403 connauth->auth_data_len = auth_data_len;
404 connauth->completion = completion;
405 connauth->context = context;
407 /* Link to packet stream to get packets */
408 silc_packet_stream_link(connauth->ske->stream,
409 &silc_connauth_stream_cbs, connauth, 1000000,
411 SILC_PACKET_FAILURE, -1);
413 /* Start the protocol */
414 op = silc_async_alloc(silc_connauth_abort, NULL, connauth);
415 silc_fsm_start(&connauth->fsm, silc_connauth_st_initiator_start);
421 /******************************** Responder *********************************/
423 SILC_FSM_STATE(silc_connauth_st_responder_start);
424 SILC_FSM_STATE(silc_connauth_st_responder_authenticate);
425 SILC_FSM_STATE(silc_connauth_st_responder_authenticate_pk);
426 SILC_FSM_STATE(silc_connauth_st_responder_success);
427 SILC_FSM_STATE(silc_connauth_st_responder_failure);
429 SILC_FSM_STATE(silc_connauth_st_responder_start)
431 SilcConnAuth connauth = fsm_context;
433 SILC_LOG_DEBUG(("Start"));
435 if (connauth->aborted) {
437 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
438 return SILC_FSM_CONTINUE;
442 if (connauth->timeout_secs)
443 silc_schedule_task_add_timeout(silc_fsm_get_schedule(fsm),
444 silc_connauth_timeout, connauth,
445 connauth->timeout_secs, 0);
447 /** Wait for initiator */
448 silc_fsm_next(fsm, silc_connauth_st_responder_authenticate);
449 return SILC_FSM_WAIT;
452 SILC_FSM_STATE(silc_connauth_st_responder_authenticate)
454 SilcConnAuth connauth = fsm_context;
455 SilcUInt16 payload_len;
456 SilcUInt16 conn_type;
457 unsigned char *auth_data = NULL, *passphrase = NULL;
458 SilcUInt32 passphrase_len;
459 SilcSKR repository = NULL;
462 SILC_LOG_DEBUG(("Start"));
464 if (connauth->aborted) {
466 silc_packet_free(connauth->packet);
467 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
468 return SILC_FSM_CONTINUE;
471 if (connauth->packet->type != SILC_PACKET_CONNECTION_AUTH) {
472 /** Protocol failure */
473 silc_packet_free(connauth->packet);
474 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
475 return SILC_FSM_CONTINUE;
478 /* Parse the received authentication data packet. The received
479 payload is Connection Auth Payload. */
480 ret = silc_buffer_unformat(&connauth->packet->buffer,
481 SILC_STR_UI_SHORT(&payload_len),
482 SILC_STR_UI_SHORT(&conn_type),
486 SILC_LOG_ERROR(("Bad payload in authentication packet"));
487 silc_packet_free(connauth->packet);
488 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
489 return SILC_FSM_CONTINUE;
492 if (payload_len != silc_buffer_len(&connauth->packet->buffer)) {
493 /** Bad payload length */
494 SILC_LOG_ERROR(("Bad payload length in authentication packet"));
495 silc_packet_free(connauth->packet);
496 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
497 return SILC_FSM_CONTINUE;
502 if (conn_type < SILC_CONN_CLIENT || conn_type > SILC_CONN_ROUTER) {
503 /** Bad connection type */
504 SILC_LOG_ERROR(("Bad connection type (%d) in authentication packet",
506 silc_packet_free(connauth->packet);
507 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
508 return SILC_FSM_CONTINUE;
511 if (payload_len > 0) {
512 /* Get authentication data */
513 ret = silc_buffer_unformat(&connauth->packet->buffer,
515 SILC_STR_UI_XNSTRING(&auth_data,
520 SILC_LOG_DEBUG(("Bad payload in authentication payload"));
521 silc_packet_free(connauth->packet);
522 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
523 return SILC_FSM_CONTINUE;
526 silc_packet_free(connauth->packet);
528 SILC_LOG_DEBUG(("Remote connection type %d", conn_type));
530 /* Get authentication data */
531 if (!connauth->get_auth_data(connauth, conn_type, &passphrase,
532 &passphrase_len, &repository,
533 connauth->context)) {
534 /** Connection not configured */
535 SILC_LOG_ERROR(("Remote connection not configured"));
536 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
537 return SILC_FSM_CONTINUE;
542 /* Passphrase authentication */
543 if (passphrase && passphrase_len) {
544 SILC_LOG_DEBUG(("Passphrase authentication"));
545 if (!memcmp(auth_data, passphrase, passphrase_len)) {
546 /** Authentication failed */
547 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
548 return SILC_FSM_CONTINUE;
550 } else if (repository) {
551 /* Digital signature */
554 SILC_LOG_DEBUG(("Digital signature authentication"));
556 connauth->auth_data = silc_memdup(auth_data, payload_len);
557 connauth->auth_data_len = payload_len;
559 /* Allocate search constraints for finding the key */
560 find = silc_skr_find_alloc();
562 if (!find || !connauth->auth_data) {
564 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
565 return SILC_FSM_CONTINUE;
568 silc_skr_find_set_pkcs_type(find, connauth->ske->pk_type);
569 silc_skr_find_set_public_key(find, connauth->ske->public_key);
570 silc_skr_find_set_usage(find, (SILC_SKR_USAGE_AUTH |
571 SILC_SKR_USAGE_KEY_AGREEMENT));
573 /** Find public key */
574 silc_fsm_next(fsm, silc_connauth_st_responder_authenticate_pk);
575 SILC_FSM_CALL(silc_skr_find(repository, find, silc_connauth_skr_callback,
580 /* Passphrase auth Ok, or no authentication required */
582 /** Authentication successful */
583 silc_fsm_next(fsm, silc_connauth_st_responder_success);
584 return SILC_FSM_CONTINUE;
587 SILC_FSM_STATE(silc_connauth_st_responder_authenticate_pk)
589 SilcConnAuth connauth = fsm_context;
592 if (connauth->aborted) {
594 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
595 return SILC_FSM_CONTINUE;
598 if (connauth->skr_status != SILC_SKR_OK) {
599 /** Public key not found */
600 SILC_LOG_DEBUG(("Public key not found, error %d", connauth->skr_status));
601 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
602 return SILC_FSM_CONTINUE;
605 SILC_LOG_DEBUG(("Found %d public keys",
606 silc_dlist_count(connauth->public_keys)));
608 /* Verify signature */
609 key = silc_dlist_get(connauth->public_keys);
610 if (!silc_connauth_verify_signature(connauth, key->key,
612 connauth->auth_data_len)) {
613 /** Invalid signature */
614 SILC_LOG_DEBUG(("Invalid signature"));
615 silc_free(connauth->auth_data);
616 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
617 return SILC_FSM_CONTINUE;
620 silc_free(connauth->auth_data);
622 /** Authentication successful */
623 silc_fsm_next(fsm, silc_connauth_st_responder_success);
624 return SILC_FSM_CONTINUE;
627 SILC_FSM_STATE(silc_connauth_st_responder_success)
629 SilcConnAuth connauth = fsm_context;
630 unsigned char tmp[4];
632 SILC_LOG_DEBUG(("Authentication successful"));
634 /* Send FAILURE packet */
635 SILC_PUT32_MSB(SILC_AUTH_OK, tmp);
636 silc_packet_send(connauth->ske->stream, SILC_PACKET_SUCCESS, 0, tmp, 4);
638 /* Call completion callback */
639 connauth->completion(connauth, TRUE, connauth->context);
641 silc_packet_stream_unlink(connauth->ske->stream,
642 &silc_connauth_stream_cbs, connauth);
643 silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
645 return SILC_FSM_FINISH;
648 SILC_FSM_STATE(silc_connauth_st_responder_failure)
650 SilcConnAuth connauth = fsm_context;
651 unsigned char error[4];
653 SILC_LOG_ERROR(("Authentication failed"));
655 /* Send FAILURE packet */
656 SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
657 silc_packet_send(connauth->ske->stream, SILC_PACKET_FAILURE, 0, error, 4);
659 /* Call completion callback */
660 connauth->completion(connauth, FALSE, connauth->context);
662 silc_packet_stream_unlink(connauth->ske->stream,
663 &silc_connauth_stream_cbs, connauth);
664 silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
666 return SILC_FSM_FINISH;
670 silc_connauth_responder(SilcConnAuth connauth,
671 SilcConnAuthGetAuthData get_auth_data,
672 SilcConnAuthCompletion completion,
675 SilcAsyncOperation op;
677 SILC_LOG_DEBUG(("Connection authentication as responder"));
679 connauth->get_auth_data = get_auth_data;
680 connauth->completion = completion;
681 connauth->context = context;
683 /* Link to packet stream to get packets */
684 silc_packet_stream_link(connauth->ske->stream,
685 &silc_connauth_stream_cbs, connauth, 1000000,
686 SILC_PACKET_CONNECTION_AUTH,
687 SILC_PACKET_FAILURE, -1);
689 /* Start the protocol */
690 op = silc_async_alloc(silc_connauth_abort, NULL, connauth);
691 silc_fsm_start(&connauth->fsm, silc_connauth_st_responder_start);