summaryrefslogtreecommitdiff
path: root/pkcs11/gkm/gkm-dh-key.c
blob: 9ecbaf5021bbeecb841bd603dc40e5878f428692 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * gnome-keyring
 *
 * Copyright (C) 2008 Stefan Walter
 *
 * This program 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.1 of
 * the License, or (at your option) any later version.
 *
 * This program 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 program; if not, see
 * <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "pkcs11/pkcs11.h"

#include "gkm-attributes.h"
#include "gkm-crypto.h"
#include "gkm-dh-key.h"
#include "gkm-dh-mechanism.h"
#include "gkm-session.h"
#include "gkm-util.h"

struct _GkmDhKeyPrivate {
	gcry_mpi_t prime;
	gcry_mpi_t base;
	gpointer id;
	gsize n_id;
};

G_DEFINE_TYPE_WITH_PRIVATE (GkmDhKey, gkm_dh_key, GKM_TYPE_OBJECT);

/* -----------------------------------------------------------------------------
 * INTERNAL
 */

/* -----------------------------------------------------------------------------
 * PUBLIC_DH_KEY
 */

static CK_RV
gkm_dh_key_real_get_attribute (GkmObject *base, GkmSession *session, CK_ATTRIBUTE* attr)
{
	GkmDhKey *self = GKM_DH_KEY (base);

	switch (attr->type)
	{

	case CKA_KEY_TYPE:
		return gkm_attribute_set_ulong (attr, CKK_DH);

	case CKA_START_DATE:
	case CKA_END_DATE:
		return gkm_attribute_set_empty (attr);

	case CKA_LOCAL:
		return gkm_attribute_set_bool (attr, FALSE);

	case CKA_KEY_GEN_MECHANISM:
		return gkm_attribute_set_ulong (attr, CK_UNAVAILABLE_INFORMATION);

	case CKA_ALLOWED_MECHANISMS:
		return gkm_attribute_set_data (attr, (CK_VOID_PTR)GKM_DH_MECHANISMS,
		                               sizeof (GKM_DH_MECHANISMS));

	case CKA_ID:
		return gkm_attribute_set_data (attr, self->pv->id, self->pv->n_id);

	case CKA_SUBJECT:
		return gkm_attribute_set_empty (attr);

	case CKA_PRIME:
		return gkm_attribute_set_mpi (attr, self->pv->prime);

	case CKA_BASE:
		return gkm_attribute_set_mpi (attr, self->pv->base);
	};

	return GKM_OBJECT_CLASS (gkm_dh_key_parent_class)->get_attribute (base, session, attr);
}

static void
gkm_dh_key_init (GkmDhKey *self)
{
	self->pv = gkm_dh_key_get_instance_private (self);
}

static void
gkm_dh_key_finalize (GObject *obj)
{
	GkmDhKey *self = GKM_DH_KEY (obj);

	gcry_mpi_release (self->pv->prime);
	self->pv->prime = NULL;

	gcry_mpi_release (self->pv->base);
	self->pv->base = NULL;

	g_free (self->pv->id);
	self->pv->id = NULL;
	self->pv->n_id = 0;

	G_OBJECT_CLASS (gkm_dh_key_parent_class)->finalize (obj);
}

static void
gkm_dh_key_class_init (GkmDhKeyClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
	GkmObjectClass *gkm_class = GKM_OBJECT_CLASS (klass);

	gobject_class->finalize = gkm_dh_key_finalize;

	gkm_class->get_attribute = gkm_dh_key_real_get_attribute;
}

/* -----------------------------------------------------------------------------
 * PUBLIC
 */

void
gkm_dh_key_initialize (GkmDhKey *self, gcry_mpi_t prime, gcry_mpi_t base,
                       gpointer id, gsize n_id)
{
	g_return_if_fail (GKM_IS_DH_KEY (self));
	g_return_if_fail (base);
	g_return_if_fail (prime);
	g_return_if_fail (!self->pv->base);
	g_return_if_fail (!self->pv->prime);

	self->pv->base = base;
	self->pv->prime = prime;
	self->pv->id = id;
	self->pv->n_id = n_id;
}

gcry_mpi_t
gkm_dh_key_get_prime (GkmDhKey *self)
{
	g_return_val_if_fail (GKM_IS_DH_KEY (self), NULL);
	return self->pv->prime;
}