/* Set the engine info for the context CTX, protocol PROTO, to the file name FILE_NAME and the home directory HOME_DIR. */ gpgme_error_t gpgme_ctx_set_engine_info (gpgme_ctx_t ctx, gpgme_protocol_t proto, const char *file_name, const char *home_dir) { gpgme_error_t err; TRACE_BEG4 (DEBUG_CTX, "gpgme_ctx_set_engine_info", ctx, "protocol=%i (%s), file_name=%s, home_dir=%s", proto, gpgme_get_protocol_name (proto) ? gpgme_get_protocol_name (proto) : "unknown", file_name ? file_name : "(default)", home_dir ? home_dir : "(default)"); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); /* Shut down the engine when changing engine info. */ if (ctx->engine) { TRACE_LOG1 ("releasing ctx->engine=%p", ctx->engine); _gpgme_engine_release (ctx->engine); ctx->engine = NULL; } err = _gpgme_set_engine_info (ctx->engine_info, proto, file_name, home_dir); return TRACE_ERR (err); }
gpgme_error_t gpgme_set_protocol (gpgme_ctx_t ctx, gpgme_protocol_t protocol) { TRACE_BEG2 (DEBUG_CTX, "gpgme_set_protocol", ctx, "protocol=%i (%s)", protocol, gpgme_get_protocol_name (protocol) ? gpgme_get_protocol_name (protocol) : "invalid"); if (protocol != GPGME_PROTOCOL_OpenPGP && protocol != GPGME_PROTOCOL_CMS && protocol != GPGME_PROTOCOL_GPGCONF && protocol != GPGME_PROTOCOL_ASSUAN && protocol != GPGME_PROTOCOL_G13 && protocol != GPGME_PROTOCOL_UISERVER && protocol != GPGME_PROTOCOL_SPAWN) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (ctx->protocol != protocol) { /* Shut down the engine when switching protocols. */ if (ctx->engine) { TRACE_LOG1 ("releasing ctx->engine=%p", ctx->engine); _gpgme_engine_release (ctx->engine); ctx->engine = NULL; } ctx->protocol = protocol; } return TRACE_ERR (0); }
gpgme_protocol_t gpgme_get_sub_protocol (gpgme_ctx_t ctx) { TRACE2 (DEBUG_CTX, "gpgme_get_sub_protocol", ctx, "ctx->sub_protocol=%i (%s)", ctx->sub_protocol, gpgme_get_protocol_name (ctx->sub_protocol) ? gpgme_get_protocol_name (ctx->sub_protocol) : "invalid"); return ctx->sub_protocol; }
gpgme_error_t gpgme_set_sub_protocol (gpgme_ctx_t ctx, gpgme_protocol_t protocol) { TRACE2 (DEBUG_CTX, "gpgme_set_sub_protocol", ctx, "protocol=%i (%s)", protocol, gpgme_get_protocol_name (protocol) ? gpgme_get_protocol_name (protocol) : "invalid"); if (!ctx) return gpg_error (GPG_ERR_INV_VALUE); ctx->sub_protocol = protocol; return 0; }
/* * create a GpgME context for the passed protocol */ static gpgme_error_t gpgme_new_with_protocol(gpgme_ctx_t * ctx, gpgme_protocol_t protocol, GtkWindow * parent, GError ** error) { gpgme_error_t err; /* create the GpgME context */ if ((err = gpgme_new(ctx)) != GPG_ERR_NO_ERROR) { g_set_error_from_gpgme(error, err, _("could not create context")); } else { if ((err = gpgme_set_protocol(*ctx, protocol)) != GPG_ERR_NO_ERROR) { gchar *errmsg = g_strdup_printf(_("could not set protocol '%s'"), gpgme_get_protocol_name(protocol)); g_set_error_from_gpgme(error, err, errmsg); g_free(errmsg); gpgme_release(*ctx); } else { if (protocol == GPGME_PROTOCOL_OpenPGP) gpgme_set_passphrase_cb(*ctx, gpgme_passphrase_cb, parent); } } return err; }
int setup(gpgme_ctx_t* ctx) { char *p; gpgme_error_t err; gpgme_engine_info_t enginfo; setlocale (LC_ALL, ""); p = (char *) gpgme_check_version(NULL); printf("version=%s\n",p); /* set locale, because tests do also */ gpgme_set_locale(NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL)); /* check for OpenPGP support */ err = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP); if(err != GPG_ERR_NO_ERROR) return 1; p = (char *) gpgme_get_protocol_name(GPGME_PROTOCOL_OpenPGP); printf("Protocol name: %s\n",p); /* get engine information */ err = gpgme_get_engine_info(&enginfo); if(err != GPG_ERR_NO_ERROR) return 2; printf("file=%s, home=%s\n",enginfo->file_name,enginfo->home_dir); /* create our own context */ err = gpgme_new(ctx); if(err != GPG_ERR_NO_ERROR) return 3; /* set protocol to use in our context */ err = gpgme_set_protocol(*ctx,GPGME_PROTOCOL_OpenPGP); if(err != GPG_ERR_NO_ERROR) return 4; /* set engine info in our context; I changed it for ceof like this: err = gpgme_ctx_set_engine_info (*ctx, GPGME_PROTOCOL_OpenPGP, "/usr/bin/gpg","/home/user/nico/.ceof/gpg/"); but I'll use standard values for this example: */ err = gpgme_ctx_set_engine_info (*ctx, GPGME_PROTOCOL_OpenPGP, enginfo->file_name,enginfo->home_dir); if(err != GPG_ERR_NO_ERROR) return 5; return 0; }
void sgpgme_init() { gchar *ctype_locale = NULL, *messages_locale = NULL; gchar *ctype_utf8_locale = NULL, *messages_utf8_locale = NULL; gpgme_engine_info_t engineInfo; if (gpgme_check_version("1.0.0")) { #ifdef LC_CTYPE debug_print("setting gpgme CTYPE locale\n"); #ifdef G_OS_WIN32 ctype_locale = g_win32_getlocale(); #else ctype_locale = g_strdup(setlocale(LC_CTYPE, NULL)); #endif if (ctype_locale) { debug_print("setting gpgme CTYPE locale to: %s\n", ctype_locale); if (strchr(ctype_locale, '.')) *(strchr(ctype_locale, '.')) = '\0'; else if (strchr(ctype_locale, '@')) *(strchr(ctype_locale, '@')) = '\0'; ctype_utf8_locale = g_strconcat(ctype_locale, ".UTF-8", NULL); debug_print("setting gpgme locale to UTF8: %s\n", ctype_utf8_locale ? ctype_utf8_locale : "NULL"); gpgme_set_locale(NULL, LC_CTYPE, ctype_utf8_locale); debug_print("done\n"); g_free(ctype_utf8_locale); g_free(ctype_locale); } else { debug_print("couldn't set gpgme CTYPE locale\n"); } #endif #ifdef LC_MESSAGES debug_print("setting gpgme MESSAGES locale\n"); #ifdef G_OS_WIN32 messages_locale = g_win32_getlocale(); #else messages_locale = g_strdup(setlocale(LC_MESSAGES, NULL)); #endif if (messages_locale) { debug_print("setting gpgme MESSAGES locale to: %s\n", messages_locale); if (strchr(messages_locale, '.')) *(strchr(messages_locale, '.')) = '\0'; else if (strchr(messages_locale, '@')) *(strchr(messages_locale, '@')) = '\0'; messages_utf8_locale = g_strconcat(messages_locale, ".UTF-8", NULL); debug_print("setting gpgme locale to UTF8: %s\n", messages_utf8_locale ? messages_utf8_locale : "NULL"); gpgme_set_locale(NULL, LC_MESSAGES, messages_utf8_locale); debug_print("done\n"); g_free(messages_utf8_locale); g_free(messages_locale); } else { debug_print("couldn't set gpgme MESSAGES locale\n"); } #endif if (!gpgme_get_engine_info(&engineInfo)) { while (engineInfo) { debug_print("GpgME Protocol: %s\n" "Version: %s (req %s)\n" "Executable: %s\n", gpgme_get_protocol_name(engineInfo->protocol) ? gpgme_get_protocol_name(engineInfo->protocol):"???", engineInfo->version ? engineInfo->version:"???", engineInfo->req_version ? engineInfo->req_version:"???", engineInfo->file_name ? engineInfo->file_name:"???"); if (engineInfo->protocol == GPGME_PROTOCOL_OpenPGP && gpgme_engine_check_version(engineInfo->protocol) != GPG_ERR_NO_ERROR) { if (engineInfo->file_name && !engineInfo->version) { alertpanel_error(_("Gpgme protocol '%s' is unusable: " "Engine '%s' isn't installed properly."), gpgme_get_protocol_name(engineInfo->protocol), engineInfo->file_name); } else if (engineInfo->file_name && engineInfo->version && engineInfo->req_version) { alertpanel_error(_("Gpgme protocol '%s' is unusable: " "Engine '%s' version %s is installed, " "but version %s is required.\n"), gpgme_get_protocol_name(engineInfo->protocol), engineInfo->file_name, engineInfo->version, engineInfo->req_version); } else { alertpanel_error(_("Gpgme protocol '%s' is unusable " "(unknown problem)"), gpgme_get_protocol_name(engineInfo->protocol)); } } engineInfo = engineInfo->next; } } } else { sgpgme_disable_all(); if (prefs_gpg_get_config()->gpg_warning) { AlertValue val; val = alertpanel_full (_("Warning"), _("GnuPG is not installed properly, or needs " "to be upgraded.\n" "OpenPGP support disabled."), GTK_STOCK_CLOSE, NULL, NULL, TRUE, NULL, ALERT_WARNING, G_ALERTDEFAULT); if (val & G_ALERTDISABLE) prefs_gpg_get_config()->gpg_warning = FALSE; } } }
int main (int argc, char **argv) { gpgme_error_t err; gpgme_ctx_t ctx; gpgme_key_t key; gpgme_keylist_result_t result; int i = 0; init_gpgme (GPGME_PROTOCOL_CMS); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS); err = gpgme_op_keylist_start (ctx, NULL, 0); fail_if_err (err); while (!(err = gpgme_op_keylist_next (ctx, &key))) { if (!keys[i].fpr) { fprintf (stderr, "More keys returned than expected\n"); exit (1); } if (strcmp (key->subkeys->fpr, keys[i].fpr)) { fprintf (stderr, "Warning: Skipping unknown key %s\n", key->subkeys->fpr); continue; } else printf ("Checking key %s\n", key->subkeys->fpr); /* Global key flags. */ if (key->revoked) { fprintf (stderr, "Key unexpectedly revoked\n"); exit (1); } if (key->expired) { fprintf (stderr, "Key unexpectedly expired\n"); exit (1); } if (key->disabled) { fprintf (stderr, "Key unexpectedly disabled\n"); exit (1); } if (key->invalid) { fprintf (stderr, "Key unexpectedly invalid\n"); exit (1); } if (key->can_encrypt != keys[i].secret) { fprintf (stderr, "Key unexpectedly%s usable for encryption\n", key->can_encrypt ? "" : " not"); exit (1); } if (key->can_sign != keys[i].secret) { fprintf (stderr, "Key unexpectedly%s usable for signing\n", key->can_sign ? "" : " not"); exit (1); } if (!key->can_certify) { fprintf (stderr, "Key unexpectedly unusable for certifications\n"); exit (1); } if (key->secret != keys[i].secret) { fprintf (stderr, "Key unexpectedly%s secret\n", key->secret ? "" : " not"); exit (1); } if (key->protocol != GPGME_PROTOCOL_CMS) { fprintf (stderr, "Key has unexpected protocol: %s\n", gpgme_get_protocol_name (key->protocol)); exit (1); } if (!key->issuer_serial) { fprintf (stderr, "Key unexpectedly misses issuer serial\n"); exit (1); } if (strcmp (key->issuer_serial, keys[i].issuer_serial)) { fprintf (stderr, "Key has unexpected issuer serial: %s\n", key->issuer_serial); exit (1); } if (!key->issuer_name) { fprintf (stderr, "Key unexpectedly misses issuer name\n"); exit (1); } if (strcmp (key->issuer_name, keys[i].issuer_name)) { fprintf (stderr, "Key has unexpected issuer name: %s\n", key->issuer_name); exit (1); } if (key->chain_id && !keys[i].chain_id) { fprintf (stderr, "Key unexpectedly carries chain ID: %s\n", key->chain_id); exit (1); } if (!key->chain_id && keys[i].chain_id) { fprintf (stderr, "Key unexpectedly carries no chain ID\n"); exit (1); } if (key->chain_id && strcmp (key->chain_id, keys[i].chain_id)) { fprintf (stderr, "Key carries unexpected chain ID: %s\n", key->chain_id); exit (1); } if (key->owner_trust != GPGME_VALIDITY_UNKNOWN) { fprintf (stderr, "Key has unexpected owner trust: %i\n", key->owner_trust); exit (1); } if (!key->subkeys || key->subkeys->next) { fprintf (stderr, "Key has unexpected number of subkeys\n"); exit (1); } /* Primary key. */ if (key->subkeys->revoked) { fprintf (stderr, "Primary key unexpectedly revoked\n"); exit (1); } if (key->subkeys->expired) { fprintf (stderr, "Primary key unexpectedly expired\n"); exit (1); } if (key->subkeys->disabled) { fprintf (stderr, "Primary key unexpectedly disabled\n"); exit (1); } if (key->subkeys->invalid) { fprintf (stderr, "Primary key unexpectedly invalid\n"); exit (1); } if (key->subkeys->can_encrypt != keys[i].secret) { fprintf (stderr, "Key unexpectedly%s usable for encryption\n", key->subkeys->can_encrypt ? "" : " not"); exit (1); } if (key->subkeys->can_sign != keys[i].secret) { fprintf (stderr, "Key unexpectedly%s usable for signing\n", key->subkeys->can_sign ? "" : " not"); exit (1); } if (!key->subkeys->can_certify) { fprintf (stderr, "Primary key unexpectedly unusable for certifications\n"); exit (1); } if (key->subkeys->secret != keys[i].secret) { fprintf (stderr, "Primary Key unexpectedly%s secret\n", key->secret ? "" : " not"); exit (1); } if (key->subkeys->pubkey_algo != GPGME_PK_RSA) { fprintf (stderr, "Primary key has unexpected public key algo: %s\n", gpgme_pubkey_algo_name (key->subkeys->pubkey_algo)); exit (1); } if (key->subkeys->length != keys[i].key_length) { fprintf (stderr, "Primary key has unexpected length: %i\n", key->subkeys->length); exit (1); } if (strcmp (key->subkeys->keyid, &keys[i].fpr[40 - 16])) { fprintf (stderr, "Primary key has unexpected key ID: %s\n", key->subkeys->keyid); exit (1); } if (strcmp (key->subkeys->fpr, keys[i].fpr)) { fprintf (stderr, "Primary key has unexpected fingerprint: %s\n", key->subkeys->fpr); exit (1); } if (key->subkeys->timestamp != keys[i].timestamp) { fprintf (stderr, "Primary key unexpected timestamp: %lu\n", key->subkeys->timestamp); exit (1); } if (key->subkeys->expires != keys[i].expires) { fprintf (stderr, "Primary key unexpectedly expires: %lu\n", key->subkeys->expires); exit (1); } /* Be tolerant against a missing email (ie, older gpgsm versions). */ if (!key->uids || (key->uids->next && !keys[i].email)) { fprintf (stderr, "Key has unexpected number of user IDs\n"); exit (1); } if (key->uids->revoked) { fprintf (stderr, "User ID unexpectedly revoked\n"); exit (1); } if (key->uids->invalid) { fprintf (stderr, "User ID unexpectedly invalid\n"); exit (1); } if (key->uids->validity != keys[i].validity) { fprintf (stderr, "User ID unexpectedly validity: %i\n", key->uids->validity); exit (1); } if (key->uids->signatures) { fprintf (stderr, "User ID unexpectedly signed\n"); exit (1); } if (!key->uids->name || key->uids->name[0]) { fprintf (stderr, "Unexpected name in user ID: %s\n", key->uids->name); exit (1); } if (!key->uids->comment || key->uids->comment[0]) { fprintf (stderr, "Unexpected comment in user ID: %s\n", key->uids->comment); exit (1); } if (!key->uids->email || key->uids->email[0]) { fprintf (stderr, "Unexpected email in user ID: %s\n", key->uids->email); exit (1); } if (!key->uids->uid || strcmp (key->uids->uid, keys[i].uid)) { fprintf (stderr, "Unexpected uid in user ID: %s\n", key->uids->uid); exit (1); } if (key->uids->next && strcmp (key->uids->next->uid, keys[i].email)) { fprintf (stderr, "Unexpected email in user ID: %s\n", key->uids->next->uid); exit (1); } if (key->uids->next && strcmp (key->uids->next->uid, keys[i].email)) { fprintf (stderr, "Unexpected email in user ID: %s\n", key->uids->next->uid); exit (1); } gpgme_key_unref (key); i++; } if (gpg_err_code (err) != GPG_ERR_EOF) fail_if_err (err); err = gpgme_op_keylist_end (ctx); fail_if_err (err); result = gpgme_op_keylist_result (ctx); if (result->truncated) { fprintf (stderr, "Key listing unexpectedly truncated\n"); exit (1); } if (keys[i].fpr) { fprintf (stderr, "Less keys returned than expected\n"); exit (1); } gpgme_release (ctx); return 0; }
/** \brief Initialise GpgME * * \param get_passphrase Callback function to read a passphrase from the * user. Note that this function is used \em only for OpenPGP and * \em only if no GPG Agent is running and can therefore usually be * NULL. The first (HOOK) argument the passed function accepts * shall be the parent GtkWindow. * \param select_key Callback function to let the user select a key from a * list if more than one is available. * \param accept_low_trust Callback function to ask the user whether a low * trust key shall be accepted. * * Initialise the GpgME backend and remember the callback functions. * * \note This function \em must be called before using any other function * from this module. */ void libbalsa_gpgme_init(gpgme_passphrase_cb_t get_passphrase, lbgpgme_select_key_cb select_key, lbgpgme_accept_low_trust_cb accept_low_trust) { gpgme_engine_info_t e; const gchar *agent_info; /* initialise the gpgme library */ g_message("init gpgme version %s", gpgme_check_version(NULL)); #ifdef HAVE_GPG /* configure the GnuPG engine if a specific gpg path has been * detected */ gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, GPG_PATH, NULL); #endif #ifdef ENABLE_NLS gpgme_set_locale(NULL, LC_CTYPE, get_utf8_locale(LC_CTYPE)); gpgme_set_locale(NULL, LC_MESSAGES, get_utf8_locale(LC_MESSAGES)); #endif /* ENABLE_NLS */ /* dump the available engines */ if (gpgme_get_engine_info(&e) == GPG_ERR_NO_ERROR) { while (e) { g_message("protocol %s: engine %s (home %s, version %s)", gpgme_get_protocol_name(e->protocol), e->file_name, e->home_dir, e->version); e = e->next; } } /* check for gpg-agent */ agent_info = g_getenv("GPG_AGENT_INFO"); if (agent_info) { g_message("gpg-agent found: %s", agent_info); gpgme_passphrase_cb = NULL; } else { gpgme_passphrase_cb = get_passphrase; } /* verify that the engines we need are there */ if (gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP) == GPG_ERR_NO_ERROR) { g_message("OpenPGP protocol supported"); has_proto_openpgp = TRUE; } else { g_warning ("OpenPGP protocol not supported, basic crypto will not work!"); has_proto_openpgp = FALSE; } #ifdef HAVE_SMIME if (gpgme_engine_check_version(GPGME_PROTOCOL_CMS) == GPG_ERR_NO_ERROR) { g_message("CMS (aka S/MIME) protocol supported"); has_proto_cms = TRUE; } else { g_warning("CMS protocol not supported, S/MIME will not work!"); has_proto_cms = FALSE; } #else g_message("built without CMS (aka S/MIME) protocol support"); has_proto_cms = FALSE; #endif /* remember callbacks */ select_key = select_key_cb; accept_low_trust = accept_low_trust_cb; }
int main() { int fd; gpgme_ctx_t g_context; gpgme_engine_info_t enginfo; gpgme_data_t data; gpgme_error_t gerr; gpg_err_code_t gpg_err; gpgme_data_t g_plain; gpgme_data_t g_plain_recv; gpgme_data_t g_encrypt; gpgme_data_t g_encrypt_send; gpgme_key_t g_recipient[MAX_RCP+1]; char *p; char b_encrypt[BIGBUF+1]; char msg[EOF_L_MESSAGE]; char msg_in[EOF_L_MESSAGE]; int i, tmp; for(i=0;i<EOF_L_MESSAGE; i++) msg_in[i] = 0; for(i=0;i<EOF_L_MESSAGE; i++) msg[i] = 0; for(i=0;i<=BIGBUF; i++) b_encrypt[i] = 0; gpgme_check_version(NULL); gerr = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP); if(gerr != GPG_ERR_NO_ERROR) return 10; p = (char *) gpgme_get_protocol_name(GPGME_PROTOCOL_OpenPGP); printf("Version: %s\n",p); gerr = gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, NULL, home); if(gerr == GPG_ERR_NO_ERROR) { printf("gpgme_set_engine_info: ok\n"); } else { printf("gpgme_set_engine_info: err\n"); } /* get engine information */ gerr = gpgme_get_engine_info(&enginfo); if(gerr != GPG_ERR_NO_ERROR) return 3; printf("file=%s, home=%s\n",enginfo->file_name,enginfo->home_dir); /* create our own context */ gerr = gpgme_new(&g_context); if(gerr != GPG_ERR_NO_ERROR) return 1; /* FIXME: both needed? */ /* FIXME: why is the path (FILE_NAME) needed? */ /* FIXME: /usr/bin/gpg must be changed to ~/.ceof/gpg/binary or similar */ gerr = gpgme_ctx_set_engine_info (g_context, GPGME_PROTOCOL_OpenPGP, "/usr/bin/gpg",home); if(gerr != GPG_ERR_NO_ERROR) return 4; /* do not ascii armor data; use 1 for testing */ //gpgme_set_armor(g_context, 0); gpgme_set_armor(g_context, 1); /* create buffers */ gerr = gpgme_data_new(&data); if(gerr != GPG_ERR_NO_ERROR) return 12; gerr = gpgme_data_new(&g_plain_recv); if(gerr != GPG_ERR_NO_ERROR) return 20; gerr = gpgme_data_new(&g_encrypt); if(gerr != GPG_ERR_NO_ERROR) return 14; gerr = gpgme_data_new(&g_encrypt_send); if(gerr != GPG_ERR_NO_ERROR) return 24; /* fill buffers */ /* gerr = gpgme_data_new(&g_plain); if(gerr != GPG_ERR_NO_ERROR) return 13; printf("strlen(%s) = %d\n",msg,i); i -= gpgme_data_write(g_plain, msg, i); if(i) { printf("size mismatch\n"); return 12; } */ strncpy(msg, "==> Erste Nachricht\n\n", EOF_L_MESSAGE); i = strlen(msg); gerr = gpgme_data_new_from_mem (&g_plain, msg, i, 0); /* setup recipient */ gerr = gpgme_op_keylist_start(g_context, "nico schottelius", 0); if(gerr != GPG_ERR_NO_ERROR) return 11; i=0; gerr = gpgme_op_keylist_next(g_context, &g_recipient[0]); while((gpg_err = gpg_err_code(gerr)) != GPG_ERR_EOF) { /* for testing: one call of gpgme_op_keylist_next is enough */ break; if(gerr == GPG_ERR_INV_VALUE) { printf("invalid pointer\n"); return 15; } else if(gerr == GPG_ERR_ENOMEM) { printf("no mem\n"); return 16; } printf ("%s: %s <%s> (%d)\n", g_recipient[0]->subkeys->keyid, g_recipient[0]->uids->name, g_recipient[0]->uids->email, i); i++; /* FIXME: this resets the good filled buffer ... */ gerr = gpgme_op_keylist_next(g_context, &g_recipient[0]); } g_recipient[1] = NULL; /* all above seems to be wrong ... */ gerr = gpgme_get_key(g_context,"775506B45998BF57D0D4AFF27C6E747C38616ADC", &g_recipient[0], 0); if(gerr != GPG_ERR_NO_ERROR) return 32; /* en/decrypt message */ //gerr = gpgme_op_encrypt_sign(g_context, g_recipient, 0, g_plain, g_encrypt); gerr = gpgme_op_encrypt(g_context, g_recipient, 0, g_plain, g_encrypt); if(gerr != GPG_ERR_NO_ERROR) { printf("gerr=%d\n",gerr); return 18; } /* transfer the data into our own buffer, * so the data is saved; you cannot * reuse the gpgme buffers directly as in * gerr = gpgme_op_decrypt(g_context, g_encrypt, g_plain_recv); * */ i = gpgme_data_seek(g_encrypt, 0, SEEK_END); if(i > BIGBUF) return 22; printf("buflen: %d\n",i); /* reset to the beginning */ gpgme_data_seek(g_encrypt, 0, SEEK_SET); if(gpgme_data_read(g_encrypt, b_encrypt, i) == -1) { perror("pgme_data_read"); return 23; } printf("crypt:\n%s\n", b_encrypt); fd = open("testcrypt",O_RDWR|O_CREAT); if(fd == -1) return 40; if(write_all(fd, b_encrypt, BIGBUF) <= 0) return 41; close(fd); /* until here it works, testcrypt contains * data and can be decrypted ... * * perhaps the context needs some reset? */ if((tmp = gpgme_data_write(g_encrypt_send, b_encrypt, i)) == -1) { perror("pgme_data_write"); return 23; } printf("crypt-wrote:%d\n", tmp); /* look for contexts */ gerr = gpgme_op_decrypt(g_context, g_encrypt_send, g_plain_recv); if(gerr != GPG_ERR_NO_ERROR) { printf("gerr=%d\n",gerr); return 19; } /* open communication channel: netcat */ /* listen for input from: * stdin * communication channel */ /* de/encode data from comm channel */ return 1; }
GPGME_Error GPGME::init(const QString & gpgHomeDir) { if (instancesStore.contains(gpgHomeDir)) { return GPG_ERR_NO_ERROR; } setlocale(LC_ALL, ""); gpgme_check_version(NULL); gpgme_set_locale(NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL)); #ifdef LC_MESSAGES gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL)); #endif gpgme_error_t err; gpgme_ctx_t context; err = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP); if (err != GPG_ERR_NO_ERROR) { return err; } QString protocolName = gpgme_get_protocol_name(GPGME_PROTOCOL_OpenPGP); qDebug() << "protocol: " << protocolName; gpgme_engine_info_t engineInfo; err = gpgme_get_engine_info(&engineInfo); if (err != GPG_ERR_NO_ERROR) { return err; } qDebug() << "Backend info"; qDebug() << "filename: " << engineInfo->file_name << ", homedir: " << engineInfo->home_dir; err = gpgme_new(&context); if (err != GPG_ERR_NO_ERROR) { return err; } err = gpgme_set_protocol(context, GPGME_PROTOCOL_OpenPGP); if (err != GPG_ERR_NO_ERROR) { return err; } // we don't need to set gnupg home dir explicitly QString gnupgHome; if (gpgHomeDir.isEmpty()) { // i.e. use default gnupg directory or one from environment QString gnupgHomeEnv = QString::fromLatin1(qgetenv("GNUPGHOME")); if (!gnupgHomeEnv.isEmpty()) { gnupgHome = gnupgHomeEnv; } else { // use default path: "~/.gnupg" QDir gh = QDir::home(); gh.cd(".gnupg"); gnupgHome = gh.canonicalPath(); } } else { QDir gh(gpgHomeDir); gnupgHome = gh.canonicalPath(); } qDebug() << "GNUPGHOME" << gnupgHome; err = gpgme_ctx_set_engine_info(context, GPGME_PROTOCOL_OpenPGP, engineInfo->file_name, gnupgHome.toLatin1().data() ); if (err != GPG_ERR_NO_ERROR) { return err; } GPGME * inst = new GPGME(context, gnupgHome); instancesStore[gpgHomeDir] = inst; qDebug() << "gpgme initalized for the directory " << gnupgHome << "[store key: " << gpgHomeDir << "]"; return GPG_ERR_NO_ERROR; }