/** * Import a key defined by a fingerprint into the local keyring. * @param handle the context handle * @param fpr the fingerprint key ID to import * @return 0 on success, -1 on error */ int _alpm_key_import(alpm_handle_t *handle, const char *fpr) { int answer = 0, ret = -1; alpm_pgpkey_t fetch_key; memset(&fetch_key, 0, sizeof(fetch_key)); if(key_search(handle, fpr, &fetch_key) == 1) { _alpm_log(handle, ALPM_LOG_DEBUG, "unknown key, found %s on keyserver\n", fetch_key.uid); if(!_alpm_access(handle, handle->gpgdir, "pubring.gpg", W_OK)) { QUESTION(handle, ALPM_QUESTION_IMPORT_KEY, &fetch_key, NULL, NULL, &answer); if(answer) { if(key_import(handle, &fetch_key) == 0) { ret = 0; } else { _alpm_log(handle, ALPM_LOG_ERROR, _("key \"%s\" could not be imported\n"), fetch_key.uid); } } } else { /* keyring directory was not writable, so we don't even try */ _alpm_log(handle, ALPM_LOG_WARNING, _("key %s, \"%s\" found on keyserver, keyring is not writable\n"), fetch_key.fingerprint, fetch_key.uid); } } else { _alpm_log(handle, ALPM_LOG_ERROR, _("key \"%s\" could not be looked up remotely\n"), fpr); } gpgme_key_unref(fetch_key.data); return ret; }
static PyObject * pygpgme_context_get_signers(PyGpgmeContext *self) { PyObject *list, *tuple; gpgme_key_t key; int i; list = PyList_New(0); for (i = 0, key = gpgme_signers_enum(self->ctx, 0); key != NULL; key = gpgme_signers_enum(self->ctx, ++i)) { PyObject *item; item = pygpgme_key_new(key); gpgme_key_unref(key); if (item == NULL) { Py_DECREF(list); return NULL; } PyList_Append(list, item); Py_DECREF(item); } tuple = PySequence_Tuple(list); Py_DECREF(list); return tuple; }
gboolean p_gpg_valid_key(const char *const keyid, char **err_str) { gpgme_ctx_t ctx; gpgme_error_t error = gpgme_new(&ctx); if (error) { log_error("GPG: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error)); *err_str = strdup(gpgme_strerror(error)); return FALSE; } gpgme_key_t key = NULL; error = gpgme_get_key(ctx, keyid, &key, 1); if (error || key == NULL) { log_error("GPG: Failed to get key. %s %s", gpgme_strsource(error), gpgme_strerror(error)); *err_str = strdup(gpgme_strerror(error)); gpgme_release(ctx); return FALSE; } if (key == NULL) { *err_str = strdup("Unknown error"); gpgme_release(ctx); return FALSE; } gpgme_release(ctx); gpgme_key_unref(key); return TRUE; }
static PyObject * pygpgme_keyiter_next(PyGpgmeKeyIter *self) { gpgme_key_t key = NULL; gpgme_error_t err; PyObject *ret; Py_BEGIN_ALLOW_THREADS; err = gpgme_op_keylist_next(self->ctx->ctx, &key); Py_END_ALLOW_THREADS; /* end iteration */ if (gpgme_err_source(err) == GPG_ERR_SOURCE_GPGME && gpgme_err_code(err) == GPG_ERR_EOF) { PyErr_SetNone(PyExc_StopIteration); return NULL; } if (pygpgme_check_error(err)) return NULL; if (key == NULL) Py_RETURN_NONE; ret = pygpgme_key_new(key); gpgme_key_unref(key); return ret; }
gboolean p_gpg_addkey(const char *const jid, const char *const keyid) { gpgme_ctx_t ctx; gpgme_error_t error = gpgme_new(&ctx); if (error) { log_error("GPG: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error)); return FALSE; } gpgme_key_t key = NULL; error = gpgme_get_key(ctx, keyid, &key, 0); gpgme_release(ctx); if (error || key == NULL) { log_error("GPG: Failed to get key. %s %s", gpgme_strsource(error), gpgme_strerror(error)); return FALSE; } // save to public key file g_key_file_set_string(pubkeyfile, jid, "keyid", keyid); _save_pubkeys(); // update in memory pubkeys list ProfPGPPubKeyId *pubkeyid = malloc(sizeof(ProfPGPPubKeyId)); pubkeyid->id = strdup(keyid); pubkeyid->received = FALSE; g_hash_table_replace(pubkeys, strdup(jid), pubkeyid); gpgme_key_unref(key); return TRUE; }
/** * Determine if we have a key is known in our local keyring. * @param handle the context handle * @param fpr the fingerprint key ID to look up * @return 1 if key is known, 0 if key is unknown, -1 on error */ static int key_in_keychain(alpm_handle_t *handle, const char *fpr) { gpgme_error_t err; gpgme_ctx_t ctx; gpgme_key_t key; int ret = -1; memset(&ctx, 0, sizeof(ctx)); err = gpgme_new(&ctx); CHECK_ERR(); _alpm_log(handle, ALPM_LOG_DEBUG, "looking up key %s locally\n", fpr); err = gpgme_get_key(ctx, fpr, &key, 0); if(gpg_err_code(err) == GPG_ERR_EOF) { _alpm_log(handle, ALPM_LOG_DEBUG, "key lookup failed, unknown key\n"); ret = 0; } else if(gpg_err_code(err) == GPG_ERR_NO_ERROR) { _alpm_log(handle, ALPM_LOG_DEBUG, "key lookup success, key exists\n"); ret = 1; } else { _alpm_log(handle, ALPM_LOG_DEBUG, "gpg error: %s\n", gpgme_strerror(err)); } gpgme_key_unref(key); error: gpgme_release(ctx); return ret; }
/* Created a detached signature for INDATA and write it to OUTDATA. On termination of the signing command engine_private_finished() is called with FILTER as the first argument. */ int op_gpgme_sign (protocol_t protocol, gpgme_data_t indata, gpgme_data_t outdata, engine_filter_t filter, void *hwnd) { gpg_error_t err; closure_data_t cld; gpgme_ctx_t ctx = NULL; gpgme_key_t sign_key = NULL; (void)hwnd; if (signer_dialog_box (&sign_key, NULL, 0) == -1) { log_debug ("%s:%s: leave (dialog failed)\n", SRCNAME, __func__); return gpg_error (GPG_ERR_CANCELED); } cld = xcalloc (1, sizeof *cld); cld->closure = sign_closure; cld->filter = filter; err = gpgme_new (&ctx); if (err) goto leave; gpgme_set_progress_cb (ctx, NULL, cld); switch (protocol) { case PROTOCOL_OPENPGP: /* Gpgme's default. */ break; case PROTOCOL_SMIME: err = gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS); break; default: err = gpg_error (GPG_ERR_UNSUPPORTED_PROTOCOL); break; } if (err) goto leave; gpgme_set_armor (ctx, 1); gpgme_set_passphrase_cb (ctx, passphrase_callback_box, &cld->pw_cb); cld->pw_cb.ctx = ctx; cld->pw_cb.ttl = opt.passwd_ttl; err = gpgme_signers_add (ctx, sign_key); if (!err) err = gpgme_op_sign_start (ctx, indata, outdata, GPGME_SIG_MODE_DETACH); leave: if (err) { xfree (cld); gpgme_release (ctx); } else engine_private_set_cancel (filter, ctx); gpgme_key_unref (sign_key); return err; }
int main (int argc, char *argv[]) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; gpgme_key_t key[3] = { NULL, NULL, NULL }; gpgme_encrypt_result_t result; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_armor (ctx, 1); err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0); fail_if_err (err); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_get_key (ctx, "A0FF4590BB6122EDEF6E3C542D727CC768697734", &key[0], 0); fail_if_err (err); err = gpgme_get_key (ctx, "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2", &key[1], 0); fail_if_err (err); err = gpgme_op_encrypt (ctx, key, GPGME_ENCRYPT_ALWAYS_TRUST, in, out); fail_if_err (err); result = gpgme_op_encrypt_result (ctx); if (result->invalid_recipients) { fprintf (stderr, "Invalid recipient encountered: %s\n", result->invalid_recipients->fpr); exit (1); } print_data (out); gpgme_key_unref (key[0]); gpgme_key_unref (key[1]); gpgme_data_release (in); gpgme_data_release (out); gpgme_release (ctx); return 0; }
/** * keys: gpgme_key_t list of keys * * Frees the keys * */ static void free_keys (gpgme_key_t* keys) { gpgme_key_t* k = keys; if (!keys) return; while (*k) gpgme_key_unref (*(k++)); g_free (keys); }
void seahorse_util_free_keys (gpgme_key_t* keys) { gpgme_key_t* k = keys; if (!keys) return; while (*k) gpgme_key_unref (*(k++)); g_free (keys); }
static retvalue check_primary_keys(struct signatures *signatures) { /* Get the primary keys belonging to each signing key. This might also invalidate a signature previously believed valid if the primary key is expired */ int i; for (i = 0 ; i < signatures->count ; i++) { gpg_error_t err; gpgme_key_t gpgme_key = NULL; gpgme_subkey_t subkey; struct signature *sig = &signatures->signatures[i]; if (sig->state == sist_error || sig->state == sist_missing) { sig->primary_keyid = strdup(sig->keyid); if (FAILEDTOALLOC(sig->primary_keyid)) return RET_ERROR_OOM; continue; } err = gpgme_get_key(context, sig->keyid, &gpgme_key, 0); if (err != 0) { fprintf(stderr, "gpgme error %s:%d retrieving key '%s': %s\n", gpg_strsource(err), (int)gpg_err_code(err), sig->keyid, gpg_strerror(err)); if (gpg_err_code(err) == GPG_ERR_ENOMEM) return RET_ERROR_OOM; else return RET_ERROR_GPGME; } assert (gpgme_key != NULL); /* the first "sub"key is the primary key */ subkey = gpgme_key->subkeys; if (subkey->revoked) { sig->revoced_key = true; if (sig->state == sist_valid) { sig->state = sist_mostly; signatures->validcount--; } } if (subkey->expired) { sig->expired_key = true; if (sig->state == sist_valid) { sig->state = sist_mostly; signatures->validcount--; } } sig->primary_keyid = strdup(subkey->keyid); gpgme_key_unref(gpgme_key); if (FAILEDTOALLOC(sig->primary_keyid)) return RET_ERROR_OOM; } return RET_OK; }
bool KGpgMe::encrypt(const QByteArray& inBuffer, Q_ULONG length, QByteArray* outBuffer, QString keyid /* = QString::null */) { gpgme_error_t err = 0; gpgme_data_t in = 0, out = 0; gpgme_key_t keys[2] = { NULL, NULL }; gpgme_key_t* key = NULL; gpgme_encrypt_result_t result = 0; outBuffer->resize(0); if(m_ctx) { err = gpgme_data_new_from_mem(&in, inBuffer.data(), length, 1); if(!err) { err = gpgme_data_new(&out); if(!err) { if(keyid.isNull()) { key = NULL; } else { err = gpgme_get_key(m_ctx, keyid.ascii(), &keys[0], 0); key = keys; } if(!err) { err = gpgme_op_encrypt(m_ctx, key, GPGME_ENCRYPT_ALWAYS_TRUST, in, out); if(!err) { result = gpgme_op_encrypt_result(m_ctx); if (result->invalid_recipients) { KMessageBox::error(kapp->activeWindow(), QString("%1: %2") .arg(i18n("That public key is not meant for encryption")) .arg(result->invalid_recipients->fpr)); } else { err = readToBuffer(out, outBuffer); } } } } } } if(err != GPG_ERR_NO_ERROR && err != GPG_ERR_CANCELED) { KMessageBox::error(kapp->activeWindow(), QString("%1: %2") .arg(gpgme_strsource(err)).arg(gpgme_strerror(err))); } if(err != GPG_ERR_NO_ERROR) clearCache(); if(keys[0]) gpgme_key_unref(keys[0]); if(in) gpgme_data_release(in); if(out) gpgme_data_release(out); return (err == GPG_ERR_NO_ERROR); }
void p_gpg_verify(const char *const barejid, const char *const sign) { if (!sign) { return; } gpgme_ctx_t ctx; gpgme_error_t error = gpgme_new(&ctx); if (error) { log_error("GPG: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error)); return; } char *sign_with_header_footer = _add_header_footer(sign, PGP_SIGNATURE_HEADER, PGP_SIGNATURE_FOOTER); gpgme_data_t sign_data; gpgme_data_new_from_mem(&sign_data, sign_with_header_footer, strlen(sign_with_header_footer), 1); free(sign_with_header_footer); gpgme_data_t plain_data; gpgme_data_new(&plain_data); error = gpgme_op_verify(ctx, sign_data, NULL, plain_data); gpgme_data_release(sign_data); gpgme_data_release(plain_data); if (error) { log_error("GPG: Failed to verify. %s %s", gpgme_strsource(error), gpgme_strerror(error)); gpgme_release(ctx); return; } gpgme_verify_result_t result = gpgme_op_verify_result(ctx); if (result) { if (result->signatures) { gpgme_key_t key = NULL; error = gpgme_get_key(ctx, result->signatures->fpr, &key, 0); if (error) { log_debug("Could not find PGP key with ID %s for %s", result->signatures->fpr, barejid); } else { log_debug("Fingerprint found for %s: %s ", barejid, key->subkeys->fpr); ProfPGPPubKeyId *pubkeyid = malloc(sizeof(ProfPGPPubKeyId)); pubkeyid->id = strdup(key->subkeys->keyid); pubkeyid->received = TRUE; g_hash_table_replace(pubkeys, strdup(barejid), pubkeyid); } gpgme_key_unref(key); } } gpgme_release(ctx); }
char *gpg_encrypt_msg(const char *msg, const char *fpr) { gpgme_ctx_t ctx; gpgme_data_t plain, cipher; gpgme_error_t err; char *temp, *str; char *ret = 0; gpgme_key_t key_arr[2] = {0, 0}; size_t n = 0; err = gpgme_new(&ctx); if (err) { return 0; } gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP); gpgme_set_armor(ctx, 1); err = gpgme_get_key(ctx, fpr, &key_arr[0], 0); if (err) { gpgme_release(ctx); return 0; } err = gpgme_data_new_from_mem(&plain, msg, strlen(msg), 0); if (err) { goto end; } gpgme_data_new(&cipher); err = gpgme_op_encrypt(ctx, key_arr, GPGME_ENCRYPT_ALWAYS_TRUST, plain, cipher); gpgme_data_release(plain); if (err) { gpgme_data_release(cipher); goto end; } temp = gpgme_data_release_and_get_mem(cipher, &n); if (!temp) { goto end; } str = strndup(temp, n); gpgme_free(temp); if (!str) { goto end; } ret = drop_gpg_headers(str); free(str); end: gpgme_key_unref(key_arr[0]); gpgme_release(ctx); return ret; }
static void gpa_backup_operation_finalize (GObject *object) { GpaBackupOperation *op = GPA_BACKUP_OPERATION (object); gpgme_key_unref (op->key); g_free (op->fpr); g_free (op->key_id); G_OBJECT_CLASS (parent_class)->finalize (object); }
void crypto_asym::release_key_list(gpgme_key_t * & ciphering_keys) { if(ciphering_keys != NULL) { for(U_I i = 0; ciphering_keys[i] != NULL; ++i) gpgme_key_unref(ciphering_keys[i]); delete [] ciphering_keys; ciphering_keys = NULL; } }
/* * helper function: unref all keys in the NULL-terminated array keylist and * finally release the array itself */ static void release_keylist(gpgme_key_t * keylist) { gpgme_key_t *key = keylist; while (*key) { gpgme_key_unref(*key); key++; } g_free(keylist); }
static void key_list_free (gpgme_key_t *keys) { gpgme_key_t *key = keys; while (key != NULL) { gpgme_key_unref (*key); key++; } g_free (keys); }
void geanypg_release_keys(encrypt_data * ed) { gpgme_key_t * ptr; if (ed->key_array) { ptr = ed->key_array; while (ptr < ed->key_array + ed->nkeys) gpgme_key_unref(*(ptr++)); free(ed->key_array); ed->key_array = NULL; ed->nkeys = 0; } if (ed->skey_array) { ptr = ed->skey_array; while (ptr < ed->skey_array + ed->nskeys) gpgme_key_unref(*(ptr++)); free(ed->skey_array); ed->skey_array = NULL; ed->nskeys = 0; } }
static void seahorse_gpgme_subkey_finalize (GObject *gobject) { SeahorseGpgmeSubkey *self = SEAHORSE_GPGME_SUBKEY (gobject); /* Unref the key */ if (self->pv->pubkey) gpgme_key_unref (self->pv->pubkey); self->pv->pubkey = NULL; self->pv->subkey = NULL; G_OBJECT_CLASS (seahorse_gpgme_subkey_parent_class)->finalize (gobject); }
static void seahorse_gpgme_uid_object_finalize (GObject *gobject) { SeahorseGpgmeUid *self = SEAHORSE_GPGME_UID (gobject); /* Unref the key */ if (self->pv->pubkey) gpgme_key_unref (self->pv->pubkey); self->pv->pubkey = NULL; self->pv->userid = NULL; G_OBJECT_CLASS (seahorse_gpgme_uid_parent_class)->finalize (gobject); }
/* Default key section. */ static void key_selected_cb (SettingsDlg *dialog) { GList *selected; selected = gpa_key_selector_get_selected_keys (dialog->default_key.list); gpgme_key_unref (dialog->default_key.key); dialog->default_key.key = selected? selected->data : NULL; if (dialog->default_key.key) gpgme_key_ref (dialog->default_key.key); g_list_free (selected); update_modified (dialog, 1); }
static gboolean pkcs7_add_signer (Pkcs7Ctx *pkcs7, const char *signer, GError **err) { gpgme_key_t key = NULL; if (!(key = pkcs7_get_key_by_name (pkcs7, signer, TRUE, err))) return FALSE; /* set the key (the previous operation guaranteed that it exists, no need * 2 check return values...) */ gpgme_signers_add (pkcs7->ctx, key); gpgme_key_unref (key); return TRUE; }
/* Delete all signers from CTX. */ void _gpgme_signers_clear (gpgme_ctx_t ctx) { unsigned int i; if (!ctx || !ctx->signers) return; for (i = 0; i < ctx->signers_len; i++) { assert (ctx->signers[i]); gpgme_key_unref (ctx->signers[i]); ctx->signers[i] = NULL; } ctx->signers_len = 0; }
static void settings_dlg_finalize (GObject *object) { SettingsDlg *dialog = SETTINGS_DLG (object); gpgme_key_unref (dialog->default_key.key); dialog->default_key.key = NULL; xfree (dialog->keyserver.url); dialog->keyserver.url = NULL; xfree (dialog->akl.ip_addr); dialog->akl.ip_addr = NULL; G_OBJECT_CLASS (parent_class)->finalize (object); }
KGpgKeyList KGpgMe::keys(bool privateKeys /* = false */) const { KGpgKeyList keys; gpgme_error_t err = 0, err2 = 0; gpgme_key_t key = 0; gpgme_keylist_result_t result = 0; if(m_ctx) { err = gpgme_op_keylist_start(m_ctx, NULL, privateKeys); if(!err) { while(!(err = gpgme_op_keylist_next(m_ctx, &key))) { KGpgKey gpgkey; if(!key->subkeys) continue; gpgkey.id = key->subkeys->keyid; if(key->uids) { gpgkey.name = key->uids->name; gpgkey.email = key->uids->email; } keys.append(gpgkey); gpgme_key_unref(key); } if (gpg_err_code (err) == GPG_ERR_EOF) err = 0; err2 = gpgme_op_keylist_end(m_ctx); if(!err) err = err2; } } if(err) { KMessageBox::error(kapp->activeWindow(), QString("%1: %2") .arg(gpgme_strsource(err)).arg(gpgme_strerror(err))); } else { result = gpgme_op_keylist_result(m_ctx); if (result->truncated) { KMessageBox::error(kapp->activeWindow(), i18n("Key listing unexpectedly truncated.")); } } return keys; }
/* * Add signer to ctx's list of signers and return TRUE on success or FALSE * on error. */ static gboolean gpgme_add_signer(gpgme_ctx_t ctx, const gchar * signer, GtkWindow * parent, GError ** error) { gpgme_key_t key = NULL; /* note: private (secret) key has never low trust... */ if (! (key = get_key_from_name(ctx, signer, TRUE, FALSE, parent, error))) return FALSE; /* set the key (the previous operation guaranteed that it exists, no * need 2 check return values...) */ gpgme_signers_add(ctx, key); gpgme_key_unref(key); return TRUE; }
void encrypt(const char *message, char *dest, const char *fingerprint) { gpgme_data_t in; gpgme_key_t key[2] = { NULL, NULL }; gpgme_encrypt_result_t result; init_cryptofox(); gpgme_set_armor (_cf_ctx, 1); _cf_err = gpgme_data_new_from_mem (&in, message, strlen(message), 0); fail_if_err (_cf_err); _cf_err = gpgme_data_new (&_cf_out); fail_if_err (_cf_err); // my key ([email protected]) _cf_err = gpgme_get_key (_cf_ctx, fingerprint, &key[0], 0); fail_if_err (_cf_err); // err = gpgme_get_key(ctx,"C0C13F91F6F111E8C66CA6E518A66F16FBFD6A72", // &key[1], 0); // fail_if_err (err); _cf_err = gpgme_op_encrypt (_cf_ctx, key, GPGME_ENCRYPT_ALWAYS_TRUST, in, _cf_out); fail_if_err (_cf_err); result = gpgme_op_encrypt_result (_cf_ctx); if (result->invalid_recipients) { fprintf (stderr, "Invalid recipient encountered: %s\n", result->invalid_recipients->fpr); exit (1); } int ret; ret = gpgme_data_seek(_cf_out, 0, SEEK_SET); if(ret) fail_if_err(gpgme_err_code_from_errno(errno)); gpgme_key_unref (key[0]); gpgme_data_release (in); gpgme_release (_cf_ctx); }
static PyObject * pygpgme_context_get_key(PyGpgmeContext *self, PyObject *args) { const char *fpr; int secret = 0; gpgme_error_t err; gpgme_key_t key; PyObject *ret; if (!PyArg_ParseTuple(args, "s|i", &fpr, &secret)) return NULL; Py_BEGIN_ALLOW_THREADS; err = gpgme_get_key(self->ctx, fpr, &key, secret); Py_END_ALLOW_THREADS; if (pygpgme_check_error(err)) return NULL; ret = pygpgme_key_new(key); gpgme_key_unref(key); return ret; }
int geanypg_get_secret_keys(encrypt_data * ed) { gpgme_error_t err; unsigned long size = SIZE; /* initialize idx to 0 */ unsigned long idx = 0; /* allocate array of size 1N */ gpgme_key_t * key; ed->skey_array = (gpgme_key_t*) malloc(SIZE * sizeof(gpgme_key_t)); err = gpgme_op_keylist_start(ed->ctx, NULL, 1); while (!err) { key = ed->skey_array + idx; err = gpgme_op_keylist_next(ed->ctx, key); if (err) break; if ((*key)->revoked || /* key cannot be used */ (*key)->expired || (*key)->disabled || (*key)->invalid) gpgme_key_unref(*key); else /* key is valid */ ++idx; if (idx >= size) { size += SIZE; ed->skey_array = (gpgme_key_t*) realloc(ed->skey_array, size * sizeof(gpgme_key_t)); } } ed->nskeys = idx; if (gpg_err_code(err) != GPG_ERR_EOF) { geanypg_show_err_msg(err); return 0; } return 1; }