typo
[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 #include <openssl/crypto.h>
70
71 #define OSSLTEST_LIB_NAME "OSSLTEST"
72 #include "e_ossltest_err.c"
73
74 /* Engine Id and Name */
75 static const char *engine_ossltest_id = "ossltest";
76 static const char *engine_ossltest_name = "OpenSSL Test engine support";
77
78
79 /* Engine Lifetime functions */
80 static int ossltest_destroy(ENGINE *e);
81 static int ossltest_init(ENGINE *e);
82 static int ossltest_finish(ENGINE *e);
83 void ENGINE_load_ossltest(void);
84
85
86 /* Set up digests */
87 static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
88                           const int **nids, int nid);
89
90 /* MD5 */
91 static int digest_md5_init(EVP_MD_CTX *ctx);
92 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
93                              size_t count);
94 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md);
95
96 static EVP_MD *_hidden_md5_md = NULL;
97 static const EVP_MD *digest_md5(void)
98 {
99     if (_hidden_md5_md == NULL) {
100         EVP_MD *md;
101
102         if ((md = EVP_MD_meth_new(NID_md5, NID_md5WithRSAEncryption)) == NULL
103             || !EVP_MD_meth_set_result_size(md, MD5_DIGEST_LENGTH)
104             || !EVP_MD_meth_set_input_blocksize(md, MD5_CBLOCK)
105             || !EVP_MD_meth_set_app_datasize(md,
106                                              sizeof(EVP_MD *) + sizeof(MD5_CTX))
107             || !EVP_MD_meth_set_flags(md, 0)
108             || !EVP_MD_meth_set_init(md, digest_md5_init)
109             || !EVP_MD_meth_set_update(md, digest_md5_update)
110             || !EVP_MD_meth_set_final(md, digest_md5_final)) {
111             EVP_MD_meth_free(md);
112             md = NULL;
113         }
114         _hidden_md5_md = md;
115     }
116     return _hidden_md5_md;
117 }
118
119 /* SHA1 */
120 static int digest_sha1_init(EVP_MD_CTX *ctx);
121 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
122                               size_t count);
123 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
124
125 static EVP_MD *_hidden_sha1_md = NULL;
126 static const EVP_MD *digest_sha1(void)
127 {
128     if (_hidden_sha1_md == NULL) {
129         EVP_MD *md;
130
131         if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
132             || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
133             || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
134             || !EVP_MD_meth_set_app_datasize(md,
135                                              sizeof(EVP_MD *) + sizeof(SHA_CTX))
136             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
137             || !EVP_MD_meth_set_init(md, digest_sha1_init)
138             || !EVP_MD_meth_set_update(md, digest_sha1_update)
139             || !EVP_MD_meth_set_final(md, digest_sha1_final)) {
140             EVP_MD_meth_free(md);
141             md = NULL;
142         }
143         _hidden_sha1_md = md;
144     }
145     return _hidden_sha1_md;
146 }
147
148 /* SHA256 */
149 static int digest_sha256_init(EVP_MD_CTX *ctx);
150 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
151                                 size_t count);
152 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md);
153
154 static EVP_MD *_hidden_sha256_md = NULL;
155 static const EVP_MD *digest_sha256(void)
156 {
157     if (_hidden_sha256_md == NULL) {
158         EVP_MD *md;
159
160         if ((md = EVP_MD_meth_new(NID_sha256, NID_sha256WithRSAEncryption)) == NULL
161             || !EVP_MD_meth_set_result_size(md, SHA256_DIGEST_LENGTH)
162             || !EVP_MD_meth_set_input_blocksize(md, SHA256_CBLOCK)
163             || !EVP_MD_meth_set_app_datasize(md,
164                                              sizeof(EVP_MD *) + sizeof(SHA256_CTX))
165             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
166             || !EVP_MD_meth_set_init(md, digest_sha256_init)
167             || !EVP_MD_meth_set_update(md, digest_sha256_update)
168             || !EVP_MD_meth_set_final(md, digest_sha256_final)) {
169             EVP_MD_meth_free(md);
170             md = NULL;
171         }
172         _hidden_sha256_md = md;
173     }
174     return _hidden_sha256_md;
175 }
176
177 /* SHA384/SHA512 */
178 static int digest_sha384_init(EVP_MD_CTX *ctx);
179 static int digest_sha512_init(EVP_MD_CTX *ctx);
180 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
181                                 size_t count);
182 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md);
183 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md);
184
185 static EVP_MD *_hidden_sha384_md = NULL;
186 static const EVP_MD *digest_sha384(void)
187 {
188     if (_hidden_sha384_md == NULL) {
189         EVP_MD *md;
190
191         if ((md = EVP_MD_meth_new(NID_sha384, NID_sha384WithRSAEncryption)) == NULL
192             || !EVP_MD_meth_set_result_size(md, SHA384_DIGEST_LENGTH)
193             || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
194             || !EVP_MD_meth_set_app_datasize(md,
195                                              sizeof(EVP_MD *) + sizeof(SHA512_CTX))
196             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
197             || !EVP_MD_meth_set_init(md, digest_sha384_init)
198             || !EVP_MD_meth_set_update(md, digest_sha512_update)
199             || !EVP_MD_meth_set_final(md, digest_sha384_final)) {
200             EVP_MD_meth_free(md);
201             md = NULL;
202         }
203         _hidden_sha384_md = md;
204     }
205     return _hidden_sha384_md;
206 }
207 static EVP_MD *_hidden_sha512_md = NULL;
208 static const EVP_MD *digest_sha512(void)
209 {
210     if (_hidden_sha512_md == NULL) {
211         EVP_MD *md;
212
213         if ((md = EVP_MD_meth_new(NID_sha512, NID_sha512WithRSAEncryption)) == NULL
214             || !EVP_MD_meth_set_result_size(md, SHA512_DIGEST_LENGTH)
215             || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
216             || !EVP_MD_meth_set_app_datasize(md,
217                                              sizeof(EVP_MD *) + sizeof(SHA512_CTX))
218             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
219             || !EVP_MD_meth_set_init(md, digest_sha512_init)
220             || !EVP_MD_meth_set_update(md, digest_sha512_update)
221             || !EVP_MD_meth_set_final(md, digest_sha512_final)) {
222             EVP_MD_meth_free(md);
223             md = NULL;
224         }
225         _hidden_sha512_md = md;
226     }
227     return _hidden_sha512_md;
228 }
229 static void destroy_digests(void)
230 {
231     EVP_MD_meth_free(_hidden_md5_md);
232     _hidden_md5_md = NULL;
233     EVP_MD_meth_free(_hidden_sha1_md);
234     _hidden_sha1_md = NULL;
235     EVP_MD_meth_free(_hidden_sha256_md);
236     _hidden_sha256_md = NULL;
237     EVP_MD_meth_free(_hidden_sha384_md);
238     _hidden_sha384_md = NULL;
239     EVP_MD_meth_free(_hidden_sha512_md);
240     _hidden_sha512_md = NULL;
241 }
242 static int ossltest_digest_nids(const int **nids)
243 {
244     static int digest_nids[6] = { 0, 0, 0, 0, 0, 0 };
245     static int pos = 0;
246     static int init = 0;
247
248     if (!init) {
249         const EVP_MD *md;
250         if ((md = digest_md5()) != NULL)
251             digest_nids[pos++] = EVP_MD_type(md);
252         if ((md = digest_sha1()) != NULL)
253             digest_nids[pos++] = EVP_MD_type(md);
254         if ((md = digest_sha256()) != NULL)
255             digest_nids[pos++] = EVP_MD_type(md);
256         if ((md = digest_sha384()) != NULL)
257             digest_nids[pos++] = EVP_MD_type(md);
258         if ((md = digest_sha512()) != NULL)
259             digest_nids[pos++] = EVP_MD_type(md);
260         digest_nids[pos] = 0;
261         init = 1;
262     }
263     *nids = digest_nids;
264     return pos;
265 }
266
267 /* Setup ciphers */
268 static int ossltest_ciphers(ENGINE *, const EVP_CIPHER **,
269                             const int **, int);
270
271 static int ossltest_cipher_nids[] = {
272     NID_aes_128_cbc, 0
273 };
274
275 /* AES128 */
276
277 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
278                              const unsigned char *iv, int enc);
279 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
280                                const unsigned char *in, size_t inl);
281
282 static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
283 static const EVP_CIPHER *ossltest_aes_128_cbc(void)
284 {
285     if (_hidden_aes_128_cbc == NULL
286         && ((_hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
287                                                        16 /* block size */,
288                                                        16 /* key len */)) == NULL
289             || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
290             || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
291                                           EVP_CIPH_FLAG_DEFAULT_ASN1
292                                           | EVP_CIPH_CBC_MODE)
293             || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
294                                          ossltest_aes128_init_key)
295             || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
296                                               ossltest_aes128_cbc_cipher)
297             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
298                                                   EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc())))) {
299         EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
300         _hidden_aes_128_cbc = NULL;
301     }
302     return _hidden_aes_128_cbc;
303 }
304 static void destroy_ciphers(void)
305 {
306     EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
307     _hidden_aes_128_cbc = NULL;
308 }
309
310 static int bind_ossltest(ENGINE *e)
311 {
312     /* Ensure the ossltest error handling is set up */
313     ERR_load_OSSLTEST_strings();
314
315     if (!ENGINE_set_id(e, engine_ossltest_id)
316         || !ENGINE_set_name(e, engine_ossltest_name)
317         || !ENGINE_set_digests(e, ossltest_digests)
318         || !ENGINE_set_ciphers(e, ossltest_ciphers)
319         || !ENGINE_set_destroy_function(e, ossltest_destroy)
320         || !ENGINE_set_init_function(e, ossltest_init)
321         || !ENGINE_set_finish_function(e, ossltest_finish)) {
322         OSSLTESTerr(OSSLTEST_F_BIND_OSSLTEST, OSSLTEST_R_INIT_FAILED);
323         return 0;
324     }
325
326     return 1;
327 }
328
329 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
330 static int bind_helper(ENGINE *e, const char *id)
331 {
332     if (id && (strcmp(id, engine_ossltest_id) != 0))
333         return 0;
334     if (!bind_ossltest(e))
335         return 0;
336     return 1;
337 }
338
339 IMPLEMENT_DYNAMIC_CHECK_FN()
340     IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
341 #endif
342
343 static ENGINE *engine_ossltest(void)
344 {
345     ENGINE *ret = ENGINE_new();
346     if (ret == NULL)
347         return NULL;
348     if (!bind_ossltest(ret)) {
349         ENGINE_free(ret);
350         return NULL;
351     }
352     return ret;
353 }
354
355 void ENGINE_load_ossltest(void)
356 {
357     /* Copied from eng_[openssl|dyn].c */
358     ENGINE *toadd = engine_ossltest();
359     if (!toadd)
360         return;
361     ENGINE_add(toadd);
362     ENGINE_free(toadd);
363     ERR_clear_error();
364 }
365
366
367 static int ossltest_init(ENGINE *e)
368 {
369     return 1;
370 }
371
372
373 static int ossltest_finish(ENGINE *e)
374 {
375     return 1;
376 }
377
378
379 static int ossltest_destroy(ENGINE *e)
380 {
381     destroy_digests();
382     destroy_ciphers();
383     ERR_unload_OSSLTEST_strings();
384     return 1;
385 }
386
387 static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
388                           const int **nids, int nid)
389 {
390     int ok = 1;
391     if (!digest) {
392         /* We are returning a list of supported nids */
393         return ossltest_digest_nids(nids);
394     }
395     /* We are being asked for a specific digest */
396     switch (nid) {
397     case NID_md5:
398         *digest = digest_md5();
399         break;
400     case NID_sha1:
401         *digest = digest_sha1();
402         break;
403     case NID_sha256:
404         *digest = digest_sha256();
405         break;
406     case NID_sha384:
407         *digest = digest_sha384();
408         break;
409     case NID_sha512:
410         *digest = digest_sha512();
411         break;
412     default:
413         ok = 0;
414         *digest = NULL;
415         break;
416     }
417     return ok;
418 }
419
420 static int ossltest_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
421                           const int **nids, int nid)
422 {
423     int ok = 1;
424     if (!cipher) {
425         /* We are returning a list of supported nids */
426         *nids = ossltest_cipher_nids;
427         return (sizeof(ossltest_cipher_nids) - 1)
428                / sizeof(ossltest_cipher_nids[0]);
429     }
430     /* We are being asked for a specific cipher */
431     switch (nid) {
432     case NID_aes_128_cbc:
433         *cipher = ossltest_aes_128_cbc();
434         break;
435     default:
436         ok = 0;
437         *cipher = NULL;
438         break;
439     }
440     return ok;
441 }
442
443 static void fill_known_data(unsigned char *md, unsigned int len)
444 {
445     unsigned int i;
446
447     for (i=0; i<len; i++) {
448         md[i] = (unsigned char)(i & 0xff);
449     }
450 }
451
452 /*
453  * MD5 implementation. We go through the motions of doing MD5 by deferring to
454  * the standard implementation. Then we overwrite the result with a will defined
455  * value, so that all "MD5" digests using the test engine always end up with
456  * the same value.
457  */
458 #undef data
459 #define data(ctx) ((MD5_CTX *)EVP_MD_CTX_md_data(ctx))
460 static int digest_md5_init(EVP_MD_CTX *ctx)
461 {
462     return MD5_Init(data(ctx));
463 }
464
465 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
466                              size_t count)
467 {
468     return MD5_Update(data(ctx), data, (size_t)count);
469 }
470
471 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
472 {
473     int ret;
474     ret = MD5_Final(md, data(ctx));
475
476     if (ret > 0) {
477         fill_known_data(md, MD5_DIGEST_LENGTH);
478     }
479     return ret;
480 }
481
482 /*
483  * SHA1 implementation.
484  */
485 #undef data
486 #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
487 static int digest_sha1_init(EVP_MD_CTX *ctx)
488 {
489     return SHA1_Init(data(ctx));
490 }
491
492 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
493                               size_t count)
494 {
495     return SHA1_Update(data(ctx), data, (size_t)count);
496 }
497
498 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
499 {
500     int ret;
501     ret = SHA1_Final(md, data(ctx));
502
503     if (ret > 0) {
504         fill_known_data(md, SHA_DIGEST_LENGTH);
505     }
506     return ret;
507 }
508
509 /*
510  * SHA256 implementation.
511  */
512 #undef data
513 #define data(ctx) ((SHA256_CTX *)EVP_MD_CTX_md_data(ctx))
514 static int digest_sha256_init(EVP_MD_CTX *ctx)
515 {
516     return SHA256_Init(data(ctx));
517 }
518
519 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
520                                 size_t count)
521 {
522     return SHA256_Update(data(ctx), data, (size_t)count);
523 }
524
525 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md)
526 {
527     int ret;
528     ret = SHA256_Final(md, data(ctx));
529
530     if (ret > 0) {
531         fill_known_data(md, SHA256_DIGEST_LENGTH);
532     }
533     return ret;
534 }
535
536 /*
537  * SHA384/512 implementation.
538  */
539 #undef data
540 #define data(ctx) ((SHA512_CTX *)EVP_MD_CTX_md_data(ctx))
541 static int digest_sha384_init(EVP_MD_CTX *ctx)
542 {
543     return SHA384_Init(data(ctx));
544 }
545
546 static int digest_sha512_init(EVP_MD_CTX *ctx)
547 {
548     return SHA512_Init(data(ctx));
549 }
550
551 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
552                                 size_t count)
553 {
554     return SHA512_Update(data(ctx), data, (size_t)count);
555 }
556
557 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md)
558 {
559     int ret;
560     /* Actually uses SHA512_Final! */
561     ret = SHA512_Final(md, data(ctx));
562
563     if (ret > 0) {
564         fill_known_data(md, SHA384_DIGEST_LENGTH);
565     }
566     return ret;
567 }
568
569 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md)
570 {
571     int ret;
572     ret = SHA512_Final(md, data(ctx));
573
574     if (ret > 0) {
575         fill_known_data(md, SHA512_DIGEST_LENGTH);
576     }
577     return ret;
578 }
579
580 /*
581  * AES128 Implementation
582  */
583
584 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
585                              const unsigned char *iv, int enc)
586 {
587     return EVP_CIPHER_meth_get_init(EVP_aes_128_cbc()) (ctx, key, iv, enc);
588 }
589
590 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
591                                const unsigned char *in, size_t inl)
592 {
593     unsigned char *tmpbuf;
594     int ret;
595
596     tmpbuf = OPENSSL_malloc(inl);
597     if (tmpbuf == NULL)
598         return -1;
599
600     /* Remember what we were asked to encrypt */
601     memcpy(tmpbuf, in, inl);
602
603     /* Go through the motions of encrypting it */
604     ret = EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc())(ctx, out, in, inl);
605
606     /* Throw it all away and just use the plaintext as the output */
607     memcpy(out, tmpbuf, inl);
608     OPENSSL_free(tmpbuf);
609
610     return ret;
611 }