24 #include <seclabel_api.h>
27 #include "ms_example.h"
30 #define PCT_OID "1.2.840.113549.1.9.16.1.6"
32 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:S:Y:Z4B:F:N:";
35 static char *default_recip =
"/CN=lppt1/OU=lppt/O=attlee/PRMD=TestPRMD/ADMD=TestADMD/C=GB/";
37 static char *content_id =
"030924.140212";
39 static const char text[] =
"First line\r\nSecond line\r\n";
41 static char *binary_data;
43 static int setup_msg_sec_env = 0;
46 int send_ia5_as_att = 0;
50 static void usage(
void) ;
58 static int submit_msg(
60 struct X400msSession *sp
63 static int setup_default_new_sec_env(
64 struct X400msSession *sp,
65 char *identity_filename,
69 static int setup_default_old_sec_env(
70 struct X400msSession *sp,
76 static int setup_msg_new_sec_env(
77 struct X400msMessage *mp,
78 char *identity_filename,
82 static int setup_msg_old_sec_env(
83 struct X400msMessage *mp,
89 static int add_sec_label(
90 struct X400msMessage *mp
94 static int add_content(
95 struct X400msMessage *mp
111 char password[BUFSIZ];
113 if (get_args(argc, argv, optstr)) {
118 printf(
"Connection type (0 = P7, 1 = P3 submit only, 2 = P3 both directions) [%d]: ", x400_contype);
119 contype = ic_fgetc(x400_contype, stdin);
121 ic_fgetc(x400_contype, stdin);
123 if ( contype <
'0' ||
'2' < contype )
124 contype = x400_contype;
129 def_oraddr = x400_ms_user_addr;
130 def_dn = x400_ms_user_dn;
131 def_pa = x400_ms_presentation_address;
133 def_oraddr = x400_mta_user_addr;
134 def_dn = x400_mta_user_dn;
135 def_pa = x400_mta_presentation_address;
138 printf(
"Your ORAddress [%s] > ", def_oraddr);
139 ic_fgets (orn,
sizeof orn, stdin);
141 if ( orn[strlen(orn)-1] ==
'\n' )
142 orn[strlen(orn)-1] =
'\0';
145 strcpy(orn, def_oraddr);
148 printf (
"Password [%s]: ",
149 contype == 0 ? x400_p7_password : x400_p3_password);
150 if ( ic_fgets (password,
sizeof password, stdin) == NULL )
153 if (password[strlen(password)-1] ==
'\n' )
154 password[strlen(password)-1] =
'\0';
155 if (password[0] ==
'\0')
156 strcpy(password, contype == 0 ? x400_p7_password : x400_p3_password);
159 printf(
"Presentation Address [%s] > ", def_pa);
160 ic_fgets (pa,
sizeof pa, stdin);
162 if ( pa[strlen(pa)-1] ==
'\n' )
163 pa[strlen(pa)-1] =
'\0';
168 printf(
"sending message using session 1\n");
169 if ((status = send_msg(contype, orn, def_dn, pa, password))
171 fprintf (stderr,
"Error in sending message\n");
193 struct X400msSession *sp;
199 status =
X400msOpen (contype, orn, def_dn, password, pa, NULL, &sp);
201 fprintf (stderr,
"Error in Open: %s\n",
X400msError (status));
202 fprintf (stderr,
"%s %s %s\n", orn, def_dn, pa);
221 if (use_new_sec_env) {
222 status = setup_default_new_sec_env(sp, identity_filename, passphrase);
224 status = setup_default_old_sec_env(sp, security_id, identity_dn,
229 fprintf (stderr,
"Can't setup security environment\n");
233 printf(
"sending message 1\n");
234 status = submit_msg(orn, sp);
236 fprintf (stderr,
"Can't submit\n");
242 fprintf (stderr,
"X400msClose returned error: %s\n",
X400msError (status));
248 static int submit_msg(
250 struct X400msSession *sp
253 struct X400msMessage *mp;
254 struct X400Recipient *rp;
260 if (x400_default_recipient != NULL)
261 recip = x400_default_recipient;
263 recip = default_recip;
265 printf(
"Message recipient [%s]: ", recip);
266 ic_fgets (tmp,
sizeof tmp, stdin);
268 if ( tmp[strlen(tmp)-1] ==
'\n' )
269 tmp[strlen(tmp)-1] =
'\0';
270 if (strlen(tmp) != 0)
273 printf(
"Subject [%s]: ", subject);
274 ic_fgets (tmp,
sizeof tmp, stdin);
276 if ( tmp[strlen(tmp)-1] ==
'\n' )
277 tmp[strlen(tmp)-1] =
'\0';
278 if (strlen(tmp) != 0)
279 subject = strdup(tmp);
283 fprintf (stderr,
"x400msMsgNew returned error: %s\n",
X400msError (status));
287 printf(
"\n\nPreparing Message\n");
290 if (x400_default_external_content_type != NULL) {
291 printf(
"Sending external content type %s\n",
292 x400_default_external_content_type);
294 x400_default_external_content_type, -1);
296 printf(
"Sending content type %d\n", x400_default_content_type);
298 x400_default_content_type);
301 fprintf (stderr,
"X400msMsgAddIntParam returned error: %s\n",
319 if (setup_msg_sec_env) {
320 if (use_new_sec_env) {
321 status = setup_msg_new_sec_env(mp, identity_filename, passphrase);
323 fprintf (stderr,
"Can't setup new security environment\n");
327 status = setup_msg_old_sec_env(mp, security_id, identity_dn2,
330 fprintf (stderr,
"Can't setup old security environment\n");
345 fprintf (stderr,
"x400msMsgAddIntParam returned error: %s\n",
359 printf(
"\nRequesting 4406 signature\n");
362 fprintf (stderr,
"x400msMsgAddIntParam returned error: %s\n",
369 printf(
"Sending external content type %s\n", PCT_OID);
371 printf(
"\nNot requesting 4406 signature\n");
374 status = add_sec_label (mp);
376 fprintf (stderr,
"Failed to add 4406 Security Label: %s\n",
383 fprintf (stderr,
"x400msRecipNew returned error: %s\n",
X400msError (status));
390 fprintf (stderr,
"x400msRecipAddIntParam returned error: %s\n",
397 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
X400msError (status));
403 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
X400msError (status));
409 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
X400msError (status));
415 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
X400msError (status));
422 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
X400msError (status));
429 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
X400msError (status));
436 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
X400msError (status));
444 char tmp_buffer[255];
446 snprintf(tmp_buffer, 244,
"%s '%s' '%.19s'",
447 subject, get_x400_pty_str_from_4406(x400_default_priority), ctime(&t));
448 printf(
"Subject is '%s'\n", tmp_buffer);
451 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
458 if (flot_string != NULL) {
461 fprintf (stderr,
"x400ms returned error: %s\n",
465 printf(
"Sent IA5 FLOT:\n%s\n", flot_string);
469 fprintf (stderr,
"add_content() returned error: %s\n",
476 fprintf (stderr,
"x400ms returned error: %s\n",
X400msError (status));
479 printf(
"Sent 8859 content:\n%s\n", text);
484 printf(
"failed to add X400_T_IA5TEXT BP\n");
487 printf(
"Sent IA5 content:\n%s\n", text);
490 if (filename_to_send != NULL) {
491 binary_data = (
char *) malloc(100000);
492 if ( binary_data == NULL )
494 fp = fopen(filename_to_send,
"r");
495 if (fp == (FILE *)NULL) {
496 printf(
"Cannot open binary file\n");
499 if ((fs = fread (binary_data,
sizeof(
char), 100000/
sizeof(
char), fp) ) == -1) {
500 printf(
"Cannot read from binary file\n");
507 printf(
"failed to add X400_T_BINARY BP\n");
511 printf(
"no binary file set - not sending X400_T_BINARY\n");
516 fprintf (stderr,
"x400msMsgSend returned error: %s\n",
X400msError (status));
519 printf(
"Message submitted successfully\n");
524 fprintf (stderr,
"x400msMsgDelete returned error: %s\n",
X400msError (status));
535 static int setup_default_new_sec_env(
536 struct X400msSession *sp,
547 fprintf (stderr,
"X400msSetStrDefault returned error: %s\n",
555 fprintf (stderr,
"X400msSetStrDefault returned error: %s\n",
563 fprintf (stderr,
"X400msTestSecurityEnv returned error: %s\n",
568 printf (
"X400msTestSecurityEnv returned success\n");
573 static int setup_default_old_sec_env(
574 struct X400msSession *sp,
591 fprintf (stderr,
"X400msSetStrDefault returned error: %s\n",
599 fprintf (stderr,
"X400msSetStrDefault returned error: %s\n",
607 static int setup_msg_new_sec_env(
608 struct X400msMessage *mp,
619 fprintf (stderr,
"X400msMsgAddStrParam returned error: %s\n",
627 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
634 static int setup_msg_old_sec_env(
635 struct X400msMessage *mp,
649 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
657 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
665 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
672 static int add_sec_label(
673 struct X400msMessage *mp
676 #define XML_BUFSIZE 1024
677 #define STRING_BUFSIZE 1024
679 const char* xml_filename =
"seclabel.xml";
680 char xml_content[XML_BUFSIZE];
681 char xml_content_inserted[XML_BUFSIZE];
682 char str_content[STRING_BUFSIZE];
683 int str_len = STRING_BUFSIZE;
688 fd = fopen(xml_filename,
"r");
690 fprintf(stderr,
"Failed to open %s : %s\n",
691 xml_filename,strerror(errno));
695 fread(&xml_content,XML_BUFSIZE,1,fd);
699 status = SecLabelInit(
"Example program");
700 if (status != SECLABEL_E_NOERROR) {
701 fprintf(stderr,
"SecLabelInit returned error %d\n", status);
706 status = SecLabelParse(xml_content,
711 if (status != SECLABEL_E_NOERROR) {
712 fprintf(stderr,
"SecLabelParse returned error %d\n", status);
717 if ((gen_4406_sig) && (send_4406_label)) {
721 str_content,str_len);
723 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
729 if (send_x411_label) {
733 str_content,str_len);
735 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
740 printf(
"\nNot requesting X.411 Label\n");
744 (void) SecLabelPrint((
const unsigned char *)str_content,
746 xml_content_inserted,
750 if (send_x411_label) {
751 printf(
"Added this X.411 security label:%s\n", xml_content);
753 printf(
"Not adding X.411 security label\n");
755 if ((gen_4406_sig) && (send_4406_label)) {
756 printf(
"Added this 4406 security label:%s\n", xml_content_inserted);
758 printf(
"Not adding 4406 security label\n");
764 static int add_content(
765 struct X400msMessage *mp
789 fprintf (stderr,
"x400ms returned error: %s\n",
X400msError (status));
792 printf(
"Sent 8859 attachment as string:\n%s\n", text);
794 if (send_ia5_as_att) {
799 printf(
"failed to add X400_T_IA5TEXT attachment\n");
802 printf(
"Sent IA5 as first attachment:\n%s\n", text);
807 printf(
"failed to add X400_T_IA5TEXT BP\n");
810 printf(
"Sent IA5 as string in message \n%s\n", text);
816 printf(
"failed to add X400_T_ISO8859_1, attachment\n");
819 printf(
"Sent 8859-1 as attachment:\n%s\n", text);
822 if (filename_to_send != NULL) {
823 binary_data = (
char *) malloc(100000);
824 if ( binary_data == NULL )
826 fp = fopen(filename_to_send,
"r");
827 if (fp == (FILE *)NULL) {
828 printf(
"Cannot open binary file\n");
831 if ((fs = fread (binary_data,
sizeof(
char), 100000/
sizeof(
char), fp))
833 printf(
"Cannot read from binary file\n");
840 printf(
"failed to add X400_T_BINARY BP\n");
847 printf(
"no binary file set - not sending X400_T_BINARY\n");
852 static void usage(
void) {
853 printf(
"usage: %s\n", optstr);
854 printf(
"\t where:\n");
855 printf(
"\t -u : Don't prompt to override defaults \n");
856 printf(
"\t -3 : Use P3 connection \n");
857 printf(
"\t -7 : Use P7 connection \n");
858 printf(
"\t -m : OR Address in P7 bind arg \n");
859 printf(
"\t -d : DN in P7 bind arg \n");
860 printf(
"\t -p : Presentation Address of P7 Store \n");
861 printf(
"\t -w : P7 password of P7 user \n");
862 printf(
"\t -M : OR Address in P3 bind arg \n");
863 printf(
"\t -D : DN in P3 bind arg \n");
864 printf(
"\t -P : Presentation Address of P3 server\n");
865 printf(
"\t -W : P3 password of P3 user \n");
866 printf(
"\t -o : Originator \n");
867 printf(
"\t -O : Originator Return Address \n");
868 printf(
"\t -r : Recipient\n");
869 printf(
"\t -l : Logline\n");
870 printf(
"\t -y : Military Priority \n");
871 printf(
"\t\t 0 - deferred, 1 - routine, 2 - priority \n");
872 printf(
"\t\t 3 - immediate, 4 - flash, 5 - override \n");
873 printf(
"\t -C : Content Type (2/22/772/OID) \n");
874 printf(
"\t -i : Implicit conversion prohibited = TRUE \n");
875 printf(
"\t -a : Alternate Recipient Prohibited = TRUE \n");
876 printf(
"\t -q : Content Return Request = TRUE \n");
877 printf(
"\t -s : Disclosure of Recipient = FALSE \n");
878 printf(
"\t -A : Recipient Reassignment Prohibited = FALSE \n");
879 printf(
"\t -v : Conversion with Loss Prohibited = FALSE \n");
880 printf(
"\t -e : Security Environment (dir with x509 subdir): obsolete, use -Y <p12file>\n");
881 printf(
"\t -x : DN of X.509 Digital Identity\n");
882 printf(
"\t -b : Passphrase for private key in PKCS12 file\n");
883 printf(
"\t -f : Filename to transfer as binary bp\n");
884 printf(
"\t -Y : Filename of PKCS12 file containing Digital Identity\n");
885 printf(
"\t -Z : Generate MOAC Signature\n");
886 printf(
"\t -4 : Generate STANAG 4406 PCT Signatures\n");
887 printf(
"\t -B : Subject of message\n");
888 printf(
"\t -F : String to insert in first line of text (e.g. as FLOT)\n");
889 printf(
"\t -N : Insert Security Label in (e)nvelope or (4)406 signature\n");