2 * Copyright (C) 2008 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
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>.
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
16 #include "openssl_rsa_private_key.h"
17 #include "openssl_rsa_public_key.h"
21 #include <openssl/evp.h>
22 #include <openssl/rsa.h>
23 #include <openssl/engine.h>
26 * Public exponent to use for key generation.
28 #define PUBLIC_EXPONENT 0x10001
30 typedef struct private_openssl_rsa_private_key_t private_openssl_rsa_private_key_t
;
33 * Private data of a openssl_rsa_private_key_t object.
35 struct private_openssl_rsa_private_key_t
{
37 * Public interface for this signer.
39 openssl_rsa_private_key_t
public;
42 * RSA object from OpenSSL
47 * TRUE if the key is from an OpenSSL ENGINE and might not be readable
52 * Keyid formed as a SHA-1 hash of a privateKey object
54 identification_t
* keyid
;
57 * Keyid formed as a SHA-1 hash of a privateKeyInfo object
59 identification_t
* keyid_info
;
68 * shared functions, implemented in openssl_rsa_public_key.c
70 bool openssl_rsa_public_key_build_id(RSA
*rsa
, identification_t
**keyid
,
71 identification_t
**keyid_info
);
74 openssl_rsa_public_key_t
*openssl_rsa_public_key_create_from_n_e(BIGNUM
*n
, BIGNUM
*e
);
78 * Build an EMPSA PKCS1 signature described in PKCS#1
80 static bool build_emsa_pkcs1_signature(private_openssl_rsa_private_key_t
*this,
81 int type
, chunk_t data
, chunk_t
*sig
)
85 *sig
= chunk_alloc(RSA_size(this->rsa
));
87 if (type
== NID_undef
)
89 if (RSA_private_encrypt(data
.len
, data
.ptr
, sig
->ptr
, this->rsa
,
90 RSA_PKCS1_PADDING
) == sig
->len
)
102 hasher
= EVP_get_digestbynid(type
);
108 ctx
= EVP_MD_CTX_create();
109 key
= EVP_PKEY_new();
114 if (!EVP_PKEY_set1_RSA(key
, this->rsa
))
118 if (!EVP_SignInit_ex(ctx
, hasher
, NULL
))
122 if (!EVP_SignUpdate(ctx
, data
.ptr
, data
.len
))
126 if (EVP_SignFinal(ctx
, sig
->ptr
, &len
, key
))
138 EVP_MD_CTX_destroy(ctx
);
149 * Implementation of openssl_rsa_private_key.get_type.
151 static key_type_t
get_type(private_openssl_rsa_private_key_t
*this)
157 * Implementation of openssl_rsa_private_key.sign.
159 static bool sign(private_openssl_rsa_private_key_t
*this, signature_scheme_t scheme
,
160 chunk_t data
, chunk_t
*signature
)
164 case SIGN_RSA_EMSA_PKCS1_NULL
:
165 return build_emsa_pkcs1_signature(this, NID_undef
, data
, signature
);
166 case SIGN_RSA_EMSA_PKCS1_SHA1
:
167 return build_emsa_pkcs1_signature(this, NID_sha1
, data
, signature
);
168 case SIGN_RSA_EMSA_PKCS1_SHA256
:
169 return build_emsa_pkcs1_signature(this, NID_sha256
, data
, signature
);
170 case SIGN_RSA_EMSA_PKCS1_SHA384
:
171 return build_emsa_pkcs1_signature(this, NID_sha384
, data
, signature
);
172 case SIGN_RSA_EMSA_PKCS1_SHA512
:
173 return build_emsa_pkcs1_signature(this, NID_sha512
, data
, signature
);
174 case SIGN_RSA_EMSA_PKCS1_MD5
:
175 return build_emsa_pkcs1_signature(this, NID_md5
, data
, signature
);
177 DBG1("signature scheme %N not supported in RSA",
178 signature_scheme_names
, scheme
);
184 * Implementation of openssl_rsa_private_key.decrypt.
186 static bool decrypt(private_openssl_rsa_private_key_t
*this,
187 chunk_t crypto
, chunk_t
*plain
)
189 DBG1("RSA private key decryption not implemented");
194 * Implementation of openssl_rsa_private_key.get_keysize.
196 static size_t get_keysize(private_openssl_rsa_private_key_t
*this)
198 return RSA_size(this->rsa
);
202 * Implementation of openssl_rsa_private_key.get_id.
204 static identification_t
* get_id(private_openssl_rsa_private_key_t
*this,
209 case ID_PUBKEY_INFO_SHA1
:
210 return this->keyid_info
;
219 * Implementation of openssl_rsa_private_key.get_public_key.
221 static openssl_rsa_public_key_t
* get_public_key(private_openssl_rsa_private_key_t
*this)
223 return openssl_rsa_public_key_create_from_n_e(this->rsa
->n
, this->rsa
->e
);
227 * Implementation of openssl_rsa_private_key.equals.
229 static bool equals(private_openssl_rsa_private_key_t
*this, private_key_t
*other
)
231 identification_t
*keyid
;
233 if (&this->public.interface
== other
)
237 if (other
->get_type(other
) != KEY_RSA
)
241 keyid
= other
->get_id(other
, ID_PUBKEY_SHA1
);
242 if (keyid
&& keyid
->equals(keyid
, this->keyid
))
246 keyid
= other
->get_id(other
, ID_PUBKEY_INFO_SHA1
);
247 if (keyid
&& keyid
->equals(keyid
, this->keyid_info
))
255 * Implementation of openssl_rsa_private_key.belongs_to.
257 static bool belongs_to(private_openssl_rsa_private_key_t
*this, public_key_t
*public)
259 identification_t
*keyid
;
261 if (public->get_type(public) != KEY_RSA
)
265 keyid
= public->get_id(public, ID_PUBKEY_SHA1
);
266 if (keyid
&& keyid
->equals(keyid
, this->keyid
))
270 keyid
= public->get_id(public, ID_PUBKEY_INFO_SHA1
);
271 if (keyid
&& keyid
->equals(keyid
, this->keyid_info
))
279 * Implementation of private_key_t.get_encoding.
281 static chunk_t
get_encoding(private_openssl_rsa_private_key_t
*this)
283 chunk_t enc
= chunk_empty
;
286 enc
= chunk_alloc(i2d_RSAPrivateKey(this->rsa
, NULL
));
288 i2d_RSAPrivateKey(this->rsa
, &p
);
294 * Implementation of openssl_rsa_private_key.get_ref.
296 static private_openssl_rsa_private_key_t
* get_ref(private_openssl_rsa_private_key_t
*this)
304 * Implementation of openssl_rsa_private_key.destroy.
306 static void destroy(private_openssl_rsa_private_key_t
*this)
308 if (ref_put(&this->ref
))
314 DESTROY_IF(this->keyid
);
315 DESTROY_IF(this->keyid_info
);
321 * Internal generic constructor
323 static private_openssl_rsa_private_key_t
*openssl_rsa_private_key_create_empty(void)
325 private_openssl_rsa_private_key_t
*this = malloc_thing(private_openssl_rsa_private_key_t
);
327 this->public.interface
.get_type
= (key_type_t (*) (private_key_t
*))get_type
;
328 this->public.interface
.sign
= (bool (*) (private_key_t
*, signature_scheme_t
, chunk_t
, chunk_t
*))sign
;
329 this->public.interface
.decrypt
= (bool (*) (private_key_t
*, chunk_t
, chunk_t
*))decrypt
;
330 this->public.interface
.get_keysize
= (size_t (*) (private_key_t
*))get_keysize
;
331 this->public.interface
.get_id
= (identification_t
* (*) (private_key_t
*, id_type_t
))get_id
;
332 this->public.interface
.get_public_key
= (public_key_t
* (*) (private_key_t
*))get_public_key
;
333 this->public.interface
.equals
= (bool (*) (private_key_t
*, private_key_t
*))equals
;
334 this->public.interface
.belongs_to
= (bool (*) (private_key_t
*, public_key_t
*))belongs_to
;
335 this->public.interface
.get_encoding
= (chunk_t(*) (private_key_t
*))get_encoding
;
336 this->public.interface
.get_ref
= (private_key_t
* (*) (private_key_t
*))get_ref
;
337 this->public.interface
.destroy
= (void (*) (private_key_t
*))destroy
;
339 this->engine
= FALSE
;
341 this->keyid_info
= NULL
;
348 * Generate an RSA key of specified key size
350 static openssl_rsa_private_key_t
*generate(size_t key_size
)
352 private_openssl_rsa_private_key_t
*this = openssl_rsa_private_key_create_empty();
354 this->rsa
= RSA_generate_key(key_size
, PUBLIC_EXPONENT
, NULL
, NULL
);
356 if (!openssl_rsa_public_key_build_id(this->rsa
, &this->keyid
, &this->keyid_info
))
362 return &this->public;
366 * load private key from an ASN1 encoded blob
368 static openssl_rsa_private_key_t
*load(chunk_t blob
)
370 u_char
*p
= blob
.ptr
;
371 private_openssl_rsa_private_key_t
*this = openssl_rsa_private_key_create_empty();
373 this->rsa
= d2i_RSAPrivateKey(NULL
, (const u_char
**)&p
, blob
.len
);
383 if (!openssl_rsa_public_key_build_id(this->rsa
, &this->keyid
, &this->keyid_info
))
389 if (!RSA_check_key(this->rsa
))
395 return &this->public;
399 * load private key from a smart card
401 static openssl_rsa_private_key_t
*load_from_smartcard(char *keyid
, char *pin
)
403 private_openssl_rsa_private_key_t
*this = NULL
;
405 char *engine_id
= lib
->settings
->get_str(lib
->settings
,
406 "library.plugins.openssl.engine_id", "pkcs11");
408 ENGINE
*engine
= ENGINE_by_id(engine_id
);
411 DBG1("engine '%s' is not available", engine_id
);
415 if (!ENGINE_init(engine
))
417 DBG1("failed to initialize engine '%s'", engine_id
);
421 if (!ENGINE_ctrl_cmd_string(engine
, "PIN", pin
, 0))
423 DBG1("failed to set PIN on engine '%s'", engine_id
);
427 key
= ENGINE_load_private_key(engine
, keyid
, NULL
, NULL
);
431 DBG1("failed to load private key with ID '%s' from engine '%s'", keyid
,
437 this = openssl_rsa_private_key_create_empty();
438 this->rsa
= EVP_PKEY_get1_RSA(key
);
441 if (!openssl_rsa_public_key_build_id(this->rsa
, &this->keyid
, &this->keyid_info
))
446 return &this->public;
453 typedef struct private_builder_t private_builder_t
;
455 * Builder implementation for key loading/generation
457 struct private_builder_t
{
458 /** implements the builder interface */
460 /** loaded/generated private key */
461 openssl_rsa_private_key_t
*key
;
462 /** temporary stored smartcard key ID */
464 /** temporary stored smartcard pin */
469 * Implementation of builder_t.build
471 static openssl_rsa_private_key_t
*build(private_builder_t
*this)
473 openssl_rsa_private_key_t
*key
= this->key
;
475 if (this->keyid
&& this->pin
)
477 key
= load_from_smartcard(this->keyid
, this->pin
);
484 * Implementation of builder_t.add
486 static void add(private_builder_t
*this, builder_part_t part
, ...)
495 case BUILD_BLOB_ASN1_DER
:
497 va_start(args
, part
);
498 chunk
= va_arg(args
, chunk_t
);
499 this->key
= load(chunk_clone(chunk
));
505 va_start(args
, part
);
506 this->key
= generate(va_arg(args
, u_int
));
510 case BUILD_SMARTCARD_KEYID
:
512 va_start(args
, part
);
513 this->keyid
= va_arg(args
, char*);
517 case BUILD_SMARTCARD_PIN
:
519 va_start(args
, part
);
520 this->pin
= va_arg(args
, char*);
530 destroy((private_openssl_rsa_private_key_t
*)this->key
);
532 builder_cancel(&this->public);
536 * Builder construction function
538 builder_t
*openssl_rsa_private_key_builder(key_type_t type
)
540 private_builder_t
*this;
547 this = malloc_thing(private_builder_t
);
550 this->public.add
= (void(*)(builder_t
*this, builder_part_t part
, ...))add
;
551 this->public.build
= (void*(*)(builder_t
*this))build
;
555 return &this->public;