2 * Copyright (C) 2009 Martin Willi
3 * Copyright (C) 2008 Tobias Brunner
4 * Hochschule fuer Technik Rapperswil
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>.
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
17 #include "openssl_rsa_private_key.h"
18 #include "openssl_rsa_public_key.h"
22 #include <openssl/evp.h>
23 #include <openssl/rsa.h>
24 #include <openssl/engine.h>
27 * Public exponent to use for key generation.
29 #define PUBLIC_EXPONENT 0x10001
31 typedef struct private_openssl_rsa_private_key_t private_openssl_rsa_private_key_t
;
34 * Private data of a openssl_rsa_private_key_t object.
36 struct private_openssl_rsa_private_key_t
{
38 * Public interface for this signer.
40 openssl_rsa_private_key_t
public;
43 * RSA object from OpenSSL
48 * TRUE if the key is from an OpenSSL ENGINE and might not be readable
58 /* implemented in rsa public key */
59 bool openssl_rsa_fingerprint(RSA
*rsa
, key_encoding_type_t type
, chunk_t
*fp
);
62 * Build an EMPSA PKCS1 signature described in PKCS#1
64 static bool build_emsa_pkcs1_signature(private_openssl_rsa_private_key_t
*this,
65 int type
, chunk_t data
, chunk_t
*sig
)
69 *sig
= chunk_alloc(RSA_size(this->rsa
));
71 if (type
== NID_undef
)
73 if (RSA_private_encrypt(data
.len
, data
.ptr
, sig
->ptr
, this->rsa
,
74 RSA_PKCS1_PADDING
) == sig
->len
)
86 hasher
= EVP_get_digestbynid(type
);
92 ctx
= EVP_MD_CTX_create();
98 if (!EVP_PKEY_set1_RSA(key
, this->rsa
))
102 if (!EVP_SignInit_ex(ctx
, hasher
, NULL
))
106 if (!EVP_SignUpdate(ctx
, data
.ptr
, data
.len
))
110 if (EVP_SignFinal(ctx
, sig
->ptr
, &len
, key
))
122 EVP_MD_CTX_destroy(ctx
);
133 * Implementation of openssl_rsa_private_key.get_type.
135 static key_type_t
get_type(private_openssl_rsa_private_key_t
*this)
141 * Implementation of openssl_rsa_private_key.sign.
143 static bool sign(private_openssl_rsa_private_key_t
*this, signature_scheme_t scheme
,
144 chunk_t data
, chunk_t
*signature
)
148 case SIGN_RSA_EMSA_PKCS1_NULL
:
149 return build_emsa_pkcs1_signature(this, NID_undef
, data
, signature
);
150 case SIGN_RSA_EMSA_PKCS1_SHA1
:
151 return build_emsa_pkcs1_signature(this, NID_sha1
, data
, signature
);
152 case SIGN_RSA_EMSA_PKCS1_SHA224
:
153 return build_emsa_pkcs1_signature(this, NID_sha224
, data
, signature
);
154 case SIGN_RSA_EMSA_PKCS1_SHA256
:
155 return build_emsa_pkcs1_signature(this, NID_sha256
, data
, signature
);
156 case SIGN_RSA_EMSA_PKCS1_SHA384
:
157 return build_emsa_pkcs1_signature(this, NID_sha384
, data
, signature
);
158 case SIGN_RSA_EMSA_PKCS1_SHA512
:
159 return build_emsa_pkcs1_signature(this, NID_sha512
, data
, signature
);
160 case SIGN_RSA_EMSA_PKCS1_MD5
:
161 return build_emsa_pkcs1_signature(this, NID_md5
, data
, signature
);
163 DBG1("signature scheme %N not supported in RSA",
164 signature_scheme_names
, scheme
);
170 * Implementation of openssl_rsa_private_key.decrypt.
172 static bool decrypt(private_openssl_rsa_private_key_t
*this,
173 chunk_t crypto
, chunk_t
*plain
)
175 DBG1("RSA private key decryption not implemented");
180 * Implementation of openssl_rsa_private_key.get_keysize.
182 static size_t get_keysize(private_openssl_rsa_private_key_t
*this)
184 return RSA_size(this->rsa
);
188 * Implementation of openssl_rsa_private_key.get_public_key.
190 static public_key_t
* get_public_key(private_openssl_rsa_private_key_t
*this)
196 enc
= chunk_alloc(i2d_RSAPublicKey(this->rsa
, NULL
));
198 i2d_RSAPublicKey(this->rsa
, &p
);
199 key
= lib
->creds
->create(lib
->creds
, CRED_PUBLIC_KEY
, KEY_RSA
,
200 BUILD_BLOB_ASN1_DER
, enc
, BUILD_END
);
206 * Implementation of public_key_t.get_fingerprint.
208 static bool get_fingerprint(private_openssl_rsa_private_key_t
*this,
209 key_encoding_type_t type
, chunk_t
*fingerprint
)
211 return openssl_rsa_fingerprint(this->rsa
, type
, fingerprint
);
215 * Implementation of public_key_t.get_encoding.
217 static bool get_encoding(private_openssl_rsa_private_key_t
*this,
218 key_encoding_type_t type
, chunk_t
*encoding
)
228 case KEY_PRIV_ASN1_DER
:
230 *encoding
= chunk_alloc(i2d_RSAPrivateKey(this->rsa
, NULL
));
232 i2d_RSAPrivateKey(this->rsa
, &p
);
241 * Implementation of openssl_rsa_private_key.get_ref.
243 static private_openssl_rsa_private_key_t
* get_ref(private_openssl_rsa_private_key_t
*this)
250 * Implementation of openssl_rsa_private_key.destroy.
252 static void destroy(private_openssl_rsa_private_key_t
*this)
254 if (ref_put(&this->ref
))
258 lib
->encoding
->clear_cache(lib
->encoding
, this->rsa
);
266 * Internal generic constructor
268 static private_openssl_rsa_private_key_t
*create_empty(void)
270 private_openssl_rsa_private_key_t
*this = malloc_thing(private_openssl_rsa_private_key_t
);
272 this->public.interface
.get_type
= (key_type_t (*) (private_key_t
*))get_type
;
273 this->public.interface
.sign
= (bool (*) (private_key_t
*, signature_scheme_t
, chunk_t
, chunk_t
*))sign
;
274 this->public.interface
.decrypt
= (bool (*) (private_key_t
*, chunk_t
, chunk_t
*))decrypt
;
275 this->public.interface
.get_keysize
= (size_t (*) (private_key_t
*))get_keysize
;
276 this->public.interface
.get_public_key
= (public_key_t
* (*) (private_key_t
*))get_public_key
;
277 this->public.interface
.equals
= private_key_equals
;
278 this->public.interface
.belongs_to
= private_key_belongs_to
;
279 this->public.interface
.get_fingerprint
= (bool(*)(private_key_t
*, key_encoding_type_t type
, chunk_t
*fp
))get_fingerprint
;
280 this->public.interface
.get_encoding
= (bool(*)(private_key_t
*, key_encoding_type_t type
, chunk_t
*encoding
))get_encoding
;
281 this->public.interface
.get_ref
= (private_key_t
* (*) (private_key_t
*))get_ref
;
282 this->public.interface
.destroy
= (void (*) (private_key_t
*))destroy
;
284 this->engine
= FALSE
;
293 openssl_rsa_private_key_t
*openssl_rsa_private_key_gen(key_type_t type
,
296 private_openssl_rsa_private_key_t
*this;
301 switch (va_arg(args
, builder_part_t
))
304 key_size
= va_arg(args
, u_int
);
317 this = create_empty();
318 this->rsa
= RSA_generate_key(key_size
, PUBLIC_EXPONENT
, NULL
, NULL
);
320 return &this->public;
326 openssl_rsa_private_key_t
*openssl_rsa_private_key_load(key_type_t type
,
329 private_openssl_rsa_private_key_t
*this;
330 chunk_t blob
= chunk_empty
;
334 switch (va_arg(args
, builder_part_t
))
336 case BUILD_BLOB_ASN1_DER
:
337 blob
= va_arg(args
, chunk_t
);
347 this = create_empty();
348 this->rsa
= d2i_RSAPrivateKey(NULL
, (const u_char
**)&blob
.ptr
, blob
.len
);
354 if (!RSA_check_key(this->rsa
))
359 return &this->public;
365 openssl_rsa_private_key_t
*openssl_rsa_private_key_connect(key_type_t type
,
368 private_openssl_rsa_private_key_t
*this;
369 char *keyid
= NULL
, *pin
= NULL
;
376 switch (va_arg(args
, builder_part_t
))
378 case BUILD_SMARTCARD_KEYID
:
379 keyid
= va_arg(args
, char*);
381 case BUILD_SMARTCARD_PIN
:
382 pin
= va_arg(args
, char*);
396 engine_id
= lib
->settings
->get_str(lib
->settings
,
397 "library.plugins.openssl.engine_id", "pkcs11");
398 engine
= ENGINE_by_id(engine_id
);
401 DBG1("engine '%s' is not available", engine_id
);
404 if (!ENGINE_init(engine
))
406 DBG1("failed to initialize engine '%s'", engine_id
);
410 if (!ENGINE_ctrl_cmd_string(engine
, "PIN", pin
, 0))
412 DBG1("failed to set PIN on engine '%s'", engine_id
);
417 key
= ENGINE_load_private_key(engine
, keyid
, NULL
, NULL
);
420 DBG1("failed to load private key with ID '%s' from engine '%s'",
427 this = create_empty();
428 this->rsa
= EVP_PKEY_get1_RSA(key
);
431 return &this->public;