]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/kdf.c
PROV: Add type specific MSBLOB and PVK decoding for the MS->key decoders
[thirdparty/openssl.git] / apps / kdf.c
CommitLineData
c54492ec 1/*
8020d79b 2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
c54492ec 3 *
a6ed19dc 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
c54492ec
SL
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>
55accfd2 18#include <openssl/params.h>
c54492ec
SL
19
20typedef enum OPTION_choice {
21 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
6bd4e3f2
P
22 OPT_KDFOPT, OPT_BIN, OPT_KEYLEN, OPT_OUT,
23 OPT_PROV_ENUM
c54492ec
SL
24} OPTION_CHOICE;
25
26const OPTIONS kdf_options[] = {
27 {OPT_HELP_STR, 1, '-', "Usage: %s [options] kdf_name\n"},
5388f986
RS
28
29 OPT_SECTION("General"),
c54492ec 30 {"help", OPT_HELP, '-', "Display this summary"},
92de469f
RS
31 {"kdfopt", OPT_KDFOPT, 's', "KDF algorithm control parameters in n:v form"},
32 {OPT_MORE_STR, 1, '-', "See 'Supported Controls' in the EVP_KDF_ docs\n"},
c54492ec 33 {"keylen", OPT_KEYLEN, 's', "The size of the output derived key"},
5388f986
RS
34
35 OPT_SECTION("Output"),
c54492ec 36 {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
92de469f
RS
37 {"binary", OPT_BIN, '-',
38 "Output in binary format (default is hexadecimal)"},
39
6bd4e3f2
P
40 OPT_PROV_OPTIONS,
41
92de469f
RS
42 OPT_PARAMETERS(),
43 {"kdf_name", 0, 0, "Name of the KDF algorithm"},
c54492ec
SL
44 {NULL}
45};
46
c54492ec
SL
47int kdf_main(int argc, char **argv)
48{
55accfd2 49 int ret = 1, out_bin = 0;
c54492ec
SL
50 OPTION_CHOICE o;
51 STACK_OF(OPENSSL_STRING) *opts = NULL;
52 char *prog, *hexout = NULL;
53 const char *outfile = NULL;
54 unsigned char *dkm_bytes = NULL;
55 size_t dkm_len = 0;
56 BIO *out = NULL;
55accfd2 57 EVP_KDF *kdf = NULL;
c54492ec
SL
58 EVP_KDF_CTX *ctx = NULL;
59
60 prog = opt_init(argc, argv, kdf_options);
61 while ((o = opt_next()) != OPT_EOF) {
62 switch (o) {
63 default:
64opthelp:
65 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
66 goto err;
67 case OPT_HELP:
68 opt_help(kdf_options);
69 ret = 0;
70 goto err;
71 case OPT_BIN:
72 out_bin = 1;
73 break;
74 case OPT_KEYLEN:
75 dkm_len = (size_t)atoi(opt_arg());
76 break;
77 case OPT_OUT:
78 outfile = opt_arg();
79 break;
80 case OPT_KDFOPT:
81 if (opts == NULL)
82 opts = sk_OPENSSL_STRING_new_null();
83 if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
84 goto opthelp;
85 break;
6bd4e3f2
P
86 case OPT_PROV_CASES:
87 if (!opt_provider(o))
88 goto err;
89 break;
c54492ec
SL
90 }
91 }
021410ea
RS
92
93 /* One argument, the KDF name. */
c54492ec
SL
94 argc = opt_num_rest();
95 argv = opt_rest();
021410ea 96 if (argc != 1)
c54492ec 97 goto opthelp;
c54492ec 98
55accfd2 99 if ((kdf = EVP_KDF_fetch(NULL, argv[0], NULL)) == NULL) {
c54492ec
SL
100 BIO_printf(bio_err, "Invalid KDF name %s\n", argv[0]);
101 goto opthelp;
102 }
103
660c5344 104 ctx = EVP_KDF_CTX_new(kdf);
c54492ec
SL
105 if (ctx == NULL)
106 goto err;
107
108 if (opts != NULL) {
55accfd2
P
109 int ok = 1;
110 OSSL_PARAM *params =
41f7ecf3 111 app_params_new_from_opts(opts, EVP_KDF_settable_ctx_params(kdf));
55accfd2
P
112
113 if (params == NULL)
114 goto err;
115
660c5344 116 if (!EVP_KDF_CTX_set_params(ctx, params)) {
55accfd2
P
117 BIO_printf(bio_err, "KDF parameter error\n");
118 ERR_print_errors(bio_err);
119 ok = 0;
c54492ec 120 }
55accfd2
P
121 app_params_free(params);
122 if (!ok)
123 goto err;
c54492ec
SL
124 }
125
126 out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
127 if (out == NULL)
128 goto err;
129
130 if (dkm_len <= 0) {
131 BIO_printf(bio_err, "Invalid derived key length.\n");
132 goto err;
133 }
134 dkm_bytes = app_malloc(dkm_len, "out buffer");
135 if (dkm_bytes == NULL)
136 goto err;
137
bb0ab821 138 if (!EVP_KDF_derive(ctx, dkm_bytes, dkm_len, NULL)) {
c54492ec
SL
139 BIO_printf(bio_err, "EVP_KDF_derive failed\n");
140 goto err;
141 }
142
143 if (out_bin) {
144 BIO_write(out, dkm_bytes, dkm_len);
145 } else {
146 hexout = OPENSSL_buf2hexstr(dkm_bytes, dkm_len);
17197a2f
MC
147 if (hexout == NULL) {
148 BIO_printf(bio_err, "Memory allocation failure\n");
149 goto err;
150 }
c54492ec
SL
151 BIO_printf(out, "%s\n\n", hexout);
152 }
153
154 ret = 0;
155err:
156 if (ret != 0)
157 ERR_print_errors(bio_err);
158 OPENSSL_clear_free(dkm_bytes, dkm_len);
159 sk_OPENSSL_STRING_free(opts);
55accfd2 160 EVP_KDF_free(kdf);
660c5344 161 EVP_KDF_CTX_free(ctx);
c54492ec
SL
162 BIO_free(out);
163 OPENSSL_free(hexout);
164 return ret;
165}