diff options
-rw-r--r-- | Userland/Applications/CertificateSettings/CertificateStore.cpp | 34 | ||||
-rw-r--r-- | Userland/Applications/CertificateSettings/CertificateStore.h | 3 |
2 files changed, 37 insertions, 0 deletions
diff --git a/Userland/Applications/CertificateSettings/CertificateStore.cpp b/Userland/Applications/CertificateSettings/CertificateStore.cpp index a036954c8a..0dc78a3f7d 100644 --- a/Userland/Applications/CertificateSettings/CertificateStore.cpp +++ b/Userland/Applications/CertificateSettings/CertificateStore.cpp @@ -106,6 +106,28 @@ ErrorOr<void> CertificateStoreWidget::import_pem() return {}; } +Certificate CertificateStoreModel::get(int index) +{ + return m_certificates.at(index); +} + +ErrorOr<void> CertificateStoreWidget::export_pem() +{ + auto index = m_root_ca_tableview->selection().first().row(); + auto cert = m_root_ca_model->get(index); + + auto filename = cert.subject.subject.is_empty() ? cert.subject.unit : cert.subject.subject; + auto file = FileSystemAccessClient::Client::the().save_file(window(), filename.replace(" "sv, "_"sv), "pem"sv); + if (file.is_error()) + return {}; + + auto data = TRY(Crypto::encode_pem(cert.original_asn1)); + + TRY(file.release_value().release_stream()->write_until_depleted(data)); + + return {}; +} + ErrorOr<NonnullRefPtr<CertificateStoreWidget>> CertificateStoreWidget::try_create() { auto widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) CertificateStoreWidget())); @@ -127,6 +149,10 @@ ErrorOr<void> CertificateStoreWidget::initialize() m_root_ca_tableview->set_column_width(CertificateStoreModel::Column::IssuedTo, 150); m_root_ca_tableview->set_column_width(CertificateStoreModel::Column::IssuedBy, 150); + m_root_ca_tableview->on_selection_change = [&]() { + m_export_ca_button->set_enabled(m_root_ca_tableview->selection().size() == 1); + }; + m_import_ca_button = find_descendant_of_type_named<GUI::Button>("import_button"); m_import_ca_button->on_click = [&](auto) { auto import_result = import_pem(); @@ -135,6 +161,14 @@ ErrorOr<void> CertificateStoreWidget::initialize() } }; + m_export_ca_button = find_descendant_of_type_named<GUI::Button>("export_button"); + m_export_ca_button->on_click = [&](auto) { + auto export_result = export_pem(); + if (export_result.is_error()) { + GUI::MessageBox::show_error(window(), DeprecatedString::formatted("{}", export_result.release_error())); + } + }; + return {}; } } diff --git a/Userland/Applications/CertificateSettings/CertificateStore.h b/Userland/Applications/CertificateSettings/CertificateStore.h index 8369b00cb2..1812d370c0 100644 --- a/Userland/Applications/CertificateSettings/CertificateStore.h +++ b/Userland/Applications/CertificateSettings/CertificateStore.h @@ -31,6 +31,7 @@ public: virtual GUI::Variant data(GUI::ModelIndex const&, GUI::ModelRole) const override; virtual ErrorOr<void> load(); virtual ErrorOr<size_t> add(Vector<Certificate> const&); + virtual Certificate get(int index); private: Vector<Certificate> m_certificates; @@ -47,9 +48,11 @@ private: CertificateStoreWidget() = default; ErrorOr<void> initialize(); ErrorOr<void> import_pem(); + ErrorOr<void> export_pem(); RefPtr<CertificateStoreModel> m_root_ca_model; RefPtr<GUI::TableView> m_root_ca_tableview; RefPtr<GUI::Button> m_import_ca_button; + RefPtr<GUI::Button> m_export_ca_button; }; } |