/[pkg-mixmaster]/branches/mixmaster_2_9_STABLE/Mix/Src/chain1.c
ViewVC logotype

Contents of /branches/mixmaster_2_9_STABLE/Mix/Src/chain1.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 525 - (hide annotations) (download)
Sun Jun 1 23:39:11 2003 UTC (9 years, 11 months ago) by rabbi
File MIME type: text/plain
File size: 8130 byte(s)
Windows buld support changes.
1 rabbi 418 /* Mixmaster version 2.9 -- (C) 1999 - 2002 Anonymizer Inc. and others.
2 rabbi 1
3     Mixmaster may be redistributed and modified under certain conditions.
4     This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
5     ANY KIND, either express or implied. See the file COPYRIGHT for
6     details.
7    
8     Encrypt message for Cypherpunk remailer chain
9 rabbi 525 $Id: chain1.c,v 1.2.2.3 2003/06/01 23:39:09 rabbi Exp $ */
10 rabbi 1
11 rabbi 525 #include "mix.h"
12 rabbi 1 #include "mix3.h"
13 rabbi 525 #ifdef USE_PGP
14     # include "pgp.h"
15     #endif
16 rabbi 1 #include <string.h>
17     #include <ctype.h>
18    
19     #define N(X) (isdigit(X) ? (X)-'0' : 0)
20    
21     int t1_rlist(REMAILER remailer[])
22     {
23     FILE *list;
24     int i, listed = 0;
25     int n = 0;
26     char line[2 * LINELEN], l2[LINELEN], name[LINELEN], *flags;
27    
28     list = mix_openfile(TYPE1LIST, "r");
29     if (list == NULL)
30     return (-1);
31     while (fgets(line, sizeof(line), list) != NULL && n < MAXREM) {
32     if (strleft(line, "$remailer") &&
33     strchr(line, '<') && strchr(line, '>') &&
34     strchr(line, '{') && strchr(line, '{') + 4 < strchr(line, '}')) {
35     if (line[strlen(line) - 1] == '\n')
36     line[strlen(line) - 1] = '\0';
37     if (line[strlen(line) - 1] == '\r')
38     line[strlen(line) - 1] = '\0';
39     while (line[strlen(line) - 1] == ' ')
40     line[strlen(line) - 1] = '\0';
41     if (line[strlen(line) - 1] != ';'
42     && fgets(l2, sizeof(l2), list) != NULL)
43     strcatn(line, l2, LINELEN);
44     flags = strchr(line, '>');
45     strncpy(name, strchr(line, '{') + 2,
46     strchr(line, '}') - strchr(line, '{') - 3);
47     name[strchr(line, '}') - strchr(line, '{') - 3] = '\0';
48     name[20] = '\0';
49    
50     for (i = 1; i <= n; i++)
51     if (streq(name, remailer[i].name))
52     break;
53     if (i > n) {
54     /* not in mix list */
55     n++;
56     strcpy(remailer[i].name, name);
57     strncpy(remailer[i].addr, strchr(line, '<') + 1,
58     strchr(line, '>') - strchr(line, '<'));
59     remailer[i].addr[strchr(line, '>') - strchr(line, '<') - 1]
60     = '\0';
61     remailer[i].flags.mix = 0;
62     remailer[i].flags.post = strifind(flags, " post");
63     }
64     remailer[i].flags.cpunk = strfind(flags, " cpunk");
65     remailer[i].flags.pgp = strfind(flags, " pgp");
66     remailer[i].flags.pgponly = strfind(flags, " pgponly");
67     remailer[i].flags.latent = strfind(flags, " latent");
68     remailer[i].flags.middle = strfind(flags, " middle");
69     remailer[i].flags.ek = strfind(flags, " ek");
70     remailer[i].flags.esub = strfind(flags, " esub");
71     remailer[i].flags.newnym = strfind(flags, " newnym");
72     remailer[i].flags.nym = strfind(flags, " nym");
73     remailer[i].info[1].reliability = 0;
74     remailer[i].info[1].latency = 0;
75     remailer[i].info[1].history[0] = '\0';
76     }
77     if (strleft(line,
78     "-----------------------------------------------------------------------"))
79     break;
80     }
81     n++; /* ?? */
82     while (fgets(line, sizeof(line), list) != NULL) {
83     if (strlen(line) >= 72 && strlen(line) <= 73)
84     for (i = 1; i < n; i++)
85     if (strleft(line, remailer[i].name) &&
86     line[strlen(remailer[i].name)] == ' ') {
87     strncpy(remailer[i].info[1].history, line + 42, 12);
88     remailer[i].info[1].history[12] = '\0';
89     remailer[i].info[1].reliability = 10000 * N(line[64])
90     + 1000 * N(line[65]) + 100 * N(line[66])
91     + 10 * N(line[68]) + N(line[69]);
92     remailer[i].info[1].latency = 36000 * N(line[55])
93     + 3600 * N(line[56]) + 600 * N(line[58])
94     + 60 * N(line[59]) + 10 * N(line[61])
95     + N(line[62]);
96     listed++;
97     }
98     }
99     fclose(list);
100     if (listed < 4) /* we have no valid reliability info */
101     for (i = 1; i < n; i++)
102     remailer[i].info[1].reliability = 10000;
103    
104     #ifdef USE_PGP
105     pgp_rlist(remailer, n);
106 weaselp 327 #endif /* USE_PGP */
107 rabbi 1 return (n);
108     }
109    
110     int t1_ek(BUFFER *key, BUFFER *seed, int num)
111     {
112     buf_reset(key);
113     buf_appendc(key, (byte) num);
114     buf_cat(key, seed);
115     digest_md5(key, key);
116     encode(key, 0);
117     #ifdef DEBUG
118     fprintf(stderr, "passphrase=%s (%2X%2X%2X%2X %d)\n", key->data,
119     seed->data[0], seed->data[1], seed->data[2], seed->data[3], num);
120 weaselp 327 #endif /* DEBUG */
121 rabbi 1 return (0);
122     }
123    
124     int t1_encrypt(int type, BUFFER *message, char *chainstr, int latency,
125     BUFFER *ek, BUFFER *feedback)
126     {
127     BUFFER *b, *rem, *dest, *line, *field, *content;
128     REMAILER remailer[MAXREM];
129     int maxrem, chainlen = 0;
130     int chain[20];
131     int hop;
132     int hashmark = 0;
133     int err = 0;
134    
135     b = buf_new();
136     rem = buf_new();
137     dest = buf_new();
138     line = buf_new();
139     field = buf_new();
140     content = buf_new();
141    
142     maxrem = t1_rlist(remailer);
143     if (maxrem < 1) {
144     clienterr(feedback, "No remailer list!");
145     err = -1;
146     goto end;
147     }
148     chainlen = chain_select(chain, chainstr, maxrem, remailer, 1, line);
149     if (chainlen < 1) {
150     if (line->length)
151     clienterr(feedback, line->data);
152     else
153     clienterr(feedback, "Invalid remailer chain!");
154     err = -1;
155     goto end;
156     }
157     if (chain[0] == 0)
158     chain[0] = chain_randfinal(type, remailer, maxrem, 1);
159    
160     if (chain[0] == -1) {
161     clienterr(feedback, "Invalid remailer chain!");
162     err = -1;
163     goto end;
164     }
165     if (chain_rand(remailer, maxrem, chain, chainlen, 1) == -1) {
166     clienterr(feedback, "No reliable remailers!");
167     err = -1;
168     goto end;
169     }
170     while (buf_getheader(message, field, content) == 0) {
171     hdr_encode(content, 0);
172     if (type == MSG_POST && bufieq(field, "newsgroups") &&
173     remailer[chain[0]].flags.post) {
174     buf_appendf(dest, "Anon-Post-To: %b\n", content);
175     } else if (type == MSG_MAIL && bufieq(field, "to")) {
176     buf_appendf(dest, "Anon-To: %b\n", content);
177     } else {
178     /* paste header */
179     if (type == MSG_POST && bufieq(field, "newsgroups"))
180     buf_appendf(dest, "Anon-To: %s\n", MAILtoNEWS);
181     if (hashmark == 0) {
182     buf_appends(b, "##\n");
183     hashmark = 1;
184     }
185     buf_appendheader(b, field, content);
186     }
187     }
188     buf_nl(b);
189     buf_rest(b, message);
190     buf_move(message, b);
191    
192     if (type != MSG_NULL && dest->length == 0) {
193     clienterr(feedback, "No destination address!");
194     err = -1;
195     goto end;
196     }
197     if (type == MSG_NULL) {
198     buf_sets(dest, "Null:\n");
199     }
200     for (hop = 0; hop < chainlen; hop++) {
201     if (hop == 0) {
202     buf_sets(b, "::\n");
203     buf_cat(b, dest);
204     } else {
205     buf_sets(b, "::\nAnon-To: ");
206     buf_appends(b, remailer[chain[hop - 1]].addr);
207     buf_nl(b);
208     }
209     if (remailer[chain[hop]].flags.latent && latency > 0)
210     buf_appendf(b, "Latent-Time: +%d:00r\n", latency);
211     if (ek && remailer[chain[hop]].flags.ek) {
212     t1_ek(line, ek, hop);
213     buf_appendf(b, "Encrypt-Key: %b\n", line);
214     }
215     buf_nl(b);
216     buf_cat(b, message);
217     #ifdef USE_PGP
218     if (remailer[chain[hop]].flags.pgp) {
219     buf_clear(message);
220     buf_clear(rem);
221     buf_setf(rem, "<%s>", remailer[chain[hop]].addr);
222     err = pgp_encrypt(PGP_ENCRYPT | PGP_REMAIL | PGP_TEXT, b, rem,
223     NULL, NULL, NULL, NULL);
224     if (err < 0) {
225     buf_setf(line, "No PGP key for remailer %s!\n",
226     remailer[chain[hop]].name);
227     clienterr(feedback, line->data);
228     goto end;
229     }
230     buf_appends(message, "::\nEncrypted: PGP\n\n");
231     buf_cat(message, b);
232     } else
233 weaselp 327 #endif /* USE_PGP */
234 rabbi 1 {
235     if (remailer[chain[hop]].flags.pgponly) {
236     buf_setf(line, "PGP encryption needed for remailer %s!\n",
237     remailer[chain[hop]].name);
238     clienterr(feedback, line->data);
239     goto end;
240     }
241     buf_move(message, b);
242     }
243     if (ek && remailer[chain[hop]].flags.ek)
244     buf_appends(message, "\n**\n");
245     }
246     buf_clear(b);
247     if (chainlen == 0) {
248     buf_appends(b, "::\n");
249     buf_cat(b, dest);
250     } else {
251 rabbi 40 buf_appendf(b, "%s: %s\n", ek ? "::\nAnon-To" : "To",
252 rabbi 1 remailer[chain[chainlen - 1]].addr);
253     }
254     buf_nl(b);
255     buf_cat(b, message);
256     buf_move(message, b);
257     end:
258     buf_free(b);
259     buf_free(rem);
260     buf_free(dest);
261     buf_free(line);
262     buf_free(field);
263     buf_free(content);
264     return (err);
265     }
266    
267     #ifdef USE_PGP
268     int t1_getreply(BUFFER *msg, BUFFER *ek, int len)
269     {
270     BUFFER *key, *decrypt;
271     int err = -1;
272     int hop = 0;
273    
274     key = buf_new();
275     decrypt = buf_new();
276    
277     do {
278     t1_ek(key, ek, hop);
279     buf_set(decrypt, msg);
280     if (pgp_decrypt(decrypt, key, NULL, NULL, NULL) == 0
281     && decrypt->data != NULL)
282     err = 0, buf_move(msg, decrypt);
283     }
284     while (hop++ < len);
285     return (err);
286     }
287    
288 weaselp 327 #endif /* USE_PGP */

  ViewVC Help
Powered by ViewVC 1.1.5