[network-manager-applet/lr/pkcs11: 8/9] WIP libnma: add a file certificate picker
- From: Lubomir Rintel <lkundrak src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [network-manager-applet/lr/pkcs11: 8/9] WIP libnma: add a file certificate picker
- Date: Mon, 20 Feb 2017 11:40:59 +0000 (UTC)
commit 880501adf6d15df2d9e1f530d0df4cf57f3a6bc7
Author: Lubomir Rintel <lkundrak v3 sk>
Date: Mon Feb 20 10:22:58 2017 +0000
WIP libnma: add a file certificate picker
Makefile.am | 6 +-
src/libnma/libnma.ver | 1 +
src/libnma/nma-cert-chooser.c | 6 +-
src/libnma/nma-cert-file-chooser.c | 186 ++++++++++++++++++++++++++++++++++++
src/libnma/nma-cert-file-chooser.h | 48 +++++++++
5 files changed, 244 insertions(+), 3 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 4056095..7e4f29f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -477,7 +477,8 @@ libnma_h_pub = \
src/libnma/nma-mobile-providers.h \
src/libnma/nma-vpn-password-dialog.h \
src/libnma/nma-ui-utils.h \
- src/libnma/nma-cert-chooser.h
+ src/libnma/nma-cert-chooser.h \
+ src/libnma/nma-cert-file-chooser.h
libnma_c_real = \
src/libnma/nma-wifi-dialog.c \
@@ -486,7 +487,8 @@ libnma_c_real = \
src/libnma/nma-vpn-password-dialog.c \
src/libnma/nma-ui-utils.c \
src/libnma/init.c \
- src/libnma/nma-cert-chooser.c
+ src/libnma/nma-cert-chooser.c \
+ src/libnma/nma-cert-file-chooser.c
src_libnma_libnmadir = $(includedir)/libnma
diff --git a/src/libnma/libnma.ver b/src/libnma/libnma.ver
index 1ccc946..f0268d3 100644
--- a/src/libnma/libnma.ver
+++ b/src/libnma/libnma.ver
@@ -78,4 +78,5 @@ global:
nma_cert_chooser_set_password;
nma_cert_chooser_get_password;
nma_cert_chooser_new;
+ nma_cert_file_chooser_get_type;
} libnma_1_2_0;
diff --git a/src/libnma/nma-cert-chooser.c b/src/libnma/nma-cert-chooser.c
index 5a5eaa8..79f0f59 100644
--- a/src/libnma/nma-cert-chooser.c
+++ b/src/libnma/nma-cert-chooser.c
@@ -28,6 +28,7 @@
#include <errno.h>
#include "nma-cert-chooser.h"
+#include "nma-cert-file-chooser.h"
G_DEFINE_INTERFACE (NMACertChooser, nma_cert_chooser, G_TYPE_OBJECT)
@@ -217,5 +218,8 @@ nma_cert_chooser_get_key_password (NMACertChooser *cert_chooser)
GtkWidget *
nma_cert_chooser_new (const gchar *title, NMACertChooserFlags flags)
{
- g_return_val_if_reached (NULL);
+ if (flags & NMA_CERT_CHOOSER_FLAG_PEM)
+ return nma_cert_file_chooser_new (title, flags);
+
+ return nma_cert_file_chooser_new (title, flags);
}
diff --git a/src/libnma/nma-cert-file-chooser.c b/src/libnma/nma-cert-file-chooser.c
new file mode 100644
index 0000000..471f5e5
--- /dev/null
+++ b/src/libnma/nma-cert-file-chooser.c
@@ -0,0 +1,186 @@
+/*
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA.
+ *
+ * Copyright (C) 2015,2017 Red Hat, Inc.
+ */
+
+#include <config.h>
+
+#include <glib/gi18n-lib.h>
+#include <glib/gstdio.h>
+#include <gtk/gtk.h>
+
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "nma-cert-file-chooser.h"
+
+void nma_cert_chooser_interface_init (NMACertChooserInterface *iface);
+
+typedef struct {
+ GtkWidget *key_button;
+ GtkWidget *cert_button;
+} NMACertFileChooserPrivate;
+
+#define NMA_CERT_FILE_CHOOSER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NMA_TYPE_CERT_FILE_CHOOSER,
NMACertFileChooserPrivate))
+
+G_DEFINE_TYPE_WITH_CODE (NMACertFileChooser, nma_cert_file_chooser, GTK_TYPE_BOX,
+ G_IMPLEMENT_INTERFACE (NMA_TYPE_CERT_CHOOSER,
+ nma_cert_chooser_interface_init))
+
+static const char *pem_cert_begin = "-----BEGIN CERTIFICATE-----";
+
+static const char *
+find_tag (const char *tag, const char *buf, gsize len)
+{
+ gsize i, taglen;
+
+ taglen = strlen (tag);
+ if (len < taglen)
+ return NULL;
+
+ for (i = 0; i < len - taglen + 1; i++) {
+ if (memcmp (buf + i, tag, taglen) == 0)
+ return buf + i;
+ }
+ return NULL;
+}
+
+static gboolean
+cert_filter (const GtkFileFilterInfo *filter_info, gpointer data)
+{
+ char *contents = NULL, *p, *ext;
+ gsize bytes_read = 0;
+ gboolean show = FALSE;
+ struct stat statbuf;
+
+ if (!filter_info->filename)
+ return FALSE;
+
+ p = strrchr (filter_info->filename, '.');
+ if (!p)
+ return FALSE;
+
+ ext = g_ascii_strdown (p, -1);
+ if (!ext)
+ return FALSE;
+
+ if (strcmp (ext, ".pem") && strcmp (ext, ".crt") && strcmp (ext, ".cer")) {
+ g_free (ext);
+ return FALSE;
+ }
+ g_free (ext);
+
+ /* Ignore files that are really large */
+ if (!stat (filter_info->filename, &statbuf)) {
+ if (statbuf.st_size > 500000)
+ return FALSE;
+ }
+
+ if (!g_file_get_contents (filter_info->filename, &contents, &bytes_read, NULL))
+ return FALSE;
+
+ if (bytes_read < 400) /* needs to be lower? */
+ goto out;
+
+ if (find_tag (pem_cert_begin, (const char *) contents, bytes_read)) {
+ show = TRUE;
+ goto out;
+ }
+
+out:
+ g_free (contents);
+ return show;
+}
+
+static void
+set_cert (NMACertChooser *cert_chooser, const gchar *value, NMSetting8021xCKScheme scheme)
+{
+ NMACertFileChooserPrivate *priv = NMA_CERT_FILE_CHOOSER_GET_PRIVATE (cert_chooser);
+
+ gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (priv->cert_button), value);
+}
+
+static gchar *
+get_cert (NMACertChooser *cert_chooser, NMSetting8021xCKScheme *scheme)
+{
+ NMACertFileChooserPrivate *priv = NMA_CERT_FILE_CHOOSER_GET_PRIVATE (cert_chooser);
+
+ return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (priv->cert_button));
+}
+
+static void
+set_key (NMACertChooser *cert_chooser, const gchar *value, NMSetting8021xCKScheme scheme)
+{
+ NMACertFileChooserPrivate *priv = NMA_CERT_FILE_CHOOSER_GET_PRIVATE (cert_chooser);
+
+ gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (priv->key_button), value);
+}
+
+static gchar *
+get_key (NMACertChooser *cert_chooser, NMSetting8021xCKScheme *scheme)
+{
+ NMACertFileChooserPrivate *priv = NMA_CERT_FILE_CHOOSER_GET_PRIVATE (cert_chooser);
+
+ return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (priv->key_button));
+}
+
+void
+nma_cert_chooser_interface_init (NMACertChooserInterface *iface)
+{
+ iface->set_cert = set_cert;
+ iface->get_cert = get_cert;
+ iface->set_key = set_key;
+ iface->get_key = get_key;
+}
+
+GtkWidget *
+nma_cert_file_chooser_new (const gchar *title, NMACertChooserFlags flags)
+{
+ GtkWidget *self;
+ NMACertFileChooserPrivate *priv;
+ GtkFileFilter *filter = NULL;
+
+ self = g_object_new (NMA_TYPE_CERT_CHOOSER, NULL);
+ priv = NMA_CERT_FILE_CHOOSER_GET_PRIVATE (self);
+
+ filter = gtk_file_filter_new ();
+ gtk_file_filter_add_custom (filter, GTK_FILE_FILTER_FILENAME, cert_filter, NULL, NULL);
+ gtk_file_filter_set_name (filter, _("PEM certificates (*.pem, *.crt, *.cer)"));
+
+ priv->cert_button = g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
+ "action", GTK_FILE_CHOOSER_ACTION_OPEN,
+ "title", title,
+ "filter", filter,
+ "local-only", TRUE,
+ NULL);
+
+ return self;
+}
+
+static void
+nma_cert_file_chooser_class_init (NMACertFileChooserClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ g_type_class_add_private (object_class, sizeof (NMACertFileChooserPrivate));
+}
+
+static void
+nma_cert_file_chooser_init (NMACertFileChooser *cert_file_chooser)
+{
+}
diff --git a/src/libnma/nma-cert-file-chooser.h b/src/libnma/nma-cert-file-chooser.h
new file mode 100644
index 0000000..ae3dd9c
--- /dev/null
+++ b/src/libnma/nma-cert-file-chooser.h
@@ -0,0 +1,48 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
+/*
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA.
+ *
+ * Copyright (C) 2015,2017 Red Hat, Inc.
+ */
+
+#ifndef NMA_CERT_FILE_CHOOSER_H
+#define NMA_CERT_FILE_CHOOSER_H
+
+#include <gtk/gtk.h>
+#include "nma-cert-chooser.h"
+
+G_BEGIN_DECLS
+
+#define NMA_TYPE_CERT_FILE_CHOOSER (nma_cert_file_chooser_get_type ())
+#define NMA_CERT_FILE_CHOOSER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj),
NMA_TYPE_CERT_FILE_CHOOSER, NMACertChooser))
+#define NMA_IS_CERT_FILE_CHOOSER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj),
NMA_TYPE_CERT_FILE_CHOOSER))
+#define NMA_CERT_FILE_CHOOSER_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj),
NMA_TYPE_CERT_FILE_CHOOSER, NMACertChooserInterface))
+
+typedef struct {
+ GtkBox parent;
+} NMACertFileChooser;
+
+typedef struct {
+ GtkBoxClass parent_class;
+} NMACertFileChooserClass;
+
+GType nma_cert_file_chooser_get_type (void);
+
+GtkWidget *nma_cert_file_chooser_new (const gchar *title, NMACertChooserFlags flags);
+
+G_END_DECLS
+
+#endif /* NMA_CERT_FILE_CHOOSER_H */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]