]> git.ipfire.org Git - thirdparty/openssl.git/blob - engines/e_ossltest.c
Remove /* foo.c */ comments
[thirdparty/openssl.git] / engines / e_ossltest.c
1 /*
2 * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
3 */
4 /* ====================================================================
5 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
23 *
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * licensing@OpenSSL.org.
28 *
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
32 *
33 * 6. Redistributions of any form whatsoever must retain the following
34 * acknowledgment:
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
51 */
52
53 /*
54 * This is the OSSLTEST engine. It provides deliberately crippled digest
55 * implementations for test purposes. It is highly insecure and must NOT be
56 * used for any purpose except testing
57 */
58
59 #include <stdio.h>
60 #include <string.h>
61
62 #include <openssl/engine.h>
63 #include <openssl/sha.h>
64 #include <openssl/md5.h>
65 #include <openssl/rsa.h>
66 #include <openssl/evp.h>
67 #include <openssl/modes.h>
68 #include <openssl/aes.h>
69
70 #define OSSLTEST_LIB_NAME "OSSLTEST"
71 #include "e_ossltest_err.c"
72
73 /* Engine Id and Name */
74 static const char *engine_ossltest_id = "ossltest";
75 static const char *engine_ossltest_name = "OpenSSL Test engine support";
76
77
78 /* Engine Lifetime functions */
79 static int ossltest_destroy(ENGINE *e);
80 static int ossltest_init(ENGINE *e);
81 static int ossltest_finish(ENGINE *e);
82 void ENGINE_load_ossltest(void);
83
84
85 /* Set up digests */
86 static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
87 const int **nids, int nid);
88
89 /* MD5 */
90 static int digest_md5_init(EVP_MD_CTX *ctx);
91 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
92 size_t count);
93 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md);
94
95 static EVP_MD *_hidden_md5_md = NULL;
96 static const EVP_MD *digest_md5(void)
97 {
98 if (_hidden_md5_md == NULL) {
99 EVP_MD *md;
100
101 if ((md = EVP_MD_meth_new(NID_md5, NID_md5WithRSAEncryption)) == NULL
102 || !EVP_MD_meth_set_result_size(md, MD5_DIGEST_LENGTH)
103 || !EVP_MD_meth_set_input_blocksize(md, MD5_CBLOCK)
104 || !EVP_MD_meth_set_app_datasize(md,
105 sizeof(EVP_MD *) + sizeof(MD5_CTX))
106 || !EVP_MD_meth_set_flags(md, 0)
107 || !EVP_MD_meth_set_init(md, digest_md5_init)
108 || !EVP_MD_meth_set_update(md, digest_md5_update)
109 || !EVP_MD_meth_set_final(md, digest_md5_final)) {
110 EVP_MD_meth_free(md);
111 md = NULL;
112 }
113 _hidden_md5_md = md;
114 }
115 return _hidden_md5_md;
116 }
117
118 /* SHA1 */
119 static int digest_sha1_init(EVP_MD_CTX *ctx);
120 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
121 size_t count);
122 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
123
124 static EVP_MD *_hidden_sha1_md = NULL;
125 static const EVP_MD *digest_sha1(void)
126 {
127 if (_hidden_sha1_md == NULL) {
128 EVP_MD *md;
129
130 if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
131 || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
132 || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
133 || !EVP_MD_meth_set_app_datasize(md,
134 sizeof(EVP_MD *) + sizeof(SHA_CTX))
135 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
136 || !EVP_MD_meth_set_init(md, digest_sha1_init)
137 || !EVP_MD_meth_set_update(md, digest_sha1_update)
138 || !EVP_MD_meth_set_final(md, digest_sha1_final)) {
139 EVP_MD_meth_free(md);
140 md = NULL;
141 }
142 _hidden_sha1_md = md;
143 }
144 return _hidden_sha1_md;
145 }
146
147 /* SHA256 */
148 static int digest_sha256_init(EVP_MD_CTX *ctx);
149 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
150 size_t count);
151 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md);
152
153 static EVP_MD *_hidden_sha256_md = NULL;
154 static const EVP_MD *digest_sha256(void)
155 {
156 if (_hidden_sha256_md == NULL) {
157 EVP_MD *md;
158
159 if ((md = EVP_MD_meth_new(NID_sha256, NID_sha256WithRSAEncryption)) == NULL
160 || !EVP_MD_meth_set_result_size(md, SHA256_DIGEST_LENGTH)
161 || !EVP_MD_meth_set_input_blocksize(md, SHA256_CBLOCK)
162 || !EVP_MD_meth_set_app_datasize(md,
163 sizeof(EVP_MD *) + sizeof(SHA256_CTX))
164 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
165 || !EVP_MD_meth_set_init(md, digest_sha256_init)
166 || !EVP_MD_meth_set_update(md, digest_sha256_update)
167 || !EVP_MD_meth_set_final(md, digest_sha256_final)) {
168 EVP_MD_meth_free(md);
169 md = NULL;
170 }
171 _hidden_sha256_md = md;
172 }
173 return _hidden_sha256_md;
174 }
175
176 /* SHA384/SHA512 */
177 static int digest_sha384_init(EVP_MD_CTX *ctx);
178 static int digest_sha512_init(EVP_MD_CTX *ctx);
179 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
180 size_t count);
181 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md);
182 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md);
183
184 static EVP_MD *_hidden_sha384_md = NULL;
185 static const EVP_MD *digest_sha384(void)
186 {
187 if (_hidden_sha384_md == NULL) {
188 EVP_MD *md;
189
190 if ((md = EVP_MD_meth_new(NID_sha384, NID_sha384WithRSAEncryption)) == NULL
191 || !EVP_MD_meth_set_result_size(md, SHA384_DIGEST_LENGTH)
192 || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
193 || !EVP_MD_meth_set_app_datasize(md,
194 sizeof(EVP_MD *) + sizeof(SHA512_CTX))
195 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
196 || !EVP_MD_meth_set_init(md, digest_sha384_init)
197 || !EVP_MD_meth_set_update(md, digest_sha512_update)
198 || !EVP_MD_meth_set_final(md, digest_sha384_final)) {
199 EVP_MD_meth_free(md);
200 md = NULL;
201 }
202 _hidden_sha384_md = md;
203 }
204 return _hidden_sha384_md;
205 }
206 static EVP_MD *_hidden_sha512_md = NULL;
207 static const EVP_MD *digest_sha512(void)
208 {
209 if (_hidden_sha512_md == NULL) {
210 EVP_MD *md;
211
212 if ((md = EVP_MD_meth_new(NID_sha512, NID_sha512WithRSAEncryption)) == NULL
213 || !EVP_MD_meth_set_result_size(md, SHA512_DIGEST_LENGTH)
214 || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
215 || !EVP_MD_meth_set_app_datasize(md,
216 sizeof(EVP_MD *) + sizeof(SHA512_CTX))
217 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
218 || !EVP_MD_meth_set_init(md, digest_sha512_init)
219 || !EVP_MD_meth_set_update(md, digest_sha512_update)
220 || !EVP_MD_meth_set_final(md, digest_sha512_final)) {
221 EVP_MD_meth_free(md);
222 md = NULL;
223 }
224 _hidden_sha512_md = md;
225 }
226 return _hidden_sha512_md;
227 }
228 static void destroy_digests(void)
229 {
230 EVP_MD_meth_free(_hidden_md5_md);
231 _hidden_md5_md = NULL;
232 EVP_MD_meth_free(_hidden_sha1_md);
233 _hidden_sha1_md = NULL;
234 EVP_MD_meth_free(_hidden_sha256_md);
235 _hidden_sha256_md = NULL;
236 EVP_MD_meth_free(_hidden_sha384_md);
237 _hidden_sha384_md = NULL;
238 EVP_MD_meth_free(_hidden_sha512_md);
239 _hidden_sha512_md = NULL;
240 }
241 static int ossltest_digest_nids(const int **nids)
242 {
243 static int digest_nids[6] = { 0, 0, 0, 0, 0, 0 };
244 static int pos = 0;
245 static int init = 0;
246
247 if (!init) {
248 const EVP_MD *md;
249 if ((md = digest_md5()) != NULL)
250 digest_nids[pos++] = EVP_MD_type(md);
251 if ((md = digest_sha1()) != NULL)
252 digest_nids[pos++] = EVP_MD_type(md);
253 if ((md = digest_sha256()) != NULL)
254 digest_nids[pos++] = EVP_MD_type(md);
255 if ((md = digest_sha384()) != NULL)
256 digest_nids[pos++] = EVP_MD_type(md);
257 if ((md = digest_sha512()) != NULL)
258 digest_nids[pos++] = EVP_MD_type(md);
259 digest_nids[pos] = 0;
260 init = 1;
261 }
262 *nids = digest_nids;
263 return pos;
264 }
265
266 /* Setup ciphers */
267 static int ossltest_ciphers(ENGINE *, const EVP_CIPHER **,
268 const int **, int);
269
270 static int ossltest_cipher_nids[] = {
271 NID_aes_128_cbc, 0
272 };
273
274 /* AES128 */
275
276 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
277 const unsigned char *iv, int enc);
278 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
279 const unsigned char *in, size_t inl);
280
281 static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
282 static const EVP_CIPHER *ossltest_aes_128_cbc(void)
283 {
284 if (_hidden_aes_128_cbc == NULL
285 && ((_hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
286 16 /* block size */,
287 16 /* key len */)) == NULL
288 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
289 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
290 EVP_CIPH_FLAG_DEFAULT_ASN1
291 | EVP_CIPH_CBC_MODE)
292 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
293 ossltest_aes128_init_key)
294 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
295 ossltest_aes128_cbc_cipher)
296 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
297 EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc())))) {
298 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
299 _hidden_aes_128_cbc = NULL;
300 }
301 return _hidden_aes_128_cbc;
302 }
303 static void destroy_ciphers(void)
304 {
305 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
306 _hidden_aes_128_cbc = NULL;
307 }
308
309 static int bind_ossltest(ENGINE *e)
310 {
311 /* Ensure the ossltest error handling is set up */
312 ERR_load_OSSLTEST_strings();
313
314 if (!ENGINE_set_id(e, engine_ossltest_id)
315 || !ENGINE_set_name(e, engine_ossltest_name)
316 || !ENGINE_set_digests(e, ossltest_digests)
317 || !ENGINE_set_ciphers(e, ossltest_ciphers)
318 || !ENGINE_set_destroy_function(e, ossltest_destroy)
319 || !ENGINE_set_init_function(e, ossltest_init)
320 || !ENGINE_set_finish_function(e, ossltest_finish)) {
321 OSSLTESTerr(OSSLTEST_F_BIND_OSSLTEST, OSSLTEST_R_INIT_FAILED);
322 return 0;
323 }
324
325 return 1;
326 }
327
328 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
329 static int bind_helper(ENGINE *e, const char *id)
330 {
331 if (id && (strcmp(id, engine_ossltest_id) != 0))
332 return 0;
333 if (!bind_ossltest(e))
334 return 0;
335 return 1;
336 }
337
338 IMPLEMENT_DYNAMIC_CHECK_FN()
339 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
340 #endif
341
342 static ENGINE *engine_ossltest(void)
343 {
344 ENGINE *ret = ENGINE_new();
345 if (ret == NULL)
346 return NULL;
347 if (!bind_ossltest(ret)) {
348 ENGINE_free(ret);
349 return NULL;
350 }
351 return ret;
352 }
353
354 void ENGINE_load_ossltest(void)
355 {
356 /* Copied from eng_[openssl|dyn].c */
357 ENGINE *toadd = engine_ossltest();
358 if (!toadd)
359 return;
360 ENGINE_add(toadd);
361 ENGINE_free(toadd);
362 ERR_clear_error();
363 }
364
365
366 static int ossltest_init(ENGINE *e)
367 {
368 return 1;
369 }
370
371
372 static int ossltest_finish(ENGINE *e)
373 {
374 return 1;
375 }
376
377
378 static int ossltest_destroy(ENGINE *e)
379 {
380 destroy_digests();
381 destroy_ciphers();
382 ERR_unload_OSSLTEST_strings();
383 return 1;
384 }
385
386 static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
387 const int **nids, int nid)
388 {
389 int ok = 1;
390 if (!digest) {
391 /* We are returning a list of supported nids */
392 return ossltest_digest_nids(nids);
393 }
394 /* We are being asked for a specific digest */
395 switch (nid) {
396 case NID_md5:
397 *digest = digest_md5();
398 break;
399 case NID_sha1:
400 *digest = digest_sha1();
401 break;
402 case NID_sha256:
403 *digest = digest_sha256();
404 break;
405 case NID_sha384:
406 *digest = digest_sha384();
407 break;
408 case NID_sha512:
409 *digest = digest_sha512();
410 break;
411 default:
412 ok = 0;
413 *digest = NULL;
414 break;
415 }
416 return ok;
417 }
418
419 static int ossltest_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
420 const int **nids, int nid)
421 {
422 int ok = 1;
423 if (!cipher) {
424 /* We are returning a list of supported nids */
425 *nids = ossltest_cipher_nids;
426 return (sizeof(ossltest_cipher_nids) - 1)
427 / sizeof(ossltest_cipher_nids[0]);
428 }
429 /* We are being asked for a specific cipher */
430 switch (nid) {
431 case NID_aes_128_cbc:
432 *cipher = ossltest_aes_128_cbc();
433 break;
434 default:
435 ok = 0;
436 *cipher = NULL;
437 break;
438 }
439 return ok;
440 }
441
442 static void fill_known_data(unsigned char *md, unsigned int len)
443 {
444 unsigned int i;
445
446 for (i=0; i<len; i++) {
447 md[i] = (unsigned char)(i & 0xff);
448 }
449 }
450
451 /*
452 * MD5 implementation. We go through the motions of doing MD5 by deferring to
453 * the standard implementation. Then we overwrite the result with a will defined
454 * value, so that all "MD5" digests using the test engine always end up with
455 * the same value.
456 */
457 #undef data
458 #define data(ctx) ((MD5_CTX *)EVP_MD_CTX_md_data(ctx))
459 static int digest_md5_init(EVP_MD_CTX *ctx)
460 {
461 return MD5_Init(data(ctx));
462 }
463
464 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
465 size_t count)
466 {
467 return MD5_Update(data(ctx), data, (size_t)count);
468 }
469
470 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
471 {
472 int ret;
473 ret = MD5_Final(md, data(ctx));
474
475 if (ret > 0) {
476 fill_known_data(md, MD5_DIGEST_LENGTH);
477 }
478 return ret;
479 }
480
481 /*
482 * SHA1 implementation.
483 */
484 #undef data
485 #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
486 static int digest_sha1_init(EVP_MD_CTX *ctx)
487 {
488 return SHA1_Init(data(ctx));
489 }
490
491 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
492 size_t count)
493 {
494 return SHA1_Update(data(ctx), data, (size_t)count);
495 }
496
497 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
498 {
499 int ret;
500 ret = SHA1_Final(md, data(ctx));
501
502 if (ret > 0) {
503 fill_known_data(md, SHA_DIGEST_LENGTH);
504 }
505 return ret;
506 }
507
508 /*
509 * SHA256 implementation.
510 */
511 #undef data
512 #define data(ctx) ((SHA256_CTX *)EVP_MD_CTX_md_data(ctx))
513 static int digest_sha256_init(EVP_MD_CTX *ctx)
514 {
515 return SHA256_Init(data(ctx));
516 }
517
518 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
519 size_t count)
520 {
521 return SHA256_Update(data(ctx), data, (size_t)count);
522 }
523
524 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md)
525 {
526 int ret;
527 ret = SHA256_Final(md, data(ctx));
528
529 if (ret > 0) {
530 fill_known_data(md, SHA256_DIGEST_LENGTH);
531 }
532 return ret;
533 }
534
535 /*
536 * SHA384/512 implementation.
537 */
538 #undef data
539 #define data(ctx) ((SHA512_CTX *)EVP_MD_CTX_md_data(ctx))
540 static int digest_sha384_init(EVP_MD_CTX *ctx)
541 {
542 return SHA384_Init(data(ctx));
543 }
544
545 static int digest_sha512_init(EVP_MD_CTX *ctx)
546 {
547 return SHA512_Init(data(ctx));
548 }
549
550 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
551 size_t count)
552 {
553 return SHA512_Update(data(ctx), data, (size_t)count);
554 }
555
556 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md)
557 {
558 int ret;
559 /* Actually uses SHA512_Final! */
560 ret = SHA512_Final(md, data(ctx));
561
562 if (ret > 0) {
563 fill_known_data(md, SHA384_DIGEST_LENGTH);
564 }
565 return ret;
566 }
567
568 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md)
569 {
570 int ret;
571 ret = SHA512_Final(md, data(ctx));
572
573 if (ret > 0) {
574 fill_known_data(md, SHA512_DIGEST_LENGTH);
575 }
576 return ret;
577 }
578
579 /*
580 * AES128 Implementation
581 */
582
583 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
584 const unsigned char *iv, int enc)
585 {
586 return EVP_CIPHER_meth_get_init(EVP_aes_128_cbc()) (ctx, key, iv, enc);
587 }
588
589 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
590 const unsigned char *in, size_t inl)
591 {
592 unsigned char *tmpbuf;
593 int ret;
594
595 tmpbuf = OPENSSL_malloc(inl);
596 if (tmpbuf == NULL)
597 return -1;
598
599 /* Remember what we were asked to encrypt */
600 memcpy(tmpbuf, in, inl);
601
602 /* Go through the motions of encrypting it */
603 ret = EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc())(ctx, out, in, inl);
604
605 /* Throw it all away and just use the plaintext as the output */
606 memcpy(out, tmpbuf, inl);
607 OPENSSL_free(tmpbuf);
608
609 return ret;
610 }