779da6881ee6dddec58c754b2064a1c4dcb2af51
[strongswan.git] / src / libstrongswan / plugins / gmp / gmp_rsa_public_key.c
1 /*
2 * Copyright (C) 2005-2008 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
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 * $Id$
17 */
18
19 #include <gmp.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "gmp_rsa_public_key.h"
26
27 #include <debug.h>
28 #include <asn1/oid.h>
29 #include <asn1/asn1.h>
30 #include <asn1/asn1_parser.h>
31 #include <asn1/pem.h>
32 #include <crypto/hashers/hasher.h>
33
34 /**
35 * defined in gmp_rsa_private_key.c
36 */
37 extern chunk_t gmp_mpz_to_asn1(const mpz_t value);
38
39 typedef struct private_gmp_rsa_public_key_t private_gmp_rsa_public_key_t;
40
41 /**
42 * Private data structure with signing context.
43 */
44 struct private_gmp_rsa_public_key_t {
45 /**
46 * Public interface for this signer.
47 */
48 gmp_rsa_public_key_t public;
49
50 /**
51 * Public modulus.
52 */
53 mpz_t n;
54
55 /**
56 * Public exponent.
57 */
58 mpz_t e;
59
60 /**
61 * Keysize in bytes.
62 */
63 size_t k;
64
65 /**
66 * Keyid formed as a SHA-1 hash of a publicKeyInfo object
67 */
68 identification_t *keyid_info;
69
70 /**
71 * Keyid formed as a SHA-1 hash of a publicKey object
72 */
73 identification_t *keyid;
74
75 /**
76 * reference counter
77 */
78 refcount_t ref;
79 };
80
81 /**
82 * RSAEP algorithm specified in PKCS#1.
83 */
84 static chunk_t rsaep(private_gmp_rsa_public_key_t *this, chunk_t data)
85 {
86 mpz_t m, c;
87 chunk_t encrypted;
88
89 mpz_init(c);
90 mpz_init(m);
91
92 mpz_import(m, data.len, 1, 1, 1, 0, data.ptr);
93
94 mpz_powm(c, m, this->e, this->n);
95
96 encrypted.len = this->k;
97 encrypted.ptr = mpz_export(NULL, NULL, 1, encrypted.len, 1, 0, c);
98
99 mpz_clear(c);
100 mpz_clear(m);
101
102 return encrypted;
103 }
104
105 /**
106 * RSAVP1 algorithm specified in PKCS#1.
107 */
108 static chunk_t rsavp1(private_gmp_rsa_public_key_t *this, chunk_t data)
109 {
110 return rsaep(this, data);
111 }
112
113 /**
114 * ASN.1 definition of digestInfo
115 */
116 static const asn1Object_t digestInfoObjects[] = {
117 { 0, "digestInfo", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
118 { 1, "digestAlgorithm", ASN1_EOC, ASN1_RAW }, /* 1 */
119 { 1, "digest", ASN1_OCTET_STRING, ASN1_BODY }, /* 2 */
120 { 0, "exit", ASN1_EOC, ASN1_EXIT }
121 };
122 #define DIGEST_INFO 0
123 #define DIGEST_INFO_ALGORITHM 1
124 #define DIGEST_INFO_DIGEST 2
125
126 /**
127 * Verification of an EMPSA PKCS1 signature described in PKCS#1
128 */
129 static bool verify_emsa_pkcs1_signature(private_gmp_rsa_public_key_t *this,
130 hash_algorithm_t algorithm,
131 chunk_t data, chunk_t signature)
132 {
133 chunk_t em_ori, em;
134 bool success = FALSE;
135
136 /* remove any preceding 0-bytes from signature */
137 while (signature.len && *(signature.ptr) == 0x00)
138 {
139 signature.len -= 1;
140 signature.ptr++;
141 }
142
143 if (signature.len > this->k)
144 {
145 return INVALID_ARG;
146 }
147
148 /* unpack signature */
149 em_ori = em = rsavp1(this, signature);
150
151 /* result should look like this:
152 * EM = 0x00 || 0x01 || PS || 0x00 || T.
153 * PS = 0xFF padding, with length to fill em
154 * T = oid || hash
155 */
156
157 /* check magic bytes */
158 if (*(em.ptr) != 0x00 || *(em.ptr+1) != 0x01)
159 {
160 goto end;
161 }
162 em.ptr += 2;
163 em.len -= 2;
164
165 /* find magic 0x00 */
166 while (em.len > 0)
167 {
168 if (*em.ptr == 0x00)
169 {
170 /* found magic byte, stop */
171 em.ptr++;
172 em.len--;
173 break;
174 }
175 else if (*em.ptr != 0xFF)
176 {
177 /* bad padding, decryption failed ?!*/
178 goto end;
179 }
180 em.ptr++;
181 em.len--;
182 }
183
184 if (em.len == 0)
185 {
186 /* no digestInfo found */
187 goto end;
188 }
189
190 /* parse ASN.1-based digestInfo */
191 {
192 asn1_parser_t *parser;
193 chunk_t object;
194 int objectID;
195 hash_algorithm_t hash_algorithm = HASH_UNKNOWN;
196
197 parser = asn1_parser_create(digestInfoObjects, em);
198
199 while (parser->iterate(parser, &objectID, &object))
200 {
201 switch (objectID)
202 {
203 case DIGEST_INFO:
204 {
205 if (em.len > object.len)
206 {
207 DBG1("digestInfo field in signature is followed by %u surplus bytes",
208 em.len - object.len);
209 goto end_parser;
210 }
211 break;
212 }
213 case DIGEST_INFO_ALGORITHM:
214 {
215 int hash_oid = asn1_parse_algorithmIdentifier(object,
216 parser->get_level(parser)+1, NULL);
217
218 hash_algorithm = hasher_algorithm_from_oid(hash_oid);
219 if (hash_algorithm == HASH_UNKNOWN ||
220 (algorithm != HASH_UNKNOWN && hash_algorithm != algorithm))
221 {
222 DBG1("wrong hash algorithm used in signature");
223 goto end_parser;
224 }
225 break;
226 }
227 case DIGEST_INFO_DIGEST:
228 {
229 chunk_t hash;
230 hasher_t *hasher;
231
232 hasher = lib->crypto->create_hasher(lib->crypto, hash_algorithm);
233 if (hasher == NULL)
234 {
235 DBG1("hash algorithm %N not supported",
236 hash_algorithm_names, hash_algorithm);
237 goto end_parser;
238 }
239
240 if (object.len != hasher->get_hash_size(hasher))
241 {
242 DBG1("hash size in signature is %u bytes instead of %u "
243 "bytes", object.len, hasher->get_hash_size(hasher));
244 hasher->destroy(hasher);
245 goto end_parser;
246 }
247
248 /* build our own hash and compare */
249 hasher->allocate_hash(hasher, data, &hash);
250 hasher->destroy(hasher);
251 success = memeq(object.ptr, hash.ptr, hash.len);
252 free(hash.ptr);
253 break;
254 }
255 default:
256 break;
257 }
258 }
259
260 end_parser:
261 success &= parser->success(parser);
262 parser->destroy(parser);
263 }
264
265 end:
266 free(em_ori.ptr);
267 return success;
268 }
269
270 /**
271 * Implementation of public_key_t.get_type.
272 */
273 static key_type_t get_type(private_gmp_rsa_public_key_t *this)
274 {
275 return KEY_RSA;
276 }
277
278 /**
279 * Implementation of public_key_t.verify.
280 */
281 static bool verify(private_gmp_rsa_public_key_t *this, signature_scheme_t scheme,
282 chunk_t data, chunk_t signature)
283 {
284 switch (scheme)
285 {
286 case SIGN_DEFAULT: /* default is EMSA-PKCS1 using included OID */
287 return verify_emsa_pkcs1_signature(this, HASH_UNKNOWN, data, signature);
288 case SIGN_RSA_EMSA_PKCS1_MD5:
289 return verify_emsa_pkcs1_signature(this, HASH_MD5, data, signature);
290 case SIGN_RSA_EMSA_PKCS1_SHA1:
291 return verify_emsa_pkcs1_signature(this, HASH_SHA1, data, signature);
292 case SIGN_RSA_EMSA_PKCS1_SHA256:
293 return verify_emsa_pkcs1_signature(this, HASH_SHA256, data, signature);
294 case SIGN_RSA_EMSA_PKCS1_SHA384:
295 return verify_emsa_pkcs1_signature(this, HASH_SHA384, data, signature);
296 case SIGN_RSA_EMSA_PKCS1_SHA512:
297 return verify_emsa_pkcs1_signature(this, HASH_SHA512, data, signature);
298 default:
299 DBG1("signature scheme %N not supported in RSA",
300 signature_scheme_names, scheme);
301 return FALSE;
302 }
303 }
304
305 /**
306 * Implementation of public_key_t.get_keysize.
307 */
308 static bool encrypt(private_gmp_rsa_public_key_t *this, chunk_t crypto, chunk_t *plain)
309 {
310 DBG1("RSA public key encryption not implemented");
311 return FALSE;
312 }
313
314 /**
315 * Implementation of public_key_t.get_keysize.
316 */
317 static size_t get_keysize(private_gmp_rsa_public_key_t *this)
318 {
319 return this->k;
320 }
321
322 /**
323 * Implementation of public_key_t.get_id.
324 */
325 static identification_t *get_id(private_gmp_rsa_public_key_t *this,
326 id_type_t type)
327 {
328 switch (type)
329 {
330 case ID_PUBKEY_INFO_SHA1:
331 return this->keyid_info;
332 case ID_PUBKEY_SHA1:
333 return this->keyid;
334 default:
335 return NULL;
336 }
337 }
338
339 /*
340 * Implementation of public_key_t.get_encoding.
341 */
342 static chunk_t get_encoding(private_gmp_rsa_public_key_t *this)
343 {
344 return asn1_wrap(ASN1_SEQUENCE, "mm",
345 gmp_mpz_to_asn1(this->n),
346 gmp_mpz_to_asn1(this->e));
347 }
348
349 /**
350 * Implementation of public_key_t.get_ref.
351 */
352 static private_gmp_rsa_public_key_t* get_ref(private_gmp_rsa_public_key_t *this)
353 {
354 ref_get(&this->ref);
355 return this;
356 }
357
358 /**
359 * Implementation of gmp_rsa_public_key.destroy.
360 */
361 static void destroy(private_gmp_rsa_public_key_t *this)
362 {
363 if (ref_put(&this->ref))
364 {
365 mpz_clear(this->n);
366 mpz_clear(this->e);
367 DESTROY_IF(this->keyid);
368 DESTROY_IF(this->keyid_info);
369 free(this);
370 }
371 }
372
373 /**
374 * Generic private constructor
375 */
376 static private_gmp_rsa_public_key_t *gmp_rsa_public_key_create_empty()
377 {
378 private_gmp_rsa_public_key_t *this = malloc_thing(private_gmp_rsa_public_key_t);
379
380 this->public.interface.get_type = (key_type_t (*)(public_key_t *this))get_type;
381 this->public.interface.verify = (bool (*)(public_key_t *this, signature_scheme_t scheme, chunk_t data, chunk_t signature))verify;
382 this->public.interface.encrypt = (bool (*)(public_key_t *this, chunk_t crypto, chunk_t *plain))encrypt;
383 this->public.interface.get_keysize = (size_t (*) (public_key_t *this))get_keysize;
384 this->public.interface.get_id = (identification_t* (*) (public_key_t *this,id_type_t))get_id;
385 this->public.interface.get_encoding = (chunk_t(*)(public_key_t*))get_encoding;
386 this->public.interface.get_ref = (public_key_t* (*)(public_key_t *this))get_ref;
387 this->public.interface.destroy = (void (*)(public_key_t *this))destroy;
388
389 this->keyid = NULL;
390 this->keyid_info = NULL;
391 this->ref = 1;
392
393 return this;
394 }
395
396 /**
397 * Build the RSA key identifier from n and e using SHA1 hashed publicKey(Info).
398 * Also used in rsa_private_key.c.
399 */
400 bool gmp_rsa_public_key_build_id(mpz_t n, mpz_t e, identification_t **keyid,
401 identification_t **keyid_info)
402 {
403 chunk_t publicKeyInfo, publicKey, hash;
404 hasher_t *hasher;
405
406 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
407 if (hasher == NULL)
408 {
409 DBG1("SHA1 hash algorithm not supported, unable to use RSA");
410 return FALSE;
411 }
412 publicKey = asn1_wrap(ASN1_SEQUENCE, "mm",
413 gmp_mpz_to_asn1(n),
414 gmp_mpz_to_asn1(e));
415 hasher->allocate_hash(hasher, publicKey, &hash);
416 *keyid = identification_create_from_encoding(ID_PUBKEY_SHA1, hash);
417 chunk_free(&hash);
418
419 publicKeyInfo = asn1_wrap(ASN1_SEQUENCE, "cm",
420 asn1_algorithmIdentifier(OID_RSA_ENCRYPTION),
421 asn1_bitstring("m", publicKey));
422 hasher->allocate_hash(hasher, publicKeyInfo, &hash);
423 *keyid_info = identification_create_from_encoding(ID_PUBKEY_INFO_SHA1, hash);
424 chunk_free(&hash);
425
426 hasher->destroy(hasher);
427 chunk_free(&publicKeyInfo);
428
429 return TRUE;
430 }
431
432 /**
433 * Create a public key from mpz values, used in gmp_rsa_private_key
434 */
435 gmp_rsa_public_key_t *gmp_rsa_public_key_create_from_n_e(mpz_t n, mpz_t e)
436 {
437 private_gmp_rsa_public_key_t *this = gmp_rsa_public_key_create_empty();
438
439 mpz_init_set(this->n, n);
440 mpz_init_set(this->e, e);
441
442 this->k = (mpz_sizeinbase(this->n, 2) + 7) / 8;
443 if (!gmp_rsa_public_key_build_id(this->n, this->e,
444 &this->keyid, &this->keyid_info))
445 {
446 destroy(this);
447 return NULL;
448 }
449 return &this->public;
450 }
451
452 /**
453 * ASN.1 definition of RSApublicKey
454 */
455 static const asn1Object_t pubkeyObjects[] = {
456 { 0, "RSAPublicKey", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
457 { 1, "modulus", ASN1_INTEGER, ASN1_BODY }, /* 1 */
458 { 1, "publicExponent", ASN1_INTEGER, ASN1_BODY }, /* 2 */
459 { 0, "exit", ASN1_EOC, ASN1_EXIT }
460 };
461 #define PUB_KEY_RSA_PUBLIC_KEY 0
462 #define PUB_KEY_MODULUS 1
463 #define PUB_KEY_EXPONENT 2
464
465 /**
466 * Load a public key from an ASN1 encoded blob
467 */
468 static gmp_rsa_public_key_t *load(chunk_t blob)
469 {
470 asn1_parser_t *parser;
471 chunk_t object;
472 int objectID;
473 bool success = FALSE;
474
475 private_gmp_rsa_public_key_t *this = gmp_rsa_public_key_create_empty();
476
477 mpz_init(this->n);
478 mpz_init(this->e);
479
480 parser = asn1_parser_create(pubkeyObjects, blob);
481
482 while (parser->iterate(parser, &objectID, &object))
483 {
484 switch (objectID)
485 {
486 case PUB_KEY_MODULUS:
487 mpz_import(this->n, object.len, 1, 1, 1, 0, object.ptr);
488 break;
489 case PUB_KEY_EXPONENT:
490 mpz_import(this->e, object.len, 1, 1, 1, 0, object.ptr);
491 break;
492 }
493 }
494 success = parser->success(parser);
495 free(blob.ptr);
496 parser->destroy(parser);
497
498 if (!success)
499 {
500 destroy(this);
501 return NULL;
502 }
503
504 this->k = (mpz_sizeinbase(this->n, 2) + 7) / 8;
505
506 if (!gmp_rsa_public_key_build_id(this->n, this->e,
507 &this->keyid, &this->keyid_info))
508 {
509 destroy(this);
510 return NULL;
511 }
512 return &this->public;
513 }
514
515 typedef struct private_builder_t private_builder_t;
516 /**
517 * Builder implementation for key loading
518 */
519 struct private_builder_t {
520 /** implements the builder interface */
521 builder_t public;
522 /** loaded public key */
523 gmp_rsa_public_key_t *key;
524 };
525
526 /**
527 * Implementation of builder_t.build
528 */
529 static gmp_rsa_public_key_t *build(private_builder_t *this)
530 {
531 gmp_rsa_public_key_t *key = this->key;
532
533 free(this);
534 return key;
535 }
536
537 /**
538 * Implementation of builder_t.add
539 */
540 static void add(private_builder_t *this, builder_part_t part, ...)
541 {
542 va_list args;
543
544 if (this->key)
545 {
546 DBG1("ignoring surplus build part %N", builder_part_names, part);
547 return;
548 }
549
550 switch (part)
551 {
552 case BUILD_BLOB_ASN1_DER:
553 {
554 va_start(args, part);
555 this->key = load(va_arg(args, chunk_t));
556 va_end(args);
557 break;
558 }
559 default:
560 DBG1("ignoring unsupported build part %N", builder_part_names, part);
561 break;
562 }
563 }
564
565 /**
566 * Builder construction function
567 */
568 builder_t *gmp_rsa_public_key_builder(key_type_t type)
569 {
570 private_builder_t *this;
571
572 if (type != KEY_RSA)
573 {
574 return NULL;
575 }
576
577 this = malloc_thing(private_builder_t);
578
579 this->key = NULL;
580 this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))add;
581 this->public.build = (void*(*)(builder_t *this))build;
582
583 return &this->public;
584 }
585