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 #ifndef OPENSSL_NO_ENGINE
25 #include <openssl/engine.h>
26 #endif /* OPENSSL_NO_ENGINE */
29 * Public exponent to use for key generation.
31 #define PUBLIC_EXPONENT 0x10001
33 typedef struct private_openssl_rsa_private_key_t private_openssl_rsa_private_key_t
;
36 * Private data of a openssl_rsa_private_key_t object.
38 struct private_openssl_rsa_private_key_t
{
40 * Public interface for this signer.
42 openssl_rsa_private_key_t
public;
45 * RSA object from OpenSSL
50 * TRUE if the key is from an OpenSSL ENGINE and might not be readable
60 /* implemented in rsa public key */
61 bool openssl_rsa_fingerprint(RSA
*rsa
, cred_encoding_type_t type
, chunk_t
*fp
);
64 * Build an EMPSA PKCS1 signature described in PKCS#1
66 static bool build_emsa_pkcs1_signature(private_openssl_rsa_private_key_t
*this,
67 int type
, chunk_t data
, chunk_t
*sig
)
71 *sig
= chunk_alloc(RSA_size(this->rsa
));
73 if (type
== NID_undef
)
75 if (RSA_private_encrypt(data
.len
, data
.ptr
, sig
->ptr
, this->rsa
,
76 RSA_PKCS1_PADDING
) == sig
->len
)
88 hasher
= EVP_get_digestbynid(type
);
94 ctx
= EVP_MD_CTX_create();
100 if (!EVP_PKEY_set1_RSA(key
, this->rsa
))
104 if (!EVP_SignInit_ex(ctx
, hasher
, NULL
))
108 if (!EVP_SignUpdate(ctx
, data
.ptr
, data
.len
))
112 if (EVP_SignFinal(ctx
, sig
->ptr
, &len
, key
))
124 EVP_MD_CTX_destroy(ctx
);
135 METHOD(private_key_t
, get_type
, key_type_t
,
136 private_openssl_rsa_private_key_t
*this)
141 METHOD(private_key_t
, sign
, bool,
142 private_openssl_rsa_private_key_t
*this, signature_scheme_t scheme
,
143 chunk_t data
, chunk_t
*signature
)
147 case SIGN_RSA_EMSA_PKCS1_NULL
:
148 return build_emsa_pkcs1_signature(this, NID_undef
, data
, signature
);
149 case SIGN_RSA_EMSA_PKCS1_SHA1
:
150 return build_emsa_pkcs1_signature(this, NID_sha1
, data
, signature
);
151 case SIGN_RSA_EMSA_PKCS1_SHA224
:
152 return build_emsa_pkcs1_signature(this, NID_sha224
, data
, signature
);
153 case SIGN_RSA_EMSA_PKCS1_SHA256
:
154 return build_emsa_pkcs1_signature(this, NID_sha256
, data
, signature
);
155 case SIGN_RSA_EMSA_PKCS1_SHA384
:
156 return build_emsa_pkcs1_signature(this, NID_sha384
, data
, signature
);
157 case SIGN_RSA_EMSA_PKCS1_SHA512
:
158 return build_emsa_pkcs1_signature(this, NID_sha512
, data
, signature
);
159 case SIGN_RSA_EMSA_PKCS1_MD5
:
160 return build_emsa_pkcs1_signature(this, NID_md5
, data
, signature
);
162 DBG1(DBG_LIB
, "signature scheme %N not supported in RSA",
163 signature_scheme_names
, scheme
);
168 METHOD(private_key_t
, decrypt
, bool,
169 private_openssl_rsa_private_key_t
*this, encryption_scheme_t scheme
,
170 chunk_t crypto
, chunk_t
*plain
)
177 case ENCRYPT_RSA_PKCS1
:
178 padding
= RSA_PKCS1_PADDING
;
180 case ENCRYPT_RSA_OAEP_SHA1
:
181 padding
= RSA_PKCS1_OAEP_PADDING
;
184 DBG1(DBG_LIB
, "encryption scheme %N not supported via openssl",
185 encryption_scheme_names
, scheme
);
188 decrypted
= malloc(RSA_size(this->rsa
));
189 len
= RSA_private_decrypt(crypto
.len
, crypto
.ptr
, decrypted
,
193 DBG1(DBG_LIB
, "RSA decryption failed");
197 *plain
= chunk_create(decrypted
, len
);
201 METHOD(private_key_t
, get_keysize
, int,
202 private_openssl_rsa_private_key_t
*this)
204 return RSA_size(this->rsa
) * 8;
207 METHOD(private_key_t
, get_public_key
, public_key_t
*,
208 private_openssl_rsa_private_key_t
*this)
214 enc
= chunk_alloc(i2d_RSAPublicKey(this->rsa
, NULL
));
216 i2d_RSAPublicKey(this->rsa
, &p
);
217 key
= lib
->creds
->create(lib
->creds
, CRED_PUBLIC_KEY
, KEY_RSA
,
218 BUILD_BLOB_ASN1_DER
, enc
, BUILD_END
);
223 METHOD(private_key_t
, get_fingerprint
, bool,
224 private_openssl_rsa_private_key_t
*this, cred_encoding_type_t type
,
225 chunk_t
*fingerprint
)
227 return openssl_rsa_fingerprint(this->rsa
, type
, fingerprint
);
230 METHOD(private_key_t
, get_encoding
, bool,
231 private_openssl_rsa_private_key_t
*this, cred_encoding_type_t type
,
242 case PRIVKEY_ASN1_DER
:
247 *encoding
= chunk_alloc(i2d_RSAPrivateKey(this->rsa
, NULL
));
249 i2d_RSAPrivateKey(this->rsa
, &p
);
251 if (type
== PRIVKEY_PEM
)
253 chunk_t asn1_encoding
= *encoding
;
255 success
= lib
->encoding
->encode(lib
->encoding
, PRIVKEY_PEM
,
256 NULL
, encoding
, CRED_PART_RSA_PRIV_ASN1_DER
,
257 asn1_encoding
, CRED_PART_END
);
258 chunk_clear(&asn1_encoding
);
267 METHOD(private_key_t
, get_ref
, private_key_t
*,
268 private_openssl_rsa_private_key_t
*this)
271 return &this->public.key
;
274 METHOD(private_key_t
, destroy
, void,
275 private_openssl_rsa_private_key_t
*this)
277 if (ref_put(&this->ref
))
281 lib
->encoding
->clear_cache(lib
->encoding
, this->rsa
);
289 * Internal generic constructor
291 static private_openssl_rsa_private_key_t
*create_empty()
293 private_openssl_rsa_private_key_t
*this;
298 .get_type
= _get_type
,
301 .get_keysize
= _get_keysize
,
302 .get_public_key
= _get_public_key
,
303 .equals
= private_key_equals
,
304 .belongs_to
= private_key_belongs_to
,
305 .get_fingerprint
= _get_fingerprint
,
306 .has_fingerprint
= private_key_has_fingerprint
,
307 .get_encoding
= _get_encoding
,
321 openssl_rsa_private_key_t
*openssl_rsa_private_key_gen(key_type_t type
,
324 private_openssl_rsa_private_key_t
*this;
331 switch (va_arg(args
, builder_part_t
))
334 key_size
= va_arg(args
, u_int
);
348 if (!e
|| !BN_set_word(e
, PUBLIC_EXPONENT
))
353 if (!rsa
|| !RSA_generate_key_ex(rsa
, key_size
, e
, NULL
))
357 this = create_empty();
360 return &this->public;
377 openssl_rsa_private_key_t
*openssl_rsa_private_key_load(key_type_t type
,
380 private_openssl_rsa_private_key_t
*this;
381 chunk_t blob
, n
, e
, d
, p
, q
, exp1
, exp2
, coeff
;
383 blob
= n
= e
= d
= p
= q
= exp1
= exp2
= coeff
= chunk_empty
;
386 switch (va_arg(args
, builder_part_t
))
388 case BUILD_BLOB_ASN1_DER
:
389 blob
= va_arg(args
, chunk_t
);
391 case BUILD_RSA_MODULUS
:
392 n
= va_arg(args
, chunk_t
);
394 case BUILD_RSA_PUB_EXP
:
395 e
= va_arg(args
, chunk_t
);
397 case BUILD_RSA_PRIV_EXP
:
398 d
= va_arg(args
, chunk_t
);
400 case BUILD_RSA_PRIME1
:
401 p
= va_arg(args
, chunk_t
);
403 case BUILD_RSA_PRIME2
:
404 q
= va_arg(args
, chunk_t
);
407 exp1
= va_arg(args
, chunk_t
);
410 exp2
= va_arg(args
, chunk_t
);
412 case BUILD_RSA_COEFF
:
413 coeff
= va_arg(args
, chunk_t
);
423 this = create_empty();
426 this->rsa
= d2i_RSAPrivateKey(NULL
, (const u_char
**)&blob
.ptr
, blob
.len
);
427 if (this->rsa
&& RSA_check_key(this->rsa
))
429 return &this->public;
432 else if (n
.ptr
&& e
.ptr
&& d
.ptr
&& p
.ptr
&& q
.ptr
&& coeff
.ptr
)
434 this->rsa
= RSA_new();
435 this->rsa
->n
= BN_bin2bn((const u_char
*)n
.ptr
, n
.len
, NULL
);
436 this->rsa
->e
= BN_bin2bn((const u_char
*)e
.ptr
, e
.len
, NULL
);
437 this->rsa
->d
= BN_bin2bn((const u_char
*)d
.ptr
, d
.len
, NULL
);
438 this->rsa
->p
= BN_bin2bn((const u_char
*)p
.ptr
, p
.len
, NULL
);
439 this->rsa
->q
= BN_bin2bn((const u_char
*)q
.ptr
, q
.len
, NULL
);
442 this->rsa
->dmp1
= BN_bin2bn((const u_char
*)exp1
.ptr
, exp1
.len
, NULL
);
446 this->rsa
->dmq1
= BN_bin2bn((const u_char
*)exp2
.ptr
, exp2
.len
, NULL
);
448 this->rsa
->iqmp
= BN_bin2bn((const u_char
*)coeff
.ptr
, coeff
.len
, NULL
);
449 if (RSA_check_key(this->rsa
))
451 return &this->public;
458 #ifndef OPENSSL_NO_ENGINE
460 * Login to engine with a PIN specified for a keyid
462 static bool login(ENGINE
*engine
, chunk_t keyid
)
464 enumerator_t
*enumerator
;
465 shared_key_t
*shared
;
466 identification_t
*id
;
469 bool found
= FALSE
, success
= FALSE
;
471 id
= identification_create_from_encoding(ID_KEY_ID
, keyid
);
472 enumerator
= lib
->credmgr
->create_shared_enumerator(lib
->credmgr
,
473 SHARED_PIN
, id
, NULL
);
474 while (enumerator
->enumerate(enumerator
, &shared
, NULL
, NULL
))
477 key
= shared
->get_key(shared
);
478 if (snprintf(pin
, sizeof(pin
),
479 "%.*s", (int)key
.len
, key
.ptr
) >= sizeof(pin
))
483 if (ENGINE_ctrl_cmd_string(engine
, "PIN", pin
, 0))
490 DBG1(DBG_CFG
, "setting PIN on engine failed");
493 enumerator
->destroy(enumerator
);
497 DBG1(DBG_CFG
, "no PIN found for %#B", &keyid
);
501 #endif /* OPENSSL_NO_ENGINE */
506 openssl_rsa_private_key_t
*openssl_rsa_private_key_connect(key_type_t type
,
509 #ifndef OPENSSL_NO_ENGINE
510 private_openssl_rsa_private_key_t
*this;
511 char *engine_id
= NULL
;
513 chunk_t keyid
= chunk_empty
;;
520 switch (va_arg(args
, builder_part_t
))
522 case BUILD_PKCS11_KEYID
:
523 keyid
= va_arg(args
, chunk_t
);
525 case BUILD_PKCS11_SLOT
:
526 slot
= va_arg(args
, int);
528 case BUILD_PKCS11_MODULE
:
529 engine_id
= va_arg(args
, char*);
538 if (!keyid
.len
|| keyid
.len
> 40)
543 memset(keyname
, 0, sizeof(keyname
));
546 snprintf(keyname
, sizeof(keyname
), "%d:", slot
);
548 if (sizeof(keyname
) - strlen(keyname
) <= keyid
.len
* 4 / 3 + 1)
552 chunk_to_hex(keyid
, keyname
+ strlen(keyname
), FALSE
);
556 engine_id
= lib
->settings
->get_str(lib
->settings
,
557 "libstrongswan.plugins.openssl.engine_id", "pkcs11");
559 engine
= ENGINE_by_id(engine_id
);
562 DBG2(DBG_LIB
, "engine '%s' is not available", engine_id
);
565 if (!ENGINE_init(engine
))
567 DBG1(DBG_LIB
, "failed to initialize engine '%s'", engine_id
);
571 if (!login(engine
, keyid
))
573 DBG1(DBG_LIB
, "login to engine '%s' failed", engine_id
);
577 key
= ENGINE_load_private_key(engine
, keyname
, NULL
, NULL
);
580 DBG1(DBG_LIB
, "failed to load private key with ID '%s' from "
581 "engine '%s'", keyname
, engine_id
);
587 this = create_empty();
588 this->rsa
= EVP_PKEY_get1_RSA(key
);
596 return &this->public;
597 #else /* OPENSSL_NO_ENGINE */
599 #endif /* OPENSSL_NO_ENGINE */