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