Pass the PKCS11 keyid as chunk, not as string
[strongswan.git] / src / libstrongswan / plugins / openssl / openssl_rsa_private_key.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Copyright (C) 2008 Tobias Brunner
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
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 MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "openssl_rsa_private_key.h"
18 #include "openssl_rsa_public_key.h"
19
20 #include <debug.h>
21
22 #include <openssl/evp.h>
23 #include <openssl/rsa.h>
24 #ifndef OPENSSL_NO_ENGINE
25 #include <openssl/engine.h>
26 #endif /* OPENSSL_NO_ENGINE */
27
28 /**
29 * Public exponent to use for key generation.
30 */
31 #define PUBLIC_EXPONENT 0x10001
32
33 typedef struct private_openssl_rsa_private_key_t private_openssl_rsa_private_key_t;
34
35 /**
36 * Private data of a openssl_rsa_private_key_t object.
37 */
38 struct private_openssl_rsa_private_key_t {
39 /**
40 * Public interface for this signer.
41 */
42 openssl_rsa_private_key_t public;
43
44 /**
45 * RSA object from OpenSSL
46 */
47 RSA *rsa;
48
49 /**
50 * TRUE if the key is from an OpenSSL ENGINE and might not be readable
51 */
52 bool engine;
53
54 /**
55 * reference count
56 */
57 refcount_t ref;
58 };
59
60 /* implemented in rsa public key */
61 bool openssl_rsa_fingerprint(RSA *rsa, cred_encoding_type_t type, chunk_t *fp);
62
63 /**
64 * Build an EMPSA PKCS1 signature described in PKCS#1
65 */
66 static bool build_emsa_pkcs1_signature(private_openssl_rsa_private_key_t *this,
67 int type, chunk_t data, chunk_t *sig)
68 {
69 bool success = FALSE;
70
71 *sig = chunk_alloc(RSA_size(this->rsa));
72
73 if (type == NID_undef)
74 {
75 if (RSA_private_encrypt(data.len, data.ptr, sig->ptr, this->rsa,
76 RSA_PKCS1_PADDING) == sig->len)
77 {
78 success = TRUE;
79 }
80 }
81 else
82 {
83 EVP_MD_CTX *ctx;
84 EVP_PKEY *key;
85 const EVP_MD *hasher;
86 u_int len;
87
88 hasher = EVP_get_digestbynid(type);
89 if (!hasher)
90 {
91 return FALSE;
92 }
93
94 ctx = EVP_MD_CTX_create();
95 key = EVP_PKEY_new();
96 if (!ctx || !key)
97 {
98 goto error;
99 }
100 if (!EVP_PKEY_set1_RSA(key, this->rsa))
101 {
102 goto error;
103 }
104 if (!EVP_SignInit_ex(ctx, hasher, NULL))
105 {
106 goto error;
107 }
108 if (!EVP_SignUpdate(ctx, data.ptr, data.len))
109 {
110 goto error;
111 }
112 if (EVP_SignFinal(ctx, sig->ptr, &len, key))
113 {
114 success = TRUE;
115 }
116
117 error:
118 if (key)
119 {
120 EVP_PKEY_free(key);
121 }
122 if (ctx)
123 {
124 EVP_MD_CTX_destroy(ctx);
125 }
126 }
127 if (!success)
128 {
129 free(sig->ptr);
130 }
131 return success;
132 }
133
134 /**
135 * Implementation of openssl_rsa_private_key.get_type.
136 */
137 static key_type_t get_type(private_openssl_rsa_private_key_t *this)
138 {
139 return KEY_RSA;
140 }
141
142 /**
143 * Implementation of openssl_rsa_private_key.sign.
144 */
145 static bool sign(private_openssl_rsa_private_key_t *this, signature_scheme_t scheme,
146 chunk_t data, chunk_t *signature)
147 {
148 switch (scheme)
149 {
150 case SIGN_RSA_EMSA_PKCS1_NULL:
151 return build_emsa_pkcs1_signature(this, NID_undef, data, signature);
152 case SIGN_RSA_EMSA_PKCS1_SHA1:
153 return build_emsa_pkcs1_signature(this, NID_sha1, data, signature);
154 case SIGN_RSA_EMSA_PKCS1_SHA224:
155 return build_emsa_pkcs1_signature(this, NID_sha224, data, signature);
156 case SIGN_RSA_EMSA_PKCS1_SHA256:
157 return build_emsa_pkcs1_signature(this, NID_sha256, data, signature);
158 case SIGN_RSA_EMSA_PKCS1_SHA384:
159 return build_emsa_pkcs1_signature(this, NID_sha384, data, signature);
160 case SIGN_RSA_EMSA_PKCS1_SHA512:
161 return build_emsa_pkcs1_signature(this, NID_sha512, data, signature);
162 case SIGN_RSA_EMSA_PKCS1_MD5:
163 return build_emsa_pkcs1_signature(this, NID_md5, data, signature);
164 default:
165 DBG1(DBG_LIB, "signature scheme %N not supported in RSA",
166 signature_scheme_names, scheme);
167 return FALSE;
168 }
169 }
170
171 /**
172 * Implementation of openssl_rsa_private_key.decrypt.
173 */
174 static bool decrypt(private_openssl_rsa_private_key_t *this,
175 chunk_t crypto, chunk_t *plain)
176 {
177 DBG1(DBG_LIB, "RSA private key decryption not implemented");
178 return FALSE;
179 }
180
181 /**
182 * Implementation of openssl_rsa_private_key.get_keysize.
183 */
184 static size_t get_keysize(private_openssl_rsa_private_key_t *this)
185 {
186 return RSA_size(this->rsa);
187 }
188
189 /**
190 * Implementation of openssl_rsa_private_key.get_public_key.
191 */
192 static public_key_t* get_public_key(private_openssl_rsa_private_key_t *this)
193 {
194 chunk_t enc;
195 public_key_t *key;
196 u_char *p;
197
198 enc = chunk_alloc(i2d_RSAPublicKey(this->rsa, NULL));
199 p = enc.ptr;
200 i2d_RSAPublicKey(this->rsa, &p);
201 key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
202 BUILD_BLOB_ASN1_DER, enc, BUILD_END);
203 free(enc.ptr);
204 return key;
205 }
206
207 /**
208 * Implementation of public_key_t.get_fingerprint.
209 */
210 static bool get_fingerprint(private_openssl_rsa_private_key_t *this,
211 cred_encoding_type_t type, chunk_t *fingerprint)
212 {
213 return openssl_rsa_fingerprint(this->rsa, type, fingerprint);
214 }
215
216 /*
217 * Implementation of public_key_t.get_encoding.
218 */
219 static bool get_encoding(private_openssl_rsa_private_key_t *this,
220 cred_encoding_type_t type, chunk_t *encoding)
221 {
222 u_char *p;
223
224 if (this->engine)
225 {
226 return FALSE;
227 }
228 switch (type)
229 {
230 case PRIVKEY_ASN1_DER:
231 case PRIVKEY_PEM:
232 {
233 bool success = TRUE;
234
235 *encoding = chunk_alloc(i2d_RSAPrivateKey(this->rsa, NULL));
236 p = encoding->ptr;
237 i2d_RSAPrivateKey(this->rsa, &p);
238
239 if (type == PRIVKEY_PEM)
240 {
241 chunk_t asn1_encoding = *encoding;
242
243 success = lib->encoding->encode(lib->encoding, PRIVKEY_PEM,
244 NULL, encoding, CRED_PART_RSA_PRIV_ASN1_DER,
245 asn1_encoding, CRED_PART_END);
246 chunk_clear(&asn1_encoding);
247 }
248 return success;
249 }
250 default:
251 return FALSE;
252 }
253 }
254
255 /**
256 * Implementation of openssl_rsa_private_key.get_ref.
257 */
258 static private_openssl_rsa_private_key_t* get_ref(private_openssl_rsa_private_key_t *this)
259 {
260 ref_get(&this->ref);
261 return this;
262 }
263
264 /**
265 * Implementation of openssl_rsa_private_key.destroy.
266 */
267 static void destroy(private_openssl_rsa_private_key_t *this)
268 {
269 if (ref_put(&this->ref))
270 {
271 if (this->rsa)
272 {
273 lib->encoding->clear_cache(lib->encoding, this->rsa);
274 RSA_free(this->rsa);
275 }
276 free(this);
277 }
278 }
279
280 /**
281 * Internal generic constructor
282 */
283 static private_openssl_rsa_private_key_t *create_empty(void)
284 {
285 private_openssl_rsa_private_key_t *this = malloc_thing(private_openssl_rsa_private_key_t);
286
287 this->public.interface.get_type = (key_type_t (*) (private_key_t*))get_type;
288 this->public.interface.sign = (bool (*) (private_key_t*, signature_scheme_t, chunk_t, chunk_t*))sign;
289 this->public.interface.decrypt = (bool (*) (private_key_t*, chunk_t, chunk_t*))decrypt;
290 this->public.interface.get_keysize = (size_t (*) (private_key_t*))get_keysize;
291 this->public.interface.get_public_key = (public_key_t* (*) (private_key_t*))get_public_key;
292 this->public.interface.equals = private_key_equals;
293 this->public.interface.belongs_to = private_key_belongs_to;
294 this->public.interface.get_fingerprint = (bool(*)(private_key_t*, cred_encoding_type_t type, chunk_t *fp))get_fingerprint;
295 this->public.interface.has_fingerprint = (bool(*)(private_key_t*, chunk_t fp))private_key_has_fingerprint;
296 this->public.interface.get_encoding = (bool(*)(private_key_t*, cred_encoding_type_t type, chunk_t *encoding))get_encoding;
297 this->public.interface.get_ref = (private_key_t* (*) (private_key_t*))get_ref;
298 this->public.interface.destroy = (void (*) (private_key_t*))destroy;
299
300 this->engine = FALSE;
301 this->ref = 1;
302
303 return this;
304 }
305
306 /**
307 * See header.
308 */
309 openssl_rsa_private_key_t *openssl_rsa_private_key_gen(key_type_t type,
310 va_list args)
311 {
312 private_openssl_rsa_private_key_t *this;
313 u_int key_size = 0;
314 RSA *rsa = NULL;
315 BIGNUM *e = NULL;
316
317 while (TRUE)
318 {
319 switch (va_arg(args, builder_part_t))
320 {
321 case BUILD_KEY_SIZE:
322 key_size = va_arg(args, u_int);
323 continue;
324 case BUILD_END:
325 break;
326 default:
327 return NULL;
328 }
329 break;
330 }
331 if (!key_size)
332 {
333 return NULL;
334 }
335 e = BN_new();
336 if (!e || !BN_set_word(e, PUBLIC_EXPONENT))
337 {
338 goto error;
339 }
340 rsa = RSA_new();
341 if (!rsa || !RSA_generate_key_ex(rsa, key_size, e, NULL))
342 {
343 goto error;
344 }
345 this = create_empty();
346 this->rsa = rsa;
347 BN_free(e);
348 return &this->public;
349
350 error:
351 if (e)
352 {
353 BN_free(e);
354 }
355 if (rsa)
356 {
357 RSA_free(rsa);
358 }
359 return NULL;
360 }
361
362 /**
363 * See header
364 */
365 openssl_rsa_private_key_t *openssl_rsa_private_key_load(key_type_t type,
366 va_list args)
367 {
368 private_openssl_rsa_private_key_t *this;
369 chunk_t blob, n, e, d, p, q, exp1, exp2, coeff;
370
371 blob = n = e = d = p = q = exp1 = exp2 = coeff = chunk_empty;
372 while (TRUE)
373 {
374 switch (va_arg(args, builder_part_t))
375 {
376 case BUILD_BLOB_ASN1_DER:
377 blob = va_arg(args, chunk_t);
378 continue;
379 case BUILD_RSA_MODULUS:
380 n = va_arg(args, chunk_t);
381 continue;
382 case BUILD_RSA_PUB_EXP:
383 e = va_arg(args, chunk_t);
384 continue;
385 case BUILD_RSA_PRIV_EXP:
386 d = va_arg(args, chunk_t);
387 continue;
388 case BUILD_RSA_PRIME1:
389 p = va_arg(args, chunk_t);
390 continue;
391 case BUILD_RSA_PRIME2:
392 q = va_arg(args, chunk_t);
393 continue;
394 case BUILD_RSA_EXP1:
395 exp1 = va_arg(args, chunk_t);
396 continue;
397 case BUILD_RSA_EXP2:
398 exp2 = va_arg(args, chunk_t);
399 continue;
400 case BUILD_RSA_COEFF:
401 coeff = va_arg(args, chunk_t);
402 continue;
403 case BUILD_END:
404 break;
405 default:
406 return NULL;
407 }
408 break;
409 }
410
411 this = create_empty();
412 if (blob.ptr)
413 {
414 this->rsa = d2i_RSAPrivateKey(NULL, (const u_char**)&blob.ptr, blob.len);
415 if (this->rsa && RSA_check_key(this->rsa))
416 {
417 return &this->public;
418 }
419 }
420 else if (n.ptr && e.ptr && d.ptr && p.ptr && q.ptr && coeff.ptr)
421 {
422 this->rsa = RSA_new();
423 this->rsa->n = BN_bin2bn((const u_char*)n.ptr, n.len, NULL);
424 this->rsa->e = BN_bin2bn((const u_char*)e.ptr, e.len, NULL);
425 this->rsa->d = BN_bin2bn((const u_char*)d.ptr, d.len, NULL);
426 this->rsa->p = BN_bin2bn((const u_char*)p.ptr, p.len, NULL);
427 this->rsa->q = BN_bin2bn((const u_char*)q.ptr, q.len, NULL);
428 if (exp1.ptr)
429 {
430 this->rsa->dmp1 = BN_bin2bn((const u_char*)exp1.ptr, exp1.len, NULL);
431 }
432 if (exp2.ptr)
433 {
434 this->rsa->dmq1 = BN_bin2bn((const u_char*)exp2.ptr, exp2.len, NULL);
435 }
436 this->rsa->iqmp = BN_bin2bn((const u_char*)coeff.ptr, coeff.len, NULL);
437 if (RSA_check_key(this->rsa))
438 {
439 return &this->public;
440 }
441 }
442 destroy(this);
443 return NULL;
444 }
445
446 /**
447 * See header.
448 */
449 openssl_rsa_private_key_t *openssl_rsa_private_key_connect(key_type_t type,
450 va_list args)
451 {
452 #ifndef OPENSSL_NO_ENGINE
453 private_openssl_rsa_private_key_t *this;
454 char *engine_id = NULL;
455 char keyname[64], pin[32];;
456 chunk_t secret = chunk_empty, keyid = chunk_empty;;
457 EVP_PKEY *key;
458 ENGINE *engine;
459 int slot = -1;
460
461 while (TRUE)
462 {
463 switch (va_arg(args, builder_part_t))
464 {
465 case BUILD_PKCS11_KEYID:
466 keyid = va_arg(args, chunk_t);
467 continue;
468 case BUILD_PASSPHRASE:
469 secret = va_arg(args, chunk_t);
470 continue;
471 case BUILD_PKCS11_SLOT:
472 slot = va_arg(args, int);
473 continue;
474 case BUILD_PKCS11_MODULE:
475 engine_id = va_arg(args, char*);
476 continue;
477 case BUILD_END:
478 break;
479 default:
480 return NULL;
481 }
482 break;
483 }
484 if (!keyid.len || keyid.len > 40 || !secret.len)
485 {
486 return NULL;
487 }
488
489 memset(keyname, 0, sizeof(keyname));
490 if (slot != -1)
491 {
492 snprintf(keyname, sizeof(keyname), "%d:", slot);
493 }
494 if (sizeof(keyname) - strlen(keyname) <= keyid.len * 4 / 3 + 1)
495 {
496 return NULL;
497 }
498 chunk_to_hex(keyid, keyname + strlen(keyname), FALSE);
499
500 snprintf(pin, sizeof(pin), "%.*s", secret.len, secret.ptr);
501
502 if (!engine_id)
503 {
504 engine_id = lib->settings->get_str(lib->settings,
505 "libstrongswan.plugins.openssl.engine_id", "pkcs11");
506 }
507 engine = ENGINE_by_id(engine_id);
508 if (!engine)
509 {
510 DBG2(DBG_LIB, "engine '%s' is not available", engine_id);
511 return NULL;
512 }
513 if (!ENGINE_init(engine))
514 {
515 DBG1(DBG_LIB, "failed to initialize engine '%s'", engine_id);
516 ENGINE_free(engine);
517 return NULL;
518 }
519 if (!ENGINE_ctrl_cmd_string(engine, "PIN", pin, 0))
520 {
521 DBG1(DBG_LIB, "failed to set PIN on engine '%s'", engine_id);
522 ENGINE_free(engine);
523 return NULL;
524 }
525
526 key = ENGINE_load_private_key(engine, keyname, NULL, NULL);
527 if (!key)
528 {
529 DBG1(DBG_LIB, "failed to load private key with ID '%s' from "
530 "engine '%s'", keyname, engine_id);
531 ENGINE_free(engine);
532 return NULL;
533 }
534 ENGINE_free(engine);
535
536 this = create_empty();
537 this->rsa = EVP_PKEY_get1_RSA(key);
538 this->engine = TRUE;
539 if (!this->rsa)
540 {
541 destroy(this);
542 return NULL;
543 }
544
545 return &this->public;
546 #else /* OPENSSL_NO_ENGINE */
547 return NULL;
548 #endif /* OPENSSL_NO_ENGINE */
549 }
550