static void geanypg_encrypt(encrypt_data * ed, gpgme_key_t * recp, int sign, int flags) { /* FACTORIZE */ gpgme_data_t plain, cipher; gpgme_error_t err; FILE * tempfile; tempfile = tmpfile(); if (!(tempfile)) { g_warning("%s: %s.", _("couldn't create tempfile"), strerror(errno)); return ; } gpgme_data_new_from_stream(&cipher, tempfile); gpgme_data_set_encoding(cipher, GPGME_DATA_ENCODING_ARMOR); geanypg_load_buffer(&plain); /* do the actual encryption */ if (sign) err = gpgme_op_encrypt_sign(ed->ctx, recp, flags, plain, cipher); else err = gpgme_op_encrypt(ed->ctx, recp, flags, plain, cipher); if (err != GPG_ERR_NO_ERROR && gpgme_err_code(err) != GPG_ERR_CANCELED) geanypg_show_err_msg(err); else if(gpgme_err_code(err) != GPG_ERR_CANCELED) { rewind(tempfile); geanypg_write_file(tempfile); } fclose(tempfile); /* release buffers */ gpgme_data_release(plain); gpgme_data_release(cipher); }
void import ( gpgme_ctx_t * context, telem_gpg_opts * options, int num_keys, char ** key_path ) { gpgme_error_t err; int url_mode = 0; int nul_mode = 0; gpgme_import_result_t impres; gpgme_data_t data; for (; num_keys; --num_keys, key_path++) { printf ("reading file `%s'\n", *key_path); err = gpgme_data_new_from_file(&data, *key_path, 1); fail_if_err(err); if (url_mode) gpgme_data_set_encoding( data, (nul_mode? GPGME_DATA_ENCODING_URL0 : GPGME_DATA_ENCODING_URL) ); err = gpgme_op_import(*context, data); fail_if_err(err); impres = gpgme_op_import_result(*context); if (!impres) { fprintf(stderr, "No import result returned\n"); exit(1); } print_import_result(impres); gpgme_data_release(data); } }
void geanypg_load_buffer(gpgme_data_t * buffer) { /* gpgme_data_new_from_mem(buffer, text, size, 0); */ GeanyDocument * doc = document_get_current(); char * data = NULL; unsigned long size = 0; if (sci_has_selection(doc->editor->sci)) { size = scintilla_send_message(doc->editor->sci, SCI_GETSELTEXT, 0, 0) - 1; data = (char *) malloc(size + 1); scintilla_send_message(doc->editor->sci, SCI_GETSELTEXT, 0, (sptr_t)data); gpgme_data_new_from_mem(buffer, data, size, 1); } else { size = scintilla_send_message(doc->editor->sci, SCI_GETLENGTH, 0, 0); data = (char *) malloc(size + 1); scintilla_send_message(doc->editor->sci, SCI_GETTEXT, (uptr_t)(size + 1), (sptr_t)data); gpgme_data_new_from_mem(buffer, data, size, 1); } if (data) /* if there is no text data may still be NULL */ free(data); gpgme_data_set_encoding(*buffer, GPGME_DATA_ENCODING_BINARY); }
int main(int argc, char *argv[]) { char * op=(char *)0; char * search=(char *)0; char * searchdec=(char *)0; char * exact=(char *)0; gpgme_ctx_t gpgctx; gpgme_key_t gpgkey; gpgme_error_t gpgerr; gpgme_engine_info_t enginfo; char * qstring, * pchar; pchar=getenv("QUERY_STRING"); if (! pchar || *pchar == '\0' ) { http_header(500,CTYPE_HTML_STR); printf("<html><head><title>Error handling request</title></head><body><h1>Error handling request: there is no query string.</h1></body></html>"); return 1; } qstring=strndup(pchar,QSTRING_MAX); /* copy the QUERY from env to write in */ pchar=qstring; while (pchar && *pchar) { if (!strncmp(pchar,"op=",3)) { pchar+=3; op=pchar; } else if (!strncmp(pchar,"search=",7)) { pchar+=7; search=pchar; } else if (!strncmp(pchar,"options=",8)) { /*this parameter is useless now, as today we only support "mr" option and always enable it (machine readable) */ pchar+=8; //options=pchar; } else if (!strncmp(pchar,"fingerprint=",12)) { /*this parameter is useless now as we only support "mr" options which don't care this */ pchar+=12; //fingerprints=pchar; } else if (!strncmp(pchar,"exact=",6)) { pchar+=6; exact=pchar; } /*else: Other parameter not in hkp draft are quietly ignored */ pchar=strchr(pchar,'&'); if (pchar) { *pchar='\0'; pchar++; } } if (exact) { if (!strcmp(exact,"off")) { exact=(char *) 0; /* off is default */ } else if (!strcmp(exact,"on")) { http_header(501,CTYPE_HTML_STR); printf("<html><head><title>Not implemented</title></head><body><h1>Error handling request: \"exact\" parameter is not implemented.</h1></body></html>"); return 1; } else { http_header(500,CTYPE_HTML_STR); printf("<html><head><title>Error handling request</title></head><body><h1>Error handling request: \"exact\" parameter only take \"on\" or \"off\" as argument.</h1></body></html>"); return 1; } } if ( ! search ) { /* (mandatory parameter) */ http_header(500,CTYPE_HTML_STR); printf("<html><head><title>Error handling request</title></head><body><h1>Error handling request: Missing \"search\" parameter in \"%s\".</h1></body></html>",getenv("QUERY_STRING")); return 1; } else { if (searchdec=malloc(strlen(search)*sizeof(char)+1)) strdecode(searchdec,search); else { http_header(500,CTYPE_HTML_STR); printf("<html><head><title>Internal Error</title></head><body><h1>Internal malloc(%d) for search fail.</h1></body></html>",strlen(search)*sizeof(char)+1); return 1; } } if ( ! op ) op="index"; /* defaut operation */ /* Check gpgme version ( http://www.gnupg.org/documentation/manuals/gpgme/Library-Version-Check.html )*/ setlocale (LC_ALL, ""); gpgme_check_version (NULL); gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL)); /* check for OpenPGP support */ gpgerr=gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP); if ( gpgerr != GPG_ERR_NO_ERROR ) { http_header(500,CTYPE_HTML_STR); printf("<html><head><title>Internal Error</title></head><body><h1>Error handling request due to internal error (gpgme_engine_check_version).</h1></body></html>"); return 1; } /* create context */ gpgerr=gpgme_new(&gpgctx); if ( gpgerr != GPG_ERR_NO_ERROR ) { http_header(500,CTYPE_HTML_STR); printf("<html><head><title>Internal Error</title></head><body><h1>Error handling request due to internal error (gpgme_new %d).</h1></body></html>",gpgerr); return 1; } /*gpgerr = gpgme_get_engine_info(&enginfo); gpgerr |= gpgme_ctx_set_engine_info(gpgctx, GPGME_PROTOCOL_OpenPGP, enginfo->file_name,"../../new"); if ( gpgerr != GPG_ERR_NO_ERROR ) { http_header(500,CTYPE_HTML_STR); printf("<html><head><title>Internal Error</title></head><body><h1>Error handling request due to internal error (gpgme_ctx_set_engine_info %d).</h1></body></html>",gpgerr); return 1; }*/ if (!strcmp(op, "get")) { gpgme_data_t gpgdata; char buff[BUFFSIZE]; ssize_t read_bytes; gpgme_set_armor(gpgctx,1); gpgerr = gpgme_data_new(&gpgdata); if (gpgerr == GPG_ERR_NO_ERROR) { gpgerr = gpgme_data_set_encoding(gpgdata,GPGME_DATA_ENCODING_ARMOR); if (gpgerr == GPG_ERR_NO_ERROR) gpgerr = gpgme_op_export(gpgctx,searchdec,0,gpgdata); } if ( gpgerr != GPG_ERR_NO_ERROR) { http_header(500,CTYPE_HTML_STR); printf("<html><head><title>Internal Error</title></head><body><h1>Error handling request due to internal error (%d).</h1></body></html>",gpgerr); return 1; } gpgme_data_seek (gpgdata, 0, SEEK_SET); read_bytes = gpgme_data_read (gpgdata, buff, BUFFSIZE); if ( read_bytes == -1 ) { http_header(500,CTYPE_HTML_STR); printf("<html><head><title>Internal Error</title></head><body><h1>Error handling request due to internal error (%s).</h1></body></html>",gpgme_strerror(errno)); return 1; } else if ( read_bytes <= 0 ) { http_header(404,CTYPE_HTML_STR); printf("<html><head><title>ludd Public Key Server -- Get: %s</title></head><body><h1>Public Key Server -- Get: %s : No key found ! :-( </h1></body></html>",search,search); return 0; } else { http_header(200,CTYPE_HTML_STR); printf("<html><head><title>ludd Public Key Server -- Get: %s</title></head><body><h1>Public Key Server -- Get: %s</h1><pre>",search,search); fwrite(buff, sizeof(char),read_bytes,stdout); /* Now it's too late to test fwrite return value ;-) */ while ( (read_bytes = gpgme_data_read (gpgdata, buff, BUFFSIZE)) > 0 ) fwrite(buff, sizeof(char),read_bytes,stdout); printf("\n</pre></body></html>"); return 0; } } else if (!strcmp(op, "index")) { char uidenc[BUFFSIZE]; char begin=0; gpgme_user_id_t gpguid; /* check for the searched key(s) */ gpgerr = gpgme_op_keylist_start(gpgctx, searchdec, 0); //gpgerr = gpgme_op_keylist_start(gpgctx, NULL, 0); if ( gpgerr != GPG_ERR_NO_ERROR ) { http_header(500,CTYPE_HTML_STR); printf("<html><head><title>Internal Error</title></head><body><h1>Error handling request due to internal error (gpgme_op_keylist_start %d).</h1></body></html>",gpgerr); return 1; } gpgerr = gpgme_op_keylist_next (gpgctx, &gpgkey); while (gpgerr == GPG_ERR_NO_ERROR) { if (!begin) { http_header(200,"text/plain; charset=utf-8"); begin=1; /* Luckily: info "header" is optionnal, see draft-shaw-openpgp-hkp-00.txt */ } /* first subkey is the main key */ printf("pub:%s:%d:%d:%d:%d\n",gpgkey->subkeys->fpr,gpgkey->subkeys->pubkey_algo,gpgkey->subkeys->length,gpgkey->subkeys->timestamp,(gpgkey->subkeys->expires?gpgkey->subkeys->expires:-1)); gpguid=gpgkey->uids; while (gpguid) { printf("uid:%s (%s) <%s>:\n",gpguid->name,gpguid->comment,gpguid->email); gpguid=gpguid->next; } gpgme_key_unref(gpgkey); gpgerr = gpgme_op_keylist_next (gpgctx, &gpgkey); } gpgme_key_unref(gpgkey); /* ... because i don't know how "gpgme_op_keylist_next" behave when not returning GPG_ERR_NO_ERROR */ if (!begin) { http_header(404,CTYPE_HTML_STR); printf("<html><head><title>ludd Public Key Server -- index: %s</title></head><body><h1>index Error: No keys found</h1></body></html>",search); return 1; } return 0; } else if ( !strcmp(op, "photo") || !strcmp(op, "x-photo") ) { http_header(501,CTYPE_HTML_STR); printf("<html><head><title>Not implemented</title></head><body><h1>Error handling request: \"%s\" operation is not implemented.</h1></body></html>",op); return 1; } else { http_header(500,CTYPE_HTML_STR); printf("<html><head><title>Error handling request</title></head><body><h1>Error handling request: Unrecognized action in \"%s\".</h1></body></html>",getenv("QUERY_STRING")); return 1; } }
static gint pgpmime_check_signature(MimeInfo *mimeinfo) { PrivacyDataPGP *data; MimeInfo *parent, *signature; FILE *fp; gchar *boundary; gchar *textstr; gpgme_data_t sigdata = NULL, textdata = NULL; gpgme_error_t err; cm_return_val_if_fail(mimeinfo != NULL, -1); cm_return_val_if_fail(mimeinfo->privacy != NULL, -1); data = (PrivacyDataPGP *) mimeinfo->privacy; if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) { debug_print(("Couldn't initialize GPG context, %s\n"), gpgme_strerror(err)); privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err)); return 0; } debug_print("Checking PGP/MIME signature\n"); err = gpgme_set_protocol(data->ctx, GPGME_PROTOCOL_OpenPGP); if (err) { debug_print ("gpgme_set_protocol failed: %s\n", gpgme_strerror (err)); } parent = procmime_mimeinfo_parent(mimeinfo); fp = claws_fopen(parent->data.filename, "rb"); cm_return_val_if_fail(fp != NULL, SIGNATURE_INVALID); boundary = g_hash_table_lookup(parent->typeparameters, "boundary"); if (!boundary) { privacy_set_error(_("Signature boundary not found.")); claws_fclose(fp); return 0; } textstr = get_canonical_content(fp, boundary); err = gpgme_data_new_from_mem(&textdata, textstr, (size_t)strlen(textstr), 0); if (err) { debug_print ("gpgme_data_new_from_mem failed: %s\n", gpgme_strerror (err)); } signature = (MimeInfo *) mimeinfo->node->next->data; sigdata = sgpgme_data_from_mimeinfo(signature); err = 0; if (signature->encoding_type == ENC_BASE64) { err = gpgme_data_set_encoding (sigdata, GPGME_DATA_ENCODING_BASE64); } if (err) { debug_print ("gpgme_data_set_encoding failed: %s\n", gpgme_strerror (err)); } data->sigstatus = sgpgme_verify_signature (data->ctx, sigdata, textdata, NULL); gpgme_data_release(sigdata); gpgme_data_release(textdata); g_free(textstr); claws_fclose(fp); return 0; }