SILC SKR API changes.
[silc.git] / lib / silcske / silcconnauth.c
index 368b2aa75b1ff8e608298a3209e58804595806dc..e7b41c55e6b6b36f042db9d3b221b44ba45b8b0e 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2005 Pekka Riikonen
+  Copyright (C) 2005 - 2007 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -31,7 +31,8 @@ static SilcBool silc_connauth_packet_receive(SilcPacketEngine engine,
 /* Connection authentication context */
 struct SilcConnAuthStruct {
   SilcSKE ske;
-  SilcFSMStruct fsm;
+  SilcFSM fsm;
+  SilcAsyncOperationStruct op;
   SilcConnectionType conn_type;
   SilcAuthMethod auth_method;
   void *auth_data;
@@ -66,7 +67,7 @@ static SilcBool silc_connauth_packet_receive(SilcPacketEngine engine,
 {
   SilcConnAuth connauth = callback_context;
   connauth->packet = packet;
-  silc_fsm_continue(&connauth->fsm);
+  silc_fsm_continue(connauth->fsm);
   return TRUE;
 }
 
@@ -116,7 +117,7 @@ static SilcBool silc_connauth_get_signature(SilcConnAuth connauth,
 
   /* Compute signature */
   if (!silc_pkcs_sign(private_key, auth->data, silc_buffer_len(auth),
-                     *auth_data, len, auth_data_len, ske->prop->hash)) {
+                     *auth_data, len, auth_data_len, TRUE, ske->prop->hash)) {
     silc_free(*auth_data);
     silc_buffer_free(auth);
     return FALSE;
@@ -172,7 +173,7 @@ SILC_TASK_CALLBACK(silc_connauth_timeout)
   SilcConnAuth connauth = context;
   SILC_LOG_DEBUG(("Protocol timeout"));
   connauth->aborted = TRUE;
-  silc_fsm_continue_sync(&connauth->fsm);
+  silc_fsm_continue_sync(connauth->fsm);
 }
 
 /* SKR callback */
@@ -188,7 +189,15 @@ static void silc_connauth_skr_callback(SilcSKR skr, SilcSKRFind find,
   connauth->public_keys = results;
   connauth->skr_status = status;
 
-  SILC_FSM_CALL_CONTINUE(&connauth->fsm);
+  SILC_FSM_CALL_CONTINUE(connauth->fsm);
+}
+
+/* FSM destructor */
+
+static void silc_connauth_fsm_destructor(SilcFSM fsm, void *fsm_context,
+                                        void *destructor_context)
+{
+  silc_fsm_free(fsm);
 }
 
 
@@ -209,13 +218,16 @@ SilcConnAuth silc_connauth_alloc(SilcSchedule schedule,
   if (!connauth)
     return NULL;
 
-  if (!silc_fsm_init(&connauth->fsm, connauth, NULL, NULL, schedule)) {
+  connauth->fsm = silc_fsm_alloc(connauth, silc_connauth_fsm_destructor,
+                                NULL, schedule);
+  if (!connauth->fsm) {
     silc_connauth_free(connauth);
     return NULL;
   }
 
   connauth->timeout_secs = timeout_secs;
   connauth->ske = ske;
+  ske->refcnt++;
 
   return connauth;
 }
@@ -226,6 +238,10 @@ void silc_connauth_free(SilcConnAuth connauth)
 {
   if (connauth->public_keys)
     silc_dlist_uninit(connauth->public_keys);
+
+  /* Free reference */
+  silc_ske_free(connauth->ske);
+
   silc_free(connauth);
 }
 
@@ -250,6 +266,7 @@ SILC_FSM_STATE(silc_connauth_st_initiator_start)
   int payload_len = 0;
   unsigned char *auth_data = NULL;
   SilcUInt32 auth_data_len = 0;
+  SilcPacketFlags flags = 0;
 
   SILC_LOG_DEBUG(("Start"));
 
@@ -278,6 +295,7 @@ SILC_FSM_STATE(silc_connauth_st_initiator_start)
       return SILC_FSM_CONTINUE;
     }
     auth_data_len = connauth->auth_data_len;
+    flags = SILC_PACKET_FLAG_LONG_PAD;
     break;
 
   case SILC_AUTH_PUBLIC_KEY:
@@ -304,8 +322,8 @@ SILC_FSM_STATE(silc_connauth_st_initiator_start)
                     SILC_STR_END);
 
   /* Send the packet */
-  if (!silc_packet_send(connauth->ske->stream, SILC_PACKET_CONNECTION_AUTH, 0,
-                       packet->data, silc_buffer_len(packet))) {
+  if (!silc_packet_send(connauth->ske->stream, SILC_PACKET_CONNECTION_AUTH,
+                       flags, packet->data, silc_buffer_len(packet))) {
     /** Error sending packet */
     silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
     return SILC_FSM_CONTINUE;
@@ -344,13 +362,13 @@ SILC_FSM_STATE(silc_connauth_st_initiator_result)
   }
   silc_packet_free(connauth->packet);
 
-  /* Call completion callback */
-  connauth->completion(connauth, connauth->success, connauth->context);
-
   silc_packet_stream_unlink(connauth->ske->stream,
                            &silc_connauth_stream_cbs, connauth);
   silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
 
+  /* Call completion callback */
+  connauth->completion(connauth, connauth->success, connauth->context);
+
   return SILC_FSM_FINISH;
 }
 
@@ -361,12 +379,14 @@ SILC_FSM_STATE(silc_connauth_st_initiator_failure)
 
   SILC_LOG_DEBUG(("Start"));
 
-  /* Send FAILURE packet */
-  SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
-  silc_packet_send(connauth->ske->stream, SILC_PACKET_FAILURE, 0, error, 4);
+  if (!connauth->aborted) {
+    /* Send FAILURE packet */
+    SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
+    silc_packet_send(connauth->ske->stream, SILC_PACKET_FAILURE, 0, error, 4);
 
-  /* Call completion callback */
-  connauth->completion(connauth, FALSE, connauth->context);
+    /* Call completion callback */
+    connauth->completion(connauth, FALSE, connauth->context);
+  }
 
   silc_packet_stream_unlink(connauth->ske->stream,
                            &silc_connauth_stream_cbs, connauth);
@@ -383,8 +403,6 @@ silc_connauth_initiator(SilcConnAuth connauth,
                        SilcConnAuthCompletion completion,
                        void *context)
 {
-  SilcAsyncOperation op;
-
   SILC_LOG_DEBUG(("Connection authentication as initiator"));
 
   if (auth_method == SILC_AUTH_PASSWORD && !auth_data) {
@@ -411,10 +429,10 @@ silc_connauth_initiator(SilcConnAuth connauth,
                          SILC_PACKET_FAILURE, -1);
 
   /* Start the protocol */
-  op = silc_async_alloc(silc_connauth_abort, NULL, connauth);
-  silc_fsm_start(&connauth->fsm, silc_connauth_st_initiator_start);
+  silc_async_init(&connauth->op, silc_connauth_abort, NULL, connauth);
+  silc_fsm_start(connauth->fsm, silc_connauth_st_initiator_start);
 
-  return op;
+  return &connauth->op;
 }
 
 
@@ -572,7 +590,8 @@ SILC_FSM_STATE(silc_connauth_st_responder_authenticate)
 
     /** Find public key */
     silc_fsm_next(fsm, silc_connauth_st_responder_authenticate_pk);
-    SILC_FSM_CALL(silc_skr_find(repository, find, silc_connauth_skr_callback,
+    SILC_FSM_CALL(silc_skr_find(repository, silc_fsm_get_schedule(fsm),
+                               find, silc_connauth_skr_callback,
                                connauth));
     /* NOT REACHED */
   }
@@ -652,12 +671,14 @@ SILC_FSM_STATE(silc_connauth_st_responder_failure)
 
   SILC_LOG_ERROR(("Authentication failed"));
 
-  /* Send FAILURE packet */
-  SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
-  silc_packet_send(connauth->ske->stream, SILC_PACKET_FAILURE, 0, error, 4);
+  if (!connauth->aborted) {
+    /* Send FAILURE packet */
+    SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
+    silc_packet_send(connauth->ske->stream, SILC_PACKET_FAILURE, 0, error, 4);
 
-  /* Call completion callback */
-  connauth->completion(connauth, FALSE, connauth->context);
+    /* Call completion callback */
+    connauth->completion(connauth, FALSE, connauth->context);
+  }
 
   silc_packet_stream_unlink(connauth->ske->stream,
                            &silc_connauth_stream_cbs, connauth);
@@ -672,8 +693,6 @@ silc_connauth_responder(SilcConnAuth connauth,
                        SilcConnAuthCompletion completion,
                        void *context)
 {
-  SilcAsyncOperation op;
-
   SILC_LOG_DEBUG(("Connection authentication as responder"));
 
   connauth->get_auth_data = get_auth_data;
@@ -687,8 +706,8 @@ silc_connauth_responder(SilcConnAuth connauth,
                          SILC_PACKET_FAILURE, -1);
 
   /* Start the protocol */
-  op = silc_async_alloc(silc_connauth_abort, NULL, connauth);
-  silc_fsm_start(&connauth->fsm, silc_connauth_st_responder_start);
+  silc_async_init(&connauth->op, silc_connauth_abort, NULL, connauth);
+  silc_fsm_start(connauth->fsm, silc_connauth_st_responder_start);
 
-  return op;
+  return &connauth->op;
 }