pluto supports ECDSA authentication
[strongswan.git] / src / libstrongswan / plugins / gcrypt / gcrypt_rsa_private_key.c
1 /*
2 * Copyright (C) 2005-2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <gcrypt.h>
17
18 #include "gcrypt_rsa_private_key.h"
19
20 #include <debug.h>
21 #include <asn1/oid.h>
22 #include <asn1/asn1.h>
23 #include <asn1/asn1_parser.h>
24
25 typedef struct private_gcrypt_rsa_private_key_t private_gcrypt_rsa_private_key_t;
26
27 /**
28 * Private data of a gcrypt_rsa_private_key_t object.
29 */
30 struct private_gcrypt_rsa_private_key_t {
31
32 /**
33 * Public interface
34 */
35 gcrypt_rsa_private_key_t public;
36
37 /**
38 * gcrypt S-expression representing an RSA key
39 */
40 gcry_sexp_t key;
41
42 /**
43 * Keyid formed as a SHA-1 hash of a publicKey object
44 */
45 identification_t* keyid;
46
47 /**
48 * Keyid formed as a SHA-1 hash of a publicKeyInfo object
49 */
50 identification_t* keyid_info;
51
52 /**
53 * reference count
54 */
55 refcount_t ref;
56 };
57
58 /**
59 * Implemented in gcrypt_rsa_public_key.c
60 */
61 public_key_t *gcrypt_rsa_public_key_create_from_sexp(gcry_sexp_t key);
62
63 /**
64 * find a token in a S-expression
65 */
66 chunk_t gcrypt_rsa_find_token(gcry_sexp_t sexp, char *name)
67 {
68 gcry_sexp_t token;
69 chunk_t data = chunk_empty;
70
71 token = gcry_sexp_find_token(sexp, name, 1);
72 if (token)
73 {
74 data.ptr = (char*)gcry_sexp_nth_data(token, 1, &data.len);
75 if (!data.ptr)
76 {
77 data.len = 0;
78 }
79 data = chunk_clone(data);
80 gcry_sexp_release(token);
81 }
82 return data;
83 }
84
85 /**
86 * Implementation of gcrypt_rsa_private_key_t.build_emsa_pkcs1_signature.
87 */
88 static bool sign_pkcs1(private_gcrypt_rsa_private_key_t *this,
89 hash_algorithm_t hash_algorithm, char *hash_name,
90 chunk_t data, chunk_t *signature)
91 {
92 hasher_t *hasher;
93 chunk_t hash;
94 gcry_error_t err;
95 gcry_sexp_t in, out;
96 int hash_oid;
97
98 hash_oid = hasher_algorithm_to_oid(hash_algorithm);
99 if (hash_oid == OID_UNKNOWN)
100 {
101 return FALSE;
102 }
103 hasher = lib->crypto->create_hasher(lib->crypto, hash_algorithm);
104 if (!hasher)
105 {
106 return FALSE;
107 }
108 hasher->allocate_hash(hasher, data, &hash);
109 hasher->destroy(hasher);
110
111 err = gcry_sexp_build(&in, NULL, "(data(flags pkcs1)(hash %s %b))",
112 hash_name, hash.len, hash.ptr);
113 chunk_free(&hash);
114 if (err)
115 {
116 DBG1("building signature S-expression failed: %s", gpg_strerror(err));
117 return FALSE;
118 }
119 err = gcry_pk_sign(&out, in, this->key);
120 gcry_sexp_release(in);
121 if (err)
122 {
123 DBG1("creating pkcs1 signature failed: %s", gpg_strerror(err));
124 return FALSE;
125 }
126 *signature = gcrypt_rsa_find_token(out, "s");
127 gcry_sexp_release(out);
128 return !!signature->len;
129 }
130
131 /**
132 * Implementation of gcrypt_rsa_private_key.destroy.
133 */
134 static key_type_t get_type(private_gcrypt_rsa_private_key_t *this)
135 {
136 return KEY_RSA;
137 }
138
139 /**
140 * Implementation of gcrypt_rsa_private_key.destroy.
141 */
142 static bool sign(private_gcrypt_rsa_private_key_t *this, signature_scheme_t scheme,
143 chunk_t data, chunk_t *sig)
144 {
145 switch (scheme)
146 {
147 case SIGN_RSA_EMSA_PKCS1_SHA1:
148 return sign_pkcs1(this, HASH_SHA1, "sha1", data, sig);
149 case SIGN_RSA_EMSA_PKCS1_SHA256:
150 return sign_pkcs1(this, HASH_SHA256, "sha256", data, sig);
151 case SIGN_RSA_EMSA_PKCS1_SHA384:
152 return sign_pkcs1(this, HASH_SHA384, "sha384", data, sig);
153 case SIGN_RSA_EMSA_PKCS1_SHA512:
154 return sign_pkcs1(this, HASH_SHA512, "sha512", data, sig);
155 case SIGN_RSA_EMSA_PKCS1_MD5:
156 return sign_pkcs1(this, HASH_MD5, "md5", data, sig);
157 default:
158 DBG1("signature scheme %N not supported in RSA",
159 signature_scheme_names, scheme);
160 return FALSE;
161 }
162 }
163
164 /**
165 * Implementation of gcrypt_rsa_private_key.destroy.
166 */
167 static bool decrypt(private_gcrypt_rsa_private_key_t *this,
168 chunk_t crypto, chunk_t *plain)
169 {
170 DBG1("RSA private key decryption not implemented");
171 return FALSE;
172 }
173
174 /**
175 * Implementation of gcrypt_rsa_private_key.get_keysize.
176 */
177 static size_t get_keysize(private_gcrypt_rsa_private_key_t *this)
178 {
179 return gcry_pk_get_nbits(this->key) / 8;
180 }
181
182 /**
183 * Implementation of gcrypt_rsa_private_key.destroy.
184 */
185 static identification_t* get_id(private_gcrypt_rsa_private_key_t *this,
186 id_type_t type)
187 {
188 switch (type)
189 {
190 case ID_PUBKEY_INFO_SHA1:
191 return this->keyid_info;
192 case ID_PUBKEY_SHA1:
193 return this->keyid;
194 default:
195 return NULL;
196 }
197 }
198
199 /**
200 * Implementation of gcrypt_rsa_private_key.get_public_key.
201 */
202 static public_key_t* get_public_key(private_gcrypt_rsa_private_key_t *this)
203 {
204 return gcrypt_rsa_public_key_create_from_sexp(this->key);
205 }
206
207 /**
208 * Implementation of gcrypt_rsa_private_key.equals.
209 */
210 static bool equals(private_gcrypt_rsa_private_key_t *this, private_key_t *other)
211 {
212 identification_t *keyid;
213
214 if (&this->public.interface == other)
215 {
216 return TRUE;
217 }
218 if (other->get_type(other) != KEY_RSA)
219 {
220 return FALSE;
221 }
222 keyid = other->get_id(other, ID_PUBKEY_SHA1);
223 if (keyid && keyid->equals(keyid, this->keyid))
224 {
225 return TRUE;
226 }
227 keyid = other->get_id(other, ID_PUBKEY_INFO_SHA1);
228 if (keyid && keyid->equals(keyid, this->keyid_info))
229 {
230 return TRUE;
231 }
232 return FALSE;
233 }
234
235 /**
236 * Implementation of gcrypt_rsa_private_key.belongs_to.
237 */
238 static bool belongs_to(private_gcrypt_rsa_private_key_t *this,
239 public_key_t *public)
240 {
241 identification_t *keyid;
242
243 if (public->get_type(public) != KEY_RSA)
244 {
245 return FALSE;
246 }
247 keyid = public->get_id(public, ID_PUBKEY_SHA1);
248 if (keyid && keyid->equals(keyid, this->keyid))
249 {
250 return TRUE;
251 }
252 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
253 if (keyid && keyid->equals(keyid, this->keyid_info))
254 {
255 return TRUE;
256 }
257 return FALSE;
258 }
259
260 /**
261 * Implementation of private_key_t.get_encoding.
262 */
263 static chunk_t get_encoding(private_gcrypt_rsa_private_key_t *this)
264 {
265 chunk_t cp, cq, cd, cexp1 = chunk_empty, cexp2 = chunk_empty;
266 gcry_mpi_t p = NULL, q = NULL, d = NULL, exp1, exp2;
267 gcry_error_t err;
268
269 /* p and q are swapped, gcrypt expects p < q */
270 cp = gcrypt_rsa_find_token(this->key, "q");
271 cq = gcrypt_rsa_find_token(this->key, "p");
272 cd = gcrypt_rsa_find_token(this->key, "d");
273
274 err = gcry_mpi_scan(&p, GCRYMPI_FMT_USG, cp.ptr, cp.len, NULL)
275 | gcry_mpi_scan(&q, GCRYMPI_FMT_USG, cq.ptr, cq.len, NULL)
276 | gcry_mpi_scan(&d, GCRYMPI_FMT_USG, cd.ptr, cd.len, NULL);
277 if (err)
278 {
279 gcry_mpi_release(p);
280 gcry_mpi_release(q);
281 gcry_mpi_release(d);
282 chunk_clear(&cp);
283 chunk_clear(&cq);
284 chunk_clear(&cd);
285 DBG1("scanning mpi for export failed: %s", gpg_strerror(err));
286 return chunk_empty;
287 }
288
289 gcry_mpi_sub_ui(p, p, 1);
290 exp1 = gcry_mpi_new(gcry_pk_get_nbits(this->key));
291 gcry_mpi_mod(exp1, d, p);
292 gcry_mpi_release(p);
293
294 gcry_mpi_sub_ui(q, q, 1);
295 exp2 = gcry_mpi_new(gcry_pk_get_nbits(this->key));
296 gcry_mpi_mod(exp1, d, q);
297 gcry_mpi_release(q);
298
299 err = gcry_mpi_aprint(GCRYMPI_FMT_USG, &cexp1.ptr, &cexp1.len, exp1)
300 | gcry_mpi_aprint(GCRYMPI_FMT_USG, &cexp2.ptr, &cexp2.len, exp2);
301
302 gcry_mpi_release(d);
303 gcry_mpi_release(exp1);
304 gcry_mpi_release(exp2);
305
306 if (err)
307 {
308 DBG1("printing mpi for export failed: %s", gpg_strerror(err));
309 chunk_clear(&cp);
310 chunk_clear(&cq);
311 chunk_clear(&cd);
312 chunk_clear(&cexp1);
313 chunk_clear(&cexp2);
314 return chunk_empty;
315 }
316
317 return asn1_wrap(ASN1_SEQUENCE, "cmmmmmmmm", ASN1_INTEGER_0,
318 asn1_integer("m", gcrypt_rsa_find_token(this->key, "n")),
319 asn1_integer("m", gcrypt_rsa_find_token(this->key, "e")),
320 asn1_integer("m", cd),
321 asn1_integer("m", cp),
322 asn1_integer("m", cq),
323 asn1_integer("m", cexp1),
324 asn1_integer("m", cexp2),
325 asn1_integer("m", gcrypt_rsa_find_token(this->key, "u")));
326 }
327
328 /**
329 * Implementation of gcrypt_rsa_private_key.get_ref.
330 */
331 static private_key_t* get_ref(private_gcrypt_rsa_private_key_t *this)
332 {
333 ref_get(&this->ref);
334 return &this->public.interface;
335 }
336
337 /**
338 * Implementation of gcrypt_rsa_private_key.destroy.
339 */
340 static void destroy(private_gcrypt_rsa_private_key_t *this)
341 {
342 if (ref_put(&this->ref))
343 {
344 DESTROY_IF(this->keyid);
345 DESTROY_IF(this->keyid_info);
346 gcry_sexp_release(this->key);
347 free(this);
348 }
349 }
350
351 /**
352 * Internal generic constructor
353 */
354 static private_gcrypt_rsa_private_key_t *gcrypt_rsa_private_key_create_empty()
355 {
356 private_gcrypt_rsa_private_key_t *this = malloc_thing(private_gcrypt_rsa_private_key_t);
357
358 this->public.interface.get_type = (key_type_t (*)(private_key_t *this))get_type;
359 this->public.interface.sign = (bool (*)(private_key_t *this, signature_scheme_t scheme, chunk_t data, chunk_t *signature))sign;
360 this->public.interface.decrypt = (bool (*)(private_key_t *this, chunk_t crypto, chunk_t *plain))decrypt;
361 this->public.interface.get_keysize = (size_t (*) (private_key_t *this))get_keysize;
362 this->public.interface.get_id = (identification_t* (*) (private_key_t *this,id_type_t))get_id;
363 this->public.interface.get_public_key = (public_key_t* (*)(private_key_t *this))get_public_key;
364 this->public.interface.equals = (bool (*) (private_key_t*, private_key_t*))equals;
365 this->public.interface.belongs_to = (bool (*) (private_key_t *this, public_key_t *public))belongs_to;
366 this->public.interface.get_encoding = (chunk_t(*)(private_key_t*))get_encoding;
367 this->public.interface.get_ref = (private_key_t* (*)(private_key_t *this))get_ref;
368 this->public.interface.destroy = (void (*)(private_key_t *this))destroy;
369
370 this->key = NULL;
371 this->keyid = NULL;
372 this->keyid_info = NULL;
373 this->ref = 1;
374
375 return this;
376 }
377
378 /**
379 * build the keyids of a private/public key
380 */
381 bool gcrypt_rsa_build_keyids(gcry_sexp_t key, identification_t **keyid,
382 identification_t **keyid_info)
383 {
384 chunk_t publicKeyInfo, publicKey, hash;
385 hasher_t *hasher;
386
387 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
388 if (!hasher)
389 {
390 DBG1("SHA1 hash algorithm not supported, unable to use RSA");
391 return FALSE;
392 }
393 publicKey = asn1_wrap(ASN1_SEQUENCE, "mm",
394 asn1_integer("m", gcrypt_rsa_find_token(key, "n")),
395 asn1_integer("m", gcrypt_rsa_find_token(key, "e")));
396 hasher->allocate_hash(hasher, publicKey, &hash);
397 *keyid = identification_create_from_encoding(ID_PUBKEY_SHA1, hash);
398 chunk_free(&hash);
399
400 publicKeyInfo = asn1_wrap(ASN1_SEQUENCE, "cm",
401 asn1_algorithmIdentifier(OID_RSA_ENCRYPTION),
402 asn1_bitstring("m", publicKey));
403 hasher->allocate_hash(hasher, publicKeyInfo, &hash);
404 *keyid_info = identification_create_from_encoding(ID_PUBKEY_INFO_SHA1, hash);
405 chunk_free(&hash);
406
407 hasher->destroy(hasher);
408 chunk_free(&publicKeyInfo);
409
410 return TRUE;
411 }
412
413 /**
414 * Generate an RSA key of specified key size
415 */
416 static gcrypt_rsa_private_key_t *generate(size_t key_size)
417 {
418 private_gcrypt_rsa_private_key_t *this;
419 gcry_sexp_t param, key;
420 gcry_error_t err;
421
422 err = gcry_sexp_build(&param, NULL, "(genkey(rsa(nbits %d)))", key_size);
423 if (err)
424 {
425 DBG1("building S-expression failed: %s", gpg_strerror(err));
426 return NULL;
427 }
428
429 err = gcry_pk_genkey(&key, param);
430 gcry_sexp_release(param);
431 if (err)
432 {
433 DBG1("generating RSA key failed: %s", gpg_strerror(err));
434 return NULL;
435 }
436 this = gcrypt_rsa_private_key_create_empty();
437 this->key = key;
438
439 if (!gcrypt_rsa_build_keyids(this->key, &this->keyid, &this->keyid_info))
440 {
441 destroy(this);
442 return NULL;
443 }
444
445 return &this->public;
446 }
447
448 /**
449 * ASN.1 definition of a PKCS#1 RSA private key
450 */
451 static const asn1Object_t privkeyObjects[] = {
452 { 0, "RSAPrivateKey", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
453 { 1, "version", ASN1_INTEGER, ASN1_BODY }, /* 1 */
454 { 1, "modulus", ASN1_INTEGER, ASN1_BODY }, /* 2 */
455 { 1, "publicExponent", ASN1_INTEGER, ASN1_BODY }, /* 3 */
456 { 1, "privateExponent", ASN1_INTEGER, ASN1_BODY }, /* 4 */
457 { 1, "prime1", ASN1_INTEGER, ASN1_BODY }, /* 5 */
458 { 1, "prime2", ASN1_INTEGER, ASN1_BODY }, /* 6 */
459 { 1, "exponent1", ASN1_INTEGER, ASN1_BODY }, /* 7 */
460 { 1, "exponent2", ASN1_INTEGER, ASN1_BODY }, /* 8 */
461 { 1, "coefficient", ASN1_INTEGER, ASN1_BODY }, /* 9 */
462 { 1, "otherPrimeInfos", ASN1_SEQUENCE, ASN1_OPT |
463 ASN1_LOOP }, /* 10 */
464 { 2, "otherPrimeInfo", ASN1_SEQUENCE, ASN1_NONE }, /* 11 */
465 { 3, "prime", ASN1_INTEGER, ASN1_BODY }, /* 12 */
466 { 3, "exponent", ASN1_INTEGER, ASN1_BODY }, /* 13 */
467 { 3, "coefficient", ASN1_INTEGER, ASN1_BODY }, /* 14 */
468 { 1, "end opt or loop", ASN1_EOC, ASN1_END }, /* 15 */
469 { 0, "exit", ASN1_EOC, ASN1_EXIT }
470 };
471 #define PRIV_KEY_VERSION 1
472 #define PRIV_KEY_MODULUS 2
473 #define PRIV_KEY_PUB_EXP 3
474 #define PRIV_KEY_PRIV_EXP 4
475 #define PRIV_KEY_PRIME1 5
476 #define PRIV_KEY_PRIME2 6
477 #define PRIV_KEY_EXP1 7
478 #define PRIV_KEY_EXP2 8
479 #define PRIV_KEY_COEFF 9
480
481 /**
482 * load private key from a ASN1 encoded blob
483 */
484 static gcrypt_rsa_private_key_t *load(chunk_t blob)
485 {
486 private_gcrypt_rsa_private_key_t *this;
487 asn1_parser_t *parser;
488 chunk_t object;
489 int objectID ;
490 bool success = FALSE;
491 chunk_t n, e, d, u, p, q;
492 gcry_error_t err;
493
494 parser = asn1_parser_create(privkeyObjects, blob);
495 parser->set_flags(parser, FALSE, TRUE);
496
497 while (parser->iterate(parser, &objectID, &object))
498 {
499 switch (objectID)
500 {
501 case PRIV_KEY_VERSION:
502 if (object.len > 0 && *object.ptr != 0)
503 {
504 goto end;
505 }
506 break;
507 case PRIV_KEY_MODULUS:
508 n = object;
509 break;
510 case PRIV_KEY_PUB_EXP:
511 e = object;
512 break;
513 case PRIV_KEY_PRIV_EXP:
514 d = object;
515 break;
516 case PRIV_KEY_PRIME1:
517 /* p and q are swapped, as gcrypt expects p < q */
518 q = object;
519 break;
520 case PRIV_KEY_PRIME2:
521 p = object;
522 break;
523 case PRIV_KEY_EXP1:
524 case PRIV_KEY_EXP2:
525 break;
526 case PRIV_KEY_COEFF:
527 u = object;
528 break;
529 }
530 }
531 success = parser->success(parser);
532
533 end:
534 parser->destroy(parser);
535
536 if (!success)
537 {
538 return NULL;
539 }
540
541 this = gcrypt_rsa_private_key_create_empty();
542 err = gcry_sexp_build(&this->key, NULL,
543 "(private-key(rsa(n %b)(e %b)(d %b)(p %b)(q %b)(u %b)))",
544 n.len, n.ptr, e.len, e.ptr, d.len, d.ptr,
545 p.len, p.ptr, q.len, q.ptr, u.len, u.ptr);
546 if (err)
547 {
548 DBG1("loading private key failed: %s", gpg_strerror(err));
549 free(this);
550 return NULL;
551 }
552 err = gcry_pk_testkey(this->key);
553 if (err)
554 {
555 DBG1("private key sanity check failed: %s", gpg_strerror(err));
556 destroy(this);
557 return NULL;
558 }
559 if (!gcrypt_rsa_build_keyids(this->key, &this->keyid, &this->keyid_info))
560 {
561 destroy(this);
562 return NULL;
563 }
564 return &this->public;
565 }
566
567 typedef struct private_builder_t private_builder_t;
568
569 /**
570 * Builder implementation for key loading/generation
571 */
572 struct private_builder_t {
573 /** implements the builder interface */
574 builder_t public;
575 /** loaded/generated private key */
576 gcrypt_rsa_private_key_t *key;
577 };
578
579 /**
580 * Implementation of builder_t.build
581 */
582 static gcrypt_rsa_private_key_t *build(private_builder_t *this)
583 {
584 gcrypt_rsa_private_key_t *key = this->key;
585
586 free(this);
587 return key;
588 }
589
590 /**
591 * Implementation of builder_t.add
592 */
593 static void add(private_builder_t *this, builder_part_t part, ...)
594 {
595 if (!this->key)
596 {
597 va_list args;
598
599 switch (part)
600 {
601 case BUILD_BLOB_ASN1_DER:
602 {
603 va_start(args, part);
604 this->key = load(va_arg(args, chunk_t));
605 va_end(args);
606 return;
607 }
608 case BUILD_KEY_SIZE:
609 {
610 va_start(args, part);
611 this->key = generate(va_arg(args, u_int));
612 va_end(args);
613 return;
614 }
615 default:
616 break;
617 }
618 }
619 if (this->key)
620 {
621 destroy((private_gcrypt_rsa_private_key_t*)this->key);
622 }
623 builder_cancel(&this->public);
624 }
625
626 /**
627 * Builder construction function
628 */
629 builder_t *gcrypt_rsa_private_key_builder(key_type_t type)
630 {
631 private_builder_t *this;
632
633 if (type != KEY_RSA)
634 {
635 return NULL;
636 }
637
638 this = malloc_thing(private_builder_t);
639
640 this->key = NULL;
641 this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))add;
642 this->public.build = (void*(*)(builder_t *this))build;
643
644 return &this->public;
645 }
646