fixed memleak
[strongswan.git] / src / libstrongswan / plugins / openssl / openssl_rsa_private_key.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #include "openssl_rsa_private_key.h"
19 #include "openssl_rsa_public_key.h"
20
21 #include <debug.h>
22
23 #include <openssl/evp.h>
24 #include <openssl/rsa.h>
25
26 /**
27 * Public exponent to use for key generation.
28 */
29 #define PUBLIC_EXPONENT 0x10001
30
31 typedef struct private_openssl_rsa_private_key_t private_openssl_rsa_private_key_t;
32
33 /**
34 * Private data of a openssl_rsa_private_key_t object.
35 */
36 struct private_openssl_rsa_private_key_t {
37 /**
38 * Public interface for this signer.
39 */
40 openssl_rsa_private_key_t public;
41
42 /**
43 * RSA object from OpenSSL
44 */
45 RSA *rsa;
46
47 /**
48 * Keyid formed as a SHA-1 hash of a privateKey object
49 */
50 identification_t* keyid;
51
52 /**
53 * Keyid formed as a SHA-1 hash of a privateKeyInfo object
54 */
55 identification_t* keyid_info;
56
57 /**
58 * reference count
59 */
60 refcount_t ref;
61 };
62
63 /**
64 * shared functions, implemented in openssl_rsa_public_key.c
65 */
66 bool openssl_rsa_public_key_build_id(RSA *rsa, identification_t **keyid,
67 identification_t **keyid_info);
68
69
70 openssl_rsa_public_key_t *openssl_rsa_public_key_create_from_n_e(BIGNUM *n, BIGNUM *e);
71
72
73 /**
74 * Build an EMPSA PKCS1 signature described in PKCS#1
75 */
76 static bool build_emsa_pkcs1_signature(private_openssl_rsa_private_key_t *this,
77 int type, chunk_t data, chunk_t *out)
78 {
79 bool success = FALSE;
80 u_char *sig = NULL;
81 u_int len;
82 const EVP_MD *hasher = EVP_get_digestbynid(type);
83 if (!hasher)
84 {
85 return FALSE;
86 }
87
88 EVP_MD_CTX *ctx = EVP_MD_CTX_create();
89 EVP_PKEY *key = EVP_PKEY_new();
90 if (!ctx || !key)
91 {
92 goto error;
93 }
94
95 if (!EVP_PKEY_set1_RSA(key, this->rsa))
96 {
97 goto error;
98 }
99
100 if (!EVP_SignInit_ex(ctx, hasher, NULL))
101 {
102 goto error;
103 }
104
105 if (!EVP_SignUpdate(ctx, data.ptr, data.len))
106 {
107 goto error;
108 }
109
110 sig = malloc(EVP_PKEY_size(key));
111 if (EVP_SignFinal(ctx, sig, &len, key))
112 {
113 out->ptr = sig;
114 out->len = len;
115 success = TRUE;
116 }
117 else
118 {
119 free(sig);
120 }
121
122 error:
123 if (key)
124 {
125 EVP_PKEY_free(key);
126 }
127 if (ctx)
128 {
129 EVP_MD_CTX_destroy(ctx);
130 }
131 return success;
132 }
133
134 /**
135 * Implementation of openssl_rsa_private_key.destroy.
136 */
137 static key_type_t get_type(private_openssl_rsa_private_key_t *this)
138 {
139 return KEY_RSA;
140 }
141
142 /**
143 * Implementation of openssl_rsa_private_key.destroy.
144 */
145 static bool sign(private_openssl_rsa_private_key_t *this, signature_scheme_t scheme,
146 chunk_t data, chunk_t *signature)
147 {
148 switch (scheme)
149 {
150 case SIGN_DEFAULT:
151 /* default is EMSA-PKCS1 using SHA1 */
152 case SIGN_RSA_EMSA_PKCS1_SHA1:
153 return build_emsa_pkcs1_signature(this, NID_sha1, 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);
162 default:
163 DBG1("signature scheme %N not supported in RSA",
164 signature_scheme_names, scheme);
165 return FALSE;
166 }
167 }
168
169 /**
170 * Implementation of openssl_rsa_private_key.destroy.
171 */
172 static bool decrypt(private_openssl_rsa_private_key_t *this,
173 chunk_t crypto, chunk_t *plain)
174 {
175 DBG1("RSA private key decryption not implemented");
176 return FALSE;
177 }
178
179 /**
180 * Implementation of openssl_rsa_private_key.destroy.
181 */
182 static size_t get_keysize(private_openssl_rsa_private_key_t *this)
183 {
184 return RSA_size(this->rsa);
185 }
186
187 /**
188 * Implementation of openssl_rsa_private_key.destroy.
189 */
190 static identification_t* get_id(private_openssl_rsa_private_key_t *this,
191 id_type_t type)
192 {
193 switch (type)
194 {
195 case ID_PUBKEY_INFO_SHA1:
196 return this->keyid_info;
197 case ID_PUBKEY_SHA1:
198 return this->keyid;
199 default:
200 return NULL;
201 }
202 }
203
204 /**
205 * Implementation of openssl_rsa_private_key.destroy.
206 */
207 static openssl_rsa_public_key_t* get_public_key(private_openssl_rsa_private_key_t *this)
208 {
209 return openssl_rsa_public_key_create_from_n_e(this->rsa->n, this->rsa->e);
210 }
211
212 /**
213 * Implementation of openssl_rsa_private_key.destroy.
214 */
215 static bool belongs_to(private_openssl_rsa_private_key_t *this, public_key_t *public)
216 {
217 identification_t *keyid;
218
219 if (public->get_type(public) != KEY_RSA)
220 {
221 return FALSE;
222 }
223 keyid = public->get_id(public, ID_PUBKEY_SHA1);
224 if (keyid && keyid->equals(keyid, this->keyid))
225 {
226 return TRUE;
227 }
228 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
229 if (keyid && keyid->equals(keyid, this->keyid_info))
230 {
231 return TRUE;
232 }
233 return FALSE;
234 }
235
236 /**
237 * Implementation of private_key_t.get_encoding.
238 */
239 static chunk_t get_encoding(private_openssl_rsa_private_key_t *this)
240 {
241 chunk_t enc = chunk_alloc(i2d_RSAPrivateKey(this->rsa, NULL));
242 u_char *p = enc.ptr;
243 i2d_RSAPrivateKey(this->rsa, &p);
244 return enc;
245 }
246
247 /**
248 * Implementation of openssl_rsa_private_key.destroy.
249 */
250 static private_openssl_rsa_private_key_t* get_ref(private_openssl_rsa_private_key_t *this)
251 {
252 ref_get(&this->ref);
253 return this;
254
255 }
256
257 /**
258 * Implementation of openssl_rsa_private_key.destroy.
259 */
260 static void destroy(private_openssl_rsa_private_key_t *this)
261 {
262 if (ref_put(&this->ref))
263 {
264 if (this->rsa)
265 {
266 RSA_free(this->rsa);
267 }
268 DESTROY_IF(this->keyid);
269 DESTROY_IF(this->keyid_info);
270 free(this);
271 }
272 }
273
274 /**
275 * Internal generic constructor
276 */
277 static private_openssl_rsa_private_key_t *openssl_rsa_private_key_create_empty(void)
278 {
279 private_openssl_rsa_private_key_t *this = malloc_thing(private_openssl_rsa_private_key_t);
280
281 this->public.interface.get_type = (key_type_t (*)(private_key_t *this))get_type;
282 this->public.interface.sign = (bool (*)(private_key_t *this, signature_scheme_t scheme, chunk_t data, chunk_t *signature))sign;
283 this->public.interface.decrypt = (bool (*)(private_key_t *this, chunk_t crypto, chunk_t *plain))decrypt;
284 this->public.interface.get_keysize = (size_t (*) (private_key_t *this))get_keysize;
285 this->public.interface.get_id = (identification_t* (*) (private_key_t *this,id_type_t))get_id;
286 this->public.interface.get_public_key = (public_key_t* (*)(private_key_t *this))get_public_key;
287 this->public.interface.belongs_to = (bool (*) (private_key_t *this, public_key_t *public))belongs_to;
288 this->public.interface.get_encoding = (chunk_t(*)(private_key_t*))get_encoding;
289 this->public.interface.get_ref = (private_key_t* (*)(private_key_t *this))get_ref;
290 this->public.interface.destroy = (void (*)(private_key_t *this))destroy;
291
292 this->keyid = NULL;
293 this->keyid_info = NULL;
294 this->ref = 1;
295
296 return this;
297 }
298
299 /**
300 * Generate an RSA key of specified key size
301 */
302 static openssl_rsa_private_key_t *generate(size_t key_size)
303 {
304 private_openssl_rsa_private_key_t *this = openssl_rsa_private_key_create_empty();
305
306 this->rsa = RSA_generate_key(key_size, PUBLIC_EXPONENT, NULL, NULL);
307
308 if (!openssl_rsa_public_key_build_id(this->rsa, &this->keyid, &this->keyid_info))
309 {
310 destroy(this);
311 return NULL;
312 }
313
314 return &this->public;
315 }
316
317 /**
318 * load private key from an ASN1 encoded blob
319 */
320 static openssl_rsa_private_key_t *load(chunk_t blob)
321 {
322 u_char *p = blob.ptr;
323 private_openssl_rsa_private_key_t *this = openssl_rsa_private_key_create_empty();
324
325 this->rsa = d2i_RSAPrivateKey(NULL, (const u_char**)&p, blob.len);
326
327 chunk_clear(&blob);
328
329 if (!this->rsa)
330 {
331 destroy(this);
332 return NULL;
333 }
334
335 if (!openssl_rsa_public_key_build_id(this->rsa, &this->keyid, &this->keyid_info))
336 {
337 destroy(this);
338 return NULL;
339 }
340
341 if (!RSA_check_key(this->rsa))
342 {
343 destroy(this);
344 return NULL;
345 }
346
347 return &this->public;
348 }
349
350 typedef struct private_builder_t private_builder_t;
351 /**
352 * Builder implementation for key loading/generation
353 */
354 struct private_builder_t {
355 /** implements the builder interface */
356 builder_t public;
357 /** loaded/generated private key */
358 openssl_rsa_private_key_t *key;
359 };
360
361 /**
362 * Implementation of builder_t.build
363 */
364 static openssl_rsa_private_key_t *build(private_builder_t *this)
365 {
366 openssl_rsa_private_key_t *key = this->key;
367
368 free(this);
369 return key;
370 }
371
372 /**
373 * Implementation of builder_t.add
374 */
375 static void add(private_builder_t *this, builder_part_t part, ...)
376 {
377 if (!this->key)
378 {
379 va_list args;
380 chunk_t chunk;
381
382 switch (part)
383 {
384 case BUILD_BLOB_ASN1_DER:
385 {
386 va_start(args, part);
387 chunk = va_arg(args, chunk_t);
388 this->key = load(chunk_clone(chunk));
389 va_end(args);
390 return;
391 }
392 case BUILD_KEY_SIZE:
393 {
394 va_start(args, part);
395 this->key = generate(va_arg(args, u_int));
396 va_end(args);
397 return;
398 }
399 default:
400 break;
401 }
402 }
403 if (this->key)
404 {
405 destroy((private_openssl_rsa_private_key_t*)this->key);
406 }
407 builder_cancel(&this->public);
408 }
409
410 /**
411 * Builder construction function
412 */
413 builder_t *openssl_rsa_private_key_builder(key_type_t type)
414 {
415 private_builder_t *this;
416
417 if (type != KEY_RSA)
418 {
419 return NULL;
420 }
421
422 this = malloc_thing(private_builder_t);
423
424 this->key = NULL;
425 this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))add;
426 this->public.build = (void*(*)(builder_t *this))build;
427
428 return &this->public;
429 }
430