| 6 |
details. |
details. |
| 7 |
|
|
| 8 |
OpenPGP key database |
OpenPGP key database |
| 9 |
$Id: pgpdb.c,v 1.16 2002/09/26 22:40:56 weaselp Exp $ */ |
$Id: pgpdb.c,v 1.17 2002/09/27 09:15:54 weaselp Exp $ */ |
| 10 |
|
|
| 11 |
|
|
| 12 |
#include "mix3.h" |
#include "mix3.h" |
| 330 |
FILE *f = NULL; |
FILE *f = NULL; |
| 331 |
BUFFER *key, *keybak, *userid, *out, *outkey, *outtxt, *pass, *secout; |
BUFFER *key, *keybak, *userid, *out, *outkey, *outtxt, *pass, *secout; |
| 332 |
KEYRING *keys; |
KEYRING *keys; |
| 333 |
int err = 0, type = 0, res; |
int err = 0, res, recreate_pubring = 0, dsa_ok = 0; |
| 334 |
|
#ifdef USE_RSA |
| 335 |
|
#ifdef USE_IDEA |
| 336 |
|
int rsa_ok = 0; |
| 337 |
|
#endif /* USE_IDEA */ |
| 338 |
|
#endif /* USE_RSA */ |
| 339 |
long expires; |
long expires; |
| 340 |
LOCK *seclock; |
LOCK *seclock; |
| 341 |
|
|
| 357 |
* with IDEA. |
* with IDEA. |
| 358 |
*/ |
*/ |
| 359 |
#ifdef USE_IDEA |
#ifdef USE_IDEA |
| 360 |
|
/* FIXME: pgpdb_getky returns the expiration date from the last key in the keyring |
| 361 |
|
* which probably works most of the time if the keys are in the correct order |
| 362 |
|
* it doesn't return the latest expiration date (or 0) if the key in question |
| 363 |
|
* is before another matching key in the keyring tho |
| 364 |
|
*/ |
| 365 |
res = pgpdb_getkey(PK_DECRYPT, PGP_ES_RSA, NULL, NULL, &expires, NULL, NULL, |
res = pgpdb_getkey(PK_DECRYPT, PGP_ES_RSA, NULL, NULL, &expires, NULL, NULL, |
| 366 |
NULL, NULL, NULL, pass); |
NULL, NULL, NULL, pass); |
| 367 |
if (force == 2 || res < 0 || (expires > 0 && expires - KEYOVERLAPPERIOD < time(NULL))) |
if (force == 2 || res < 0 || (expires > 0 && expires - KEYOVERLAPPERIOD < time(NULL))) { |
| 368 |
|
rsa_ok = -1; |
| 369 |
pgp_keygen(PGP_ES_RSA, 0, userid, pass, PGPKEY, PGPREMSECRING, 0); |
pgp_keygen(PGP_ES_RSA, 0, userid, pass, PGPKEY, PGPREMSECRING, 0); |
| 370 |
|
}; |
| 371 |
|
|
| 372 |
if (force == 0 && (pgpdb_getkey(PK_ENCRYPT, PGP_ES_RSA, NULL, NULL, NULL, NULL, NULL, |
if (force == 0 && (pgpdb_getkey(PK_ENCRYPT, PGP_ES_RSA, NULL, NULL, NULL, NULL, NULL, |
| 373 |
NULL, NULL, PGPKEY, NULL) < 0)) |
NULL, NULL, PGPKEY, NULL) < 0) && rsa_ok == 0) |
| 374 |
goto end; |
rsa_ok = 1; |
| 375 |
#endif /* USE_IDEA */ |
#endif /* USE_IDEA */ |
| 376 |
#endif /* USE_RSA */ |
#endif /* USE_RSA */ |
| 377 |
|
/* FIXME: pgpdb_getky returns the expiration date from the last key in the keyring |
| 378 |
|
* which probably works most of the time if the keys are in the correct order |
| 379 |
|
* it doesn't return the latest expiration date (or 0) if the key in question |
| 380 |
|
* is before another matching key in the keyring tho |
| 381 |
|
*/ |
| 382 |
res = pgpdb_getkey(PK_DECRYPT, PGP_E_ELG, NULL, NULL, &expires, NULL, NULL, |
res = pgpdb_getkey(PK_DECRYPT, PGP_E_ELG, NULL, NULL, &expires, NULL, NULL, |
| 383 |
NULL, NULL, NULL, pass); |
NULL, NULL, NULL, pass); |
| 384 |
if (force == 2 || res < 0 || (expires > 0 && expires - KEYOVERLAPPERIOD < time(NULL))) |
if (force == 2 || res < 0 || (expires > 0 && expires - KEYOVERLAPPERIOD < time(NULL))) { |
| 385 |
|
dsa_ok = -1; |
| 386 |
pgp_keygen(PGP_E_ELG, 0, userid, pass, PGPKEY, PGPREMSECRING, 0); |
pgp_keygen(PGP_E_ELG, 0, userid, pass, PGPKEY, PGPREMSECRING, 0); |
| 387 |
|
} |
| 388 |
|
|
| 389 |
if (force == 0 && (pgpdb_getkey(PK_ENCRYPT, PGP_E_ELG, NULL, NULL, NULL, NULL, NULL, |
if (force == 0 && (pgpdb_getkey(PK_ENCRYPT, PGP_E_ELG, NULL, NULL, NULL, NULL, NULL, |
| 390 |
NULL, NULL, PGPKEY, NULL) > 0)) |
NULL, NULL, PGPKEY, NULL) > 0) && dsa_ok == 0) |
| 391 |
|
dsa_ok = 1; |
| 392 |
|
|
| 393 |
|
/* No need to rewrite the files - we didn't change a thing */ |
| 394 |
|
if ( |
| 395 |
|
#ifdef USE_RSA |
| 396 |
|
#ifdef USE_IDEA |
| 397 |
|
rsa_ok == 1 && |
| 398 |
|
#endif /* USE_IDEA */ |
| 399 |
|
#endif /* USE_RSA */ |
| 400 |
|
dsa_ok == 1) |
| 401 |
goto end; |
goto end; |
| 402 |
|
|
| 403 |
/* write RSA and DSA/ElGamal keys separately to make old PGP |
/* write keys one key per armor to make hand editing easy and old PGP |
| 404 |
versions happy */ |
* versions happy */ |
| 405 |
err = -1; |
err = -1; |
| 406 |
for (type = 0; type < 2; type++) { |
keys = pgpdb_open(PGPKEY, NULL, 0, PGP_TYPE_PUBLIC); |
| 407 |
keys = pgpdb_open(PGPREMSECRING, NULL, 0, PGP_TYPE_PRIVATE); |
if (keys == NULL) |
| 408 |
if (keys == NULL) |
recreate_pubring = 1; |
| 409 |
goto end; |
else { |
| 410 |
while (pgpdb_getnext(keys, key, NULL, userid) != -1) { |
while (pgpdb_getnext(keys, key, NULL, userid) != -1) { |
| 411 |
buf_clear(outtxt); |
buf_clear(outtxt); |
| 412 |
buf_clear(outkey); |
buf_clear(outkey); |
| 413 |
buf_clear(keybak); |
buf_appends(outtxt, "FIXME"); |
| 414 |
buf_cat(keybak, key); |
err = 0; |
|
if (pgp_makeseckey(key, outtxt, pass, |
|
|
type == 0 ? PGP_ES_RSA : PGP_S_DSA) == 0) { |
|
|
err = 0; |
|
|
buf_appends(secout, "Type Bits/KeyID Date User ID\n"); |
|
|
buf_cat(secout, outtxt); |
|
|
buf_nl(secout); |
|
|
pgp_armor(key, PGP_ARMOR_SECKEY); |
|
|
buf_cat(secout, key); |
|
|
buf_nl(secout); |
|
|
} |
|
|
buf_clear(outtxt); |
|
|
if (pgp_makepubkey(keybak, outtxt, outkey, pass, |
|
|
type == 0 ? PGP_ES_RSA : PGP_S_DSA) == 0) { |
|
|
err = 0; |
|
| 415 |
buf_appends(out, "Type Bits/KeyID Date User ID\n"); |
buf_appends(out, "Type Bits/KeyID Date User ID\n"); |
| 416 |
buf_cat(out, outtxt); |
buf_cat(out, outtxt); |
| 417 |
buf_nl(out); |
buf_nl(out); |
| 418 |
pgp_armor(outkey, PGP_ARMOR_KEY); |
pgp_armor(key, PGP_ARMOR_KEY); |
| 419 |
buf_cat(out, outkey); |
buf_cat(out, key); |
| 420 |
buf_nl(out); |
buf_nl(out); |
|
} |
|
| 421 |
} |
} |
| 422 |
pgpdb_close(keys); |
pgpdb_close(keys); |
| 423 |
} |
} |
| 424 |
|
if (err != 0) |
| 425 |
|
recreate_pubring = 1; |
| 426 |
|
err = -1; |
| 427 |
|
|
| 428 |
|
keys = pgpdb_open(PGPREMSECRING, NULL, 0, PGP_TYPE_PRIVATE); |
| 429 |
|
if (keys == NULL) |
| 430 |
|
goto end; |
| 431 |
|
while (pgpdb_getnext(keys, key, NULL, userid) != -1) { |
| 432 |
|
buf_clear(outtxt); |
| 433 |
|
buf_clear(outkey); |
| 434 |
|
buf_clear(keybak); |
| 435 |
|
buf_cat(keybak, key); |
| 436 |
|
if (pgp_makeseckey(key, outtxt, pass, PGP_ANY) == 0) { |
| 437 |
|
err = 0; |
| 438 |
|
buf_appends(secout, "Type Bits/KeyID Date User ID\n"); |
| 439 |
|
buf_cat(secout, outtxt); |
| 440 |
|
buf_nl(secout); |
| 441 |
|
pgp_armor(key, PGP_ARMOR_SECKEY); |
| 442 |
|
buf_cat(secout, key); |
| 443 |
|
buf_nl(secout); |
| 444 |
|
} |
| 445 |
|
buf_clear(outtxt); |
| 446 |
|
if (recreate_pubring && |
| 447 |
|
pgp_makepubkey(keybak, outtxt, outkey, pass, PGP_ANY) == 0) { |
| 448 |
|
buf_appends(out, "Type Bits/KeyID Date User ID\n"); |
| 449 |
|
buf_cat(out, outtxt); |
| 450 |
|
buf_nl(out); |
| 451 |
|
pgp_armor(outkey, PGP_ARMOR_KEY); |
| 452 |
|
buf_cat(out, outkey); |
| 453 |
|
buf_nl(out); |
| 454 |
|
} |
| 455 |
|
} |
| 456 |
|
pgpdb_close(keys); |
| 457 |
|
|
| 458 |
seclock = lockfile(PGPREMSECRING); |
seclock = lockfile(PGPREMSECRING); |
| 459 |
if (err == 0 && (f = mix_openfile(PGPREMSECRING, "w")) != NULL) { |
if (err == 0 && (f = mix_openfile(PGPREMSECRING, "w")) != NULL) { |