Use bits instead of bytes for a private/public key
[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 METHOD(public_key_t, get_type, key_type_t,
118 private_openssl_rsa_public_key_t *this)
119 {
120 return KEY_RSA;
121 }
122
123 METHOD(public_key_t, verify, bool,
124 private_openssl_rsa_public_key_t *this, signature_scheme_t scheme,
125 chunk_t data, chunk_t signature)
126 {
127 switch (scheme)
128 {
129 case SIGN_RSA_EMSA_PKCS1_NULL:
130 return verify_emsa_pkcs1_signature(this, NID_undef, data, signature);
131 case SIGN_RSA_EMSA_PKCS1_SHA1:
132 return verify_emsa_pkcs1_signature(this, NID_sha1, data, signature);
133 case SIGN_RSA_EMSA_PKCS1_SHA224:
134 return verify_emsa_pkcs1_signature(this, NID_sha224, data, signature);
135 case SIGN_RSA_EMSA_PKCS1_SHA256:
136 return verify_emsa_pkcs1_signature(this, NID_sha256, data, signature);
137 case SIGN_RSA_EMSA_PKCS1_SHA384:
138 return verify_emsa_pkcs1_signature(this, NID_sha384, data, signature);
139 case SIGN_RSA_EMSA_PKCS1_SHA512:
140 return verify_emsa_pkcs1_signature(this, NID_sha512, data, signature);
141 case SIGN_RSA_EMSA_PKCS1_MD5:
142 return verify_emsa_pkcs1_signature(this, NID_md5, data, signature);
143 default:
144 DBG1(DBG_LIB, "signature scheme %N not supported in RSA",
145 signature_scheme_names, scheme);
146 return FALSE;
147 }
148 }
149
150 METHOD(public_key_t, encrypt, bool,
151 private_openssl_rsa_public_key_t *this, encryption_scheme_t scheme,
152 chunk_t crypto, chunk_t *plain)
153 {
154 DBG1(DBG_LIB, "RSA public key encryption not implemented");
155 return FALSE;
156 }
157
158 METHOD(public_key_t, get_keysize, int,
159 private_openssl_rsa_public_key_t *this)
160 {
161 return RSA_size(this->rsa) * 8;
162 }
163
164 /**
165 * Calculate fingerprint from a RSA key, also used in rsa private key.
166 */
167 bool openssl_rsa_fingerprint(RSA *rsa, cred_encoding_type_t type, chunk_t *fp)
168 {
169 hasher_t *hasher;
170 chunk_t key;
171 u_char *p;
172
173 if (lib->encoding->get_cache(lib->encoding, type, rsa, fp))
174 {
175 return TRUE;
176 }
177 switch (type)
178 {
179 case KEYID_PUBKEY_SHA1:
180 key = chunk_alloc(i2d_RSAPublicKey(rsa, NULL));
181 p = key.ptr;
182 i2d_RSAPublicKey(rsa, &p);
183 break;
184 case KEYID_PUBKEY_INFO_SHA1:
185 key = chunk_alloc(i2d_RSA_PUBKEY(rsa, NULL));
186 p = key.ptr;
187 i2d_RSA_PUBKEY(rsa, &p);
188 break;
189 default:
190 return FALSE;
191 }
192 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
193 if (!hasher)
194 {
195 DBG1(DBG_LIB, "SHA1 hash algorithm not supported, fingerprinting failed");
196 free(key.ptr);
197 return FALSE;
198 }
199 hasher->allocate_hash(hasher, key, fp);
200 free(key.ptr);
201 hasher->destroy(hasher);
202 lib->encoding->cache(lib->encoding, type, rsa, *fp);
203 return TRUE;
204 }
205
206 METHOD(public_key_t, get_fingerprint, bool,
207 private_openssl_rsa_public_key_t *this, cred_encoding_type_t type,
208 chunk_t *fingerprint)
209 {
210 return openssl_rsa_fingerprint(this->rsa, type, fingerprint);
211 }
212
213 METHOD(public_key_t, get_encoding, bool,
214 private_openssl_rsa_public_key_t *this, cred_encoding_type_t type,
215 chunk_t *encoding)
216 {
217 u_char *p;
218
219 switch (type)
220 {
221 case PUBKEY_SPKI_ASN1_DER:
222 case PUBKEY_PEM:
223 {
224 bool success = TRUE;
225
226 *encoding = chunk_alloc(i2d_RSA_PUBKEY(this->rsa, NULL));
227 p = encoding->ptr;
228 i2d_RSA_PUBKEY(this->rsa, &p);
229
230 if (type == PUBKEY_PEM)
231 {
232 chunk_t asn1_encoding = *encoding;
233
234 success = lib->encoding->encode(lib->encoding, PUBKEY_PEM,
235 NULL, encoding, CRED_PART_RSA_PUB_ASN1_DER,
236 asn1_encoding, CRED_PART_END);
237 chunk_clear(&asn1_encoding);
238 }
239 return success;
240 }
241 case PUBKEY_ASN1_DER:
242 {
243 *encoding = chunk_alloc(i2d_RSAPublicKey(this->rsa, NULL));
244 p = encoding->ptr;
245 i2d_RSAPublicKey(this->rsa, &p);
246 return TRUE;
247 }
248 default:
249 return FALSE;
250 }
251 }
252
253 METHOD(public_key_t, get_ref, public_key_t*,
254 private_openssl_rsa_public_key_t *this)
255 {
256 ref_get(&this->ref);
257 return &this->public.key;
258 }
259
260 METHOD(public_key_t, destroy, void,
261 private_openssl_rsa_public_key_t *this)
262 {
263 if (ref_put(&this->ref))
264 {
265 if (this->rsa)
266 {
267 lib->encoding->clear_cache(lib->encoding, this->rsa);
268 RSA_free(this->rsa);
269 }
270 free(this);
271 }
272 }
273
274 /**
275 * Generic private constructor
276 */
277 static private_openssl_rsa_public_key_t *create_empty()
278 {
279 private_openssl_rsa_public_key_t *this;
280
281 INIT(this,
282 .public.key = {
283 .get_type = _get_type,
284 .verify = _verify,
285 .encrypt = _encrypt,
286 .equals = public_key_equals,
287 .get_keysize = _get_keysize,
288 .get_fingerprint = _get_fingerprint,
289 .has_fingerprint = public_key_has_fingerprint,
290 .get_encoding = _get_encoding,
291 .get_ref = _get_ref,
292 .destroy = _destroy,
293 },
294 .ref = 1,
295 );
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, n, e;
308
309 n = e = blob = chunk_empty;
310 while (TRUE)
311 {
312 switch (va_arg(args, builder_part_t))
313 {
314 case BUILD_BLOB_ASN1_DER:
315 blob = va_arg(args, chunk_t);
316 continue;
317 case BUILD_RSA_MODULUS:
318 n = va_arg(args, chunk_t);
319 continue;
320 case BUILD_RSA_PUB_EXP:
321 e = va_arg(args, chunk_t);
322 continue;
323 case BUILD_END:
324 break;
325 default:
326 return NULL;
327 }
328 break;
329 }
330
331 this = create_empty();
332 if (blob.ptr)
333 {
334 switch (type)
335 {
336 case KEY_ANY:
337 this->rsa = d2i_RSA_PUBKEY(NULL, (const u_char**)&blob.ptr,
338 blob.len);
339 break;
340 case KEY_RSA:
341 this->rsa = d2i_RSAPublicKey(NULL, (const u_char**)&blob.ptr,
342 blob.len);
343 break;
344 default:
345 break;
346 }
347 if (this->rsa)
348 {
349 return &this->public;
350 }
351 }
352 else if (n.ptr && e.ptr && type == KEY_RSA)
353 {
354 this->rsa = RSA_new();
355 this->rsa->n = BN_bin2bn((const u_char*)n.ptr, n.len, NULL);
356 this->rsa->e = BN_bin2bn((const u_char*)e.ptr, e.len, NULL);
357 return &this->public;
358 }
359 destroy(this);
360 return NULL;
361 }
362