-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpicocert_tests.c
More file actions
1317 lines (1062 loc) · 46.9 KB
/
Copy pathpicocert_tests.c
File metadata and controls
1317 lines (1062 loc) · 46.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <openssl/bn.h>
#include <openssl/ec.h>
#include <openssl/ecdsa.h>
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "picocert.h"
// Test framework macros
#define TEST_ASSERT(condition, message) \
do { \
if (!(condition)) { \
printf("FAIL: %s - %s\n", __func__, message); \
return 0; \
} \
} while (0)
#define TEST_PASS() \
do { \
printf("PASS: %s\n", __func__); \
return 1; \
} while (0)
// Mock time function for testing
static uint64_t mock_current_time = 1000000000; // Some base time
uint64_t mock_time_callback(void) { return mock_current_time; }
// =============================================================================
// OPENSSL CRYPTO FUNCTIONS
// =============================================================================
bool openssl_sha256_hash(const uint8_t* data, uint32_t data_len,
uint8_t* digest, uint32_t digest_len) {
if (digest_len < PICOCERT_HASH_SHA256_DIGEST_SIZE) {
return false;
}
EVP_MD_CTX* ctx = EVP_MD_CTX_new();
if (!ctx) {
return false;
}
if (EVP_DigestInit_ex(ctx, EVP_sha256(), NULL) != 1) {
EVP_MD_CTX_free(ctx);
return false;
}
if (EVP_DigestUpdate(ctx, data, data_len) != 1) {
EVP_MD_CTX_free(ctx);
return false;
}
unsigned int digest_len_out;
if (EVP_DigestFinal_ex(ctx, digest, &digest_len_out) != 1) {
EVP_MD_CTX_free(ctx);
return false;
}
EVP_MD_CTX_free(ctx);
return (digest_len_out == PICOCERT_HASH_SHA256_DIGEST_SIZE);
}
/**
* Real ECC signature verification using OpenSSL
*/
bool openssl_ecc_verify(const uint8_t* key, size_t key_size,
const uint8_t* hash, uint32_t hash_len,
const uint8_t* signature) {
if (!key || !hash || !signature ||
hash_len != PICOCERT_HASH_SHA256_DIGEST_SIZE) {
return false;
}
if (key_size != PICOCERT_ECC_PUBKEY_SIZE_ECDSA_UNCOMPRESSED) {
return false;
}
// Create EC_KEY from public key data
EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
if (!ec_key) {
return false;
}
// The public key data is 64 bytes (32 bytes X + 32 bytes Y)
const uint8_t* pubkey = key;
BIGNUM* x = BN_bin2bn(pubkey, 32, NULL);
BIGNUM* y = BN_bin2bn(pubkey + 32, 32, NULL);
if (!x || !y) {
EC_KEY_free(ec_key);
if (x) BN_free(x);
if (y) BN_free(y);
return false;
}
EC_POINT* point = EC_POINT_new(EC_KEY_get0_group(ec_key));
if (!point) {
EC_KEY_free(ec_key);
BN_free(x);
BN_free(y);
return false;
}
if (EC_POINT_set_affine_coordinates_GFp(EC_KEY_get0_group(ec_key), point, x,
y, NULL) != 1) {
EC_KEY_free(ec_key);
EC_POINT_free(point);
BN_free(x);
BN_free(y);
return false;
}
if (EC_KEY_set_public_key(ec_key, point) != 1) {
EC_KEY_free(ec_key);
EC_POINT_free(point);
BN_free(x);
BN_free(y);
return false;
}
// Parse signature (r, s) from DER format or raw format
// For simplicity, assume raw format: first 32 bytes r, next 32 bytes s
ECDSA_SIG* ecdsa_sig = ECDSA_SIG_new();
if (!ecdsa_sig) {
EC_KEY_free(ec_key);
EC_POINT_free(point);
BN_free(x);
BN_free(y);
return false;
}
BIGNUM* r = BN_bin2bn(signature, 32, NULL);
BIGNUM* s = BN_bin2bn(signature + 32, 32, NULL);
if (!r || !s) {
ECDSA_SIG_free(ecdsa_sig);
EC_KEY_free(ec_key);
EC_POINT_free(point);
BN_free(x);
BN_free(y);
if (r) BN_free(r);
if (s) BN_free(s);
return false;
}
if (ECDSA_SIG_set0(ecdsa_sig, r, s) != 1) {
ECDSA_SIG_free(ecdsa_sig);
EC_KEY_free(ec_key);
EC_POINT_free(point);
BN_free(x);
BN_free(y);
BN_free(r);
BN_free(s);
return false;
}
// Verify signature
int result = ECDSA_do_verify(hash, hash_len, ecdsa_sig, ec_key);
// Cleanup
ECDSA_SIG_free(ecdsa_sig);
EC_KEY_free(ec_key);
EC_POINT_free(point);
BN_free(x);
BN_free(y);
return (result == 1);
}
bool accept_all_ecc_signatures(const uint8_t* key, size_t key_size,
const uint8_t* hash, uint32_t hash_len,
const uint8_t* signature) {
(void)key;
(void)key_size;
(void)hash;
(void)hash_len;
(void)signature;
return true;
}
// Helper function to initialize picocert context for testing with OpenSSL
picocert_err_t init_picocert_context_for_testing(picocert_context_t* ctx) {
return picocert_init_context(ctx, openssl_sha256_hash, openssl_ecc_verify,
mock_time_callback);
}
// Helper function to create a test certificate
picocert_t create_test_cert(const char* issuer, const char* subject,
uint64_t valid_from, uint64_t valid_to) {
picocert_t cert = {0};
cert.version = PICOCERT_CURRENT_VERSION;
strncpy(cert.issuer, issuer, PICOCERT_MAX_NAME_LEN - 1);
cert.issuer[PICOCERT_MAX_NAME_LEN - 1] = '\0'; // Ensure null termination
strncpy(cert.subject, subject, PICOCERT_MAX_NAME_LEN - 1);
cert.subject[PICOCERT_MAX_NAME_LEN - 1] = '\0'; // Ensure null termination
picocert_set_valid_from(&cert, valid_from);
picocert_set_valid_to(&cert, valid_to);
cert.curve = PICOCERT_P256;
cert.hash = PICOCERT_SHA256;
picocert_set_reserved(&cert, 0);
// Generate a real key pair for testing
EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
if (ec_key && EC_KEY_generate_key(ec_key) == 1) {
const EC_POINT* pubkey_point = EC_KEY_get0_public_key(ec_key);
const EC_GROUP* group = EC_KEY_get0_group(ec_key);
BIGNUM* x = BN_new();
BIGNUM* y = BN_new();
if (x && y &&
EC_POINT_get_affine_coordinates_GFp(group, pubkey_point, x, y, NULL) ==
1) {
cert.public_key[0] = 0x04; // Uncompressed key marker
// Convert BIGNUMs to binary
int x_len = BN_num_bytes(x);
int y_len = BN_num_bytes(y);
// Pad with zeros if needed
memset(&cert.public_key[1], 0, 32);
memset(&cert.public_key[33], 0, 32);
BN_bn2bin(x, &cert.public_key[1 + (32 - x_len)]);
BN_bn2bin(y, &cert.public_key[33 + (32 - y_len)]);
// Create a signature using the private key
uint8_t cert_data[sizeof(picocert_t) - sizeof(cert.signature)];
memcpy(cert_data, &cert, sizeof(cert_data));
uint8_t hash[PICOCERT_HASH_SHA256_DIGEST_SIZE];
if (openssl_sha256_hash(cert_data, sizeof(cert_data), hash,
sizeof(hash))) {
ECDSA_SIG* sig = ECDSA_do_sign(hash, sizeof(hash), ec_key);
if (sig) {
const BIGNUM* r = ECDSA_SIG_get0_r(sig);
const BIGNUM* s = ECDSA_SIG_get0_s(sig);
// Convert signature to raw format
memset(cert.signature, 0, sizeof(cert.signature));
int r_len = BN_num_bytes(r);
int s_len = BN_num_bytes(s);
BN_bn2bin(r, &cert.signature[32 - r_len]);
BN_bn2bin(s, &cert.signature[64 - s_len]);
ECDSA_SIG_free(sig);
}
}
}
if (x) BN_free(x);
if (y) BN_free(y);
}
if (ec_key) {
EC_KEY_free(ec_key);
}
return cert;
}
// Helper structure to hold a certificate with its private key
typedef struct {
picocert_t cert;
EC_KEY* private_key;
} cert_with_key_t;
// Create a certificate with a real key pair that can be signed by another
// cert's private key
cert_with_key_t create_cert_with_key(const char* issuer, const char* subject,
uint64_t valid_from, uint64_t valid_to,
EC_KEY* issuer_private_key) {
cert_with_key_t cert_with_key = {0};
picocert_t* cert = &cert_with_key.cert;
cert->version = PICOCERT_CURRENT_VERSION;
strncpy(cert->issuer, issuer, PICOCERT_MAX_NAME_LEN - 1);
cert->issuer[PICOCERT_MAX_NAME_LEN - 1] = '\0';
strncpy(cert->subject, subject, PICOCERT_MAX_NAME_LEN - 1);
cert->subject[PICOCERT_MAX_NAME_LEN - 1] = '\0';
picocert_set_valid_from(cert, valid_from);
picocert_set_valid_to(cert, valid_to);
cert->curve = PICOCERT_P256;
cert->hash = PICOCERT_SHA256;
picocert_set_reserved(cert, 0);
// Generate a new key pair for this certificate
EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
if (!ec_key || EC_KEY_generate_key(ec_key) != 1) {
if (ec_key) EC_KEY_free(ec_key);
return cert_with_key; // Return empty cert on error
}
cert_with_key.private_key = ec_key;
// Set the public key in the certificate
const EC_POINT* pubkey_point = EC_KEY_get0_public_key(ec_key);
const EC_GROUP* group = EC_KEY_get0_group(ec_key);
BIGNUM* x = BN_new();
BIGNUM* y = BN_new();
if (x && y &&
EC_POINT_get_affine_coordinates_GFp(group, pubkey_point, x, y, NULL) ==
1) {
cert->public_key[0] = 0x04; // Uncompressed key marker
// Convert BIGNUMs to binary
int x_len = BN_num_bytes(x);
int y_len = BN_num_bytes(y);
// Pad with zeros if needed
memset(&cert->public_key[1], 0, 32);
memset(&cert->public_key[33], 0, 32);
BN_bn2bin(x, &cert->public_key[1 + (32 - x_len)]);
BN_bn2bin(y, &cert->public_key[33 + (32 - y_len)]);
// Create certificate data to be signed (everything except signature)
uint8_t cert_data[sizeof(picocert_t) - sizeof(cert->signature)];
memcpy(cert_data, cert, sizeof(cert_data));
// Hash the certificate data
uint8_t hash[PICOCERT_HASH_SHA256_DIGEST_SIZE];
if (openssl_sha256_hash(cert_data, sizeof(cert_data), hash, sizeof(hash))) {
// Sign with the issuer's private key (or own key if self-signed)
EC_KEY* signing_key = issuer_private_key ? issuer_private_key : ec_key;
ECDSA_SIG* sig = ECDSA_do_sign(hash, sizeof(hash), signing_key);
if (sig) {
const BIGNUM* r = ECDSA_SIG_get0_r(sig);
const BIGNUM* s = ECDSA_SIG_get0_s(sig);
// Convert signature to raw format
memset(cert->signature, 0, sizeof(cert->signature));
int r_len = BN_num_bytes(r);
int s_len = BN_num_bytes(s);
BN_bn2bin(r, &cert->signature[32 - r_len]);
BN_bn2bin(s, &cert->signature[64 - s_len]);
ECDSA_SIG_free(sig);
}
}
}
if (x) BN_free(x);
if (y) BN_free(y);
return cert_with_key;
}
// Clean up cert_with_key_t structure
void cleanup_cert_with_key(cert_with_key_t* cert_with_key) {
if (cert_with_key && cert_with_key->private_key) {
EC_KEY_free(cert_with_key->private_key);
cert_with_key->private_key = NULL;
}
}
// Test proper 3-tier PKI validation with real certificates
int test_3tier_pki_validation(void) {
picocert_context_t ctx = {0};
init_picocert_context_for_testing(&ctx);
printf("\n=== 3-Tier PKI Validation Test ===\n");
// Create root CA (self-signed)
cert_with_key_t root_ca =
create_cert_with_key("RootCA", "RootCA", mock_current_time - 1000,
mock_current_time + 10000, NULL);
TEST_ASSERT(root_ca.private_key != NULL, "Root CA should have private key");
TEST_ASSERT(picocert_is_self_signed(&root_ca.cert),
"Root CA should be self-signed");
printf("Created Root CA: %s\n", root_ca.cert.subject);
// Create intermediate CA signed by root CA
cert_with_key_t intermediate_ca =
create_cert_with_key("RootCA", "IntermediateCA", mock_current_time - 500,
mock_current_time + 8000, root_ca.private_key);
TEST_ASSERT(intermediate_ca.private_key != NULL,
"Intermediate CA should have private key");
TEST_ASSERT(!picocert_is_self_signed(&intermediate_ca.cert),
"Intermediate CA should not be self-signed");
printf("Created Intermediate CA: %s (signed by %s)\n",
intermediate_ca.cert.subject, intermediate_ca.cert.issuer);
// Create leaf certificate signed by intermediate CA
cert_with_key_t leaf_cert = create_cert_with_key(
"IntermediateCA", "LeafCert", mock_current_time - 100,
mock_current_time + 5000, intermediate_ca.private_key);
TEST_ASSERT(leaf_cert.private_key != NULL,
"Leaf cert should have private key");
TEST_ASSERT(!picocert_is_self_signed(&leaf_cert.cert),
"Leaf cert should not be self-signed");
printf("Created Leaf Certificate: %s (signed by %s)\n",
leaf_cert.cert.subject, leaf_cert.cert.issuer);
// Test individual certificate validations
printf("\n--- Individual Certificate Validations ---\n");
// Validate root CA (self-signed)
picocert_err_t err =
picocert_validate_cert(&ctx, &root_ca.cert, &root_ca.cert);
TEST_ASSERT(err == PICOCERT_OK, "Root CA self-validation should succeed");
printf("✓ Root CA self-validation passed\n");
// Validate intermediate CA against root CA
err = picocert_validate_cert(&ctx, &root_ca.cert, &intermediate_ca.cert);
TEST_ASSERT(err == PICOCERT_OK,
"Intermediate CA validation against root should succeed");
printf("✓ Intermediate CA validation against root passed\n");
// Validate leaf cert against intermediate CA
err = picocert_validate_cert(&ctx, &intermediate_ca.cert, &leaf_cert.cert);
TEST_ASSERT(err == PICOCERT_OK,
"Leaf cert validation against intermediate should succeed");
printf("✓ Leaf cert validation against intermediate passed\n");
// Test full certificate chain validation
printf("\n--- Full Certificate Chain Validation ---\n");
picocert_t cert_chain[3] = {leaf_cert.cert, intermediate_ca.cert,
root_ca.cert};
err = picocert_validate_cert_chain(&ctx, cert_chain, 3, &root_ca.cert);
TEST_ASSERT(err == PICOCERT_OK,
"Full 3-tier certificate chain validation should succeed");
printf("✓ Full 3-tier certificate chain validation passed\n");
// Test data signing and verification with the leaf certificate
printf("\n--- Data Signing and Verification ---\n");
const char* test_data =
"This is important data signed by the leaf certificate";
uint32_t data_len = strlen(test_data);
// Hash the data
uint8_t data_hash[PICOCERT_HASH_SHA256_DIGEST_SIZE];
TEST_ASSERT(openssl_sha256_hash((const uint8_t*)test_data, data_len,
data_hash, sizeof(data_hash)),
"Data hashing should succeed");
// Sign the hash with the leaf certificate's private key
ECDSA_SIG* sig =
ECDSA_do_sign(data_hash, sizeof(data_hash), leaf_cert.private_key);
TEST_ASSERT(sig != NULL, "Data signing should succeed");
// Convert signature to raw format
uint8_t signature[PICOCERT_ECC_SIG_SIZE] = {0};
const BIGNUM* r = ECDSA_SIG_get0_r(sig);
const BIGNUM* s = ECDSA_SIG_get0_s(sig);
int r_len = BN_num_bytes(r);
int s_len = BN_num_bytes(s);
BN_bn2bin(r, &signature[32 - r_len]);
BN_bn2bin(s, &signature[64 - s_len]);
ECDSA_SIG_free(sig);
printf("✓ Data signed with leaf certificate's private key\n");
// Verify the signature using the leaf certificate's public key
err = picocert_verify_hash(&ctx, &leaf_cert.cert, data_hash, signature);
TEST_ASSERT(err == PICOCERT_OK,
"Signature verification with leaf cert should succeed");
printf("✓ Signature verification with leaf certificate passed\n");
// Test full end-to-end verification: validate chain + verify data
err = picocert_verify_hash_and_validate_chain(
&ctx, cert_chain, 3, &root_ca.cert, data_hash, signature);
TEST_ASSERT(err == PICOCERT_OK,
"Full end-to-end verification should succeed");
printf("✓ Full end-to-end verification (chain + data) passed\n");
// Test the hash-based data verification API with certificate chain validation
err = picocert_verify_hash_and_validate_chain(
&ctx, cert_chain, 3, &root_ca.cert, data_hash, signature);
TEST_ASSERT(err == PICOCERT_OK,
"Full data verification with chain should succeed");
printf("✓ Full data verification with chain passed\n");
// Test negative cases
printf("\n--- Negative Test Cases ---\n");
// Test with tampered data
const char* tampered_data =
"This is TAMPERED data signed by the leaf certificate";
uint32_t tampered_len = strlen(tampered_data);
// Hash the tampered data
uint8_t tampered_hash[PICOCERT_HASH_SHA256_DIGEST_SIZE];
TEST_ASSERT(openssl_sha256_hash((const uint8_t*)tampered_data, tampered_len,
tampered_hash, sizeof(tampered_hash)),
"Tampered data hashing should succeed");
// Try to verify with original signature (should fail)
err = picocert_verify_hash_and_validate_chain(
&ctx, cert_chain, 3, &root_ca.cert, tampered_hash, signature);
TEST_ASSERT(err == PICOCERT_ERR_SIGNATURE,
"Tampered data should fail verification");
printf("✓ Tampered data correctly rejected\n");
// Test with wrong certificate order
picocert_t wrong_order_chain[3] = {root_ca.cert, intermediate_ca.cert,
leaf_cert.cert};
err = picocert_validate_cert_chain(&ctx, wrong_order_chain, 3,
&root_ca.cert);
TEST_ASSERT(err != PICOCERT_OK,
"Wrong certificate order should fail validation");
printf("✓ Wrong certificate order correctly rejected\n");
// Test with missing intermediate certificate
picocert_t incomplete_chain[2] = {leaf_cert.cert, root_ca.cert};
err = picocert_validate_cert_chain(&ctx, incomplete_chain, 2, &root_ca.cert);
// A valid attacker-controlled chain must not validate against another root.
picocert_t untrusted_root = root_ca.cert;
untrusted_root.public_key[1] ^= 0x01;
err = picocert_validate_cert_chain(&ctx, cert_chain, 3, &untrusted_root);
TEST_ASSERT(err == PICOCERT_ERR_UNTRUSTED_ROOT,
"Chain should be rejected when its root is not trusted");
TEST_ASSERT(err != PICOCERT_OK,
"Incomplete certificate chain should fail validation");
printf("✓ Incomplete certificate chain correctly rejected\n");
// Clean up
cleanup_cert_with_key(&root_ca);
cleanup_cert_with_key(&intermediate_ca);
cleanup_cert_with_key(&leaf_cert);
printf("=== End 3-Tier PKI Validation Test ===\n\n");
TEST_PASS();
}
// =============================================================================
// TESTS
// =============================================================================
// Test library initialization
int test_library_initialization(void) {
picocert_context_t ctx = {0};
// Test with invalid parameters - NULL context
picocert_err_t err = picocert_init_context(
NULL, openssl_sha256_hash, openssl_ecc_verify, mock_time_callback);
TEST_ASSERT(err == PICOCERT_ERR_INVALID, "Should fail with NULL context");
// Test with invalid parameters - NULL hash function
err =
picocert_init_context(&ctx, NULL, openssl_ecc_verify, mock_time_callback);
TEST_ASSERT(err == PICOCERT_ERR_INVALID,
"Should fail with NULL hash function");
// Test with invalid parameters - NULL ECC verify function
err = picocert_init_context(&ctx, openssl_sha256_hash, NULL,
mock_time_callback);
TEST_ASSERT(err == PICOCERT_ERR_INVALID,
"Should fail with NULL ECC verify function");
// Test with valid parameters
err = picocert_init_context(&ctx, openssl_sha256_hash, openssl_ecc_verify,
mock_time_callback);
TEST_ASSERT(err == PICOCERT_OK, "Should succeed with valid parameters");
// Test time callback can be NULL
err = picocert_init_context(&ctx, openssl_sha256_hash, openssl_ecc_verify,
NULL);
TEST_ASSERT(err == PICOCERT_OK, "Should succeed with NULL time callback");
TEST_PASS();
}
// Test key extraction from certificate
int test_key_extraction(void) {
picocert_t cert = create_test_cert(
"TestIssuer", "TestSubject", mock_current_time, mock_current_time + 3600);
// Test successful key extraction
const uint8_t* key;
size_t key_size;
picocert_err_t err = picocert_cert_to_key(&cert, &key, &key_size);
TEST_ASSERT(err == PICOCERT_OK, "Should successfully extract key");
TEST_ASSERT(key != NULL, "Key pointer should not be NULL");
TEST_ASSERT(key_size == PICOCERT_ECC_PUBKEY_SIZE_ECDSA_UNCOMPRESSED,
"Key size should be correct");
TEST_ASSERT(key == &cert.public_key[1],
"Key should point to correct location");
// Test with NULL certificate
err = picocert_cert_to_key(NULL, &key, &key_size);
TEST_ASSERT(err == PICOCERT_ERR_INVALID, "Should fail with NULL cert");
// Test with NULL key_out
err = picocert_cert_to_key(&cert, NULL, &key_size);
TEST_ASSERT(err == PICOCERT_ERR_INVALID, "Should fail with NULL key_out");
// Test with NULL key_size_out
err = picocert_cert_to_key(&cert, &key, NULL);
TEST_ASSERT(err == PICOCERT_ERR_INVALID,
"Should fail with NULL key_size_out");
// Test with invalid public key format (not starting with 0x04)
picocert_t invalid_cert = cert;
invalid_cert.public_key[0] = 0x03; // compressed format
err = picocert_cert_to_key(&invalid_cert, &key, &key_size);
TEST_ASSERT(err == PICOCERT_ERR_INVALID_FORMAT,
"Should fail with invalid key format");
TEST_PASS();
}
// Test basic certificate structure
int test_certificate_structure(void) {
picocert_context_t ctx = {0};
init_picocert_context_for_testing(&ctx);
picocert_t cert = create_test_cert(
"TestIssuer", "TestSubject", mock_current_time, mock_current_time + 3600);
TEST_ASSERT(cert.version == PICOCERT_CURRENT_VERSION,
"Version should be current");
TEST_ASSERT(strcmp(cert.issuer, "TestIssuer") == 0, "Issuer should match");
TEST_ASSERT(strcmp(cert.subject, "TestSubject") == 0, "Subject should match");
TEST_ASSERT(cert.curve == PICOCERT_P256, "Curve should be P256");
TEST_ASSERT(cert.hash == PICOCERT_SHA256, "Hash should be SHA256");
TEST_ASSERT(picocert_get_reserved(&cert) == 0,
"Reserved field should be zero");
TEST_ASSERT(cert.public_key[0] == 0x04, "Public key should start with 0x04");
TEST_PASS();
}
// Test self-signed certificate detection
int test_self_signed_detection(void) {
picocert_context_t ctx = {0};
init_picocert_context_for_testing(&ctx);
picocert_t self_signed = create_test_cert(
"SelfSigned", "SelfSigned", mock_current_time, mock_current_time + 3600);
picocert_t not_self_signed = create_test_cert(
"Issuer", "Subject", mock_current_time, mock_current_time + 3600);
TEST_ASSERT(picocert_is_self_signed(&self_signed),
"Should detect self-signed cert");
TEST_ASSERT(!picocert_is_self_signed(¬_self_signed),
"Should detect non-self-signed cert");
picocert_t embedded_null_names = {0};
embedded_null_names.issuer[0] = 'A';
embedded_null_names.issuer[2] = 'X';
embedded_null_names.subject[0] = 'A';
embedded_null_names.subject[2] = 'Y';
TEST_ASSERT(!picocert_is_self_signed(&embedded_null_names),
"Bytes after an embedded NUL must be compared");
TEST_PASS();
}
int test_fixed_width_name_validation(void) {
picocert_context_t ctx = {0};
picocert_err_t err = picocert_init_context(
&ctx, openssl_sha256_hash, accept_all_ecc_signatures, mock_time_callback);
TEST_ASSERT(err == PICOCERT_OK, "Context initialization should succeed");
picocert_t issuer = create_test_cert(
"issuer", "issuer", mock_current_time - 1000, mock_current_time + 1000);
picocert_t subject = create_test_cert(
"issuer", "subject", mock_current_time - 1000, mock_current_time + 1000);
memset(issuer.subject, 0, sizeof(issuer.subject));
memset(subject.issuer, 0, sizeof(subject.issuer));
issuer.subject[0] = 'A';
issuer.subject[2] = 'X';
subject.issuer[0] = 'A';
subject.issuer[2] = 'Y';
err = picocert_validate_cert(&ctx, &issuer, &subject);
TEST_ASSERT(err == PICOCERT_ERR_ISSUER,
"Issuer names must differ when bytes after a NUL differ");
TEST_PASS();
}
// Test time callback functionality
int test_time_callback(void) {
picocert_context_t ctx = {0};
// Test without initialized context
uint64_t time1;
picocert_err_t err = picocert_current_time(NULL, &time1);
TEST_ASSERT(err == PICOCERT_ERR_INVALID, "Should fail with NULL context");
// Test with uninitialized context
uint64_t time2;
err = picocert_current_time(&ctx, &time2);
TEST_ASSERT(err == PICOCERT_ERR_INVALID, "Should fail when no callback set");
// Test with callback
init_picocert_context_for_testing(&ctx);
uint64_t time3;
err = picocert_current_time(&ctx, &time3);
TEST_ASSERT(err == PICOCERT_OK, "Should succeed with callback");
TEST_ASSERT(time3 == mock_current_time, "Should return callback time");
// Test initialization with NULL time callback
picocert_context_t ctx_no_time = {0};
err = picocert_init_context(&ctx_no_time, openssl_sha256_hash,
openssl_ecc_verify, NULL);
TEST_ASSERT(err == PICOCERT_OK, "Should initialize without time callback");
uint64_t time4;
err = picocert_current_time(&ctx_no_time, &time4);
TEST_ASSERT(err == PICOCERT_ERR_INVALID,
"Should fail when NULL callback was set during init");
// Test initialization with valid time callback
picocert_context_t ctx_with_time = {0};
err = picocert_init_context(&ctx_with_time, openssl_sha256_hash,
openssl_ecc_verify, mock_time_callback);
TEST_ASSERT(err == PICOCERT_OK, "Should initialize with time callback");
uint64_t time5;
err = picocert_current_time(&ctx_with_time, &time5);
TEST_ASSERT(err == PICOCERT_OK, "Should succeed with valid callback");
TEST_ASSERT(time5 == mock_current_time, "Should return callback time");
TEST_PASS();
}
// Test certificate validation with time
int test_certificate_time_validation(void) {
picocert_context_t ctx = {0};
init_picocert_context_for_testing(&ctx);
// Create valid certificate
picocert_t valid_cert = create_test_cert(
"Root", "Root", mock_current_time - 1000, mock_current_time + 1000);
// Create expired certificate
picocert_t expired_cert = create_test_cert(
"Root", "Root", mock_current_time - 2000, mock_current_time - 1000);
// Create not-yet-valid certificate
picocert_t future_cert = create_test_cert(
"Root", "Root", mock_current_time + 1000, mock_current_time + 2000);
// Test validation without time callback should fail
picocert_context_t ctx_no_time = {0};
picocert_err_t err = picocert_init_context(&ctx_no_time, openssl_sha256_hash,
openssl_ecc_verify, NULL);
TEST_ASSERT(err == PICOCERT_OK, "Should initialize without time callback");
err = picocert_validate_cert(&ctx_no_time, &valid_cert, &valid_cert);
TEST_ASSERT(err == PICOCERT_ERR_INVALID,
"Should fail validation without time callback");
// Test validation with time callback
picocert_err_t err1 = picocert_validate_cert(&ctx, &valid_cert, &valid_cert);
TEST_ASSERT(err1 == PICOCERT_OK, "Valid certificate should pass validation");
picocert_err_t err2 =
picocert_validate_cert(&ctx, &expired_cert, &expired_cert);
TEST_ASSERT(err2 == PICOCERT_ERR_EXPIRED,
"Expired certificate should fail validation");
picocert_err_t err3 =
picocert_validate_cert(&ctx, &future_cert, &future_cert);
TEST_ASSERT(err3 == PICOCERT_ERR_EXPIRED,
"Future certificate should fail validation");
TEST_PASS();
}
// Test hash verification
int test_hash_verification(void) {
picocert_context_t ctx = {0};
init_picocert_context_for_testing(&ctx);
picocert_t cert =
create_test_cert("TestCert", "TestCert", mock_current_time - 1000,
mock_current_time + 1000);
uint8_t test_hash[PICOCERT_HASH_SHA256_DIGEST_SIZE] = {0};
for (int i = 0; i < PICOCERT_HASH_SHA256_DIGEST_SIZE; i++) {
test_hash[i] = (uint8_t)(i + 0x30);
}
uint8_t test_signature[PICOCERT_ECC_SIG_SIZE] = {0};
for (int i = 0; i < PICOCERT_ECC_SIG_SIZE; i++) {
test_signature[i] = (uint8_t)(i + 0x40);
}
// Test hash verification (will fail with dummy data, but tests the API)
picocert_err_t err =
picocert_verify_hash(&ctx, &cert, test_hash, test_signature);
TEST_ASSERT(err == PICOCERT_ERR_SIGNATURE,
"Hash verification should fail with dummy data");
// Test with NULL context
err = picocert_verify_hash(NULL, &cert, test_hash, test_signature);
TEST_ASSERT(err == PICOCERT_ERR_INVALID, "NULL context should fail");
// Test with NULL certificate
err = picocert_verify_hash(&ctx, NULL, test_hash, test_signature);
TEST_ASSERT(err == PICOCERT_ERR_INVALID, "NULL certificate should fail");
// Test with NULL hash
err = picocert_verify_hash(&ctx, &cert, NULL, test_signature);
TEST_ASSERT(err == PICOCERT_ERR_INVALID, "NULL hash should fail");
TEST_PASS();
}
// Test data verification
int test_data_verification(void) {
picocert_context_t ctx = {0};
init_picocert_context_for_testing(&ctx);
picocert_t cert =
create_test_cert("TestCert", "TestCert", mock_current_time - 1000,
mock_current_time + 1000);
uint8_t test_hash[PICOCERT_HASH_SHA256_DIGEST_SIZE] = {0};
uint8_t test_signature[PICOCERT_ECC_SIG_SIZE] = {0};
// Test hash verification (will fail with random signature, but tests the API)
picocert_err_t err =
picocert_verify_hash(&ctx, &cert, test_hash, test_signature);
TEST_ASSERT(err == PICOCERT_ERR_SIGNATURE,
"Hash verification should fail with random signature");
// Test with NULL context
err = picocert_verify_hash(NULL, &cert, test_hash, test_signature);
TEST_ASSERT(err == PICOCERT_ERR_INVALID, "NULL context should fail");
// Test with NULL hash
err = picocert_verify_hash(&ctx, &cert, NULL, test_signature);
TEST_ASSERT(err == PICOCERT_ERR_INVALID, "NULL hash should fail");
// Test with NULL certificate
err = picocert_verify_hash(&ctx, NULL, test_hash, test_signature);
TEST_ASSERT(err == PICOCERT_ERR_INVALID, "NULL certificate should fail");
TEST_PASS();
}
// Test certificate chain validation
int test_certificate_chain_validation(void) {
picocert_context_t ctx = {0};
init_picocert_context_for_testing(&ctx);
// Create a simple 2-certificate chain
picocert_t root = create_test_cert("Root", "Root", mock_current_time - 1000,
mock_current_time + 1000);
picocert_t leaf = create_test_cert("Root", "Leaf", mock_current_time - 500,
mock_current_time + 500);
picocert_t chain[2] = {leaf, root};
// Test chain validation (will fail with mismatched keys, but tests the API)
picocert_err_t err = picocert_validate_cert_chain(&ctx, chain, 2, &root);
TEST_ASSERT(err == PICOCERT_ERR_SIGNATURE,
"Chain validation should fail with mismatched keys");
// Test with NULL context
err = picocert_validate_cert_chain(NULL, chain, 2, &root);
TEST_ASSERT(err == PICOCERT_ERR_INVALID, "NULL context should fail");
// Test with NULL chain
err = picocert_validate_cert_chain(&ctx, NULL, 2, &root);
TEST_ASSERT(err == PICOCERT_ERR_INVALID, "NULL chain should fail");
// Test with zero length
err = picocert_validate_cert_chain(&ctx, chain, 0, &root);
TEST_ASSERT(err == PICOCERT_ERR_INVALID, "Zero length chain should fail");
err = picocert_validate_cert_chain(&ctx, chain, 2, NULL);
TEST_ASSERT(err == PICOCERT_ERR_INVALID, "NULL trust anchor should fail");
TEST_PASS();
}
// Test certificate version validation
int test_version_validation(void) {
picocert_context_t ctx = {0};
init_picocert_context_for_testing(&ctx);
picocert_t cert = create_test_cert("Root", "Root", mock_current_time - 1000,
mock_current_time + 1000);
// Test with invalid version
cert.version = 99;
picocert_err_t err = picocert_validate_cert(&ctx, &cert, &cert);
TEST_ASSERT(err == PICOCERT_ERR_VERSION, "Invalid version should fail");
// Test with valid version
cert.version = PICOCERT_CURRENT_VERSION;
err = picocert_validate_cert(&ctx, &cert, &cert);
TEST_ASSERT(err == PICOCERT_OK, "Valid version should pass");
TEST_PASS();
}
// Test reserved field validation
int test_reserved_field_validation(void) {
picocert_context_t ctx = {0};
init_picocert_context_for_testing(&ctx);
picocert_t cert = create_test_cert("Root", "Root", mock_current_time - 1000,
mock_current_time + 1000);
// Test with non-zero reserved field
picocert_set_reserved(&cert, 1);
picocert_err_t err = picocert_validate_cert(&ctx, &cert, &cert);
TEST_ASSERT(err == PICOCERT_ERR_RESERVED,
"Non-zero reserved field should fail");
// Test with zero reserved field
picocert_set_reserved(&cert, 0);
err = picocert_validate_cert(&ctx, &cert, &cert);
TEST_ASSERT(err == PICOCERT_OK, "Zero reserved field should pass");
TEST_PASS();
}
// Test uninitialized library behavior
int test_uninitialized_library(void) {
picocert_context_t ctx = {0}; // Uninitialized context
// Try to use functions without proper initialization
picocert_t cert = create_test_cert("Test", "Test", mock_current_time - 1000,
mock_current_time + 1000);
uint8_t test_hash[PICOCERT_HASH_SHA256_DIGEST_SIZE] = {0};
uint8_t test_signature[PICOCERT_ECC_SIG_SIZE] = {0};
// These should fail because context is not initialized
picocert_err_t err =
picocert_verify_hash(&ctx, &cert, test_hash, test_signature);
TEST_ASSERT(err == PICOCERT_ERR_CONTEXT_NOT_INITIALIZED,
"Should fail when not initialized");
// Test certificate signature verification with uninitialized context
err = picocert_verify_cert_signature(&ctx, &cert, &cert);
TEST_ASSERT(err == PICOCERT_ERR_CONTEXT_NOT_INITIALIZED,
"Should fail when not initialized");
TEST_PASS();
}
// Test public key format validation
int test_public_key_format_validation(void) {
picocert_context_t ctx = {0};
init_picocert_context_for_testing(&ctx);
picocert_t cert =
create_test_cert("TestCert", "TestCert", mock_current_time - 1000,
mock_current_time + 1000);
// Test with valid uncompressed format (0x04)
const uint8_t* key;
size_t key_size;
picocert_err_t err = picocert_cert_to_key(&cert, &key, &key_size);
TEST_ASSERT(err == PICOCERT_OK, "Valid uncompressed key should succeed");
// Test with compressed format (0x02) - should fail
cert.public_key[0] = 0x02;
err = picocert_cert_to_key(&cert, &key, &key_size);
TEST_ASSERT(err == PICOCERT_ERR_INVALID_FORMAT,
"Compressed key format should fail");
// Test with compressed format (0x03) - should fail
cert.public_key[0] = 0x03;
err = picocert_cert_to_key(&cert, &key, &key_size);
TEST_ASSERT(err == PICOCERT_ERR_INVALID_FORMAT,
"Compressed key format should fail");
// Test with invalid format (0x01) - should fail
cert.public_key[0] = 0x01;
err = picocert_cert_to_key(&cert, &key, &key_size);
TEST_ASSERT(err == PICOCERT_ERR_INVALID_FORMAT,
"Invalid key format should fail");