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 "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 24 : 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 10 : gkm_dh_key_real_get_attribute (GkmObject *base, GkmSession *session, CK_ATTRIBUTE* attr)
51 : {
52 10 : GkmDhKey *self = GKM_DH_KEY (base);
53 :
54 10 : switch (attr->type)
55 : {
56 :
57 2 : case CKA_KEY_TYPE:
58 2 : return gkm_attribute_set_ulong (attr, CKK_DH);
59 :
60 0 : case CKA_START_DATE:
61 : case CKA_END_DATE:
62 0 : return gkm_attribute_set_empty (attr);
63 :
64 0 : case CKA_LOCAL:
65 0 : return gkm_attribute_set_bool (attr, FALSE);
66 :
67 0 : case CKA_KEY_GEN_MECHANISM:
68 0 : return gkm_attribute_set_ulong (attr, CK_UNAVAILABLE_INFORMATION);
69 :
70 2 : case CKA_ALLOWED_MECHANISMS:
71 2 : return gkm_attribute_set_data (attr, (CK_VOID_PTR)GKM_DH_MECHANISMS,
72 : sizeof (GKM_DH_MECHANISMS));
73 :
74 6 : case CKA_ID:
75 6 : return gkm_attribute_set_data (attr, self->pv->id, self->pv->n_id);
76 :
77 0 : case CKA_SUBJECT:
78 0 : return gkm_attribute_set_empty (attr);
79 :
80 0 : case CKA_PRIME:
81 0 : return gkm_attribute_set_mpi (attr, self->pv->prime);
82 :
83 0 : case CKA_BASE:
84 0 : return gkm_attribute_set_mpi (attr, self->pv->base);
85 : };
86 :
87 0 : return GKM_OBJECT_CLASS (gkm_dh_key_parent_class)->get_attribute (base, session, attr);
88 : }
89 :
90 : static void
91 2 : gkm_dh_key_init (GkmDhKey *self)
92 : {
93 2 : self->pv = gkm_dh_key_get_instance_private (self);
94 2 : }
95 :
96 : static void
97 2 : gkm_dh_key_finalize (GObject *obj)
98 : {
99 2 : GkmDhKey *self = GKM_DH_KEY (obj);
100 :
101 2 : gcry_mpi_release (self->pv->prime);
102 2 : self->pv->prime = NULL;
103 :
104 2 : gcry_mpi_release (self->pv->base);
105 2 : self->pv->base = NULL;
106 :
107 2 : g_free (self->pv->id);
108 2 : self->pv->id = NULL;
109 2 : self->pv->n_id = 0;
110 :
111 2 : G_OBJECT_CLASS (gkm_dh_key_parent_class)->finalize (obj);
112 2 : }
113 :
114 : static void
115 1 : gkm_dh_key_class_init (GkmDhKeyClass *klass)
116 : {
117 1 : GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
118 1 : GkmObjectClass *gkm_class = GKM_OBJECT_CLASS (klass);
119 :
120 1 : gobject_class->finalize = gkm_dh_key_finalize;
121 :
122 1 : gkm_class->get_attribute = gkm_dh_key_real_get_attribute;
123 1 : }
124 :
125 : /* -----------------------------------------------------------------------------
126 : * PUBLIC
127 : */
128 :
129 : void
130 2 : gkm_dh_key_initialize (GkmDhKey *self, gcry_mpi_t prime, gcry_mpi_t base,
131 : gpointer id, gsize n_id)
132 : {
133 2 : g_return_if_fail (GKM_IS_DH_KEY (self));
134 2 : g_return_if_fail (base);
135 2 : g_return_if_fail (prime);
136 2 : g_return_if_fail (!self->pv->base);
137 2 : g_return_if_fail (!self->pv->prime);
138 :
139 2 : self->pv->base = base;
140 2 : self->pv->prime = prime;
141 2 : self->pv->id = id;
142 2 : self->pv->n_id = n_id;
143 : }
144 :
145 : gcry_mpi_t
146 1 : gkm_dh_key_get_prime (GkmDhKey *self)
147 : {
148 1 : g_return_val_if_fail (GKM_IS_DH_KEY (self), NULL);
149 1 : return self->pv->prime;
150 : }
|