Add "sections" to -help output
[openssl.git] / apps / kdf.c
1 /*
2  * Copyright 2019 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
10 #include <string.h>
11
12 #include "apps.h"
13 #include "progs.h"
14 #include <openssl/bio.h>
15 #include <openssl/err.h>
16 #include <openssl/evp.h>
17 #include <openssl/kdf.h>
18 #include <openssl/params.h>
19
20 typedef enum OPTION_choice {
21     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
22     OPT_KDFOPT, OPT_BIN, OPT_KEYLEN, OPT_OUT
23 } OPTION_CHOICE;
24
25 const OPTIONS kdf_options[] = {
26     {OPT_HELP_STR, 1, '-', "Usage: %s [options] kdf_name\n"},
27     {OPT_HELP_STR, 1, '-', "kdf_name\t KDF algorithm.\n"},
28
29     OPT_SECTION("General"),
30     {"help", OPT_HELP, '-', "Display this summary"},
31     {"kdfopt", OPT_KDFOPT, 's', "KDF algorithm control parameters in n:v form."},
32     {OPT_HELP_STR, 1, '-', "See 'Supported Controls' in the EVP_KDF_ docs"},
33     {"keylen", OPT_KEYLEN, 's', "The size of the output derived key"},
34
35     OPT_SECTION("Output"),
36     {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
37     {"binary", OPT_BIN, '-', "Output in binary format (Default is hexadecimal "
38                              "output)"},
39     {NULL}
40 };
41
42 int kdf_main(int argc, char **argv)
43 {
44     int ret = 1, out_bin = 0;
45     OPTION_CHOICE o;
46     STACK_OF(OPENSSL_STRING) *opts = NULL;
47     char *prog, *hexout = NULL;
48     const char *outfile = NULL;
49     unsigned char *dkm_bytes = NULL;
50     size_t dkm_len = 0;
51     BIO *out = NULL;
52     EVP_KDF *kdf = NULL;
53     EVP_KDF_CTX *ctx = NULL;
54
55     prog = opt_init(argc, argv, kdf_options);
56     while ((o = opt_next()) != OPT_EOF) {
57         switch (o) {
58         default:
59 opthelp:
60             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
61             goto err;
62         case OPT_HELP:
63             opt_help(kdf_options);
64             ret = 0;
65             goto err;
66         case OPT_BIN:
67             out_bin = 1;
68             break;
69         case OPT_KEYLEN:
70             dkm_len = (size_t)atoi(opt_arg());
71             break;
72         case OPT_OUT:
73             outfile = opt_arg();
74             break;
75         case OPT_KDFOPT:
76             if (opts == NULL)
77                 opts = sk_OPENSSL_STRING_new_null();
78             if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
79                 goto opthelp;
80             break;
81         }
82     }
83     argc = opt_num_rest();
84     argv = opt_rest();
85
86     if (argc != 1) {
87         BIO_printf(bio_err, "Invalid number of extra arguments\n");
88         goto opthelp;
89     }
90
91     if ((kdf = EVP_KDF_fetch(NULL, argv[0], NULL)) == NULL) {
92         BIO_printf(bio_err, "Invalid KDF name %s\n", argv[0]);
93         goto opthelp;
94     }
95
96     ctx = EVP_KDF_CTX_new(kdf);
97     if (ctx == NULL)
98         goto err;
99
100     if (opts != NULL) {
101         int ok = 1;
102         OSSL_PARAM *params =
103             app_params_new_from_opts(opts, EVP_KDF_settable_ctx_params(kdf));
104
105         if (params == NULL)
106             goto err;
107
108         if (!EVP_KDF_CTX_set_params(ctx, params)) {
109             BIO_printf(bio_err, "KDF parameter error\n");
110             ERR_print_errors(bio_err);
111             ok = 0;
112         }
113         app_params_free(params);
114         if (!ok)
115             goto err;
116     }
117
118     out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
119     if (out == NULL)
120         goto err;
121
122     if (dkm_len <= 0) {
123         BIO_printf(bio_err, "Invalid derived key length.\n");
124         goto err;
125     }
126     dkm_bytes = app_malloc(dkm_len, "out buffer");
127     if (dkm_bytes == NULL)
128         goto err;
129
130     if (!EVP_KDF_derive(ctx, dkm_bytes, dkm_len)) {
131         BIO_printf(bio_err, "EVP_KDF_derive failed\n");
132         goto err;
133     }
134
135     if (out_bin) {
136         BIO_write(out, dkm_bytes, dkm_len);
137     } else {
138         hexout = OPENSSL_buf2hexstr(dkm_bytes, dkm_len);
139         BIO_printf(out, "%s\n\n", hexout);
140     }
141
142     ret = 0;
143 err:
144     if (ret != 0)
145         ERR_print_errors(bio_err);
146     OPENSSL_clear_free(dkm_bytes, dkm_len);
147     sk_OPENSSL_STRING_free(opts);
148     EVP_KDF_free(kdf);
149     EVP_KDF_CTX_free(ctx);
150     BIO_free(out);
151     OPENSSL_free(hexout);
152     return ret;
153 }