/[pkg-mixmaster]/trunk/Mix/Src/mpgp.c
ViewVC logotype

Contents of /trunk/Mix/Src/mpgp.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 710 - (show annotations) (download)
Wed Jan 21 00:47:21 2004 UTC (9 years, 3 months ago) by rabbi
File MIME type: text/plain
File size: 5849 byte(s)
Updated copyright dates, doc issues, misc fixes.
1 /* mpgp version 0.3.0 -- (C) 2000 - 2004 Ulf Moeller and others.
2
3 mpgp 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 Test application for OpenPGP features
9 $Id: mpgp.c $ */
10
11
12 #include "mix3.h"
13 #include "pgp.h"
14 #include <stdlib.h>
15 #include <string.h>
16 #include <fcntl.h>
17 #include <assert.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <time.h>
21 #include <fcntl.h>
22 #ifdef POSIX
23 #include <unistd.h>
24 #include <termios.h>
25 #endif /* POSIX */
26
27 int pass(BUFFER *b)
28 {
29 char p[LINELEN];
30 int fd;
31 int n;
32
33 #ifdef HAVE_TERMIOS
34 struct termios attr;
35 #endif /* HAVE_TERMIOS */
36
37 fprintf(stderr, "enter passphrase: ");
38 fflush(stderr);
39 #ifdef HAVE_TERMIOS
40 fd = open("/dev/tty", O_RDONLY);
41 if (tcgetattr(fd, &attr) != 0)
42 return (-1);
43 attr.c_lflag &= ~ECHO;
44 attr.c_lflag |= ICANON;
45 if (tcsetattr(fd, TCSAFLUSH, &attr) != 0)
46 return (-1);
47 n = read(fd, p, LINELEN);
48
49 attr.c_lflag |= ECHO;
50 if (tcsetattr(fd, TCSAFLUSH, &attr) != 0)
51 return (-1);
52 close(fd);
53 p[n - 1] = 0;
54
55 #else /* end of HAVE_TERMIOS */
56 fgets(p, LINELEN, stdin);
57 if (p[strlen(p)-1]=='\n')
58 p[strlen(p)-1] = 0;
59 #endif /* else if not HAVE_TERMIOS */
60
61 fprintf(stderr, "\n");
62 buf_appends(b, p);
63 return (0);
64 }
65
66 void usage(char *n)
67 {
68 fprintf(stderr, "Usage: %s -e [-b] user@domain\n", n);
69 fprintf(stderr, " %s -s [-b] [yourname@domain]\n", n);
70 fprintf(stderr, " %s -c [-b]\n", n);
71 fprintf(stderr, " %s -C [-b]\n", n);
72 fprintf(stderr, " %s -d [passphrase]\n", n);
73 fprintf(stderr, " %s -g[r] yourname@domain [bits]\n", n);
74 fprintf(stderr, " %s -a[+-] [-b]\n\n", n);
75 fprintf(stderr, "PGP public key ring: %s\n", PGPPUBRING);
76 fprintf(stderr, "PGP secret key ring: %s\n", PGPSECRING);
77 }
78
79 int decrypt(BUFFER *u, BUFFER *option, char *n)
80 {
81 BUFFER *v;
82 BUFFER *sig;
83 int err = 0;
84
85 v = buf_new();
86 sig = buf_new();
87
88 buf_set(v, u);
89 err = pgp_decrypt(v, NULL, sig, PGPPUBRING, PGPSECRING);
90 if (err >= 0 || err == PGP_SIGBAD)
91 buf_move(u, v);
92
93 if (err == PGP_ERR) {
94 pass(option);
95 err = pgp_decrypt(u, option, sig, PGPPUBRING, PGPSECRING);
96 }
97 switch (err) {
98 case PGP_NOMSG:
99 fprintf(stderr, "%s: Not a PGP message.\n", n);
100 break;
101 case PGP_ERR:
102 fprintf(stderr, "%s: Can't read message.\n", n);
103 break;
104 case PGP_SIGOK:
105 fprintf(stderr, "%s: Valid signature: %s\n", n, sig->data);
106 err = 0;
107 break;
108 case PGP_SIGNKEY:
109 fprintf(stderr, "%s: Unknown signature key %s, cannot verify.\n", n, sig->data);
110 err = 1;
111 break;
112 case PGP_SIGBAD:
113 fprintf(stderr, "%s: Bad signature.\n", n);
114 err = 1;
115 break;
116 }
117
118 buf_free(v);
119 buf_free(sig);
120
121 return (err);
122 }
123
124 int main(int argc, char *argv[])
125 {
126 BUFFER *u, *option, *pp;
127 char *filename = NULL;
128 char *cmd = NULL;
129 int text = 1;
130 int err = 99;
131 int bits = 0;
132
133 mix_init(NULL);
134 VERBOSE = 3;
135
136 u = buf_new();
137 option = buf_new();
138 pp = buf_new();
139
140 if (argc > 1 && argv[1][0] == '-')
141 cmd = argv[1];
142
143 if (argc == 1 || (cmd > 0 && (cmd[1] == 'e' || cmd[1] == 'c' ||
144 cmd[1] == 'd' || cmd[1] == 'a' ||
145 cmd[1] == 's' || cmd[1] == 'C'))) {
146 if ((argc > 2 && (cmd == NULL || cmd[1] == 'a')) || argc > 3) {
147 FILE *f;
148
149 f = fopen(argv[argc - 1], "rb");
150 if (f == NULL) {
151 fprintf(stderr, "%s: Can't open %s\n", argv[0], argv[argc - 1]);
152 err = -1;
153 } else {
154 buf_read(u, f);
155 fclose(f);
156 filename = argv[argc - 1];
157 argc--;
158 }
159 } else
160 buf_read(u, stdin);
161 }
162 if (argc == 1)
163 err = decrypt(u, option, argv[0]);
164
165 if (argc > 2 && argv[2][0] == '-' && argv[2][1] == 'b') {
166 text = 0;
167 if (argc > 3)
168 buf_appends(option, argv[3]);
169 } else if (argc > 2)
170 buf_appends(option, argv[2]);
171
172 if (cmd)
173 switch (cmd[1]) {
174 case 's':
175 err = pgp_encrypt(PGP_SIGN | (text ? PGP_TEXT : 0), u, NULL, option,
176 NULL, PGPPUBRING, PGPSECRING);
177 if (err != 0) {
178 pass(pp);
179 err = pgp_encrypt(PGP_SIGN | (text ? PGP_TEXT : 0), u, NULL, option,
180 pp, PGPPUBRING, PGPSECRING);
181 }
182 if (err != 0)
183 fprintf(stderr, "Error.\n");
184 break;
185 case 'e':
186 if (option->length) {
187 err = pgp_encrypt(PGP_ENCRYPT | (text ? PGP_TEXT : 0), u, option, NULL,
188 NULL, PGPPUBRING, PGPSECRING);
189 if (err < 0)
190 fprintf(stderr, "%s: can't encrypt message for %s\n",
191 argv[0], argv[2]);
192 }
193 break;
194 case 'c':
195 pass(option);
196 err = pgp_encrypt(PGP_CONVENTIONAL | (text ? PGP_TEXT : 0), u, option,
197 NULL, NULL, PGPPUBRING, PGPSECRING);
198 if (err < 0)
199 fprintf(stderr, "%s: can't encrypt message\n", argv[0]);
200 break;
201 case 'C':
202 pass(option);
203 err = pgp_encrypt(PGP_NCONVENTIONAL | (text ? PGP_TEXT : 0), u, option,
204 NULL, NULL, PGPPUBRING, PGPSECRING);
205 if (err < 0)
206 fprintf(stderr, "%s: can't encrypt message\n", argv[0]);
207 break;
208 case 'g':
209 if (argc < 3) {
210 err = 99;
211 goto end;
212 }
213 pass(pp);
214 if (argc == 4)
215 sscanf(argv[3], "%d", &bits);
216 err = pgp_keygen(cmd[2] == 'r' ? PGP_ES_RSA : PGP_E_ELG,
217 bits, option, pp, PGPPUBRING, PGPSECRING, 0);
218 break;
219 case 'a':
220 switch (cmd[2]) {
221 case '-':
222 err = pgp_dearmor(u, u);
223 if (err == -1)
224 fprintf(stderr, "Not a PGP-armored message\n");
225 goto end;
226 case '+':
227 break;
228 default:
229 pgp_literal(u, filename, text);
230 pgp_compress(u);
231 break;
232 }
233 err = pgp_armor(u, PGP_ARMOR_NORMAL);
234 break;
235 case 'd':
236 err = decrypt(u, option, argv[0]);
237 break;
238 }
239 end:
240 if (err == 99)
241 usage(argv[0]);
242
243 if (err >= 0)
244 buf_write(u, stdout);
245
246 buf_free(option);
247 buf_free(pp);
248 buf_free(u);
249
250 mix_exit();
251 return (err == -1 ? 1 : err);
252 }

  ViewVC Help
Powered by ViewVC 1.1.5