Debian Codegraph @

pkcs11/gkm/gkm-dh-key.c

raw
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 "pkcs11/pkcs11.h"
24
25 #include "gkm-attributes.h"
26 #include "gkm-crypto.h"
27 #include "gkm-dh-key.h"
28 #include "gkm-dh-mechanism.h"
29 #include "gkm-session.h"
30 #include "gkm-util.h"
31
32 struct _GkmDhKeyPrivate {
33 gcry_mpi_t prime;
34 gcry_mpi_t base;
35 gpointer id;
36 gsize n_id;
37 };
38
39 G_DEFINE_TYPE_WITH_PRIVATE (GkmDhKey, gkm_dh_key, GKM_TYPE_OBJECT);
40
41 /* -----------------------------------------------------------------------------
42 * INTERNAL
43 */
44
45 /* -----------------------------------------------------------------------------
46 * PUBLIC_DH_KEY
47 */
48
49 static CK_RV
50 gkm_dh_key_real_get_attribute (GkmObject *base, GkmSession *session, CK_ATTRIBUTE* attr)
51 {
52 GkmDhKey *self = GKM_DH_KEY (base);
53
54 switch (attr->type)
55 {
56
57 case CKA_KEY_TYPE:
58 return gkm_attribute_set_ulong (attr, CKK_DH);
59
60 case CKA_START_DATE:
61 case CKA_END_DATE:
62 return gkm_attribute_set_empty (attr);
63
64 case CKA_LOCAL:
65 return gkm_attribute_set_bool (attr, FALSE);
66
67 case CKA_KEY_GEN_MECHANISM:
68 return gkm_attribute_set_ulong (attr, CK_UNAVAILABLE_INFORMATION);
69
70 case CKA_ALLOWED_MECHANISMS:
71 return gkm_attribute_set_data (attr, (CK_VOID_PTR)GKM_DH_MECHANISMS,
72 sizeof (GKM_DH_MECHANISMS));
73
74 case CKA_ID:
75 return gkm_attribute_set_data (attr, self->pv->id, self->pv->n_id);
76
77 case CKA_SUBJECT:
78 return gkm_attribute_set_empty (attr);
79
80 case CKA_PRIME:
81 return gkm_attribute_set_mpi (attr, self->pv->prime);
82
83 case CKA_BASE:
84 return gkm_attribute_set_mpi (attr, self->pv->base);
85 };
86
87 return GKM_OBJECT_CLASS (gkm_dh_key_parent_class)->get_attribute (base, session, attr);
88 }
89
90 static void
91 gkm_dh_key_init (GkmDhKey *self)
92 {
93 self->pv = gkm_dh_key_get_instance_private (self);
94 }
95
96 static void
97 gkm_dh_key_finalize (GObject *obj)
98 {
99 GkmDhKey *self = GKM_DH_KEY (obj);
100
101 gcry_mpi_release (self->pv->prime);
102 self->pv->prime = NULL;
103
104 gcry_mpi_release (self->pv->base);
105 self->pv->base = NULL;
106
107 g_free (self->pv->id);
108 self->pv->id = NULL;
109 self->pv->n_id = 0;
110
111 G_OBJECT_CLASS (gkm_dh_key_parent_class)->finalize (obj);
112 }
113
114 static void
115 gkm_dh_key_class_init (GkmDhKeyClass *klass)
116 {
117 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
118 GkmObjectClass *gkm_class = GKM_OBJECT_CLASS (klass);
119
120 gobject_class->finalize = gkm_dh_key_finalize;
121
122 gkm_class->get_attribute = gkm_dh_key_real_get_attribute;
123 }
124
125 /* -----------------------------------------------------------------------------
126 * PUBLIC
127 */
128
129 void
130 gkm_dh_key_initialize (GkmDhKey *self, gcry_mpi_t prime, gcry_mpi_t base,
131 gpointer id, gsize n_id)
132 {
133 g_return_if_fail (GKM_IS_DH_KEY (self));
134 g_return_if_fail (base);
135 g_return_if_fail (prime);
136 g_return_if_fail (!self->pv->base);
137 g_return_if_fail (!self->pv->prime);
138
139 self->pv->base = base;
140 self->pv->prime = prime;
141 self->pv->id = id;
142 self->pv->n_id = n_id;
143 }
144
145 gcry_mpi_t
146 gkm_dh_key_get_prime (GkmDhKey *self)
147 {
148 g_return_val_if_fail (GKM_IS_DH_KEY (self), NULL);
149 return self->pv->prime;
150 }