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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
|
/* Copyright 2017 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* This program is a stub 'signer' application used to test the Notes code signer framework. It
simply prints a 'hello' message and then the command line arguments it was called with.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include "openssl/evp.h"
#include "cca_functions.h"
#include "cca_structures.h"
#include "ossl_functions.h"
#ifdef ADD_ECC
#include "cca_structures_ecc.h"
#include "cca_functions_ecc.h"
#include "ossl_functions_ecc.h"
#endif
#include "utils.h"
#include "debug.h"
/* local prototypes */
int EchoArgs(int argc, char** argv);
int CheckAuxArgs(int argc, char** argv);
int GetArgs(const char **outputBodyFilename,
const char **usr,
const char **password,
const char **projectLogFileName,
const char **projectAuxConfigFileName,
const char **sender,
const char **project,
const char **keyFileName,
const char **inputAttachmentFileName,
const char **outputAttachmentFileName,
int *verbose,
int argc,
char **argv);
int GetAuxArgs(char **signAlgorithm,
const char *projectAuxConfigFileName);
void PrintUsage(void);
int SignSample(const char *keyFileName,
const char *inputAttachmentFileName,
const char *outputAttachmentFileName,
FILE *projectLogFile,
const char *signAlgorithm);
int SignSampleRSA(unsigned char *keyToken,
size_t keyTokenLength,
unsigned char *testMessage,
size_t testMessageLength,
const char *outputAttachmentFileName,
FILE *projectLogFile);
#ifdef ADD_ECC
int SignSampleECCP521(unsigned char *keyToken,
size_t keyTokenLength,
unsigned char *testMessage,
size_t testMessageLength,
const char *outputAttachmentFileName,
FILE *projectLogFile);
#endif
/* This table describes the parameters added by the auxiliary configuration file. They should not
be in the input body, as this might indicate a misguided or malicious attempt to override the
project auxiliary configuration file values. It might also indicate a program design error,
where a framework value clashes with the project auxiliary configuration file values.
Use this table to screen the user input.
*/
static const char *claTable[] = {
"-sign_algorithm",
};
/* messages are traced here */
FILE *messageFile = NULL;
int verbose = FALSE;
int debug = FALSE;
int main(int argc, char** argv)
{
int rc = 0;
const char *usr = NULL;
const char *password = NULL;
const char *projectLogFileName = NULL;
FILE *projectLogFile = NULL; /* closed @1 */
const char *projectAuxConfigFileName = NULL;
char *signAlgorithm = NULL; /* freed @2 */
time_t log_time;
const char *sender = NULL;
const char *project = NULL;
const char *keyFileName = NULL;
const char *inputAttachmentFileName = NULL;
const char *outputAttachmentFileName = NULL;
const char *outputBodyFilename = NULL;
messageFile = stdout;
/* OpenSSL_add_all_algorithms(); */
/* get caller's command line arguments */
if (rc == 0) {
rc = GetArgs(&outputBodyFilename,
&usr,
&password,
&projectLogFileName,
&projectAuxConfigFileName,
&sender,
&project,
&keyFileName,
&inputAttachmentFileName,
&outputAttachmentFileName,
&verbose,
argc, argv);
}
/* check that no command line arguments clash with those in the project auxiliary configuration
file */
if (rc == 0) {
rc = CheckAuxArgs(argc, argv);
}
/* get additional parameters from the project auxiliary configuration file */
if (rc == 0) {
rc = GetAuxArgs(&signAlgorithm, /* freed @2 */
projectAuxConfigFileName);
}
/* sample - log in to CCA */
if (rc == 0) {
if (verbose) fprintf(messageFile, "Hello from framework_test\n");
if (verbose) fprintf(messageFile, "Logging in with user name %s\n", usr);
rc = Login_Control(TRUE, /* log in */
usr, /* CCA profile */
password); /* CCA password */
}
/* sample audit logging */
if (rc == 0) {
if (verbose) fprintf(messageFile, "Opening audit log %s\n", projectLogFileName);
projectLogFile = fopen(projectLogFileName, "a"); /* closed @1 */
if (projectLogFile == NULL) {
fprintf(messageFile, "ERROR1015: Cannot open audit log %s, %s\n",
projectLogFileName, strerror(errno));
rc = ERROR_CODE;
}
}
/* update audit log, begin this entry */
if (projectLogFile != NULL) {
if (verbose) fprintf(messageFile, "Updating audit log\n");
log_time = time(NULL);
fprintf(projectLogFile, "\n%s", ctime(&log_time));
fprintf(projectLogFile, "\tSender: %s\n", sender);
fprintf(projectLogFile, "\tProject: %s\n", project);
fprintf(projectLogFile, "\tProgram: %s\n", argv[0]);
fprintf(projectLogFile, "\tKey file: %s\n", keyFileName);
fprintf(projectLogFile, "\tProfile %s\n", usr);
}
/*
sample - sign and verify
*/
if (rc == 0) {
if (verbose) fprintf(messageFile, "Signing\n");
if (verbose) fprintf(messageFile, " Input attachment %s\n", inputAttachmentFileName);
if (verbose) fprintf(messageFile, " Output attachment %s\n", outputAttachmentFileName);
rc = SignSample(keyFileName,
inputAttachmentFileName,
outputAttachmentFileName,
projectLogFile,
signAlgorithm);
}
/* sample log out of CCA */
if (rc == 0) {
if (verbose) fprintf(messageFile, "Logging out with user name %s\n", usr);
rc = Login_Control(FALSE, /* log out */
usr, /* CCA profile */
NULL); /* password */
}
/* stub processor prints the command line arguments */
if (rc == 0) {
rc = EchoArgs(argc, argv);
}
fprintf(messageFile, "Return code: %u\n", rc);
/* update audit log */
if (projectLogFile != NULL) {
fprintf(projectLogFile, "\tReturn code: %d\n", rc);
}
/* clean up */
if (projectLogFile != NULL) {
fclose(projectLogFile); /* @1 */
}
free(signAlgorithm); /* @2 */
if (messageFile != stdout) {
fflush(messageFile);
fclose(messageFile);
}
return rc;
}
/* SignSample() is sample code to demonstrate CCA signing and verification
*/
int SignSample(const char *keyFileName,
const char *inputAttachmentFileName,
const char *outputAttachmentFileName,
FILE *projectLogFile,
const char *signAlgorithm)
{
int rc = 0;
/*
signing key
*/
unsigned char *keyToken = NULL; /* CCA key token */
size_t keyTokenLength;
/*
data to be signed
*/
unsigned char *testMessage = NULL;
size_t testMessageLength;
/* get the CCA key token */
if (rc == 0) {
if (verbose) fprintf(messageFile, "SignSample: Reading CCA key token file %s\n",
keyFileName);
rc = File_ReadBinaryFile(&keyToken, &keyTokenLength, 2000, keyFileName); /* freed @1 */
if (rc != 0) {
fprintf(messageFile, "ERROR1026: Could not open key file: %s\n", keyFileName);
}
}
/* get the input message */
if (rc == 0) {
if (verbose) fprintf(messageFile, "SignSample: Reading input file %s\n",
inputAttachmentFileName);
rc = File_ReadBinaryFile(&testMessage , &testMessageLength, 10000,
inputAttachmentFileName); /* freed @2 */
if (rc != 0) {
fprintf(messageFile, "ERROR1027 while opening the attachment, file: %s\n",
inputAttachmentFileName);
}
}
if (rc == 0) {
if (strcmp(signAlgorithm, "rsa") == 0) {
rc = SignSampleRSA(keyToken,
keyTokenLength,
testMessage,
testMessageLength,
outputAttachmentFileName,
projectLogFile);
}
#ifdef ADD_ECC
else if (strcmp(signAlgorithm, "eccp521") == 0) {
rc = SignSampleECCP521(keyToken,
keyTokenLength,
testMessage,
testMessageLength,
outputAttachmentFileName,
projectLogFile);
}
#endif
else {
fprintf(messageFile,
"ERROR1022: Unsupported signature algorithm: %s\n",
signAlgorithm);
rc = ERROR_CODE;
}
}
/* clean up */
free(keyToken); /* @1 */
free(testMessage); /* @2 */
return rc;
}
int SignSampleRSA(unsigned char *keyToken,
size_t keyTokenLength,
unsigned char *testMessage,
size_t testMessageLength,
const char *outputAttachmentFileName,
FILE *projectLogFile)
{
int rc = 0;
int valid; /* true if signature verifies */
RsaKeyTokenPublic rsaKeyTokenPublic; /* CCA public key structure */
/* http://tools.ietf.org/html/draft-ietf-smime-sha2-11 */
/* SHA-1 with RSA OID (Object Identifier) */
static const unsigned char sha1_rsa_oid[] = {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E,
0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14};
/* SHA-256 with RSA OID (Object Identifier) */
static const unsigned char sha256_rsa_oid[] = {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
0x00, 0x04, 0x20};
/* SHA-512 with RSA OID (Object Identifier) */
static const unsigned char sha512_rsa_oid[] = {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05,
0x00, 0x04, 0x40};
/*
digest to be signed
*/
unsigned char hash1[sizeof(sha1_rsa_oid) + SHA1_SIZE]; /* OID + SHA-1 digest */
unsigned char hash256[sizeof(sha256_rsa_oid) + SHA256_SIZE]; /* OID + SHA-256 digest */
unsigned char hash512[sizeof(sha512_rsa_oid) + SHA512_SIZE]; /* OID + SHA-512 digest */
unsigned long hashLength;
/*
signature
*/
unsigned char signature[N_SIZE];
unsigned long signatureLength;
unsigned long signatureBitLength;
/* extract the CCA public key from the CCA key token */
if (rc == 0) {
if (verbose) fprintf(messageFile, "SignSample: key token length %u\n", (uint)keyTokenLength);
if (verbose)
fprintf(messageFile, "SignSample: extract the public key from CCA key token\n");
rc = getPKA96PublicKey(&rsaKeyTokenPublic, /* output: structure */
keyTokenLength,
keyToken, /* input: PKA96 key token */
2048);
}
unsigned int i; /* loop through digest algorithms */
const char *hashStr; /* test, for trace */
const unsigned char *oid; /* object identifier */
size_t oidSize; /* size of object identifier */
unsigned char *hash; /* OID + digest */
unsigned int digestLength; /* digest length */
long (*verifyFunc)(); /* signature verification function */
/* loop for SHA1, SHA256, SHA512 */
for (i = 0 ; (rc == 0) && (i < 3) ; i++) {
switch (i) {
case 0:
hashStr = "sha1";
oid = sha1_rsa_oid;
oidSize = sizeof(sha1_rsa_oid);
hash = hash1;
hashLength = sizeof(hash1);
digestLength = SHA1_SIZE;
verifyFunc = osslVerify;
break;
case 1:
hashStr = "sha256";
oid = sha256_rsa_oid;
oidSize = sizeof(sha256_rsa_oid);
hash = hash256;
hashLength = sizeof(hash256);
digestLength = SHA256_SIZE;
verifyFunc = osslVerify256;
break;
case 2:
hashStr = "sha512";
oid = sha512_rsa_oid;
oidSize = sizeof(sha512_rsa_oid);
hash = hash512;
hashLength = sizeof(hash512);
digestLength = SHA512_SIZE;
verifyFunc = osslVerify512;
break;
}
/* prepend OID */
if (rc == 0) {
if (verbose) fprintf(messageFile, "SignSample: Hashing input data with %s\n", hashStr);
memcpy(hash, oid, oidSize);
}
/* hash the data for signing, append the hash */
if (rc == 0) {
switch (i) {
case 0:
Ossl_SHA1(hash + oidSize,
testMessageLength, testMessage,
0L, NULL);
break;
case 1:
Ossl_SHA256(hash + oidSize,
testMessageLength, testMessage,
0L, NULL);
break;
case 2:
Ossl_SHA512(hash + oidSize,
testMessageLength, testMessage,
0L, NULL);
break;
}
}
/* sign with the coprocessor. The coprocessor doesn't know the digest algorithm. It just
signs an OID + digest1 */
if (rc == 0) {
if (verbose) PrintAll(messageFile,
"SignSample: hash to sign",
hashLength, hash);
signatureLength = sizeof(signature);
rc = Digital_Signature_Generate(&signatureLength, /* i/o */
&signatureBitLength, /* output */
signature, /* output */
keyTokenLength, /* input */
keyToken, /* input */
hashLength, /* input */
hash); /* input */
}
/* sample - create the audit log entry */
if (rc == 0) {
if (verbose) fprintf(messageFile, "SignSample: Updating audit log\n");
/* binary data as printable */
char pubkey_string[N_SIZE * 4];
char digest_string[SHA512_SIZE * 4]; /* use the largest */
char sig_string[N_SIZE * 4];
/* get the user and group structures */
/* binary to printable */
sprintAll(pubkey_string, N_SIZE, rsaKeyTokenPublic.n);
sprintAll(digest_string, digestLength, hash + oidSize);
sprintAll(sig_string, N_SIZE, signature);
/* send to audit log */
fprintf(projectLogFile, "\tPublic Key:\n %s\n", pubkey_string);
fprintf(projectLogFile, "\tDigest:\n %s\n", digest_string);
fprintf(projectLogFile, "\tSignature:\n %s\n", sig_string);
}
/*
The verify functions should never fail. They are just sanity checks on the code.
*/
/* sanity check on the signature length */
if (rc == 0) {
if (signatureLength != N_SIZE) {
fprintf(messageFile, "ERROR1001: signature invalid length %lu\n", signatureLength);
rc = ERROR_CODE;
}
}
/* verify the signature with the coprocessor key CCA token */
if (rc == 0) {
if (verbose)
fprintf(messageFile,
"SignSample: verify signature with the coprocessor key token\n");
rc = Digital_Signature_Verify(N_SIZE, /* input */
signature, /* input signature */
keyTokenLength, /* input */
keyToken, /* input key */
hashLength, /* input */
hash); /* input hash */
}
/* sample code to verify the signature using openssl */
if (rc == 0) {
if (verbose) fprintf(messageFile,
"SignSample: verify signature with OpenSSL and the key token\n");
rc = verifyFunc(&valid,
hash + oidSize, /* input: digest to be verified */
rsaKeyTokenPublic.e, /* exponent */
rsaKeyTokenPublic.eLength,
rsaKeyTokenPublic.n, /* public key */
rsaKeyTokenPublic.nByteLength,
signature, /* signature */
signatureLength);
if (!valid) {
fprintf(messageFile,
"ERROR1023: Error verifying signature with OpenSSL and the key token\n");
rc = ERROR_CODE;
}
}
/* write the SHA-256 signature to the output attachment */
if ((rc == 0) && (outputAttachmentFileName != NULL) && (i == 1)) {
if (verbose) fprintf(messageFile, "SignSample: Writing output file %s\n",
outputAttachmentFileName);
rc = File_WriteBinaryFile(signature, signatureLength, outputAttachmentFileName);
}
/* write the signature as hex ascii to the output body */
if (rc == 0) {
char *signatureString = NULL; /* freed @3 */
if (rc == 0) {
rc = Malloc_Safe((unsigned char **)&signatureString, /* freed @3 */
(signatureLength * 2) + 1,
(signatureLength * 2) + 1);
}
if (rc == 0) {
Format_ToHexascii(signatureString, signature, signatureLength);
fprintf(messageFile, "Signature with %s digest, length %u\n%s\n",
hashStr,
(uint)strlen(signatureString),
signatureString);
}
free(signatureString); /* @3 */
}
}
return rc;
}
#ifdef ADD_ECC
int SignSampleECCP521(unsigned char *keyToken,
size_t keyTokenLength,
unsigned char *testMessage,
size_t testMessageLength,
const char *outputAttachmentFileName,
FILE *projectLogFile)
{
int rc = 0;
int valid; /* true if signature verifies */
EccKeyTokenPublic eccKeyTokenPublic; /* CCA public key structure */
/*
digest to be signed
*/
unsigned char hash1[SHA1_SIZE]; /* SHA-1 digest */
unsigned char hash256[SHA256_SIZE]; /* SHA-256 digest */
unsigned char hash512[SHA512_SIZE]; /* SHA-512 digest */
unsigned long hashLength;
/*
signature
*/
unsigned char signature[132]; /* FIXME 132 according to CCA, openssl produces
139 */
unsigned long signatureLength;
unsigned long signatureBitLength;
/* extract the CCA public key from the CCA key token */
if (rc == 0) {
if (verbose) fprintf(messageFile, "SignSampleECCP521: key token length %u\n",
(uint)keyTokenLength);
if (verbose)
fprintf(messageFile, "SignSampleECCP521: extract the public key from CCA key token\n");
rc = getPKA96EccPublicKey(&eccKeyTokenPublic, /* output: structure */
keyTokenLength,
keyToken); /* input: PKA96 key token */
}
unsigned int i; /* loop through digest algorithms */
const char *hashStr; /* test, for trace */
unsigned char *hash; /* digest */
/* loop for SHA1, SHA256, SHA512 */
for (i = 0 ; (rc == 0) && (i < 3) ; i++) {
if (rc == 0) {
switch (i) {
case 0:
hashStr = "sha1";
hash = hash1;
hashLength = sizeof(hash1);
break;
case 1:
hashStr = "sha256";
hash = hash256;
hashLength = sizeof(hash256);
break;
case 2:
hashStr = "sha512";
hash = hash512;
hashLength = sizeof(hash512);
break;
}
/* hash the data for signing */
if (verbose) fprintf(messageFile, "SignSampleECCP521: Hashing input data with %s\n",
hashStr);
switch (i) {
case 0:
Ossl_SHA1(hash,
testMessageLength, testMessage,
0L, NULL);
break;
case 1:
Ossl_SHA256(hash,
testMessageLength, testMessage,
0L, NULL);
break;
case 2:
Ossl_SHA512(hash,
testMessageLength, testMessage,
0L, NULL);
break;
}
if (verbose) PrintAll(messageFile,
"SignSampleECCP521: hash to sign",
hashLength, hash);
}
/* sign with the coprocessor. The coprocessor doesn't know the digest algorithm. It just
signs a digest */
if (rc == 0) {
signatureLength = sizeof(signature);
rc = Digital_Signature_Generate_ECC(&signatureLength, /* i/o */
&signatureBitLength, /* output */
signature, /* output */
keyTokenLength, /* input */
keyToken, /* input */
hashLength, /* input */
hash); /* input */
}
/* sample - create the audit log entry */
if (rc == 0) {
if (verbose) fprintf(messageFile, "SignSampleECCP521: Updating audit log\n");
/* binary data as printable */
char pubkey_string[133 * 4];
char digest_string[SHA512_SIZE * 4]; /* use the largest */
char sig_string[132 * 4];
/* get the user and group structures */
/* binary to printable */
sprintAll(pubkey_string, eccKeyTokenPublic.qLen, eccKeyTokenPublic.publicKey);
sprintAll(digest_string, hashLength, hash);
sprintAll(sig_string, signatureLength, signature);
/* send to audit log */
fprintf(projectLogFile, "\tPublic Key:\n %s\n", pubkey_string);
fprintf(projectLogFile, "\tDigest:\n %s\n", digest_string);
fprintf(projectLogFile, "\tSignature:\n %s\n", sig_string);
}
/*
The verify functions should never fail. They are just sanity checks on the code.
*/
/* sanity check on the signature length */
if (rc == 0) {
if (signatureLength != sizeof(signature)) {
fprintf(messageFile, "ERROR1001: signature invalid length %lu\n", signatureLength);
rc = ERROR_CODE;
}
}
/* verify the signature with the coprocessor key CCA token */
if (rc == 0) {
if (verbose)
fprintf(messageFile,
"SignSampleECCP521: verify signature with the coprocessor key token\n");
rc = Digital_Signature_Verify_ECC(signatureLength, /* input */
signature, /* input signature */
keyTokenLength, /* input */
keyToken, /* input key */
hashLength, /* input */
hash); /* input hash */
}
/* sample code to verify the signature using openssl */
if (rc == 0) {
if (verbose) fprintf(messageFile,
"SignSampleECCP521: "
"verify signature with OpenSSL and the key token\n");
rc = Ossl_VerifyECC(&valid,
hash, /* input: digest to be verified */
hashLength,
eccKeyTokenPublic.publicKey,
eccKeyTokenPublic.qLen,
signature, /* input: signature */
signatureLength);
if (!valid) {
fprintf(messageFile,
"ERROR1024: "
"Error verifying signature with OpenSSL and the key token\n");
rc = ERROR_CODE;
}
}
/* write one signature to the output attachment */
if ((rc == 0) && (outputAttachmentFileName != NULL)) {
if (verbose) fprintf(messageFile, "SignSampleECCP521: Writing output file %s\n",
outputAttachmentFileName);
rc = File_WriteBinaryFile(signature, signatureLength, outputAttachmentFileName);
}
/* write the signature as hex ascii to the output body */
if (rc == 0) {
char *signatureString = NULL; /* freed @3 */
if (rc == 0) {
rc = Malloc_Safe((unsigned char **)&signatureString, /* freed @3 */
(signatureLength * 2) + 1,
(signatureLength * 2) + 1);
}
if (rc == 0) {
Format_ToHexascii(signatureString, signature, signatureLength);
fprintf(messageFile, "Signature with %s digest\n%s\n",
hashStr,
signatureString);
}
free(signatureString); /* @3 */
}
}
/* cleanup */
return rc;
}
#endif
/* EchoArgs() prints all incoming command line arguments, except it does not print the plaintext
password. */
int EchoArgs(int argc, char** argv)
{
int rc = 0;
int irc;
int i;
fprintf(messageFile, "framework_test argc = %u\n", argc);
for (i = 0 ; i < argc ; i++) {
fprintf(messageFile, "%u: %s\n", i, argv[i]);
irc = strcmp(argv[i], "-pwd"); /* if the argv is -pwd */
if (irc == 0) {
i++; /* skip the next argument */
}
}
return rc;
}
/* GetArgs() gets the command line arguments from the framework.
*/
int GetArgs(const char **outputBodyFilename,
const char **usr,
const char **password,
const char **projectLogFileName,
const char **projectAuxConfigFileName,
const char **sender,
const char **project,
const char **keyFileName,
const char **inputAttachmentFileName,
const char **outputAttachmentFileName,
int *verbose,
int argc,
char **argv)
{
long rc = 0;
int i;
FILE *tmpFile;
/* command line argument defaults */
*outputBodyFilename = NULL;
*verbose = FALSE;
/* get the command line arguments */
for (i = 1 ; (i < argc) && (rc == 0) ; i++) {
if (strcmp(argv[i],"-obody") == 0) {
i++;
if (i < argc) {
*outputBodyFilename = argv[i];
rc = File_Open(&tmpFile, *outputBodyFilename, "a");
/* switch messageFile from stdout ASAP so all messages get returned via email */
if (rc == 0) {
messageFile = tmpFile;
setvbuf(messageFile , 0, _IONBF, 0);
}
}
else {
fprintf(messageFile,
"ERROR1002: -obody option (output email body) needs a value\n");
rc = ERROR_CODE;
}
}
else if (strcmp(argv[i],"-usr") == 0) {
i++;
if (i < argc) {
*usr = argv[i];
}
else {
fprintf(messageFile,
"ERROR1003: -usr option (CCA user ID) needs a value\n");
rc = ERROR_CODE;
}
}
else if (strcmp(argv[i],"-core") == 0) {
int *p = NULL;
i = *p;
}
else if (strcmp(argv[i],"-pwd") == 0) {
i++;
if (i < argc) {
*password = argv[i];
}
else {
fprintf(messageFile,
"ERROR1004: -pwd option (CCA password) needs a value\n");
rc = ERROR_CODE;
}
}
else if (strcmp(argv[i],"-log") == 0) {
i++;
if (i < argc) {
*projectLogFileName = argv[i];
}
else {
fprintf(messageFile,
"ERROR1005: -log option (audit log file name) needs a value\n");
rc = ERROR_CODE;
}
}
else if (strcmp(argv[i],"-auxcfg") == 0) {
i++;
if (i < argc) {
*projectAuxConfigFileName = argv[i];
}
else {
fprintf(messageFile,
"ERROR1019: "
"-auxcfg option (auxiliary configuration file name) needs a value\n");
rc = ERROR_CODE;
}
}
else if (strcmp(argv[i],"-sender") == 0) {
i++;
if (i < argc) {
*sender = argv[i];
}
else {
fprintf(messageFile,
"ERROR1006: -sender option needs a value\n");
rc = ERROR_CODE;
}
}
else if (strcmp(argv[i],"-project") == 0) {
i++;
if (i < argc) {
*project = argv[i];
}
else {
fprintf(messageFile,
"ERROR1007: -project option needs a value\n");
rc = ERROR_CODE;
}
}
else if (strcmp(argv[i],"-key") == 0) {
i++;
if (i < argc) {
*keyFileName = argv[i];
}
else {
fprintf(messageFile,
"ERROR1008: -key option needs a value\n");
rc = ERROR_CODE;
}
}
else if (strcmp(argv[i],"-di") == 0) {
i++;
if (i < argc) {
*inputAttachmentFileName = argv[i];
}
else {
fprintf(messageFile,
"ERROR1016: -di option needs a value\n");
rc = ERROR_CODE;
}
}
else if (strcmp(argv[i],"-do") == 0) {
i++;
if (i < argc) {
*outputAttachmentFileName = argv[i];
}
else {
fprintf(messageFile,
"ERROR1009: -do option needs a value\n");
rc = ERROR_CODE;
}
}
/* this allows the framework to probe whether the project specific program can be called.
The program should do nothing except return success. */
else if (strcmp(argv[i],"-h") == 0) {
PrintUsage();
exit(0);
}
else if (strcmp(argv[i],"-v") == 0) {
*verbose = TRUE;
}
/* This code intentionally does not have an 'else error' clause. The framework can in
general add command line arguments that are ignored by the project specific program. */
}
/* verify mandatory command line arguments */
if (rc == 0) {
// If the usr isn't specified just use the sender
if (*usr == NULL) {
*usr = *sender;
}
if (*usr == NULL) {
fprintf(messageFile,
"ERROR1010: -usr option missing\n");
rc = ERROR_CODE;
}
}
if (rc == 0) {
if (*password == NULL) {
fprintf(messageFile,
"ERROR1018: -pwd option missing\n");
rc = ERROR_CODE;
}
}
if (rc == 0) {
if (*sender== NULL) {
fprintf(messageFile,
"ERROR1011: -sender option missing\n");
rc = ERROR_CODE;
}
}
if (rc == 0) {
if (*projectLogFileName == NULL) {
fprintf(messageFile,
"ERROR1012: -log option missing\n");
rc = ERROR_CODE;
}
}
#if 0 /* A program that needs auxiliary configuration data would use this test. It's commented
out here because the regression test tries both. */
if (rc == 0) {
if (*projectAuxConfigFileName == NULL) {
fprintf(messageFile,
"ERROR1020: -auxcfg option missing\n");
rc = ERROR_CODE;
}
}
#endif
if (rc == 0) {
if (*keyFileName == NULL) {
fprintf(messageFile,
"ERROR1014: -key option missing\n");
rc = ERROR_CODE;
}
}
if (rc == 0) {
if (*inputAttachmentFileName == NULL) {
fprintf(messageFile,
"ERROR1017: -di option missing\n");
rc = ERROR_CODE;
}
}
return rc;
}
/* CheckAuxArgs() checks that no command line arguments clash with those in the project auxiliary
configuration file
*/
int CheckAuxArgs(int argc, char** argv)
{
int rc = 0;
int irc;
size_t i;
int j;
/* screen out command line arguments that attempt to override the project auxiliary
configuration file values */
/* for each value in the project auxiliary configuration file */
for (i = 0 ; (rc == 0) && (i < (sizeof(claTable)/sizeof(char *))) ; i++) {
/* for each value from the command line */
for (j = 0 ; (rc == 0) && (j < argc) ; j++) {
irc = strcmp(claTable[i], argv[j]);
if (irc == 0) {
fprintf(messageFile, "ERROR1025: %s illegal in input body\n", argv[j]);
rc = ERROR_CODE;
}
}
}
return rc;
}
int GetAuxArgs(char **signAlgorithm, /* freed by caller */
const char *projectAuxConfigFileName)
{
int rc = 0;
char *lineBuffer = NULL; /* freed @2 */
size_t lineBufferLength = 4000; /* hard code for the project */
FILE *projectAuxConfigFile = NULL; /* closed @1 */
if (projectAuxConfigFileName != NULL) {
/* open project auxiliary configuration file */
if (rc == 0) {
if (verbose) fprintf(messageFile,
"Opening auxiliary configuration file %s\n",
projectAuxConfigFileName);
projectAuxConfigFile = fopen(projectAuxConfigFileName, "r"); /* closed @1 */
if (projectAuxConfigFile == NULL) {
fprintf(messageFile,
"ERROR1021: Cannot open auxiliary configuration file %s, %s\n",
projectAuxConfigFileName, strerror(errno));
rc = ERROR_CODE;
}
}
/* allocate a line buffer, used when parsing the configuration file */
if (rc == 0) {
rc = Malloc_Safe((unsigned char **)&lineBuffer, /* freed @2 */
lineBufferLength,
lineBufferLength); /* hard code for the project */
}
if (rc == 0) {
rc = File_MapNameToValue(signAlgorithm, /* freed by caller */
"sign_algorithm", /* name to search for */
lineBuffer, /* supplied buffer for lines */
lineBufferLength, /* size of the line buffer */
projectAuxConfigFile); /* input file stream */
}
if (rc == 0) {
if (verbose) fprintf(messageFile, "Signature algorithm: %s\n", *signAlgorithm);
}
if (projectAuxConfigFile != NULL) {
fclose(projectAuxConfigFile); /* @1 */
}
}
else { /* projectAuxConfigFileName == NULL, use default */
if (rc == 0) {
rc = Malloc_Safe((unsigned char **)signAlgorithm, /* freed by caller */
sizeof("rsa"),
sizeof("rsa")); /* hard code the default */
}
if (rc == 0) {
memcpy(*signAlgorithm, "rsa", sizeof("rsa"));
}
}
free(lineBuffer); /* @2 */
return rc;
}
void PrintUsage()
{
fprintf(messageFile, "\n");
fprintf(messageFile,
"\tframework_test usage:\n"
"\n"
"Common arguments:\n"
"\n"
"\t-usr - CCA user (profile) ID\n"
"\t[-core - cause the program to core dump]\n"
"\t[-v - verbose logging]\n"
"\t[-h - print usage help]\n"
"\n"
"Email only arguments:\n"
"\n"
"\t-project - project name\n"
"\t-epwd - CCA user password (encrypted)\n"
"\n"
"Command line only arguments:\n"
"\n"
"\t-obody - output email body file name (should be first argument)\n"
"\t-sender - email sender\n"
"\t-di - input attachment file name\n"
"\t-do - output attachment file name\n"
"\t-log - project audit log file name\n"
"\t-auxcfg - project auxiliary configuration file name\n"
"\t-key - project CCA signing key token\n"
"\t-pwd - CCA user password (plaintext)\n"
);
fprintf(messageFile, "\n");
return;
}
|