Bug Summary

File:out/../deps/openssl/openssl/apps/pkeyutl.c
Warning:line 502, column 9
3rd function call argument is an uninitialized value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name pkeyutl.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/home/maurizio/node-v18.6.0/out -resource-dir /usr/local/lib/clang/16.0.0 -D V8_DEPRECATION_WARNINGS -D V8_IMMINENT_DEPRECATION_WARNINGS -D _GLIBCXX_USE_CXX11_ABI=1 -D NODE_OPENSSL_CONF_NAME=nodejs_conf -D NODE_OPENSSL_HAS_QUIC -D __STDC_FORMAT_MACROS -D OPENSSL_NO_PINSHARED -D OPENSSL_THREADS -D OPENSSL_API_COMPAT=0x10100001L -D NDEBUG -D OPENSSL_USE_NODELETE -D L_ENDIAN -D OPENSSL_BUILDING_OPENSSL -D AES_ASM -D BSAES_ASM -D CMLL_ASM -D ECP_NISTZ256_ASM -D GHASH_ASM -D KECCAK1600_ASM -D MD5_ASM -D OPENSSL_BN_ASM_GF2m -D OPENSSL_BN_ASM_MONT -D OPENSSL_BN_ASM_MONT5 -D OPENSSL_CPUID_OBJ -D OPENSSL_IA32_SSE2 -D PADLOCK_ASM -D POLY1305_ASM -D SHA1_ASM -D SHA256_ASM -D SHA512_ASM -D VPAES_ASM -D WHIRLPOOL_ASM -D X25519_ASM -D OPENSSL_PIC -D OPENSSLDIR="/etc/ssl" -D ENGINESDIR="/dev/null" -D TERMIOS -I ../deps/openssl/openssl/apps/include -I ../deps/openssl/openssl -I ../deps/openssl/openssl/include -I ../deps/openssl/openssl/crypto -I ../deps/openssl/openssl/crypto/include -I ../deps/openssl/openssl/crypto/modes -I ../deps/openssl/openssl/crypto/ec/curve448 -I ../deps/openssl/openssl/crypto/ec/curve448/arch_32 -I ../deps/openssl/openssl/providers/common/include -I ../deps/openssl/openssl/providers/implementations/include -I ../deps/openssl/config -I ../deps/openssl/config/archs/linux-x86_64/asm/include -I ../deps/openssl/openssl/include -I ../deps/openssl/openssl/crypto/include -I ../deps/openssl/config/archs/linux-x86_64/asm -internal-isystem /usr/local/lib/clang/16.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../x86_64-redhat-linux/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wno-unused-parameter -Wno-missing-field-initializers -Wno-old-style-declaration -fdebug-compilation-dir=/home/maurizio/node-v18.6.0/out -ferror-limit 19 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-08-22-142216-507842-1 -x c ../deps/openssl/openssl/apps/pkeyutl.c
1/*
2 * Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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 "apps.h"
11#include "progs.h"
12#include <string.h>
13#include <openssl/err.h>
14#include <openssl/pem.h>
15#include <openssl/evp.h>
16#include <sys/stat.h>
17
18#define KEY_NONE0 0
19#define KEY_PRIVKEY1 1
20#define KEY_PUBKEY2 2
21#define KEY_CERT3 3
22
23static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
24 const char *keyfile, int keyform, int key_type,
25 char *passinarg, int pkey_op, ENGINE *e,
26 const int impl, int rawin, EVP_PKEY **ppkey,
27 EVP_MD_CTX *mctx, const char *digestname,
28 OSSL_LIB_CTX *libctx, const char *propq);
29
30static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
31 ENGINE *e);
32
33static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
34 unsigned char *out, size_t *poutlen,
35 const unsigned char *in, size_t inlen);
36
37static int do_raw_keyop(int pkey_op, EVP_MD_CTX *mctx,
38 EVP_PKEY *pkey, BIO *in,
39 int filesize, unsigned char *sig, int siglen,
40 unsigned char **out, size_t *poutlen);
41
42typedef enum OPTION_choice {
43 OPT_COMMONOPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
44 OPT_ENGINE, OPT_ENGINE_IMPL, OPT_IN, OPT_OUT,
45 OPT_PUBIN, OPT_CERTIN, OPT_ASN1PARSE, OPT_HEXDUMP, OPT_SIGN,
46 OPT_VERIFY, OPT_VERIFYRECOVER, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT,
47 OPT_DERIVE, OPT_SIGFILE, OPT_INKEY, OPT_PEERKEY, OPT_PASSIN,
48 OPT_PEERFORM, OPT_KEYFORM, OPT_PKEYOPT, OPT_PKEYOPT_PASSIN, OPT_KDF,
49 OPT_KDFLEN, OPT_R_ENUMOPT_R__FIRST=1500, OPT_R_RAND, OPT_R_WRITERAND, OPT_R__LAST, OPT_PROV_ENUMOPT_PROV__FIRST=1600, OPT_PROV_PROVIDER, OPT_PROV_PROVIDER_PATH
, OPT_PROV_PROPQUERY, OPT_PROV__LAST
,
50 OPT_CONFIG,
51 OPT_RAWIN, OPT_DIGEST
52} OPTION_CHOICE;
53
54const OPTIONS pkeyutl_options[] = {
55 OPT_SECTION("General"){ OPT_SECTION_STR, 1, '-', "General" " options:\n" },
56 {"help", OPT_HELP, '-', "Display this summary"},
57#ifndef OPENSSL_NO_ENGINE
58 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
59 {"engine_impl", OPT_ENGINE_IMPL, '-',
60 "Also use engine given by -engine for crypto operations"},
61#endif
62 {"sign", OPT_SIGN, '-', "Sign input data with private key"},
63 {"verify", OPT_VERIFY, '-', "Verify with public key"},
64 {"encrypt", OPT_ENCRYPT, '-', "Encrypt input data with public key"},
65 {"decrypt", OPT_DECRYPT, '-', "Decrypt input data with private key"},
66 {"derive", OPT_DERIVE, '-', "Derive shared secret"},
67 OPT_CONFIG_OPTION{ "config", OPT_CONFIG, '<', "Load a configuration file (this may load modules)"
}
,
68
69 OPT_SECTION("Input"){ OPT_SECTION_STR, 1, '-', "Input" " options:\n" },
70 {"in", OPT_IN, '<', "Input file - default stdin"},
71 {"rawin", OPT_RAWIN, '-', "Indicate the input data is in raw form"},
72 {"pubin", OPT_PUBIN, '-', "Input is a public key"},
73 {"inkey", OPT_INKEY, 's', "Input private key file"},
74 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
75 {"peerkey", OPT_PEERKEY, 's', "Peer key file used in key derivation"},
76 {"peerform", OPT_PEERFORM, 'E', "Peer key format (DER/PEM/P12/ENGINE)"},
77 {"certin", OPT_CERTIN, '-', "Input is a cert with a public key"},
78 {"rev", OPT_REV, '-', "Reverse the order of the input buffer"},
79 {"sigfile", OPT_SIGFILE, '<', "Signature file (verify operation only)"},
80 {"keyform", OPT_KEYFORM, 'E', "Private key format (ENGINE, other values ignored)"},
81
82 OPT_SECTION("Output"){ OPT_SECTION_STR, 1, '-', "Output" " options:\n" },
83 {"out", OPT_OUT, '>', "Output file - default stdout"},
84 {"asn1parse", OPT_ASN1PARSE, '-', "asn1parse the output data"},
85 {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
86 {"verifyrecover", OPT_VERIFYRECOVER, '-',
87 "Verify with public key, recover original data"},
88
89 OPT_SECTION("Signing/Derivation"){ OPT_SECTION_STR, 1, '-', "Signing/Derivation" " options:\n"
}
,
90 {"digest", OPT_DIGEST, 's',
91 "Specify the digest algorithm when signing the raw input data"},
92 {"pkeyopt", OPT_PKEYOPT, 's', "Public key options as opt:value"},
93 {"pkeyopt_passin", OPT_PKEYOPT_PASSIN, 's',
94 "Public key option that is read as a passphrase argument opt:passphrase"},
95 {"kdf", OPT_KDF, 's', "Use KDF algorithm"},
96 {"kdflen", OPT_KDFLEN, 'p', "KDF algorithm output length"},
97
98 OPT_R_OPTIONS{ OPT_SECTION_STR, 1, '-', "Random state" " options:\n" }, {"rand"
, OPT_R_RAND, 's', "Load the given file(s) into the random number generator"
}, {"writerand", OPT_R_WRITERAND, '>', "Write random data to the specified file"
}
,
99 OPT_PROV_OPTIONS{ OPT_SECTION_STR, 1, '-', "Provider" " options:\n" }, { "provider-path"
, OPT_PROV_PROVIDER_PATH, 's', "Provider load path (must be before 'provider' argument if required)"
}, { "provider", OPT_PROV_PROVIDER, 's', "Provider to load (can be specified multiple times)"
}, { "propquery", OPT_PROV_PROPQUERY, 's', "Property query used when fetching algorithms"
}
,
100 {NULL((void*)0)}
101};
102
103int pkeyutl_main(int argc, char **argv)
104{
105 CONF *conf = NULL((void*)0);
106 BIO *in = NULL((void*)0), *out = NULL((void*)0);
107 ENGINE *e = NULL((void*)0);
108 EVP_PKEY_CTX *ctx = NULL((void*)0);
109 EVP_PKEY *pkey = NULL((void*)0);
110 char *infile = NULL((void*)0), *outfile = NULL((void*)0), *sigfile = NULL((void*)0), *passinarg = NULL((void*)0);
111 char hexdump = 0, asn1parse = 0, rev = 0, *prog;
112 unsigned char *buf_in = NULL((void*)0), *buf_out = NULL((void*)0), *sig = NULL((void*)0);
113 OPTION_CHOICE o;
114 int buf_inlen = 0, siglen = -1;
115 int keyform = FORMAT_UNDEF0, peerform = FORMAT_UNDEF0;
116 int keysize = -1, pkey_op = EVP_PKEY_OP_SIGN(1<<4), key_type = KEY_PRIVKEY1;
117 int engine_impl = 0;
118 int ret = 1, rv = -1;
119 size_t buf_outlen;
1
'buf_outlen' declared without an initial value
120 const char *inkey = NULL((void*)0);
121 const char *peerkey = NULL((void*)0);
122 const char *kdfalg = NULL((void*)0), *digestname = NULL((void*)0);
123 int kdflen = 0;
124 STACK_OF(OPENSSL_STRING)struct stack_st_OPENSSL_STRING *pkeyopts = NULL((void*)0);
125 STACK_OF(OPENSSL_STRING)struct stack_st_OPENSSL_STRING *pkeyopts_passin = NULL((void*)0);
126 int rawin = 0;
127 EVP_MD_CTX *mctx = NULL((void*)0);
128 EVP_MD *md = NULL((void*)0);
129 int filesize = -1;
130 OSSL_LIB_CTX *libctx = app_get0_libctx();
131
132 prog = opt_init(argc, argv, pkeyutl_options);
133 while ((o = opt_next()) != OPT_EOF) {
2
Assuming the condition is true
3
Loop condition is true. Entering loop body
5
Execution continues on line 133
6
Assuming the condition is true
7
Loop condition is true. Entering loop body
9
Execution continues on line 133
10
Assuming the condition is false
11
Loop condition is false. Execution continues on line 256
134 switch (o) {
4
Control jumps to 'case OPT_RAWIN:' at line 246
8
Control jumps to 'case OPT_INKEY:' at line 156
135 case OPT_EOF:
136 case OPT_ERR:
137 opthelp:
138 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
139 goto end;
140 case OPT_HELP:
141 opt_help(pkeyutl_options);
142 ret = 0;
143 goto end;
144 case OPT_IN:
145 infile = opt_arg();
146 break;
147 case OPT_OUT:
148 outfile = opt_arg();
149 break;
150 case OPT_SIGFILE:
151 sigfile = opt_arg();
152 break;
153 case OPT_ENGINE_IMPL:
154 engine_impl = 1;
155 break;
156 case OPT_INKEY:
157 inkey = opt_arg();
158 break;
159 case OPT_PEERKEY:
160 peerkey = opt_arg();
161 break;
162 case OPT_PASSIN:
163 passinarg = opt_arg();
164 break;
165 case OPT_PEERFORM:
166 if (!opt_format(opt_arg(), OPT_FMT_ANY( (1L << 1) | (1L << 2) | (1L << 3) | (1L <<
4) | (1L << 5) | (1L << 7) | (1L << 8) | (
1L << 9) | (1L << 10))
, &peerform))
167 goto opthelp;
168 break;
169 case OPT_KEYFORM:
170 if (!opt_format(opt_arg(), OPT_FMT_ANY( (1L << 1) | (1L << 2) | (1L << 3) | (1L <<
4) | (1L << 5) | (1L << 7) | (1L << 8) | (
1L << 9) | (1L << 10))
, &keyform))
171 goto opthelp;
172 break;
173 case OPT_R_CASESOPT_R__FIRST: case OPT_R__LAST: break; case OPT_R_RAND: case OPT_R_WRITERAND:
174 if (!opt_rand(o))
175 goto end;
176 break;
177 case OPT_CONFIG:
178 conf = app_load_config_modules(opt_arg());
179 if (conf == NULL((void*)0))
180 goto end;
181 break;
182 case OPT_PROV_CASESOPT_PROV__FIRST: case OPT_PROV__LAST: break; case OPT_PROV_PROVIDER
: case OPT_PROV_PROVIDER_PATH: case OPT_PROV_PROPQUERY
:
183 if (!opt_provider(o))
184 goto end;
185 break;
186 case OPT_ENGINE:
187 e = setup_engine(opt_arg(), 0)setup_engine_methods(opt_arg(), (unsigned int)-1, 0);
188 break;
189 case OPT_PUBIN:
190 key_type = KEY_PUBKEY2;
191 break;
192 case OPT_CERTIN:
193 key_type = KEY_CERT3;
194 break;
195 case OPT_ASN1PARSE:
196 asn1parse = 1;
197 break;
198 case OPT_HEXDUMP:
199 hexdump = 1;
200 break;
201 case OPT_SIGN:
202 pkey_op = EVP_PKEY_OP_SIGN(1<<4);
203 break;
204 case OPT_VERIFY:
205 pkey_op = EVP_PKEY_OP_VERIFY(1<<5);
206 break;
207 case OPT_VERIFYRECOVER:
208 pkey_op = EVP_PKEY_OP_VERIFYRECOVER(1<<6);
209 break;
210 case OPT_ENCRYPT:
211 pkey_op = EVP_PKEY_OP_ENCRYPT(1<<9);
212 break;
213 case OPT_DECRYPT:
214 pkey_op = EVP_PKEY_OP_DECRYPT(1<<10);
215 break;
216 case OPT_DERIVE:
217 pkey_op = EVP_PKEY_OP_DERIVE(1<<11);
218 break;
219 case OPT_KDF:
220 pkey_op = EVP_PKEY_OP_DERIVE(1<<11);
221 key_type = KEY_NONE0;
222 kdfalg = opt_arg();
223 break;
224 case OPT_KDFLEN:
225 kdflen = atoi(opt_arg());
226 break;
227 case OPT_REV:
228 rev = 1;
229 break;
230 case OPT_PKEYOPT:
231 if ((pkeyopts == NULL((void*)0) &&
232 (pkeyopts = sk_OPENSSL_STRING_new_null()((struct stack_st_OPENSSL_STRING *)OPENSSL_sk_new_null())) == NULL((void*)0)) ||
233 sk_OPENSSL_STRING_push(pkeyopts, opt_arg())OPENSSL_sk_push(ossl_check_OPENSSL_STRING_sk_type(pkeyopts), ossl_check_OPENSSL_STRING_type
(opt_arg()))
== 0) {
234 BIO_puts(bio_err, "out of memory\n");
235 goto end;
236 }
237 break;
238 case OPT_PKEYOPT_PASSIN:
239 if ((pkeyopts_passin == NULL((void*)0) &&
240 (pkeyopts_passin = sk_OPENSSL_STRING_new_null()((struct stack_st_OPENSSL_STRING *)OPENSSL_sk_new_null())) == NULL((void*)0)) ||
241 sk_OPENSSL_STRING_push(pkeyopts_passin, opt_arg())OPENSSL_sk_push(ossl_check_OPENSSL_STRING_sk_type(pkeyopts_passin
), ossl_check_OPENSSL_STRING_type(opt_arg()))
== 0) {
242 BIO_puts(bio_err, "out of memory\n");
243 goto end;
244 }
245 break;
246 case OPT_RAWIN:
247 rawin = 1;
248 break;
249 case OPT_DIGEST:
250 digestname = opt_arg();
251 break;
252 }
253 }
254
255 /* No extra arguments. */
256 argc = opt_num_rest();
257 if (argc != 0)
12
Assuming 'argc' is equal to 0
13
Taking false branch
258 goto opthelp;
259
260 if (!app_RAND_load())
14
Assuming the condition is false
261 goto end;
262
263 if (rawin
14.1
'rawin' is 1
&& pkey_op != EVP_PKEY_OP_SIGN(1<<4) && pkey_op != EVP_PKEY_OP_VERIFY(1<<5)) {
264 BIO_printf(bio_err,
265 "%s: -rawin can only be used with -sign or -verify\n",
266 prog);
267 goto opthelp;
268 }
269
270 if (digestname
14.2
'digestname' is equal to NULL
!= NULL((void*)0) && !rawin) {
271 BIO_printf(bio_err,
272 "%s: -digest can only be used with -rawin\n",
273 prog);
274 goto opthelp;
275 }
276
277 if (rawin
14.3
'rawin' is 1
&& rev
14.4
'rev' is 0
) {
15
Taking false branch
278 BIO_printf(bio_err, "%s: -rev cannot be used with raw input\n",
279 prog);
280 goto opthelp;
281 }
282
283 if (kdfalg
15.1
'kdfalg' is equal to NULL
!= NULL((void*)0)) {
16
Taking false branch
284 if (kdflen == 0) {
285 BIO_printf(bio_err,
286 "%s: no KDF length given (-kdflen parameter).\n", prog);
287 goto opthelp;
288 }
289 } else if (inkey == NULL((void*)0)) {
17
Assuming 'inkey' is not equal to NULL
290 BIO_printf(bio_err,
291 "%s: no private key given (-inkey parameter).\n", prog);
292 goto opthelp;
293 } else if (peerkey
17.1
'peerkey' is equal to NULL
!= NULL((void*)0) && pkey_op != EVP_PKEY_OP_DERIVE(1<<11)) {
294 BIO_printf(bio_err,
295 "%s: no peer key given (-peerkey parameter).\n", prog);
296 goto opthelp;
297 }
298
299 if (rawin
17.2
'rawin' is 1
) {
18
Taking true branch
300 if ((mctx = EVP_MD_CTX_new()) == NULL((void*)0)) {
19
Assuming the condition is false
20
Taking false branch
301 BIO_printf(bio_err, "Error: out of memory\n");
302 goto end;
303 }
304 }
305 ctx = init_ctx(kdfalg, &keysize, inkey, keyform, key_type,
306 passinarg, pkey_op, e, engine_impl, rawin, &pkey,
307 mctx, digestname, libctx, app_get0_propq());
308 if (ctx
20.1
'ctx' is not equal to NULL
== NULL((void*)0)) {
309 BIO_printf(bio_err, "%s: Error initializing context\n", prog);
310 goto end;
311 }
312 if (peerkey
20.2
'peerkey' is equal to NULL
!= NULL((void*)0) && !setup_peer(ctx, peerform, peerkey, e)) {
313 BIO_printf(bio_err, "%s: Error setting up peer key\n", prog);
314 goto end;
315 }
316 if (pkeyopts
20.3
'pkeyopts' is equal to NULL
!= NULL((void*)0)) {
21
Taking false branch
317 int num = sk_OPENSSL_STRING_num(pkeyopts)OPENSSL_sk_num(ossl_check_const_OPENSSL_STRING_sk_type(pkeyopts
))
;
318 int i;
319
320 for (i = 0; i < num; ++i) {
321 const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i)((char *)OPENSSL_sk_value(ossl_check_const_OPENSSL_STRING_sk_type
(pkeyopts), (i)))
;
322
323 if (pkey_ctrl_string(ctx, opt) <= 0) {
324 BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
325 prog, opt);
326 goto end;
327 }
328 }
329 }
330 if (pkeyopts_passin
21.1
'pkeyopts_passin' is equal to NULL
!= NULL((void*)0)) {
331 int num = sk_OPENSSL_STRING_num(pkeyopts_passin)OPENSSL_sk_num(ossl_check_const_OPENSSL_STRING_sk_type(pkeyopts_passin
))
;
332 int i;
333
334 for (i = 0; i < num; i++) {
335 char *opt = sk_OPENSSL_STRING_value(pkeyopts_passin, i)((char *)OPENSSL_sk_value(ossl_check_const_OPENSSL_STRING_sk_type
(pkeyopts_passin), (i)))
;
336 char *passin = strchr(opt, ':');
337 char *passwd;
338
339 if (passin == NULL((void*)0)) {
340 /* Get password interactively */
341 char passwd_buf[4096];
342 int r;
343
344 BIO_snprintf(passwd_buf, sizeof(passwd_buf), "Enter %s: ", opt);
345 r = EVP_read_pw_string(passwd_buf, sizeof(passwd_buf) - 1,
346 passwd_buf, 0);
347 if (r < 0) {
348 if (r == -2)
349 BIO_puts(bio_err, "user abort\n");
350 else
351 BIO_puts(bio_err, "entry failed\n");
352 goto end;
353 }
354 passwd = OPENSSL_strdup(passwd_buf)CRYPTO_strdup(passwd_buf, "../deps/openssl/openssl/apps/pkeyutl.c"
, 354)
;
355 if (passwd == NULL((void*)0)) {
356 BIO_puts(bio_err, "out of memory\n");
357 goto end;
358 }
359 } else {
360 /* Get password as a passin argument: First split option name
361 * and passphrase argument into two strings */
362 *passin = 0;
363 passin++;
364 if (app_passwd(passin, NULL((void*)0), &passwd, NULL((void*)0)) == 0) {
365 BIO_printf(bio_err, "failed to get '%s'\n", opt);
366 goto end;
367 }
368 }
369
370 if (EVP_PKEY_CTX_ctrl_str(ctx, opt, passwd) <= 0) {
371 BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
372 prog, opt);
373 goto end;
374 }
375 OPENSSL_free(passwd)CRYPTO_free(passwd, "../deps/openssl/openssl/apps/pkeyutl.c",
375)
;
376 }
377 }
378
379 if (sigfile
21.2
'sigfile' is equal to NULL
!= NULL((void*)0) && (pkey_op != EVP_PKEY_OP_VERIFY(1<<5))) {
380 BIO_printf(bio_err,
381 "%s: Signature file specified for non verify\n", prog);
382 goto end;
383 }
384
385 if (sigfile
21.3
'sigfile' is equal to NULL
== NULL((void*)0) && (pkey_op == EVP_PKEY_OP_VERIFY(1<<5))) {
22
Taking false branch
386 BIO_printf(bio_err,
387 "%s: No signature file specified for verify\n", prog);
388 goto end;
389 }
390
391 if (pkey_op != EVP_PKEY_OP_DERIVE(1<<11)) {
23
Taking true branch
392 in = bio_open_default(infile, 'r', FORMAT_BINARY2);
393 if (infile
23.1
'infile' is equal to NULL
!= NULL((void*)0)) {
24
Taking false branch
394 struct stat st;
395
396 if (stat(infile, &st) == 0 && st.st_size <= INT_MAX2147483647)
397 filesize = (int)st.st_size;
398 }
399 if (in == NULL((void*)0))
25
Assuming 'in' is not equal to NULL
26
Taking false branch
400 goto end;
401 }
402 out = bio_open_default(outfile, 'w', FORMAT_BINARY2);
403 if (out == NULL((void*)0))
27
Assuming 'out' is not equal to NULL
28
Taking false branch
404 goto end;
405
406 if (sigfile
28.1
'sigfile' is equal to NULL
!= NULL((void*)0)) {
407 BIO *sigbio = BIO_new_file(sigfile, "rb");
408
409 if (sigbio == NULL((void*)0)) {
410 BIO_printf(bio_err, "Can't open signature file %s\n", sigfile);
411 goto end;
412 }
413 siglen = bio_to_mem(&sig, keysize * 10, sigbio);
414 BIO_free(sigbio);
415 if (siglen < 0) {
416 BIO_printf(bio_err, "Error reading signature data\n");
417 goto end;
418 }
419 }
420
421 /* Raw input data is handled elsewhere */
422 if (in
28.2
'in' is not equal to NULL
!= NULL((void*)0) && !rawin
28.3
'rawin' is 1
) {
423 /* Read the input data */
424 buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
425 if (buf_inlen < 0) {
426 BIO_printf(bio_err, "Error reading input Data\n");
427 goto end;
428 }
429 if (rev) {
430 size_t i;
431 unsigned char ctmp;
432 size_t l = (size_t)buf_inlen;
433 for (i = 0; i < l / 2; i++) {
434 ctmp = buf_in[i];
435 buf_in[i] = buf_in[l - 1 - i];
436 buf_in[l - 1 - i] = ctmp;
437 }
438 }
439 }
440
441 /* Sanity check the input if the input is not raw */
442 if (!rawin
28.4
'rawin' is 1
443 && buf_inlen > EVP_MAX_MD_SIZE64 444 && (pkey_op == EVP_PKEY_OP_SIGN(1<<4) 445 || pkey_op == EVP_PKEY_OP_VERIFY(1<<5))) { 446 BIO_printf(bio_err, 447 "Error: The input data looks too long to be a hash\n"); 448 goto end; 449 } 450 451 if (pkey_op == EVP_PKEY_OP_VERIFY(1<<5)) {
29
Taking false branch
452 if (rawin) { 453 rv = do_raw_keyop(pkey_op, mctx, pkey, in, filesize, sig, siglen, 454 NULL((void*)0), 0); 455 } else { 456 rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen, 457 buf_in, (size_t)buf_inlen); 458 } 459 if (rv == 1) { 460 BIO_puts(out, "Signature Verified Successfully\n"); 461 ret = 0; 462 } else { 463 BIO_puts(out, "Signature Verification Failure\n"); 464 } 465 goto end; 466 } 467 if (rawin
29.1
'rawin' is 1
) {
30
Taking true branch
468 /* rawin allocates the buffer in do_raw_keyop() */ 469 rv = do_raw_keyop(pkey_op, mctx, pkey, in, filesize, NULL((void*)0), 0,
31
Calling 'do_raw_keyop'
45
Returning from 'do_raw_keyop'
470 &buf_out, (size_t *)&buf_outlen); 471 } else { 472 if (kdflen != 0) { 473 buf_outlen = kdflen; 474 rv = 1; 475 } else { 476 rv = do_keyop(ctx, pkey_op, NULL((void*)0), (size_t *)&buf_outlen, 477 buf_in, (size_t)buf_inlen); 478 } 479 if (rv > 0 && buf_outlen != 0) { 480 buf_out = app_malloc(buf_outlen, "buffer output"); 481 rv = do_keyop(ctx, pkey_op, 482 buf_out, (size_t *)&buf_outlen, 483 buf_in, (size_t)buf_inlen); 484 } 485 } 486 if (rv <= 0) {
46
Assuming 'rv' is > 0
47
Taking false branch
487 if (pkey_op != EVP_PKEY_OP_DERIVE(1<<11)) { 488 BIO_puts(bio_err, "Public Key operation error\n"); 489 } else { 490 BIO_puts(bio_err, "Key derivation failed\n"); 491 } 492 goto end; 493 } 494 ret = 0; 495 496 if (asn1parse
47.1
'asn1parse' is 0
) {
48
Taking false branch
497 if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1)) 498 ERR_print_errors(bio_err); /* but still return success */ 499 } else if (hexdump
48.1
'hexdump' is 0
) {
49
Taking false branch
500 BIO_dump(out, (char *)buf_out, buf_outlen); 501 } else { 502 BIO_write(out, buf_out, buf_outlen);
50
3rd function call argument is an uninitialized value
503 } 504 505 end: 506 if (ret != 0) 507 ERR_print_errors(bio_err); 508 EVP_MD_CTX_free(mctx); 509 EVP_PKEY_CTX_free(ctx); 510 EVP_MD_free(md); 511 release_engine(e); 512 BIO_free(in); 513 BIO_free_all(out); 514 OPENSSL_free(buf_in)CRYPTO_free(buf_in, "../deps/openssl/openssl/apps/pkeyutl.c",
514)
; 515 OPENSSL_free(buf_out)CRYPTO_free(buf_out, "../deps/openssl/openssl/apps/pkeyutl.c"
, 515)
; 516 OPENSSL_free(sig)CRYPTO_free(sig, "../deps/openssl/openssl/apps/pkeyutl.c", 516
)
; 517 sk_OPENSSL_STRING_free(pkeyopts)OPENSSL_sk_free(ossl_check_OPENSSL_STRING_sk_type(pkeyopts)); 518 sk_OPENSSL_STRING_free(pkeyopts_passin)OPENSSL_sk_free(ossl_check_OPENSSL_STRING_sk_type(pkeyopts_passin
))
; 519 NCONF_free(conf); 520 return ret; 521} 522 523static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize, 524 const char *keyfile, int keyform, int key_type, 525 char *passinarg, int pkey_op, ENGINE *e, 526 const int engine_impl, int rawin, 527 EVP_PKEY **ppkey, EVP_MD_CTX *mctx, const char *digestname, 528 OSSL_LIB_CTX *libctx, const char *propq) 529{ 530 EVP_PKEY *pkey = NULL((void*)0); 531 EVP_PKEY_CTX *ctx = NULL((void*)0); 532 ENGINE *impl = NULL((void*)0); 533 char *passin = NULL((void*)0); 534 int rv = -1; 535 X509 *x; 536 537 if (((pkey_op == EVP_PKEY_OP_SIGN(1<<4)) || (pkey_op == EVP_PKEY_OP_DECRYPT(1<<10)) 538 || (pkey_op == EVP_PKEY_OP_DERIVE(1<<11))) 539 && (key_type != KEY_PRIVKEY1 && kdfalg == NULL((void*)0))) { 540 BIO_printf(bio_err, "A private key is needed for this operation\n"); 541 goto end; 542 } 543 if (!app_passwd(passinarg, NULL((void*)0), &passin, NULL((void*)0))) { 544 BIO_printf(bio_err, "Error getting password\n"); 545 goto end; 546 } 547 switch (key_type) { 548 case KEY_PRIVKEY1: 549 pkey = load_key(keyfile, keyform, 0, passin, e, "private key"); 550 break; 551 552 case KEY_PUBKEY2: 553 pkey = load_pubkey(keyfile, keyform, 0, NULL((void*)0), e, "public key"); 554 break; 555 556 case KEY_CERT3: 557 x = load_cert(keyfile, keyform, "Certificate")load_cert_pass(keyfile, keyform, 1, ((void*)0), "Certificate"
)
; 558 if (x) { 559 pkey = X509_get_pubkey(x); 560 X509_free(x); 561 } 562 break; 563 564 case KEY_NONE0: 565 break; 566 567 } 568 569#ifndef OPENSSL_NO_ENGINE 570 if (engine_impl) 571 impl = e; 572#endif 573 574 if (kdfalg != NULL((void*)0)) { 575 int kdfnid = OBJ_sn2nid(kdfalg); 576 577 if (kdfnid == NID_undef0) { 578 kdfnid = OBJ_ln2nid(kdfalg); 579 if (kdfnid == NID_undef0) { 580 BIO_printf(bio_err, "The given KDF \"%s\" is unknown.\n", 581 kdfalg); 582 goto end; 583 } 584 } 585 if (impl != NULL((void*)0)) 586 ctx = EVP_PKEY_CTX_new_id(kdfnid, impl); 587 else 588 ctx = EVP_PKEY_CTX_new_from_name(libctx, kdfalg, propq); 589 } else { 590 if (pkey == NULL((void*)0)) 591 goto end; 592 593 *pkeysize = EVP_PKEY_get_size(pkey); 594 if (impl != NULL((void*)0)) 595 ctx = EVP_PKEY_CTX_new(pkey, impl); 596 else 597 ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq); 598 if (ppkey != NULL((void*)0)) 599 *ppkey = pkey; 600 EVP_PKEY_free(pkey); 601 } 602 603 if (ctx == NULL((void*)0)) 604 goto end; 605 606 if (rawin) { 607 EVP_MD_CTX_set_pkey_ctx(mctx, ctx); 608 609 switch (pkey_op) { 610 case EVP_PKEY_OP_SIGN(1<<4): 611 rv = EVP_DigestSignInit_ex(mctx, NULL((void*)0), digestname, libctx, propq, 612 pkey, NULL((void*)0)); 613 break; 614 615 case EVP_PKEY_OP_VERIFY(1<<5): 616 rv = EVP_DigestVerifyInit_ex(mctx, NULL((void*)0), digestname, libctx, propq, 617 pkey, NULL((void*)0)); 618 break; 619 } 620 621 } else { 622 switch (pkey_op) { 623 case EVP_PKEY_OP_SIGN(1<<4): 624 rv = EVP_PKEY_sign_init(ctx); 625 break; 626 627 case EVP_PKEY_OP_VERIFY(1<<5): 628 rv = EVP_PKEY_verify_init(ctx); 629 break; 630 631 case EVP_PKEY_OP_VERIFYRECOVER(1<<6): 632 rv = EVP_PKEY_verify_recover_init(ctx); 633 break; 634 635 case EVP_PKEY_OP_ENCRYPT(1<<9): 636 rv = EVP_PKEY_encrypt_init(ctx); 637 break; 638 639 case EVP_PKEY_OP_DECRYPT(1<<10): 640 rv = EVP_PKEY_decrypt_init(ctx); 641 break; 642 643 case EVP_PKEY_OP_DERIVE(1<<11): 644 rv = EVP_PKEY_derive_init(ctx); 645 break; 646 } 647 } 648 649 if (rv <= 0) { 650 EVP_PKEY_CTX_free(ctx); 651 ctx = NULL((void*)0); 652 } 653 654 end: 655 OPENSSL_free(passin)CRYPTO_free(passin, "../deps/openssl/openssl/apps/pkeyutl.c",
655)
; 656 return ctx; 657 658} 659 660static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file, 661 ENGINE *e) 662{ 663 EVP_PKEY *peer = NULL((void*)0); 664 ENGINE *engine = NULL((void*)0); 665 int ret; 666 667 if (peerform == FORMAT_ENGINE8) 668 engine = e; 669 peer = load_pubkey(file, peerform, 0, NULL((void*)0), engine, "peer key"); 670 if (peer == NULL((void*)0)) { 671 BIO_printf(bio_err, "Error reading peer key %s\n", file); 672 return 0; 673 } 674 675 ret = EVP_PKEY_derive_set_peer(ctx, peer) > 0; 676 677 EVP_PKEY_free(peer); 678 return ret; 679} 680 681static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op, 682 unsigned char *out, size_t *poutlen, 683 const unsigned char *in, size_t inlen) 684{ 685 int rv = 0; 686 switch (pkey_op) { 687 case EVP_PKEY_OP_VERIFYRECOVER(1<<6): 688 rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen); 689 break; 690 691 case EVP_PKEY_OP_SIGN(1<<4): 692 rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen); 693 break; 694 695 case EVP_PKEY_OP_ENCRYPT(1<<9): 696 rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen); 697 break; 698 699 case EVP_PKEY_OP_DECRYPT(1<<10): 700 rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen); 701 break; 702 703 case EVP_PKEY_OP_DERIVE(1<<11): 704 rv = EVP_PKEY_derive(ctx, out, poutlen); 705 break; 706 707 } 708 return rv; 709} 710 711#define TBUF_MAXSIZE2048 2048 712 713static int do_raw_keyop(int pkey_op, EVP_MD_CTX *mctx, 714 EVP_PKEY *pkey, BIO *in, 715 int filesize, unsigned char *sig, int siglen, 716 unsigned char **out, size_t *poutlen) 717{ 718 int rv = 0; 719 unsigned char tbuf[TBUF_MAXSIZE2048]; 720 unsigned char *mbuf = NULL((void*)0); 721 int buf_len = 0; 722 723 /* Some algorithms only support oneshot digests */ 724 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED255191087
32
Assuming the condition is false
34
Taking false branch
725 || EVP_PKEY_get_id(pkey) == EVP_PKEY_ED4481088) {
33
Assuming the condition is false
726 if (filesize < 0) { 727 BIO_printf(bio_err, 728 "Error: unable to determine file size for oneshot operation\n"); 729 goto end; 730 } 731 mbuf = app_malloc(filesize, "oneshot sign/verify buffer"); 732 switch(pkey_op) { 733 case EVP_PKEY_OP_VERIFY(1<<5): 734 buf_len = BIO_read(in, mbuf, filesize); 735 if (buf_len != filesize) { 736 BIO_printf(bio_err, "Error reading raw input data\n"); 737 goto end; 738 } 739 rv = EVP_DigestVerify(mctx, sig, (size_t)siglen, mbuf, buf_len); 740 break; 741 case EVP_PKEY_OP_SIGN(1<<4): 742 buf_len = BIO_read(in, mbuf, filesize); 743 if (buf_len != filesize) { 744 BIO_printf(bio_err, "Error reading raw input data\n"); 745 goto end; 746 } 747 rv = EVP_DigestSign(mctx, NULL((void*)0), poutlen, mbuf, buf_len); 748 if (rv == 1 && out != NULL((void*)0)) { 749 *out = app_malloc(*poutlen, "buffer output"); 750 rv = EVP_DigestSign(mctx, *out, poutlen, mbuf, buf_len); 751 } 752 break; 753 } 754 goto end; 755 } 756 757 switch(pkey_op) {
35
Control jumps to 'case 16:' at line 775
758 case EVP_PKEY_OP_VERIFY(1<<5): 759 for (;;) { 760 buf_len = BIO_read(in, tbuf, TBUF_MAXSIZE2048); 761 if (buf_len == 0) 762 break; 763 if (buf_len < 0) { 764 BIO_printf(bio_err, "Error reading raw input data\n"); 765 goto end; 766 } 767 rv = EVP_DigestVerifyUpdate(mctx, tbuf, (size_t)buf_len); 768 if (rv != 1) { 769 BIO_printf(bio_err, "Error verifying raw input data\n"); 770 goto end; 771 } 772 } 773 rv = EVP_DigestVerifyFinal(mctx, sig, (size_t)siglen); 774 break; 775 case EVP_PKEY_OP_SIGN(1<<4): 776 for (;;) {
36
Loop condition is true. Entering loop body
777 buf_len = BIO_read(in, tbuf, TBUF_MAXSIZE2048); 778 if (buf_len == 0)
37
Assuming 'buf_len' is not equal to 0
38
Taking false branch
779 break; 780 if (buf_len < 0) {
39
Assuming 'buf_len' is >= 0
40
Taking false branch
781 BIO_printf(bio_err, "Error reading raw input data\n"); 782 goto end; 783 } 784 rv = EVP_DigestSignUpdate(mctx, tbuf, (size_t)buf_len); 785 if (rv != 1) {
41
Assuming 'rv' is not equal to 1
42
Taking true branch
786 BIO_printf(bio_err, "Error signing raw input data\n"); 787 goto end;
43
Control jumps to line 799
788 } 789 } 790 rv = EVP_DigestSignFinal(mctx, NULL((void*)0), poutlen); 791 if (rv == 1 && out != NULL((void*)0)) { 792 *out = app_malloc(*poutlen, "buffer output"); 793 rv = EVP_DigestSignFinal(mctx, *out, poutlen); 794 } 795 break; 796 } 797 798 end: 799 OPENSSL_free(mbuf)CRYPTO_free(mbuf, "../deps/openssl/openssl/apps/pkeyutl.c", 799
)
; 800 return rv;
44
Returning without writing to '*poutlen'
801}