#include "prefs_common.h"
#include "passphrase.h"
#include "select-keys.h"
-#include "sigstatus.h"
#include "rfc2015.h"
+#include "alertpanel.h"
#define DIM(v) (sizeof(v)/sizeof((v)[0]))
NULL
};
-static void sig_expiration_check (GString *str,
- GpgmeCtx ctx,
- GpgmeKey key,
- GpgmeSigStat status,
- int idx);
-static void sig_expired (GString *str,
- GpgmeCtx ctx,
- int idx);
-static void sig_key_expired (GString *str,
- GpgmeKey key,
- int idx);
-
#if 0
static void dump_mimeinfo (const char *text, MimeInfo *x)
{
}
#endif
-void
-rfc2015_disable_all (void)
-{
- /* FIXME: set a flag, so that we don't bother the user with failed
- * gpgme messages */
-}
-
-
void
rfc2015_secure_remove (const char *fname)
{
}
-static const gchar *
-sig_status_to_string (GpgmeSigStat status)
-{
- const gchar *result;
-
- switch (status) {
- case GPGME_SIG_STAT_NONE:
- result = _("Oops: Signature not verified");
- break;
- case GPGME_SIG_STAT_NOSIG:
- result = _("No signature found");
- break;
- case GPGME_SIG_STAT_GOOD:
- result = _("Good signature");
- break;
- case GPGME_SIG_STAT_GOOD_EXP:
- result = _("Good signature but it has expired");
- break;
- case GPGME_SIG_STAT_GOOD_EXPKEY:
- result = _("Good signature but the key has expired");
- break;
- case GPGME_SIG_STAT_BAD:
- result = _("BAD signature");
- break;
- case GPGME_SIG_STAT_NOKEY:
- result = _("No public key to verify the signature");
- break;
- case GPGME_SIG_STAT_ERROR:
- result = _("Error verifying the signature");
- break;
- case GPGME_SIG_STAT_DIFF:
- result = _("Different results for signatures");
- break;
- default:
- result = _("Error: Unknown status");
- break;
- }
-
- return result;
-}
-
-static const gchar *
-sig_status_with_name (GpgmeSigStat status)
-{
- const gchar *result;
-
- switch (status) {
- case GPGME_SIG_STAT_NONE:
- result = _("Oops: Signature not verified");
- break;
- case GPGME_SIG_STAT_NOSIG:
- result = _("No signature found");
- break;
- case GPGME_SIG_STAT_GOOD:
- result = _("Good signature from \"%s\"");
- break;
- case GPGME_SIG_STAT_GOOD_EXP:
- result = _("Good signature from \"%s\" but it has expired");
- break;
- case GPGME_SIG_STAT_GOOD_EXPKEY:
- result = _("Good signature from \"%s\" but the key has expired");
- break;
- case GPGME_SIG_STAT_BAD:
- result = _("BAD signature from \"%s\"");
- break;
- case GPGME_SIG_STAT_NOKEY:
- result = _("No public key to verify the signature");
- break;
- case GPGME_SIG_STAT_ERROR:
- result = _("Error verifying the signature");
- break;
- case GPGME_SIG_STAT_DIFF:
- result = _("Different results for signatures");
- break;
- default:
- result = _("Error: Unknown status");
- break;
- }
-
- return result;
-}
-
-static void
-sig_status_for_key(GString *str, GpgmeCtx ctx, GpgmeSigStat status,
- GpgmeKey key, const gchar *fpr)
-{
- gint idx = 0;
- const char *uid;
-
- uid = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID, NULL, idx);
- if (uid == NULL) {
- g_string_sprintfa (str, "%s\n",
- sig_status_to_string (status));
- if ((fpr != NULL) && (*fpr != '\0'))
- g_string_sprintfa (str, "Key fingerprint: %s\n", fpr);
- g_string_append (str, _("Cannot find user ID for this key."));
- sig_expiration_check(str, ctx, key, status, 0);
- return;
- }
- g_string_sprintfa (str, sig_status_with_name (status), uid);
- g_string_append (str, "\n");
-
- while (1) {
- uid = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID,
- NULL, ++idx);
- if (uid == NULL)
- break;
- g_string_sprintfa (str, _(" aka \"%s\"\n"),
- uid);
- }
- sig_expiration_check(str, ctx, key, status, 0);
-}
-
-static void
-sig_expiration_check(GString *str, GpgmeCtx ctx, GpgmeKey key,
- GpgmeSigStat status, int idx)
-{
- if (status == GPGME_SIG_STAT_GOOD_EXP)
- sig_expired(str, ctx, idx);
- else if (status == GPGME_SIG_STAT_GOOD_EXPKEY)
- sig_key_expired(str, key, idx);
-}
-
-static void
-sig_expired(GString *str, GpgmeCtx ctx, int idx)
-{
- unsigned long exp_time;
- exp_time = gpgme_get_sig_ulong_attr(ctx, idx, GPGME_ATTR_EXPIRE, 0);
- g_string_sprintfa(str, _("Signature expired %s"), ctime(&exp_time));
-}
-
-static void
-sig_key_expired(GString *str, GpgmeKey key, int idx)
-{
- unsigned long exp_time;
- exp_time = gpgme_key_get_ulong_attr(key, GPGME_ATTR_EXPIRE, NULL, idx);
- g_string_sprintfa(str, _("Key expired %s"), ctime(&exp_time));
-}
-
-static gchar *
-sig_status_full (GpgmeCtx ctx)
-{
- GString *str;
- gint sig_idx = 0;
- GpgmeError err;
- GpgmeSigStat status;
- GpgmeKey key;
- const char *fpr;
- time_t created;
- struct tm *ctime_val;
- char ctime_str[80];
- gchar *retval;
-
- str = g_string_new ("");
-
- fpr = gpgme_get_sig_status (ctx, sig_idx, &status, &created);
- while (fpr != NULL) {
- if (created != 0) {
- ctime_val = localtime (&created);
- strftime (ctime_str, sizeof (ctime_str), "%c",
- ctime_val);
- g_string_sprintfa (str,
- _("Signature made at %s\n"),
- ctime_str);
- }
- err = gpgme_get_sig_key (ctx, sig_idx, &key);
- if (err != 0) {
- g_string_sprintfa (str, "%s\n",
- sig_status_to_string (status));
- if ((fpr != NULL) && (*fpr != '\0'))
- g_string_sprintfa (str,
- _("Key fingerprint: %s\n"),
- fpr);
- } else {
- sig_status_for_key (str, ctx, status, key, fpr);
- gpgme_key_unref (key);
- }
- g_string_append (str, "\n\n");
-
- fpr = gpgme_get_sig_status (ctx, ++sig_idx, &status, &created);
- }
-
- retval = str->str;
- g_string_free (str, FALSE);
- return retval;
-}
-
-static void check_signature (MimeInfo *mimeinfo, MimeInfo *partinfo, FILE *fp)
-{
- GpgmeCtx ctx = NULL;
- GpgmeError err;
- GpgmeData sig = NULL, text = NULL;
- GpgmeSigStat status = GPGME_SIG_STAT_NONE;
- GpgmegtkSigStatus statuswindow = NULL;
- const char *result = NULL;
- gchar *tmp_file;
- gint n_exclude_chars = 0;
-
- if (prefs_common.gpg_signature_popup)
- statuswindow = gpgmegtk_sig_status_create ();
-
- err = gpgme_new (&ctx);
- if (err) {
- debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
- goto leave;
- }
-
- /* don't include the last empty line.
- It does not belong to the signed text */
- if (mimeinfo->children->size > 0) {
- if (fseek(fp, mimeinfo->children->fpos + mimeinfo->children->size - 1,
- SEEK_SET) < 0) {
- perror("fseek");
- goto leave;
- }
- if (fgetc(fp) == '\n') {
- n_exclude_chars++;
- if (mimeinfo->children->size > 1) {
- if (fseek(fp, mimeinfo->children->fpos + mimeinfo->children->size - 2,
- SEEK_SET) < 0) {
- perror("fseek");
- goto leave;
- }
- if (fgetc(fp) == '\r')
- n_exclude_chars++;
- }
- }
- }
-
- /* canonicalize the file part. */
- tmp_file = get_tmp_file();
- if (copy_file_part(fp, mimeinfo->children->fpos,
- mimeinfo->children->size - n_exclude_chars,
- tmp_file) < 0) {
- g_free(tmp_file);
- goto leave;
- }
- if (canonicalize_file_replace(tmp_file) < 0) {
- unlink(tmp_file);
- g_free(tmp_file);
- goto leave;
- }
-
- err = gpgme_data_new_from_file(&text, tmp_file, 1);
-
- unlink(tmp_file);
- g_free(tmp_file);
- if (!err)
- err = gpgme_data_new_from_filepart (&sig, NULL, fp,
- partinfo->fpos, partinfo->size);
- if (err) {
- debug_print ("gpgme_data_new_from_filepart failed: %s\n",
- gpgme_strerror (err));
- goto leave;
- }
-
- err = gpgme_op_verify (ctx, sig, text, &status);
- if (err) {
- debug_print ("gpgme_op_verify failed: %s\n", gpgme_strerror (err));
- goto leave;
- }
-
- /* FIXME: check what the heck this sig_status_full stuff is.
- * it should better go into sigstatus.c */
- g_free (partinfo->sigstatus_full);
- partinfo->sigstatus_full = sig_status_full (ctx);
-
-leave:
- result = gpgmegtk_sig_status_to_string(status);
- debug_print("verification status: %s\n", result);
- if (prefs_common.gpg_signature_popup)
- gpgmegtk_sig_status_update (statuswindow, ctx);
-
- g_free (partinfo->sigstatus);
- partinfo->sigstatus = g_strdup (result);
- partinfo->sig_ok = (status == GPGME_SIG_STAT_GOOD);
- partinfo->sig_unknown = (status == GPGME_SIG_STAT_NOKEY);
- partinfo->sig_expired = (status == GPGME_SIG_STAT_GOOD_EXP);
- partinfo->key_expired = (status == GPGME_SIG_STAT_GOOD_EXPKEY);
-
- gpgme_data_release (sig);
- gpgme_data_release (text);
- gpgme_release (ctx);
- if (prefs_common.gpg_signature_popup)
- gpgmegtk_sig_status_destroy (statuswindow);
-}
-
-/*
- * Copy a gpgme data object to a temporary file and
- * return this filename
- */
-#if 0
-static char *
-copy_gpgmedata_to_temp (GpgmeData data, guint *length)
-{
- static int id;
- char *tmp;
- FILE *fp;
- char buf[100];
- size_t nread;
- GpgmeError err;
-
- tmp = g_strdup_printf("%s%cgpgtmp.%08x",
- get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id );
-
- if ((fp = fopen(tmp, "wb")) == NULL) {
- FILE_OP_ERROR(tmp, "fopen");
- g_free(tmp);
- return NULL;
- }
-
- err = gpgme_data_rewind ( data );
- if (err)
- debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
-
- while (!(err = gpgme_data_read (data, buf, 100, &nread))) {
- fwrite ( buf, nread, 1, fp );
- }
-
- if (err != GPGME_EOF)
- debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
-
- fclose (fp);
- *length = nread;
-
- return tmp;
-}
-#endif
-
-static GpgmeData
-pgp_decrypt (MimeInfo *partinfo, FILE *fp)
-{
- GpgmeCtx ctx = NULL;
- GpgmeError err;
- GpgmeData cipher = NULL, plain = NULL;
- struct passphrase_cb_info_s info;
-
- memset (&info, 0, sizeof info);
-
- err = gpgme_new (&ctx);
- if (err) {
- debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
- goto leave;
- }
-
- err = gpgme_data_new_from_filepart (&cipher, NULL, fp,
- partinfo->fpos, partinfo->size);
- if (err) {
- debug_print ("gpgme_data_new_from_filepart failed: %s\n",
- gpgme_strerror (err));
- goto leave;
- }
-
- err = gpgme_data_new (&plain);
- if (err) {
- debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
- goto leave;
- }
-
- if (!getenv("GPG_AGENT_INFO")) {
- info.c = ctx;
- gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
- }
-
- err = gpgme_op_decrypt (ctx, cipher, plain);
-
-leave:
- gpgme_data_release (cipher);
- if (err) {
- gpgmegtk_free_passphrase();
- debug_print ("decryption failed: %s\n", gpgme_strerror (err));
- gpgme_data_release (plain);
- plain = NULL;
- }
- else
- debug_print ("** decryption succeeded\n");
-
- gpgme_release (ctx);
- return plain;
-}
-
-MimeInfo * rfc2015_find_signature (MimeInfo *mimeinfo)
-{
- MimeInfo *partinfo;
- int n = 0;
-
- if (!mimeinfo)
- return NULL;
- if (g_strcasecmp (mimeinfo->content_type, "multipart/signed"))
- return NULL;
-
- debug_print ("** multipart/signed encountered\n");
-
- /* check that we have at least 2 parts of the correct type */
- for (partinfo = mimeinfo->children;
- partinfo != NULL; partinfo = partinfo->next) {
- if (++n > 1 && !g_strcasecmp (partinfo->content_type,
- "application/pgp-signature"))
- break;
- }
-
- return partinfo;
-}
-
-gboolean rfc2015_has_signature (MimeInfo *mimeinfo)
-{
- return rfc2015_find_signature (mimeinfo) != NULL;
-}
-
-void rfc2015_check_signature (MimeInfo *mimeinfo, FILE *fp)
-{
- MimeInfo *partinfo;
-
- partinfo = rfc2015_find_signature (mimeinfo);
- if (!partinfo)
- return;
-
-#if 0
- g_message ("** yep, it is a pgp signature");
- dump_mimeinfo ("gpg-signature", partinfo );
- dump_part (partinfo, fp );
- dump_mimeinfo ("signed text", mimeinfo->children );
- dump_part (mimeinfo->children, fp);
-#endif
-
- check_signature (mimeinfo, partinfo, fp);
-}
-
-int rfc2015_is_encrypted (MimeInfo *mimeinfo)
-{
- if (!mimeinfo || mimeinfo->mime_type != MIME_MULTIPART)
- return 0;
- if (g_strcasecmp (mimeinfo->content_type, "multipart/encrypted"))
- return 0;
- /* fixme: we should check the protocol parameter */
- return 1;
-}
-
-gboolean rfc2015_msg_is_encrypted (const gchar *file)
-{
- FILE *fp;
- MimeInfo *mimeinfo;
- int ret;
-
- if ((fp = fopen(file, "rb")) == NULL)
- return FALSE;
-
- mimeinfo = procmime_scan_mime_header(fp);
- if(!mimeinfo) {
- fclose(fp);
- return FALSE;
- }
-
- ret = rfc2015_is_encrypted(mimeinfo);
- procmime_mimeinfo_free_all(mimeinfo);
- return ret != 0 ? TRUE : FALSE;
-}
static int
name_cmp(const char *a, const char *b)
return names[i] != NULL;
}
-
-#define DECRYPTION_ABORT() \
-{ \
- procmime_mimeinfo_free_all(tmpinfo); \
- msginfo->decryption_failed = 1; \
- return; \
-}
-
-void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
-{
- static int id;
- MimeInfo *tmpinfo, *partinfo;
- int ver_ok = 0;
- char *fname;
- GpgmeData plain;
- FILE *dstfp;
- size_t nread;
- char buf[BUFFSIZE];
- GpgmeError err;
-
- g_return_if_fail (msginfo != NULL);
- g_return_if_fail (mimeinfo != NULL);
- g_return_if_fail (fp != NULL);
- g_return_if_fail (mimeinfo->mime_type == MIME_MULTIPART);
-
- debug_print ("** decrypting multipart/encrypted message\n");
-
- /* skip headers */
- if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
- perror("fseek");
- tmpinfo = procmime_scan_mime_header(fp);
- if (!tmpinfo || tmpinfo->mime_type != MIME_MULTIPART) {
- DECRYPTION_ABORT();
- }
-
- procmime_scan_multipart_message(tmpinfo, fp);
-
- /* check that we have the 2 parts */
- partinfo = tmpinfo->children;
- if (!partinfo || !partinfo->next) {
- DECRYPTION_ABORT();
- }
- if (!g_strcasecmp (partinfo->content_type, "application/pgp-encrypted")) {
- /* Fixme: check that the version is 1 */
- ver_ok = 1;
- }
- partinfo = partinfo->next;
- if (ver_ok &&
- !g_strcasecmp (partinfo->content_type, "application/octet-stream")) {
- if (partinfo->next)
- g_warning ("oops: pgp_encrypted with more than 2 parts");
- }
- else {
- DECRYPTION_ABORT();
- }
-
- debug_print ("** yep, it is pgp encrypted\n");
-
- plain = pgp_decrypt (partinfo, fp);
- if (!plain) {
- DECRYPTION_ABORT();
- }
-
- fname = g_strdup_printf("%s%cplaintext.%08x",
- get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
-
- if ((dstfp = fopen(fname, "wb")) == NULL) {
- FILE_OP_ERROR(fname, "fopen");
- g_free(fname);
- DECRYPTION_ABORT();
- }
-
- /* write the orginal header to the new file */
- if (fseek(fp, tmpinfo->fpos, SEEK_SET) < 0)
- perror("fseek");
-
- while (fgets(buf, sizeof(buf), fp)) {
- if (headerp (buf, content_names))
- continue;
- if (buf[0] == '\r' || buf[0] == '\n')
- break;
- fputs (buf, dstfp);
- }
-
- err = gpgme_data_rewind (plain);
- if (err)
- debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
-
- while (!(err = gpgme_data_read (plain, buf, sizeof(buf), &nread))) {
- fwrite (buf, nread, 1, dstfp);
- }
-
- if (err != GPGME_EOF) {
- debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
- }
-
- fclose (dstfp);
- procmime_mimeinfo_free_all(tmpinfo);
-
- msginfo->plaintext_file = fname;
- msginfo->decryption_failed = 0;
-}
-
-#undef DECRYPTION_ABORT
-
-
/*
* plain contains an entire mime object.
* Encrypt it and return an GpgmeData object with the encrypted version of
GpgmeRecipients rset = NULL;
size_t nread;
int mime_version_seen = 0;
- char *boundary = generate_mime_boundary ("Encrypt");
+ char *boundary;
+
+ boundary = generate_mime_boundary ("Encrypt");
/* Create the list of recipients */
rset = gpgmegtk_recipient_selection (recp_list);
}
/* get the content header lines from the source */
- clineidx=0;
+ clineidx = 0;
saved_last = 0;
while (!err && fgets(buf, sizeof(buf), fp)) {
/* fixme: check for overlong lines */
continue;
}
if (saved_last) {
- saved_last = 0;
if (*buf == ' ' || *buf == '\t') {
- char *last = clines[clineidx-1];
- clines[clineidx-1] = g_strconcat (last, buf, NULL);
+ char *last = clines[clineidx - 1];
+ clines[clineidx - 1] = g_strconcat (last, buf, NULL);
g_free (last);
continue;
}
+ saved_last = 0;
}
if (headerp (buf, mime_version_name))
GpgmeData sigdata = NULL;
size_t nread;
int mime_version_seen = 0;
- char *boundary = generate_mime_boundary ("Signature");
+ char *boundary;
char *micalg = NULL;
char *siginfo;
+ boundary = generate_mime_boundary ("Signature");
+
/* Open the source file */
if ((fp = fopen(file, "rb")) == NULL) {
FILE_OP_ERROR(file, "fopen");
continue;
}
if (saved_last) {
- saved_last = 0;
if (*buf == ' ' || *buf == '\t') {
char *last = clines[clineidx - 1];
clines[clineidx - 1] = g_strconcat (last, buf, NULL);
g_free (last);
continue;
}
+ saved_last = 0;
}
if (headerp (buf, mime_version_name))
fprintf (fp, "Content-Type: multipart/signed; "
"protocol=\"application/pgp-signature\";\r\n");
if (micalg)
- fprintf (fp, " micalg=\"%s\";", micalg);
+ fprintf (fp, " micalg=\"%s\";\r\n", micalg);
fprintf (fp, " boundary=\"%s\"\r\n", boundary);
/* Part 1: signed material */