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