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