Skip to content

Commit 264a879

Browse files
olszomalmtrojnar
authored andcommitted
Added deallocation of allocated structures in tests
1 parent 56f80c3 commit 264a879

File tree

7 files changed

+70
-28
lines changed

7 files changed

+70
-28
lines changed

tests/check-privkey.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ int main(int argc, char *argv[])
125125
ret = 1;
126126
goto end;
127127
}
128+
/*
129+
* ENGINE_init() returned a functional reference, so free the structural
130+
* reference from ENGINE_by_id().
131+
*/
132+
ENGINE_free(engine);
128133

129134
if (!strncmp(certfile, "pkcs11:", 7)) {
130135
params.cert_id = certfile;
@@ -154,17 +159,18 @@ int main(int argc, char *argv[])
154159
}
155160

156161
pkey = ENGINE_load_private_key(engine, privkey, 0, 0);
157-
158162
if (pkey == NULL) {
159163
printf("Could not load key\n");
160164
display_openssl_errors(__LINE__);
161165
ret = 1;
162166
goto end;
163167
}
164168

169+
/* Free the functional reference from ENGINE_init */
165170
ENGINE_finish(engine);
166171

167172
ret = X509_check_private_key(cert, pkey);
173+
EVP_PKEY_free(pkey);
168174
if (!ret) {
169175
printf("Could not check private key\n");
170176
display_openssl_errors(__LINE__);
@@ -178,7 +184,6 @@ int main(int argc, char *argv[])
178184
CONF_modules_unload(1);
179185
end:
180186
X509_free(cert);
181-
EVP_PKEY_free(pkey);
182187

183188
return ret;
184189
}

tests/evp-sign.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ int main(int argc, char **argv)
220220
display_openssl_errors(__LINE__);
221221
exit(1);
222222
}
223+
/*
224+
* ENGINE_init() returned a functional reference, so free the structural
225+
* reference from ENGINE_by_id().
226+
*/
227+
ENGINE_free(e);
223228

224229
switch (pin_method) {
225230
case BY_DEFAULT:
@@ -253,6 +258,9 @@ int main(int argc, char **argv)
253258
exit(1);
254259
}
255260

261+
/* Free the functional reference from ENGINE_init */
262+
ENGINE_finish(e);
263+
256264
digest_algo = EVP_get_digestbyname("sha256");
257265

258266
ctx = EVP_MD_CTX_create();
@@ -275,6 +283,7 @@ int main(int argc, char **argv)
275283
exit(1);
276284
}
277285
EVP_MD_CTX_destroy(ctx);
286+
EVP_PKEY_free(private_key);
278287

279288
printf("Signature created\n");
280289

@@ -301,6 +310,7 @@ int main(int argc, char **argv)
301310
exit(1);
302311
}
303312
EVP_MD_CTX_destroy(ctx);
313+
EVP_PKEY_free(public_key);
304314

305315
printf("Signature verified\n");
306316

@@ -310,8 +320,10 @@ int main(int argc, char **argv)
310320

311321
#endif /* OPENSSL_VERSION_NUMBER >= 0x1000000fL */
312322

313-
ENGINE_finish(e);
314323
CONF_modules_unload(1);
324+
UI_destroy_method(ui_detect_failed_ctrl);
325+
UI_destroy_method(ui_console_with_default);
326+
315327
return 0;
316328
}
317329

tests/fork-change-slot.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -232,20 +232,22 @@ int main(int argc, char *argv[])
232232
}
233233

234234
/* Initialize to get the engine functional reference */
235-
if (ENGINE_init(engine)) {
236-
pkey = ENGINE_load_private_key(engine, argv[1], 0, 0);
237-
if (pkey == NULL) {
238-
error_queue("ENGINE_load_private_key", pid);
239-
goto failed;
240-
}
241-
242-
ENGINE_free(engine);
243-
engine = NULL;
244-
}
245-
else {
235+
if (!ENGINE_init(engine)) {
236+
printf("Could not initialize engine\n");
246237
error_queue("ENGINE_init", pid);
247238
goto failed;
248239
}
240+
/*
241+
* ENGINE_init() returned a functional reference, so free the structural
242+
* reference from ENGINE_by_id().
243+
*/
244+
ENGINE_free(engine);
245+
246+
pkey = ENGINE_load_private_key(engine, argv[1], 0, 0);
247+
if (pkey == NULL) {
248+
error_queue("ENGINE_load_private_key", pid);
249+
goto failed;
250+
}
249251

250252
/* Spawn processes and check child return */
251253
if (spawn_processes(num_processes)) {
@@ -307,8 +309,9 @@ int main(int argc, char *argv[])
307309
EVP_MD_CTX_destroy(md_ctx);
308310
if (pkey != NULL)
309311
EVP_PKEY_free(pkey);
310-
if (engine != NULL)
311-
ENGINE_free(engine);
312+
313+
/* Free the functional reference from ENGINE_init */
314+
ENGINE_finish(engine);
312315

313316
return rv;
314317
}

tests/fork-test.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ int main(int argc, char *argv[])
195195
goto failed;
196196
}
197197

198+
do_fork();
198199
/* ask for a sha256 hash of the random data, signed by the key */
199200
siglen = MAX_SIGSIZE;
200201
signature = OPENSSL_malloc(MAX_SIGSIZE);
@@ -203,7 +204,6 @@ int main(int argc, char *argv[])
203204

204205
digest_algo = EVP_get_digestbyname("sha256");
205206

206-
do_fork();
207207
privkey = PKCS11_get_private_key(authkey);
208208
if (privkey == NULL) {
209209
fprintf(stderr, "Could not extract the private key\n");
@@ -255,18 +255,16 @@ int main(int argc, char *argv[])
255255
error_queue("EVP_VerifyFinal");
256256
goto failed;
257257
}
258+
EVP_MD_CTX_destroy(md_ctx);
259+
258260
printf("Signature matched\n");
259261

260-
if (md_ctx != NULL)
261-
EVP_MD_CTX_destroy(md_ctx);
262-
if (privkey != NULL)
263-
EVP_PKEY_free(privkey);
264-
if (pubkey != NULL)
265-
EVP_PKEY_free(pubkey);
266-
if (random != NULL)
267-
OPENSSL_free(random);
268-
if (signature != NULL)
269-
OPENSSL_free(signature);
262+
/* If key is NULL nothing is done */
263+
EVP_PKEY_free(privkey);
264+
EVP_PKEY_free(pubkey);
265+
266+
OPENSSL_free(random);
267+
OPENSSL_free(signature);
270268

271269
PKCS11_release_all_slots(ctx, slots, nslots);
272270
PKCS11_CTX_unload(ctx);

tests/rsa-oaep.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ int main(int argc, char **argv)
131131
display_openssl_errors(__LINE__);
132132
exit(1);
133133
}
134+
/*
135+
* ENGINE_init() returned a functional reference, so free the structural
136+
* reference from ENGINE_by_id().
137+
*/
138+
ENGINE_free(e);
134139

135140
if (key_pass && !ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0)) {
136141
display_openssl_errors(__LINE__);
@@ -160,7 +165,6 @@ int main(int argc, char **argv)
160165

161166
/* Encrypt the data */
162167
pkey_ctx = EVP_PKEY_CTX_new(public_key, e);
163-
164168
if (pkey_ctx == NULL) {
165169
fprintf(stderr, "Could not create context\n");
166170
display_openssl_errors(__LINE__);
@@ -186,6 +190,7 @@ int main(int argc, char **argv)
186190
}
187191

188192
EVP_PKEY_CTX_free(pkey_ctx);
193+
EVP_PKEY_free(public_key);
189194

190195
printf("Data encrypted\n");
191196

@@ -230,6 +235,7 @@ int main(int argc, char **argv)
230235
}
231236

232237
EVP_PKEY_CTX_free(pkey_ctx);
238+
EVP_PKEY_free(private_key);
233239

234240
/* Compare output */
235241

@@ -242,6 +248,7 @@ int main(int argc, char **argv)
242248
exit(1);
243249
}
244250

251+
/* Free the functional reference from ENGINE_init */
245252
ENGINE_finish(e);
246253
CONF_modules_unload(1);
247254
return 0;

tests/rsa-pss-sign.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ int main(int argc, char **argv)
132132
display_openssl_errors(__LINE__);
133133
exit(1);
134134
}
135+
/*
136+
* ENGINE_init() returned a functional reference, so free the structural
137+
* reference from ENGINE_by_id().
138+
*/
139+
ENGINE_free(e);
135140

136141
if (key_pass && !ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0)) {
137142
display_openssl_errors(__LINE__);
@@ -211,6 +216,7 @@ int main(int argc, char **argv)
211216
}
212217

213218
EVP_PKEY_CTX_free(pkey_ctx);
219+
EVP_PKEY_free(private_key);
214220

215221
printf("Signature created\n");
216222

@@ -249,6 +255,7 @@ int main(int argc, char **argv)
249255
}
250256

251257
EVP_PKEY_CTX_free(pkey_ctx);
258+
EVP_PKEY_free(public_key);
252259

253260
if (ret == 1) {
254261
printf("Signature verified\n");
@@ -265,6 +272,7 @@ int main(int argc, char **argv)
265272

266273
#endif /* OPENSSL_VERSION_NUMBER >= 0x1000000fL */
267274

275+
/* Free the functional reference from ENGINE_init */
268276
ENGINE_finish(e);
269277
CONF_modules_unload(1);
270278
return 0;

tests/store-cert.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ store_certificate(char* address, X509* cert)
143143
printf("Could not store certificate\n");
144144
return -1;
145145
}
146+
PKCS11_release_all_slots(global_pkcs11_ctx, global_pkcs11_slots,
147+
global_pkcs11_slot_num);
146148

147149
return 0;
148150
}
@@ -221,6 +223,11 @@ main(int argc, char* argv[])
221223
ret = 1;
222224
goto end;
223225
}
226+
/*
227+
* ENGINE_init() returned a functional reference, so free the structural
228+
* reference from ENGINE_by_id().
229+
*/
230+
ENGINE_free(engine);
224231

225232
if (!strncmp(certfile, "pkcs11:", 7)) {
226233
params.cert_id = certfile;
@@ -267,7 +274,9 @@ main(int argc, char* argv[])
267274
printf("Certificate stored\n");
268275
ret = 0;
269276
}
277+
PKCS11_CTX_free(global_pkcs11_ctx);
270278

279+
/* Free the functional reference from ENGINE_init */
271280
ENGINE_finish(engine);
272281

273282
CONF_modules_unload(1);

0 commit comments

Comments
 (0)