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