mac.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include "apps.h"
  11. #include "progs.h"
  12. #include <openssl/bio.h>
  13. #include <openssl/err.h>
  14. #include <openssl/evp.h>
  15. #undef BUFSIZE
  16. #define BUFSIZE 1024*8
  17. typedef enum OPTION_choice {
  18. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  19. OPT_MACOPT, OPT_BIN, OPT_IN, OPT_OUT
  20. } OPTION_CHOICE;
  21. const OPTIONS mac_options[] = {
  22. {OPT_HELP_STR, 1, '-', "Usage: %s [options] mac_name\n"},
  23. {OPT_HELP_STR, 1, '-', "mac_name\t\t MAC algorithm (See list "
  24. "-mac-algorithms)"},
  25. {"help", OPT_HELP, '-', "Display this summary"},
  26. {"macopt", OPT_MACOPT, 's', "MAC algorithm control parameters in n:v form. "
  27. "See 'Supported Controls' in the EVP_MAC_ docs"},
  28. {"in", OPT_IN, '<', "Input file to MAC (default is stdin)"},
  29. {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
  30. {"binary", OPT_BIN, '-', "Output in binary format (Default is hexadecimal "
  31. "output)"},
  32. {NULL}
  33. };
  34. static int mac_ctrl_string(EVP_MAC_CTX *ctx, const char *value)
  35. {
  36. int rv;
  37. char *stmp, *vtmp = NULL;
  38. stmp = OPENSSL_strdup(value);
  39. if (stmp == NULL)
  40. return -1;
  41. vtmp = strchr(stmp, ':');
  42. if (vtmp != NULL) {
  43. *vtmp = 0;
  44. vtmp++;
  45. }
  46. rv = EVP_MAC_ctrl_str(ctx, stmp, vtmp);
  47. OPENSSL_free(stmp);
  48. return rv;
  49. }
  50. int mac_main(int argc, char **argv)
  51. {
  52. int ret = 1;
  53. char *prog;
  54. const EVP_MAC *mac = NULL;
  55. OPTION_CHOICE o;
  56. EVP_MAC_CTX *ctx = NULL;
  57. STACK_OF(OPENSSL_STRING) *opts = NULL;
  58. unsigned char *buf = NULL;
  59. size_t len;
  60. int i;
  61. BIO *in = NULL, *out = NULL;
  62. const char *outfile = NULL;
  63. const char *infile = NULL;
  64. int out_bin = 0;
  65. int inform = FORMAT_BINARY;
  66. prog = opt_init(argc, argv, mac_options);
  67. buf = app_malloc(BUFSIZE, "I/O buffer");
  68. while ((o = opt_next()) != OPT_EOF) {
  69. switch (o) {
  70. default:
  71. opthelp:
  72. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  73. goto err;
  74. case OPT_HELP:
  75. opt_help(mac_options);
  76. ret = 0;
  77. goto err;
  78. case OPT_BIN:
  79. out_bin = 1;
  80. break;
  81. case OPT_IN:
  82. infile = opt_arg();
  83. break;
  84. case OPT_OUT:
  85. outfile = opt_arg();
  86. break;
  87. case OPT_MACOPT:
  88. if (opts == NULL)
  89. opts = sk_OPENSSL_STRING_new_null();
  90. if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
  91. goto opthelp;
  92. break;
  93. }
  94. }
  95. argc = opt_num_rest();
  96. argv = opt_rest();
  97. if (argc != 1) {
  98. BIO_printf(bio_err, "Invalid number of extra arguments\n");
  99. goto opthelp;
  100. }
  101. mac = EVP_get_macbyname(argv[0]);
  102. if (mac == NULL) {
  103. BIO_printf(bio_err, "Invalid MAC name %s\n", argv[0]);
  104. goto opthelp;
  105. }
  106. ctx = EVP_MAC_CTX_new(mac);
  107. if (ctx == NULL)
  108. goto err;
  109. if (opts != NULL) {
  110. for (i = 0; i < sk_OPENSSL_STRING_num(opts); i++) {
  111. char *opt = sk_OPENSSL_STRING_value(opts, i);
  112. if (mac_ctrl_string(ctx, opt) <= 0) {
  113. BIO_printf(bio_err, "MAC parameter error '%s'\n", opt);
  114. ERR_print_errors(bio_err);
  115. goto err;
  116. }
  117. }
  118. }
  119. /* Use text mode for stdin */
  120. if (infile == NULL || strcmp(infile, "-") == 0)
  121. inform = FORMAT_TEXT;
  122. in = bio_open_default(infile, 'r', inform);
  123. if (in == NULL)
  124. goto err;
  125. out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
  126. if (out == NULL)
  127. goto err;
  128. if (!EVP_MAC_init(ctx)) {
  129. BIO_printf(bio_err, "EVP_MAC_Init failed\n");
  130. goto err;
  131. }
  132. for (;;) {
  133. i = BIO_read(in, (char *)buf, BUFSIZE);
  134. if (i < 0) {
  135. BIO_printf(bio_err, "Read Error in '%s'\n", infile);
  136. goto err;
  137. }
  138. if (i == 0)
  139. break;
  140. if (!EVP_MAC_update(ctx, buf, i)) {
  141. BIO_printf(bio_err, "EVP_MAC_update failed\n");
  142. goto err;
  143. }
  144. }
  145. if (!EVP_MAC_final(ctx, NULL, &len)) {
  146. BIO_printf(bio_err, "EVP_MAC_final failed\n");
  147. goto err;
  148. }
  149. if (len > BUFSIZE) {
  150. BIO_printf(bio_err, "output len is too large\n");
  151. goto err;
  152. }
  153. if (!EVP_MAC_final(ctx, buf, &len)) {
  154. BIO_printf(bio_err, "EVP_MAC_final failed\n");
  155. goto err;
  156. }
  157. if (out_bin) {
  158. BIO_write(out, buf, len);
  159. } else {
  160. if (outfile == NULL)
  161. BIO_printf(out,"\n");
  162. for (i = 0; i < (int)len; ++i)
  163. BIO_printf(out, "%02X", buf[i]);
  164. if (outfile == NULL)
  165. BIO_printf(out,"\n");
  166. }
  167. ret = 0;
  168. err:
  169. if (ret != 0)
  170. ERR_print_errors(bio_err);
  171. OPENSSL_clear_free(buf, BUFSIZE);
  172. sk_OPENSSL_STRING_free(opts);
  173. BIO_free(in);
  174. BIO_free(out);
  175. EVP_MAC_CTX_free(ctx);
  176. return ret;
  177. }