LCOV - code coverage report
Current view: top level - pkcs11/gnome2-store - gkm-gnome2-public-key.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 3.1 % 65 2
Test Date: 2024-05-07 18:02:03 Functions: 7.7 % 13 1

            Line data    Source code
       1              : /*
       2              :  * gnome-keyring
       3              :  *
       4              :  * Copyright (C) 2008 Stefan Walter
       5              :  *
       6              :  * This program is free software; you can redistribute it and/or modify
       7              :  * it under the terms of the GNU Lesser General Public License as
       8              :  * published by the Free Software Foundation; either version 2.1 of
       9              :  * the License, or (at your option) any later version.
      10              :  *
      11              :  * This program is distributed in the hope that it will be useful, but
      12              :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      13              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14              :  * Lesser General Public License for more details.
      15              :  *
      16              :  * You should have received a copy of the GNU Lesser General Public
      17              :  * License along with this program; if not, see
      18              :  * <http://www.gnu.org/licenses/>.
      19              :  */
      20              : 
      21              : #include "config.h"
      22              : 
      23              : #include "gkm-gnome2-public-key.h"
      24              : 
      25              : #include "gkm/gkm-attributes.h"
      26              : #include "gkm/gkm-data-der.h"
      27              : #include "gkm/gkm-factory.h"
      28              : #include "gkm/gkm-serializable.h"
      29              : #include "gkm/gkm-session.h"
      30              : #include "gkm/gkm-object.h"
      31              : #include "gkm/gkm-util.h"
      32              : 
      33              : #include <glib/gi18n.h>
      34              : 
      35              : struct _GkmGnome2PublicKey {
      36              :         GkmPublicXsaKey parent;
      37              : };
      38              : 
      39              : static void gkm_gnome2_public_key_serializable (GkmSerializableIface *iface);
      40              : 
      41            0 : G_DEFINE_TYPE_EXTENDED (GkmGnome2PublicKey, gkm_gnome2_public_key, GKM_TYPE_PUBLIC_XSA_KEY, 0,
      42              :                G_IMPLEMENT_INTERFACE (GKM_TYPE_SERIALIZABLE, gkm_gnome2_public_key_serializable));
      43              : 
      44              : /* -----------------------------------------------------------------------------
      45              :  * INTERNAL
      46              :  */
      47              : 
      48              : static GkmObject*
      49            0 : factory_create_public_key (GkmSession *session, GkmTransaction *transaction,
      50              :                            CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs)
      51              : {
      52            0 :         GkmObject *object = NULL;
      53              :         GkmSexp *sexp;
      54              : 
      55            0 :         g_return_val_if_fail (attrs || !n_attrs, NULL);
      56              : 
      57            0 :         sexp = gkm_public_xsa_key_create_sexp (session, transaction, attrs, n_attrs);
      58            0 :         if (sexp != NULL) {
      59            0 :                 object = g_object_new (GKM_TYPE_GNOME2_PUBLIC_KEY, "base-sexp", sexp,
      60              :                                        "module", gkm_session_get_module (session),
      61              :                                        "manager", gkm_manager_for_template (attrs, n_attrs, session),
      62              :                                        NULL);
      63            0 :                 gkm_sexp_unref (sexp);
      64            0 :                 gkm_session_complete_object_creation (session, transaction, object,
      65              :                                                       TRUE, attrs, n_attrs);
      66              :         }
      67              : 
      68            0 :         return object;
      69              : }
      70              : 
      71              : /* -----------------------------------------------------------------------------
      72              :  * OBJECT
      73              :  */
      74              : 
      75              : static void
      76            0 : gkm_gnome2_public_key_init (GkmGnome2PublicKey *self)
      77              : {
      78              : 
      79            0 : }
      80              : 
      81              : static void
      82            0 : gkm_gnome2_public_key_finalize (GObject *obj)
      83              : {
      84              :         /* GkmGnome2PublicKey *self = GKM_GNOME2_PUBLIC_KEY (obj); */
      85            0 :         G_OBJECT_CLASS (gkm_gnome2_public_key_parent_class)->finalize (obj);
      86            0 : }
      87              : 
      88              : static void
      89            0 : gkm_gnome2_public_key_set_property (GObject *obj, guint prop_id, const GValue *value,
      90              :                                   GParamSpec *pspec)
      91              : {
      92              :         switch (prop_id) {
      93              :         default:
      94            0 :                 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
      95            0 :                 break;
      96              :         }
      97            0 : }
      98              : 
      99              : static void
     100            0 : gkm_gnome2_public_key_get_property (GObject *obj, guint prop_id, GValue *value,
     101              :                                   GParamSpec *pspec)
     102              : {
     103              :         switch (prop_id) {
     104              :         default:
     105            0 :                 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
     106            0 :                 break;
     107              :         }
     108            0 : }
     109              : 
     110              : static void
     111            0 : gkm_gnome2_public_key_class_init (GkmGnome2PublicKeyClass *klass)
     112              : {
     113            0 :         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
     114              : 
     115            0 :         gobject_class->finalize = gkm_gnome2_public_key_finalize;
     116            0 :         gobject_class->set_property = gkm_gnome2_public_key_set_property;
     117            0 :         gobject_class->get_property = gkm_gnome2_public_key_get_property;
     118            0 : }
     119              : 
     120              : 
     121              : static gboolean
     122            0 : gkm_gnome2_public_key_real_load (GkmSerializable *base,
     123              :                                  GkmSecret *login,
     124              :                                  GBytes *data)
     125              : {
     126            0 :         GkmGnome2PublicKey *self = GKM_GNOME2_PUBLIC_KEY (base);
     127              :         GkmDataResult res;
     128              :         GkmSexp *wrapper;
     129              :         gcry_sexp_t sexp;
     130              : 
     131            0 :         if (g_bytes_get_size (data) == 0)
     132            0 :                 return FALSE;
     133              : 
     134            0 :         res = gkm_data_der_read_public_key (data, &sexp);
     135              : 
     136            0 :         switch (res) {
     137            0 :         case GKM_DATA_LOCKED:
     138            0 :                 g_message ("public key is locked");
     139            0 :                 return FALSE;
     140            0 :         case GKM_DATA_FAILURE:
     141            0 :                 g_message ("couldn't parse public key");
     142            0 :                 return FALSE;
     143            0 :         case GKM_DATA_UNRECOGNIZED:
     144            0 :                 g_message ("invalid or unrecognized public key");
     145            0 :                 return FALSE;
     146            0 :         case GKM_DATA_SUCCESS:
     147            0 :                 break;
     148            0 :         default:
     149            0 :                 g_assert_not_reached();
     150              :         }
     151              : 
     152            0 :         wrapper = gkm_sexp_new (sexp);
     153            0 :         gkm_sexp_key_set_base (GKM_SEXP_KEY (self), wrapper);
     154            0 :         gkm_sexp_unref (wrapper);
     155              : 
     156            0 :         return TRUE;
     157              : }
     158              : 
     159              : static GBytes *
     160            0 : gkm_gnome2_public_key_real_save (GkmSerializable *base, GkmSecret *login)
     161              : {
     162            0 :         GkmGnome2PublicKey *self = GKM_GNOME2_PUBLIC_KEY (base);
     163              :         GkmSexp *wrapper;
     164              : 
     165            0 :         g_return_val_if_fail (GKM_IS_GNOME2_PUBLIC_KEY (self), FALSE);
     166              : 
     167            0 :         wrapper = gkm_sexp_key_get_base (GKM_SEXP_KEY (self));
     168            0 :         g_return_val_if_fail (wrapper, FALSE);
     169              : 
     170            0 :         return gkm_data_der_write_public_key (gkm_sexp_get (wrapper));
     171              : }
     172              : 
     173              : static void
     174            0 : gkm_gnome2_public_key_serializable (GkmSerializableIface *iface)
     175              : {
     176            0 :         iface->extension = ".pub";
     177            0 :         iface->load = gkm_gnome2_public_key_real_load;
     178            0 :         iface->save = gkm_gnome2_public_key_real_save;
     179            0 : }
     180              : 
     181              : /* -----------------------------------------------------------------------------
     182              :  * PUBLIC
     183              :  */
     184              : 
     185              : GkmFactory*
     186           34 : gkm_gnome2_public_key_get_factory (void)
     187              : {
     188              :         static CK_OBJECT_CLASS klass = CKO_PUBLIC_KEY;
     189              :         static CK_BBOOL token = CK_TRUE;
     190              : 
     191              :         static CK_ATTRIBUTE attributes[] = {
     192              :                 { CKA_CLASS, &klass, sizeof (klass) },
     193              :                 { CKA_TOKEN, &token, sizeof (token) },
     194              :         };
     195              : 
     196              :         static GkmFactory factory = {
     197              :                 attributes,
     198              :                 G_N_ELEMENTS (attributes),
     199              :                 factory_create_public_key
     200              :         };
     201              : 
     202           34 :         return &factory;
     203              : }
        

Generated by: LCOV version 2.0-1