| 6 |
details. |
details. |
| 7 |
|
|
| 8 |
Key management |
Key management |
| 9 |
$Id: keymgt.c,v 1.11 2002/08/28 08:13:59 weaselp Exp $ */ |
$Id: keymgt.c,v 1.12 2002/09/05 01:21:54 weaselp Exp $ */ |
| 10 |
|
|
| 11 |
|
|
| 12 |
#include "mix3.h" |
#include "mix3.h" |
| 13 |
#include <string.h> |
#include <string.h> |
| 14 |
|
#include <time.h> |
| 15 |
#include <assert.h> |
#include <assert.h> |
| 16 |
|
|
| 17 |
int getv2seckey(byte keyid[], BUFFER *key); |
int getv2seckey(byte keyid[], BUFFER *key); |
| 55 |
char idstr[33]; |
char idstr[33]; |
| 56 |
char line[LINELEN]; |
char line[LINELEN]; |
| 57 |
int err = -1; |
int err = -1; |
| 58 |
|
char *res; |
| 59 |
|
time_t created, expires; |
| 60 |
|
|
| 61 |
pass = buf_new(); |
pass = buf_new(); |
| 62 |
iv = buf_new(); |
iv = buf_new(); |
| 74 |
if (fgets(line, sizeof(line), keyring) == NULL) |
if (fgets(line, sizeof(line), keyring) == NULL) |
| 75 |
break; |
break; |
| 76 |
if (strleft(line, begin_key)) { |
if (strleft(line, begin_key)) { |
| 77 |
if (fgets(line, sizeof(line), keyring) == NULL) |
expires = 0; |
| 78 |
|
created = 0; |
| 79 |
|
do { |
| 80 |
|
res = fgets(line, sizeof(line), keyring); |
| 81 |
|
if (strileft(line, "created:")) { |
| 82 |
|
created = parse_yearmonthday(strchr(line, ':')+1); |
| 83 |
|
if (created == -1) |
| 84 |
|
created = 0; |
| 85 |
|
} else if (strileft(line, "expires:")) { |
| 86 |
|
expires = parse_yearmonthday(strchr(line, ':')+1); |
| 87 |
|
if (expires == -1) |
| 88 |
|
expires = 0; |
| 89 |
|
} |
| 90 |
|
/* Fetch lines until we fail or get a non-header line */ |
| 91 |
|
} while ( res != NULL && strchr(line, ':') != NULL ); |
| 92 |
|
if (res == NULL) |
| 93 |
break; |
break; |
| 94 |
if (keyid && !streq(line, idstr)) |
if (keyid && !streq(line, idstr)) |
| 95 |
continue; |
continue; |
| 96 |
|
if (created != 0 && (created > time(NULL))) { |
| 97 |
|
errlog(ERRORMSG, "Key is not valid yet (create date in the future): %s", idstr); |
| 98 |
|
break; |
| 99 |
|
} |
| 100 |
|
if (expires != 0 && (expires + KEYGRACEPERIOD < time(NULL))) { |
| 101 |
|
errlog(ERRORMSG, "Key is expired: %s", idstr); |
| 102 |
|
break; |
| 103 |
|
} |
| 104 |
fgets(line, sizeof(line), keyring); |
fgets(line, sizeof(line), keyring); |
| 105 |
fgets(line, sizeof(line), keyring); |
fgets(line, sizeof(line), keyring); |
| 106 |
buf_sets(iv, line); |
buf_sets(iv, line); |
| 271 |
|
|
| 272 |
#ifdef USE_RSA |
#ifdef USE_RSA |
| 273 |
int v2keymgt(int force) |
int v2keymgt(int force) |
| 274 |
|
/* |
| 275 |
|
* Mixmaster v2 Key Management |
| 276 |
|
* |
| 277 |
|
* This function triggers creation of mix keys (see parameter force) which are |
| 278 |
|
* stored in secring.mix. One public mix key is also written to key.txt. This |
| 279 |
|
* is the key with the latest expire date (keys with no expiration date are |
| 280 |
|
* always considered newer if they appear later in the secret mix file - key |
| 281 |
|
* creation appends keys). |
| 282 |
|
* |
| 283 |
|
* force: |
| 284 |
|
* 0, 1: create key when necessary: |
| 285 |
|
* - no key exists as of yet |
| 286 |
|
* - old keys are due to expire/already expired |
| 287 |
|
* 2: always create a new mix key. |
| 288 |
|
* |
| 289 |
|
* (force = 0 is used in mix_daily, and before remailer-key replies) |
| 290 |
|
* (force = 1 is used by mixmaster -K) |
| 291 |
|
* (force = 2 is used by mixmaster -G) |
| 292 |
|
*/ |
| 293 |
{ |
{ |
|
/* scan secring, write the pubkey. function will be rewritten |
|
|
for advanced key management in v3 */ |
|
|
|
|
| 294 |
FILE *keyring, *f; |
FILE *keyring, *f; |
| 295 |
char line[LINELEN]; |
char line[LINELEN]; |
| 296 |
byte k1[16]; |
byte k1[16], k1_found[16]; |
| 297 |
BUFFER *b, *temp, *iv, *pass, *pk; |
BUFFER *b, *temp, *iv, *pass, *pk, *pk_found; |
| 298 |
int err = 0; |
int err = 0; |
| 299 |
int found = 0; |
int found, foundnonexpiring; |
| 300 |
|
time_t created, expires, created_found, expires_found; |
| 301 |
|
char *res; |
| 302 |
|
|
| 303 |
b = buf_new(); |
b = buf_new(); |
| 304 |
temp = buf_new(); |
temp = buf_new(); |
| 305 |
iv = buf_new(); |
iv = buf_new(); |
| 306 |
pass = buf_new(); |
pass = buf_new(); |
| 307 |
pk = buf_new(); |
pk = buf_new(); |
| 308 |
|
pk_found = buf_new(); |
| 309 |
|
|
| 310 |
if (force == 2) |
foundnonexpiring = 0; |
| 311 |
v2createkey(); |
for (;;) { |
| 312 |
else { |
found = 0; |
| 313 |
if ((f = mix_openfile(SECRING, "r")) == NULL) |
created_found = 0; |
| 314 |
v2createkey(); |
expires_found = 0; |
|
else |
|
|
fclose(f); |
|
|
} |
|
| 315 |
|
|
| 316 |
if (force == 0 && (f = mix_openfile(KEYFILE, "r")) != NULL) { |
keyring = mix_openfile(SECRING, "r"); |
| 317 |
fclose(f); |
if (keyring != NULL) { |
| 318 |
goto end; |
for (;;) { |
|
} |
|
|
keyring = mix_openfile(SECRING, "r"); |
|
|
if (keyring != NULL) { |
|
|
for (;;) { |
|
|
if (fgets(line, sizeof(line), keyring) == NULL) |
|
|
break; |
|
|
if (strleft(line, begin_key)) { |
|
|
if (fgets(line, sizeof(line), keyring) == NULL) |
|
|
break; |
|
|
id_decode(line, k1); |
|
|
fgets(line, sizeof(line), keyring); |
|
| 319 |
if (fgets(line, sizeof(line), keyring) == NULL) |
if (fgets(line, sizeof(line), keyring) == NULL) |
| 320 |
break; |
break; |
| 321 |
buf_sets(iv, line); |
if (strleft(line, begin_key)) { |
| 322 |
decode(iv, iv); |
expires = 0; |
| 323 |
buf_reset(b); |
created = 0; |
| 324 |
for (;;) { |
do { |
| 325 |
|
res = fgets(line, sizeof(line), keyring); |
| 326 |
|
if (strileft(line, "created:")) { |
| 327 |
|
created = parse_yearmonthday(strchr(line, ':')+1); |
| 328 |
|
if (created == -1) |
| 329 |
|
created = 0; |
| 330 |
|
} else if (strileft(line, "expires:")) { |
| 331 |
|
expires = parse_yearmonthday(strchr(line, ':')+1); |
| 332 |
|
if (expires == -1) |
| 333 |
|
expires = 0; |
| 334 |
|
} |
| 335 |
|
/* Fetch lines until we fail or get a non-header line */ |
| 336 |
|
} while ( res != NULL && strchr(line, ':') != NULL ); |
| 337 |
|
if (res == NULL) |
| 338 |
|
break; |
| 339 |
|
if (((created != 0) && (created > time(NULL))) || |
| 340 |
|
((expires != 0) && (expires < time(NULL)))) { |
| 341 |
|
/* Key already is expired or has creation date in the future */ |
| 342 |
|
continue; |
| 343 |
|
} |
| 344 |
|
id_decode(line, k1); |
| 345 |
|
fgets(line, sizeof(line), keyring); |
| 346 |
if (fgets(line, sizeof(line), keyring) == NULL) |
if (fgets(line, sizeof(line), keyring) == NULL) |
| 347 |
break; |
break; |
| 348 |
if (strleft(line, end_key)) |
buf_sets(iv, line); |
| 349 |
|
decode(iv, iv); |
| 350 |
|
buf_reset(b); |
| 351 |
|
for (;;) { |
| 352 |
|
if (fgets(line, sizeof(line), keyring) == NULL) |
| 353 |
|
break; |
| 354 |
|
if (strleft(line, end_key)) |
| 355 |
|
break; |
| 356 |
|
buf_append(b, line, strlen(line) - 1); |
| 357 |
|
} |
| 358 |
|
if (decode(b, b) == -1) |
| 359 |
break; |
break; |
| 360 |
buf_append(b, line, strlen(line) - 1); |
buf_sets(temp, PASSPHRASE); |
| 361 |
|
digest_md5(temp, pass); |
| 362 |
|
buf_crypt(b, pass, iv, DECRYPT); |
| 363 |
|
buf_clear(pk); |
| 364 |
|
if (seckeytopub(pk, b, k1) == 0) { |
| 365 |
|
found = 1; |
| 366 |
|
if (expires == 0 || (expires - KEYOVERLAPPERIOD >= time(NULL))) |
| 367 |
|
foundnonexpiring = 1; |
| 368 |
|
if (expires == 0 || (expires_found < expires)) { |
| 369 |
|
buf_clear(pk_found); |
| 370 |
|
buf_cat(pk_found, pk); |
| 371 |
|
memcpy(&k1_found, &k1, sizeof(k1)); |
| 372 |
|
expires_found = expires; |
| 373 |
|
created_found = created; |
| 374 |
|
} |
| 375 |
|
} |
| 376 |
} |
} |
|
if (decode(b, b) == -1) |
|
|
break; |
|
|
buf_sets(temp, PASSPHRASE); |
|
|
digest_md5(temp, pass); |
|
|
buf_crypt(b, pass, iv, DECRYPT); |
|
|
if (seckeytopub(pk, b, k1) == 0) |
|
|
found = 1; |
|
|
break; |
|
| 377 |
} |
} |
| 378 |
|
fclose(keyring); |
| 379 |
} |
} |
| 380 |
fclose(keyring); |
|
| 381 |
} |
if (!foundnonexpiring || (force == 2)) { |
| 382 |
|
v2createkey(); |
| 383 |
|
foundnonexpiring = 1; |
| 384 |
|
force = 1; |
| 385 |
|
} else |
| 386 |
|
break; |
| 387 |
|
}; |
| 388 |
|
|
| 389 |
if (found) { |
if (found) { |
|
id_encode(k1, line); |
|
| 390 |
if ((f = mix_openfile(KEYFILE, "w")) != NULL) { |
if ((f = mix_openfile(KEYFILE, "w")) != NULL) { |
| 391 |
fprintf(f, "%s %s %s %s %s%s\n", SHORTNAME, |
id_encode(k1_found, line); |
| 392 |
|
fprintf(f, "%s %s %s %s %s%s", SHORTNAME, |
| 393 |
REMAILERADDR, line, VERSION, |
REMAILERADDR, line, VERSION, |
| 394 |
MIDDLEMAN ? "M" : "", |
MIDDLEMAN ? "M" : "", |
| 395 |
NEWS[0] == '\0' ? "C" : (strchr(NEWS, '@') ? "CNm" : "CNp")); |
NEWS[0] == '\0' ? "C" : (strchr(NEWS, '@') ? "CNm" : "CNp")); |
| 396 |
fprintf(f, "\n%s\n", begin_key); |
if (created_found) { |
| 397 |
|
struct tm *gt; |
| 398 |
|
gt = gmtime(&created_found); |
| 399 |
|
strftime(line, LINELEN, "%Y-%m-%d", gt); |
| 400 |
|
fprintf(f, " %s", line); |
| 401 |
|
if (expires_found) { |
| 402 |
|
struct tm *gt; |
| 403 |
|
gt = gmtime(&expires_found); |
| 404 |
|
strftime(line, LINELEN, "%Y-%m-%d", gt); |
| 405 |
|
fprintf(f, " %s", line); |
| 406 |
|
} |
| 407 |
|
} |
| 408 |
|
fprintf(f, "\n\n%s\n", begin_key); |
| 409 |
|
id_encode(k1_found, line); |
| 410 |
fprintf(f, "%s\n258\n", line); |
fprintf(f, "%s\n258\n", line); |
| 411 |
encode(pk, 40); |
encode(pk_found, 40); |
| 412 |
buf_write(pk, f); |
buf_write(pk_found, f); |
| 413 |
fprintf(f, "%s\n\n", end_key); |
fprintf(f, "%s\n\n", end_key); |
| 414 |
fclose(f); |
fclose(f); |
| 415 |
} |
} |
| 416 |
} else |
} else |
| 417 |
err = -1; |
err = -1; |
| 418 |
|
|
|
end: |
|
| 419 |
buf_free(b); |
buf_free(b); |
| 420 |
buf_free(temp); |
buf_free(temp); |
| 421 |
buf_free(iv); |
buf_free(iv); |
| 422 |
buf_free(pass); |
buf_free(pass); |
| 423 |
buf_free(pk); |
buf_free(pk); |
| 424 |
|
buf_free(pk_found); |
| 425 |
|
|
| 426 |
return (err); |
return (err); |
| 427 |
} |
} |