1*b0d17251Schristos /*
2*b0d17251Schristos * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b0d17251Schristos *
4*b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5*b0d17251Schristos * this file except in compliance with the License. You can obtain a copy
6*b0d17251Schristos * in the file LICENSE in the source distribution or at
7*b0d17251Schristos * https://www.openssl.org/source/license.html
8*b0d17251Schristos */
9*b0d17251Schristos
10*b0d17251Schristos #include <string.h>
11*b0d17251Schristos
12*b0d17251Schristos #include "apps.h"
13*b0d17251Schristos #include "progs.h"
14*b0d17251Schristos #include <openssl/bio.h>
15*b0d17251Schristos #include <openssl/err.h>
16*b0d17251Schristos #include <openssl/evp.h>
17*b0d17251Schristos #include <openssl/kdf.h>
18*b0d17251Schristos #include <openssl/params.h>
19*b0d17251Schristos
20*b0d17251Schristos typedef enum OPTION_choice {
21*b0d17251Schristos OPT_COMMON,
22*b0d17251Schristos OPT_KDFOPT, OPT_BIN, OPT_KEYLEN, OPT_OUT,
23*b0d17251Schristos OPT_CIPHER, OPT_DIGEST, OPT_MAC,
24*b0d17251Schristos OPT_PROV_ENUM
25*b0d17251Schristos } OPTION_CHOICE;
26*b0d17251Schristos
27*b0d17251Schristos const OPTIONS kdf_options[] = {
28*b0d17251Schristos {OPT_HELP_STR, 1, '-', "Usage: %s [options] kdf_name\n"},
29*b0d17251Schristos
30*b0d17251Schristos OPT_SECTION("General"),
31*b0d17251Schristos {"help", OPT_HELP, '-', "Display this summary"},
32*b0d17251Schristos {"kdfopt", OPT_KDFOPT, 's', "KDF algorithm control parameters in n:v form"},
33*b0d17251Schristos {"cipher", OPT_CIPHER, 's', "Cipher"},
34*b0d17251Schristos {"digest", OPT_DIGEST, 's', "Digest"},
35*b0d17251Schristos {"mac", OPT_MAC, 's', "MAC"},
36*b0d17251Schristos {OPT_MORE_STR, 1, '-', "See 'Supported Controls' in the EVP_KDF_ docs\n"},
37*b0d17251Schristos {"keylen", OPT_KEYLEN, 's', "The size of the output derived key"},
38*b0d17251Schristos
39*b0d17251Schristos OPT_SECTION("Output"),
40*b0d17251Schristos {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
41*b0d17251Schristos {"binary", OPT_BIN, '-',
42*b0d17251Schristos "Output in binary format (default is hexadecimal)"},
43*b0d17251Schristos
44*b0d17251Schristos OPT_PROV_OPTIONS,
45*b0d17251Schristos
46*b0d17251Schristos OPT_PARAMETERS(),
47*b0d17251Schristos {"kdf_name", 0, 0, "Name of the KDF algorithm"},
48*b0d17251Schristos {NULL}
49*b0d17251Schristos };
50*b0d17251Schristos
alloc_kdf_algorithm_name(STACK_OF (OPENSSL_STRING)** optp,const char * name,const char * arg)51*b0d17251Schristos static char *alloc_kdf_algorithm_name(STACK_OF(OPENSSL_STRING) **optp,
52*b0d17251Schristos const char *name, const char *arg)
53*b0d17251Schristos {
54*b0d17251Schristos size_t len = strlen(name) + strlen(arg) + 2;
55*b0d17251Schristos char *res;
56*b0d17251Schristos
57*b0d17251Schristos if (*optp == NULL)
58*b0d17251Schristos *optp = sk_OPENSSL_STRING_new_null();
59*b0d17251Schristos if (*optp == NULL)
60*b0d17251Schristos return NULL;
61*b0d17251Schristos
62*b0d17251Schristos res = app_malloc(len, "algorithm name");
63*b0d17251Schristos BIO_snprintf(res, len, "%s:%s", name, arg);
64*b0d17251Schristos if (sk_OPENSSL_STRING_push(*optp, res))
65*b0d17251Schristos return res;
66*b0d17251Schristos OPENSSL_free(res);
67*b0d17251Schristos return NULL;
68*b0d17251Schristos }
69*b0d17251Schristos
kdf_main(int argc,char ** argv)70*b0d17251Schristos int kdf_main(int argc, char **argv)
71*b0d17251Schristos {
72*b0d17251Schristos int ret = 1, out_bin = 0;
73*b0d17251Schristos OPTION_CHOICE o;
74*b0d17251Schristos STACK_OF(OPENSSL_STRING) *opts = NULL;
75*b0d17251Schristos char *prog, *hexout = NULL;
76*b0d17251Schristos const char *outfile = NULL;
77*b0d17251Schristos unsigned char *dkm_bytes = NULL;
78*b0d17251Schristos size_t dkm_len = 0;
79*b0d17251Schristos BIO *out = NULL;
80*b0d17251Schristos EVP_KDF *kdf = NULL;
81*b0d17251Schristos EVP_KDF_CTX *ctx = NULL;
82*b0d17251Schristos char *digest = NULL, *cipher = NULL, *mac = NULL;
83*b0d17251Schristos
84*b0d17251Schristos prog = opt_init(argc, argv, kdf_options);
85*b0d17251Schristos while ((o = opt_next()) != OPT_EOF) {
86*b0d17251Schristos switch (o) {
87*b0d17251Schristos default:
88*b0d17251Schristos opthelp:
89*b0d17251Schristos BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
90*b0d17251Schristos goto err;
91*b0d17251Schristos case OPT_HELP:
92*b0d17251Schristos opt_help(kdf_options);
93*b0d17251Schristos ret = 0;
94*b0d17251Schristos goto err;
95*b0d17251Schristos case OPT_BIN:
96*b0d17251Schristos out_bin = 1;
97*b0d17251Schristos break;
98*b0d17251Schristos case OPT_KEYLEN:
99*b0d17251Schristos dkm_len = (size_t)atoi(opt_arg());
100*b0d17251Schristos break;
101*b0d17251Schristos case OPT_OUT:
102*b0d17251Schristos outfile = opt_arg();
103*b0d17251Schristos break;
104*b0d17251Schristos case OPT_KDFOPT:
105*b0d17251Schristos if (opts == NULL)
106*b0d17251Schristos opts = sk_OPENSSL_STRING_new_null();
107*b0d17251Schristos if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
108*b0d17251Schristos goto opthelp;
109*b0d17251Schristos break;
110*b0d17251Schristos case OPT_CIPHER:
111*b0d17251Schristos OPENSSL_free(cipher);
112*b0d17251Schristos cipher = alloc_kdf_algorithm_name(&opts, "cipher", opt_arg());
113*b0d17251Schristos if (cipher == NULL)
114*b0d17251Schristos goto opthelp;
115*b0d17251Schristos break;
116*b0d17251Schristos case OPT_DIGEST:
117*b0d17251Schristos OPENSSL_free(digest);
118*b0d17251Schristos digest = alloc_kdf_algorithm_name(&opts, "digest", opt_arg());
119*b0d17251Schristos if (digest == NULL)
120*b0d17251Schristos goto opthelp;
121*b0d17251Schristos break;
122*b0d17251Schristos case OPT_MAC:
123*b0d17251Schristos OPENSSL_free(mac);
124*b0d17251Schristos mac = alloc_kdf_algorithm_name(&opts, "mac", opt_arg());
125*b0d17251Schristos if (mac == NULL)
126*b0d17251Schristos goto opthelp;
127*b0d17251Schristos break;
128*b0d17251Schristos case OPT_PROV_CASES:
129*b0d17251Schristos if (!opt_provider(o))
130*b0d17251Schristos goto err;
131*b0d17251Schristos break;
132*b0d17251Schristos }
133*b0d17251Schristos }
134*b0d17251Schristos
135*b0d17251Schristos /* One argument, the KDF name. */
136*b0d17251Schristos argc = opt_num_rest();
137*b0d17251Schristos argv = opt_rest();
138*b0d17251Schristos if (argc != 1)
139*b0d17251Schristos goto opthelp;
140*b0d17251Schristos
141*b0d17251Schristos if ((kdf = EVP_KDF_fetch(app_get0_libctx(), argv[0],
142*b0d17251Schristos app_get0_propq())) == NULL) {
143*b0d17251Schristos BIO_printf(bio_err, "Invalid KDF name %s\n", argv[0]);
144*b0d17251Schristos goto opthelp;
145*b0d17251Schristos }
146*b0d17251Schristos
147*b0d17251Schristos ctx = EVP_KDF_CTX_new(kdf);
148*b0d17251Schristos if (ctx == NULL)
149*b0d17251Schristos goto err;
150*b0d17251Schristos
151*b0d17251Schristos if (opts != NULL) {
152*b0d17251Schristos int ok = 1;
153*b0d17251Schristos OSSL_PARAM *params =
154*b0d17251Schristos app_params_new_from_opts(opts, EVP_KDF_settable_ctx_params(kdf));
155*b0d17251Schristos
156*b0d17251Schristos if (params == NULL)
157*b0d17251Schristos goto err;
158*b0d17251Schristos
159*b0d17251Schristos if (!EVP_KDF_CTX_set_params(ctx, params)) {
160*b0d17251Schristos BIO_printf(bio_err, "KDF parameter error\n");
161*b0d17251Schristos ERR_print_errors(bio_err);
162*b0d17251Schristos ok = 0;
163*b0d17251Schristos }
164*b0d17251Schristos app_params_free(params);
165*b0d17251Schristos if (!ok)
166*b0d17251Schristos goto err;
167*b0d17251Schristos }
168*b0d17251Schristos
169*b0d17251Schristos out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
170*b0d17251Schristos if (out == NULL)
171*b0d17251Schristos goto err;
172*b0d17251Schristos
173*b0d17251Schristos if (dkm_len <= 0) {
174*b0d17251Schristos BIO_printf(bio_err, "Invalid derived key length.\n");
175*b0d17251Schristos goto err;
176*b0d17251Schristos }
177*b0d17251Schristos dkm_bytes = app_malloc(dkm_len, "out buffer");
178*b0d17251Schristos if (dkm_bytes == NULL)
179*b0d17251Schristos goto err;
180*b0d17251Schristos
181*b0d17251Schristos if (!EVP_KDF_derive(ctx, dkm_bytes, dkm_len, NULL)) {
182*b0d17251Schristos BIO_printf(bio_err, "EVP_KDF_derive failed\n");
183*b0d17251Schristos goto err;
184*b0d17251Schristos }
185*b0d17251Schristos
186*b0d17251Schristos if (out_bin) {
187*b0d17251Schristos BIO_write(out, dkm_bytes, dkm_len);
188*b0d17251Schristos } else {
189*b0d17251Schristos hexout = OPENSSL_buf2hexstr(dkm_bytes, dkm_len);
190*b0d17251Schristos if (hexout == NULL) {
191*b0d17251Schristos BIO_printf(bio_err, "Memory allocation failure\n");
192*b0d17251Schristos goto err;
193*b0d17251Schristos }
194*b0d17251Schristos BIO_printf(out, "%s\n\n", hexout);
195*b0d17251Schristos }
196*b0d17251Schristos
197*b0d17251Schristos ret = 0;
198*b0d17251Schristos err:
199*b0d17251Schristos if (ret != 0)
200*b0d17251Schristos ERR_print_errors(bio_err);
201*b0d17251Schristos OPENSSL_clear_free(dkm_bytes, dkm_len);
202*b0d17251Schristos sk_OPENSSL_STRING_free(opts);
203*b0d17251Schristos EVP_KDF_free(kdf);
204*b0d17251Schristos EVP_KDF_CTX_free(ctx);
205*b0d17251Schristos BIO_free(out);
206*b0d17251Schristos OPENSSL_free(hexout);
207*b0d17251Schristos OPENSSL_free(cipher);
208*b0d17251Schristos OPENSSL_free(digest);
209*b0d17251Schristos OPENSSL_free(mac);
210*b0d17251Schristos return ret;
211*b0d17251Schristos }
212