1 #ifdef HAVE_CONFIG_H
2 #	include <config.h>
3 #endif
4 
5 #include <libetpan/libetpan.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <sys/types.h>
10 #include <errno.h>
11 #ifdef WIN32
12 #	include "win_etpan.h"
13 #else
14 #	include <sys/mman.h>
15 #endif
16 #include <stdlib.h>
17 #include <string.h>
18 
get_content_of_file(char * filename,char ** p_content,size_t * p_length)19 int get_content_of_file(char * filename, char ** p_content, size_t * p_length)
20 {
21   int r;
22   struct stat stat_buf;
23   int fd;
24   char * content;
25   int nread;
26 
27   r = stat(filename, &stat_buf);
28   if (r < 0)
29     goto err;
30 
31   content = malloc(stat_buf.st_size + 1);
32   if (content == NULL)
33     goto err;
34 
35   fd = open(filename, O_RDONLY);
36   if (fd < 0)
37     goto free;
38 
39   nread = 0;
40   do
41     {
42       r = read (fd, content + nread, stat_buf.st_size - nread);
43       if (r > 0)
44 	nread += r;
45     }
46   while (nread < stat_buf.st_size && (r > 0 || (r < 0 && errno == EAGAIN)));
47 
48   if (r < 0)
49     goto close;
50 
51   content[stat_buf.st_size] = '\0';
52   close(fd);
53 
54   * p_content = content;
55   * p_length = stat_buf.st_size;
56 
57   return 0;
58 
59  close:
60   close(fd);
61  free:
62   free(content);
63  err:
64   return -1;
65 }
66 
mailmessage_encrypt(struct mailprivacy * privacy,struct mailmessage * msg,char * protocol,char * encryption_method)67 int mailmessage_encrypt(struct mailprivacy * privacy,
68     struct mailmessage * msg,
69     char * protocol, char * encryption_method)
70 {
71   int r;
72   int res;
73   struct mailmime * mime;
74   struct mailmime * encrypted_mime;
75   struct mailmime * part_to_encrypt;
76 
77   r = mailprivacy_msg_get_bodystructure(privacy, msg, &mime);
78   if (r != MAIL_NO_ERROR) {
79     res = r;
80     goto err;
81   }
82 
83   part_to_encrypt = mime->mm_data.mm_message.mm_msg_mime;
84 
85   r = mailprivacy_encrypt_msg(privacy, protocol, encryption_method,
86       msg, part_to_encrypt, &encrypted_mime);
87   if (r != MAIL_NO_ERROR) {
88     res = r;
89     goto err;
90   }
91 
92   mime->mm_data.mm_message.mm_msg_mime = encrypted_mime;
93   encrypted_mime->mm_parent = mime;
94   part_to_encrypt->mm_parent = NULL;
95   mailmime_free(part_to_encrypt);
96 
97   return MAIL_NO_ERROR;
98 
99  err:
100   return res;
101 }
102 
main(int argc,char ** argv)103 int main(int argc, char ** argv)
104 {
105   char * content;
106   size_t length;
107   mailmessage * msg;
108   int r;
109   struct mailprivacy * privacy;
110   int col;
111 
112   privacy = mailprivacy_new("/Users/hoa/tmp", 1);
113   if (privacy == NULL) {
114     goto err;
115   }
116 
117   r = mailprivacy_smime_init(privacy);
118   if (r != MAIL_NO_ERROR) {
119     goto free_privacy;
120   }
121 
122   mailprivacy_smime_set_cert_dir(privacy,
123       "/Users/hoa/LibEtPan/libetpan/tests/keys/cert");
124 
125   mailprivacy_smime_set_CA_dir(privacy,
126       "/Users/hoa/LibEtPan/libetpan/tests/keys/ca");
127 
128   mailprivacy_smime_set_private_keys_dir(privacy,
129       "/Users/hoa/LibEtPan/libetpan/tests/keys/private");
130 
131   mailprivacy_smime_set_encryption_id(privacy, "dinh.vh@euro.apple.com", "coin");
132 
133   if (argc < 2) {
134     fprintf(stderr, "syntax: pgp [message]\n");
135     goto done_gpg;
136   }
137 
138   r = get_content_of_file(argv[1], &content, &length);
139   if (r < 0) {
140     fprintf(stderr, "file not found %s\n", argv[1]);
141     goto done_gpg;
142   }
143 
144   msg = data_message_init(content, length);
145   if (msg == NULL) {
146     fprintf(stderr, "unexpected error\n");
147     goto free_content;
148   }
149 
150   r = mailmessage_encrypt(privacy, msg, "smime", "signed");
151   if (r != MAIL_NO_ERROR) {
152     {
153       clist * id_list;
154       clistiter * iter;
155 
156       id_list = mailprivacy_smime_encryption_id_list(privacy, msg);
157       if (id_list != NULL) {
158         for(iter = clist_begin(id_list) ; iter != NULL ; iter = clist_next(iter)) {
159           char * str;
160 
161           str = clist_content(iter);
162           fprintf(stderr, "%s\n", str);
163         }
164       }
165     }
166 
167     fprintf(stderr, "cannot encrypt\n");
168     goto free_content;
169   }
170 
171   col = 0;
172   mailmime_write(stdout, &col, msg->msg_mime);
173 
174   mailmessage_free(msg);
175 
176   free(content);
177   mailprivacy_gnupg_done(privacy);
178   mailprivacy_free(privacy);
179 
180   exit(EXIT_SUCCESS);
181 
182  free_content:
183   free(content);
184  done_gpg:
185   mailprivacy_smime_done(privacy);
186  free_privacy:
187   mailprivacy_free(privacy);
188  err:
189   exit(EXIT_FAILURE);
190 }
191