Fix typo in CONTRIBUTING.md
[openssl.git] / apps / mac.c
index 8220356e42e28651e010d829d488b55fe2f3f951..0a07b2ea4332968b9b86449111873d9118c6d1b8 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
  *
- * Licensed under the OpenSSL license (the "License").  You may not use
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
  * 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 <openssl/err.h>
 #include <openssl/evp.h>
 #include <openssl/params.h>
+#include <openssl/core_names.h>
 
 #undef BUFSIZE
 #define BUFSIZE 1024*8
 
 typedef enum OPTION_choice {
-    OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
-    OPT_MACOPT, OPT_BIN, OPT_IN, OPT_OUT
+    OPT_COMMON,
+    OPT_MACOPT, OPT_BIN, OPT_IN, OPT_OUT,
+    OPT_CIPHER, OPT_DIGEST,
+    OPT_PROV_ENUM
 } OPTION_CHOICE;
 
 const OPTIONS mac_options[] = {
     {OPT_HELP_STR, 1, '-', "Usage: %s [options] mac_name\n"},
-    {OPT_HELP_STR, 1, '-', "mac_name\t\t MAC algorithm (See list "
-                           "-mac-algorithms)"},
+
+    OPT_SECTION("General"),
     {"help", OPT_HELP, '-', "Display this summary"},
-    {"macopt", OPT_MACOPT, 's', "MAC algorithm control parameters in n:v form. "
-                                "See 'Supported Controls' in the EVP_MAC_ docs"},
+    {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form"},
+    {"cipher", OPT_CIPHER, 's', "Cipher"},
+    {"digest", OPT_DIGEST, 's', "Digest"},
+    {OPT_MORE_STR, 1, '-', "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
+
+    OPT_SECTION("Input"),
     {"in", OPT_IN, '<', "Input file to MAC (default is stdin)"},
+
+    OPT_SECTION("Output"),
     {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
-    {"binary", OPT_BIN, '-', "Output in binary format (Default is hexadecimal "
-                             "output)"},
+    {"binary", OPT_BIN, '-',
+        "Output in binary format (default is hexadecimal)"},
+
+    OPT_PROV_OPTIONS,
+
+    OPT_PARAMETERS(),
+    {"mac_name", 0, 0, "MAC algorithm"},
     {NULL}
 };
 
+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;
+
+    if (*optp == NULL)
+        *optp = sk_OPENSSL_STRING_new_null();
+    if (*optp == NULL)
+        return NULL;
+
+    res = app_malloc(len, "algorithm name");
+    BIO_snprintf(res, len, "%s:%s", name, arg);
+    if (sk_OPENSSL_STRING_push(*optp, res))
+        return res;
+    OPENSSL_free(res);
+    return NULL;
+}
+
 int mac_main(int argc, char **argv)
 {
     int ret = 1;
@@ -54,6 +87,8 @@ int mac_main(int argc, char **argv)
     const char *infile = NULL;
     int out_bin = 0;
     int inform = FORMAT_BINARY;
+    char *digest = NULL, *cipher = NULL;
+    OSSL_PARAM *params = NULL;
 
     prog = opt_init(argc, argv, mac_options);
     buf = app_malloc(BUFSIZE, "I/O buffer");
@@ -82,17 +117,31 @@ opthelp:
             if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
                 goto opthelp;
             break;
+        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;
+        case OPT_PROV_CASES:
+            if (!opt_provider(o))
+                goto err;
+            break;
         }
     }
-    argc = opt_num_rest();
-    argv = opt_rest();
 
-    if (argc != 1) {
-        BIO_printf(bio_err, "Invalid number of extra arguments\n");
+    /* One argument, the MAC name. */
+    if (!opt_check_rest_arg("MAC name"))
         goto opthelp;
-    }
+    argv = opt_rest();
 
-    mac = EVP_MAC_fetch(NULL, argv[0], NULL);
+    mac = EVP_MAC_fetch(app_get0_libctx(), argv[0], app_get0_propq());
     if (mac == NULL) {
         BIO_printf(bio_err, "Invalid MAC name %s\n", argv[0]);
         goto opthelp;
@@ -103,50 +152,23 @@ opthelp:
         goto err;
 
     if (opts != NULL) {
-        OSSL_PARAM *params =
-            OPENSSL_zalloc(sizeof(OSSL_PARAM)
-                           * (sk_OPENSSL_STRING_num(opts) + 1));
-        const OSSL_PARAM *paramdefs = EVP_MAC_CTX_settable_params(mac);
-        size_t params_n;
         int ok = 1;
 
-        for (params_n = 0; params_n < (size_t)sk_OPENSSL_STRING_num(opts);
-             params_n++) {
-            char *opt = sk_OPENSSL_STRING_value(opts, (int)params_n);
-            char *stmp, *vtmp = NULL;
-
-            if ((stmp = OPENSSL_strdup(opt)) == NULL
-                || (vtmp = strchr(stmp, ':')) == NULL
-                || (*vtmp++ = 0) /* Always zero */
-                || !OSSL_PARAM_allocate_from_text(&params[params_n], paramdefs,
-                                                  stmp, vtmp, strlen(vtmp))) {
-                BIO_printf(bio_err, "MAC parameter error '%s'\n", opt);
-                ERR_print_errors(bio_err);
-                ok = 0;
-            }
-            OPENSSL_free(stmp);
-            if (!ok)
-                break;
-        }
-        if (ok) {
-            params[params_n] = OSSL_PARAM_construct_end();
-            if (!EVP_MAC_CTX_set_params(ctx, params)) {
-                BIO_printf(bio_err, "MAC parameter error\n");
-                ERR_print_errors(bio_err);
-                goto err;
-            }
-        }
-        for (; params_n-- > 0;) {
-            OPENSSL_free(params[params_n].data);
+        params = app_params_new_from_opts(opts,
+                                          EVP_MAC_settable_ctx_params(mac));
+        if (params == NULL)
+            goto err;
+
+        if (!EVP_MAC_CTX_set_params(ctx, params)) {
+            BIO_printf(bio_err, "MAC parameter error\n");
+            ERR_print_errors(bio_err);
+            ok = 0;
         }
-        OPENSSL_free(params);
+        app_params_free(params);
         if (!ok)
             goto err;
     }
 
-    /* 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;
@@ -155,16 +177,16 @@ opthelp:
     if (out == NULL)
         goto err;
 
-    if (!EVP_MAC_init(ctx)) {
+    if (!EVP_MAC_init(ctx, NULL, 0, NULL)) {
         BIO_printf(bio_err, "EVP_MAC_Init failed\n");
         goto err;
     }
 
-
-    for (;;) {
+    while (BIO_pending(in) || !BIO_eof(in)) {
         i = BIO_read(in, (char *)buf, BUFSIZE);
         if (i < 0) {
             BIO_printf(bio_err, "Read Error in '%s'\n", infile);
+            ERR_print_errors(bio_err);
             goto err;
         }
         if (i == 0)
@@ -192,12 +214,10 @@ opthelp:
     if (out_bin) {
         BIO_write(out, buf, len);
     } else {
-        if (outfile == NULL)
-            BIO_printf(out,"\n");
         for (i = 0; i < (int)len; ++i)
             BIO_printf(out, "%02X", buf[i]);
         if (outfile == NULL)
-            BIO_printf(out,"\n");
+            BIO_printf(out, "\n");
     }
 
     ret = 0;
@@ -205,6 +225,8 @@ err:
     if (ret != 0)
         ERR_print_errors(bio_err);
     OPENSSL_clear_free(buf, BUFSIZE);
+    OPENSSL_free(cipher);
+    OPENSSL_free(digest);
     sk_OPENSSL_STRING_free(opts);
     BIO_free(in);
     BIO_free(out);