apps/mac: Add digest and cipher command line options
[openssl.git] / apps / mac.c
index b9610f3a185f780696777e4ef5321499572ec122..ca02a781e579707dce8303b04991140f993217e1 100644 (file)
@@ -15,6 +15,7 @@
 #include <openssl/err.h>
 #include <openssl/evp.h>
 #include <openssl/params.h>
+#include <openssl/core_names.h>
 
 #undef BUFSIZE
 #define BUFSIZE 1024*8
@@ -22,6 +23,7 @@
 typedef enum OPTION_choice {
     OPT_COMMON,
     OPT_MACOPT, OPT_BIN, OPT_IN, OPT_OUT,
+    OPT_CIPHER, OPT_DIGEST,
     OPT_PROV_ENUM
 } OPTION_CHOICE;
 
@@ -31,6 +33,8 @@ const OPTIONS mac_options[] = {
     OPT_SECTION("General"),
     {"help", OPT_HELP, '-', "Display this summary"},
     {"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"),
@@ -48,6 +52,24 @@ const OPTIONS mac_options[] = {
     {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 = 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;
+}
+
 int mac_main(int argc, char **argv)
 {
     int ret = 1;
@@ -64,6 +86,7 @@ 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);
@@ -93,6 +116,18 @@ 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;
@@ -193,6 +228,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);