static void prefs_gpg_save_func(PrefsPage *_page) { struct GPGPage *page = (struct GPGPage *) _page; GPGConfig *config = prefs_gpg_get_config(); config->auto_check_signatures = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_auto_check_signatures)); config->autocompletion = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_autocompletion)); config->use_gpg_agent = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_use_gpg_agent)); config->store_passphrase = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_store_passphrase)); config->store_passphrase_timeout = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->spinbtn_store_passphrase)); config->passphrase_grab = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_passphrase_grab)); config->gpg_warning = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_gpg_warning)); g_free(config->gpg_path); config->gpg_path = g_strdup(gtk_entry_get_text(GTK_ENTRY(page->gpg_path))); if (strcmp(config->gpg_path, "") != 0 && access(config->gpg_path, X_OK) != -1) gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, config->gpg_path, NULL); prefs_gpg_enable_agent(config->use_gpg_agent); prefs_gpg_save_config(); }
/** * Initialize the GPGME library. * This can be safely called multiple times; however it is not thread-safe. * @param handle the context handle * @return 0 on success, -1 on error */ static int init_gpgme(alpm_handle_t *handle) { static int init = 0; const char *version, *sigdir; gpgme_error_t gpg_err; gpgme_engine_info_t enginfo; if(init) { /* we already successfully initialized the library */ return 0; } sigdir = handle->gpgdir; if(_alpm_access(handle, sigdir, "pubring.gpg", R_OK) || _alpm_access(handle, sigdir, "trustdb.gpg", R_OK)) { handle->pm_errno = ALPM_ERR_NOT_A_FILE; _alpm_log(handle, ALPM_LOG_DEBUG, "Signature verification will fail!\n"); _alpm_log(handle, ALPM_LOG_WARNING, _("Public keyring not found; have you run '%s'?\n"), "pacman-key --init"); } /* calling gpgme_check_version() returns the current version and runs * some internal library setup code */ version = gpgme_check_version(NULL); _alpm_log(handle, ALPM_LOG_DEBUG, "GPGME version: %s\n", version); gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL)); #ifdef LC_MESSAGES gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL)); #endif /* NOTE: * The GPGME library installs a SIGPIPE signal handler automatically if * the default signal hander is in use. The only time we set a handler * for SIGPIPE is in dload.c, and we reset it when we are done. Given that * we do this, we can let GPGME do its automagic. However, if we install * a library-wide SIGPIPE handler, we will have to be careful. */ /* check for OpenPGP support (should be a no-brainer, but be safe) */ gpg_err = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP); CHECK_ERR(); /* set and check engine information */ gpg_err = gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, NULL, sigdir); CHECK_ERR(); gpg_err = gpgme_get_engine_info(&enginfo); CHECK_ERR(); _alpm_log(handle, ALPM_LOG_DEBUG, "GPGME engine info: file=%s, home=%s\n", enginfo->file_name, enginfo->home_dir); init = 1; return 0; gpg_error: _alpm_log(handle, ALPM_LOG_ERROR, _("GPGME error: %s\n"), gpgme_strerror(gpg_err)); RET_ERR(handle, ALPM_ERR_GPGME, -1); }
static int init_gpgme(alpm_handle_t *handle) { static int init = 0; const char *version, *sigdir; gpgme_error_t err; gpgme_engine_info_t enginfo; if(init) { /* we already successfully initialized the library */ return 0; } sigdir = alpm_option_get_gpgdir(handle); /* calling gpgme_check_version() returns the current version and runs * some internal library setup code */ version = gpgme_check_version(NULL); _alpm_log(handle, ALPM_LOG_DEBUG, "GPGME version: %s\n", version); gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL)); #ifdef LC_MESSAGES gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL)); #endif /* NOTE: * The GPGME library installs a SIGPIPE signal handler automatically if * the default signal hander is in use. The only time we set a handler * for SIGPIPE is in dload.c, and we reset it when we are done. Given that * we do this, we can let GPGME do its automagic. However, if we install * a library-wide SIGPIPE handler, we will have to be careful. */ /* check for OpenPGP support (should be a no-brainer, but be safe) */ err = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP); CHECK_ERR(); /* set and check engine information */ err = gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, NULL, sigdir); CHECK_ERR(); err = gpgme_get_engine_info(&enginfo); CHECK_ERR(); _alpm_log(handle, ALPM_LOG_DEBUG, "GPGME engine info: file=%s, home=%s\n", enginfo->file_name, enginfo->home_dir); init = 1; return 0; error: _alpm_log(handle, ALPM_LOG_ERROR, _("GPGME error: %s\n"), gpgme_strerror(err)); RET_ERR(handle, ALPM_ERR_GPGME, 1); }
int init_gpgme(fko_ctx_t fko_ctx) { gpgme_error_t err; /* If we already have a context, we are done. */ if(fko_ctx->have_gpgme_context) return(FKO_SUCCESS); /* Because the gpgme manual says you should. */ gpgme_check_version(NULL); /* Check for OpenPGP support */ err = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP); if(gpg_err_code(err) != GPG_ERR_NO_ERROR) { /* GPG engine is not available. */ fko_ctx->gpg_err = err; return(FKO_ERROR_GPGME_NO_OPENPGP); } /* Extract the current gpgme engine information. */ gpgme_set_engine_info( GPGME_PROTOCOL_OpenPGP, (fko_ctx->gpg_exe != NULL) ? fko_ctx->gpg_exe : GPG_EXE, fko_ctx->gpg_home_dir /* If this is NULL, the default is used */ ); /* Create our gpgme context */ err = gpgme_new(&(fko_ctx->gpg_ctx)); if(gpg_err_code(err) != GPG_ERR_NO_ERROR) { fko_ctx->gpg_err = err; return(FKO_ERROR_GPGME_CONTEXT); } fko_ctx->have_gpgme_context = 1; return(FKO_SUCCESS); }
/** \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; }
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 (strcmp(prefs_gpg_get_config()->gpg_path, "") != 0 && access(prefs_gpg_get_config()->gpg_path, X_OK) != -1) gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,prefs_gpg_get_config()->gpg_path, NULL); 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 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; }