Updated openssl plugin to the new builder API
[strongswan.git] / src / libstrongswan / plugins / openssl / openssl_rsa_public_key.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Copyright (C) 2008 Tobias Brunner
4 * Hochschule fuer Technik Rapperswil
5 *
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>.
10 *
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
14 * for more details.
15 */
16
17 #include "openssl_rsa_public_key.h"
18
19 #include <debug.h>
20
21 #include <openssl/evp.h>
22 #include <openssl/rsa.h>
23 #include <openssl/x509.h>
24
25 typedef struct private_openssl_rsa_public_key_t private_openssl_rsa_public_key_t;
26
27 /**
28 * Private data structure with signing context.
29 */
30 struct private_openssl_rsa_public_key_t {
31 /**
32 * Public interface for this signer.
33 */
34 openssl_rsa_public_key_t public;
35
36 /**
37 * RSA object from OpenSSL
38 */
39 RSA *rsa;
40
41 /**
42 * reference counter
43 */
44 refcount_t ref;
45 };
46
47 /**
48 * Verification of an EMPSA PKCS1 signature described in PKCS#1
49 */
50 static bool verify_emsa_pkcs1_signature(private_openssl_rsa_public_key_t *this,
51 int type, chunk_t data, chunk_t signature)
52 {
53 bool valid = FALSE;
54 int rsa_size = RSA_size(this->rsa);
55
56 /* OpenSSL expects a signature of exactly RSA size (no leading 0x00) */
57 if (signature.len > rsa_size)
58 {
59 signature = chunk_skip(signature, signature.len - rsa_size);
60 }
61
62 if (type == NID_undef)
63 {
64 chunk_t hash = chunk_alloc(rsa_size);
65
66 hash.len = RSA_public_decrypt(signature.len, signature.ptr, hash.ptr,
67 this->rsa, RSA_PKCS1_PADDING);
68 valid = chunk_equals(data, hash);
69 free(hash.ptr);
70 }
71 else
72 {
73 EVP_MD_CTX *ctx;
74 EVP_PKEY *key;
75 const EVP_MD *hasher;
76
77 hasher = EVP_get_digestbynid(type);
78 if (!hasher)
79 {
80 return FALSE;
81 }
82
83 ctx = EVP_MD_CTX_create();
84 key = EVP_PKEY_new();
85
86 if (!ctx || !key)
87 {
88 goto error;
89 }
90 if (!EVP_PKEY_set1_RSA(key, this->rsa))
91 {
92 goto error;
93 }
94 if (!EVP_VerifyInit_ex(ctx, hasher, NULL))
95 {
96 goto error;
97 }
98 if (!EVP_VerifyUpdate(ctx, data.ptr, data.len))
99 {
100 goto error;
101 }
102 valid = (EVP_VerifyFinal(ctx, signature.ptr, signature.len, key) == 1);
103
104 error:
105 if (key)
106 {
107 EVP_PKEY_free(key);
108 }
109 if (ctx)
110 {
111 EVP_MD_CTX_destroy(ctx);
112 }
113 }
114 return valid;
115 }
116
117 /**
118 * Implementation of public_key_t.get_type.
119 */
120 static key_type_t get_type(private_openssl_rsa_public_key_t *this)
121 {
122 return KEY_RSA;
123 }
124
125 /**
126 * Implementation of public_key_t.verify.
127 */
128 static bool verify(private_openssl_rsa_public_key_t *this, signature_scheme_t scheme,
129 chunk_t data, chunk_t signature)
130 {
131 switch (scheme)
132 {
133 case SIGN_RSA_EMSA_PKCS1_NULL:
134 return verify_emsa_pkcs1_signature(this, NID_undef, data, signature);
135 case SIGN_RSA_EMSA_PKCS1_SHA1:
136 return verify_emsa_pkcs1_signature(this, NID_sha1, data, signature);
137 case SIGN_RSA_EMSA_PKCS1_SHA224:
138 return verify_emsa_pkcs1_signature(this, NID_sha224, data, signature);
139 case SIGN_RSA_EMSA_PKCS1_SHA256:
140 return verify_emsa_pkcs1_signature(this, NID_sha256, data, signature);
141 case SIGN_RSA_EMSA_PKCS1_SHA384:
142 return verify_emsa_pkcs1_signature(this, NID_sha384, data, signature);
143 case SIGN_RSA_EMSA_PKCS1_SHA512:
144 return verify_emsa_pkcs1_signature(this, NID_sha512, data, signature);
145 case SIGN_RSA_EMSA_PKCS1_MD5:
146 return verify_emsa_pkcs1_signature(this, NID_md5, data, signature);
147 default:
148 DBG1("signature scheme %N not supported in RSA",
149 signature_scheme_names, scheme);
150 return FALSE;
151 }
152 }
153
154 /**
155 * Implementation of public_key_t.get_keysize.
156 */
157 static bool encrypt_(private_openssl_rsa_public_key_t *this,
158 chunk_t crypto, chunk_t *plain)
159 {
160 DBG1("RSA public key encryption not implemented");
161 return FALSE;
162 }
163
164 /**
165 * Implementation of public_key_t.get_keysize.
166 */
167 static size_t get_keysize(private_openssl_rsa_public_key_t *this)
168 {
169 return RSA_size(this->rsa);
170 }
171
172 /**
173 * Calculate fingerprint from a RSA key, also used in rsa private key.
174 */
175 bool openssl_rsa_fingerprint(RSA *rsa, key_encoding_type_t type, chunk_t *fp)
176 {
177 hasher_t *hasher;
178 chunk_t key;
179 u_char *p;
180
181 if (lib->encoding->get_cache(lib->encoding, type, rsa, fp))
182 {
183 return TRUE;
184 }
185 switch (type)
186 {
187 case KEY_ID_PUBKEY_SHA1:
188 key = chunk_alloc(i2d_RSAPublicKey(rsa, NULL));
189 p = key.ptr;
190 i2d_RSAPublicKey(rsa, &p);
191 break;
192 case KEY_ID_PUBKEY_INFO_SHA1:
193 key = chunk_alloc(i2d_RSA_PUBKEY(rsa, NULL));
194 p = key.ptr;
195 i2d_RSA_PUBKEY(rsa, &p);
196 break;
197 default:
198 return FALSE;
199 }
200 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
201 if (!hasher)
202 {
203 DBG1("SHA1 hash algorithm not supported, fingerprinting failed");
204 free(key.ptr);
205 return FALSE;
206 }
207 hasher->allocate_hash(hasher, key, fp);
208 free(key.ptr);
209 hasher->destroy(hasher);
210 lib->encoding->cache(lib->encoding, type, rsa, *fp);
211 return TRUE;
212 }
213
214 /**
215 * Implementation of public_key_t.get_fingerprint.
216 */
217 static bool get_fingerprint(private_openssl_rsa_public_key_t *this,
218 key_encoding_type_t type, chunk_t *fingerprint)
219 {
220 return openssl_rsa_fingerprint(this->rsa, type, fingerprint);
221 }
222
223 /*
224 * Implementation of public_key_t.get_encoding.
225 */
226 static bool get_encoding(private_openssl_rsa_public_key_t *this,
227 key_encoding_type_t type, chunk_t *encoding)
228 {
229 u_char *p;
230
231 switch (type)
232 {
233 case KEY_PUB_SPKI_ASN1_DER:
234 {
235 *encoding = chunk_alloc(i2d_RSA_PUBKEY(this->rsa, NULL));
236 p = encoding->ptr;
237 i2d_RSA_PUBKEY(this->rsa, &p);
238 return TRUE;
239 }
240 case KEY_PUB_ASN1_DER:
241 {
242 *encoding = chunk_alloc(i2d_RSAPublicKey(this->rsa, NULL));
243 p = encoding->ptr;
244 i2d_RSAPublicKey(this->rsa, &p);
245 return TRUE;
246 }
247 default:
248 return FALSE;
249 }
250 }
251
252 /**
253 * Implementation of public_key_t.get_ref.
254 */
255 static public_key_t* get_ref(private_openssl_rsa_public_key_t *this)
256 {
257 ref_get(&this->ref);
258 return &this->public.interface;
259 }
260
261 /**
262 * Implementation of openssl_rsa_public_key.destroy.
263 */
264 static void destroy(private_openssl_rsa_public_key_t *this)
265 {
266 if (ref_put(&this->ref))
267 {
268 if (this->rsa)
269 {
270 lib->encoding->clear_cache(lib->encoding, this->rsa);
271 RSA_free(this->rsa);
272 }
273 free(this);
274 }
275 }
276
277 /**
278 * Generic private constructor
279 */
280 static private_openssl_rsa_public_key_t *create_empty()
281 {
282 private_openssl_rsa_public_key_t *this = malloc_thing(private_openssl_rsa_public_key_t);
283
284 this->public.interface.get_type = (key_type_t (*)(public_key_t *this))get_type;
285 this->public.interface.verify = (bool (*)(public_key_t *this, signature_scheme_t scheme, chunk_t data, chunk_t signature))verify;
286 this->public.interface.encrypt = (bool (*)(public_key_t *this, chunk_t crypto, chunk_t *plain))encrypt_;
287 this->public.interface.equals = public_key_equals;
288 this->public.interface.get_keysize = (size_t (*) (public_key_t *this))get_keysize;
289 this->public.interface.get_fingerprint = (bool(*)(public_key_t*, key_encoding_type_t type, chunk_t *fp))get_fingerprint;
290 this->public.interface.get_encoding = (bool(*)(public_key_t*, key_encoding_type_t type, chunk_t *encoding))get_encoding;
291 this->public.interface.get_ref = (public_key_t* (*)(public_key_t *this))get_ref;
292 this->public.interface.destroy = (void (*)(public_key_t *this))destroy;
293
294 this->rsa = NULL;
295 this->ref = 1;
296
297 return this;
298 }
299
300 /**
301 * See header.
302 */
303 openssl_rsa_public_key_t *openssl_rsa_public_key_load(key_type_t type,
304 va_list args)
305 {
306 private_openssl_rsa_public_key_t *this;
307 chunk_t blob;
308
309 while (TRUE)
310 {
311 switch (va_arg(args, builder_part_t))
312 {
313 case BUILD_BLOB_ASN1_DER:
314 blob = va_arg(args, chunk_t);
315 continue;
316 case BUILD_END:
317 break;
318 default:
319 return NULL;
320 }
321 break;
322 }
323
324 this = create_empty();
325 this->rsa = d2i_RSAPublicKey(NULL, (const u_char**)&blob.ptr, blob.len);
326 if (!this->rsa)
327 {
328 destroy(this);
329 return NULL;
330 }
331 return &this->public;
332 }
333