81ab6cb31ac0ae62f4a87a5752c77305bdf89ac4
[strongswan.git] / src / libstrongswan / plugins / pkcs11 / pkcs11_public_key.c
1 /*
2 * Copyright (C) 2011 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * Copyright (C) 2010 Martin Willi
6 * Copyright (C) 2010 revosec AG
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #include "pkcs11_public_key.h"
20
21 #include "pkcs11.h"
22 #include "pkcs11_private_key.h"
23 #include "pkcs11_manager.h"
24
25 #include <debug.h>
26
27 typedef struct private_pkcs11_public_key_t private_pkcs11_public_key_t;
28
29 /**
30 * Private data of an pkcs11_public_key_t object.
31 */
32 struct private_pkcs11_public_key_t {
33
34 /**
35 * Public pkcs11_public_key_t interface.
36 */
37 pkcs11_public_key_t public;
38
39 /**
40 * Type of the key
41 */
42 key_type_t type;
43
44 /**
45 * Key size in bytes
46 */
47 size_t k;
48
49 /**
50 * PKCS#11 library this key uses
51 */
52 pkcs11_library_t *lib;
53
54 /**
55 * Slot the token is in
56 */
57 CK_SLOT_ID slot;
58
59 /**
60 * Session we use
61 */
62 CK_SESSION_HANDLE session;
63
64 /**
65 * Object handle to the key
66 */
67 CK_OBJECT_HANDLE object;
68
69 /**
70 * References to this key
71 */
72 refcount_t ref;
73 };
74
75 METHOD(public_key_t, get_type, key_type_t,
76 private_pkcs11_public_key_t *this)
77 {
78 return this->type;
79 }
80
81 METHOD(public_key_t, get_keysize, int,
82 private_pkcs11_public_key_t *this)
83 {
84 return this->k * 8;
85 }
86
87 METHOD(public_key_t, verify, bool,
88 private_pkcs11_public_key_t *this, signature_scheme_t scheme,
89 chunk_t data, chunk_t sig)
90 {
91 CK_MECHANISM_PTR mechanism;
92 CK_SESSION_HANDLE session;
93 CK_RV rv;
94
95 mechanism = pkcs11_signature_scheme_to_mech(scheme);
96 if (!mechanism)
97 {
98 DBG1(DBG_LIB, "signature scheme %N not supported",
99 signature_scheme_names, scheme);
100 return FALSE;
101 }
102 if (sig.len && sig.ptr[0] == 0)
103 { /* trim leading zero byte in sig */
104 sig = chunk_skip(sig, 1);
105 }
106 rv = this->lib->f->C_OpenSession(this->slot, CKF_SERIAL_SESSION, NULL, NULL,
107 &session);
108 if (rv != CKR_OK)
109 {
110 DBG1(DBG_CFG, "opening PKCS#11 session failed: %N", ck_rv_names, rv);
111 return FALSE;
112 }
113 rv = this->lib->f->C_VerifyInit(session, mechanism, this->object);
114 if (rv != CKR_OK)
115 {
116 this->lib->f->C_CloseSession(session);
117 DBG1(DBG_LIB, "C_VerifyInit() failed: %N", ck_rv_names, rv);
118 return FALSE;
119 }
120 rv = this->lib->f->C_Verify(session, data.ptr, data.len, sig.ptr, sig.len);
121 this->lib->f->C_CloseSession(session);
122 if (rv != CKR_OK)
123 {
124 DBG1(DBG_LIB, "C_Verify() failed: %N", ck_rv_names, rv);
125 return FALSE;
126 }
127 return TRUE;
128 }
129
130 METHOD(public_key_t, encrypt, bool,
131 private_pkcs11_public_key_t *this, encryption_scheme_t scheme,
132 chunk_t plain, chunk_t *crypt)
133 {
134 CK_MECHANISM_PTR mechanism;
135 CK_SESSION_HANDLE session;
136 CK_BYTE_PTR buf;
137 CK_ULONG len;
138 CK_RV rv;
139
140 mechanism = pkcs11_encryption_scheme_to_mech(scheme);
141 if (!mechanism)
142 {
143 DBG1(DBG_LIB, "encryption scheme %N not supported",
144 encryption_scheme_names, scheme);
145 return FALSE;
146 }
147 rv = this->lib->f->C_OpenSession(this->slot, CKF_SERIAL_SESSION, NULL, NULL,
148 &session);
149 if (rv != CKR_OK)
150 {
151 DBG1(DBG_CFG, "opening PKCS#11 session failed: %N", ck_rv_names, rv);
152 return FALSE;
153 }
154 rv = this->lib->f->C_EncryptInit(session, mechanism, this->object);
155 if (rv != CKR_OK)
156 {
157 this->lib->f->C_CloseSession(session);
158 DBG1(DBG_LIB, "C_EncryptInit() failed: %N", ck_rv_names, rv);
159 return FALSE;
160 }
161 len = (get_keysize(this) + 7) / 8;
162 buf = malloc(len);
163 rv = this->lib->f->C_Encrypt(session, plain.ptr, plain.len, buf, &len);
164 this->lib->f->C_CloseSession(session);
165 if (rv != CKR_OK)
166 {
167 DBG1(DBG_LIB, "C_Encrypt() failed: %N", ck_rv_names, rv);
168 free(buf);
169 return FALSE;
170 }
171 *crypt = chunk_create(buf, len);
172 return TRUE;
173 }
174
175 /**
176 * Encode RSA key using a given encoding type
177 */
178 static bool encode_rsa(private_pkcs11_public_key_t *this,
179 cred_encoding_type_t type, void *cache, chunk_t *encoding)
180 {
181 enumerator_t *enumerator;
182 bool success = FALSE;
183 CK_ATTRIBUTE attr[] = {
184 {CKA_MODULUS, NULL, 0},
185 {CKA_PUBLIC_EXPONENT, NULL, 0},
186 };
187
188 enumerator = this->lib->create_object_attr_enumerator(this->lib,
189 this->session, this->object, attr, countof(attr));
190 if (enumerator && enumerator->enumerate(enumerator, NULL) &&
191 attr[0].ulValueLen > 0 && attr[1].ulValueLen > 0)
192 {
193 chunk_t n, e;
194 n = chunk_create(attr[0].pValue, attr[0].ulValueLen);
195 if (n.ptr[0] & 0x80)
196 { /* add leading 0x00, encoders expect it already like this */
197 n = chunk_cata("cc", chunk_from_chars(0x00), n);
198 }
199 e = chunk_create(attr[1].pValue, attr[1].ulValueLen);
200 success = lib->encoding->encode(lib->encoding, type, cache, encoding,
201 CRED_PART_RSA_MODULUS, n, CRED_PART_RSA_PUB_EXP, e, CRED_PART_END);
202 }
203 DESTROY_IF(enumerator);
204 return success;
205 }
206
207 METHOD(public_key_t, get_encoding, bool,
208 private_pkcs11_public_key_t *this, cred_encoding_type_t type,
209 chunk_t *encoding)
210 {
211 switch (this->type)
212 {
213 case KEY_RSA:
214 return encode_rsa(this, type, NULL, encoding);
215 default:
216 return FALSE;
217 }
218 }
219
220 METHOD(public_key_t, get_fingerprint, bool,
221 private_pkcs11_public_key_t *this, cred_encoding_type_t type, chunk_t *fp)
222 {
223 if (lib->encoding->get_cache(lib->encoding, type, this, fp))
224 {
225 return TRUE;
226 }
227 switch (this->type)
228 {
229 case KEY_RSA:
230 return encode_rsa(this, type, this, fp);
231 default:
232 return FALSE;
233 }
234 }
235
236 METHOD(public_key_t, get_ref, public_key_t*,
237 private_pkcs11_public_key_t *this)
238 {
239 ref_get(&this->ref);
240 return &this->public.key;
241 }
242
243 METHOD(public_key_t, destroy, void,
244 private_pkcs11_public_key_t *this)
245 {
246 if (ref_put(&this->ref))
247 {
248 lib->encoding->clear_cache(lib->encoding, this);
249 this->lib->f->C_CloseSession(this->session);
250 free(this);
251 }
252 }
253
254 /**
255 * Create an empty PKCS#11 public key
256 */
257 static private_pkcs11_public_key_t *create(key_type_t type, size_t k,
258 pkcs11_library_t *p11, CK_SLOT_ID slot,
259 CK_SESSION_HANDLE session, CK_OBJECT_HANDLE object)
260 {
261 private_pkcs11_public_key_t *this;
262
263 INIT(this,
264 .public = {
265 .key = {
266 .get_type = _get_type,
267 .verify = _verify,
268 .encrypt = _encrypt,
269 .equals = public_key_equals,
270 .get_keysize = _get_keysize,
271 .get_fingerprint = _get_fingerprint,
272 .has_fingerprint = public_key_has_fingerprint,
273 .get_encoding = _get_encoding,
274 .get_ref = _get_ref,
275 .destroy = _destroy,
276 },
277 },
278 .type = type,
279 .k = k,
280 .lib = p11,
281 .slot = slot,
282 .session = session,
283 .object = object,
284 .ref = 1,
285 );
286
287 return this;
288 }
289
290 /**
291 * Find a key object, including PKCS11 library and slot
292 */
293 static private_pkcs11_public_key_t* find_rsa_key(chunk_t n, chunk_t e)
294 {
295 private_pkcs11_public_key_t *this = NULL;
296 pkcs11_manager_t *manager;
297 enumerator_t *enumerator, *keys;
298 pkcs11_library_t *p11;
299 CK_SLOT_ID slot;
300
301 manager = lib->get(lib, "pkcs11-manager");
302 if (!manager)
303 {
304 return NULL;
305 }
306
307 enumerator = manager->create_token_enumerator(manager);
308 while (enumerator->enumerate(enumerator, &p11, &slot))
309 {
310 CK_OBJECT_CLASS class = CKO_PUBLIC_KEY;
311 CK_KEY_TYPE type = CKK_RSA;
312 CK_ATTRIBUTE tmpl[] = {
313 {CKA_CLASS, &class, sizeof(class)},
314 {CKA_KEY_TYPE, &type, sizeof(type)},
315 {CKA_MODULUS, n.ptr, n.len},
316 {CKA_PUBLIC_EXPONENT, e.ptr, e.len},
317 };
318 CK_OBJECT_HANDLE object;
319 CK_SESSION_HANDLE session;
320 CK_RV rv;
321
322 rv = p11->f->C_OpenSession(slot, CKF_SERIAL_SESSION, NULL, NULL,
323 &session);
324 if (rv != CKR_OK)
325 {
326 DBG1(DBG_CFG, "opening PKCS#11 session failed: %N", ck_rv_names, rv);
327 continue;
328 }
329 keys = p11->create_object_enumerator(p11, session,
330 tmpl, countof(tmpl), NULL, 0);
331 if (keys->enumerate(keys, &object))
332 {
333 this = create(KEY_RSA, n.len, p11, slot, session, object);
334 keys->destroy(keys);
335 break;
336 }
337 keys->destroy(keys);
338 p11->f->C_CloseSession(session);
339 }
340 enumerator->destroy(enumerator);
341 return this;
342 }
343
344 /**
345 * Create a key object in a suitable token session
346 */
347 static private_pkcs11_public_key_t* create_rsa_key(chunk_t n, chunk_t e)
348 {
349 private_pkcs11_public_key_t *this = NULL;
350 pkcs11_manager_t *manager;
351 enumerator_t *enumerator, *mechs;
352 pkcs11_library_t *p11;
353 CK_SLOT_ID slot;
354
355 manager = lib->get(lib, "pkcs11-manager");
356 if (!manager)
357 {
358 return NULL;
359 }
360
361 enumerator = manager->create_token_enumerator(manager);
362 while (enumerator->enumerate(enumerator, &p11, &slot))
363 {
364 CK_MECHANISM_TYPE mech;
365 CK_MECHANISM_INFO info;
366 CK_OBJECT_CLASS class = CKO_PUBLIC_KEY;
367 CK_KEY_TYPE type = CKK_RSA;
368 CK_ATTRIBUTE tmpl[] = {
369 {CKA_CLASS, &class, sizeof(class)},
370 {CKA_KEY_TYPE, &type, sizeof(type)},
371 {CKA_MODULUS, n.ptr, n.len},
372 {CKA_PUBLIC_EXPONENT, e.ptr, e.len}
373 };
374 CK_OBJECT_HANDLE object;
375 CK_SESSION_HANDLE session;
376 CK_RV rv;
377
378 mechs = p11->create_mechanism_enumerator(p11, slot);
379 while (mechs->enumerate(mechs, &mech, &info))
380 {
381 if (!(info.flags & CKF_VERIFY))
382 {
383 continue;
384 }
385 switch (mech)
386 {
387 case CKM_RSA_PKCS:
388 case CKM_SHA1_RSA_PKCS:
389 case CKM_SHA256_RSA_PKCS:
390 case CKM_SHA384_RSA_PKCS:
391 case CKM_SHA512_RSA_PKCS:
392 case CKM_MD5_RSA_PKCS:
393 break;
394 default:
395 continue;
396 }
397 rv = p11->f->C_OpenSession(slot, CKF_SERIAL_SESSION, NULL, NULL,
398 &session);
399 if (rv != CKR_OK)
400 {
401 DBG1(DBG_CFG, "opening PKCS#11 session failed: %N",
402 ck_rv_names, rv);
403 continue;
404 }
405 rv = p11->f->C_CreateObject(session, tmpl, countof(tmpl), &object);
406 if (rv == CKR_OK)
407 {
408 this = create(KEY_RSA, n.len, p11, slot, session, object);
409 DBG2(DBG_CFG, "created RSA public key on token '%s':%d ",
410 p11->get_name(p11), slot);
411 break;
412 }
413 else
414 {
415 DBG1(DBG_CFG, "creating RSA public key on token '%s':%d "
416 "failed: %N", p11->get_name(p11), slot, ck_rv_names, rv);
417 p11->f->C_CloseSession(session);
418 }
419 }
420 mechs->destroy(mechs);
421 if (this)
422 {
423 break;
424 }
425 }
426 enumerator->destroy(enumerator);
427 return this;
428 }
429
430 /**
431 * See header
432 */
433 pkcs11_public_key_t *pkcs11_public_key_load(key_type_t type, va_list args)
434 {
435 private_pkcs11_public_key_t *this;
436 chunk_t n, e;
437
438 n = e = chunk_empty;
439 while (TRUE)
440 {
441 switch (va_arg(args, builder_part_t))
442 {
443 case BUILD_RSA_MODULUS:
444 n = va_arg(args, chunk_t);
445 continue;
446 case BUILD_RSA_PUB_EXP:
447 e = va_arg(args, chunk_t);
448 continue;
449 case BUILD_END:
450 break;
451 default:
452 return NULL;
453 }
454 break;
455 }
456 if (type == KEY_RSA && e.ptr && n.ptr)
457 {
458 if (n.len && n.ptr[0] == 0)
459 { /* trim leading zero byte in modulus */
460 n = chunk_skip(n, 1);
461 }
462 this = find_rsa_key(n, e);
463 if (this)
464 {
465 return &this->public;
466 }
467 this = create_rsa_key(n, e);
468 if (this)
469 {
470 return &this->public;
471 }
472 }
473 return NULL;
474 }
475