[rs-commit] r43 - in /redwax-signtext/trunk/src/linux: crypto.c crypto.h message.c message.h signtext.c signtext.h

rs-commit at redwax.eu rs-commit at redwax.eu
Mon Sep 12 11:21:24 CEST 2022


Author: minfrin at redwax.eu
Date: Mon Sep 12 11:21:24 2022
New Revision: 43

Log:
Return the result to the browser.

Modified:
    redwax-signtext/trunk/src/linux/crypto.c
    redwax-signtext/trunk/src/linux/crypto.h
    redwax-signtext/trunk/src/linux/message.c
    redwax-signtext/trunk/src/linux/message.h
    redwax-signtext/trunk/src/linux/signtext.c
    redwax-signtext/trunk/src/linux/signtext.h

Modified: redwax-signtext/trunk/src/linux/crypto.c
==============================================================================
--- redwax-signtext/trunk/src/linux/crypto.c	(original)
+++ redwax-signtext/trunk/src/linux/crypto.c	Mon Sep 12 11:21:24 2022
@@ -17,6 +17,7 @@
 
 
 #include "crypto.h"
+#include "message.h"
 
 #include <ksba.h>
 
@@ -443,6 +444,11 @@
     g_error_free (gerror);
   }
 
+  /*
+   * The instance is done at this point, clean it up.
+   */
+  signtext_instance_free(instance);
+
 }
 
 
@@ -460,13 +466,15 @@
 static gboolean
 crypto_sign_pulse (gpointer user_data)
 {
-  SignTextInstance *instance = user_data;
-
-  if (gtk_widget_is_visible(GTK_WIDGET(instance->progress))) {
-    gtk_progress_bar_pulse (instance->progress);
+  GtkProgressBar *progress = user_data;
+
+  if (gtk_widget_is_visible(GTK_WIDGET(progress))) {
+    gtk_progress_bar_pulse (progress);
 
     return TRUE;
   }
+
+  g_object_unref(progress);
 
   return FALSE;
 }
@@ -487,12 +495,6 @@
   memcpy(instance->payload + instance->payload_len, buffer, count);
   instance->payload_len = len;
 
-  if (len > 1024) {
-
-    
-
-  }
-
   return 0;
 }
 
@@ -501,10 +503,9 @@
  *
  * Are we good to leave?
  */
-static void
-crypto_sign_done (gpointer user_data)
-{
-  SignTextInstance *instance = user_data;
+void
+crypto_sign_done (SignTextInstance *instance)
+{
   GError *gerror = NULL;
 
   g_printerr("crypto_sign_done\n");
@@ -516,6 +517,45 @@
     g_error_free (gerror);
   }
 
+}
+
+static void
+crypto_sign_send_payload(SignTextInstance *instance, gboolean done)
+{
+  gchar *out = g_malloc( ((instance->payload_len / 3 + 1) * 4 + 4) +
+                         (((instance->payload_len / 3 + 1) * 4 + 4) / 76 + 1) +
+                         (done ? 5 : 0) +
+                         1
+                       );
+  gsize c1, c2;
+
+  g_printerr("crypto_sign_send_payload\n");
+
+  instance->done = done;
+
+  if (instance->payload) {
+    c1 = g_base64_encode_step(instance->payload, instance->payload_len, TRUE, out, &instance->state, &instance->save);
+  }
+  else {
+    c1 = 0;
+  }
+
+  if (done) {
+    c2 = g_base64_encode_close(TRUE, out + c1, &instance->state, &instance->save);
+    out[c1 + c2] = 0;
+  }
+  else {
+    out[c1] = 0;
+  }
+
+  message_send_string_response(instance, out);
+
+  /* out is freed by the message */
+
+  if (instance->payload) {
+    instance->payload_len = 0;
+    instance->payload = g_realloc(instance->payload, instance->payload_len);
+  }
 }
 
 /*
@@ -660,11 +700,16 @@
 
   }
 
-  if (instance->stopreason != KSBA_SR_READY) {
-    g_idle_add((GSourceFunc)crypto_sign_do, instance);
+  if (instance->stopreason == KSBA_SR_READY) {
+    crypto_sign_send_payload(instance, TRUE);
   }
   else {
-    g_idle_add((GSourceFunc)crypto_sign_done, instance);
+    if (instance->payload_len > 1000000) {
+      crypto_sign_send_payload(instance, FALSE);
+    }
+    else {
+      crypto_sign_continue(instance);
+    }
   }
 
   if (gerror) {
@@ -675,10 +720,30 @@
 
 }
 
+/*
+ * Continue the signing and data transfer process.
+ *
+ * If we're busy, we line up the next crypto_sign_do.
+ *
+ * If we're no longer busy, send the final eof and line up
+ * the logout once we're done.
+ */
 void
 crypto_sign_continue (SignTextInstance *instance)
 {
-  g_idle_add((GSourceFunc)crypto_sign_do, instance);
+  if (instance->stopreason == KSBA_SR_READY) {
+
+    /* send eof, we're done */
+    message_send_boolean_response(instance, FALSE);
+
+    g_idle_add((GSourceFunc)crypto_sign_done, instance);
+
+  }
+  else {
+
+    g_idle_add((GSourceFunc)crypto_sign_do, instance);
+
+  }
 }
 
 /*
@@ -1110,8 +1175,8 @@
 
   gcr_pkcs11_certificate_lookup_issuer_async(GCR_CERTIFICATE(instance->certificate), instance->signtext->cancellable, crypto_sign_lookup, instance);
 
-  g_timeout_add(250, crypto_sign_pulse, instance);
-
+  g_object_ref(instance->progress);
+  g_timeout_add(250, crypto_sign_pulse, instance->progress);
   gtk_widget_set_visible(GTK_WIDGET(instance->progress), TRUE);
 
   gtk_widget_set_sensitive(GTK_WIDGET(instance->selector), FALSE);

Modified: redwax-signtext/trunk/src/linux/crypto.h
==============================================================================
--- redwax-signtext/trunk/src/linux/crypto.h	(original)
+++ redwax-signtext/trunk/src/linux/crypto.h	Mon Sep 12 11:21:24 2022
@@ -37,6 +37,9 @@
 void
 crypto_sign_continue(SignTextInstance *instance);
 
+void
+crypto_sign_done(SignTextInstance *instance);
+
 #endif
 
 

Modified: redwax-signtext/trunk/src/linux/message.c
==============================================================================
--- redwax-signtext/trunk/src/linux/message.c	(original)
+++ redwax-signtext/trunk/src/linux/message.c	Mon Sep 12 11:21:24 2022
@@ -41,8 +41,6 @@
                             gpointer user_data)
 {
   SignTextInstance *instance = user_data;
-  JsonBuilder *builder;
-  JsonNode *root;
   GList *pages;
 
   g_printerr("message_cancel_clicked\n");
@@ -50,19 +48,7 @@
   /*
    * Send an error to show we cancelled.
    */
-  builder = json_builder_new_immutable();
-  json_builder_begin_object(builder);
-  json_builder_set_member_name(builder, "uuid");
-  json_builder_add_string_value(builder, instance->uuid);
-  json_builder_set_member_name(builder, "id");
-  json_builder_add_int_value(builder, instance->id);
-  json_builder_set_member_name(builder, "error");
-  json_builder_add_string_value(builder, "error:cancelled");
-  json_builder_end_object(builder);
-
-  root = json_builder_get_root(builder);
-
-  message_send(instance->signtext, root);
+  message_send_error(instance, "error:cancelled");
 
   gtk_container_remove(GTK_CONTAINER(instance->stack), GTK_WIDGET(instance->box));
 
@@ -168,8 +154,6 @@
      */
     if (json_node_get_value_type(request) == G_TYPE_STRING) {
 
-      JsonBuilder *builder;
-
       GtkTextIter end;
 
       gtk_text_buffer_get_end_iter(instance->buffer, &end);
@@ -178,19 +162,7 @@
       /*
        * A response with a value of TRUE means ACK, send us the next bit of data.
        */
-      builder = json_builder_new_immutable();
-      json_builder_begin_object(builder);
-      json_builder_set_member_name(builder, "uuid");
-      json_builder_add_string_value(builder, uuid);
-      json_builder_set_member_name(builder, "id");
-      json_builder_add_int_value(builder, id);
-      json_builder_set_member_name(builder, "response");
-      json_builder_add_boolean_value(builder, TRUE);
-      json_builder_end_object(builder);
-
-      root = json_builder_get_root(builder);
-
-      message_send(signtext, root);
+      message_send_boolean_response(instance, TRUE);
 
     }
 
@@ -241,6 +213,24 @@
     }
 
     /*
+     * A boolean request is an ack/nak.
+     */
+    else if (json_node_get_value_type(request) == G_TYPE_BOOLEAN) {
+
+      if (json_node_get_boolean(request)) {
+
+        /* ack, send the next bit */
+        crypto_sign_continue(instance);
+
+      }
+      else {
+
+        /* nak, close everything down */
+// fixme
+      }
+    }
+
+    /*
      * Anything else is an error.
      */
     else {
@@ -465,8 +455,10 @@
  *
  * At this point the loop is completed by repeating the request to receive the next
  * single JSON message on stdin.
+ *
+ * The JsonNode is freed at the end of the task.
  */
-void
+static void
 message_send(SignTextData *signtext, JsonNode *root)
 {
   GTask *task = g_task_new (signtext->window, signtext->cancellable, message_send_done, signtext);
@@ -475,4 +467,73 @@
   g_object_unref (task);
 }
 
-
+void
+message_send_error(SignTextInstance *instance, gchar *error)
+{
+  JsonBuilder *builder;
+  JsonNode *root;
+
+  builder = json_builder_new_immutable();
+  json_builder_begin_object(builder);
+  json_builder_set_member_name(builder, "uuid");
+  json_builder_add_string_value(builder, instance->uuid);
+  json_builder_set_member_name(builder, "id");
+  json_builder_add_int_value(builder, instance->id);
+  json_builder_set_member_name(builder, "error");
+  json_builder_add_string_value(builder, "error:cancelled");
+  json_builder_end_object(builder);
+
+  root = json_builder_get_root(builder);
+
+  message_send(instance->signtext, root);
+
+  g_object_unref(builder);
+}
+
+void
+message_send_boolean_response(SignTextInstance *instance, gboolean response)
+{
+  JsonBuilder *builder;
+  JsonNode *root;
+
+  builder = json_builder_new_immutable();
+  json_builder_begin_object(builder);
+  json_builder_set_member_name(builder, "uuid");
+  json_builder_add_string_value(builder, instance->uuid);
+  json_builder_set_member_name(builder, "id");
+  json_builder_add_int_value(builder, instance->id);
+  json_builder_set_member_name(builder, "response");
+  json_builder_add_boolean_value(builder, response);
+  json_builder_end_object(builder);
+
+  root = json_builder_get_root(builder);
+
+  message_send(instance->signtext, root);
+
+  g_object_unref(builder);
+}
+
+void
+message_send_string_response(SignTextInstance *instance, gchar *response)
+{
+  JsonBuilder *builder;
+  JsonNode *root;
+
+  builder = json_builder_new_immutable();
+  json_builder_begin_object(builder);
+  json_builder_set_member_name(builder, "uuid");
+  json_builder_add_string_value(builder, instance->uuid);
+  json_builder_set_member_name(builder, "id");
+  json_builder_add_int_value(builder, instance->id);
+  json_builder_set_member_name(builder, "response");
+  json_builder_add_string_value(builder, response);
+  json_builder_end_object(builder);
+
+  root = json_builder_get_root(builder);
+
+  message_send(instance->signtext, root);
+
+  g_object_unref(builder);
+}
+
+

Modified: redwax-signtext/trunk/src/linux/message.h
==============================================================================
--- redwax-signtext/trunk/src/linux/message.h	(original)
+++ redwax-signtext/trunk/src/linux/message.h	Mon Sep 12 11:21:24 2022
@@ -25,8 +25,17 @@
 void
 message_receive(SignTextData *signtext);
 
+//void
+//message_send(SignTextData *signtext, JsonNode *root);
+
 void
-message_send(SignTextData *signtext, JsonNode *root);
+message_send_error(SignTextInstance *instance, gchar *error);
+
+void
+message_send_boolean_response(SignTextInstance *instance, gboolean response);
+
+void
+message_send_string_response(SignTextInstance *instance, gchar *response);
 
 #endif
 

Modified: redwax-signtext/trunk/src/linux/signtext.c
==============================================================================
--- redwax-signtext/trunk/src/linux/signtext.c	(original)
+++ redwax-signtext/trunk/src/linux/signtext.c	Mon Sep 12 11:21:24 2022
@@ -131,10 +131,14 @@
 
 void signtext_instance_free(SignTextInstance *instance)
 {
+  g_printerr("signtext_instance_free\n");
   if (instance) {
     if (instance->uuid) g_free(instance->uuid);
+  g_printerr("signtext_instance_free: 1\n");
     if (instance->uri) g_uri_unref(instance->uri);
+  g_printerr("signtext_instance_free: 2\n");
 //    if (instance->label) g_object_unref(instance->label);
+  g_printerr("signtext_instance_free: 3\n");
     if (instance->label_text) g_free(instance->label_text);
 //    if (instance->textview) g_object_unref(instance->textview);
 //    if (instance->label_agree) g_object_unref(instance->label_agree);
@@ -144,7 +148,9 @@
 //    if (instance->cancel) g_object_unref(instance->cancel);
 //    if (instance->sign) g_object_unref(instance->sign);
     if (instance->box) g_object_unref(instance->box);
+  g_printerr("signtext_instance_free:4\n");
     if (instance->certificate) g_object_unref(instance->certificate);
+  g_printerr("signtext_instance_free: 5\n");
 
     if (instance->w) {
       ksba_writer_release(instance->w);

Modified: redwax-signtext/trunk/src/linux/signtext.h
==============================================================================
--- redwax-signtext/trunk/src/linux/signtext.h	(original)
+++ redwax-signtext/trunk/src/linux/signtext.h	Mon Sep 12 11:21:24 2022
@@ -104,8 +104,12 @@
   ksba_writer_t w;
   ksba_stop_reason_t stopreason;
 
-  gchar *payload;
+  guchar *payload;
   gsize payload_len;
+
+  gint state;
+  gint save;
+  gboolean done;
 } SignTextInstance;
 
 SignTextData *signtext_data_new();



More information about the rs-commit mailing list