summaryrefslogtreecommitdiff
path: root/lib/pkcs11_int.c
blob: a2277b8823b89eb4f84bfe29461e1f87f12062ae (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * GnuTLS PKCS#11 support
 * Copyright (C) 2010-2012 Free Software Foundation, Inc.
 * Copyright (C) 2008, Joe Orton <joe@manyfish.co.uk>
 * 
 * Authors: Nikos Mavrogiannopoulos, Stef Walter
 *
 * Inspired and some parts (pkcs11_login) based on neon PKCS #11 support 
 * by Joe Orton. More ideas came from the pkcs11-helper library by 
 * Alon Bar-Lev.
 *
 * The GnuTLS 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 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 program.  If not, see <http://www.gnu.org/licenses/>
 */

#include "gnutls_int.h"
#include <gnutls/pkcs11.h>
#include <stdio.h>
#include <string.h>
#include "errors.h"
#include <datum.h>

#include <pin.h>
#include <pkcs11_int.h>
#include <p11-kit/p11-kit.h>
#include <p11-kit/pkcs11.h>
#include <p11-kit/pin.h>

ck_rv_t
pkcs11_get_slot_list(struct ck_function_list * module,
		     unsigned char token_present, ck_slot_id_t * slot_list,
		     unsigned long *count)
{
	return (module)->C_GetSlotList(token_present, slot_list, count);
}

ck_rv_t
pkcs11_get_module_info(struct ck_function_list * module,
		       struct ck_info * info)
{
	return (module)->C_GetInfo(info);
}

ck_rv_t
pkcs11_get_slot_info(struct ck_function_list * module,
		     ck_slot_id_t slot_id, struct ck_slot_info * info)
{
	return (module)->C_GetSlotInfo(slot_id, info);
}

ck_rv_t
pkcs11_get_token_info(struct ck_function_list * module,
		      ck_slot_id_t slot_id, struct ck_token_info * info)
{
	return (module)->C_GetTokenInfo(slot_id, info);
}

ck_rv_t
pkcs11_find_objects_init(struct ck_function_list * module,
			 ck_session_handle_t sess,
			 struct ck_attribute * templ, unsigned long count)
{
	return (module)->C_FindObjectsInit(sess, templ, count);
}

ck_rv_t
pkcs11_find_objects(struct ck_function_list * module,
		    ck_session_handle_t sess,
		    ck_object_handle_t * objects,
		    unsigned long max_object_count,
		    unsigned long *object_count)
{
	return (module)->C_FindObjects(sess, objects, max_object_count,
				       object_count);
}

ck_rv_t pkcs11_find_objects_final(struct pkcs11_session_info * sinfo)
{
	return (sinfo->module)->C_FindObjectsFinal(sinfo->pks);
}

ck_rv_t pkcs11_close_session(struct pkcs11_session_info * sinfo)
{
	sinfo->init = 0;
	return (sinfo->module)->C_CloseSession(sinfo->pks);
}

ck_rv_t
pkcs11_set_attribute_value(struct ck_function_list * module,
			   ck_session_handle_t sess,
			   ck_object_handle_t object,
			   struct ck_attribute * templ,
			   unsigned long count)
{
	return (module)->C_SetAttributeValue(sess, object, templ, count);
}

ck_rv_t
pkcs11_get_attribute_value(struct ck_function_list * module,
			   ck_session_handle_t sess,
			   ck_object_handle_t object,
			   struct ck_attribute * templ,
			   unsigned long count)
{
	return (module)->C_GetAttributeValue(sess, object, templ, count);
}

/* Returns only a single attribute value, but allocates its data 
 * Only the type needs to be set.
 */
ck_rv_t
pkcs11_get_attribute_avalue(struct ck_function_list * module,
			   ck_session_handle_t sess,
			   ck_object_handle_t object,
			   ck_attribute_type_t type,
			   gnutls_datum_t *res)
{
	ck_rv_t rv;
	struct ck_attribute templ;
	void *t;

	res->data = NULL;
	res->size = 0;

	templ.type = type;
	templ.value = NULL;
	templ.value_len = 0;
	rv = (module)->C_GetAttributeValue(sess, object, &templ, 1);
	if (rv == CKR_OK) {
		if (templ.value_len == 0)
			return rv;

		templ.type = type;
		t = gnutls_malloc(templ.value_len);
		if (t == NULL)
			return gnutls_assert_val(CKR_HOST_MEMORY);
		templ.value = t;
		rv = (module)->C_GetAttributeValue(sess, object, &templ, 1);
		if (rv != CKR_OK) {
			gnutls_free(t);
		}
		res->data = t;
		res->size = templ.value_len;
	}
	return rv;
}

ck_rv_t
pkcs11_get_mechanism_list(struct ck_function_list * module,
			  ck_slot_id_t slot_id,
			  ck_mechanism_type_t * mechanism_list,
			  unsigned long *count)
{
	return (module)->C_GetMechanismList(slot_id, mechanism_list,
					    count);
}

ck_rv_t
pkcs11_sign_init(struct ck_function_list * module,
		 ck_session_handle_t sess,
		 struct ck_mechanism * mechanism, ck_object_handle_t key)
{
	return (module)->C_SignInit(sess, mechanism, key);
}

ck_rv_t
pkcs11_sign(struct ck_function_list * module,
	    ck_session_handle_t sess,
	    unsigned char *data,
	    unsigned long data_len,
	    unsigned char *signature, unsigned long *signature_len)
{
	return (module)->C_Sign(sess, data, data_len, signature,
				signature_len);
}

ck_rv_t
pkcs11_generate_key_pair(struct ck_function_list * module,
			 ck_session_handle_t sess,
			 struct ck_mechanism * mechanism,
			 struct ck_attribute * pub_templ,
			 unsigned long pub_templ_count,
			 struct ck_attribute * priv_templ,
			 unsigned long priv_templ_count,
			 ck_object_handle_t * pub,
			 ck_object_handle_t * priv)
{
	return (module)->C_GenerateKeyPair(sess, mechanism, pub_templ,
					   pub_templ_count, priv_templ,
					   priv_templ_count, pub, priv);
}

ck_rv_t
pkcs11_decrypt_init(struct ck_function_list * module,
		    ck_session_handle_t sess,
		    struct ck_mechanism * mechanism,
		    ck_object_handle_t key)
{
	return (module)->C_DecryptInit(sess, mechanism, key);
}

ck_rv_t
pkcs11_decrypt(struct ck_function_list * module,
	       ck_session_handle_t sess,
	       unsigned char *encrypted_data,
	       unsigned long encrypted_data_len,
	       unsigned char *data, unsigned long *data_len)
{
	return (module)->C_Decrypt(sess, encrypted_data,
				   encrypted_data_len, data, data_len);
}

ck_rv_t
pkcs11_create_object(struct ck_function_list * module,
		     ck_session_handle_t sess,
		     struct ck_attribute * templ,
		     unsigned long count, ck_object_handle_t * object)
{
	return (module)->C_CreateObject(sess, templ, count, object);
}

ck_rv_t
pkcs11_destroy_object(struct ck_function_list * module,
		      ck_session_handle_t sess, ck_object_handle_t object)
{
	return (module)->C_DestroyObject(sess, object);
}

ck_rv_t
pkcs11_init_token(struct ck_function_list * module,
		  ck_slot_id_t slot_id, unsigned char *pin,
		  unsigned long pin_len, unsigned char *label)
{
	return (module)->C_InitToken(slot_id, pin, pin_len, label);
}

ck_rv_t
pkcs11_init_pin(struct ck_function_list * module,
		ck_session_handle_t sess,
		unsigned char *pin, unsigned long pin_len)
{
	return (module)->C_InitPIN(sess, pin, pin_len);
}

ck_rv_t
pkcs11_set_pin(struct ck_function_list * module,
	       ck_session_handle_t sess,
	       const char *old_pin,
	       unsigned long old_len,
	       const char *new_pin, unsigned long new_len)
{
	return (module)->C_SetPIN(sess, (uint8_t *) old_pin, old_len,
				  (uint8_t *) new_pin, new_len);
}

ck_rv_t
_gnutls_pkcs11_get_random(struct ck_function_list * module,
		  ck_session_handle_t sess, void *data, size_t len)
{
	return (module)->C_GenerateRandom(sess, data, len);
}

const char *pkcs11_strerror(ck_rv_t rv)
{
	return p11_kit_strerror(rv);
}