| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-02-18 22:57:13 +08:00
										 |  |  |  * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2020-04-27 12:59:50 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "apps.h"
 | 
					
						
							|  |  |  | #include "progs.h"
 | 
					
						
							|  |  |  | #include <openssl/bio.h>
 | 
					
						
							|  |  |  | #include <openssl/err.h>
 | 
					
						
							|  |  |  | #include <openssl/evp.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-02 23:11:53 +08:00
										 |  |  | #include <openssl/params.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-07 13:48:27 +08:00
										 |  |  | #include <openssl/core_names.h>
 | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #undef BUFSIZE
 | 
					
						
							|  |  |  | #define BUFSIZE 1024*8
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef enum OPTION_choice { | 
					
						
							| 
									
										
										
										
											2021-05-01 21:29:00 +08:00
										 |  |  |     OPT_COMMON, | 
					
						
							| 
									
										
										
										
											2020-02-25 12:29:30 +08:00
										 |  |  |     OPT_MACOPT, OPT_BIN, OPT_IN, OPT_OUT, | 
					
						
							| 
									
										
										
										
											2021-05-07 13:48:27 +08:00
										 |  |  |     OPT_CIPHER, OPT_DIGEST, | 
					
						
							| 
									
										
										
										
											2020-02-25 12:29:30 +08:00
										 |  |  |     OPT_PROV_ENUM | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  | } OPTION_CHOICE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const OPTIONS mac_options[] = { | 
					
						
							|  |  |  |     {OPT_HELP_STR, 1, '-', "Usage: %s [options] mac_name\n"}, | 
					
						
							| 
									
										
										
										
											2019-11-08 04:08:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     OPT_SECTION("General"), | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |     {"help", OPT_HELP, '-', "Display this summary"}, | 
					
						
							| 
									
										
										
										
											2019-09-20 09:33:17 +08:00
										 |  |  |     {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form"}, | 
					
						
							| 
									
										
										
										
											2021-05-07 13:48:27 +08:00
										 |  |  |     {"cipher", OPT_CIPHER, 's', "Cipher"}, | 
					
						
							|  |  |  |     {"digest", OPT_DIGEST, 's', "Digest"}, | 
					
						
							| 
									
										
										
										
											2019-09-20 09:33:17 +08:00
										 |  |  |     {OPT_MORE_STR, 1, '-', "See 'PARAMETER NAMES' in the EVP_MAC_ docs"}, | 
					
						
							| 
									
										
										
										
											2019-11-08 04:08:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     OPT_SECTION("Input"), | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |     {"in", OPT_IN, '<', "Input file to MAC (default is stdin)"}, | 
					
						
							| 
									
										
										
										
											2019-11-08 04:08:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     OPT_SECTION("Output"), | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |     {"out", OPT_OUT, '>', "Output to filename rather than stdout"}, | 
					
						
							| 
									
										
										
										
											2019-09-20 09:33:17 +08:00
										 |  |  |     {"binary", OPT_BIN, '-', | 
					
						
							|  |  |  |         "Output in binary format (default is hexadecimal)"}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-25 12:29:30 +08:00
										 |  |  |     OPT_PROV_OPTIONS, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-20 09:33:17 +08:00
										 |  |  |     OPT_PARAMETERS(), | 
					
						
							|  |  |  |     {"mac_name", 0, 0, "MAC algorithm"}, | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |     {NULL} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-07 13:48:27 +08:00
										 |  |  | static char *alloc_mac_algorithm_name(STACK_OF(OPENSSL_STRING) **optp, | 
					
						
							|  |  |  |                                       const char *name, const char *arg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     size_t len = strlen(name) + strlen(arg) + 2; | 
					
						
							|  |  |  |     char *res = app_malloc(len, "algorithm name"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (*optp == NULL) | 
					
						
							|  |  |  |         *optp = sk_OPENSSL_STRING_new_null(); | 
					
						
							|  |  |  |     if (*optp == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     BIO_snprintf(res, len, "%s:%s", name, arg); | 
					
						
							|  |  |  |     if (sk_OPENSSL_STRING_push(*optp, res)) | 
					
						
							|  |  |  |         return res; | 
					
						
							|  |  |  |     OPENSSL_free(res); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  | int mac_main(int argc, char **argv) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int ret = 1; | 
					
						
							|  |  |  |     char *prog; | 
					
						
							| 
									
										
										
										
											2019-06-02 23:11:53 +08:00
										 |  |  |     EVP_MAC *mac = NULL; | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |     OPTION_CHOICE o; | 
					
						
							|  |  |  |     EVP_MAC_CTX *ctx = NULL; | 
					
						
							|  |  |  |     STACK_OF(OPENSSL_STRING) *opts = NULL; | 
					
						
							|  |  |  |     unsigned char *buf = NULL; | 
					
						
							|  |  |  |     size_t len; | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  |     BIO *in = NULL, *out = NULL; | 
					
						
							|  |  |  |     const char *outfile = NULL; | 
					
						
							|  |  |  |     const char *infile = NULL; | 
					
						
							|  |  |  |     int out_bin = 0; | 
					
						
							|  |  |  |     int inform = FORMAT_BINARY; | 
					
						
							| 
									
										
										
										
											2021-05-07 13:48:27 +08:00
										 |  |  |     char *digest = NULL, *cipher = NULL; | 
					
						
							| 
									
										
										
										
											2021-02-25 11:47:36 +08:00
										 |  |  |     OSSL_PARAM *params = NULL; | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     prog = opt_init(argc, argv, mac_options); | 
					
						
							|  |  |  |     buf = app_malloc(BUFSIZE, "I/O buffer"); | 
					
						
							|  |  |  |     while ((o = opt_next()) != OPT_EOF) { | 
					
						
							|  |  |  |         switch (o) { | 
					
						
							| 
									
										
										
										
											2019-03-24 17:11:42 +08:00
										 |  |  |         default: | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  | opthelp: | 
					
						
							|  |  |  |             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | 
					
						
							|  |  |  |             goto err; | 
					
						
							|  |  |  |         case OPT_HELP: | 
					
						
							|  |  |  |             opt_help(mac_options); | 
					
						
							|  |  |  |             ret = 0; | 
					
						
							|  |  |  |             goto err; | 
					
						
							|  |  |  |         case OPT_BIN: | 
					
						
							|  |  |  |             out_bin = 1; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case OPT_IN: | 
					
						
							|  |  |  |             infile = opt_arg(); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case OPT_OUT: | 
					
						
							|  |  |  |             outfile = opt_arg(); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case OPT_MACOPT: | 
					
						
							|  |  |  |             if (opts == NULL) | 
					
						
							|  |  |  |                 opts = sk_OPENSSL_STRING_new_null(); | 
					
						
							|  |  |  |             if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg())) | 
					
						
							|  |  |  |                 goto opthelp; | 
					
						
							|  |  |  |             break; | 
					
						
							| 
									
										
										
										
											2021-05-07 13:48:27 +08:00
										 |  |  |         case OPT_CIPHER: | 
					
						
							|  |  |  |             OPENSSL_free(cipher); | 
					
						
							|  |  |  |             cipher = alloc_mac_algorithm_name(&opts, "cipher", opt_arg()); | 
					
						
							|  |  |  |             if (cipher == NULL) | 
					
						
							|  |  |  |                 goto opthelp; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case OPT_DIGEST: | 
					
						
							|  |  |  |             OPENSSL_free(digest); | 
					
						
							|  |  |  |             digest = alloc_mac_algorithm_name(&opts, "digest", opt_arg()); | 
					
						
							|  |  |  |             if (digest == NULL) | 
					
						
							|  |  |  |                 goto opthelp; | 
					
						
							|  |  |  |             break; | 
					
						
							| 
									
										
										
										
											2020-02-25 12:29:30 +08:00
										 |  |  |         case OPT_PROV_CASES: | 
					
						
							|  |  |  |             if (!opt_provider(o)) | 
					
						
							|  |  |  |                 goto err; | 
					
						
							|  |  |  |             break; | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-11-29 05:12:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* One argument, the MAC name. */ | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |     argc = opt_num_rest(); | 
					
						
							|  |  |  |     argv = opt_rest(); | 
					
						
							| 
									
										
										
										
											2020-11-29 05:12:58 +08:00
										 |  |  |     if (argc != 1) | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |         goto opthelp; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-27 04:32:14 +08:00
										 |  |  |     mac = EVP_MAC_fetch(app_get0_libctx(), argv[0], app_get0_propq()); | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |     if (mac == NULL) { | 
					
						
							|  |  |  |         BIO_printf(bio_err, "Invalid MAC name %s\n", argv[0]); | 
					
						
							|  |  |  |         goto opthelp; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-18 16:26:22 +08:00
										 |  |  |     ctx = EVP_MAC_CTX_new(mac); | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |     if (ctx == NULL) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (opts != NULL) { | 
					
						
							| 
									
										
										
										
											2019-06-02 23:11:53 +08:00
										 |  |  |         int ok = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-25 11:47:36 +08:00
										 |  |  |         params = app_params_new_from_opts(opts, | 
					
						
							|  |  |  |                                           EVP_MAC_settable_ctx_params(mac)); | 
					
						
							| 
									
										
										
										
											2019-08-24 16:56:34 +08:00
										 |  |  |         if (params == NULL) | 
					
						
							|  |  |  |             goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-18 16:26:22 +08:00
										 |  |  |         if (!EVP_MAC_CTX_set_params(ctx, params)) { | 
					
						
							| 
									
										
										
										
											2019-08-24 16:56:34 +08:00
										 |  |  |             BIO_printf(bio_err, "MAC parameter error\n"); | 
					
						
							|  |  |  |             ERR_print_errors(bio_err); | 
					
						
							|  |  |  |             ok = 0; | 
					
						
							| 
									
										
										
										
											2019-06-02 23:11:53 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-08-24 16:56:34 +08:00
										 |  |  |         app_params_free(params); | 
					
						
							| 
									
										
										
										
											2019-06-02 23:11:53 +08:00
										 |  |  |         if (!ok) | 
					
						
							|  |  |  |             goto err; | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Use text mode for stdin */ | 
					
						
							|  |  |  |     if (infile == NULL || strcmp(infile, "-") == 0) | 
					
						
							|  |  |  |         inform = FORMAT_TEXT; | 
					
						
							|  |  |  |     in = bio_open_default(infile, 'r', inform); | 
					
						
							|  |  |  |     if (in == NULL) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT); | 
					
						
							|  |  |  |     if (out == NULL) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-25 11:47:36 +08:00
										 |  |  |     if (!EVP_MAC_init(ctx, NULL, 0, NULL)) { | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |         BIO_printf(bio_err, "EVP_MAC_Init failed\n"); | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-07 12:19:06 +08:00
										 |  |  |     while (BIO_pending(in) || !BIO_eof(in)) { | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |         i = BIO_read(in, (char *)buf, BUFSIZE); | 
					
						
							|  |  |  |         if (i < 0) { | 
					
						
							|  |  |  |             BIO_printf(bio_err, "Read Error in '%s'\n", infile); | 
					
						
							| 
									
										
										
										
											2021-05-07 12:19:06 +08:00
										 |  |  |             ERR_print_errors(bio_err); | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |             goto err; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (i == 0) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         if (!EVP_MAC_update(ctx, buf, i)) { | 
					
						
							|  |  |  |             BIO_printf(bio_err, "EVP_MAC_update failed\n"); | 
					
						
							|  |  |  |             goto err; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-02 23:11:53 +08:00
										 |  |  |     if (!EVP_MAC_final(ctx, NULL, &len, 0)) { | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |         BIO_printf(bio_err, "EVP_MAC_final failed\n"); | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (len > BUFSIZE) { | 
					
						
							|  |  |  |         BIO_printf(bio_err, "output len is too large\n"); | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-02 23:11:53 +08:00
										 |  |  |     if (!EVP_MAC_final(ctx, buf, &len, BUFSIZE)) { | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |         BIO_printf(bio_err, "EVP_MAC_final failed\n"); | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (out_bin) { | 
					
						
							|  |  |  |         BIO_write(out, buf, len); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         for (i = 0; i < (int)len; ++i) | 
					
						
							|  |  |  |             BIO_printf(out, "%02X", buf[i]); | 
					
						
							|  |  |  |         if (outfile == NULL) | 
					
						
							|  |  |  |             BIO_printf(out,"\n"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ret = 0; | 
					
						
							|  |  |  | err: | 
					
						
							|  |  |  |     if (ret != 0) | 
					
						
							|  |  |  |         ERR_print_errors(bio_err); | 
					
						
							|  |  |  |     OPENSSL_clear_free(buf, BUFSIZE); | 
					
						
							| 
									
										
										
										
											2021-05-07 13:48:27 +08:00
										 |  |  |     OPENSSL_free(cipher); | 
					
						
							|  |  |  |     OPENSSL_free(digest); | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |     sk_OPENSSL_STRING_free(opts); | 
					
						
							|  |  |  |     BIO_free(in); | 
					
						
							|  |  |  |     BIO_free(out); | 
					
						
							| 
									
										
										
										
											2020-06-18 16:26:22 +08:00
										 |  |  |     EVP_MAC_CTX_free(ctx); | 
					
						
							| 
									
										
										
										
											2019-06-02 23:11:53 +08:00
										 |  |  |     EVP_MAC_free(mac); | 
					
						
							| 
									
										
										
										
											2018-11-20 08:45:44 +08:00
										 |  |  |     return ret; | 
					
						
							|  |  |  | } |