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
18 #include "openssl_rsa_private_key.h"
19 #include "openssl_rsa_public_key.h"
23 #include <openssl/evp.h>
24 #include <openssl/rsa.h>
25 #include <openssl/engine.h>
28 * Public exponent to use for key generation.
30 #define PUBLIC_EXPONENT 0x10001
32 typedef struct private_openssl_rsa_private_key_t private_openssl_rsa_private_key_t
;
35 * Private data of a openssl_rsa_private_key_t object.
37 struct private_openssl_rsa_private_key_t
{
39 * Public interface for this signer.
41 openssl_rsa_private_key_t
public;
44 * RSA object from OpenSSL
49 * TRUE if the key is from an OpenSSL ENGINE and might not be readable
54 * Keyid formed as a SHA-1 hash of a privateKey object
56 identification_t
* keyid
;
59 * Keyid formed as a SHA-1 hash of a privateKeyInfo object
61 identification_t
* keyid_info
;
70 * shared functions, implemented in openssl_rsa_public_key.c
72 bool openssl_rsa_public_key_build_id(RSA
*rsa
, identification_t
**keyid
,
73 identification_t
**keyid_info
);
76 openssl_rsa_public_key_t
*openssl_rsa_public_key_create_from_n_e(BIGNUM
*n
, BIGNUM
*e
);
80 * Build an EMPSA PKCS1 signature described in PKCS#1
82 static bool build_emsa_pkcs1_signature(private_openssl_rsa_private_key_t
*this,
83 int type
, chunk_t data
, chunk_t
*out
)
88 const EVP_MD
*hasher
= EVP_get_digestbynid(type
);
94 EVP_MD_CTX
*ctx
= EVP_MD_CTX_create();
95 EVP_PKEY
*key
= EVP_PKEY_new();
101 if (!EVP_PKEY_set1_RSA(key
, this->rsa
))
106 if (!EVP_SignInit_ex(ctx
, hasher
, NULL
))
111 if (!EVP_SignUpdate(ctx
, data
.ptr
, data
.len
))
116 sig
= malloc(EVP_PKEY_size(key
));
117 if (EVP_SignFinal(ctx
, sig
, &len
, key
))
135 EVP_MD_CTX_destroy(ctx
);
141 * Implementation of openssl_rsa_private_key.destroy.
143 static key_type_t
get_type(private_openssl_rsa_private_key_t
*this)
149 * Implementation of openssl_rsa_private_key.destroy.
151 static bool sign(private_openssl_rsa_private_key_t
*this, signature_scheme_t scheme
,
152 chunk_t data
, chunk_t
*signature
)
157 /* default is EMSA-PKCS1 using SHA1 */
158 case SIGN_RSA_EMSA_PKCS1_SHA1
:
159 return build_emsa_pkcs1_signature(this, NID_sha1
, data
, signature
);
160 case SIGN_RSA_EMSA_PKCS1_SHA256
:
161 return build_emsa_pkcs1_signature(this, NID_sha256
, data
, signature
);
162 case SIGN_RSA_EMSA_PKCS1_SHA384
:
163 return build_emsa_pkcs1_signature(this, NID_sha384
, data
, signature
);
164 case SIGN_RSA_EMSA_PKCS1_SHA512
:
165 return build_emsa_pkcs1_signature(this, NID_sha512
, data
, signature
);
166 case SIGN_RSA_EMSA_PKCS1_MD5
:
167 return build_emsa_pkcs1_signature(this, NID_md5
, data
, signature
);
169 DBG1("signature scheme %N not supported in RSA",
170 signature_scheme_names
, scheme
);
176 * Implementation of openssl_rsa_private_key.destroy.
178 static bool decrypt(private_openssl_rsa_private_key_t
*this,
179 chunk_t crypto
, chunk_t
*plain
)
181 DBG1("RSA private key decryption not implemented");
186 * Implementation of openssl_rsa_private_key.destroy.
188 static size_t get_keysize(private_openssl_rsa_private_key_t
*this)
190 return RSA_size(this->rsa
);
194 * Implementation of openssl_rsa_private_key.destroy.
196 static identification_t
* get_id(private_openssl_rsa_private_key_t
*this,
201 case ID_PUBKEY_INFO_SHA1
:
202 return this->keyid_info
;
211 * Implementation of openssl_rsa_private_key.destroy.
213 static openssl_rsa_public_key_t
* get_public_key(private_openssl_rsa_private_key_t
*this)
215 return openssl_rsa_public_key_create_from_n_e(this->rsa
->n
, this->rsa
->e
);
219 * Implementation of openssl_rsa_private_key.destroy.
221 static bool belongs_to(private_openssl_rsa_private_key_t
*this, public_key_t
*public)
223 identification_t
*keyid
;
225 if (public->get_type(public) != KEY_RSA
)
229 keyid
= public->get_id(public, ID_PUBKEY_SHA1
);
230 if (keyid
&& keyid
->equals(keyid
, this->keyid
))
234 keyid
= public->get_id(public, ID_PUBKEY_INFO_SHA1
);
235 if (keyid
&& keyid
->equals(keyid
, this->keyid_info
))
243 * Implementation of private_key_t.get_encoding.
245 static chunk_t
get_encoding(private_openssl_rsa_private_key_t
*this)
247 chunk_t enc
= chunk_empty
;
250 enc
= chunk_alloc(i2d_RSAPrivateKey(this->rsa
, NULL
));
252 i2d_RSAPrivateKey(this->rsa
, &p
);
258 * Implementation of openssl_rsa_private_key.destroy.
260 static private_openssl_rsa_private_key_t
* get_ref(private_openssl_rsa_private_key_t
*this)
268 * Implementation of openssl_rsa_private_key.destroy.
270 static void destroy(private_openssl_rsa_private_key_t
*this)
272 if (ref_put(&this->ref
))
278 DESTROY_IF(this->keyid
);
279 DESTROY_IF(this->keyid_info
);
285 * Internal generic constructor
287 static private_openssl_rsa_private_key_t
*openssl_rsa_private_key_create_empty(void)
289 private_openssl_rsa_private_key_t
*this = malloc_thing(private_openssl_rsa_private_key_t
);
291 this->public.interface
.get_type
= (key_type_t (*)(private_key_t
*this))get_type
;
292 this->public.interface
.sign
= (bool (*)(private_key_t
*this, signature_scheme_t scheme
, chunk_t data
, chunk_t
*signature
))sign
;
293 this->public.interface
.decrypt
= (bool (*)(private_key_t
*this, chunk_t crypto
, chunk_t
*plain
))decrypt
;
294 this->public.interface
.get_keysize
= (size_t (*) (private_key_t
*this))get_keysize
;
295 this->public.interface
.get_id
= (identification_t
* (*) (private_key_t
*this,id_type_t
))get_id
;
296 this->public.interface
.get_public_key
= (public_key_t
* (*)(private_key_t
*this))get_public_key
;
297 this->public.interface
.belongs_to
= (bool (*) (private_key_t
*this, public_key_t
*public))belongs_to
;
298 this->public.interface
.get_encoding
= (chunk_t(*)(private_key_t
*))get_encoding
;
299 this->public.interface
.get_ref
= (private_key_t
* (*)(private_key_t
*this))get_ref
;
300 this->public.interface
.destroy
= (void (*)(private_key_t
*this))destroy
;
302 this->engine
= FALSE
;
304 this->keyid_info
= NULL
;
311 * Generate an RSA key of specified key size
313 static openssl_rsa_private_key_t
*generate(size_t key_size
)
315 private_openssl_rsa_private_key_t
*this = openssl_rsa_private_key_create_empty();
317 this->rsa
= RSA_generate_key(key_size
, PUBLIC_EXPONENT
, NULL
, NULL
);
319 if (!openssl_rsa_public_key_build_id(this->rsa
, &this->keyid
, &this->keyid_info
))
325 return &this->public;
329 * load private key from an ASN1 encoded blob
331 static openssl_rsa_private_key_t
*load(chunk_t blob
)
333 u_char
*p
= blob
.ptr
;
334 private_openssl_rsa_private_key_t
*this = openssl_rsa_private_key_create_empty();
336 this->rsa
= d2i_RSAPrivateKey(NULL
, (const u_char
**)&p
, blob
.len
);
346 if (!openssl_rsa_public_key_build_id(this->rsa
, &this->keyid
, &this->keyid_info
))
352 if (!RSA_check_key(this->rsa
))
358 return &this->public;
362 * load private key from a smart card
364 static openssl_rsa_private_key_t
*load_from_smartcard(char *keyid
, char *pin
)
366 private_openssl_rsa_private_key_t
*this = NULL
;
368 char *engine_id
= lib
->settings
->get_str(lib
->settings
,
369 "library.plugins.openssl.engine_id", "pkcs11");
371 ENGINE
*engine
= ENGINE_by_id(engine_id
);
374 DBG1("engine '%s' is not available", engine_id
);
378 if (!ENGINE_init(engine
))
380 DBG1("failed to initialize engine '%s'", engine_id
);
384 if (!ENGINE_ctrl_cmd_string(engine
, "PIN", pin
, 0))
386 DBG1("failed to set PIN on engine '%s'", engine_id
);
390 key
= ENGINE_load_private_key(engine
, keyid
, NULL
, NULL
);
394 DBG1("failed to load private key with ID '%s' from engine '%s'", keyid
,
400 this = openssl_rsa_private_key_create_empty();
401 this->rsa
= EVP_PKEY_get1_RSA(key
);
404 if (!openssl_rsa_public_key_build_id(this->rsa
, &this->keyid
, &this->keyid_info
))
409 return &this->public;
416 typedef struct private_builder_t private_builder_t
;
418 * Builder implementation for key loading/generation
420 struct private_builder_t
{
421 /** implements the builder interface */
423 /** loaded/generated private key */
424 openssl_rsa_private_key_t
*key
;
425 /** temporary stored smartcard key ID */
427 /** temporary stored smartcard pin */
432 * Implementation of builder_t.build
434 static openssl_rsa_private_key_t
*build(private_builder_t
*this)
436 openssl_rsa_private_key_t
*key
= this->key
;
438 if (this->keyid
&& this->pin
)
440 key
= load_from_smartcard(this->keyid
, this->pin
);
447 * Implementation of builder_t.add
449 static void add(private_builder_t
*this, builder_part_t part
, ...)
458 case BUILD_BLOB_ASN1_DER
:
460 va_start(args
, part
);
461 chunk
= va_arg(args
, chunk_t
);
462 this->key
= load(chunk_clone(chunk
));
468 va_start(args
, part
);
469 this->key
= generate(va_arg(args
, u_int
));
473 case BUILD_SMARTCARD_KEYID
:
475 va_start(args
, part
);
476 this->keyid
= va_arg(args
, char*);
480 case BUILD_SMARTCARD_PIN
:
482 va_start(args
, part
);
483 this->pin
= va_arg(args
, char*);
493 destroy((private_openssl_rsa_private_key_t
*)this->key
);
495 builder_cancel(&this->public);
499 * Builder construction function
501 builder_t
*openssl_rsa_private_key_builder(key_type_t type
)
503 private_builder_t
*this;
510 this = malloc_thing(private_builder_t
);
513 this->public.add
= (void(*)(builder_t
*this, builder_part_t part
, ...))add
;
514 this->public.build
= (void*(*)(builder_t
*this))build
;
518 return &this->public;