-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgplib5.c
469 lines (363 loc) · 9.68 KB
/
pgplib5.c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/* $Id$
*
* Copyright : (c) 1998 by Glenn Trigg. All Rights Reserved
* Project : Privtool
* File : pgplib5
*
* Author : Glenn Trigg
* Created : 23 Jan 1998
*
* Description : This implementation of the public functions in pgplib.c
* uses the PGP 5.5 SDK to perform the necessary encryption
* and key management functions.
*
* Note: Some of this code was taken from the test code of
* the PGP 5.5 SDK source. This is mainly due to the lack
* of programming documentation at this time.
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "def.h"
#include "buffers.h"
#include <pgp/pgpConfig.h>
#include <pgp/pgpEncode.h>
#include <pgp/pgpErrors.h>
#include <pgp/pgpUtilities.h>
/* Following are for storing messages from PGP or other programs */
static BUFFER error_messages;
static BUFFER stdout_messages;
static PGPContextRef pgpContext;
static PGPKeySetRef pgpKeyset;
static void clear_output(void);
static void add_to_error(byte *, int);
static void add_to_std(byte *, int);
/* BUF_SIZE is the general size of stack buffers. Should this ever be used
on DOS, you might want to reduce the value of BUF_SIZE, or replace the
stack use with a malloc(). */
#ifdef DOS
#define BUF_SIZE 512
#else
#define BUF_SIZE 4096
#endif
/*----------------------------------------------------------------------*/
void
init_pgplib()
{
/* Should this function really return if initialization fails? - ggt */
PGPError err = kPGPError_NoErr;
err = PGPsdkInit( );
if ( IsPGPError( err ) ) {
printf ("Initialization error: %d\n", err );
}
/* Create the pgpContext. */
err = PGPNewContext(kPGPsdkAPIVersion, &pgpContext);
if ( IsPGPError( err ) ) {
printf ("Error creating context: %d\n", err );
}
/* Open the default keyring pair. */
err = PGPOpenDefaultKeyRings(pgpContext, 0, &pgpKeyset);
if ( IsPGPError( err ) ) {
printf ("Error opening key rings: %d\n", err );
}
} /* init_pgplib */
void
close_pgplib()
{
PGPFreeKeySet(pgpKeyset);
PGPFreeContext(pgpContext);
PGPsdkCleanup();
} /* close_pgplib */
/*----------------------------------------------------------------------*/
int
encrypt_message(char **user, BUFFER *message, BUFFER *encrypted,
int flags, char *pass, char *key_name, byte *md5_pass)
{
/* TODO */
} /* encrypt_message */
int
decrypt_message(BUFFER *message, BUFFER *decrypted, BUFFER *signature,
char *pass, int flags, byte *md5_pass)
{
int err;
/* TODO */
err = PGPDecode(pgpContext,
PGPOInputBuffer(pgpContext, message->message,
message->length),
PGPOPassphrase(pgpContext, pass),
PGPOAllocatedOutputBuffer(pgpContext, &decrypted->message,
(PGPSize)(1024*1024),
&decrypted->length),
PGPOLastOption(0));
if ( IsPGPError( err ) ) {
printf ("Error decoding: %d\n", err );
return DEC_BAD_FILE;
}
return SIG_GOOD;
} /* decrypt_message */
/*----------------------------------------------------------------------*/
int
buffer_contains_key(BUFFER *b)
{
/* TODO */
} /* buffer_contains_key */
int
add_key(BUFFER *m)
{
/* TODO */
} /* add_key */
/*----------------------------------------------------------------------*/
/* Run a specified program with the appropriate arguments, and pass in
the specified data. If we're running PGP then we may also need to
give it a passphrase.
Not sure why this is in pgplib.c but it's copied straight from there to
pgplib5.c - ggt
*/
int
run_program(char *prog, byte *message, int msg_len,
char **args, char *pass, BUFFER *ret)
{
int fd_in[2],fd_err[2],fd_out[2],pass_fd[2];
int from_pgp,to_pgp,pgp_error,pass_in;
int child_pid;
fd_set r_fdset,w_fdset;
#ifndef SYSV
struct rusage rusage;
#endif
int statusp;
int fds_found;
char buf[BUF_SIZE];
int size;
#ifdef SYSV
struct timeval t, to;
/* We set the default timeout to 1 second */
t.tv_sec = 1;
t.tv_usec = 0;
#endif
/* Clear the buffers */
clear_output();
/* We need some pipes to communicate with the other
process. We only need an input pipe if we have data
to pass through to the program. */
if (message)
pipe (fd_in);
pipe (fd_err);
pipe (fd_out);
/* If we have a passphrase then we need a pipe for it */
if (pass)
pipe (pass_fd);
/* Fork and execute the program */
if (!(child_pid = vfork())) {
char pass_env[32];
/* Duplicate our pipe files on stdin/stdout/stderr */
close (0);
close (1);
close (2);
if (message)
dup2 (fd_in[0],0);
dup2 (fd_out[1],1);
dup2 (fd_err[1],2);
/* We should really close all file descriptors here,
but we shouldn't have any sensitive files open when
called. XView could be a problem, but everything
currently works. */
if (message)
close (fd_in[1]);
close (fd_out[0]);
close (fd_err[0]);
/* Point PGPPASSFD to the descriptor that we will be
writing the passphrase on, if neccesary. This is
more secure than putting the passphrase itself into
an environment variable (try ps -e). */
if (pass) {
close (pass_fd[1]);
sprintf (pass_env,"PGPPASSFD=%d\n",pass_fd[0]);
putenv (pass_env);
}
/* Exec and return an error if it fails */
if (execvp (prog, args)<0) {
_exit(-1);
}
exit(1);
}
/* If fork failed then return error */
if (child_pid < 0)
return -1;
/* Close the other ends of the pipes */
if (message)
close (fd_in[0]);
close (fd_out[1]);
close (fd_err[1]);
/* Make some aliases to save typing and clarify the code */
if (message)
to_pgp = fd_in[1];
else
to_pgp = (-1);
from_pgp = fd_out[0];
pgp_error = fd_err[0];
/* Same for the passphrase pipe */
if (pass) {
pass_in = pass_fd[1];
close (pass_fd[0]);
}
else
pass_in = (-1);
/* Now, we look around and check for input until the process
exits */
#if ! (defined(SYSV) || defined(LINUX))
while (!wait4(child_pid,&statusp,WNOHANG,&rusage))
#else
while (!waitpid (child_pid, &statusp, WNOHANG))
#endif
{
/* Set things up for the select call. Only include the
file descriptors which are still open. */
FD_ZERO(&r_fdset);
FD_SET(from_pgp, &r_fdset);
FD_SET(pgp_error, &r_fdset);
FD_ZERO(&w_fdset);
if (to_pgp >= 0) {
FD_SET(to_pgp,&w_fdset);
}
if (pass && pass_in >= 0) {
FD_SET (pass_in, &w_fdset);
}
/* See which descriptors are available */
#ifndef SYSV
fds_found = select(getdtablesize(),&r_fdset,&w_fdset,0,0);
#else
/* SYSV select changes the timeout value! */
to = t;
fds_found = select(FD_SETSIZE,&r_fdset,&w_fdset,0,&to);
#endif
/* Did we find any, or time out? */
if (fds_found > 0) {
/* First check for error messages and read a buffer
full */
if (FD_ISSET(pgp_error,&r_fdset)) {
size = read(pgp_error,buf,BUF_SIZE);
/* Save it for the calling code */
if (size > 0) {
add_to_error(buf,size);
}
}
/* Send the passphrase */
if (pass_in >= 0 && FD_ISSET (pass_in,&w_fdset)) {
if (*pass) {
size = write(pass_in,pass,strlen(pass));
pass += size;
}
/* If we sent the entire passphrase then
close the descriptor. */
if (!*pass) {
write (pass_in,"\n",1);
close (pass_in);
pass_in = (-1);
/* To be secure, we should destroy
the passphrase here */
#ifdef MORE_SECURE
destroy_passphrase(FALSE);
#endif
}
}
/* If we have data to send, then send some */
if (to_pgp >= 0 && FD_ISSET(to_pgp,&w_fdset)) {
size = write(to_pgp,message,msg_len);
if (size>0) {
message += size;
msg_len -= size;
}
/* Again, if we sent it all then close
the descriptor so the program gets
an EOF */
if (msg_len <= 0) {
close(to_pgp);
to_pgp = (-1);
}
}
/* Read some data from the program */
if (FD_ISSET(from_pgp,&r_fdset)) {
size = read(from_pgp,buf,BUF_SIZE);
/* And store it away for the caller */
if (size > 0) {
add_to_std(buf,size);
}
}
}
}
/* Just in case */
#ifdef MORE_SECURE
if (pass)
destroy_passphrase(FALSE);
#endif
/* Read remaining stdout data */
do {
size = read (from_pgp,buf,BUF_SIZE);
if (size > 0) {
add_to_std(buf, size);
}
} while (size > 0);
/* Close it */
close (from_pgp);
/* Read remaining error info */
do {
size = read (pgp_error,buf,BUF_SIZE);
if (size>0) {
add_to_error(buf, size);
}
} while (size > 0);
/* Close it */
close (pgp_error);
/* Close remaining open files */
if (to_pgp >= 0)
close (to_pgp);
if (pass_in >= 0)
close (pass_in);
/* Copy the stdout_message data into the ret buffer,
so that the caller can read it directly. Some GUI code calls
this function and doesn't have access to the static data
in this module */
if (ret) {
add_to_buffer (ret, stdout_messages.message,
stdout_messages.length);
}
/* Finally, return the exit code */
return statusp;
} /* run_program */
/*----------------------------------------------------------------------*/
/* Clear all the output buffers */
static void
clear_output(void)
{
clear_buffer(&error_messages);
clear_buffer(&stdout_messages);
}
/* Add a string to the error messages buffer */
static void
add_to_error(byte *buf, int len)
{
add_to_buffer(&error_messages, buf,len);
}
/* Add a string to the stdout buffer. */
static void
add_to_std(byte *buf, int len)
{
add_to_buffer(&stdout_messages, buf,len);
}
/*----------------------------------------------------------------------*/
void
update_random(void)
{
/* TODO */
}
void
get_md5(char *pass, byte *md5_val)
{
/* TODO */
}
void
reseed_random(void)
{
/* TODO */
}