/*
- * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
*
- * Licensed under the OpenSSL license (the "License"). You may not use
+ * Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/kdf.h>
+#include <openssl/params.h>
+
+DEFINE_STACK_OF_STRING()
typedef enum OPTION_choice {
OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
- OPT_KDFOPT, OPT_BIN, OPT_KEYLEN, OPT_OUT
+ OPT_KDFOPT, OPT_BIN, OPT_KEYLEN, OPT_OUT,
+ OPT_PROV_ENUM
} OPTION_CHOICE;
const OPTIONS kdf_options[] = {
{OPT_HELP_STR, 1, '-', "Usage: %s [options] kdf_name\n"},
- {OPT_HELP_STR, 1, '-', "kdf_name\t KDF algorithm.\n"},
+
+ OPT_SECTION("General"),
{"help", OPT_HELP, '-', "Display this summary"},
- {"kdfopt", OPT_KDFOPT, 's', "KDF algorithm control parameters in n:v form. "
- "See 'Supported Controls' in the EVP_KDF_ docs"},
+ {"kdfopt", OPT_KDFOPT, 's', "KDF algorithm control parameters in n:v form"},
+ {OPT_MORE_STR, 1, '-', "See 'Supported Controls' in the EVP_KDF_ docs\n"},
{"keylen", OPT_KEYLEN, 's', "The size of the output derived key"},
+
+ OPT_SECTION("Output"),
{"out", OPT_OUT, '>', "Output to filename rather than stdout"},
- {"binary", OPT_BIN, '-', "Output in binary format (Default is hexadecimal "
- "output)"},
+ {"binary", OPT_BIN, '-',
+ "Output in binary format (default is hexadecimal)"},
+
+ OPT_PROV_OPTIONS,
+
+ OPT_PARAMETERS(),
+ {"kdf_name", 0, 0, "Name of the KDF algorithm"},
{NULL}
};
-static int kdf_ctrl_string(EVP_KDF_CTX *ctx, const char *value)
-{
- int rv;
- char *stmp, *vtmp = NULL;
-
- stmp = OPENSSL_strdup(value);
- if (stmp == NULL)
- return -1;
- vtmp = strchr(stmp, ':');
- if (vtmp != NULL) {
- *vtmp = 0;
- vtmp++;
- }
- rv = EVP_KDF_ctrl_str(ctx, stmp, vtmp);
- OPENSSL_free(stmp);
- return rv;
-}
-
int kdf_main(int argc, char **argv)
{
- int ret = 1, i, id, out_bin = 0;
+ int ret = 1, out_bin = 0;
OPTION_CHOICE o;
STACK_OF(OPENSSL_STRING) *opts = NULL;
char *prog, *hexout = NULL;
unsigned char *dkm_bytes = NULL;
size_t dkm_len = 0;
BIO *out = NULL;
+ EVP_KDF *kdf = NULL;
EVP_KDF_CTX *ctx = NULL;
prog = opt_init(argc, argv, kdf_options);
if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
goto opthelp;
break;
+ case OPT_PROV_CASES:
+ if (!opt_provider(o))
+ goto err;
+ break;
}
}
argc = opt_num_rest();
goto opthelp;
}
- id = OBJ_sn2nid(argv[0]);
- if (id == NID_undef) {
+ if ((kdf = EVP_KDF_fetch(NULL, argv[0], NULL)) == NULL) {
BIO_printf(bio_err, "Invalid KDF name %s\n", argv[0]);
goto opthelp;
}
- ctx = EVP_KDF_CTX_new_id(id);
+ ctx = EVP_KDF_CTX_new(kdf);
if (ctx == NULL)
goto err;
if (opts != NULL) {
- for (i = 0; i < sk_OPENSSL_STRING_num(opts); i++) {
- char *opt = sk_OPENSSL_STRING_value(opts, i);
- if (kdf_ctrl_string(ctx, opt) <= 0) {
- BIO_printf(bio_err, "KDF parameter error '%s'\n", opt);
- ERR_print_errors(bio_err);
- goto err;
- }
+ int ok = 1;
+ OSSL_PARAM *params =
+ app_params_new_from_opts(opts, EVP_KDF_settable_ctx_params(kdf));
+
+ if (params == NULL)
+ goto err;
+
+ if (!EVP_KDF_CTX_set_params(ctx, params)) {
+ BIO_printf(bio_err, "KDF parameter error\n");
+ ERR_print_errors(bio_err);
+ ok = 0;
}
+ app_params_free(params);
+ if (!ok)
+ goto err;
}
out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
BIO_write(out, dkm_bytes, dkm_len);
} else {
hexout = OPENSSL_buf2hexstr(dkm_bytes, dkm_len);
+ if (hexout == NULL) {
+ BIO_printf(bio_err, "Memory allocation failure\n");
+ goto err;
+ }
BIO_printf(out, "%s\n\n", hexout);
}
ERR_print_errors(bio_err);
OPENSSL_clear_free(dkm_bytes, dkm_len);
sk_OPENSSL_STRING_free(opts);
+ EVP_KDF_free(kdf);
EVP_KDF_CTX_free(ctx);
BIO_free(out);
OPENSSL_free(hexout);