Print this page
9644 Double-free in crypto tests on failure
Reviewed by: Dan McDonald <danmcd@joyent.com>
Reviewed by: Mike Zeller <mike.zeller@joyent.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/test/crypto-tests/tests/common/cryptotest_pkcs.c
+++ new/usr/src/test/crypto-tests/tests/common/cryptotest_pkcs.c
1 1 /*
2 2 * This file and its contents are supplied under the terms of the
3 3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 4 * You may only use this file in accordance with the terms of version
5 5 * 1.0 of the CDDL.
6 6 *
7 7 * A full copy of the text of the CDDL should have accompanied this
8 8 * source. A copy of the CDDL is also available via the Internet at
9 9 * http://www.illumos.org/license/CDDL.
10 10 */
11 11
12 12 /*
13 13 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
14 14 */
15 15
16 16 #include <stdio.h>
17 17 #include <cryptoutil.h>
18 18
19 19 #include "cryptotest.h"
20 20
21 21 struct crypto_op {
22 22 CK_BYTE_PTR in;
23 23 CK_BYTE_PTR out;
24 24 CK_BYTE_PTR key;
25 25 CK_BYTE_PTR param;
26 26
27 27 size_t inlen;
28 28 size_t outlen;
29 29 size_t keylen;
30 30 size_t paramlen;
31 31 size_t updatelen;
32 32
33 33 char *mechname;
34 34
35 35 /* internal */
36 36 CK_MECHANISM_TYPE mech;
37 37 CK_OBJECT_HANDLE keyt;
38 38 CK_SESSION_HANDLE hsession;
39 39 size_t fg;
40 40 };
41 41
42 42 static void
43 43 cryptotest_error(char *name, CK_RV rv)
44 44 {
45 45 (void) fprintf(stderr, "%s: Error = 0x%.8lX '%s'\n",
46 46 name, rv, pkcs11_strerror(rv));
47 47 }
48 48
49 49 crypto_op_t *
50 50 cryptotest_init(cryptotest_t *arg, size_t fg)
51 51 {
52 52 crypto_op_t *op = malloc(sizeof (*op));
53 53
54 54 op->in = (CK_BYTE_PTR)arg->in;
55 55 op->out = (CK_BYTE_PTR)arg->out;
56 56 op->key = (CK_BYTE_PTR)arg->key;
57 57 op->param = (CK_BYTE_PTR)arg->param;
58 58
59 59 op->inlen = arg->inlen;
60 60 op->outlen = arg->outlen;
61 61 op->keylen = arg->keylen;
62 62 op->paramlen = arg->plen;
63 63 op->updatelen = arg->updatelen;
64 64
65 65 op->mechname = arg->mechname;
66 66
67 67 op->hsession = CRYPTO_INVALID_SESSION;
68 68 op->fg = fg;
69 69
70 70 if (op->out == NULL)
71 71 op->outlen = op->inlen;
72 72 return (op);
73 73 }
74 74
75 75 int
76 76 cryptotest_close_session(CK_SESSION_HANDLE hsession)
77 77 {
78 78 CK_RV rv;
79 79 rv = C_CloseSession(hsession);
80 80 if (rv != CKR_OK)
81 81 cryptotest_error("cryptotest_close_session", rv);
82 82
83 83 return (rv);
84 84 }
85 85
86 86 int
87 87 cryptotest_close(crypto_op_t *op)
88 88 {
89 89 (void) C_DestroyObject(op->hsession, op->keyt);
90 90 if (op->hsession != CRYPTO_INVALID_SESSION)
91 91 (void) cryptotest_close_session(op->hsession);
92 92 free(op);
93 93 return (C_Finalize(NULL));
94 94 }
↓ open down ↓ |
94 lines elided |
↑ open up ↑ |
95 95
96 96 int
97 97 get_mech_info(crypto_op_t *op)
98 98 {
99 99 CK_RV rv;
100 100 rv = pkcs11_str2mech(op->mechname, &op->mech);
101 101 if (rv != CKR_OK) {
102 102 cryptotest_error("get_mech_info", rv);
103 103 (void) fprintf(stderr, "failed to resolve mechanism name %s\n",
104 104 op->mechname);
105 - (void) cryptotest_close(op);
106 105 return (CTEST_NAME_RESOLVE_FAILED);
107 106 }
108 107 return (rv);
109 108 }
110 109
111 110
112 111 int
113 112 get_hsession_by_mech(crypto_op_t *op)
114 113 {
115 114 CK_RV rv;
116 115 rv = SUNW_C_GetMechSession(op->mech, &op->hsession);
117 116 if (rv != CKR_OK) {
118 117 cryptotest_error("get_hsession_by_mech", rv);
119 118 (void) fprintf(stderr,
120 119 "could not find provider for mechanism %lu\n",
121 120 op->mech);
122 - (void) cryptotest_close(op);
123 121 return (CTEST_MECH_NO_PROVIDER);
124 122 }
125 123 return (rv);
126 124 }
127 125
128 126 /*
129 127 * SIGN_* functions
130 128 */
131 129 int
132 130 sign_init(crypto_op_t *op)
133 131 {
134 132 CK_MECHANISM mech;
135 133 CK_RV rv;
136 134
137 135 mech.mechanism = op->mech;
138 136 mech.pParameter = NULL;
139 137 mech.ulParameterLen = 0;
140 138
141 139 rv = SUNW_C_KeyToObject(op->hsession, op->mech,
142 140 op->key, op->keylen, &op->keyt);
143 141
144 142 if (rv != CKR_OK)
145 143 cryptotest_error("SUNW_C_KeyToObject", rv);
146 144
147 145 rv = C_SignInit(op->hsession, &mech, op->keyt);
148 146
149 147 if (rv != CKR_OK)
150 148 cryptotest_error("C_SignInit", rv);
151 149
152 150 return (rv);
153 151 }
154 152
155 153 int
156 154 sign_single(crypto_op_t *op)
157 155 {
158 156 CK_RV rv;
159 157
160 158 rv = C_Sign(op->hsession, op->in, op->inlen,
161 159 op->out, (CK_ULONG_PTR)&op->outlen);
162 160 if (rv != CKR_OK)
163 161 cryptotest_error("C_Sign", rv);
164 162 return (rv);
165 163 }
166 164
167 165 int
168 166 sign_update(crypto_op_t *op, int offset)
169 167 {
170 168 CK_RV rv;
171 169 rv = C_SignUpdate(op->hsession, op->in + offset, op->updatelen);
172 170 if (rv != CKR_OK)
173 171 cryptotest_error("C_SignUpdate", rv);
174 172
175 173 return (rv);
176 174 }
177 175
178 176 int
179 177 sign_final(crypto_op_t *op)
180 178 {
181 179 CK_RV rv;
182 180 rv = C_SignFinal(op->hsession, op->out, (CK_ULONG_PTR)&op->outlen);
183 181 if (rv != CKR_OK)
184 182 cryptotest_error("C_SignFinal", rv);
185 183 return (rv);
186 184 }
187 185
188 186 /*
189 187 * MAC_* functions
190 188 */
191 189 int
192 190 mac_init(crypto_op_t *op)
193 191 {
194 192 return (sign_init(op));
195 193 }
196 194
197 195 int
198 196 mac_single(crypto_op_t *op)
199 197 {
200 198 return (sign_single(op));
201 199 }
202 200
203 201 int
204 202 mac_update(crypto_op_t *op, int offset)
205 203 {
206 204 return (sign_update(op, offset));
207 205 }
208 206
209 207 int
210 208 mac_final(crypto_op_t *op)
211 209 {
212 210 return (sign_final(op));
213 211 }
214 212
215 213 /*
216 214 * VERIFY_* functions
217 215 */
218 216 int
219 217 verify_init(crypto_op_t *op)
220 218 {
221 219 CK_MECHANISM mech;
222 220 CK_RV rv;
223 221
224 222 mech.mechanism = op->mech;
225 223 mech.pParameter = NULL;
226 224 mech.ulParameterLen = 0;
227 225
228 226 rv = SUNW_C_KeyToObject(op->hsession, op->mech,
229 227 op->key, op->keylen, &op->keyt);
230 228
231 229 if (rv != CKR_OK)
232 230 cryptotest_error("SUNW_C_KeyToObject", rv);
233 231
234 232 rv = C_VerifyInit(op->hsession, &mech, op->keyt);
235 233
236 234 if (rv != CKR_OK)
237 235 cryptotest_error("C_VerifyInit", rv);
238 236
239 237 return (rv);
240 238 }
241 239
242 240 int
243 241 verify_single(crypto_op_t *op)
244 242 {
245 243 CK_RV rv;
246 244
247 245 rv = C_Verify(op->hsession, op->in, op->inlen, op->out, op->outlen);
248 246 if (rv != CKR_OK && rv != CKR_SIGNATURE_INVALID &&
249 247 rv != CKR_SIGNATURE_LEN_RANGE)
250 248 cryptotest_error("C_Verify", rv);
251 249 return (rv);
252 250 }
253 251
254 252 int
255 253 verify_update(crypto_op_t *op, int offset)
256 254 {
257 255 CK_RV rv;
258 256 rv = C_VerifyUpdate(op->hsession, op->in + offset, op->updatelen);
259 257 if (rv != CKR_OK)
260 258 cryptotest_error("C_VerifyUpdate", rv);
261 259 return (rv);
262 260 }
263 261
264 262 int
265 263 verify_final(crypto_op_t *op)
266 264 {
267 265 CK_RV rv;
268 266 rv = C_VerifyFinal(op->hsession, op->out, op->outlen);
269 267 if (rv != CKR_OK && rv != CKR_SIGNATURE_INVALID &&
270 268 rv != CKR_SIGNATURE_LEN_RANGE)
271 269 cryptotest_error("C_VerifyFinal", rv);
272 270 return (rv);
273 271 }
274 272
275 273 /*
276 274 * ENCRYPT_* functions
277 275 */
278 276 int
279 277 encrypt_init(crypto_op_t *op)
280 278 {
281 279 CK_MECHANISM mech;
282 280 CK_RV rv;
283 281
284 282 mech.mechanism = op->mech;
285 283 mech.pParameter = op->param;
286 284 mech.ulParameterLen = op->paramlen;
287 285
288 286 rv = SUNW_C_KeyToObject(op->hsession, op->mech,
289 287 op->key, op->keylen, &op->keyt);
290 288
291 289 if (rv != CKR_OK)
292 290 cryptotest_error("SUNW_C_KeyToObject", rv);
293 291
294 292 rv = C_EncryptInit(op->hsession, &mech, op->keyt);
295 293
296 294 if (rv != CKR_OK)
297 295 cryptotest_error("C_EncryptInit", rv);
298 296
299 297 return (rv);
300 298 }
301 299
302 300 int
303 301 encrypt_single(crypto_op_t *op)
304 302 {
305 303 CK_RV rv;
306 304
307 305 rv = C_Encrypt(op->hsession, op->in, op->inlen,
308 306 op->out, (CK_ULONG_PTR)&op->outlen);
309 307 if (rv != CKR_OK)
310 308 cryptotest_error("C_Encrypt", rv);
311 309 return (rv);
312 310 }
313 311
314 312 int
315 313 encrypt_update(crypto_op_t *op, int offset, size_t *encrlen)
316 314 {
317 315 CK_RV rv;
318 316 CK_ULONG outlen = op->outlen - *encrlen;
319 317 rv = C_EncryptUpdate(op->hsession, op->in + offset, op->updatelen,
320 318 op->out + *encrlen, &outlen);
321 319 if (rv != CKR_OK)
322 320 cryptotest_error("C_EncryptUpdate", rv);
323 321
324 322 *encrlen += outlen;
325 323 return (rv);
326 324 }
327 325
328 326 int
329 327 encrypt_final(crypto_op_t *op, size_t encrlen)
330 328 {
331 329 CK_RV rv;
332 330 CK_ULONG outlen = op->outlen - encrlen;
333 331 rv = C_EncryptFinal(op->hsession, op->out + encrlen, &outlen);
334 332 if (rv != CKR_OK)
335 333 cryptotest_error("C_EncryptFinal", rv);
336 334 return (rv);
337 335 }
338 336
339 337 /*
340 338 * DECRYPT_* functions
341 339 */
342 340 int
343 341 decrypt_init(crypto_op_t *op)
344 342 {
345 343 CK_MECHANISM mech;
346 344 CK_RV rv;
347 345
348 346 mech.mechanism = op->mech;
349 347 mech.pParameter = op->param;
350 348 mech.ulParameterLen = op->paramlen;
351 349
352 350 rv = SUNW_C_KeyToObject(op->hsession, op->mech,
353 351 op->key, op->keylen, &op->keyt);
354 352
355 353 if (rv != CKR_OK)
356 354 cryptotest_error("SUNW_C_KeyToObject", rv);
357 355
358 356 rv = C_DecryptInit(op->hsession, &mech, op->keyt);
359 357
360 358 if (rv != CKR_OK)
361 359 cryptotest_error("C_DecryptInit", rv);
362 360
363 361 return (rv);
364 362 }
365 363
366 364 int
367 365 decrypt_single(crypto_op_t *op)
368 366 {
369 367 CK_RV rv;
370 368
371 369 rv = C_Decrypt(op->hsession, op->in, op->inlen,
372 370 op->out, (CK_ULONG_PTR)&op->outlen);
373 371 if (rv != CKR_OK)
374 372 cryptotest_error("C_Decrypt", rv);
375 373 return (rv);
376 374 }
377 375
378 376 int
379 377 decrypt_update(crypto_op_t *op, int offset, size_t *encrlen)
380 378 {
381 379 CK_RV rv;
382 380 CK_ULONG outlen = op->outlen - *encrlen;
383 381 rv = C_DecryptUpdate(op->hsession, op->in + offset, op->updatelen,
384 382 op->out + *encrlen, &outlen);
385 383 if (rv != CKR_OK)
386 384 cryptotest_error("C_DecryptUpdate", rv);
387 385
388 386 *encrlen += outlen;
389 387 return (rv);
390 388 }
391 389
392 390 int
393 391 decrypt_final(crypto_op_t *op, size_t encrlen)
394 392 {
395 393 CK_RV rv;
396 394 CK_ULONG outlen = op->outlen - encrlen;
397 395 rv = C_DecryptFinal(op->hsession, op->out + encrlen, &outlen);
398 396 if (rv != CKR_OK)
399 397 cryptotest_error("C_DecryptFinal", rv);
400 398 return (rv);
401 399 }
↓ open down ↓ |
269 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX