smime.c 47.4 KB
Newer Older
1
/*
2
 * Copyright (C) 2001,2002 Oliver Ehli <[email protected]>
3
 * Copyright (C) 2002 Mike Schiraldi <[email protected]>
4
 * Copyright (C) 2004 g10 Code GmbH
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 * 
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
18
 *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 20
 */

21 22 23 24
#if HAVE_CONFIG_H
# include "config.h"
#endif

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include "mutt.h"
#include "mutt_curses.h"
#include "mutt_menu.h"
#include "smime.h"
#include "mime.h"
#include "copy.h"

#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <ctype.h>

#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif

#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif

#ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
#endif

52
#ifdef CRYPT_BACKEND_CLASSIC_SMIME
53

54
#include "mutt_crypt.h"
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

struct smime_command_context {
  const char *key;		    /* %k */
  const char *cryptalg;		    /* %a */
  const char *fname;		    /* %f */
  const char *sig_fname;	    /* %s */
  const char *certificates;	    /* %c */
  const char *intermediates;        /* %i */
};


typedef struct {
  unsigned int hash;
  char suffix;
  char email[256];
  char nick[256];
  char trust; /* i=Invalid r=revoked e=expired u=unverified v=verified t=trusted */
  short public; /* 1=public 0=private */
} smime_id;


char SmimePass[STRING];
time_t SmimeExptime = 0; /* when does the cached passphrase expire? */


80
static char SmimeKeyToUse[_POSIX_PATH_MAX] = { 0 };
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
static char SmimeCertToUse[_POSIX_PATH_MAX];
static char SmimeIntermediateToUse[_POSIX_PATH_MAX];


/*
 *     Queries and passphrase handling.
 */




/* these are copies from pgp.c */


void smime_void_passphrase (void)
{
  memset (SmimePass, 0, sizeof (SmimePass));
  SmimeExptime = 0;
}

101 102 103
int smime_valid_passphrase (void)
{
  time_t now = time (NULL);
104

105 106 107
  if (now < SmimeExptime)
    /* Use cached copy.  */
    return 1;
108

109 110
  smime_void_passphrase();
  
111
  if (mutt_get_password (_("Enter S/MIME passphrase:"), SmimePass, sizeof (SmimePass)) == 0)
112 113 114 115 116 117
    {
      SmimeExptime = time (NULL) + SmimeTimeout;
      return (1);
    }
  else
    SmimeExptime = 0;
118

119 120
  return 0;
}
121 122 123 124 125 126 127 128 129 130


/*
 *     The OpenSSL interface
 */

/* This is almost identical to ppgp's invoking interface. */

static const char *_mutt_fmt_smime_command (char *dest,
					    size_t destlen,
131
					    size_t col,
132 133 134 135 136 137 138 139 140 141 142 143 144 145
					    char op,
					    const char *src,
					    const char *prefix,
					    const char *ifstring,
					    const char *elsestring,
					    unsigned long data,
					    format_flag flags)
{
  char fmt[16];
  struct smime_command_context *cctx = (struct smime_command_context *) data;
  int optional = (flags & M_FORMAT_OPTIONAL);
  
  switch (op)
  {
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    case 'C':
    {
      if (!optional)
      {
	char path[_POSIX_PATH_MAX];
	char buf1[LONG_STRING], buf2[LONG_STRING];
	struct stat sb;

	strfcpy (path, NONULL (SmimeCALocation), sizeof (path));
	mutt_expand_path (path, sizeof (path));
	mutt_quote_filename (buf1, sizeof (buf1), path);

	if (stat (path, &sb) != 0 || !S_ISDIR (sb.st_mode))
	  snprintf (buf2, sizeof (buf2), "-CAfile %s", buf1);
	else
	  snprintf (buf2, sizeof (buf2), "-CApath %s", buf1);
	
	snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
	snprintf (dest, destlen, fmt, buf2);
      }
      else if (!SmimeCALocation)
	optional = 0;
      break;
    }
    
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    case 'c':
    {           /* certificate (list) */
      if (!optional) {
	snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
	snprintf (dest, destlen, fmt, NONULL(cctx->certificates));
      }
      else if (!cctx->certificates)
	optional = 0;
      break;
    }
    
    case 'i':
    {           /* intermediate certificates  */
      if (!optional) {
	snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
	snprintf (dest, destlen, fmt, NONULL(cctx->intermediates));
      }
      else if (!cctx->intermediates)
	optional = 0;
      break;
    }
    
    case 's':
    {           /* detached signature */
      if (!optional)
      {
	snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
	snprintf (dest, destlen, fmt, NONULL (cctx->sig_fname));
      }
      else if (!cctx->sig_fname)
	optional = 0;
      break;
    }
    
    case 'k':
    {           /* private key */
      if (!optional)
      {
	snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
	snprintf (dest, destlen, fmt, NONULL (cctx->key));
      }
      else if (!cctx->key)
	optional = 0;
      break;
    }
    
    case 'a':
    {           /* algorithm for encryption */
      if (!optional) {
	snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
	snprintf (dest, destlen, fmt, NONULL (cctx->cryptalg));
      }
      else if (!cctx->key)
	optional = 0;
      break;
    }
    
    case 'f':
    {           /* file to process */
      if (!optional)
      {
	snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
	snprintf (dest, destlen, fmt, NONULL (cctx->fname));
      }
      else if (!cctx->fname)
	optional = 0;
      break;
    }
    
    default:
      *dest = '\0';
      break;
  }

  if (optional)
246
    mutt_FormatString (dest, destlen, col, ifstring, _mutt_fmt_smime_command,
247 248
		       data, 0);
  else if (flags & M_FORMAT_OPTIONAL)
249
    mutt_FormatString (dest, destlen, col, elsestring, _mutt_fmt_smime_command,
250 251 252 253 254 255 256 257 258 259
		       data, 0);

  return (src);
}



static void mutt_smime_command (char *d, size_t dlen,
				struct smime_command_context *cctx, const char *fmt)
{
260
  mutt_FormatString (d, dlen, 0, NONULL(fmt), _mutt_fmt_smime_command,
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
		    (unsigned long) cctx, 0);
  dprint (2,(debugfile, "mutt_smime_command: %s\n", d));
}




static pid_t smime_invoke (FILE **smimein, FILE **smimeout, FILE **smimeerr,
			   int smimeinfd, int smimeoutfd, int smimeerrfd,
			   const char *fname,
			   const char *sig_fname,
			   const char *cryptalg,
			   const char *key,
			   const char *certificates,
			   const char *intermediates,
			   const char *format)
{
  struct smime_command_context cctx;
  char cmd[HUGE_STRING];
  
  memset (&cctx, 0, sizeof (cctx));

  if (!format || !*format)
    return (pid_t) -1;
  
  cctx.fname	       = fname;
  cctx.sig_fname       = sig_fname;
  cctx.key	       = key;
  cctx.cryptalg	       = cryptalg;
  cctx.certificates    = certificates;
  cctx.intermediates   = intermediates;
  
  mutt_smime_command (cmd, sizeof (cmd), &cctx, format);

  return mutt_create_filter_fd (cmd, smimein, smimeout, smimeerr,
				smimeinfd, smimeoutfd, smimeerrfd);
}






/*
 *    Key and certificate handling.
 */



/* 
   Search the certificate index for given mailbox.
   return certificate file name.
*/

static void smime_entry (char *s, size_t l, MUTTMENU * menu, int num)
{
  smime_id *Table = (smime_id*) menu->data;
  smime_id this = Table[num];
  char* truststate;
  switch(this.trust) {
    case 't':
      truststate = N_("Trusted   ");
      break;
    case 'v':
      truststate = N_("Verified  ");
      break;
    case 'u':
      truststate = N_("Unverified");
      break;
    case 'e':
      truststate = N_("Expired   ");
      break;
    case 'r':
      truststate = N_("Revoked   ");
      break;
    case 'i':
      truststate = N_("Invalid   ");
      break;
    default:
      truststate = N_("Unknown   ");
  }
  if (this.public)
343
    snprintf(s, l, " 0x%.8X.%i %s %-35.35s %s", this.hash, this.suffix, truststate, this.email, this.nick);
344
  else
345
    snprintf(s, l, " 0x%.8X.%i %-35.35s %s", this.hash, this.suffix, this.email, this.nick);
346 347 348 349 350 351 352 353 354
}





char* smime_ask_for_key (char *prompt, char *mailbox, short public)
{
  char *fname;
355 356
  smime_id *table = 0;
  int table_count;
357 358
  char index_file[_POSIX_PATH_MAX];
  FILE *index;
359
  char buf[LONG_STRING];
360
  char fields[5][STRING+1]; /* +1 due to use of fscanf() below. the max field width does not include the null terminator (see http://dev.mutt.org/trac/ticket/3636) */
361 362 363
  int numFields, hash_suffix, done, cur; /* The current entry */
  MUTTMENU* menu;
  unsigned int hash;
364
  char helpstr[HUGE_STRING*3];
365 366 367
  char qry[256];
  char title[256];

368
  if (!prompt) prompt = _("Enter keyID: ");
369 370 371 372 373 374 375 376 377
  snprintf(index_file, sizeof (index_file), "%s/.index",
    public ? NONULL(SmimeCertificates) : NONULL(SmimeKeys));
  
  FOREVER
  {
    *qry = 0;
    if (mutt_get_field(prompt,
      qry, sizeof(qry), 0))
      return NULL;
378
    snprintf(title, sizeof(title), _("S/MIME certificates matching \"%s\"."),
379 380 381 382
      qry);

    
    index = fopen(index_file, "r");
383 384 385 386 387
    if (index == NULL) 
    {
      mutt_perror (index_file);      
      return NULL;
    }
388 389
    /* Read Entries */
    cur = 0;
390
    table_count = 0;
391
    while (!feof(index)) {
392
        numFields = fscanf (index, MUTT_FORMAT(STRING) " %x.%i " MUTT_FORMAT(STRING), fields[0], &hash,
393 394
          &hash_suffix, fields[2]);
        if (public)
Michael Elkins's avatar
Michael Elkins committed
395
          fscanf (index, MUTT_FORMAT(STRING) " " MUTT_FORMAT(STRING) "\n", fields[3], fields[4]);
396 397 398 399 400 401 402 403 404
  
      /* 0=email 1=name 2=nick 3=intermediate 4=trust */
      if (numFields < 2) continue;
  
      /* Check if query matches this certificate */
      if (!mutt_stristr(fields[0], qry) &&
          !mutt_stristr(fields[2], qry))
        continue;
  
405 406 407 408 409 410 411 412
      ++table_count;
      safe_realloc(&table, sizeof(smime_id) * table_count);
      table[cur].hash = hash;
      table[cur].suffix = hash_suffix;
      strncpy(table[cur].email, fields[0], sizeof(table[cur].email));
      strncpy(table[cur].nick, fields[2], sizeof(table[cur].nick));
      table[cur].trust = *fields[4];
      table[cur].public = public;
413 414 415
  
      cur++;
    }
416
    safe_fclose (&index);
417 418 419 420
  
    /* Make Helpstring */
    helpstr[0] = 0;
    mutt_make_help (buf, sizeof (buf), _("Exit  "), MENU_SMIME, OP_EXIT);
421
    strcat (helpstr, buf);	/* __STRCAT_CHECKED__ */
422 423
    mutt_make_help (buf, sizeof (buf), _("Select  "), MENU_SMIME,
        OP_GENERIC_SELECT_ENTRY);
424
    strcat (helpstr, buf);	/* __STRCAT_CHECKED__ */
425
    mutt_make_help (buf, sizeof(buf), _("Help"), MENU_SMIME, OP_HELP);
426
    strcat (helpstr, buf);	/* __STRCAT_CHECKED__ */
427 428
  
    /* Create the menu */
429
    menu = mutt_new_menu(MENU_SMIME);
430 431 432
    menu->max = cur;
    menu->make_entry = smime_entry;
    menu->help = helpstr;
433
    menu->data = table;
434 435 436 437 438 439 440 441 442 443 444
    menu->title = title;
    /* sorting keys might be done later - TODO */
  
    mutt_clear_error();
  
    done = 0;
    hash = 0;
    while (!done) {
      switch (mutt_menuLoop (menu)) {
        case OP_GENERIC_SELECT_ENTRY:
          cur = menu->current;
445
	  hash = 1;
446 447 448 449 450 451 452 453
          done = 1;
          break;
        case OP_EXIT:
          hash = 0;
          done = 1;
          break;
      }
    }
454 455
    if (table_count && hash)
      safe_asprintf(&fname, "%.8x.%i", table[cur].hash, table[cur].suffix);
456 457 458
    else fname = NULL;
  
    mutt_menuDestroy (&menu);
459
    FREE (&table);
460 461 462 463 464 465 466 467
    set_option (OPTNEEDREDRAW);
  
    if (fname) return fname;
  }
}



Thomas Roessler's avatar
Thomas Roessler committed
468
char *smime_get_field_from_db (char *mailbox, char *query, short public, short may_ask)
469 470 471 472
{
  int addr_len, query_len, found = 0, ask = 0, choice = 0;
  char cert_path[_POSIX_PATH_MAX];
  char buf[LONG_STRING], prompt[STRING];
473
  char fields[5][STRING+1]; /* +1 due to use of fscanf() below. the max field width does not include the null terminator (see http://dev.mutt.org/trac/ticket/3636) */
474
  char key[STRING];  
475 476
  int numFields;
  struct stat info;
477
  char key_trust_level = 0;
478 479 480 481 482 483
  FILE *fp;

  if(!mailbox && !query) return(NULL);

  addr_len = mailbox ? mutt_strlen (mailbox) : 0;
  query_len = query ? mutt_strlen (query) : 0;
484 485
  
  *key = '\0';
486 487 488 489 490 491 492 493 494

  /* index-file format:
     mailbox certfile label issuer_certfile trust_flags\n

     certfile is a hash value generated by openssl.
     Note that this was done according to the OpenSSL
     specs on their CA-directory.

  */
495
  snprintf (cert_path, sizeof (cert_path), "%s/.index",
496 497 498 499 500 501 502 503 504 505 506 507 508
	    (public ? NONULL(SmimeCertificates) : NONULL(SmimeKeys)));

  if (!stat (cert_path, &info))
  {
    if ((fp = safe_fopen (cert_path, "r")) == NULL)
    {
      mutt_perror (cert_path);
      return (NULL);
    }

    while (fgets (buf, sizeof (buf) - 1, fp) != NULL)
      if (mailbox && !(mutt_strncasecmp (mailbox, buf, addr_len)))
      {
509 510 511 512 513 514 515
	numFields = sscanf (buf, 
			    MUTT_FORMAT(STRING) " " MUTT_FORMAT(STRING) " " 
			    MUTT_FORMAT(STRING) " " MUTT_FORMAT(STRING) " " 
			    MUTT_FORMAT(STRING) "\n", 
			    fields[0], fields[1],
			   fields[2], fields[3], 
			    fields[4]);
516 517 518
	if (numFields < 2)
	    continue;
	if (mailbox && public && 
519
	    (*fields[4] == 'i' || *fields[4] == 'e' || *fields[4] == 'r'))
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
	    continue;

	if (found)
	{
	  if (public && *fields[4] == 'u' )
	    snprintf (prompt, sizeof (prompt),
		      _("ID %s is unverified. Do you want to use it for %s ?"),
		      fields[1], mailbox);
	  else if (public && *fields[4] == 'v' )
	    snprintf (prompt, sizeof (prompt),
		      _("Use (untrusted!) ID %s for %s ?"),
		      fields[1], mailbox);
	  else
	    snprintf (prompt, sizeof (prompt), _("Use ID %s for %s ?"),
		      fields[1], mailbox);
Thomas Roessler's avatar
Thomas Roessler committed
535 536 537
	  if (may_ask == 0)
	    choice = M_YES;
	  if (may_ask && (choice = mutt_yesorno (prompt, M_NO)) == -1)
538 539 540
	  {
	    found = 0;
	    ask = 0;
541
	    *key = '\0';
542 543 544 545 546 547 548 549 550
	    break;
	  }
	  else if (choice == M_NO) 
	  {
	    ask = 1;
	    continue;
	  }
	  else if (choice == M_YES)
	  {
551
	    strfcpy (key, fields[1], sizeof (key));
552 553 554 555 556 557
	    ask = 0;
	    break;
	  }
	}
	else
	{
558 559 560
	  if (public) 
	    key_trust_level = *fields[4];
	  strfcpy (key, fields[1], sizeof (key));
561 562 563 564 565
	}
	found = 1;
      }
      else if(query)
      {
566 567 568 569 570 571 572
	numFields = sscanf (buf, 
			    MUTT_FORMAT(STRING) " " MUTT_FORMAT(STRING) " " 
			    MUTT_FORMAT(STRING) " " MUTT_FORMAT(STRING) " " 
			    MUTT_FORMAT(STRING) "\n", 
			    fields[0], fields[1],
			    fields[2], fields[3], 
			    fields[4]);
573 574 575 576 577 578

	/* query = label: return certificate. */
	if (numFields >= 3 && 
	    !(mutt_strncasecmp (query, fields[2], query_len)))
	{
	  ask = 0;
579
	  strfcpy (key, fields[1], sizeof (key));
580 581 582 583 584 585
	}
	/* query = certificate: return intermediate certificate. */
	else if (numFields >= 4 && 
		 !(mutt_strncasecmp (query, fields[1], query_len)))
	{
	  ask = 0;
586
	  strfcpy (key, fields[3], sizeof (key));
587 588 589
	}
      }

590
    safe_fclose (&fp);
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606

    if (ask)
    {
      if (public && *fields[4] == 'u' )
	snprintf (prompt, sizeof (prompt),
		  _("ID %s is unverified. Do you want to use it for %s ?"),
		  fields[1], mailbox);
      else if (public && *fields[4] == 'v' )
	snprintf (prompt, sizeof (prompt),
		  _("Use (untrusted!) ID %s for %s ?"),
		  fields[1], mailbox);
      else
	snprintf (prompt, sizeof(prompt), _("Use ID %s for %s ?"), key,
		  mailbox);
      choice = mutt_yesorno (prompt, M_NO);
      if (choice == -1 || choice == M_NO)
607
	*key = '\0';
608
    }
Thomas Roessler's avatar
Thomas Roessler committed
609
    else if (key_trust_level && may_ask)
610 611 612 613 614 615 616 617
    {
      if (key_trust_level == 'u' )
      {
	snprintf (prompt, sizeof (prompt),
		  _("ID %s is unverified. Do you want to use it for %s ?"),
		  key, mailbox);
	choice = mutt_yesorno (prompt, M_NO);
	if (choice != M_YES)
618
	  *key = '\0';
619 620 621
      }
      else if (key_trust_level == 'v' )
      {
Thomas Roessler's avatar
Thomas Roessler committed
622
	mutt_error (_("Warning: You have not yet decided to trust ID %s. (any key to continue)"), key);
623
	mutt_sleep (5);
624 625 626 627 628
      }
    }

  }

629 630
  /* Note: safe_strdup ("") returns NULL. */
  return safe_strdup (key);
631 632 633 634 635 636 637
}




/* 
   This sets the '*ToUse' variables for an upcoming decryption, where
Ondřej Bílka's avatar
Ondřej Bílka committed
638
   the required key is different from SmimeDefaultKey.
639 640
*/

641
void _smime_getkeys (char *mailbox)
642
{
643
  char *k = NULL;
644 645
  char buf[STRING];

646 647
  k = smime_get_field_from_db (mailbox, NULL, 0, 1);

648 649
  if (!k)
  {
650
    snprintf(buf, sizeof(buf), _("Enter keyID for %s: "),
651 652 653 654 655 656 657
	     mailbox);
    k = smime_ask_for_key(buf, mailbox, 0);
  }

  if (k)
  {
    /* the key used last time. */
658 659
    if (*SmimeKeyToUse && 
        !mutt_strcasecmp (k, SmimeKeyToUse + mutt_strlen (SmimeKeys)+1))
660
    {
661
      FREE (&k);
662 663 664 665 666 667 668 669 670 671
      return;
    }
    else smime_void_passphrase ();

    snprintf (SmimeKeyToUse, sizeof (SmimeKeyToUse), "%s/%s", 
	      NONULL(SmimeKeys), k);
    
    snprintf (SmimeCertToUse, sizeof (SmimeCertToUse), "%s/%s",
	      NONULL(SmimeCertificates), k);

672
    if (mutt_strcasecmp (k, SmimeDefaultKey))
673 674
      smime_void_passphrase ();

675
    FREE (&k);
676 677 678
    return;
  }

679 680
  if (*SmimeKeyToUse)
  {
681
    if (!mutt_strcasecmp (SmimeDefaultKey, 
682 683 684 685 686 687
                          SmimeKeyToUse + mutt_strlen (SmimeKeys)+1))
      return;

    smime_void_passphrase ();
  }

688
  snprintf (SmimeKeyToUse, sizeof (SmimeKeyToUse), "%s/%s", 
689
	    NONULL (SmimeKeys), NONULL (SmimeDefaultKey));
690 691
  
  snprintf (SmimeCertToUse, sizeof (SmimeCertToUse), "%s/%s",
692
	    NONULL (SmimeCertificates), NONULL (SmimeDefaultKey));
693 694
}

695 696 697 698
void smime_getkeys (ENVELOPE *env)
{
  ADDRESS *t;
  int found = 0;
699

700 701 702 703 704 705 706 707 708 709 710
  if (option (OPTSDEFAULTDECRYPTKEY) && SmimeDefaultKey && *SmimeDefaultKey)
  {
    snprintf (SmimeKeyToUse, sizeof (SmimeKeyToUse), "%s/%s", 
	      NONULL (SmimeKeys), SmimeDefaultKey);
    
    snprintf (SmimeCertToUse, sizeof (SmimeCertToUse), "%s/%s",
	      NONULL(SmimeCertificates), SmimeDefaultKey);

    return;
  }

711 712 713 714
  for (t = env->to; !found && t; t = t->next)
    if (mutt_addr_is_user (t))
    {
      found = 1;
Thomas Roessler's avatar
ups  
Thomas Roessler committed
715 716
      _smime_getkeys (t->mailbox);
    }
717 718 719 720
  for (t = env->cc; !found && t; t = t->next)
    if (mutt_addr_is_user (t))
    {
      found = 1;
Thomas Roessler's avatar
ups  
Thomas Roessler committed
721
      _smime_getkeys (t->mailbox);
722 723 724 725 726 727 728
    }
  if (!found && (t = mutt_default_from()))
  {
    _smime_getkeys (t->mailbox);
    rfc822_free_address (&t);
  }
}
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755

/* This routine attempts to find the keyids of the recipients of a message.
 * It returns NULL if any of the keys can not be found.
 */

char *smime_findKeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
{
  char *keyID, *keylist = NULL;
  size_t keylist_size = 0;
  size_t keylist_used = 0;
  ADDRESS *tmp = NULL, *addr = NULL;
  ADDRESS **last = &tmp;
  ADDRESS *p, *q;
  int i;

  const char *fqdn = mutt_fqdn (1);
  
  for (i = 0; i < 3; i++)
  {
    switch (i)
    {
      case 0: p = to; break;
      case 1: p = cc; break;
      case 2: p = bcc; break;
      default: abort ();
    }
    
756
    *last = rfc822_cpy_adr (p, 0);
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
    while (*last)
      last = &((*last)->next);
  }

  if (fqdn)
    rfc822_qualify (tmp, fqdn);

  tmp = mutt_remove_duplicates (tmp);
  
  for (p = tmp; p ; p = p->next)
  {
    char buf[LONG_STRING];

    q = p;

Thomas Roessler's avatar
Thomas Roessler committed
772
    if ((keyID = smime_get_field_from_db (q->mailbox, NULL, 1, 1)) == NULL)
773 774
    {
      snprintf(buf, sizeof(buf),
775
	       _("Enter keyID for %s: "),
776 777 778 779 780
	       q->mailbox);
      keyID = smime_ask_for_key(buf, q->mailbox, 1);
    }
    if(!keyID)
    {
Thomas Roessler's avatar
Thomas Roessler committed
781
      mutt_message (_("No (valid) certificate found for %s."), q->mailbox);
782
      FREE (&keylist);
783 784 785 786 787
      rfc822_free_address (&tmp);
      rfc822_free_address (&addr);
      return NULL;
    }
    
788
    keylist_size += mutt_strlen (keyID) + 2;
789
    safe_realloc (&keylist, keylist_size);
790
    sprintf (keylist + keylist_used, "%s\n", keyID);	/* __SPRINTF_CHECKED__ */
791 792 793 794 795 796 797 798 799 800 801
    keylist_used = mutt_strlen (keylist);

    rfc822_free_address (&addr);

  }
  rfc822_free_address (&tmp);
  return (keylist);
}



802 803 804



805 806
static int smime_handle_cert_email (char *certificate, char *mailbox,
				   int copy, char ***buffer, int *num)
807 808 809
{
  FILE *fpout = NULL, *fperr = NULL;
  char tmpfname[_POSIX_PATH_MAX];
810
  char email[STRING];
811
  int ret = -1, count = 0;
812 813
  pid_t thepid;

814
  mutt_mktemp (tmpfname, sizeof (tmpfname));
815 816 817 818 819 820 821
  if ((fperr = safe_fopen (tmpfname, "w+")) == NULL)
  {
    mutt_perror (tmpfname);
    return 1;
  }
  mutt_unlink (tmpfname);

822
  mutt_mktemp (tmpfname, sizeof (tmpfname));
823 824
  if ((fpout = safe_fopen (tmpfname, "w+")) == NULL)
  {
825
    safe_fclose (&fperr);
826 827 828 829 830
    mutt_perror (tmpfname);
    return 1;
  }
  mutt_unlink (tmpfname);

831
  if ((thepid =  smime_invoke (NULL, NULL, NULL,
832 833 834 835 836
			       -1, fileno (fpout), fileno (fperr),
			       certificate, NULL, NULL, NULL, NULL, NULL,
			       SmimeGetCertEmailCommand))== -1)
  {
    mutt_message (_("Error: unable to create OpenSSL subprocess!"));
837 838
    safe_fclose (&fperr);
    safe_fclose (&fpout);
839 840 841 842 843 844 845 846
    return 1;
  }

  mutt_wait_filter (thepid);

  fflush (fpout);
  rewind (fpout);
  fflush (fperr);
847
  rewind (fperr);
848

849

850 851
  while ((fgets (email, sizeof (email), fpout)))
  {
852 853 854 855 856 857
    *(email + mutt_strlen (email)-1) = '\0';
    if(mutt_strncasecmp (email, mailbox, mutt_strlen (mailbox)) == 0)
      ret=1;

    ret = ret < 0 ? 0 : ret;
    count++;
858
  }
859

860
  if (ret == -1)
861 862
  {
    mutt_endwin(NULL);
Thomas Roessler's avatar
Thomas Roessler committed
863 864
    mutt_copy_stream (fperr, stdout);
    mutt_any_key_to_continue (_("Error: unable to create OpenSSL subprocess!"));
865
    ret = 1;
866
  }
867
  else if (!ret)
868
    ret = 1;
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
  else ret = 0;

  if(copy && buffer && num)
  {
    (*num) = count;
    *buffer =  safe_calloc(sizeof(char*), count);
    count = 0;

    rewind (fpout);
    while ((fgets (email, sizeof (email), fpout)))
    {
      *(email + mutt_strlen (email) - 1) = '\0';
      (*buffer)[count] = safe_calloc(1, mutt_strlen (email) + 1);
      strncpy((*buffer)[count], email, mutt_strlen (email));
      count++;
    }
  }
  else if(copy) ret = 2;
887

888 889
  safe_fclose (&fpout);
  safe_fclose (&fperr);
890

891
  return ret;
892 893 894 895
}



896
static char *smime_extract_certificate (char *infile)
897 898 899 900 901 902 903 904
{
  FILE *fpout = NULL, *fperr = NULL;
  char pk7out[_POSIX_PATH_MAX], certfile[_POSIX_PATH_MAX];
  char tmpfname[_POSIX_PATH_MAX];
  pid_t thepid;
  int empty;


905
  mutt_mktemp (tmpfname, sizeof (tmpfname));
906 907 908 909 910 911 912
  if ((fperr = safe_fopen (tmpfname, "w+")) == NULL)
  {
    mutt_perror (tmpfname);
    return NULL;
  }
  mutt_unlink (tmpfname);

913
  mutt_mktemp (pk7out, sizeof (pk7out));
914 915
  if ((fpout = safe_fopen (pk7out, "w+")) == NULL)
  {
916
    safe_fclose (&fperr);
917 918 919 920 921 922 923 924 925 926 927 928 929
    mutt_perror (pk7out);
    return NULL;
  }

  /* Step 1: Convert the signature to a PKCS#7 structure, as we can't
     extract the full set of certificates directly.
  */
  if ((thepid =  smime_invoke (NULL, NULL, NULL,
			       -1, fileno (fpout), fileno (fperr),
			       infile, NULL, NULL, NULL, NULL, NULL,
			       SmimePk7outCommand))== -1)
  {
    mutt_any_key_to_continue (_("Error: unable to create OpenSSL subprocess!"));
930 931
    safe_fclose (&fperr);
    safe_fclose (&fpout);
932 933 934 935 936 937 938 939 940 941
    mutt_unlink (pk7out);
    return NULL;
  }

  mutt_wait_filter (thepid);


  fflush (fpout);
  rewind (fpout);
  fflush (fperr);
942
  rewind (fperr);
943 944 945 946 947
  empty = (fgetc (fpout) == EOF);
  if (empty)
  {
    mutt_perror (pk7out);
    mutt_copy_stream (fperr, stdout);
948 949
    safe_fclose (&fpout);
    safe_fclose (&fperr);
950 951 952 953 954 955
    mutt_unlink (pk7out);
    return NULL;
    
  }


956
  safe_fclose (&fpout);
957
  mutt_mktemp (certfile, sizeof (certfile));
958 959
  if ((fpout = safe_fopen (certfile, "w+")) == NULL)
  {
960
    safe_fclose (&fperr);
961 962 963 964 965 966 967 968 969 970 971 972 973
    mutt_unlink (pk7out);
    mutt_perror (certfile);
    return NULL;
  }
  
  /* Step 2: Extract the certificates from a PKCS#7 structure.
   */
  if ((thepid =  smime_invoke (NULL, NULL, NULL,
			       -1, fileno (fpout), fileno (fperr),
			       pk7out, NULL, NULL, NULL, NULL, NULL,
			       SmimeGetCertCommand))== -1)
  {
    mutt_any_key_to_continue (_("Error: unable to create OpenSSL subprocess!"));
974 975
    safe_fclose (&fperr);
    safe_fclose (&fpout);
976 977 978 979 980 981 982 983 984 985 986 987
    mutt_unlink (pk7out);
    mutt_unlink (certfile);
    return NULL;
  }

  mutt_wait_filter (thepid);

  mutt_unlink (pk7out);

  fflush (fpout);
  rewind (fpout);
  fflush (fperr);
988
  rewind (fperr);
989 990 991 992
  empty =  (fgetc (fpout) == EOF);
  if (empty)
  {
    mutt_copy_stream (fperr, stdout);
993 994
    safe_fclose (&fpout);
    safe_fclose (&fperr);
995 996 997 998
    mutt_unlink (certfile);
    return NULL;
  }

999 1000
  safe_fclose (&fpout);
  safe_fclose (&fperr);
1001 1002 1003 1004

  return safe_strdup (certfile);
}

1005
static char *smime_extract_signer_certificate (char *infile)
1006 1007 1008 1009 1010 1011 1012 1013
{
  FILE *fpout = NULL, *fperr = NULL;
  char pk7out[_POSIX_PATH_MAX], certfile[_POSIX_PATH_MAX];
  char tmpfname[_POSIX_PATH_MAX];
  pid_t thepid;
  int empty;


1014
  mutt_mktemp (tmpfname, sizeof (tmpfname));
1015 1016 1017 1018 1019 1020 1021 1022
  if ((fperr = safe_fopen (tmpfname, "w+")) == NULL)
  {
    mutt_perror (tmpfname);
    return NULL;
  }
  mutt_unlink (tmpfname);


1023
  mutt_mktemp (certfile, sizeof (certfile));
1024 1025
  if ((fpout = safe_fopen (certfile, "w+")) == NULL)
  {
1026
    safe_fclose (&fperr);
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
    mutt_perror (certfile);
    return NULL;
  }
  
  /* Extract signer's certificate
   */
  if ((thepid =  smime_invoke (NULL, NULL, NULL,
			       -1, -1, fileno (fperr),
			       infile, NULL, NULL, NULL, certfile, NULL,
			       SmimeGetSignerCertCommand))== -1)
  {
    mutt_any_key_to_continue (_("Error: unable to create OpenSSL subprocess!"));
1039 1040
    safe_fclose (&fperr);
    safe_fclose (&fpout);
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
    mutt_unlink (pk7out);
    mutt_unlink (certfile);
    return NULL;
  }

  mutt_wait_filter (thepid);

  fflush (fpout);
  rewind (fpout);
  fflush (fperr);
1051
  rewind (fperr);
1052 1053 1054
  empty =  (fgetc (fpout) == EOF);
  if (empty)
  {
1055
    mutt_endwin (NULL);
1056
    mutt_copy_stream (fperr, stdout);
1057
    mutt_any_key_to_continue (NULL);
1058 1059
    safe_fclose (&fpout);
    safe_fclose (&fperr);
1060 1061 1062 1063
    mutt_unlink (certfile);
    return NULL;
  }

1064 1065
  safe_fclose (&fpout);
  safe_fclose (&fperr);
1066 1067 1068 1069 1070

  return safe_strdup (certfile);
}


1071

Thomas Roessler's avatar
Thomas Roessler committed
1072 1073 1074 1075

/* Add a certificate and update index file (externally). */

void smime_invoke_import (char *infile, char *mailbox)
1076
{
Thomas Roessler's avatar
Thomas Roessler committed
1077 1078 1079
  char tmpfname[_POSIX_PATH_MAX], *certfile = NULL, buf[STRING];
  FILE *smimein=NULL, *fpout = NULL, *fperr = NULL;
  pid_t thepid=-1;
1080

1081
  mutt_mktemp (tmpfname, sizeof (tmpfname));
1082 1083 1084
  if ((fperr = safe_fopen (tmpfname, "w+")) == NULL)
  {
    mutt_perror (tmpfname);
Thomas Roessler's avatar
Thomas Roessler committed
1085
    return;
1086 1087 1088
  }
  mutt_unlink (tmpfname);

1089
  mutt_mktemp (tmpfname, sizeof (tmpfname));
1090 1091
  if ((fpout = safe_fopen (tmpfname, "w+")) == NULL)
  {
1092
    safe_fclose (&fperr);
1093
    mutt_perror (tmpfname);
Thomas Roessler's avatar
Thomas Roessler committed
1094
    return;
1095 1096 1097 1098
  }
  mutt_unlink (tmpfname);


Thomas Roessler's avatar
Thomas Roessler committed
1099 1100 1101
  buf[0] = '\0';
  if (option (OPTASKCERTLABEL))
    mutt_get_field ("Label for certificate:", buf, sizeof (buf), 0);
1102

Thomas Roessler's avatar
Thomas Roessler committed
1103 1104
  mutt_endwin (NULL);
  if ((certfile = smime_extract_certificate(infile)))
1105
  {
Thomas Roessler's avatar
Thomas Roessler committed
1106
    mutt_endwin (NULL);
1107
  
Thomas Roessler's avatar
Thomas Roessler committed
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
    if ((thepid =  smime_invoke (&smimein, NULL, NULL,
				 -1, fileno(fpout), fileno(fperr),
				 certfile, NULL, NULL, NULL, NULL, NULL,
				 SmimeImportCertCommand))== -1)
    {
      mutt_message (_("Error: unable to create OpenSSL subprocess!"));
      return;
    }
    fputs (buf, smimein);
    fputc ('\n', smimein);
1118
    safe_fclose (&smimein);
1119

Thomas Roessler's avatar
Thomas Roessler committed
1120
    mutt_wait_filter (thepid);
1121
  
Thomas Roessler's avatar
Thomas Roessler committed
1122
    mutt_unlink (certfile);
1123
    FREE (&certfile);
1124 1125 1126 1127 1128
  }

  fflush (fpout);
  rewind (fpout);
  fflush (fperr);
1129
  rewind (fperr);
1130

Thomas Roessler's avatar
Thomas Roessler committed
1131 1132 1133
  mutt_copy_stream (fpout, stdout);
  mutt_copy_stream (fperr, stdout);

1134 1135
  safe_fclose (&fpout);
  safe_fclose (&fperr);
1136

1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
}



int smime_verify_sender(HEADER *h)
{
  char *mbox = NULL, *certfile, tempfname[_POSIX_PATH_MAX];
  FILE *fpout;
  int retval=1;

1147
  mutt_mktemp (tempfname, sizeof (tempfname));
1148 1149 1150 1151 1152 1153 1154 1155 1156
  if (!(fpout = safe_fopen (tempfname, "w")))
  {
    mutt_perror (tempfname);
    return 1;
  }

  if(h->security & ENCRYPT)
    mutt_copy_message (fpout, Context, h,
		       M_CM_DECODE_CRYPT & M_CM_DECODE_SMIME,
1157
		       CH_MIME|CH_WEED|CH_NONEWLINE);
1158 1159 1160 1161
  else
    mutt_copy_message (fpout, Context, h, 0, 0);

  fflush(fpout);
1162
  safe_fclose (&fpout);
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176

  if (h->env->from)
  {
    h->env->from = mutt_expand_aliases (h->env->from); 
    mbox = h->env->from->mailbox; 
  }
  else if (h->env->sender)
  {
    h->env->sender = mutt_expand_aliases (h->env->sender); 
    mbox = h->env->sender->mailbox; 
  }

  if (mbox)
  {
1177
    if ((certfile = smime_extract_signer_certificate(tempfname)))
1178 1179
    {
      mutt_unlink(tempfname);
1180
      if (smime_handle_cert_email (certfile, mbox, 0, NULL, NULL))
Thomas Roessler's avatar
Thomas Roessler committed
1181 1182 1183 1184
      {
	if(isendwin())
         mutt_any_key_to_continue(NULL);
      }
1185 1186 1187
      else
	retval = 0;
      mutt_unlink(certfile);
1188
      FREE (&certfile);
1189 1190
    }
  else 
1191
	mutt_any_key_to_continue(_("no certfile"));
1192 1193
  }
  else 
1194
	mutt_any_key_to_continue(_("no mbox"));
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251

  mutt_unlink(tempfname);
  return retval;
}









/*
 *    Creating S/MIME - bodies.
 */




static
pid_t smime_invoke_encrypt (FILE **smimein, FILE **smimeout, FILE **smimeerr,
			    int smimeinfd, int smimeoutfd, int smimeerrfd,
			    const char *fname, const char *uids)
{
  return smime_invoke (smimein, smimeout, smimeerr,
		       smimeinfd, smimeoutfd, smimeerrfd,
		       fname, NULL, SmimeCryptAlg, NULL, uids, NULL,
		       SmimeEncryptCommand);
}


static
pid_t smime_invoke_sign (FILE **smimein, FILE **smimeout, FILE **smimeerr,
			 int smimeinfd, int smimeoutfd, int smimeerrfd, 
			 const char *fname)
{
  return smime_invoke (smimein, smimeout, smimeerr, smimeinfd, smimeoutfd,
		       smimeerrfd, fname, NULL, NULL, SmimeKeyToUse,
		       SmimeCertToUse, SmimeIntermediateToUse,
		       SmimeSignCommand);
}




BODY *smime_build_smime_entity (BODY *a, char *certlist)
{
  char buf[LONG_STRING], certfile[LONG_STRING];
  char tempfile[_POSIX_PATH_MAX], smimeerrfile[_POSIX_PATH_MAX];
  char smimeinfile[_POSIX_PATH_MAX];
  char *cert_start = certlist, *cert_end = certlist;
  FILE *smimein = NULL, *smimeerr = NULL, *fpout = NULL, *fptmp = NULL;
  BODY *t;
  int err = 0, empty;
  pid_t thepid;
  
1252
  mutt_mktemp (tempfile, sizeof (tempfile));
1253 1254 1255 1256 1257 1258
  if ((fpout = safe_fopen (tempfile, "w+")) == NULL)
  {
    mutt_perror (tempfile);
    return (NULL);
  }

1259
  mutt_mktemp (smimeerrfile, sizeof (smimeerrfile));
1260 1261 1262
  if ((smimeerr = safe_fopen (smimeerrfile, "w+")) == NULL)
  {
    mutt_perror (smimeerrfile);
1263
    safe_fclose (&fpout);
1264 1265 1266 1267 1268
    mutt_unlink (tempfile);
    return NULL;
  }
  mutt_unlink (smimeerrfile);
  
1269
  mutt_mktemp (smimeinfile, sizeof (smimeinfile));
1270 1271 1272 1273
  if ((fptmp = safe_fopen (smimeinfile, "w+")) == NULL)
  {
    mutt_perror (smimeinfile);
    mutt_unlink (tempfile);
1274 1275
    safe_fclose (&fpout);
    safe_fclose (&smimeerr);
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
    return NULL;
  }

  *certfile = '\0';
  while (1)
  {
    int off = mutt_strlen (certfile);
    while (*++cert_end && *cert_end != '\n');
    if (!*cert_end) break;
    *cert_end = '\0';
    snprintf (certfile+off, sizeof (certfile)-off, " %s/%s",
	      NONULL(SmimeCertificates), cert_start);
    *cert_end = '\n';
    cert_start = cert_end;
    cert_start++;
  }

  /* write a MIME entity */
  mutt_write_mime_header (a, fptmp);
  fputc ('\n', fptmp);
  mutt_write_mime_body (a, fptmp);
1297
  safe_fclose (&fptmp);
1298 1299 1300 1301 1302 1303

  if ((thepid =
       smime_invoke_encrypt (&smimein, NULL, NULL, -1,
			     fileno (fpout), fileno (smimeerr),
			     smimeinfile, certfile)) == -1)
  {
1304
    safe_fclose (&smimeerr);
1305 1306 1307 1308 1309
    mutt_unlink (smimeinfile);
    mutt_unlink (certfile);
    return (NULL);
  }

1310
  safe_fclose (&smimein);
1311 1312 1313 1314 1315 1316 1317 1318
  
  mutt_wait_filter (thepid);
  mutt_unlink (smimeinfile);
  mutt_unlink (certfile);
  
  fflush (fpout);
  rewind (fpout);
  empty = (fgetc (fpout) == EOF);
1319
  safe_fclose (&fpout);
1320
 
1321 1322 1323 1324 1325 1326 1327
  fflush (smimeerr);
  rewind (smimeerr);
  while (fgets (buf, sizeof (buf) - 1, smimeerr) != NULL)
  {
    err = 1;
    fputs (buf, stdout);
  }
1328
  safe_fclose (&smimeerr);
1329 1330 1331 1332 1333 1334 1335 1336

  /* pause if there is any error output from SMIME */
  if (err)
    mutt_any_key_to_continue (NULL);

  if (empty)
  {
    /* fatal error while trying to encrypt message */
1337
    if (!err) mutt_any_key_to_continue _("No output from OpenSSL..");
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
    mutt_unlink (tempfile);
    return (NULL);
  }

  t = mutt_new_body ();
  t->type = TYPEAPPLICATION;
  t->subtype = safe_strdup ("x-pkcs7-mime");
  mutt_set_parameter ("name", "smime.p7m", &t->parameter);
  mutt_set_parameter ("smime-type", "enveloped-data", &t->parameter);
  t->encoding = ENCBASE64;  /* The output of OpenSSL SHOULD be binary */
  t->use_disp = 1;
  t->disposition = DISPATTACH;
  t->d_filename = safe_strdup ("smime.p7m");
  t->filename = safe_strdup (tempfile);
  t->unlink = 1; /*delete after sending the message */
  t->parts=0;
  t->next=0;
  
  return (t);
}




BODY *smime_sign_message (BODY *a )
{
  BODY *t;
  char buffer[LONG_STRING];
  char signedfile[_POSIX_PATH_MAX], filetosign[_POSIX_PATH_MAX];
  FILE *smimein = NULL, *smimeout = NULL, *smimeerr = NULL, *sfp = NULL;
  int err = 0;
  int empty = 0;
  pid_t thepid;
1371
  char *intermediates = smime_get_field_from_db(NULL, SmimeDefaultKey, 1, 1);
1372

1373 1374 1375
  if (!SmimeDefaultKey)
  {
    mutt_error _("Can't sign: No key specified. Use Sign As.");
1376
    FREE (&intermediates);
1377 1378 1379
    return NULL;
  }

1380 1381 1382
  if (!intermediates)
  {
    mutt_message(_("Warning: Intermediate certificate not found."));
1383
    intermediates = SmimeDefaultKey; /* so openssl won't complain in any case */
1384 1385 1386 1387
  }

  convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */

1388
  mutt_mktemp (filetosign, sizeof (filetosign));
1389 1390 1391
  if ((sfp = safe_fopen (filetosign, "w+")) == NULL)
  {
    mutt_perror (filetosign);
1392 1393
    if (intermediates != SmimeDefaultKey)
      FREE (&intermediates);
1394 1395 1396
    return NULL;
  }

1397
  mutt_mktemp (signedfile, sizeof (signedfile));
1398 1399 1400
  if ((smimeout = safe_fopen (signedfile, "w+")) == NULL)
  {
    mutt_perror (signedfile);
1401
    safe_fclose (&sfp);
1402
    mutt_unlink (filetosign);
1403 1404
    if (intermediates != SmimeDefaultKey)
      FREE (&intermediates);
1405 1406 1407 1408 1409 1410
    return NULL;
  }
  
  mutt_write_mime_header (a, sfp);
  fputc ('\n', sfp);
  mutt_write_mime_body (a, sfp);
1411
  safe_fclose (&sfp);
1412 1413 1414 1415

  

  snprintf (SmimeKeyToUse, sizeof (SmimeKeyToUse), "%s/%s", 
1416
	   NONULL(SmimeKeys), SmimeDefaultKey);