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