Support exporting certificates in PEM format
[moonshot-ui.git] / src / moonshot-identity-dialog.vala
index dc9142b..9c13fc7 100644 (file)
@@ -69,6 +69,9 @@ class IdentityDialog : Dialog
 
     private Label selected_item = null;
 
+    // Whether to clear the card's TrustAnchor after the user selects OK
+    internal bool clear_trust_anchor = false;
+
     public string display_name {
         get { return displayname_entry.get_text(); }
     }
@@ -175,11 +178,11 @@ class IdentityDialog : Dialog
         this.response.connect(on_response);
         content_area.set_border_width(6);
 
-        Widget trust_anchor_box = make_trust_anchor_box(card);
-        content_area.pack_start(trust_anchor_box, false, false, 15);
-
         if (!is_new_card)
         {
+            Widget trust_anchor_box = make_trust_anchor_box(card);
+            content_area.pack_start(trust_anchor_box, false, false, 15);
+
             var services_vbox = make_services_vbox();
             content_area.pack_start(services_vbox);
         }
@@ -199,7 +202,7 @@ class IdentityDialog : Dialog
         this.show_all();
     }
 
-    private static Widget make_trust_anchor_box(IdCard id)
+    private Widget make_trust_anchor_box(IdCard id)
     {
 
         Label ta_label = new Label(_("Trust anchor: ")
@@ -218,7 +221,11 @@ class IdentityDialog : Dialog
         int row = 0;
 
         var ta_clear_button = new Button.with_label(_("Clear Trust Anchor"));
-        ta_clear_button.clicked.connect((w) => {id.trust_anchor = new TrustAnchor();});
+        ta_clear_button.clicked.connect((w) => {
+                clear_trust_anchor = true;
+                ta_table.set_sensitive(false);
+            }
+            );
 
         ta_table.attach(ta_label, 0, 1, row, row + 1, opts, opts, 0, 0);
         ta_table.attach(ta_clear_button, 1, 2, row, row + 1, fill, fill, 0, 0);
@@ -239,7 +246,7 @@ class IdentityDialog : Dialog
             ca_cert_label.set_alignment(0, 0.5f);
             var export_button = new Button.with_label(_("Export Certificate"));
             //!!TODO!
-            export_button.clicked.connect((w) => {/* !!TODO! */});
+            export_button.clicked.connect((w) => {export_certificate(id);});
 
             ta_table.attach(ca_cert_label, 0, 1, row, row + 1, opts, opts, 20, 0);
             ta_table.attach(export_button, 1, 2, row, row + 1, fill, fill, 0, 0);
@@ -468,5 +475,42 @@ class IdentityDialog : Dialog
         return services_vbox;
     }
 
-
+    private void export_certificate(IdCard id) 
+    {
+        var dialog = new FileChooserDialog("Save File",
+                                           this,
+                                           FileChooserAction.SAVE,
+                                           _("Cancel"),ResponseType.CANCEL,
+                                           _("Save"), ResponseType.ACCEPT,
+                                           null);
+        dialog.set_do_overwrite_confirmation(true);
+//        dialog.set_current_folder(default_folder_for_saving);
+        //dialog.set_current_name("Untitled document");
+        if (dialog.run() == ResponseType.ACCEPT)
+        {
+            const string CERT_HEADER = "-----BEGIN CERTIFICATE-----\n";
+            const string CERT_FOOTER = "\n-----END CERTIFICATE-----\n";
+
+            // Normalize the certificate to PEM format:
+            // 1) Strip any embedded newlines in the certificate...
+            string cert = id.trust_anchor.ca_cert.replace("\n", "");
+
+            // 2), re-embed newlines every 64 chars.
+            string newcert = CERT_HEADER;
+            while (cert.length > 63) {
+                newcert += cert[0:63] + "\n";
+                cert = cert[63:cert.length];
+            }
+            if (cert.length > 0) {
+                newcert += cert;
+                newcert += CERT_FOOTER;
+            }
+
+            string filename = dialog.get_filename();
+            var file  = File.new_for_path(filename);
+            var stream = file.replace(null, false, FileCreateFlags.PRIVATE);
+            stream.write(newcert.data);
+        }
+        dialog.destroy();
+    }
 }