NULL
};
-
-struct passphrase_cb_info_s {
- GpgmeCtx c;
- int did_it;
-};
-
static char *create_boundary (void);
#if 0
gpgmegtk_sig_status_destroy (statuswindow);
}
-static const char *
-passphrase_cb (void *opaque, const char *desc, void *r_hd)
-{
- struct passphrase_cb_info_s *info = opaque;
- GpgmeCtx ctx = info ? info->c : NULL;
- const char *pass;
-
- if (!desc) {
- /* FIXME: cleanup by looking at *r_hd */
- return NULL;
- }
-
- gpgmegtk_set_passphrase_grab (prefs_common.passphrase_grab);
- debug_print ("%% requesting passphrase for `%s': ", desc );
- pass = gpgmegtk_passphrase_mbox (desc);
- if (!pass) {
- debug_print ("%% cancel passphrase entry");
- gpgme_cancel (ctx);
- }
- else
- debug_print ("%% sending passphrase");
-
- return pass;
-}
-
/*
* Copy a gpgme data object to a temporary file and
* return this filename
tmp = g_strdup_printf("%s%cgpgtmp.%08x",
get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id );
- if ((fp = fopen(tmp, "w")) == NULL) {
+ if ((fp = fopen(tmp, "wb")) == NULL) {
FILE_OP_ERROR(tmp, "fopen");
g_free(tmp);
return NULL;
if (!getenv("GPG_AGENT_INFO")) {
info.c = ctx;
- gpgme_set_passphrase_cb (ctx, passphrase_cb, &info);
+ 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;
return 0;
if (g_strcasecmp (mimeinfo->content_type, "multipart/encrypted"))
return 0;
- /* fixme: we should schek the protocol parameter */
+ /* fixme: we should check the protocol parameter */
return 1;
}
+gboolean rfc2015_msg_is_encrypted (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)
{
fname = g_strdup_printf("%s%cplaintext.%08x",
get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
- if ((dstfp = fopen(fname, "w")) == NULL) {
+ if ((dstfp = fopen(fname, "wb")) == NULL) {
FILE_OP_ERROR(fname, "fopen");
g_free(fname);
DECRYPTION_ABORT();
/*
* plain contains an entire mime object. Sign it and return an
* GpgmeData object with the signature of it or NULL in case of error.
+ * r_siginfo returns an XML object with information about the signature.
*/
static GpgmeData
-pgp_sign (GpgmeData plain, GSList *key_list)
+pgp_sign (GpgmeData plain, GSList *key_list, char **r_siginfo)
{
GSList *p;
GpgmeCtx ctx = NULL;
GpgmeData sig = NULL;
struct passphrase_cb_info_s info;
+ *r_siginfo = NULL;
memset (&info, 0, sizeof info);
err = gpgme_new (&ctx);
if (!getenv("GPG_AGENT_INFO")) {
info.c = ctx;
- gpgme_set_passphrase_cb (ctx, passphrase_cb, &info);
+ gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
}
gpgme_set_textmode (ctx, 1);
gpgme_set_armor (ctx, 1);
if (err)
goto leave;
err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_DETACH);
+ if (!err)
+ *r_siginfo = gpgme_get_op_info (ctx, 0);
leave:
if (err) {
+ gpgmegtk_free_passphrase();
debug_print ("signing failed: %s\n", gpgme_strerror (err));
gpgme_data_release (sig);
sig = NULL;
return sig;
}
+/*
+ * Find TAG in XML and return a pointer into xml set just behind the
+ * closing angle. Return NULL if not found.
+ */
+static const char *
+find_xml_tag (const char *xml, const char *tag)
+{
+ int taglen = strlen (tag);
+ const char *s = xml;
+
+ while ( (s = strchr (s, '<')) ) {
+ s++;
+ if (!strncmp (s, tag, taglen)) {
+ const char *s2 = s + taglen;
+ if (*s2 == '>' || isspace (*(const unsigned char*)s2) ) {
+ /* found */
+ while (*s2 && *s2 != '>') /* skip attributes */
+ s2++;
+ /* fixme: do need to handle angles inside attribute vallues? */
+ return *s2? (s2+1):NULL;
+ }
+ }
+ while (*s && *s != '>') /* skip to end of tag */
+ s++;
+ }
+ return NULL;
+}
+
+
+/*
+ * Extract the micalg from an GnupgOperationInfo XML container.
+ */
+const char *
+extract_micalg (char *xml)
+{
+ const char *s;
+
+ s = find_xml_tag (xml, "GnupgOperationInfo");
+ if (s) {
+ const char *s_end = find_xml_tag (s, "/GnupgOperationInfo");
+ s = find_xml_tag (s, "signature");
+ if (s && s_end && s < s_end) {
+ const char *s_end2 = find_xml_tag (s, "/signature");
+ if (s_end2 && s_end2 < s_end) {
+ s = find_xml_tag (s, "micalg");
+ if (s && s < s_end2) {
+ s_end = strchr (s, '<');
+ if (s_end) {
+ char *p = g_malloc (s_end - s + 1);
+ memcpy (p, s, s_end - s);
+ p[s_end-s] = 0;
+ return p;
+ }
+ }
+ }
+ }
+ }
+ return NULL;
+}
+
+
/*
* Sign the file and replace its content with the signed one.
*/
size_t nread;
int mime_version_seen = 0;
char *boundary = create_boundary ();
+ char *micalg = NULL;
+ char *siginfo;
/* Open the source file */
if ((fp = fopen(file, "rb")) == NULL) {
goto failure;
}
- sigdata = pgp_sign (plain, key_list);
+ sigdata = pgp_sign (plain, key_list, &siginfo);
+ if (siginfo) {
+ micalg = extract_micalg (siginfo);
+ free (siginfo);
+ }
if (!sigdata)
goto failure;
if (!mime_version_seen)
fputs ("MIME-Version: 1\r\n", fp);
fprintf (fp, "Content-Type: multipart/signed; "
- "protocol=\"application/pgp-signature\";\r\n"
- " boundary=\"%s\"\r\n", boundary );
+ "protocol=\"application/pgp-signature\";\r\n");
+ if (micalg)
+ fprintf (fp, " micalg=%s;", micalg );
+ fprintf (fp, " boundary=\"%s\"\r\n", boundary );
/* Part 1: signed material */
fprintf (fp, "\r\n--%s\r\n", boundary);
gpgme_data_release (plain);
gpgme_data_release (sigdata);
g_free (boundary);
+ g_free (micalg);
return 0;
failure:
gpgme_data_release (plain);
gpgme_data_release (sigdata);
g_free (boundary);
+ g_free (micalg);
return -1; /* error */
}