Debian Codegraph @

testcases/unit/policytest.c

raw
1 /*
2 * COPYRIGHT (c) International Business Machines Corp. 2021
3 *
4 * This program is provided under the terms of the Common Public License,
5 * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
6 * software constitutes recipient's acceptance of CPL-1.0 terms which can be
7 * found in the file LICENSE file or at
8 * https://opensource.org/licenses/cpl1.0.php
9 */
10 #include "policy.h"
11 #include "ec_defs.h"
12 #include "unittest.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <openssl/obj_mac.h>
16
17 #define UNUSED(var) ((void)(var))
18
19 /* Inlined strength definitions */
20 static const char niststrength[] =
21 "version strength-0\n"
22 "strength 112 {\n"
23 " MOD_EXP = 2048\n"
24 " ECC = 224\n"
25 " SYMMETRIC = 112\n"
26 " digest = 224\n"
27 " signature = 112\n"
28 "}\n"
29 "strength 128 {\n"
30 " MOD_EXP = 3072\n"
31 " ECC = 256\n"
32 " SYMMETRIC = 128\n"
33 " digest = 256\n"
34 " signature = 128\n"
35 "}\n"
36 "strength 192 {\n"
37 " MOD_EXP = 7680\n"
38 " ECC = 384\n"
39 " SYMMETRIC = 192\n"
40 " digest = 384\n"
41 " signature = 192\n"
42 "}\n"
43 "strength 256 {\n"
44 " MOD_EXP = 15360\n"
45 " ECC = 512\n"
46 " SYMMETRIC = 256\n"
47 " digest = 512\n"
48 " signature = 256\n"
49 "}\n";
50
51 static const char quantum112[] =
52 "version strength-0\n"
53 "strength 112 {\n"
54 " SYMMETRIC = 224\n"
55 " digest = 448\n"
56 " signature = 224\n"
57 "}\n";
58
59 static const char emptystrength[] =
60 "version strength-0\n";
61
62 /* Inlined policies */
63 static const char policysecponly[] =
64 "version policy-0\n"
65 "strength = 0\n"
66 "allowedcurves ( SECP224R1, SECP384R1, SECP521R1, SECP256K1 )\n";
67
68 static const char policynocurves[] =
69 "version policy-0\n"
70 "strength = 0\n"
71 "allowedcurves ( )\n";
72
73 static const char policystrength256[] =
74 "version policy-0\n"
75 "strength = 256\n";
76
77 static const char policystrength192[] =
78 "version policy-0\n"
79 "strength = 192\n";
80
81 static const char policystrength128[] =
82 "version policy-0\n"
83 "strength = 128\n";
84
85 static const char policystrength112[] =
86 "version policy-0\n"
87 "strength = 112\n";
88
89 static const char policyempty[] =
90 "version policy-0\n"
91 "strength = 0\n";
92
93 static const char policynomechs[] =
94 "version policy-0\n"
95 "strength = 0\n"
96 "allowedmechs ()\n";
97
98 static const char policyfixedmechs[] =
99 "version policy-0\n"
100 "strength = 0\n"
101 "allowedmechs ( CKM_RSA_PKCS, CKM_ECDSA )\n";
102
103 static const char policymgfstandard[] =
104 "version policy-0\n"
105 "strength = 0\n"
106 "allowedmgfs ( CKG_MGF1_SHA1, CKG_MGF1_SHA224, CKG_MGF1_SHA256,\n"
107 "CKG_MGF1_SHA384, CKG_MGF1_SHA512 )\n";
108
109 static const char policymgfibm[] =
110 "version policy-0\n"
111 "strength = 0\n"
112 "allowedmgfs ( CKG_IBM_MGF1_SHA3_224, CKG_IBM_MGF1_SHA3_256,\n"
113 "CKG_IBM_MGF1_SHA3_384, CKG_IBM_MGF1_SHA3_512 )\n";
114
115 static const char policymgfoneeach[] =
116 "version policy-0\n"
117 "strength = 0\n"
118 "allowedmgfs ( CKG_MGF1_SHA512, CKG_IBM_MGF1_SHA3_224 )\n";
119
120 static const char policymgfnone[] =
121 "version policy-0\n"
122 "strength = 0\n"
123 "allowedmgfs ()\n";
124
125 static const char policykdfnosha1[] =
126 "version policy-0\n"
127 "strength = 0\n"
128 "allowedkdfs ( CKD_SHA224_KDF, CKD_SHA256_KDF, CKD_SHA384_KDF,\n"
129 "CKD_SHA512_KDF )\n";
130
131 static const char policykdfnone[] =
132 "version policy-0\n"
133 "strength = 0\n"
134 "allowedkdfs ()\n";
135
136 /* Loading functions (in usr/lib/api/policy.c) */
137 struct policy_private;
138 extern void policy_init_policy(struct policy *p);
139 extern CK_RV policy_load_strength_cfg(struct policy_private *pp,
140 FILE *fp);
141 extern CK_RV policy_load_policy_cfg(struct policy_private *pp,
142 FILE *fp, CK_BBOOL *restricting);
143 extern struct policy_private *policy_private_alloc(void);
144 extern struct policy_private *policy_private_free(struct policy_private *pp);
145 extern void policy_private_deactivate(struct policy_private *pp);
146
147 struct keytest {
148 CK_ULONG keytype;
149 union {
150 CK_ULONG value;
151 int nid;
152 } strengthattr;
153 CK_ULONG sigattr;
154 CK_ULONG expstrength;/* Index: 0=256; 1=192; 2=128; 3=112; 4=0 */
155 CK_ULONG expsiglen;
156 CK_BBOOL expallowed;
157 CK_RV exprc;
158 };
159
160 #define STRENGTH_256 0
161 #define STRENGTH_192 1
162 #define STRENGTH_128 2
163 #define STRENGTH_112 3
164 #define STRENGTH_0 4
165
166 #define NUM_KEYTESTS_STRENGTHDET 4
167
168 static const struct strengthdettest {
169 const char *definition;
170 size_t definitionsize;
171 struct keytest tests[NUM_KEYTESTS_STRENGTHDET];
172 } strengthdettests[] =
173 {
174 /* 0: NIST strength definition */
175 {
176 niststrength,
177 sizeof(niststrength),
178 {
179 { CKK_RSA, { .value = 7680 }, 0, STRENGTH_192, 7680, CK_TRUE, CKR_OK },
180 { CKK_DSA, { .value = 1024 }, 512, STRENGTH_0, 512 * 2, CK_TRUE, CKR_OK },
181 { CKK_EC, { .nid = NID_secp224r1 }, 0, STRENGTH_112, 224 * 2, CK_TRUE, CKR_OK },
182 { CKK_AES, { .value = 156 / 8 }, 0, STRENGTH_128, 128, CK_TRUE, CKR_OK },
183 }
184 },
185 /* 1: PQ strength definition */
186 {
187 quantum112,
188 sizeof(quantum112),
189 {
190 { CKK_RSA, { .value = 7680 }, 0, STRENGTH_0, 7680, CK_TRUE, CKR_OK },
191 { CKK_DSA, { .value = 1024 }, 512, STRENGTH_0, 512 * 2, CK_TRUE, CKR_OK },
192 /* allowed is CK_TRUE since no policy is active */
193 { CKK_EC, { .nid = NID_secp224r1 }, 0, STRENGTH_0, 224 * 2, CK_TRUE, CKR_OK },
194 { CKK_AES, { .value = 256 / 8 }, 0, STRENGTH_112, 128, CK_TRUE, CKR_OK },
195 }
196 },
197 /* 2: empty */
198 {
199 emptystrength,
200 sizeof(emptystrength),
201 {
202 { CKK_RSA, { .value = 7680 }, 0, STRENGTH_0, 7680, CK_TRUE, CKR_OK },
203 { CKK_DSA, { .value = 1024 }, 512, STRENGTH_0, 512 * 2, CK_TRUE, CKR_OK },
204 { CKK_EC, { .nid = NID_secp224r1 }, 0, STRENGTH_0, 224 * 2, CK_TRUE, CKR_OK },
205 { CKK_AES, { .value = 156 / 8 }, 0, STRENGTH_0, 128, CK_TRUE, CKR_OK },
206 }
207 }
208 };
209
210 #define NUM_KEYTESTS_STRENGTHENFORCE 4
211
212 static const struct strengthenforcetest {
213 const char *strength;
214 size_t strengthsize;
215 const char *policy;
216 size_t policysize;
217 struct keytest tests[NUM_KEYTESTS_STRENGTHENFORCE];
218 } strengthenforcetests[] =
219 {
220 /* 0: NIST strength, require 0 and only secp curves */
221 {
222 niststrength,
223 sizeof(niststrength),
224 policysecponly,
225 sizeof(policysecponly),
226 {
227 /* MOD_EXP */
228 { CKK_RSA, { .value = 3072 }, 0, STRENGTH_128, 3072, CK_TRUE, CKR_OK },
229 /* ECC (Test 0) */
230 { CKK_EC, { .nid = NID_secp224r1 }, 0, STRENGTH_112, 448, CK_TRUE, CKR_OK },
231 /* ECC (Test 1) */
232 { CKK_EC, { .nid = NID_brainpoolP512t1 }, 0, STRENGTH_256, 1024, CK_FALSE, CKR_FUNCTION_FAILED },
233 /* SYMMETRIC */
234 { CKK_AES, { .value = 200 / 8 }, 0, STRENGTH_192, 128, CK_TRUE, CKR_OK }
235 }
236 },
237 /* 1: NIST strength, require 0 and no curves */
238 {
239 niststrength,
240 sizeof(niststrength),
241 policynocurves,
242 sizeof(policynocurves),
243 {
244 /* MOD_EXP */
245 { CKK_RSA, { .value = 3072 }, 0, STRENGTH_128, 3072, CK_TRUE, CKR_OK },
246 /* ECC (Test 0) */
247 { CKK_EC, { .nid = NID_secp224r1 }, 0, STRENGTH_112, 448, CK_FALSE, CKR_FUNCTION_FAILED },
248 /* ECC (Test 1) */
249 { CKK_EC, { .nid = NID_brainpoolP512t1 }, 0, STRENGTH_256, 1024, CK_FALSE, CKR_FUNCTION_FAILED },
250 /* SYMMETRIC */
251 { CKK_AES, { .value = 200 / 8 }, 0, STRENGTH_192, 128, CK_TRUE, CKR_OK }
252 }
253 },
254 /* 2: NIST strength, require 256 */
255 {
256 niststrength,
257 sizeof(niststrength),
258 policystrength256,
259 sizeof(policystrength256),
260 {
261 /* MOD_EXP */
262 { CKK_RSA, { .value = 3072 }, 0, STRENGTH_128, 3072, CK_TRUE, CKR_FUNCTION_FAILED },
263 /* ECC (Test 0) */
264 { CKK_EC, { .nid = NID_secp224r1 }, 0, STRENGTH_112, 448, CK_TRUE, CKR_FUNCTION_FAILED },
265 /* ECC (Test 1) */
266 { CKK_EC, { .nid = NID_brainpoolP512t1 }, 0, STRENGTH_256, 1024, CK_TRUE, CKR_OK },
267 /* SYMMETRIC */
268 { CKK_AES, { .value = 200 / 8 }, 0, STRENGTH_192, 128, CK_TRUE, CKR_FUNCTION_FAILED }
269 }
270 },
271 /* 3: NIST strength, require 192 */
272 {
273 niststrength,
274 sizeof(niststrength),
275 policystrength192,
276 sizeof(policystrength192),
277 {
278 /* MOD_EXP */
279 { CKK_RSA, { .value = 3072 }, 0, STRENGTH_128, 3072, CK_TRUE, CKR_FUNCTION_FAILED },
280 /* ECC (Test 0) */
281 { CKK_EC, { .nid = NID_secp224r1 }, 0, STRENGTH_112, 448, CK_TRUE, CKR_FUNCTION_FAILED },
282 /* ECC (Test 1) */
283 { CKK_EC, { .nid = NID_brainpoolP512t1 }, 0, STRENGTH_256, 1024, CK_TRUE, CKR_OK },
284 /* SYMMETRIC */
285 { CKK_AES, { .value = 200 / 8 }, 0, STRENGTH_192, 128, CK_TRUE, CKR_OK }
286 }
287 },
288 /* 4: NIST strength, require 128 */
289 {
290 niststrength,
291 sizeof(niststrength),
292 policystrength128,
293 sizeof(policystrength128),
294 {
295 /* MOD_EXP */
296 { CKK_RSA, { .value = 3072 }, 0, STRENGTH_128, 3072, CK_TRUE, CKR_OK },
297 /* ECC (Test 0) */
298 { CKK_EC, { .nid = NID_secp224r1 }, 0, STRENGTH_112, 448, CK_TRUE, CKR_FUNCTION_FAILED },
299 /* ECC (Test 1) */
300 { CKK_EC, { .nid = NID_brainpoolP512t1 }, 0, STRENGTH_256, 1024, CK_TRUE, CKR_OK },
301 /* SYMMETRIC */
302 { CKK_AES, { .value = 200 / 8 }, 0, STRENGTH_192, 128, CK_TRUE, CKR_OK }
303 }
304 },
305 /* 5: NIST strength, require 112 */
306 {
307 niststrength,
308 sizeof(niststrength),
309 policystrength112,
310 sizeof(policystrength112),
311 {
312 /* MOD_EXP */
313 { CKK_RSA, { .value = 3072 }, 0, STRENGTH_128, 3072, CK_TRUE, CKR_OK },
314 /* ECC (Test 0) */
315 { CKK_EC, { .nid = NID_secp224r1 }, 0, STRENGTH_112, 448, CK_TRUE, CKR_OK },
316 /* ECC (Test 1) */
317 { CKK_EC, { .nid = NID_brainpoolP512t1 }, 0, STRENGTH_256, 1024, CK_TRUE, CKR_OK },
318 /* SYMMETRIC */
319 { CKK_AES, { .value = 200 / 8 }, 0, STRENGTH_192, 128, CK_TRUE, CKR_OK }
320 }
321 },
322 /* 6: NIST strength, require 0 */
323 {
324 niststrength,
325 sizeof(niststrength),
326 policyempty,
327 sizeof(policyempty),
328 {
329 /* MOD_EXP */
330 { CKK_RSA, { .value = 3072 }, 0, STRENGTH_128, 3072, CK_TRUE, CKR_OK },
331 /* ECC (Test 0) */
332 { CKK_EC, { .nid = NID_secp224r1 }, 0, STRENGTH_112, 448, CK_TRUE, CKR_OK },
333 /* ECC (Test 1) */
334 { CKK_EC, { .nid = NID_brainpoolP512t1 }, 0, STRENGTH_256, 1024, CK_TRUE, CKR_OK },
335 /* SYMMETRIC */
336 { CKK_AES, { .value = 200 / 8 }, 0, STRENGTH_192, 128, CK_TRUE, CKR_OK }
337 }
338 }
339 };
340
341 /* These tests run with niststrength and policystrength128 */
342 static const struct policyenforcetest {
343 CK_ULONG strength;
344 CK_ULONG siglen;
345 CK_ULONG mech;
346 int check;
347 CK_RV exprc;
348 } policyenforcetests[] =
349 {
350 { STRENGTH_0, 0, CKM_SHA_1, POLICY_CHECK_DIGEST, CKR_FUNCTION_FAILED },
351 { STRENGTH_0, 0, CKM_SHA256, POLICY_CHECK_DIGEST, CKR_OK },
352 { STRENGTH_112, 0, CKM_RSA_PKCS, POLICY_CHECK_ENCRYPT, CKR_FUNCTION_FAILED },
353 { STRENGTH_128, 0, CKM_RSA_PKCS, POLICY_CHECK_ENCRYPT, CKR_OK },
354 { STRENGTH_128, 112, CKM_RSA_PKCS, POLICY_CHECK_SIGNATURE, CKR_FUNCTION_FAILED },
355 { STRENGTH_128, 128, CKM_RSA_PKCS, POLICY_CHECK_SIGNATURE, CKR_OK },
356 };
357
358 static const CK_ULONG testhashmechs[] =
359 { CKM_RSA_PKCS, CKM_SHA1_RSA_PKCS, CKM_ECDSA, CKM_ECDSA_SHA1 };
360
361 static const CK_RV testhashempty[] = { CKR_OK, CKR_OK, CKR_OK, CKR_OK };
362 static const CK_RV testhashnomechs[] =
363 { CKR_FUNCTION_FAILED, CKR_FUNCTION_FAILED, CKR_FUNCTION_FAILED,
364 CKR_FUNCTION_FAILED };
365 static const CK_RV testhashfixed[] =
366 { CKR_OK, CKR_FUNCTION_FAILED, CKR_OK, CKR_FUNCTION_FAILED };
367
368 static const struct policyhashtest {
369 const char *policy;
370 size_t policysize;
371 const CK_ULONG *mechs;
372 const CK_RV *exprcs;
373 size_t nummechs;
374 } policyhashtests[] =
375 {
376 {
377 policyempty,
378 sizeof(policyempty),
379 testhashmechs,
380 testhashempty,
381 ARRAYSIZE(testhashmechs)
382 },
383 {
384 policynomechs,
385 sizeof(policynomechs),
386 testhashmechs,
387 testhashnomechs,
388 ARRAYSIZE(testhashmechs)
389 },
390 {
391 policyfixedmechs,
392 sizeof(policyfixedmechs),
393 testhashmechs,
394 testhashfixed,
395 ARRAYSIZE(testhashmechs)
396 }
397 };
398
399 /* Helper functions for strength determination */
400 static CK_RV build_ulong_attr(CK_ATTRIBUTE_TYPE type, CK_ULONG value,
401 CK_ATTRIBUTE **attr)
402 {
403 CK_ATTRIBUTE *a;
404
405 a = malloc(sizeof(CK_ATTRIBUTE) + sizeof(CK_ULONG));
406 if (a == NULL)
407 return CKR_HOST_MEMORY;
408 a->type = type;
409 a->pValue = ((char *)a) + sizeof(CK_ATTRIBUTE);
410 a->ulValueLen = sizeof(CK_ULONG);
411 *(CK_ULONG *)a->pValue = value;
412 *attr = a;
413 return CKR_OK;
414 }
415
416 static CK_RV build_bigint_attr(CK_ATTRIBUTE_TYPE type, CK_ULONG numbits,
417 CK_ATTRIBUTE **attr)
418 {
419 CK_ATTRIBUTE *a;
420 CK_ULONG numbytes = numbits / 8u;
421
422 a = malloc(sizeof(CK_ATTRIBUTE) + numbytes);
423 if (a == NULL)
424 return CKR_HOST_MEMORY;
425 a->type = type;
426 a->pValue = ((char *)a) + sizeof(CK_ATTRIBUTE);
427 a->ulValueLen = numbytes;
428 *attr = a;
429 return CKR_OK;
430 }
431
432 static CK_RV build_oid_attr(CK_ATTRIBUTE_TYPE type, const CK_BYTE *data,
433 CK_ULONG datasize, CK_ATTRIBUTE **attr)
434 {
435 CK_ATTRIBUTE *a;
436
437 a = malloc(sizeof(CK_ATTRIBUTE));
438 if (a == NULL)
439 return CKR_HOST_MEMORY;
440 a->type = type;
441 a->pValue = (void *)data;
442 a->ulValueLen = datasize;
443 *attr = a;
444 return CKR_OK;
445 }
446
447 static int attrcalls;
448
449 static CK_RV getdetattr(void *d, CK_ATTRIBUTE_TYPE type, CK_ATTRIBUTE **attr)
450 {
451 struct keytest *t = d;
452 int i;
453
454 ++attrcalls;
455 switch (type) {
456 case CKA_KEY_TYPE:
457 return build_ulong_attr(type, t->keytype, attr);
458 case CKA_VALUE_LEN:
459 return build_ulong_attr(type, t->strengthattr.value, attr);
460 case CKA_MODULUS:
461 case CKA_PRIME:
462 return build_bigint_attr(type, t->strengthattr.value, attr);
463 case CKA_SUBPRIME:
464 return build_bigint_attr(type, t->sigattr, attr);
465 case CKA_EC_PARAMS:
466 for (i = 0; i < NUMEC; ++i) {
467 if (der_ec_supported[i].nid == t->strengthattr.nid)
468 return build_oid_attr(type, der_ec_supported[i].data,
469 der_ec_supported[i].data_size, attr);
470
471 }
472 // Fallthrough
473 default:
474 return CKR_FUNCTION_FAILED;
475 }
476 }
477
478 static void freedetattr(void *d, CK_ATTRIBUTE *a)
479 {
480 UNUSED(d);
481
482 --attrcalls;
483 free(a);
484 }
485
486 static int test_load_strength_cfg(struct policy_private *pp,
487 void *cfg, size_t size)
488 {
489 FILE *fp;
490 CK_RV rc;
491
492 fp = fmemopen(cfg, size, "r");
493 if (fp == NULL) {
494 fprintf(stderr, "Failed to memopen strength configuration\n");
495 return -1;
496 }
497 rc = policy_load_strength_cfg(pp, fp);
498 fclose(fp);
499 if (rc != CKR_OK) {
500 fprintf(stderr, "Strength configuration could not be loaded\n");
501 return -1;
502 }
503 return 0;
504 }
505
506 static int test_load_policy_cfg(struct policy_private *pp,
507 void *cfg, size_t size)
508 {
509 FILE *fp;
510 CK_RV rc;
511 CK_BBOOL ignored;
512
513 fp = fmemopen(cfg, size, "r");
514 if (fp == NULL) {
515 fprintf(stderr, "Failed to memopen policy\n");
516 return -1;
517 }
518 rc = policy_load_policy_cfg(pp, fp, &ignored);
519 fclose(fp);
520 if (rc != CKR_OK) {
521 fprintf(stderr, "Policy configuration could not be loaded\n");
522 return -1;
523 }
524 return 0;
525 }
526
527 static int runstrengthdettests(void)
528 {
529 struct objstrength strength;
530 struct policy_private *pp;
531 const struct keytest *t;
532 struct policy p;
533 unsigned int o;
534 CK_RV rc;
535 int res;
536 int i;
537
538 fprintf(stderr, "Running strengthdettests\n");
539 res = 0;
540 policy_init_policy(&p);
541 for (o = 0; o < ARRAYSIZE(strengthdettests); ++o) {
542 pp = policy_private_alloc();
543 if (pp == NULL) {
544 fprintf(stderr, "Failed to allocate policy_private\n");
545 return -1;
546 }
547 policy_private_deactivate(pp);
548 p.priv = pp;
549 if (test_load_strength_cfg(pp, (void *)strengthdettests[o].definition,
550 strengthdettests[o].definitionsize)) {
551 fprintf(stderr, "Test %u: Failed to load strength configuration\n", o);
552 return -1;
553 }
554 for (i = 0; i < NUM_KEYTESTS_STRENGTHDET; ++i) {
555 attrcalls = 0;
556 t = &(strengthdettests[o].tests[i]);
557 rc = p.store_object_strength(&p, &strength, getdetattr, (void *)t,
558 freedetattr, NULL);
559 if (rc != CKR_OK) {
560 res = -1;
561 fprintf(stderr, "Test %u, case %i: Could not store object strength\n",
562 o, i);
563 break;
564 }
565 if (strength.strength != t->expstrength) {
566 res = -1;
567 fprintf(stderr, "Test %u, case %i: Unexpected strength:\nexpected: %lu\nis: %lu\n",
568 o, i, t->expstrength, strength.strength);
569 break;
570 }
571 if (strength.siglen != t->expsiglen) {
572 res = -1;
573 fprintf(stderr, "Test %u, case %i: Unexpected siglen:\nexpected: %lu\nis: %lu\n",
574 o, i, t->expsiglen, strength.siglen);
575 break;
576 }
577 if (t->keytype == CKK_EC && strength.allowed != t->expallowed) {
578 res = -1;
579 fprintf(stderr, "Test %u, case %i: Unexpected EC allowed flag\n",
580 o, i);
581 break;
582 }
583 if (attrcalls != 0) {
584 res = -1;
585 fprintf(stderr, "Unbalanced attribute calls. Count shows %i!\n",
586 attrcalls);
587 break;
588 }
589 }
590 p.priv = policy_private_free(pp);
591 if (res != 0)
592 break;
593 }
594 return res;
595 }
596
597 static int runstrengthenforcetests(void)
598 {
599 struct objstrength strength;
600 struct policy_private *pp;
601 const struct keytest *t;
602 struct policy p;
603 unsigned int o;
604 CK_RV rc;
605 int res;
606 int i;
607
608 fprintf(stderr, "Running strengthenforcetests\n");
609 res = 0;
610 policy_init_policy(&p);
611 for (o = 0; o < ARRAYSIZE(strengthenforcetests); ++o) {
612 pp = policy_private_alloc();
613 if (pp == NULL) {
614 fprintf(stderr, "Test %u: Failed to allocate policy_private\n", o);
615 return -1;
616 }
617 p.priv = pp;
618 if (test_load_strength_cfg(pp,(void *)strengthenforcetests[o].strength,
619 strengthenforcetests[o].strengthsize)) {
620 policy_private_free(pp);
621 fprintf(stderr, "Test %u: Failed to load strength configuration\n",
622 o);
623 return -1;
624 }
625 if (test_load_policy_cfg(pp, (void *)strengthenforcetests[o].policy,
626 strengthenforcetests[o].policysize)) {
627 policy_private_free(pp);
628 fprintf(stderr, "Test %u: Failed to load policy configuration\n",
629 o);
630 return -1;
631 }
632 for (i = 0; i < NUM_KEYTESTS_STRENGTHENFORCE; ++i) {
633 attrcalls = 0;
634 t = &(strengthenforcetests[o].tests[i]);
635 rc = p.store_object_strength(&p, &strength, getdetattr, (void *)t,
636 freedetattr, NULL);
637 if (rc != t->exprc) {
638 res = -1;
639 fprintf(stderr, "Test %u, case %i: Wrong result on strength determination: expected %lu, got %lu\n",
640 o, i, t->exprc, rc);
641 break;
642 }
643 /* Even if we get CKR_FUNCTION_FAILED, the policy should
644 have stored the correct strength information. */
645 if (strength.strength != t->expstrength) {
646 res = -1;
647 fprintf(stderr, "Test %u, case %i: Unexpected strength:\nexpected: %lu\nis: %lu\n",
648 o, i, t->expstrength, strength.strength);
649 break;
650 }
651 if (strength.siglen != t->expsiglen) {
652 res = -1;
653 fprintf(stderr, "Test %u, case %i: Unexpected siglen:\nexpected: %lu\nis: %lu\n",
654 o, i, t->expsiglen, strength.siglen);
655 break;
656 }
657 if (t->keytype == CKK_EC && strength.allowed != t->expallowed) {
658 res = -1;
659 fprintf(stderr, "Test %u, case %i: Unexpected EC allowed flag\n",
660 o, i);
661 break;
662 }
663 if (attrcalls != 0) {
664 res = -1;
665 fprintf(stderr, "Unbalanced attribute calls. Count shows %i!\n",
666 attrcalls);
667 break;
668 }
669 }
670 p.priv = policy_private_free(pp);
671 if (res != 0)
672 break;
673 }
674 return res;
675 }
676
677 static int runpolicyenforcetests(void)
678 {
679 struct objstrength strength, *s;
680 struct policy_private *pp;
681 CK_MECHANISM mech;
682 struct policy p;
683 unsigned int i;
684 CK_RV rc;
685 int res;
686
687 fprintf(stderr, "Running policyenforcetests\n");
688 res = 0;
689 mech.pParameter = NULL;
690 mech.ulParameterLen = 0;
691 strength.allowed = CK_TRUE;
692 policy_init_policy(&p);
693 pp = policy_private_alloc();
694 if (pp == NULL) {
695 fprintf(stderr, "Failed to allocate policy_private\n");
696 return -1;
697 }
698 p.priv = pp;
699 if (test_load_strength_cfg(pp, (void *)niststrength, sizeof(niststrength))) {
700 policy_private_free(pp);
701 fprintf(stderr, "Failed to load NIST strength configuration\n");
702 return -1;
703 }
704 if (test_load_policy_cfg(pp, (void *)policystrength128,
705 sizeof(policystrength128))) {
706 policy_private_free(pp);
707 fprintf(stderr, "Failed to load policy with strength 128\n");
708 return -1;
709 }
710 for (i = 0; i < ARRAYSIZE(policyenforcetests); ++i) {
711 strength.strength = policyenforcetests[i].strength;
712 strength.siglen = policyenforcetests[i].siglen;
713 s = policyenforcetests[i].check == POLICY_CHECK_DIGEST ?
714 NULL : &strength;
715 mech.mechanism = policyenforcetests[i].mech;
716 rc = p.is_mech_allowed(&p, &mech, s, policyenforcetests[i].check, NULL);
717 if (rc != policyenforcetests[i].exprc) {
718 fprintf(stderr,
719 "Test %u: Unexpected result: 0x%lx (expected 0x%lx)\n",
720 i, rc, policyenforcetests[i].exprc);
721 res = -1;
722 }
723 }
724 policy_private_free(pp);
725 return res;
726 }
727
728 static int runpolicyhashtests(void)
729 {
730 struct objstrength strength;
731 struct policy_private *pp;
732 CK_MECHANISM mech;
733 struct policy p;
734 unsigned int o, i;
735 CK_RV rc;
736 int res;
737
738 fprintf(stderr, "Running policyhashtests\n");
739 res = 0;
740 policy_init_policy(&p);
741 mech.pParameter = NULL;
742 mech.ulParameterLen = 0;
743 strength.strength = 0;
744 strength.siglen = 0;
745 strength.allowed = CK_TRUE;
746 for (o = 0; o < ARRAYSIZE(policyhashtests); ++o) {
747 pp = policy_private_alloc();
748 if (pp == NULL) {
749 fprintf(stderr, "Test %u: Failed to allocate policy_private\n", o);
750 return -1;
751 }
752 p.priv = pp;
753 if (test_load_strength_cfg(pp,(void *)niststrength,
754 sizeof(niststrength))) {
755 policy_private_free(pp);
756 fprintf(stderr, "Test %u: Failed to load strength configuration\n",
757 o);
758 return -1;
759 }
760 if (test_load_policy_cfg(pp, (void *)policyhashtests[o].policy,
761 policyhashtests[o].policysize)) {
762 policy_private_free(pp);
763 fprintf(stderr, "Test %u: Failed to load policy configuration\n",
764 o);
765 return -1;
766 }
767 for (i = 0; i < policyhashtests[o].nummechs; ++i) {
768 mech.mechanism = policyhashtests[o].mechs[i];
769 /* check is wrong for most, but is chosen to not perform
770 any additional size checking. */
771 rc = p.is_mech_allowed(&p, &mech, &strength, POLICY_CHECK_ENCRYPT,
772 NULL);
773 if (rc != policyhashtests[o].exprcs[i]) {
774 fprintf(stderr, "Test %u case %u: Wrong result: 0x%lx (expected 0x%lx)\n",
775 o, i, rc, policyhashtests[o].exprcs[i]);
776 res = -1;
777 }
778 }
779 policy_private_free(pp);
780 }
781 return res;
782 }
783
784 #define POLICYDEEPCHECKMGFNUM 4
785
786 static int runpolicydeepcheckmgftests(void)
787 {
788 static const CK_ULONG mgfs[POLICYDEEPCHECKMGFNUM] =
789 { CKG_MGF1_SHA1, CKG_MGF1_SHA512,
790 CKG_IBM_MGF1_SHA3_224, CKG_IBM_MGF1_SHA3_384 };
791 static const struct {
792 const char *policy;
793 size_t policylen;
794 CK_RV exprc[POLICYDEEPCHECKMGFNUM];
795 } policies[] =
796 {
797 { policyempty, sizeof(policyempty),
798 {CKR_OK, CKR_OK, CKR_OK, CKR_OK} },
799 { policymgfstandard, sizeof(policymgfstandard),
800 {CKR_OK, CKR_OK, CKR_FUNCTION_FAILED, CKR_FUNCTION_FAILED} },
801 { policymgfibm, sizeof(policymgfibm),
802 { CKR_FUNCTION_FAILED, CKR_FUNCTION_FAILED, CKR_OK, CKR_OK} },
803 { policymgfoneeach, sizeof(policymgfoneeach),
804 { CKR_FUNCTION_FAILED, CKR_OK, CKR_OK, CKR_FUNCTION_FAILED} },
805 { policymgfnone, sizeof(policymgfnone),
806 { CKR_FUNCTION_FAILED, CKR_FUNCTION_FAILED,
807 CKR_FUNCTION_FAILED, CKR_FUNCTION_FAILED } }
808 };
809 struct objstrength strength;
810 struct policy_private *pp;
811 CK_MECHANISM oaepmech, pssmech;
812 struct policy p;
813 unsigned int i, o;
814 CK_RV rc;
815 int res;
816 CK_RSA_PKCS_OAEP_PARAMS oaepparams;
817 CK_RSA_PKCS_PSS_PARAMS pssparams;
818
819 fprintf(stderr, "Running policydeepcheckmgftests\n");
820 res = 0;
821 policy_init_policy(&p);
822 oaepparams.hashAlg = CKM_SHA_1;
823 oaepparams.source = CKZ_DATA_SPECIFIED;
824 oaepparams.pSourceData = NULL;
825 oaepparams.ulSourceDataLen = 0;
826 pssparams.hashAlg = CKM_SHA_1;
827 pssparams.sLen = 0;
828 strength.strength = 0;
829 strength.siglen = 0;
830 strength.allowed = CK_TRUE;
831 oaepmech.mechanism = CKM_RSA_PKCS_OAEP;
832 oaepmech.pParameter = &oaepparams;
833 oaepmech.ulParameterLen = sizeof(oaepparams);
834 pssmech.mechanism = CKM_RSA_PKCS_PSS;
835 pssmech.pParameter = &pssparams;
836 pssmech.ulParameterLen = sizeof(pssparams);
837
838 for (o = 0; o < ARRAYSIZE(policies); ++o) {
839 pp = policy_private_alloc();
840 if (pp == NULL) {
841 fprintf(stderr, "Test %u: Failed to allocate policy_private\n", o);
842 return -1;
843 }
844 p.priv = pp;
845 if (test_load_strength_cfg(pp,(void *)niststrength,
846 sizeof(niststrength))) {
847 policy_private_free(pp);
848 fprintf(stderr, "Test %u: Failed to load strength configuration\n",
849 o);
850 return -1;
851 }
852 if (test_load_policy_cfg(pp, (void *)policies[o].policy,
853 policies[o].policylen)) {
854 policy_private_free(pp);
855 fprintf(stderr, "Test %u: Failed to load policy configuration\n",
856 o);
857 return -1;
858 }
859 for (i = 0; i < ARRAYSIZE(mgfs); ++i) {
860 oaepparams.mgf = pssparams.mgf = mgfs[i];
861 /* Checking encrypt prevents output size checking here
862 which is not the goal of this test. */
863 rc = p.is_mech_allowed(&p, &oaepmech, &strength,
864 POLICY_CHECK_ENCRYPT,
865 NULL);
866 if (rc != policies[o].exprc[i]) {
867 fprintf(stderr,
868 "Test %u, case %u, OAEP: unexpected result 0x%lx (expected 0x%lx)\n",
869 o, i, rc, policies[o].exprc[i]);
870 res = -1;
871 }
872 rc = p.is_mech_allowed(&p, &pssmech, &strength,
873 POLICY_CHECK_ENCRYPT,
874 NULL);
875 if (rc != policies[o].exprc[i]) {
876 fprintf(stderr,
877 "Test %u, case %u, PSS: unexpected result 0x%lx (expected 0x%lx)\n",
878 o, i, rc, policies[o].exprc[i]);
879 res = -1;
880 }
881 }
882 p.priv = pp = policy_private_free(pp);
883 }
884 return res;
885 }
886
887 #define POLICYDEEPCHECKKDFNUM 4
888
889 static int runpolicydeepcheckkdftests(void)
890 {
891 static const CK_ULONG kdfs[POLICYDEEPCHECKKDFNUM] =
892 { CKD_NULL, CKD_SHA1_KDF_ASN1, CKD_SHA256_KDF, CKD_SHA512_KDF };
893 static const struct {
894 const char *policy;
895 size_t policylen;
896 CK_RV exprc[POLICYDEEPCHECKKDFNUM];
897 } policies[] =
898 {
899 {
900 policyempty, sizeof(policyempty),
901 { CKR_OK, CKR_OK, CKR_OK, CKR_OK }
902 },
903 {
904 policykdfnosha1, sizeof(policykdfnosha1),
905 { CKR_FUNCTION_FAILED, CKR_FUNCTION_FAILED, CKR_OK, CKR_OK }
906 },
907 {
908 policykdfnone, sizeof(policykdfnone),
909 { CKR_FUNCTION_FAILED, CKR_FUNCTION_FAILED,
910 CKR_FUNCTION_FAILED, CKR_FUNCTION_FAILED }
911 }
912 };
913 CK_ECDH1_DERIVE_PARAMS ecdhparams;
914 struct objstrength strength;
915 struct policy_private *pp;
916 CK_MECHANISM mech;
917 struct policy p;
918 unsigned int i, o;
919 CK_RV rc;
920 int res;
921
922 fprintf(stderr, "Running policydeepcheckkdftests\n");
923 res = 0;
924 policy_init_policy(&p);
925 strength.strength = 0;
926 strength.siglen = 0;
927 strength.allowed = CK_TRUE;
928 ecdhparams.ulSharedDataLen = 0;
929 ecdhparams.pSharedData = NULL;
930 ecdhparams.ulPublicDataLen = 0;
931 ecdhparams.pPublicData = NULL;
932 mech.mechanism = CKM_ECDH1_DERIVE;
933 mech.pParameter = &ecdhparams;
934 mech.ulParameterLen = sizeof(ecdhparams);
935 for (o = 0; o < ARRAYSIZE(policies); ++o) {
936 fprintf(stderr, "Test %u\n", o);
937 pp = policy_private_alloc();
938 if (pp == NULL) {
939 fprintf(stderr, "Test %u: Failed to allocate policy_private\n", o);
940 return -1;
941 }
942 p.priv = pp;
943 if (test_load_strength_cfg(pp,(void *)niststrength,
944 sizeof(niststrength))) {
945 policy_private_free(pp);
946 fprintf(stderr, "Test %u: Failed to load strength configuration\n",
947 o);
948 return -1;
949 }
950 if (test_load_policy_cfg(pp, (void *)policies[o].policy,
951 policies[o].policylen)) {
952 policy_private_free(pp);
953 fprintf(stderr, "Test %u: Failed to load policy configuration\n",
954 o);
955 return -1;
956 }
957 for (i = 0; i < ARRAYSIZE(kdfs); ++i) {
958 fprintf(stderr, "Case %u\n", i);
959 ecdhparams.kdf = kdfs[i];
960 rc = p.is_mech_allowed(&p, &mech, &strength, POLICY_CHECK_DERIVE,
961 NULL);
962 if (rc != policies[o].exprc[i]) {
963 fprintf(stderr,
964 "Test %u, case %u: unexpected result 0x%lx (expected 0x%lx)\n",
965 o, i, rc, policies[o].exprc[i]);
966 res = -1;
967 }
968 }
969 p.priv = pp = policy_private_free(pp);
970 }
971 return res;
972 }
973
974 static int runpolicydeepchecktests(void)
975 {
976 return runpolicydeepcheckmgftests() | runpolicydeepcheckkdftests();
977 }
978
979 static int runstrengthtests(void)
980 {
981 return runstrengthdettests() | runstrengthenforcetests();
982 }
983
984 static int runpolicytests(void)
985 {
986 return runpolicyenforcetests() | runpolicyhashtests() |
987 runpolicydeepchecktests();
988 }
989
990 int main(void)
991 {
992 if (runstrengthtests())
993 return TEST_FAIL;
994 if (runpolicytests())
995 return TEST_FAIL;
996 return TEST_PASS;
997 }