1 |
/* wptKeyEdit.cpp - GPG key edit abstraction |
/* wptKeyEdit.cpp - GPG key edit abstraction |
2 |
* Copyright (C) 2005 Timo Schulz |
* Copyright (C) 2005, 2006 Timo Schulz |
3 |
* Copyright (C) 2005 g10 Code GmbH |
* Copyright (C) 2005 g10 Code GmbH |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
224 |
return 0; |
return 0; |
225 |
} |
} |
226 |
|
|
227 |
|
|
228 |
/* Dummy handler to get the colon data and then quit. */ |
/* Dummy handler to get the colon data and then quit. */ |
229 |
static gpgme_error_t |
static gpgme_error_t |
230 |
list_handler (void *opaque, gpgme_status_code_t code, const char *key, int fd) |
list_handler (void *opaque, gpgme_status_code_t code, const char *key, int fd) |
254 |
gpgme_error_t |
gpgme_error_t |
255 |
GpgKeyEdit::getDesignatedRevoker (gpg_desig_rev_t *r_rev) |
GpgKeyEdit::getDesignatedRevoker (gpg_desig_rev_t *r_rev) |
256 |
{ |
{ |
257 |
gpgme_data_t out=NULL; |
gpgme_data_t out = NULL; |
258 |
gpg_desig_rev_t rev = NULL; |
gpg_desig_rev_t rev = NULL; |
259 |
gpgme_error_t err; |
gpgme_error_t err; |
260 |
char buf[256]; |
char buf[256]; |
285 |
return err; |
return err; |
286 |
} |
} |
287 |
|
|
288 |
|
|
289 |
/* Retrieve all user ID information of the key set via setKey |
/* Retrieve all user ID information of the key set via setKey |
290 |
in @r_inf. The result also contains the user ID number which |
in @r_inf. The result also contains the user ID number which |
291 |
is needed to securely delete the user-ID. */ |
is needed to securely delete the user-ID. */ |
325 |
} |
} |
326 |
|
|
327 |
|
|
328 |
/* Construct an object with the given key in @key. */ |
/* Clear object. */ |
329 |
GpgKeyEdit::GpgKeyEdit (gpgme_key_t _key) |
void |
330 |
|
GpgKeyEdit::clear (void) |
331 |
{ |
{ |
332 |
this->key = _key; |
pass = NULL; |
|
pass = NULL; |
|
|
type = 0; |
|
333 |
name = NULL; |
name = NULL; |
334 |
cmt = NULL; |
cmt = NULL; |
335 |
email = NULL; |
email = NULL; |
336 |
|
type = 0; |
337 |
cnt = 0; |
cnt = 0; |
338 |
cmd_sent = 0; |
cmd_sent = 0; |
339 |
resval = 0; |
resval = 0; |
340 |
|
uid_index = sig_index = key_index = -1; |
341 |
|
key_has_passwd = true; |
342 |
|
} |
343 |
|
|
344 |
|
|
345 |
|
/* Construct an object with the given key in @key. */ |
346 |
|
GpgKeyEdit::GpgKeyEdit (gpgme_key_t _key) |
347 |
|
{ |
348 |
|
clear (); |
349 |
|
this->key = _key; |
350 |
gpgme_new (&ctx); /* FIXME */ |
gpgme_new (&ctx); /* FIXME */ |
351 |
} |
} |
352 |
|
|
353 |
/* Construct an object and fetch the key with the keyid @keyid. */ |
/* Construct an object and fetch the key with the keyid @keyid. */ |
354 |
GpgKeyEdit::GpgKeyEdit (const char *_keyid) |
GpgKeyEdit::GpgKeyEdit (const char *_keyid) |
355 |
{ |
{ |
356 |
|
clear (); |
357 |
get_pubkey (_keyid, &this->key); |
get_pubkey (_keyid, &this->key); |
358 |
pass = NULL; |
gpgme_new (&ctx); /* FIXME */ |
|
type = 0; |
|
|
name = NULL; |
|
|
cmt = NULL; |
|
|
email = NULL; |
|
|
cmd_sent = 0; |
|
|
resval = 0; |
|
|
gpgme_new (&ctx); /* FIXME */ |
|
359 |
} |
} |
360 |
|
|
361 |
/* Delete the given object. */ |
/* Delete the given object. */ |
373 |
GpgKeyEdit::reset (void) |
GpgKeyEdit::reset (void) |
374 |
{ |
{ |
375 |
cmd_sent = 0; |
cmd_sent = 0; |
376 |
|
cnt = 0; |
377 |
|
//resval = 0; |
378 |
} |
} |
379 |
|
|
380 |
|
|
402 |
gpgme_set_progress_cb (ctx, cb, cb_value); |
gpgme_set_progress_cb (ctx, cb, cb_value); |
403 |
} |
} |
404 |
|
|
405 |
|
|
406 |
|
/* Clear the stored passphrase. */ |
407 |
|
void |
408 |
|
GpgKeyEdit::clearPassphrase (void) |
409 |
|
{ |
410 |
|
if (pass) |
411 |
|
pass = NULL; |
412 |
|
} |
413 |
|
|
414 |
|
|
415 |
|
|
416 |
|
/* Inidicate that a key is protected by a passphrase or not. */ |
417 |
|
void |
418 |
|
GpgKeyEdit::setNoPassphrase (bool val) |
419 |
|
{ |
420 |
|
key_has_passwd = !val; |
421 |
|
} |
422 |
|
|
423 |
/* Set the passphrase to @pass. */ |
/* Set the passphrase to @pass. */ |
424 |
void |
void |
425 |
GpgKeyEdit::setPassphrase (const char *_pass) |
GpgKeyEdit::setPassphrase (const char *_pass) |
467 |
} |
} |
468 |
|
|
469 |
|
|
470 |
|
/* Return the amount of days the key is valid. */ |
471 |
|
int |
472 |
|
GpgKeyEdit::getValidDays (void) |
473 |
|
{ |
474 |
|
return valid; |
475 |
|
} |
476 |
|
|
477 |
|
|
478 |
int |
int |
479 |
GpgKeyEdit::getType (void) |
GpgKeyEdit::getType (void) |
481 |
return type; |
return type; |
482 |
} |
} |
483 |
|
|
484 |
|
|
485 |
|
/* Add the notation data from @notation to the user ID |
486 |
|
with the index @_uid_idx. |
487 |
|
Return value: 0 on success. */ |
488 |
|
gpgme_error_t |
489 |
|
GpgKeyEdit::addNotation (int _uid_idx, const char *_notation) |
490 |
|
{ |
491 |
|
if (!key) |
492 |
|
return gpg_error (GPG_ERR_INV_OBJ); |
493 |
|
if (key_has_passwd && !this->pass) |
494 |
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
495 |
|
|
496 |
|
type = GPG_EDITKEY_NOTATION; |
497 |
|
this->uid_index = _uid_idx; |
498 |
|
this->notation = (char*)_notation; |
499 |
|
return gpg_editkey (this->ctx, this->key, this); |
500 |
|
} |
501 |
|
|
502 |
|
|
503 |
/* Sign the key stored in the object with the |
/* Sign the key stored in the object with the |
504 |
signing mode @mode and the signature class @sig_class. |
signing mode @mode and the signature class @sig_class. |
505 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
506 |
gpgme_error_t |
gpgme_error_t |
507 |
GpgKeyEdit::signKey (int mode, int _sig_class, const char *_exp_date) |
GpgKeyEdit::signKey (int mode, int _sig_class, const char *_exp_date) |
508 |
{ |
{ |
509 |
if (!this->key || !this->pass) |
if (!this->key) |
510 |
return gpg_error (GPG_ERR_INV_OBJ); |
return gpg_error (GPG_ERR_INV_OBJ); |
511 |
|
if (key_has_passwd && !this->pass) |
512 |
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
513 |
|
|
514 |
type = mode; |
type = mode; |
515 |
this->exp_date = _exp_date; |
this->exp_date = _exp_date; |
518 |
} |
} |
519 |
|
|
520 |
|
|
521 |
|
/* Sign a single user-id with the index @_uid_index. |
522 |
|
All other parameters are equal to signKey(). |
523 |
|
Return value: 0 on success. */ |
524 |
|
gpgme_error_t |
525 |
|
GpgKeyEdit::signUserid (int _uid_idx, int mode, int _sig_class, |
526 |
|
const char *_exp_date) |
527 |
|
{ |
528 |
|
if (!this->key) |
529 |
|
return gpg_error (GPG_ERR_INV_OBJ); |
530 |
|
if (key_has_passwd && !this->pass) |
531 |
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
532 |
|
|
533 |
|
this->uid_index = _uid_idx; |
534 |
|
type = mode; |
535 |
|
this->exp_date = _exp_date; |
536 |
|
this->sig_class = _sig_class; |
537 |
|
return gpg_editkey (this->ctx, this->key, this); |
538 |
|
} |
539 |
|
|
540 |
|
|
541 |
/* Set the ownertrust of the key stored in the object |
/* Set the ownertrust of the key stored in the object |
542 |
to the trust value @trust. |
to the trust value @trust. |
543 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
558 |
gpgme_error_t |
gpgme_error_t |
559 |
GpgKeyEdit::addUserid (const char *_name, const char *_cmt, const char *_email) |
GpgKeyEdit::addUserid (const char *_name, const char *_cmt, const char *_email) |
560 |
{ |
{ |
561 |
if (!this->key || !this->pass) |
if (!this->key) |
562 |
return gpg_error (GPG_ERR_INV_OBJ); |
return gpg_error (GPG_ERR_INV_OBJ); |
563 |
|
if (key_has_passwd && !this->pass) |
564 |
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
565 |
|
|
566 |
type = GPG_EDITKEY_ADDUID; |
type = GPG_EDITKEY_ADDUID; |
567 |
free_if_alloc (this->name); |
free_if_alloc (this->name); |
568 |
this->name = m_strdup (_name); |
this->name = m_strdup (_name); |
569 |
free_if_alloc (this->cmt); |
free_if_alloc (this->cmt); |
570 |
this->cmt = NULL; |
this->cmt = NULL; |
571 |
if (cmt != NULL) |
if (_cmt != NULL) |
572 |
this->cmt = m_strdup (_cmt); |
this->cmt = m_strdup (_cmt); |
573 |
free_if_alloc (this->email); |
free_if_alloc (this->email); |
574 |
this->email = m_strdup (_email); |
this->email = m_strdup (_email); |
612 |
GpgKeyEdit::addSubkey (gpgme_pubkey_algo_t _pubkey_algo, |
GpgKeyEdit::addSubkey (gpgme_pubkey_algo_t _pubkey_algo, |
613 |
unsigned int _pubkey_size, long _valid) |
unsigned int _pubkey_size, long _valid) |
614 |
{ |
{ |
615 |
if (!this->key || !this->pass) |
if (!this->key) |
616 |
return gpg_error (GPG_ERR_INV_OBJ); |
return gpg_error (GPG_ERR_INV_OBJ); |
617 |
|
if (key_has_passwd && !this->pass) |
618 |
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
619 |
|
|
620 |
type = GPG_EDITKEY_ADDKEY; |
type = GPG_EDITKEY_ADDKEY; |
621 |
this->pubkey_algo = _pubkey_algo; |
this->pubkey_algo = _pubkey_algo; |
630 |
gpgme_error_t |
gpgme_error_t |
631 |
GpgKeyEdit::changePassphrase (const char *_new_pass, int allow_empty) |
GpgKeyEdit::changePassphrase (const char *_new_pass, int allow_empty) |
632 |
{ |
{ |
633 |
if (!this->key || !this->pass) |
if (!this->key) |
634 |
return gpg_error (GPG_ERR_INV_OBJ); |
return gpg_error (GPG_ERR_INV_OBJ); |
635 |
|
if (key_has_passwd && !this->pass) |
636 |
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
637 |
|
|
638 |
type = GPG_EDITKEY_PASSWD; |
type = GPG_EDITKEY_PASSWD; |
639 |
this->new_pass = _new_pass; |
this->new_pass = _new_pass; |
647 |
gpgme_error_t |
gpgme_error_t |
648 |
GpgKeyEdit::setPrimaryUserid (int _uid_index) |
GpgKeyEdit::setPrimaryUserid (int _uid_index) |
649 |
{ |
{ |
650 |
if (!this->key || !this->pass) |
if (!this->key) |
651 |
return gpg_error (GPG_ERR_INV_OBJ); |
return gpg_error (GPG_ERR_INV_OBJ); |
652 |
|
if (key_has_passwd && !this->pass) |
653 |
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
654 |
|
|
655 |
type = GPG_EDITKEY_PRIMARY; |
type = GPG_EDITKEY_PRIMARY; |
656 |
this->uid_index = _uid_index; |
this->uid_index = _uid_index; |
662 |
if @exp_days is true, exp_timestamp is already converted to days. |
if @exp_days is true, exp_timestamp is already converted to days. |
663 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
664 |
gpgme_error_t |
gpgme_error_t |
665 |
GpgKeyEdit::setKeyExpireDate (int _key_index, |
GpgKeyEdit::setKeyExpireDate (int _key_index, long exp_timestamp, |
666 |
long exp_timestamp, bool exp_days) |
bool exp_days) |
667 |
{ |
{ |
668 |
if (!this->key || !this->pass) |
if (!this->key) |
669 |
return gpg_error (GPG_ERR_INV_OBJ); |
return gpg_error (GPG_ERR_INV_OBJ); |
670 |
|
if (key_has_passwd && !this->pass) |
671 |
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
672 |
if (!exp_days && exp_timestamp > 0 && exp_timestamp < time (NULL)) |
if (!exp_days && exp_timestamp > 0 && exp_timestamp < time (NULL)) |
673 |
return gpg_error (GPG_ERR_INV_ARG); |
return gpg_error (GPG_ERR_INV_ARG); |
674 |
|
|
688 |
gpgme_error_t |
gpgme_error_t |
689 |
GpgKeyEdit::revokeUserid (int _uid_index) |
GpgKeyEdit::revokeUserid (int _uid_index) |
690 |
{ |
{ |
691 |
if (!this->key || !this->pass) |
if (!this->key) |
692 |
return gpg_error (GPG_ERR_INV_OBJ); |
return gpg_error (GPG_ERR_INV_OBJ); |
693 |
|
if (key_has_passwd && !this->pass) |
694 |
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
695 |
|
|
696 |
type = GPG_EDITKEY_REVUID; |
type = GPG_EDITKEY_REVUID; |
697 |
this->uid_index = _uid_index; |
this->uid_index = _uid_index; |
705 |
gpgme_error_t |
gpgme_error_t |
706 |
GpgKeyEdit::revokeSignature (int _uid_index, int _sig_index) |
GpgKeyEdit::revokeSignature (int _uid_index, int _sig_index) |
707 |
{ |
{ |
708 |
if (!this->key || !this->pass) |
if (!this->key) |
709 |
return gpg_error (GPG_ERR_INV_OBJ); |
return gpg_error (GPG_ERR_INV_OBJ); |
710 |
|
if (key_has_passwd && !this->pass) |
711 |
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
712 |
|
|
713 |
type = GPG_EDITKEY_REVSIG; |
type = GPG_EDITKEY_REVSIG; |
714 |
this->uid_index = _uid_index; |
this->uid_index = _uid_index; |
724 |
gpgme_error_t |
gpgme_error_t |
725 |
GpgKeyEdit::revokeSubkey (int _key_index, int _reason, const char *_cmt) |
GpgKeyEdit::revokeSubkey (int _key_index, int _reason, const char *_cmt) |
726 |
{ |
{ |
727 |
if (!this->key || !this->pass) |
if (!this->key) |
728 |
return gpg_error (GPG_ERR_INV_OBJ); |
return gpg_error (GPG_ERR_INV_OBJ); |
729 |
|
if (key_has_passwd && !this->pass) |
730 |
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
731 |
|
|
732 |
type = GPG_EDITKEY_REVKEY; |
type = GPG_EDITKEY_REVKEY; |
733 |
this->key_index = _key_index; |
this->key_index = _key_index; |
747 |
gpgme_error_t |
gpgme_error_t |
748 |
GpgKeyEdit::addDesignatedRevoker (const char *uid) |
GpgKeyEdit::addDesignatedRevoker (const char *uid) |
749 |
{ |
{ |
750 |
if (!this->key || !this->pass) |
if (!this->key) |
751 |
return gpg_error (GPG_ERR_INV_OBJ); |
return gpg_error (GPG_ERR_INV_OBJ); |
752 |
|
if (key_has_passwd && !this->pass) |
753 |
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
754 |
|
|
755 |
type = GPG_EDITKEY_ADDREV; |
type = GPG_EDITKEY_ADDREV; |
756 |
free_if_alloc (this->name); |
free_if_alloc (this->name); |
764 |
gpgme_error_t |
gpgme_error_t |
765 |
GpgKeyEdit::addPhotoid (const char *jpg_file) |
GpgKeyEdit::addPhotoid (const char *jpg_file) |
766 |
{ |
{ |
767 |
if (!this->key || !this->pass) |
if (!this->key) |
768 |
return gpg_error (GPG_ERR_INV_OBJ); |
return gpg_error (GPG_ERR_INV_OBJ); |
769 |
|
if (key_has_passwd && !this->pass) |
770 |
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
771 |
|
|
772 |
type = GPG_EDITKEY_ADDPHOTO; |
type = GPG_EDITKEY_ADDPHOTO; |
773 |
this->url = jpg_file; |
this->url = jpg_file; |
803 |
return gpg_error (GPG_ERR_INV_OBJ); |
return gpg_error (GPG_ERR_INV_OBJ); |
804 |
type = GPG_EDITKEY_CLEAN; |
type = GPG_EDITKEY_CLEAN; |
805 |
return gpg_editkey (this->ctx, this->key, this); |
return gpg_editkey (this->ctx, this->key, this); |
|
|
|
806 |
} |
} |
807 |
|
|
808 |
|
|
812 |
gpgme_error_t |
gpgme_error_t |
813 |
GpgKeyEdit::setUseridPreferences (int _uid_index, const char *new_prefs) |
GpgKeyEdit::setUseridPreferences (int _uid_index, const char *new_prefs) |
814 |
{ |
{ |
815 |
if (!this->key || !this->pass) |
if (!this->key) |
816 |
return gpg_error (GPG_ERR_INV_OBJ); |
return gpg_error (GPG_ERR_INV_OBJ); |
817 |
|
if (key_has_passwd && !this->pass) |
818 |
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
819 |
return 0; |
return 0; |
820 |
} |
} |
821 |
|
|
835 |
} |
} |
836 |
|
|
837 |
/* Set the preferred keyserver for the given key to @url. |
/* Set the preferred keyserver for the given key to @url. |
838 |
|
If @_uid_index is -1, set the keyserver for all user-ids. |
839 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
840 |
gpgme_error_t |
gpgme_error_t |
841 |
GpgKeyEdit::setPreferredKeyserver (int _uid_index, const char *_url) |
GpgKeyEdit::setPreferredKeyserver (int _uid_index, const char *_url) |
842 |
{ |
{ |
843 |
if (!this->key || !this->pass) |
if (!this->key) |
844 |
return gpg_error (GPG_ERR_INV_OBJ); |
return gpg_error (GPG_ERR_INV_OBJ); |
845 |
if (!url) |
if (key_has_passwd && !this->pass) |
846 |
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
847 |
|
if (!_url) |
848 |
return gpg_error (GPG_ERR_INV_ARG); |
return gpg_error (GPG_ERR_INV_ARG); |
849 |
|
|
850 |
type = GPG_EDITKEY_KEYSERV; |
type = GPG_EDITKEY_KEYSERV; |
851 |
this->url = _url; |
this->url = _url; |
852 |
this->uid_index = _uid_index; |
this->uid_index = _uid_index; |
853 |
return gpg_editkey (this->ctx, this->key, this); |
return gpg_editkey (this->ctx, this->key, this); |
854 |
} |
} |
855 |
|
|
856 |
|
|
857 |
|
/* Return the saved user-id index. */ |
858 |
|
int |
859 |
|
GpgKeyEdit::getUseridIndex (void) |
860 |
|
{ |
861 |
|
return uid_index; |
862 |
|
} |
863 |
|
|
864 |
|
|
865 |
|
/* Return the saved key index. */ |
866 |
|
int |
867 |
|
GpgKeyEdit::getKeyIndex (void) |
868 |
|
{ |
869 |
|
return key_index; |
870 |
|
} |
871 |
|
|
872 |
|
|
873 |
|
/* Return the saved sig index. */ |
874 |
|
int |
875 |
|
GpgKeyEdit::getSigIndex (void) |
876 |
|
{ |
877 |
|
return sig_index; |
878 |
|
} |