Bug Summary

File:out/../deps/openssl/openssl/crypto/cms/cms_enc.c
Warning:line 165, column 13
Null pointer passed to 2nd parameter expecting 'nonnull'

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 cms_enc.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_NO_HW -D OPENSSL_API_COMPAT=0x10100001L -D STATIC_LEGACY -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 MODULESDIR="/home/maurizio/node-v18.6.0/out/Release/obj.target/deps/openssl/lib/openssl-modules" -D OPENSSLDIR="/home/maurizio/node-v18.6.0/out/Release/obj.target/deps/openssl" -D OPENSSLDIR="/etc/ssl" -D ENGINESDIR="/dev/null" -D TERMIOS -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 -I ../deps/openssl/config/archs/linux-x86_64/asm/include -I ../deps/openssl/config/archs/linux-x86_64/asm/crypto -I ../deps/openssl/config/archs/linux-x86_64/asm/crypto/include/internal -I ../deps/openssl/config/archs/linux-x86_64/asm/providers/common/include -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/crypto/cms/cms_enc.c
1/*
2 * Copyright 2008-2021 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 "internal/cryptlib.h"
11#include <openssl/asn1t.h>
12#include <openssl/pem.h>
13#include <openssl/x509v3.h>
14#include <openssl/err.h>
15#include <openssl/cms.h>
16#include <openssl/rand.h>
17#include "crypto/evp.h"
18#include "cms_local.h"
19
20/* CMS EncryptedData Utilities */
21
22/* Return BIO based on EncryptedContentInfo and key */
23
24BIO *ossl_cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec,
25 const CMS_CTX *cms_ctx)
26{
27 BIO *b;
28 EVP_CIPHER_CTX *ctx;
29 EVP_CIPHER *fetched_ciph = NULL((void*)0);
30 const EVP_CIPHER *cipher = NULL((void*)0);
31 X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
32 evp_cipher_aead_asn1_params aparams;
33 unsigned char iv[EVP_MAX_IV_LENGTH16], *piv = NULL((void*)0);
5
'piv' initialized to a null pointer value
34 unsigned char *tkey = NULL((void*)0);
35 int len;
36 int ivlen = 0;
37 size_t tkeylen = 0;
38 int ok = 0;
39 int enc, keep_key = 0;
40 OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(cms_ctx);
41 const char *propq = ossl_cms_ctx_get0_propq(cms_ctx);
42
43 enc = ec->cipher
5.1
Field 'cipher' is non-null
? 1 : 0;
6
'?' condition is true
44
45 b = BIO_new(BIO_f_cipher());
46 if (b == NULL((void*)0)) {
7
Assuming 'b' is not equal to NULL
8
Taking false branch
47 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE)(ERR_new(), ERR_set_debug("../deps/openssl/openssl/crypto/cms/cms_enc.c"
,47,__func__), ERR_set_error)((46),((256|((0x1 << 18L)|
(0x2 << 18L)))),((void*)0))
;
48 return NULL((void*)0);
49 }
50
51 BIO_get_cipher_ctx(b, &ctx)BIO_ctrl(b,129,0,(&ctx));
52
53 (void)ERR_set_mark();
54 if (enc
8.1
'enc' is 1
) {
9
Taking true branch
55 cipher = ec->cipher;
56 /*
57 * If not keeping key set cipher to NULL so subsequent calls decrypt.
58 */
59 if (ec->key != NULL((void*)0))
10
Assuming field 'key' is not equal to NULL
11
Taking true branch
60 ec->cipher = NULL((void*)0);
61 } else {
62 cipher = EVP_get_cipherbyobj(calg->algorithm)EVP_get_cipherbyname(OBJ_nid2sn(OBJ_obj2nid(calg->algorithm
)))
;
63 }
64 if (cipher
11.1
'cipher' is not equal to NULL
!= NULL((void*)0)) {
12
Taking true branch
65 fetched_ciph = EVP_CIPHER_fetch(libctx, EVP_CIPHER_get0_name(cipher),
66 propq);
67 if (fetched_ciph != NULL((void*)0))
13
Assuming 'fetched_ciph' is equal to NULL
14
Taking false branch
68 cipher = fetched_ciph;
69 }
70 if (cipher
14.1
'cipher' is not equal to NULL
== NULL((void*)0)) {
15
Taking false branch
71 (void)ERR_clear_last_mark();
72 ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER)(ERR_new(), ERR_set_debug("../deps/openssl/openssl/crypto/cms/cms_enc.c"
,72,__func__), ERR_set_error)((46),(148),((void*)0))
;
73 goto err;
74 }
75 (void)ERR_pop_to_mark();
76
77 if (EVP_CipherInit_ex(ctx, cipher, NULL((void*)0), NULL((void*)0), NULL((void*)0), enc) <= 0) {
16
Assuming the condition is false
17
Taking false branch
78 ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_INITIALISATION_ERROR)(ERR_new(), ERR_set_debug("../deps/openssl/openssl/crypto/cms/cms_enc.c"
,78,__func__), ERR_set_error)((46),(101),((void*)0))
;
79 goto err;
80 }
81
82 if (enc
17.1
'enc' is 1
) {
18
Taking true branch
83 calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_get_type(ctx)EVP_CIPHER_get_type(EVP_CIPHER_CTX_get0_cipher(ctx)));
84 /* Generate a random IV if we need one */
85 ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
86 if (ivlen > 0) {
19
Assuming 'ivlen' is <= 0
20
Taking false branch
87 if (RAND_bytes_ex(libctx, iv, ivlen, 0) <= 0)
88 goto err;
89 piv = iv;
90 }
91 } else {
92 if (evp_cipher_asn1_to_param_ex(ctx, calg->parameter, &aparams) <= 0) {
93 ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR)(ERR_new(), ERR_set_debug("../deps/openssl/openssl/crypto/cms/cms_enc.c"
,93,__func__), ERR_set_error)((46),(102),((void*)0))
;
94 goto err;
95 }
96 if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER0x200000)) {
97 piv = aparams.iv;
98 if (ec->taglen > 0
99 && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG0x11,
100 ec->taglen, ec->tag) <= 0) {
101 ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_AEAD_SET_TAG_ERROR)(ERR_new(), ERR_set_debug("../deps/openssl/openssl/crypto/cms/cms_enc.c"
,101,__func__), ERR_set_error)((46),(184),((void*)0))
;
102 goto err;
103 }
104 }
105 }
106 len = EVP_CIPHER_CTX_get_key_length(ctx);
107 if (len <= 0)
21
Assuming 'len' is > 0
22
Taking false branch
108 goto err;
109 tkeylen = (size_t)len;
110
111 /* Generate random session key */
112 if (!enc
22.1
'enc' is 1
|| !ec->key
22.2
Field 'key' is non-null
) {
23
Taking false branch
113 tkey = OPENSSL_malloc(tkeylen)CRYPTO_malloc(tkeylen, "../deps/openssl/openssl/crypto/cms/cms_enc.c"
, 113)
;
114 if (tkey == NULL((void*)0)) {
115 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE)(ERR_new(), ERR_set_debug("../deps/openssl/openssl/crypto/cms/cms_enc.c"
,115,__func__), ERR_set_error)((46),((256|((0x1 << 18L)
|(0x2 << 18L)))),((void*)0))
;
116 goto err;
117 }
118 if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0)
119 goto err;
120 }
121
122 if (!ec->key
23.1
Field 'key' is non-null
) {
24
Taking false branch
123 ec->key = tkey;
124 ec->keylen = tkeylen;
125 tkey = NULL((void*)0);
126 if (enc)
127 keep_key = 1;
128 else
129 ERR_clear_error();
130
131 }
132
133 if (ec->keylen != tkeylen) {
25
Assuming 'tkeylen' is equal to field 'keylen'
26
Taking false branch
134 /* If necessary set key length */
135 if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0) {
136 /*
137 * Only reveal failure if debugging so we don't leak information
138 * which may be useful in MMA.
139 */
140 if (enc || ec->debug) {
141 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH)(ERR_new(), ERR_set_debug("../deps/openssl/openssl/crypto/cms/cms_enc.c"
,141,__func__), ERR_set_error)((46),(118),((void*)0))
;
142 goto err;
143 } else {
144 /* Use random key */
145 OPENSSL_clear_free(ec->key, ec->keylen)CRYPTO_clear_free(ec->key, ec->keylen, "../deps/openssl/openssl/crypto/cms/cms_enc.c"
, 145)
;
146 ec->key = tkey;
147 ec->keylen = tkeylen;
148 tkey = NULL((void*)0);
149 ERR_clear_error();
150 }
151 }
152 }
153
154 if (EVP_CipherInit_ex(ctx, NULL((void*)0), NULL((void*)0), ec->key, piv, enc) <= 0) {
27
Assuming the condition is false
28
Taking false branch
155 ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_INITIALISATION_ERROR)(ERR_new(), ERR_set_debug("../deps/openssl/openssl/crypto/cms/cms_enc.c"
,155,__func__), ERR_set_error)((46),(101),((void*)0))
;
156 goto err;
157 }
158 if (enc
28.1
'enc' is 1
) {
29
Taking true branch
159 calg->parameter = ASN1_TYPE_new();
160 if (calg->parameter == NULL((void*)0)) {
30
Assuming field 'parameter' is not equal to NULL
31
Taking false branch
161 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE)(ERR_new(), ERR_set_debug("../deps/openssl/openssl/crypto/cms/cms_enc.c"
,161,__func__), ERR_set_error)((46),((256|((0x1 << 18L)
|(0x2 << 18L)))),((void*)0))
;
162 goto err;
163 }
164 if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER0x200000)) {
32
Assuming the condition is true
33
Taking true branch
165 memcpy(aparams.iv, piv, ivlen);
34
Null pointer passed to 2nd parameter expecting 'nonnull'
166 aparams.iv_len = ivlen;
167 aparams.tag_len = EVP_CIPHER_CTX_get_tag_length(ctx);
168 if (aparams.tag_len <= 0)
169 goto err;
170 }
171
172 if (evp_cipher_param_to_asn1_ex(ctx, calg->parameter, &aparams) <= 0) {
173 ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR)(ERR_new(), ERR_set_debug("../deps/openssl/openssl/crypto/cms/cms_enc.c"
,173,__func__), ERR_set_error)((46),(102),((void*)0))
;
174 goto err;
175 }
176 /* If parameter type not set omit parameter */
177 if (calg->parameter->type == V_ASN1_UNDEF-1) {
178 ASN1_TYPE_free(calg->parameter);
179 calg->parameter = NULL((void*)0);
180 }
181 }
182 ok = 1;
183
184 err:
185 EVP_CIPHER_free(fetched_ciph);
186 if (!keep_key || !ok) {
187 OPENSSL_clear_free(ec->key, ec->keylen)CRYPTO_clear_free(ec->key, ec->keylen, "../deps/openssl/openssl/crypto/cms/cms_enc.c"
, 187)
;
188 ec->key = NULL((void*)0);
189 }
190 OPENSSL_clear_free(tkey, tkeylen)CRYPTO_clear_free(tkey, tkeylen, "../deps/openssl/openssl/crypto/cms/cms_enc.c"
, 190)
;
191 if (ok)
192 return b;
193 BIO_free(b);
194 return NULL((void*)0);
195}
196
197int ossl_cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
198 const EVP_CIPHER *cipher,
199 const unsigned char *key, size_t keylen,
200 const CMS_CTX *cms_ctx)
201{
202 ec->cipher = cipher;
203 if (key) {
204 if ((ec->key = OPENSSL_malloc(keylen)CRYPTO_malloc(keylen, "../deps/openssl/openssl/crypto/cms/cms_enc.c"
, 204)
) == NULL((void*)0)) {
205 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE)(ERR_new(), ERR_set_debug("../deps/openssl/openssl/crypto/cms/cms_enc.c"
,205,__func__), ERR_set_error)((46),((256|((0x1 << 18L)
|(0x2 << 18L)))),((void*)0))
;
206 return 0;
207 }
208 memcpy(ec->key, key, keylen);
209 }
210 ec->keylen = keylen;
211 if (cipher != NULL((void*)0))
212 ec->contentType = OBJ_nid2obj(NID_pkcs7_data21);
213 return 1;
214}
215
216int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph,
217 const unsigned char *key, size_t keylen)
218{
219 CMS_EncryptedContentInfo *ec;
220
221 if (!key || !keylen) {
222 ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY)(ERR_new(), ERR_set_debug("../deps/openssl/openssl/crypto/cms/cms_enc.c"
,222,__func__), ERR_set_error)((46),(130),((void*)0))
;
223 return 0;
224 }
225 if (ciph) {
226 cms->d.encryptedData = M_ASN1_new_of(CMS_EncryptedData)(CMS_EncryptedData *)ASN1_item_new((CMS_EncryptedData_it()));
227 if (!cms->d.encryptedData) {
228 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE)(ERR_new(), ERR_set_debug("../deps/openssl/openssl/crypto/cms/cms_enc.c"
,228,__func__), ERR_set_error)((46),((256|((0x1 << 18L)
|(0x2 << 18L)))),((void*)0))
;
229 return 0;
230 }
231 cms->contentType = OBJ_nid2obj(NID_pkcs7_encrypted26);
232 cms->d.encryptedData->version = 0;
233 } else if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_encrypted26) {
234 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_ENCRYPTED_DATA)(ERR_new(), ERR_set_debug("../deps/openssl/openssl/crypto/cms/cms_enc.c"
,234,__func__), ERR_set_error)((46),(122),((void*)0))
;
235 return 0;
236 }
237 ec = cms->d.encryptedData->encryptedContentInfo;
238 return ossl_cms_EncryptedContent_init(ec, ciph, key, keylen,
239 ossl_cms_get0_cmsctx(cms));
240}
241
242BIO *ossl_cms_EncryptedData_init_bio(const CMS_ContentInfo *cms)
243{
244 CMS_EncryptedData *enc = cms->d.encryptedData;
245 if (enc->encryptedContentInfo->cipher && enc->unprotectedAttrs)
1
Assuming field 'cipher' is non-null
2
Assuming field 'unprotectedAttrs' is null
3
Taking false branch
246 enc->version = 2;
247 return ossl_cms_EncryptedContent_init_bio(enc->encryptedContentInfo,
4
Calling 'ossl_cms_EncryptedContent_init_bio'
248 ossl_cms_get0_cmsctx(cms));
249}