-
Notifications
You must be signed in to change notification settings - Fork 0
/
privacy.d
158 lines (129 loc) · 4.53 KB
/
privacy.d
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
module privacy;
struct ETPANPrivacy
{
string tmp_dir; /* working tmp directory */
ETPanMessage msg_ref; /* mailmessage => present or not - chash*/
chash * mmapstr; /* mmapstring => present or not present */
chash * mime_ref; /* mime => present or not */
carray * protocols;
int make_alternative;
}
mailprivacy *privacy;
this(string tmp,string certPath,string CAPath, string PrivateKeyPath, string[2] gnuID, string[2] smimeID)
{
privacy=mailprivacy_new(toStringz(tmp),1);
if (isNull(privacy))
throw new Exception("d_etpan:ETPANPrivacy unable to allocate memory");
throwOnError(mailprivacy_gnupg_init(privacy),MAIL_NO_ERROR);
throwOnError(mailprivacy_smime_init(privacy).MAIL_NO_ERROR); // not for pgp
mailprivacy_smime_set_cert_dir(privacy, toStringz(certPath)); // not pgp
mailprivacy_smime_set_CA_dir(privacy, toStringz(CAPath)); // not pgp
mailprivacy_smime_set_private_keys_dir(privacy, toStringz(PrivateKeyPath));
mailprivacy_gnupg_set_encryption_id(privacy, toStringz(gnuID[0]),toStringz(gnuID[1]));
mailprivacy_smime_set_encryption_id(privacy, toStringz(smimeID[0]),toStringz(smimeID[1]));
}
~this()
{
mailprivacy_free(privacy);
}
void decryptBuf(ubyte[] buf)
{
int col;
mailmime *mime;
mailmessage msg = data_message_init(cast(char*)buf, buf.length);
throwOnError(mailprivacy_msg_get_bodystructure(privacy, msg, &mime),MAIL_NO_ERROR);
mailmime_write(stdout, &col, mime);
clist * id_list;
clistiter * iter;
id_list = mailprivacy_gnupg_encryption_id_list(privacy, msg);
if (id_list != NULL) {
for(iter = clist_begin(id_list) ; iter != NULL ; iter = clist_next(iter)) {
char * str;
str = clist_content(iter);
fprintf(stderr, "%s\n", str);
}
}
scope(exit) // not quite right!
{
mailprivacy_gnupg_done(privacy);
mailmessage_free(msg);
}
mailprivacy_gnupg_encryption_id_list_clear(privacy, msg);
mailprivacy_smime_done(privacy);
auto Message=ETPANMessage(msg);
return Message;
}
// protocol = "smime";
// encryption_method="signed";
void encrypt(ubyte[] buf, string protocol, string encryption_method)
{
int r;
int res;
mailmime * mime;
mailmime * encrypted_mime;
mailmime * part_to_encrypt;
msg = data_message_init(cast(char*)buf,buf.length);
throwOnError(mailprivacy_msg_get_bodystructure(this.privacy, msg, &mime),MAIL_NO_ERROR);
part_to_encrypt = mime.mm_data.mm_message.mm_msg_mime;
throwOnError(mailprivacy_encrypt_msg(privacy, protocol, encryption_method, msg, part_to_encrypt, &encrypted_mime),MAIL_NO_ERROR);
mime.mm_data.mm_message.mm_msg_mime = encrypted_mime;
encrypted_mime.mm_parent = mime;
part_to_encrypt.mm_parent = NULL;
mailmime_free(part_to_encrypt);
if (r != MAIL_NO_ERROR) {
{
clist * id_list;
clistiter * iter;
id_list = mailprivacy_smime_encryption_id_list(privacy, msg);
if (id_list != NULL) {
for(iter = clist_begin(id_list) ; iter != NULL ; iter = clist_next(iter)) {
char * str;
str = clist_content(iter);
fprintf(stderr, "%s\n", str);
}
}
}
fprintf(stderr, "cannot encrypt\n");
goto free_content;
auto Message=ETPanMessage(msg);
scope(exit)
{
mailmessage_free(msg);
mailprivacy_gnupg_done(privacy);
mailprivacy_free(privacy);
mailprivacy_smime_done(privacy);
}
}
}
void encryptMessagePGP(ubyte[] content)
{
char * content;
size_t length;
mailmessage * msg;
int r;
mailprivacy * privacy;
int col;
char* protocol="pgp";
char* encryption_method="encrypted";
throwOnException(msg = data_message_init(content, content.length));
int res;
mailmime * mime;
mailmime * encrypted_mime;
mailmime * part_to_encrypt;
throwOnException(mailprivacy_msg_get_bodystructure(privacy, msg, &mime),MAIL_NO_ERROR);
part_to_encrypt = mime.mm_data.mm_message.mm_msg_mime;
throwOnException(mailprivacy_encrypt_msg(privacy, protocol, encryption_method, msg, part_to_encrypt, &encrypted_mime),MAIL_NO_ERROR);
mime.mm_data.mm_message.mm_msg_mime = encrypted_mime;
encrypted_mime.mm_parent = mime;
part_to_encrypt.mm_parent = NULL;
mailmime_free(part_to_encrypt);
col = 0;
mailmime_write(stdout, &col, msg.msg_mime);
scope(exit)
{
mailmessage_free(msg);
free(content);
mailprivacy_gnupg_done(privacy);
mailprivacy_free(privacy);
}
}