Added silc_snprintf, silc_vsnprintf, silc_asprintf, silc_vasprintf
[silc.git] / lib / silcske / silcconnauth.c
index 368b2aa75b1ff8e608298a3209e58804595806dc..0f417d3304dc905ba623b1f120d7b6e00ab9f023 100644 (file)
@@ -31,7 +31,7 @@ static SilcBool silc_connauth_packet_receive(SilcPacketEngine engine,
 /* Connection authentication context */
 struct SilcConnAuthStruct {
   SilcSKE ske;
-  SilcFSMStruct fsm;
+  SilcFSM fsm;
   SilcConnectionType conn_type;
   SilcAuthMethod auth_method;
   void *auth_data;
@@ -66,7 +66,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;
 }
 
@@ -172,7 +172,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 +188,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,7 +217,9 @@ 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;
   }
@@ -250,13 +260,14 @@ 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"));
 
   if (connauth->aborted) {
     /** Aborted */
     silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
-    return SILC_FSM_CONTINUE;
+    SILC_FSM_CONTINUE;
   }
 
   /* Start timeout */
@@ -275,16 +286,17 @@ SILC_FSM_STATE(silc_connauth_st_initiator_start)
     if (!auth_data) {
       /** Out of memory */
       silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
-      return SILC_FSM_CONTINUE;
+      SILC_FSM_CONTINUE;
     }
     auth_data_len = connauth->auth_data_len;
+    flags = SILC_PACKET_FLAG_LONG_PAD;
     break;
 
   case SILC_AUTH_PUBLIC_KEY:
     if (!silc_connauth_get_signature(connauth, &auth_data, &auth_data_len)) {
       /** Error computing signature */
       silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
-      return SILC_FSM_CONTINUE;
+      SILC_FSM_CONTINUE;
     }
     break;
   }
@@ -294,7 +306,7 @@ SILC_FSM_STATE(silc_connauth_st_initiator_start)
   if (!packet) {
     /** Out of memory */
     silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
-    return SILC_FSM_CONTINUE;
+    SILC_FSM_CONTINUE;
   }
 
   silc_buffer_format(packet,
@@ -304,11 +316,11 @@ 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;
+    SILC_FSM_CONTINUE;
   }
 
   if (auth_data) {
@@ -319,7 +331,7 @@ SILC_FSM_STATE(silc_connauth_st_initiator_start)
 
   /** Wait for responder */
   silc_fsm_next(fsm, silc_connauth_st_initiator_result);
-  return SILC_FSM_WAIT;
+  SILC_FSM_WAIT;
 }
 
 SILC_FSM_STATE(silc_connauth_st_initiator_result)
@@ -331,7 +343,7 @@ SILC_FSM_STATE(silc_connauth_st_initiator_result)
   if (connauth->aborted) {
     /** Aborted */
     silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
-    return SILC_FSM_CONTINUE;
+    SILC_FSM_CONTINUE;
   }
 
   /* Check the status of authentication */
@@ -344,14 +356,14 @@ 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);
 
-  return SILC_FSM_FINISH;
+  /* Call completion callback */
+  connauth->completion(connauth, connauth->success, connauth->context);
+
+  SILC_FSM_FINISH;
 }
 
 SILC_FSM_STATE(silc_connauth_st_initiator_failure)
@@ -372,7 +384,7 @@ SILC_FSM_STATE(silc_connauth_st_initiator_failure)
                            &silc_connauth_stream_cbs, connauth);
   silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
 
-  return SILC_FSM_FINISH;
+  SILC_FSM_FINISH;
 }
 
 SilcAsyncOperation
@@ -412,7 +424,7 @@ silc_connauth_initiator(SilcConnAuth connauth,
 
   /* Start the protocol */
   op = silc_async_alloc(silc_connauth_abort, NULL, connauth);
-  silc_fsm_start(&connauth->fsm, silc_connauth_st_initiator_start);
+  silc_fsm_start(connauth->fsm, silc_connauth_st_initiator_start);
 
   return op;
 }
@@ -435,7 +447,7 @@ SILC_FSM_STATE(silc_connauth_st_responder_start)
   if (connauth->aborted) {
     /** Aborted */
     silc_fsm_next(fsm, silc_connauth_st_responder_failure);
-    return SILC_FSM_CONTINUE;
+    SILC_FSM_CONTINUE;
   }
 
   /* Start timeout */
@@ -446,7 +458,7 @@ SILC_FSM_STATE(silc_connauth_st_responder_start)
 
   /** Wait for initiator */
   silc_fsm_next(fsm, silc_connauth_st_responder_authenticate);
-  return SILC_FSM_WAIT;
+  SILC_FSM_WAIT;
 }
 
 SILC_FSM_STATE(silc_connauth_st_responder_authenticate)
@@ -465,14 +477,14 @@ SILC_FSM_STATE(silc_connauth_st_responder_authenticate)
     /** Aborted */
     silc_packet_free(connauth->packet);
     silc_fsm_next(fsm, silc_connauth_st_responder_failure);
-    return SILC_FSM_CONTINUE;
+    SILC_FSM_CONTINUE;
   }
 
   if (connauth->packet->type != SILC_PACKET_CONNECTION_AUTH) {
     /** Protocol failure */
     silc_packet_free(connauth->packet);
     silc_fsm_next(fsm, silc_connauth_st_responder_failure);
-    return SILC_FSM_CONTINUE;
+    SILC_FSM_CONTINUE;
   }
 
   /* Parse the received authentication data packet. The received
@@ -486,7 +498,7 @@ SILC_FSM_STATE(silc_connauth_st_responder_authenticate)
     SILC_LOG_ERROR(("Bad payload in authentication packet"));
     silc_packet_free(connauth->packet);
     silc_fsm_next(fsm, silc_connauth_st_responder_failure);
-    return SILC_FSM_CONTINUE;
+    SILC_FSM_CONTINUE;
   }
 
   if (payload_len != silc_buffer_len(&connauth->packet->buffer)) {
@@ -494,7 +506,7 @@ SILC_FSM_STATE(silc_connauth_st_responder_authenticate)
     SILC_LOG_ERROR(("Bad payload length in authentication packet"));
     silc_packet_free(connauth->packet);
     silc_fsm_next(fsm, silc_connauth_st_responder_failure);
-    return SILC_FSM_CONTINUE;
+    SILC_FSM_CONTINUE;
   }
 
   payload_len -= 4;
@@ -505,7 +517,7 @@ SILC_FSM_STATE(silc_connauth_st_responder_authenticate)
                    conn_type));
     silc_packet_free(connauth->packet);
     silc_fsm_next(fsm, silc_connauth_st_responder_failure);
-    return SILC_FSM_CONTINUE;
+    SILC_FSM_CONTINUE;
   }
 
   if (payload_len > 0) {
@@ -520,7 +532,7 @@ SILC_FSM_STATE(silc_connauth_st_responder_authenticate)
       SILC_LOG_DEBUG(("Bad payload in authentication payload"));
       silc_packet_free(connauth->packet);
       silc_fsm_next(fsm, silc_connauth_st_responder_failure);
-      return SILC_FSM_CONTINUE;
+      SILC_FSM_CONTINUE;
     }
   }
   silc_packet_free(connauth->packet);
@@ -534,7 +546,7 @@ SILC_FSM_STATE(silc_connauth_st_responder_authenticate)
     /** Connection not configured */
     SILC_LOG_ERROR(("Remote connection not configured"));
     silc_fsm_next(fsm, silc_connauth_st_responder_failure);
-    return SILC_FSM_CONTINUE;
+    SILC_FSM_CONTINUE;
   }
 
   /* Verify */
@@ -545,7 +557,7 @@ SILC_FSM_STATE(silc_connauth_st_responder_authenticate)
     if (!memcmp(auth_data, passphrase, passphrase_len)) {
       /** Authentication failed */
       silc_fsm_next(fsm, silc_connauth_st_responder_failure);
-      return SILC_FSM_CONTINUE;
+      SILC_FSM_CONTINUE;
     }
   } else if (repository) {
     /* Digital signature */
@@ -562,7 +574,7 @@ SILC_FSM_STATE(silc_connauth_st_responder_authenticate)
     if (!find || !connauth->auth_data) {
       /** Out of memory */
       silc_fsm_next(fsm, silc_connauth_st_responder_failure);
-      return SILC_FSM_CONTINUE;
+      SILC_FSM_CONTINUE;
     }
 
     silc_skr_find_set_pkcs_type(find, connauth->ske->pk_type);
@@ -581,7 +593,7 @@ SILC_FSM_STATE(silc_connauth_st_responder_authenticate)
 
   /** Authentication successful */
   silc_fsm_next(fsm, silc_connauth_st_responder_success);
-  return SILC_FSM_CONTINUE;
+  SILC_FSM_CONTINUE;
 }
 
 SILC_FSM_STATE(silc_connauth_st_responder_authenticate_pk)
@@ -592,14 +604,14 @@ SILC_FSM_STATE(silc_connauth_st_responder_authenticate_pk)
   if (connauth->aborted) {
     /** Aborted */
     silc_fsm_next(fsm, silc_connauth_st_responder_failure);
-    return SILC_FSM_CONTINUE;
+    SILC_FSM_CONTINUE;
   }
 
   if (connauth->skr_status != SILC_SKR_OK) {
     /** Public key not found */
     SILC_LOG_DEBUG(("Public key not found, error %d", connauth->skr_status));
     silc_fsm_next(fsm, silc_connauth_st_responder_failure);
-    return SILC_FSM_CONTINUE;
+    SILC_FSM_CONTINUE;
   }
 
   SILC_LOG_DEBUG(("Found %d public keys",
@@ -614,14 +626,14 @@ SILC_FSM_STATE(silc_connauth_st_responder_authenticate_pk)
     SILC_LOG_DEBUG(("Invalid signature"));
     silc_free(connauth->auth_data);
     silc_fsm_next(fsm, silc_connauth_st_responder_failure);
-    return SILC_FSM_CONTINUE;
+    SILC_FSM_CONTINUE;
   }
 
   silc_free(connauth->auth_data);
 
   /** Authentication successful */
   silc_fsm_next(fsm, silc_connauth_st_responder_success);
-  return SILC_FSM_CONTINUE;
+  SILC_FSM_CONTINUE;
 }
 
 SILC_FSM_STATE(silc_connauth_st_responder_success)
@@ -642,7 +654,7 @@ SILC_FSM_STATE(silc_connauth_st_responder_success)
                            &silc_connauth_stream_cbs, connauth);
   silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
 
-  return SILC_FSM_FINISH;
+  SILC_FSM_FINISH;
 }
 
 SILC_FSM_STATE(silc_connauth_st_responder_failure)
@@ -663,7 +675,7 @@ SILC_FSM_STATE(silc_connauth_st_responder_failure)
                            &silc_connauth_stream_cbs, connauth);
   silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
 
-  return SILC_FSM_FINISH;
+  SILC_FSM_FINISH;
 }
 
 SilcAsyncOperation
@@ -688,7 +700,7 @@ silc_connauth_responder(SilcConnAuth connauth,
 
   /* Start the protocol */
   op = silc_async_alloc(silc_connauth_abort, NULL, connauth);
-  silc_fsm_start(&connauth->fsm, silc_connauth_st_responder_start);
+  silc_fsm_start(connauth->fsm, silc_connauth_st_responder_start);
 
   return op;
 }