int main (int argc, char *argv[]) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; gpgme_verify_result_t result; char *agent_info; int i; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); agent_info = getenv ("GPG_AGENT_INFO"); if (!(agent_info && strchr (agent_info, ':'))) gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL); 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); for (i = 0; i < sizeof (expected_notations) / sizeof (expected_notations[0]); i++) { err = gpgme_sig_notation_add (ctx, expected_notations[i].name, expected_notations[i].value, expected_notations[i].flags); fail_if_err (err); } err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_NORMAL); fail_if_err (err); gpgme_data_release (in); err = gpgme_data_new (&in); fail_if_err (err); gpgme_data_seek (out, 0, SEEK_SET); err = gpgme_op_verify (ctx, out, NULL, in); fail_if_err (err); result = gpgme_op_verify_result (ctx); check_result (result); gpgme_data_release (in); gpgme_data_release (out); gpgme_release (ctx); return 0; }
int main (int argc, char *argv[]) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t out; const char *pattern1[] = { "DFN Top Level Certification Authority", NULL }; const char *pattern2[] = { "3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E", "DFN Server Certification Authority", NULL }; init_gpgme (GPGME_PROTOCOL_CMS); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS); gpgme_set_armor (ctx, 1); /* Check exporting of one certificate. */ err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_export_ext (ctx, pattern1, 0, out); fail_if_err (err); fflush (NULL); fputs ("Begin Result:\n", stdout); print_data (out); fputs ("End Result.\n", stdout); gpgme_data_release (out); /* Check exporting of 2 certificates. */ err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_export_ext (ctx, pattern2, 0, out); fail_if_err (err); fflush (NULL); fputs ("Begin Result:\n", stdout); print_data (out); fputs ("End Result.\n", stdout); gpgme_data_release (out); gpgme_release (ctx); return 0; }
static gpgme_error_t inq_cb (void *opaque, const char *name, const char *args, gpgme_data_t *r_data) { gpgme_data_t data; gpgme_error_t err; if (name) { printf ("INQ_CB: name=`%s' args=`%s'\n", name, args); /* There shall be no data object. */ assert (!*r_data); err = gpgme_data_new (&data); fail_if_err (err); *r_data = data; printf (" sending data object %p\n", data); } else /* Finished using the formerly returned data object. */ { printf ("INQ_CB: data object %p finished\n", *r_data); /* There shall be a data object so that it can be cleaned up. */ assert (r_data); gpgme_data_release (*r_data); } /* Uncomment the next lines and send a "SCD LEARN" to test sending cancel from in inquiry. */ /* if (name && !strcmp (name, "KNOWNCARDP")) */ /* return gpgme_error (GPG_ERR_ASS_CANCELED); */ return 0; }
int op_export_keys (const char *pattern[], const char *outfile) { /* @untested@ */ gpgme_ctx_t ctx=NULL; gpgme_data_t out=NULL; gpgme_error_t err; err = gpgme_new (&ctx); if (err) return err; err = gpgme_data_new (&out); if (err) { gpgme_release (ctx); return err; } gpgme_set_armor (ctx, 1); err = gpgme_op_export_ext (ctx, pattern, 0, out); if (!err) data_to_file (&out, outfile); gpgme_data_release (out); gpgme_release (ctx); return err; }
char *gpg_decrypt_msg(const char *data, long *plain_size) { gpgme_ctx_t ctx; gpgme_error_t err; size_t n = 0; char *temp, *str, *msg; gpgme_data_t plain, cipher; msg = add_gpg_headers(data); if (!msg) { return 0; } err = gpgme_new(&ctx); if (err) { return 0; } gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP); gpgme_set_armor(ctx, 1); // char *p = getenv("GPG_AGENT_INFO"); // if (p) { // log_write("GPG_AGENT_INFO: %s\n", p); // } else { // setenv("GPG_AGENT_INFO", "/tmp/gpg-3FhMq6/S.gpg-agent:22765:1", 1); // log_write("NO GPG AGENT INFO FOUND\n"); // } gpgme_set_passphrase_cb(ctx, &passphrase_cb, 0); gpgme_data_new_from_mem(&cipher, msg, strlen(msg), 0); gpgme_data_new(&plain); err = gpgme_op_decrypt(ctx, cipher, plain); gpgme_decrypt_result_t res = gpgme_op_decrypt_result(ctx); gpgme_recipient_t recipient = res->recipients; while (recipient) { log_write(">>> recipient keyid: %s\n", recipient->keyid); recipient = recipient->next; } gpgme_data_release(cipher); free(msg); if (err) { log_write("gpgme_op_decrypt error: %s\n", gpgme_strerror(err)); gpgme_data_release(plain); gpgme_release(ctx); return 0; } temp = gpgme_data_release_and_get_mem(plain, &n); if (!temp) { gpgme_release(ctx); return 0; } *plain_size = n; str = strndup(temp, n); free(temp); gpgme_release(ctx); return str; }
/** * seahorse_service_crypto_sign_text: * @crypto: SeahorseServiceCrypto * @signer: the signer keyid * @flags: 0 (ignored) * @cleartext: the text to sign * @crypttext: the clear text signature (out) (GPGME_SIG_MODE_CLEAR) * @error: an error to return * * DBus: SignText * * Signs the @cleartext and returns the signature in @crypttext * * Returns: TRUE on success */ gboolean seahorse_service_crypto_sign_text (SeahorseServiceCrypto *crypto, const char *signer, int flags, const char *cleartext, char **crypttext, GError **error) { SeahorseObject *signkey = NULL; gpgme_error_t gerr; SeahorseGpgmeOperation *pop; gpgme_data_t plain, cipher; gboolean ret = TRUE; /* * TODO: Once we support different kinds of keys that support encryption * then all this logic will need to change. */ /* The signer */ if (!signer || !signer[0]) g_set_error (error, SEAHORSE_DBUS_ERROR, SEAHORSE_DBUS_ERROR_INVALID, _("No signer specified")); signkey = seahorse_context_object_from_dbus (SCTX_APP (), signer); if (!signkey) { g_set_error (error, SEAHORSE_DBUS_ERROR, SEAHORSE_DBUS_ERROR_INVALID, _("Invalid or unrecognized signer: %s"), signer); return FALSE; } if (!SEAHORSE_IS_GPGME_KEY (signkey) || !(seahorse_object_get_flags (signkey) & SEAHORSE_FLAG_CAN_SIGN)) { g_set_error (error, SEAHORSE_DBUS_ERROR, SEAHORSE_DBUS_ERROR_INVALID, _("Key is not valid for signing: %s"), signer); return FALSE; } pop = seahorse_gpgme_operation_new (NULL); /* new data from text */ gerr = gpgme_data_new_from_mem (&plain, cleartext, strlen (cleartext), FALSE); g_return_val_if_fail (GPG_IS_OK (gerr), FALSE); gerr = gpgme_data_new (&cipher); g_return_val_if_fail (GPG_IS_OK (gerr), FALSE); /* encrypt with armor */ gpgme_set_textmode (pop->gctx, TRUE); gpgme_set_armor (pop->gctx, TRUE); /* Do the signage */ gpgme_signers_add (pop->gctx, seahorse_gpgme_key_get_private (SEAHORSE_GPGME_KEY (signkey))); gerr = gpgme_op_sign_start (pop->gctx, plain, cipher, GPGME_SIG_MODE_CLEAR); /* Frees cipher */ ret = process_crypto_result (pop, gerr, cipher, crypttext, NULL, error); g_object_unref (pop); gpgme_data_release (plain); return ret; }
int main (int argc, char *argv[]) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; gpgme_sign_result_t result; init_gpgme (GPGME_PROTOCOL_CMS); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS); gpgme_set_textmode (ctx, 1); gpgme_set_armor (ctx, 1); err = gpgme_data_new_from_mem (&in, "Hallo Leute!\n", 13, 0); fail_if_err (err); /* First a normal signature. */ err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_NORMAL); fail_if_err (err); result = gpgme_op_sign_result (ctx); check_result (result, GPGME_SIG_MODE_NORMAL); print_data (out); gpgme_data_release (out); /* Now a detached signature. */ gpgme_data_seek (in, 0, SEEK_SET); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_DETACH); fail_if_err (err); result = gpgme_op_sign_result (ctx); check_result (result, GPGME_SIG_MODE_DETACH); print_data (out); gpgme_data_release (out); gpgme_data_release (in); gpgme_release (ctx); return 0; }
/* ------------------ * get ascii armored public key * FREE MEMORY AFTER USAGE OF RETURN VALUE! * ------------------ */ char* get_key_armored(const char* fpr) { gpgme_error_t error; gpgme_ctx_t ctx; gpgme_data_t key_data; gpgme_key_t key; gpgme_key_t key_arr[2]; key_arr[0] = key_arr[1] = NULL; size_t len = 0; char* key_str = NULL; char* key_str_dup = NULL; // connect to gpgme gpgme_check_version (NULL); error = gpgme_new(&ctx); if (error) { purple_debug_error(PLUGIN_ID,"gpgme_new failed: %s %s\n",gpgme_strsource (error), gpgme_strerror (error)); return NULL; } // get key by fingerprint error = gpgme_get_key(ctx,fpr,&key,0); if (error || !key) { purple_debug_error(PLUGIN_ID,"gpgme_get_key failed: %s %s\n",gpgme_strsource (error), gpgme_strerror (error)); gpgme_release (ctx); return NULL; } key_arr[0] = key; // create data containers gpgme_data_new(&key_data); // export key gpgme_set_armor(ctx,1); error = gpgme_op_export_keys (ctx, key_arr, 0, key_data); if (error) { purple_debug_error(PLUGIN_ID,"gpgme_op_export_keys failed: %s %s\n",gpgme_strsource (error), gpgme_strerror (error)); gpgme_release (ctx); return NULL; } // release memory for data containers key_str = gpgme_data_release_and_get_mem(key_data,&len); if (key_str != NULL) { key_str[len] = 0; key_str_dup = g_strdup(key_str); } gpgme_free(key_str); // close gpgme connection gpgme_release (ctx); // we got the key, YEAH :) return key_str_dup; }
/* ------------------ * verify a signed string with the key found with fingerprint fpr * FREE MEMORY AFTER USAGE OF RETURN VALUE! * ------------------ */ static char* verify(const char* sig_str) { gpgme_error_t error; gpgme_ctx_t ctx; gpgme_data_t plain,sig,sig_text; gpgme_verify_result_t result; char* fpr = NULL; char* armored_sig_str = NULL; if (sig_str == NULL) { purple_debug_error(PLUGIN_ID,"verify got null parameter\n"); return NULL; } // connect to gpgme gpgme_check_version (NULL); error = gpgme_new(&ctx); if (error) { purple_debug_error(PLUGIN_ID,"gpgme_new failed: %s %s\n",gpgme_strsource (error), gpgme_strerror (error)); return NULL; } // armor sig_str armored_sig_str = str_armor(sig_str); // create data containers gpgme_data_new_from_mem (&sig, armored_sig_str,strlen(armored_sig_str),1); gpgme_data_new(&plain); // try to verify error = gpgme_op_verify(ctx,sig,NULL,plain); if (error) { purple_debug_error(PLUGIN_ID,"gpgme_op_verify failed: %s %s\n",gpgme_strsource (error), gpgme_strerror (error)); gpgme_release (ctx); return NULL; } // get result result = gpgme_op_verify_result (ctx); if (result != NULL) { if (result->signatures != NULL) { // return the fingerprint of the key that made the signature fpr = g_strdup(result->signatures->fpr); } } // release memory for data containers gpgme_data_release(sig); gpgme_data_release(plain); return fpr; }
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); }
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; }
JNIEXPORT jlong JNICALL Java_com_freiheit_gnupg_GnuPGData_gpgmeDataNew(JNIEnv * env, jobject self) { gpgme_data_t data; gpgme_error_t err = gpgme_data_new(&data); if (UTILS_onErrorThrowException(env, err)) { return LNG(NULL); } return LNG(data); }
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); }
/* ------------------ * decrypt a plain string with the key found with fingerprint fpr * FREE MEMORY AFTER USAGE OF RETURN VALUE * ------------------ */ static char* decrypt(char* cipher_str) { gpgme_error_t error; gpgme_ctx_t ctx; gpgme_data_t plain,cipher; size_t len = 0; char* plain_str = NULL; char* plain_str_dup = NULL; char* armored_buffer; // add header and footer: armored_buffer = str_armor(cipher_str); // connect to gpgme gpgme_check_version (NULL); error = gpgme_new(&ctx); if (error) { purple_debug_error(PLUGIN_ID,"gpgme_new failed: %s %s\n",gpgme_strsource (error), gpgme_strerror (error)); return NULL; } // create data containers gpgme_data_new_from_mem (&cipher, armored_buffer,strlen(armored_buffer),1); gpgme_data_new(&plain); // decrypt error = gpgme_op_decrypt(ctx,cipher,plain); if (error) { purple_debug_error(PLUGIN_ID,"gpgme_op_decrypt failed: %s %s\n",gpgme_strsource (error), gpgme_strerror (error)); gpgme_release (ctx); return NULL; } // release memory for data containers gpgme_data_release(cipher); plain_str = gpgme_data_release_and_get_mem(plain,&len); if (plain_str != NULL) { plain_str[len] = 0; plain_str_dup = g_strdup(plain_str); } gpgme_free(plain_str); // close gpgme connection gpgme_release (ctx); return plain_str_dup; }
SEXP R_gpg_export(SEXP id, SEXP secret){ gpgme_data_t keydata = NULL; bail(gpgme_data_new(&keydata), "initiatie keydata"); #ifdef GPGME_EXPORT_MODE_SECRET gpgme_export_mode_t mode = asLogical(secret) * GPGME_EXPORT_MODE_SECRET; #else int mode = 0; #ifndef CHECK_OLD_GPGME if(asLogical(secret)) Rf_error("gpgme is too old, GPGME_EXPORT_MODE_SECRET not supported"); #endif #endif bail(gpgme_op_export(ctx, CHAR(STRING_ELT(id, 0)), mode, keydata), "export key"); return data_to_string(keydata); }
char *CGPGHelper::Decrypt(const char *szCipher) { #ifdef HAVE_LIBGPGME if (!mCtx) return 0; size_t nRead = 0; gpgme_data_t cipher, plain; CGPGMEMutex mutex; if (!mutex.Lock()) return 0; if (gpgme_data_new(&cipher) != GPG_ERR_NO_ERROR) return 0; char *buf = strdup(szCipher); gpgme_error_t err; gpgme_data_write(cipher, buf, strlen(buf)); free(buf); if (gpgme_data_new(&plain) != GPG_ERR_NO_ERROR) { gpgme_data_release(cipher); return 0; } gpgme_data_seek(cipher, 0, SEEK_SET); if ((err = gpgme_op_decrypt(mCtx, cipher, plain)) != GPG_ERR_NO_ERROR) gLog.Warn("%s[GPG] gpgme message decryption failed: %s\n", L_WARNxSTR, gpgme_strerror(err)); gpgme_data_release(cipher); buf = gpgme_data_release_and_get_mem(plain, &nRead); if (!buf) return 0; buf = (char *)realloc(buf, nRead+1); buf[nRead] = 0; return buf; #else return 0; #endif }
/* Change the expire date of a key. */ gpg_error_t gpa_gpgme_edit_expire_start (GpaContext *ctx, gpgme_key_t key, GDate *date) { struct edit_parms_s *parms; gpg_error_t err; gpgme_data_t out = NULL; err = gpgme_data_new (&out); if (gpg_err_code (err) != GPG_ERR_NO_ERROR) { return err; } parms = gpa_gpgme_edit_expire_parms_new (ctx, date, out); err = gpgme_op_edit_start (ctx->ctx, key, edit_fnc, parms, out); return err; }
int main (int argc, char *argv[]) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; gpgme_decrypt_result_t decrypt_result; gpgme_verify_result_t verify_result; char *cipher_2_asc = make_filename ("cipher-2.asc"); char *agent_info; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); agent_info = getenv("GPG_AGENT_INFO"); if (!(agent_info && strchr (agent_info, ':'))) gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL); err = gpgme_data_new_from_file (&in, cipher_2_asc, 1); free (cipher_2_asc); fail_if_err (err); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_decrypt_verify (ctx, in, out); fail_if_err (err); decrypt_result = gpgme_op_decrypt_result (ctx); if (decrypt_result->unsupported_algorithm) { fprintf (stderr, "%s:%i: unsupported algorithm: %s\n", __FILE__, __LINE__, decrypt_result->unsupported_algorithm); exit (1); } print_data (out); verify_result = gpgme_op_verify_result (ctx); check_verify_result (verify_result, 0, "A0FF4590BB6122EDEF6E3C542D727CC768697734", GPG_ERR_NO_ERROR); gpgme_data_release (in); gpgme_data_release (out); gpgme_release (ctx); return 0; }
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; }
int cgpg_encrypt(char *nick, char *msg, char buf[], int len, char errmsg[EOF_L_MESSAGE]) { // gpgme_error_t gerr; gpgme_key_t keyid[2]; gpgme_data_t plaintext; gpgme_data_t encrypted; char *p; /* retrieve keyid */ p = peer_keyid_get(nick); if(!p) { eof_errmsg("No key-ID registered"); return 0; } keyid[1] = NULL; printf("encrypting with keyid=%s\n", p); if(!cgpg_keyid_get(p, keyid, errmsg)) { return 0; } printf("Key info: %s <%s>\n", keyid[0]->uids->name, keyid[0]->uids->email); /* allocate buffer: non copying! */ if(GPG_ERR_NO_ERROR != gpgme_data_new_from_mem(&plaintext, msg, strlen(msg), 0)) return 0; if(GPG_ERR_NO_ERROR != gpgme_data_new(&encrypted)) return 0; /* encrypt buffer */ /* FIXME: remove GPGME_ENCRYPT_ALWAYS_TRUST and use trustlevels */ if(GPG_ERR_NO_ERROR != gpgme_op_encrypt(gpg_context, keyid, GPGME_ENCRYPT_ALWAYS_TRUST, plaintext, encrypted)) { eof_errmsg("Encryption failed"); return 0; } /* return result */ if(GPG_ERR_NO_ERROR != gpgme_data_seek(encrypted, 0, SEEK_SET)) { eof_errmsg("gpgme_data_seek"); return 0; } len = gpgme_data_read(encrypted, buf, len); buf[len] = '\0'; return len; }
int export_key(gpgme_data_t *out, char *key_id, GPG_CTX *gpg_ctx) { // validation if (key_id == NULL) error_ret("Key to connect with is required\n", ERROR_BAD_INPUT); gpgme_key_t key; int ret; bool confirm; gpgme_error_t err; // find key to register ret = GPG_get_key(gpg_ctx, key_id, &key); if (is_failure(ret)) error_ret("Could not corresponding key\n", ERROR_GPGME); printf("Going to connect with %s (%s <%s>)\n", key->subkeys->keyid, key->uids->name, key->uids->email ); // ensure key is valid if (!key->can_encrypt || key->revoked || key->disabled || key->expired) error_ret("This key cannot be used\n", ERROR_GPGME); // confirm confirm = request_confirmation("OK to proceed?"); if (!confirm) { puts("Aborting"); return ERROR_USER_ABORT; } // get key err = gpgme_data_new(out); if (gpg_err_code(err) != GPG_ERR_NO_ERROR) error_ret("Failed to allocate key buffer\n", ERROR_GPGME); ret = GPG_export(gpg_ctx, key, *out); if (is_failure(ret)) error_ret("Failed to export key\n", ERROR_GPGME); return ERROR_NO_ERROR; }
gpg_error_t gpa_gpgme_card_edit_genkey_start (GpaContext *ctx, gpa_keygen_para_t *genkey_parms) { struct edit_parms_s *edit_parms; gpgme_data_t out = NULL; gpg_error_t err; err = gpgme_data_new (&out); if (err) return err; edit_parms = card_edit_genkey_parms_new (ctx, genkey_parms, out); err = gpgme_op_card_edit_start (ctx->ctx, NULL, edit_fnc, edit_parms, out); return err; }
/* Change the ownertrust of a key. */ gpg_error_t gpa_gpgme_edit_trust_start (GpaContext *ctx, gpgme_key_t key, gpgme_validity_t ownertrust) { const gchar *trust_strings[] = {"1", "1", "2", "3", "4", "5"}; struct edit_parms_s *parms = NULL; gpg_error_t err; gpgme_data_t out = NULL; err = gpgme_data_new (&out); if (gpg_err_code (err) != GPG_ERR_NO_ERROR) { return err; } parms = gpa_gpgme_edit_trust_parms_new (ctx, trust_strings[ownertrust], out); err = gpgme_op_edit_start (ctx->ctx, key, edit_fnc, parms, out); return err; }
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 void show_auditlog (gpgme_ctx_t ctx) { gpgme_error_t err; gpgme_data_t data; err = gpgme_data_new (&data); fail_if_err (err); err = gpgme_op_getauditlog (ctx, data, 0); if (err) { fprintf (stderr, "%s:%i: Can't get audit log: %s\n", __FILE__, __LINE__, gpgme_strerror (err)); if (gpgme_err_code (err) != GPG_ERR_ASS_UNKNOWN_CMD) got_errors = 1; } print_data (data); gpgme_data_release (data); }
QString QalfCrypto::getPublicKey(QString &key) { // exporting public key gpgme_data_t keyData ; gpgme_error_t result = gpgme_data_new(&keyData); Q_ASSERT(result == GPG_ERR_NO_ERROR) ; result = gpgme_op_export(context,key.toLocal8Bit(),0,keyData) ; Q_ASSERT(result == GPG_ERR_NO_ERROR) ; QString publicKeyStr ; char * buffer = (char *) calloc(4096,sizeof(char)) ; // gpgme_data_rewind(keyData) ; gpgme_data_seek(keyData,0,SEEK_SET) ; gpgme_data_read(keyData,buffer,4096) ; publicKeyStr += buffer ; return publicKeyStr ; }
int main (int argc, char *argv[]) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; gpgme_decrypt_result_t result; char *cipher_1_asc = make_filename ("cipher-1.asc"); char *agent_info; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); agent_info = getenv("GPG_AGENT_INFO"); if (!(agent_info && strchr (agent_info, ':'))) gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL); err = gpgme_data_new_from_file (&in, cipher_1_asc, 1); free (cipher_1_asc); fail_if_err (err); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_decrypt (ctx, in, out); fail_if_err (err); result = gpgme_op_decrypt_result (ctx); if (result->unsupported_algorithm) { fprintf (stderr, "%s:%i: unsupported algorithm: %s\n", __FILE__, __LINE__, result->unsupported_algorithm); exit (1); } print_data (out); gpgme_data_release (in); gpgme_data_release (out); gpgme_release (ctx); return 0; }
bool KGpgMe::decrypt(const QByteArray& inBuffer, QByteArray* outBuffer) { gpgme_error_t err = 0; gpgme_data_t in = 0, out = 0; gpgme_decrypt_result_t result = 0; outBuffer->resize(0); if(m_ctx) { err = gpgme_data_new_from_mem(&in, inBuffer.data(), inBuffer.size(), 1); if(!err) { err = gpgme_data_new(&out); if(!err) { err = gpgme_op_decrypt(m_ctx, in, out); if(!err) { result = gpgme_op_decrypt_result(m_ctx); if(result->unsupported_algorithm) { KMessageBox::error(kapp->activeWindow(), QString("%1: %2") .arg(i18n("Unsupported algorithm")) .arg(result->unsupported_algorithm)); } 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(in) gpgme_data_release(in); if(out) gpgme_data_release(out); return (err == GPG_ERR_NO_ERROR); }
gpgme_data_t seahorse_gpgme_data_new () { gpgme_error_t gerr; gpgme_data_t data; gerr = gpgme_data_new (&data); if (!GPG_IS_OK (gerr)) { if (gpgme_err_code_to_errno (gerr) == ENOMEM || gpgme_err_code (gerr) == GPG_ERR_ENOMEM) { g_error ("%s: failed to allocate gpgme_data_t", G_STRLOC); } else { /* The only reason this should fail is above */ g_assert_not_reached (); /* Just in case */ abort (); } } return data; }
gpg_error_t gpa_gpgme_edit_passwd_start (GpaContext *ctx, gpgme_key_t key) { struct edit_parms_s *parms; gpg_error_t err; gpgme_data_t out; err = gpgme_data_new (&out); if (gpg_err_code (err) != GPG_ERR_NO_ERROR) { return err; } parms = gpa_gpgme_edit_passwd_parms_new (ctx, out); /* Use our own passphrase callback: The data are the actual edit parms. */ if (!cms_hack) gpgme_set_passphrase_cb (ctx->ctx, passwd_passphrase_cb, parms); err = gpgme_op_edit_start (ctx->ctx, key, edit_fnc, parms, out); return err; }