32 #include <seclabel_api.h>
34 #include "ms_example.h"
46 #define FIRST_SEQ_NUM 2
49 int seq_num_val_1 = FIRST_SEQ_NUM;
50 int seq_num_val_2 = FIRST_SEQ_NUM + 100;
54 int send_ia5_as_att = 0;
60 static char *optstr =
"u37m:d:p:w:M:D:P:W:r:o:O:r:g:G:c:l:R:y:C:iaqsAve:x:b:f:Y:S:j:";
63 static char *default_recip =
"/CN=P7User1/OU=sales/O=attlee/PRMD=TestPRMD/ADMD=TestADMD/C=GB/";
66 static char *content_id =
"030924.140212";
67 static const char text[] =
"First line\r\nSecond line\r\n";
69 int adding_sec_label = 1;
73 static void usage(
void) ;
81 static int submit_msg(
83 struct X400msSession *sp
86 static int setup_default_new_sec_env(
87 struct X400msSession *sp,
88 char *identity_filename,
93 static int setup_default_old_sec_env(
94 struct X400msSession *sp,
101 static int setup_msg_new_sec_env(
102 struct X400msMessage *mp,
103 char *identity_filename,
108 static int setup_msg_old_sec_env(
109 struct X400msMessage *mp,
115 static int setup_recip_new_sec_env(
116 struct X400Recipient *rp,
117 char *identity_filename,
121 static int setup_recip_old_sec_env(
122 struct X400Recipient *rp,
131 static int add_sec_label(
132 struct X400msMessage *mp
135 static int add_recip(
136 struct X400msMessage *mp,
140 static int add_content(
141 struct X400msMessage *mp
144 static int add_binary_bp(
145 struct X400msMessage *mp
149 struct X400msMessage *mp
152 static int add_ia5_str(
153 struct X400msMessage *mp
169 char password[BUFSIZ];
171 if (get_args(argc, argv, optstr)) {
176 printf(
"Connection type (0 = P7, 1 = P3 submit only, 2 = P3 both directions) [%d]: ", x400_contype);
177 contype = ic_fgetc(x400_contype, stdin);
179 ic_fgetc(x400_contype, stdin);
181 if ( contype <
'0' ||
'2' < contype )
182 contype = x400_contype;
187 def_oraddr = x400_ms_user_addr;
188 def_dn = x400_ms_user_dn;
189 def_pa = x400_ms_presentation_address;
191 def_oraddr = x400_mta_user_addr;
192 def_dn = x400_mta_user_dn;
193 def_pa = x400_mta_presentation_address;
196 printf(
"Your ORAddress [%s] > ", def_oraddr);
197 ic_fgets (orn,
sizeof orn, stdin);
199 if ( orn[strlen(orn)-1] ==
'\n' )
200 orn[strlen(orn)-1] =
'\0';
203 strcpy(orn, def_oraddr);
206 printf (
"Password [%s]: ",
207 contype == 0 ? x400_p7_password : x400_p3_password);
208 if ( ic_fgets (password,
sizeof password, stdin) == NULL )
211 if (password[strlen(password)-1] ==
'\n' )
212 password[strlen(password)-1] =
'\0';
213 if (password[0] ==
'\0')
214 strcpy(password, contype == 0 ? x400_p7_password : x400_p3_password);
217 printf(
"Presentation Address [%s] > ", def_pa);
218 ic_fgets (pa,
sizeof pa, stdin);
220 if ( pa[strlen(pa)-1] ==
'\n' )
221 pa[strlen(pa)-1] =
'\0';
226 printf(
"sending message using session 1\n");
227 if ((status = send_msg(contype, orn, def_dn, pa, password))
229 fprintf (stderr,
"Error in sending message\n");
234 printf(
"sending message using session 2\n");
235 if ((status = send_msg(contype, orn, def_dn, pa, password))
237 fprintf (stderr,
"Error sending message\n");
253 struct X400msSession *sp;
260 status =
X400msOpen (contype, orn, def_dn, password, pa, &nummsg, &sp);
262 fprintf (stderr,
"Error in Open: %s\n",
X400msError (status));
263 fprintf (stderr,
"%s %s %s\n", orn, def_dn, pa);
284 if (use_new_sec_env) {
285 status = setup_default_new_sec_env(sp, identity_filename, passphrase);
287 status = setup_default_old_sec_env(sp, security_id,
288 identity_dn, passphrase);
292 fprintf (stderr,
"Can't setup security environment\n");
296 printf(
"sending message 1\n");
297 status = submit_msg(orn, sp);
299 fprintf (stderr,
"Can't submit\n");
319 fprintf (stderr,
"X400msClose returned error: %s\n",
X400msError (status));
325 static int submit_msg(
327 struct X400msSession *sp
330 struct X400msMessage *mp;
332 struct X400Recipient *rp2;
335 static int msg_num = 0;
340 if (x400_default_recipient != NULL) {
341 recip = x400_default_recipient;
342 recip2 = x400_default_recipient;
344 recip = default_recip;
345 recip2 = default_recip;
348 printf(
"Message recipient [%s]: ", recip);
349 ic_fgets (tmp,
sizeof tmp, stdin);
351 if ( tmp[strlen(tmp)-1] ==
'\n' )
352 tmp[strlen(tmp)-1] =
'\0';
353 if (strlen(tmp) != 0) {
355 recip2 = strdup(tmp);
358 printf(
"Subject [%s]: ", subject);
359 ic_fgets (tmp,
sizeof tmp, stdin);
361 if ( tmp[strlen(tmp)-1] ==
'\n' )
362 tmp[strlen(tmp)-1] =
'\0';
363 if (strlen(tmp) != 0)
364 subject = strdup(tmp);
369 fprintf (stderr,
"x400msMsgNew returned error: %s\n",
374 #ifdef use_diff_sec_env_second_msg
378 status = setup_msg_sec_env(mp,
"/root",
379 "cn=P7User1,o=Address Book,c=GB",
"secret");
381 fprintf (stderr,
"setup_msg_sec_env returned error: %s\n",
388 #define S4404_ENCRYPT
396 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
400 fprintf (stderr,
"encrypting message as S4406 (PCT) \n");
412 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
416 fprintf (stderr,
"Adding MOAC\n");
424 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
430 "CN=originator;c=gb", -1);
432 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
437 if (adding_sec_label) {
438 status = add_sec_label (mp);
440 fprintf (stderr,
"Failed to add Security Label: %s\n",
444 fprintf (stderr,
"added Security Label\n");
450 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
467 printf(
"military message priority is %d (%s) \n",
468 x400_default_priority,
469 get_x400_pty_str_from_4406(x400_default_priority));
471 get_x400_pty_from_4406(x400_default_priority));
475 printf(
"military message priority qualifer is %d ( 0 - low, 1 - high)\n",
476 get_x400_pty_qual_from_4406(x400_default_priority));
478 get_x400_pty_qual_from_4406(x400_default_priority));
485 char tmp_buffer[255];
487 snprintf(tmp_buffer, 244,
"%s '%s' '%.19s'",
488 subject, get_x400_pty_str_from_4406(x400_default_priority), ctime(&t));
489 printf(
"Subject is '%s'\n", tmp_buffer);
492 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
502 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
X400msError (status));
507 fprintf (stderr,
"add_recip() returned error: %s\n",
523 fprintf (stderr,
"add_content() returned error: %s\n",
530 fprintf (stderr,
"x400msMsgSend returned error: %s\n",
534 printf(
"Message submitted successfully\n");
539 fprintf (stderr,
"x400msMsgDelete returned error: %s\n",
549 static int add_recip(
550 struct X400msMessage *mp,
555 struct X400Recipient *rp;
560 fprintf (stderr,
"x400msRecipNew returned error: %s\n",
568 fprintf (stderr,
"x400msRecipAddIntParam returned error: %s\n",
576 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
582 "CN=recipient;c=gb", -1);
584 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
592 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
600 fprintf (stderr,
"X400msRecipAddIntParam returned error: %s\n",
606 fprintf (stderr,
"requesting token\n");
609 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
619 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
623 fprintf (stderr,
"Added sequence number %d for token\n", seq_num_val_1);
634 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
642 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
650 static int setup_default_new_sec_env(
651 struct X400msSession *sp,
658 fprintf (stderr,
"using new_sec_nv %s\n", idf);
665 fprintf (stderr,
"X400msSetStrDefault returned error: %s\n",
673 fprintf (stderr,
"X400msSetStrDefault returned error: %s\n",
681 fprintf (stderr,
"X400msTestSecurityEnv returned error: %s\n",
690 static int setup_default_old_sec_env(
691 struct X400msSession *sp,
699 fprintf (stderr,
"using old_sec_nv\n");
709 fprintf (stderr,
"X400msSetStrDefault returned error: %s\n",
717 fprintf (stderr,
"X400msSetStrDefault returned error: %s\n",
725 static int setup_msg_new_sec_env(
726 struct X400msMessage *mp,
727 char *identity_filename,
737 identity_filename, -1);
739 fprintf (stderr,
"X400msMsgAddStrParam returned error: %s\n",
744 printf(
"setting up new message security env %s\n", identity_filename);
749 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
756 static int setup_msg_old_sec_env(
757 struct X400msMessage *mp,
765 printf(
"setting up message security env %s\n",
id);
772 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
780 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
785 printf(
"setting up new message security env %s\n", identity_filename);
790 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
797 static int setup_recip_new_sec_env(
798 struct X400Recipient *rp,
799 char *identity_filename,
805 printf(
"setting up recipient security env %s\n", identity_filename);
811 identity_filename, -1);
813 fprintf (stderr,
"X400msRecipAddStrParam returned error: %s\n",
821 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
830 static int setup_recip_old_sec_env(
831 struct X400Recipient *rp,
839 printf(
"setting up recipient security env %s\n",
id);
846 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
853 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
861 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
870 static int add_sec_label(
871 struct X400msMessage *mp
874 #define XML_BUFSIZE 1024
875 #define STRING_BUFSIZE 1024
877 const char* xml_filename =
"seclabel.xml";
878 char xml_content[XML_BUFSIZE];
879 char str_content[STRING_BUFSIZE];
880 int str_len = STRING_BUFSIZE;
885 fd = fopen(xml_filename,
"r");
887 fprintf(stderr,
"Failed to open %s : %s\n",
888 xml_filename,strerror(errno));
892 fread(&xml_content,XML_BUFSIZE,1,fd);
896 status = SecLabelInit(
"Example program");
897 if (status != SECLABEL_E_NOERROR) {
898 fprintf(stderr,
"SecLabelInit returned error %d\n", status);
903 status = SecLabelParse(xml_content,
908 if (status != SECLABEL_E_NOERROR) {
909 fprintf(stderr,
"SecLabelParse returned error %d\n", status);
915 str_content,str_len);
917 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
924 static int add_content(
925 struct X400msMessage *mp
950 fprintf (stderr,
"x400ms returned error: %s\n",
X400msError (status));
953 printf(
"Sent 8859 attachment as string:\n%s\n", text);
955 if (send_ia5_as_att) {
960 printf(
"failed to add X400_T_IA5TEXT attachment\n");
963 printf(
"Sent IA5 as first attachment:\n%s\n", text);
966 if (num_of_ia5_bytes == 0) {
969 printf(
"failed to add X400_T_IA5TEXT BP\n");
972 printf(
"Sent IA5 as string in message \n%s\n", text);
974 status = add_ia5_str(mp);
976 printf(
"failed to add IA5 str\n");
985 printf(
"failed to add X400_T_ISO8859_1, attachment\n");
988 printf(
"Sent 8859-1 as attachment:\n%s\n", text);
991 status = add_binary_bp(mp);
993 printf(
"failed to add X400_T_BINARY BP\n");
998 status = add_ftbp(mp);
1000 printf(
"failed to add X400_T_BINARY BP\n");
1006 if (filename_to_send != NULL) {
1007 binary_data = (
char *) malloc(100000);
1008 if ( binary_data == NULL )
1010 fp = fopen(filename_to_send,
"r");
1011 if (fp == (FILE *)NULL) {
1012 printf(
"Cannot open binary file\n");
1015 if ((fs = fread (binary_data,
sizeof(
char), 100000/
sizeof(
char), fp))
1017 printf(
"Cannot read from binary file\n");
1024 printf(
"failed to add X400_T_BINARY BP\n");
1030 printf(
"failed to add X400_T_IA5TEXT BP\n");
1033 printf(
"Sent IA5 as string in message \n%s\n", text);
1038 printf(
"no binary file set - not sending X400_T_BINARY\n");
1044 static int add_ia5_str(
1045 struct X400msMessage *mp
1051 char *sample_ia5 = {
"Here is some sample text to put into the IA5 attachment."};
1053 if ((tmp = malloc(num_of_ia5_bytes + 1)) == NULL) {
1054 printf(
"failed to malloc %d bytes\n", num_of_ia5_bytes);
1058 for (i = strlen(text) ; i < num_of_ia5_bytes ; i++)
1059 tmp[i] = sample_ia5[i%strlen(sample_ia5)];
1064 printf(
"failed to add X400_T_IA5TEXT BP\n");
1067 printf(
"Sent %d bytes IA5 as string in message \n%s\n", i, tmp);
1072 static int add_binary_bp(
1073 struct X400msMessage *mp
1082 if (filename_to_send != NULL) {
1083 fp = fopen(filename_to_send,
"rb");
1084 if (fp == (FILE *)NULL) {
1085 printf(
"Cannot open binary file\n");
1088 binary_data = (
char *) malloc(bin_bp_size);
1089 if ( binary_data == NULL )
1091 if ((fs = fread (binary_data,
sizeof(
char),
1092 bin_bp_size/
sizeof(
char), fp) ) == -1) {
1093 printf(
"Cannot read from binary file\n");
1099 if (fs < bin_bp_size) {
1100 printf(
"Cannot read %d bytes from binary file (got %d)\n",
1109 printf(
"failed to add X400_T_BINARY BP\n");
1112 printf(
"Sent %d bytes as X400_T_BINARY BP\n", fs);
1114 printf(
"no binary file set - not sending X400_T_BINARY\n");
1120 static int add_ftbp(
1121 struct X400msMessage *mp
1128 struct X400Bodypart *bp;
1131 if (filename_to_send != NULL) {
1132 fp = fopen(filename_to_send,
"rb");
1133 if (fp == (FILE *)NULL) {
1134 printf(
"Cannot open binary file\n");
1137 binary_data = (
char *) malloc(bin_bp_size);
1138 if ( binary_data == NULL )
1140 if ((fs = fread (binary_data,
sizeof(
char),
1141 bin_bp_size/
sizeof(
char), fp) ) == -1) {
1142 printf(
"Cannot read from binary file\n");
1148 if (fs < bin_bp_size) {
1149 printf(
"Cannot read %d bytes from binary file (got %d)\n",
1158 "Test FTBP File", -1);
1163 "20160801060101.0Z", -1);
1165 "20160801060202.0Z", -1);
1171 printf(
"failed to add X400_T_FTBP BP\n");
1174 printf(
"Sent %d bytes as X400_T_FTBP BP\n", fs);
1176 printf(
"no binary file set - not sending X400_T_FTBP\n");
1182 static void usage(
void) {
1183 printf(
"usage: %s\n", optstr);
1184 printf(
"\t where:\n");
1185 printf(
"\t -u : Don't prompt to override defaults \n");
1186 printf(
"\t -3 : Use P3 connection \n");
1187 printf(
"\t -7 : Use P7 connection \n");
1188 printf(
"\t -m : OR Address in P7 bind arg \n");
1189 printf(
"\t -d : DN in P7 bind arg \n");
1190 printf(
"\t -p : Presentation Address of P7 Store \n");
1191 printf(
"\t -w : P7 password of P7 user \n");
1192 printf(
"\t -M : OR Address in P3 bind arg \n");
1193 printf(
"\t -D : DN in P3 bind arg \n");
1194 printf(
"\t -P : Presentation Address of P3 server\n");
1195 printf(
"\t -W : P3 password of P3 user \n");
1196 printf(
"\t -o : Originator \n");
1197 printf(
"\t -O : Originator Return Address \n");
1198 printf(
"\t -r : Recipient\n");
1199 printf(
"\t -l : Logline\n");
1200 printf(
"\t -R : Report requests\n");
1201 printf(
"\t\t : 0 - none, 1 - non-delivery, 2 - delivery (implies 1)\n");
1202 printf(
"\t -y : Military Priority \n");
1203 printf(
"\t\t 0 - deferred, 1 - routine, 2 - priority \n");
1204 printf(
"\t\t 3 - immediate, 4 - flash, 5 - override \n");
1205 printf(
"\t -C : Content Type (2/22/772/OID) \n");
1206 printf(
"\t -i : Implicit conversion prohibited = TRUE \n");
1207 printf(
"\t -a : Alternate Recipient Prohibited = TRUE \n");
1208 printf(
"\t -q : Content Return Request = TRUE \n");
1209 printf(
"\t -s : Disclosure of Recipient = FALSE \n");
1210 printf(
"\t -A : Recipient Reassignment Prohibited = FALSE \n");
1211 printf(
"\t -v : Conversion with Loss Prohibited = FALSE \n");
1212 printf(
"\t -e : Security Environment (dir with x509 subdir): obsolete, use -Y <p12file>\n");
1213 printf(
"\t -x : DN of X.509 Digital Identity\n");
1214 printf(
"\t -b : Passphrase for private key in PKCS12 file\n");
1215 printf(
"\t -f : Filename to transfer as binary bp\n");
1216 printf(
"\t -Y : Filename of PKCS12 file containing Digital Identity\n");
1217 printf(
"\t -S : Number of bytes to put into Binary BP and FTBP (default 100)\n");
1218 printf(
"\t -j : Number of bytes to put into IA5 BP (default 100)\n");