]> git.ipfire.org Git - people/ms/strongswan.git/blob - src/libstrongswan/plugins/wolfssl/wolfssl_ed_private_key.c
kernel-pfroute: Set lower MTU on TUN devices
[people/ms/strongswan.git] / src / libstrongswan / plugins / wolfssl / wolfssl_ed_private_key.c
1 /*
2 * Copyright (C) 2020 Tobias Brunner
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * Copyright (C) 2019 Sean Parkinson, wolfSSL Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26 #include "wolfssl_common.h"
27
28 #if defined(HAVE_ED25519) || defined(HAVE_ED448)
29
30 #include "wolfssl_ed_private_key.h"
31
32 #include <utils/debug.h>
33
34 #include <wolfssl/wolfcrypt/asn.h>
35
36 typedef struct private_private_key_t private_private_key_t;
37
38 /**
39 * Private data
40 */
41 struct private_private_key_t {
42
43 /**
44 * Public interface
45 */
46 private_key_t public;
47
48 /**
49 * Key object
50 */
51 wolfssl_ed_key key;
52
53 /**
54 * Key type
55 */
56 key_type_t type;
57
58 /**
59 * Reference count
60 */
61 refcount_t ref;
62 };
63
64 /* from ed public key */
65 int wolfssl_ed_keysize(key_type_t type);
66 bool wolfssl_ed_create(wolfssl_ed_key *key, key_type_t type);
67 void wolfssl_ed_destroy(wolfssl_ed_key *key, key_type_t type);
68 bool wolfssl_ed_public_key(wolfssl_ed_key *key, key_type_t type, chunk_t *raw);
69 bool wolfssl_ed_fingerprint(wolfssl_ed_key *key, key_type_t key_type,
70 cred_encoding_type_t type, chunk_t *fp);
71
72 METHOD(private_key_t, sign, bool,
73 private_private_key_t *this, signature_scheme_t scheme,
74 void *params, chunk_t data, chunk_t *signature)
75 {
76 word32 len;
77 byte dummy[1];
78 int ret = -1;
79
80 if ((this->type == KEY_ED25519 && scheme != SIGN_ED25519) ||
81 (this->type == KEY_ED448 && scheme != SIGN_ED448))
82 {
83 DBG1(DBG_LIB, "signature scheme %N not supported by %N key",
84 signature_scheme_names, scheme, key_type_names, this->type);
85 return FALSE;
86 }
87
88 if (!data.ptr && !data.len)
89 {
90 data.ptr = dummy;
91 }
92
93 if (this->type == KEY_ED25519)
94 {
95 #ifdef HAVE_ED25519
96 len = ED25519_SIG_SIZE;
97 *signature = chunk_alloc(len);
98 ret = wc_ed25519_sign_msg(data.ptr, data.len, signature->ptr, &len,
99 &this->key.ed25519);
100 #endif
101 }
102 else if (this->type == KEY_ED448)
103 {
104 #ifdef HAVE_ED448
105 len = ED448_SIG_SIZE;
106 *signature = chunk_alloc(len);
107 ret = wc_ed448_sign_msg(data.ptr, data.len, signature->ptr, &len,
108 &this->key.ed448, NULL, 0);
109 #endif
110 }
111 return ret == 0;
112 }
113
114 METHOD(private_key_t, decrypt, bool,
115 private_private_key_t *this, encryption_scheme_t scheme,
116 chunk_t crypto, chunk_t *plain)
117 {
118 DBG1(DBG_LIB, "EdDSA private key decryption not implemented");
119 return FALSE;
120 }
121
122 METHOD(private_key_t, get_keysize, int,
123 private_private_key_t *this)
124 {
125 return wolfssl_ed_keysize(this->type);
126 }
127
128 METHOD(private_key_t, get_type, key_type_t,
129 private_private_key_t *this)
130 {
131 return this->type;
132 }
133
134 METHOD(private_key_t, get_public_key, public_key_t*,
135 private_private_key_t *this)
136 {
137 public_key_t *public;
138 chunk_t key;
139
140 if (!wolfssl_ed_public_key(&this->key, this->type, &key))
141 {
142 return NULL;
143 }
144 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, this->type,
145 BUILD_EDDSA_PUB, key, BUILD_END);
146 chunk_free(&key);
147 return public;
148 }
149
150 METHOD(private_key_t, get_fingerprint, bool,
151 private_private_key_t *this, cred_encoding_type_t type,
152 chunk_t *fingerprint)
153 {
154 return wolfssl_ed_fingerprint(&this->key, this->type, type, fingerprint);
155 }
156
157 METHOD(private_key_t, get_encoding, bool,
158 private_private_key_t *this, cred_encoding_type_t type, chunk_t *encoding)
159 {
160 int ret = -1;
161
162 switch (type)
163 {
164 case PRIVKEY_ASN1_DER:
165 case PRIVKEY_PEM:
166 {
167 bool success = TRUE;
168
169 /* +4 is for the two octet strings */
170 *encoding = chunk_empty;
171 if (this->type == KEY_ED25519)
172 {
173 #ifdef HAVE_ED25519
174 *encoding = chunk_alloc(ED25519_PRV_KEY_SIZE + 2 * MAX_SEQ_SZ +
175 MAX_VERSION_SZ + MAX_ALGO_SZ + 4);
176 ret = wc_Ed25519PrivateKeyToDer(&this->key.ed25519,
177 encoding->ptr, encoding->len);
178 #endif
179 }
180 else if (this->type == KEY_ED448)
181 {
182 #ifdef HAVE_ED448
183 *encoding = chunk_alloc(ED448_PRV_KEY_SIZE + 2 * MAX_SEQ_SZ +
184 MAX_VERSION_SZ + MAX_ALGO_SZ + 4);
185 ret = wc_Ed448PrivateKeyToDer(&this->key.ed448, encoding->ptr,
186 encoding->len);
187 #endif
188 }
189 if (ret < 0)
190 {
191 chunk_free(encoding);
192 return FALSE;
193 }
194 encoding->len = ret;
195
196 if (type == PRIVKEY_PEM)
197 {
198 chunk_t asn1_encoding = *encoding;
199
200 success = lib->encoding->encode(lib->encoding, PRIVKEY_PEM,
201 NULL, encoding, CRED_PART_EDDSA_PRIV_ASN1_DER,
202 asn1_encoding, CRED_PART_END);
203 chunk_clear(&asn1_encoding);
204 }
205 return success;
206 }
207 default:
208 return FALSE;
209 }
210 }
211
212 METHOD(private_key_t, get_ref, private_key_t*,
213 private_private_key_t *this)
214 {
215 ref_get(&this->ref);
216 return &this->public;
217 }
218
219 METHOD(private_key_t, destroy, void,
220 private_private_key_t *this)
221 {
222 if (ref_put(&this->ref))
223 {
224 lib->encoding->clear_cache(lib->encoding, &this->key);
225 wolfssl_ed_destroy(&this->key, this->type);
226 free(this);
227 }
228 }
229
230 /**
231 * Internal generic constructor
232 */
233 static private_private_key_t *create_internal(key_type_t type)
234 {
235 private_private_key_t *this;
236
237 INIT(this,
238 .public = {
239 .get_type = _get_type,
240 .sign = _sign,
241 .decrypt = _decrypt,
242 .get_keysize = _get_keysize,
243 .get_public_key = _get_public_key,
244 .equals = private_key_equals,
245 .belongs_to = private_key_belongs_to,
246 .get_fingerprint = _get_fingerprint,
247 .has_fingerprint = private_key_has_fingerprint,
248 .get_encoding = _get_encoding,
249 .get_ref = _get_ref,
250 .destroy = _destroy,
251 },
252 .type = type,
253 .ref = 1,
254 );
255
256 if (!wolfssl_ed_create(&this->key, type))
257 {
258 free(this);
259 this = NULL;
260 }
261 return this;
262 }
263
264 /*
265 * Described in header
266 */
267 private_key_t *wolfssl_ed_private_key_gen(key_type_t type, va_list args)
268 {
269 private_private_key_t *this;
270 WC_RNG rng;
271 int ret = -1;
272
273 while (TRUE)
274 {
275 switch (va_arg(args, builder_part_t))
276 {
277 case BUILD_KEY_SIZE:
278 /* just ignore the key size */
279 va_arg(args, u_int);
280 continue;
281 case BUILD_END:
282 break;
283 default:
284 return NULL;
285 }
286 break;
287 }
288
289 this = create_internal(type);
290 if (!this)
291 {
292 return NULL;
293 }
294
295 if (wc_InitRng(&rng) != 0)
296 {
297 DBG1(DBG_LIB, "initializing random failed");
298 destroy(this);
299 return NULL;
300 }
301
302 if (type == KEY_ED25519)
303 {
304 #ifdef HAVE_ED25519
305 ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &this->key.ed25519);
306 #endif
307 }
308 else if (type == KEY_ED448)
309 {
310 #ifdef HAVE_ED448
311 ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &this->key.ed448);
312 #endif
313 }
314 wc_FreeRng(&rng);
315
316 if (ret < 0)
317 {
318 DBG1(DBG_LIB, "generating %N key failed", key_type_names, type);
319 destroy(this);
320 return NULL;
321 }
322 return &this->public;
323 }
324
325 /**
326 * Fix the internal state if only the private key is set
327 */
328 static int set_public_key(private_private_key_t *this)
329 {
330 int ret = 0;
331
332 if (this->type == KEY_ED25519)
333 {
334 #ifdef HAVE_ED25519
335 if (!this->key.ed25519.pubKeySet)
336 {
337 ret = wc_ed25519_make_public(&this->key.ed25519,
338 this->key.ed25519.p, ED25519_PUB_KEY_SIZE);
339 if (ret == 0)
340 {
341 /* put public key after private key in the same buffer */
342 memmove(this->key.ed25519.k + ED25519_KEY_SIZE,
343 this->key.ed25519.p, ED25519_PUB_KEY_SIZE);
344 this->key.ed25519.pubKeySet = 1;
345 }
346 }
347 #endif
348 }
349 else if (this->type == KEY_ED448)
350 {
351 #ifdef HAVE_ED448
352 if (!this->key.ed448.pubKeySet)
353 {
354 ret = wc_ed448_make_public(&this->key.ed448, this->key.ed448.p,
355 ED448_PUB_KEY_SIZE);
356 if (ret == 0)
357 {
358 /* put public key after private key in the same buffer */
359 memmove(this->key.ed448.k + ED448_KEY_SIZE,
360 this->key.ed448.p, ED448_PUB_KEY_SIZE);
361 this->key.ed448.pubKeySet = 1;
362 }
363 }
364 #endif
365 }
366 return ret;
367 }
368
369 /*
370 * Described in header
371 */
372 private_key_t *wolfssl_ed_private_key_load(key_type_t type, va_list args)
373 {
374 private_private_key_t *this;
375 chunk_t blob = chunk_empty, priv = chunk_empty;
376 int idx;
377 int ret = -1;
378
379 while (TRUE)
380 {
381 switch (va_arg(args, builder_part_t))
382 {
383 case BUILD_BLOB_ASN1_DER:
384 blob = va_arg(args, chunk_t);
385 continue;
386 case BUILD_EDDSA_PRIV_ASN1_DER:
387 priv = va_arg(args, chunk_t);
388 continue;
389 case BUILD_END:
390 break;
391 default:
392 return NULL;
393 }
394 break;
395 }
396 this = create_internal(type);
397 if (!this)
398 {
399 return NULL;
400 }
401
402 if (type == KEY_ED25519)
403 {
404 #ifdef HAVE_ED25519
405 if (priv.len)
406 { /* check for ASN.1 wrapped key (Octet String == 0x04) */
407 if (priv.len == ED25519_KEY_SIZE + 2 &&
408 priv.ptr[0] == 0x04 && priv.ptr[1] == ED25519_KEY_SIZE)
409 {
410 priv = chunk_skip(priv, 2);
411 }
412 ret = wc_ed25519_import_private_only(priv.ptr, priv.len,
413 &this->key.ed25519);
414 }
415 else if (blob.len)
416 {
417 idx = 0;
418 ret = wc_Ed25519PrivateKeyDecode(blob.ptr, &idx, &this->key.ed25519,
419 blob.len);
420 }
421 #endif
422 }
423 else if (type == KEY_ED448)
424 {
425 #ifdef HAVE_ED448
426 if (priv.len)
427 { /* check for ASN.1 wrapped key (Octet String == 0x04) */
428 if (priv.len == ED448_KEY_SIZE + 2 &&
429 priv.ptr[0] == 0x04 && priv.ptr[1] == ED448_KEY_SIZE)
430 {
431 priv = chunk_skip(priv, 2);
432 }
433 ret = wc_ed448_import_private_only(priv.ptr, priv.len,
434 &this->key.ed448);
435 }
436 else if (blob.len)
437 {
438 idx = 0;
439 ret = wc_Ed448PrivateKeyDecode(blob.ptr, &idx, &this->key.ed448,
440 blob.len);
441 }
442 #endif
443 }
444
445 if (ret == 0)
446 {
447 ret = set_public_key(this);
448 }
449 if (ret != 0)
450 {
451 destroy(this);
452 return NULL;
453 }
454 return &this->public;
455 }
456
457 #endif /* HAVE_ED25519 */