5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2005 - 2007 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 SilcAsyncOperationStruct op;
36 SilcConnectionType conn_type;
37 SilcAuthMethod auth_method;
39 SilcUInt32 auth_data_len;
40 SilcConnAuthCompletion completion;
41 SilcConnAuthGetAuthData get_auth_data;
43 SilcDList public_keys;
44 SilcSKRStatus skr_status;
45 SilcUInt32 timeout_secs;
47 unsigned int aborted : 1;
48 unsigned int success : 1;
51 /* Packet stream callbacks */
52 static SilcPacketCallbacks silc_connauth_stream_cbs =
54 silc_connauth_packet_receive, NULL, NULL
58 /************************ Static utility functions **************************/
62 static SilcBool silc_connauth_packet_receive(SilcPacketEngine engine,
63 SilcPacketStream stream,
65 void *callback_context,
68 SilcConnAuth connauth = callback_context;
69 connauth->packet = packet;
70 silc_fsm_continue(connauth->fsm);
74 /* Async operation abortion callback */
76 static void silc_connauth_abort(SilcAsyncOperation op, void *context)
78 SilcConnAuth connauth = context;
79 connauth->aborted = TRUE;
82 /* Generates signature for public key based authentication */
84 static SilcBool silc_connauth_get_signature(SilcConnAuth connauth,
85 unsigned char **auth_data,
86 SilcUInt32 *auth_data_len)
90 SilcPrivateKey private_key;
93 SILC_LOG_DEBUG(("Compute signature"));
96 private_key = connauth->auth_data;
98 /* Make the authentication data. Protocol says it is HASH plus
100 len = ske->hash_len + silc_buffer_len(ske->start_payload_copy);
101 auth = silc_buffer_alloc_size(len);
104 silc_buffer_format(auth,
105 SILC_STR_UI_XNSTRING(ske->hash, ske->hash_len),
106 SILC_STR_UI_XNSTRING(
107 ske->start_payload_copy->data,
108 silc_buffer_len(ske->start_payload_copy)),
111 len = ((silc_pkcs_private_key_get_len(private_key) + 7) / 8) + 1;
112 *auth_data = silc_calloc(len, sizeof(**auth_data));
113 if (*auth_data == NULL) {
114 silc_buffer_free(auth);
118 /* Compute signature */
119 if (!silc_pkcs_sign(private_key, auth->data, silc_buffer_len(auth),
120 *auth_data, len, auth_data_len, TRUE, ske->prop->hash)) {
121 silc_free(*auth_data);
122 silc_buffer_free(auth);
126 silc_buffer_free(auth);
130 /* Verifies digital signature */
132 static SilcBool silc_connauth_verify_signature(SilcConnAuth connauth,
133 SilcPublicKey pub_key,
139 SilcSKE ske = connauth->ske;
141 if (!pub_key || !sign)
144 /* Make the authentication data. Protocol says it is HASH plus
146 len = ske->hash_len + silc_buffer_len(ske->start_payload_copy);
147 auth = silc_buffer_alloc_size(len);
150 silc_buffer_format(auth,
151 SILC_STR_UI_XNSTRING(ske->hash, ske->hash_len),
152 SILC_STR_UI_XNSTRING(
153 ske->start_payload_copy->data,
154 silc_buffer_len(ske->start_payload_copy)),
157 /* Verify signature */
158 if (!silc_pkcs_verify(pub_key, sign, sign_len, auth->data,
159 silc_buffer_len(auth), ske->prop->hash)) {
160 silc_buffer_free(auth);
164 silc_buffer_free(auth);
171 SILC_TASK_CALLBACK(silc_connauth_timeout)
173 SilcConnAuth connauth = context;
174 SILC_LOG_DEBUG(("Protocol timeout"));
175 connauth->aborted = TRUE;
176 silc_fsm_continue_sync(connauth->fsm);
181 static void silc_connauth_skr_callback(SilcSKR skr, SilcSKRFind find,
182 SilcSKRStatus status,
183 SilcDList results, void *context)
185 SilcConnAuth connauth = context;
187 silc_skr_find_free(find);
189 connauth->public_keys = results;
190 connauth->skr_status = status;
192 SILC_FSM_CALL_CONTINUE(connauth->fsm);
197 static void silc_connauth_fsm_destructor(SilcFSM fsm, void *fsm_context,
198 void *destructor_context)
204 /******************************* Protocol API *******************************/
206 /* Allocate connection authentication context */
208 SilcConnAuth silc_connauth_alloc(SilcSchedule schedule,
210 SilcUInt32 timeout_secs)
212 SilcConnAuth connauth;
214 if (!schedule || !ske)
217 connauth = silc_calloc(1, sizeof(*connauth));
221 connauth->fsm = silc_fsm_alloc(connauth, silc_connauth_fsm_destructor,
223 if (!connauth->fsm) {
224 silc_connauth_free(connauth);
228 connauth->timeout_secs = timeout_secs;
235 /* Free connection authentication context */
237 void silc_connauth_free(SilcConnAuth connauth)
239 if (connauth->public_keys)
240 silc_dlist_uninit(connauth->public_keys);
243 silc_ske_free(connauth->ske);
248 /* Return associated SKE context */
250 SilcSKE silc_connauth_get_ske(SilcConnAuth connauth)
252 return connauth->ske;
256 /******************************** Initiator *********************************/
258 SILC_FSM_STATE(silc_connauth_st_initiator_start);
259 SILC_FSM_STATE(silc_connauth_st_initiator_result);
260 SILC_FSM_STATE(silc_connauth_st_initiator_failure);
262 SILC_FSM_STATE(silc_connauth_st_initiator_start)
264 SilcConnAuth connauth = fsm_context;
267 unsigned char *auth_data = NULL;
268 SilcUInt32 auth_data_len = 0;
269 SilcPacketFlags flags = 0;
271 SILC_LOG_DEBUG(("Start"));
273 if (connauth->aborted) {
275 silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
276 return SILC_FSM_CONTINUE;
280 if (connauth->timeout_secs)
281 silc_schedule_task_add_timeout(silc_fsm_get_schedule(fsm),
282 silc_connauth_timeout, connauth,
283 connauth->timeout_secs, 0);
285 switch (connauth->auth_method) {
287 /* No authentication required */
290 case SILC_AUTH_PASSWORD:
291 auth_data = silc_memdup(connauth->auth_data, connauth->auth_data_len);
294 silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
295 return SILC_FSM_CONTINUE;
297 auth_data_len = connauth->auth_data_len;
298 flags = SILC_PACKET_FLAG_LONG_PAD;
301 case SILC_AUTH_PUBLIC_KEY:
302 if (!silc_connauth_get_signature(connauth, &auth_data, &auth_data_len)) {
303 /** Error computing signature */
304 silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
305 return SILC_FSM_CONTINUE;
310 payload_len = 4 + auth_data_len;
311 packet = silc_buffer_alloc_size(payload_len);
314 silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
315 return SILC_FSM_CONTINUE;
318 silc_buffer_format(packet,
319 SILC_STR_UI_SHORT(payload_len),
320 SILC_STR_UI_SHORT(connauth->conn_type),
321 SILC_STR_UI_XNSTRING(auth_data, auth_data_len),
324 /* Send the packet */
325 if (!silc_packet_send(connauth->ske->stream, SILC_PACKET_CONNECTION_AUTH,
326 flags, packet->data, silc_buffer_len(packet))) {
327 /** Error sending packet */
328 silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
329 return SILC_FSM_CONTINUE;
333 memset(auth_data, 0, auth_data_len);
334 silc_free(auth_data);
336 silc_buffer_free(packet);
338 /** Wait for responder */
339 silc_fsm_next(fsm, silc_connauth_st_initiator_result);
340 return SILC_FSM_WAIT;
343 SILC_FSM_STATE(silc_connauth_st_initiator_result)
345 SilcConnAuth connauth = fsm_context;
347 SILC_LOG_DEBUG(("Start"));
349 if (connauth->aborted) {
351 silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
352 return SILC_FSM_CONTINUE;
355 /* Check the status of authentication */
356 if (connauth->packet->type == SILC_PACKET_SUCCESS) {
357 SILC_LOG_DEBUG(("Authentication successful"));
358 connauth->success = TRUE;
360 SILC_LOG_DEBUG(("Authentication failed"));
361 connauth->success = FALSE;
363 silc_packet_free(connauth->packet);
365 silc_packet_stream_unlink(connauth->ske->stream,
366 &silc_connauth_stream_cbs, connauth);
367 silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
369 /* Call completion callback */
370 connauth->completion(connauth, connauth->success, connauth->context);
372 return SILC_FSM_FINISH;
375 SILC_FSM_STATE(silc_connauth_st_initiator_failure)
377 SilcConnAuth connauth = fsm_context;
378 unsigned char error[4];
380 SILC_LOG_DEBUG(("Start"));
382 if (!connauth->aborted) {
383 /* Send FAILURE packet */
384 SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
385 silc_packet_send(connauth->ske->stream, SILC_PACKET_FAILURE, 0, error, 4);
387 silc_packet_stream_unlink(connauth->ske->stream,
388 &silc_connauth_stream_cbs, connauth);
389 silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
391 /* Call completion callback */
392 connauth->completion(connauth, FALSE, connauth->context);
393 return SILC_FSM_FINISH;
396 silc_packet_stream_unlink(connauth->ske->stream,
397 &silc_connauth_stream_cbs, connauth);
398 silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
400 return SILC_FSM_FINISH;
404 silc_connauth_initiator(SilcConnAuth connauth,
405 SilcConnectionType conn_type,
406 SilcAuthMethod auth_method, void *auth_data,
407 SilcUInt32 auth_data_len,
408 SilcConnAuthCompletion completion,
411 SILC_LOG_DEBUG(("Connection authentication as initiator"));
413 if (auth_method == SILC_AUTH_PASSWORD && !auth_data) {
414 completion(connauth, FALSE, context);
418 if (auth_method == SILC_AUTH_PUBLIC_KEY && !auth_data) {
419 completion(connauth, FALSE, context);
423 connauth->conn_type = conn_type;
424 connauth->auth_method = auth_method;
425 connauth->auth_data = auth_data;
426 connauth->auth_data_len = auth_data_len;
427 connauth->completion = completion;
428 connauth->context = context;
430 /* Link to packet stream to get packets */
431 silc_packet_stream_link(connauth->ske->stream,
432 &silc_connauth_stream_cbs, connauth, 1000000,
434 SILC_PACKET_FAILURE, -1);
436 /* Start the protocol */
437 silc_async_init(&connauth->op, silc_connauth_abort, NULL, connauth);
438 silc_fsm_start(connauth->fsm, silc_connauth_st_initiator_start);
440 return &connauth->op;
444 /******************************** Responder *********************************/
446 SILC_FSM_STATE(silc_connauth_st_responder_start);
447 SILC_FSM_STATE(silc_connauth_st_responder_authenticate);
448 SILC_FSM_STATE(silc_connauth_st_responder_authenticate_pk);
449 SILC_FSM_STATE(silc_connauth_st_responder_success);
450 SILC_FSM_STATE(silc_connauth_st_responder_failure);
452 SILC_FSM_STATE(silc_connauth_st_responder_start)
454 SilcConnAuth connauth = fsm_context;
456 SILC_LOG_DEBUG(("Start"));
458 if (connauth->aborted) {
460 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
461 return SILC_FSM_CONTINUE;
465 if (connauth->timeout_secs)
466 silc_schedule_task_add_timeout(silc_fsm_get_schedule(fsm),
467 silc_connauth_timeout, connauth,
468 connauth->timeout_secs, 0);
470 /** Wait for initiator */
471 silc_fsm_next(fsm, silc_connauth_st_responder_authenticate);
472 return SILC_FSM_WAIT;
475 SILC_FSM_STATE(silc_connauth_st_responder_authenticate)
477 SilcConnAuth connauth = fsm_context;
478 SilcUInt16 payload_len;
479 SilcUInt16 conn_type;
480 unsigned char *auth_data = NULL, *passphrase = NULL;
481 SilcUInt32 passphrase_len;
482 SilcSKR repository = NULL;
485 SILC_LOG_DEBUG(("Start"));
487 if (connauth->aborted) {
489 silc_packet_free(connauth->packet);
490 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
491 return SILC_FSM_CONTINUE;
494 if (connauth->packet->type != SILC_PACKET_CONNECTION_AUTH) {
495 /** Protocol failure */
496 silc_packet_free(connauth->packet);
497 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
498 return SILC_FSM_CONTINUE;
501 /* Parse the received authentication data packet. The received
502 payload is Connection Auth Payload. */
503 ret = silc_buffer_unformat(&connauth->packet->buffer,
504 SILC_STR_UI_SHORT(&payload_len),
505 SILC_STR_UI_SHORT(&conn_type),
509 SILC_LOG_ERROR(("Bad payload in authentication packet"));
510 silc_packet_free(connauth->packet);
511 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
512 return SILC_FSM_CONTINUE;
515 if (payload_len != silc_buffer_len(&connauth->packet->buffer)) {
516 /** Bad payload length */
517 SILC_LOG_ERROR(("Bad payload length in authentication packet"));
518 silc_packet_free(connauth->packet);
519 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
520 return SILC_FSM_CONTINUE;
525 if (conn_type < SILC_CONN_CLIENT || conn_type > SILC_CONN_ROUTER) {
526 /** Bad connection type */
527 SILC_LOG_ERROR(("Bad connection type (%d) in authentication packet",
529 silc_packet_free(connauth->packet);
530 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
531 return SILC_FSM_CONTINUE;
534 if (payload_len > 0) {
535 /* Get authentication data */
536 ret = silc_buffer_unformat(&connauth->packet->buffer,
538 SILC_STR_UI_XNSTRING(&auth_data,
543 SILC_LOG_DEBUG(("Bad payload in authentication payload"));
544 silc_packet_free(connauth->packet);
545 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
546 return SILC_FSM_CONTINUE;
549 silc_packet_free(connauth->packet);
551 SILC_LOG_DEBUG(("Remote connection type %d", conn_type));
553 /* Get authentication data */
554 if (!connauth->get_auth_data(connauth, conn_type, &passphrase,
555 &passphrase_len, &repository,
556 connauth->context)) {
557 /** Connection not configured */
558 SILC_LOG_ERROR(("Remote connection not configured"));
559 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
560 return SILC_FSM_CONTINUE;
565 /* Passphrase authentication */
566 if (passphrase && passphrase_len) {
567 SILC_LOG_DEBUG(("Passphrase authentication"));
568 if (!auth_data || payload_len != passphrase_len ||
569 memcmp(auth_data, passphrase, passphrase_len)) {
570 /** Authentication failed */
571 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
572 return SILC_FSM_CONTINUE;
574 } else if (repository) {
575 /* Digital signature */
578 SILC_LOG_DEBUG(("Digital signature authentication"));
581 /** Authentication failed */
582 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
583 return SILC_FSM_CONTINUE;
586 connauth->auth_data = silc_memdup(auth_data, payload_len);
587 connauth->auth_data_len = payload_len;
589 /* Allocate search constraints for finding the key */
590 find = silc_skr_find_alloc();
592 if (!find || !connauth->auth_data) {
594 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
595 return SILC_FSM_CONTINUE;
598 silc_skr_find_set_pkcs_type(find, connauth->ske->pk_type);
599 silc_skr_find_set_public_key(find, connauth->ske->public_key);
600 silc_skr_find_set_usage(find, (SILC_SKR_USAGE_AUTH |
601 SILC_SKR_USAGE_KEY_AGREEMENT));
603 /** Find public key */
604 silc_fsm_next(fsm, silc_connauth_st_responder_authenticate_pk);
605 SILC_FSM_CALL(silc_skr_find(repository, silc_fsm_get_schedule(fsm),
606 find, silc_connauth_skr_callback,
611 /* Passphrase auth Ok, or no authentication required */
613 /** Authentication successful */
614 silc_fsm_next(fsm, silc_connauth_st_responder_success);
615 return SILC_FSM_CONTINUE;
618 SILC_FSM_STATE(silc_connauth_st_responder_authenticate_pk)
620 SilcConnAuth connauth = fsm_context;
623 if (connauth->aborted) {
625 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
626 return SILC_FSM_CONTINUE;
629 if (connauth->skr_status != SILC_SKR_OK) {
630 /** Public key not found */
631 SILC_LOG_DEBUG(("Public key not found, error %d", connauth->skr_status));
632 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
633 return SILC_FSM_CONTINUE;
636 SILC_LOG_DEBUG(("Found %d public keys",
637 silc_dlist_count(connauth->public_keys)));
639 /* Verify signature */
640 key = silc_dlist_get(connauth->public_keys);
641 if (!silc_connauth_verify_signature(connauth, key->key,
643 connauth->auth_data_len)) {
644 /** Invalid signature */
645 SILC_LOG_DEBUG(("Invalid signature"));
646 silc_free(connauth->auth_data);
647 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
648 return SILC_FSM_CONTINUE;
651 silc_free(connauth->auth_data);
653 /** Authentication successful */
654 silc_fsm_next(fsm, silc_connauth_st_responder_success);
655 return SILC_FSM_CONTINUE;
658 SILC_FSM_STATE(silc_connauth_st_responder_success)
660 SilcConnAuth connauth = fsm_context;
661 unsigned char tmp[4];
663 SILC_LOG_DEBUG(("Authentication successful"));
665 /* Send FAILURE packet */
666 SILC_PUT32_MSB(SILC_AUTH_OK, tmp);
667 silc_packet_send(connauth->ske->stream, SILC_PACKET_SUCCESS, 0, tmp, 4);
669 silc_packet_stream_unlink(connauth->ske->stream,
670 &silc_connauth_stream_cbs, connauth);
671 silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
673 /* Call completion callback */
674 connauth->completion(connauth, TRUE, connauth->context);
676 return SILC_FSM_FINISH;
679 SILC_FSM_STATE(silc_connauth_st_responder_failure)
681 SilcConnAuth connauth = fsm_context;
682 unsigned char error[4];
684 SILC_LOG_ERROR(("Authentication failed"));
686 if (!connauth->aborted) {
687 /* Send FAILURE packet */
688 SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
689 silc_packet_send(connauth->ske->stream, SILC_PACKET_FAILURE, 0, error, 4);
691 silc_packet_stream_unlink(connauth->ske->stream,
692 &silc_connauth_stream_cbs, connauth);
693 silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
695 /* Call completion callback */
696 connauth->completion(connauth, FALSE, connauth->context);
698 return SILC_FSM_FINISH;
701 silc_packet_stream_unlink(connauth->ske->stream,
702 &silc_connauth_stream_cbs, connauth);
703 silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
705 return SILC_FSM_FINISH;
709 silc_connauth_responder(SilcConnAuth connauth,
710 SilcConnAuthGetAuthData get_auth_data,
711 SilcConnAuthCompletion completion,
714 SILC_LOG_DEBUG(("Connection authentication as responder"));
716 connauth->get_auth_data = get_auth_data;
717 connauth->completion = completion;
718 connauth->context = context;
720 /* Link to packet stream to get packets */
721 silc_packet_stream_link(connauth->ske->stream,
722 &silc_connauth_stream_cbs, connauth, 1000000,
723 SILC_PACKET_CONNECTION_AUTH,
724 SILC_PACKET_FAILURE, -1);
726 /* Start the protocol */
727 silc_async_init(&connauth->op, silc_connauth_abort, NULL, connauth);
728 silc_fsm_start(connauth->fsm, silc_connauth_st_responder_start);
730 return &connauth->op;