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