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