LCOV - code coverage report
Current view: top level - g10 - decrypt.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 62 122 50.8 %
Date: 2016-11-29 15:00:56 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /* decrypt.c - decrypt and verify data
       2             :  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
       3             :  *               2007, 2009 Free Software Foundation, Inc.
       4             :  *
       5             :  * This file is part of GnuPG.
       6             :  *
       7             :  * GnuPG is free software; you can redistribute it and/or modify
       8             :  * it under the terms of the GNU General Public License as published by
       9             :  * the Free Software Foundation; either version 3 of the License, or
      10             :  * (at your option) any later version.
      11             :  *
      12             :  * GnuPG is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :  * GNU General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU General Public License
      18             :  * along with this program; if not, see <https://www.gnu.org/licenses/>.
      19             :  */
      20             : 
      21             : #include <config.h>
      22             : #include <stdio.h>
      23             : #include <stdlib.h>
      24             : #include <string.h>
      25             : #include <errno.h>
      26             : 
      27             : #include "gpg.h"
      28             : #include "options.h"
      29             : #include "packet.h"
      30             : #include "status.h"
      31             : #include "iobuf.h"
      32             : #include "keydb.h"
      33             : #include "util.h"
      34             : #include "main.h"
      35             : #include "status.h"
      36             : #include "i18n.h"
      37             : 
      38             : /* Assume that the input is an encrypted message and decrypt
      39             :  * (and if signed, verify the signature on) it.
      40             :  * This command differs from the default operation, as it never
      41             :  * writes to the filename which is included in the file and it
      42             :  * rejects files which don't begin with an encrypted message.
      43             :  */
      44             : int
      45          12 : decrypt_message (ctrl_t ctrl, const char *filename)
      46             : {
      47             :   IOBUF fp;
      48          12 :   armor_filter_context_t *afx = NULL;
      49             :   progress_filter_context_t *pfx;
      50             :   int rc;
      51          12 :   int no_out = 0;
      52             : 
      53          12 :   pfx = new_progress_context ();
      54             : 
      55             :   /* Open the message file.  */
      56          12 :   fp = iobuf_open (filename);
      57          12 :   if (fp && is_secured_file (iobuf_get_fd (fp)))
      58             :     {
      59           0 :       iobuf_close (fp);
      60           0 :       fp = NULL;
      61           0 :       gpg_err_set_errno (EPERM);
      62             :     }
      63          12 :   if ( !fp )
      64             :     {
      65           0 :       rc = gpg_error_from_syserror ();
      66           0 :       log_error (_("can't open '%s': %s\n"), print_fname_stdin(filename),
      67             :                  gpg_strerror (rc));
      68           0 :       release_progress_context (pfx);
      69           0 :       return rc;
      70             :     }
      71             : 
      72          12 :   handle_progress (pfx, fp, filename);
      73             : 
      74          12 :   if ( !opt.no_armor )
      75             :     {
      76          12 :       if ( use_armor_filter( fp ) )
      77             :         {
      78           0 :           afx = new_armor_context ();
      79           0 :           push_armor_filter ( afx, fp );
      80             :         }
      81             :     }
      82             : 
      83          12 :   if (!opt.outfile)
      84             :     {
      85          12 :       no_out = 1;
      86          12 :       opt.outfile = "-";
      87             :     }
      88          12 :   rc = proc_encryption_packets (ctrl, NULL, fp );
      89          12 :   if (no_out)
      90          12 :     opt.outfile = NULL;
      91             : 
      92          12 :   iobuf_close (fp);
      93          12 :   release_armor_context (afx);
      94          12 :   release_progress_context (pfx);
      95          12 :   return rc;
      96             : }
      97             : 
      98             : 
      99             : /* Same as decrypt_message but takes a file descriptor for input and
     100             :    output.  */
     101             : gpg_error_t
     102           0 : decrypt_message_fd (ctrl_t ctrl, int input_fd, int output_fd)
     103             : {
     104             : #ifdef HAVE_W32_SYSTEM
     105             :   /* No server mode yet.  */
     106             :   (void)ctrl;
     107             :   (void)input_fd;
     108             :   (void)output_fd;
     109             :   return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
     110             : #else
     111             :   gpg_error_t err;
     112             :   IOBUF fp;
     113           0 :   armor_filter_context_t *afx = NULL;
     114             :   progress_filter_context_t *pfx;
     115             : 
     116           0 :   if (opt.outfp)
     117           0 :     return gpg_error (GPG_ERR_BUG);
     118             : 
     119           0 :   pfx = new_progress_context ();
     120             : 
     121             :   /* Open the message file.  */
     122           0 :   fp = iobuf_fdopen_nc (FD2INT(input_fd), "rb");
     123           0 :   if (fp && is_secured_file (iobuf_get_fd (fp)))
     124             :     {
     125           0 :       iobuf_close (fp);
     126           0 :       fp = NULL;
     127           0 :       gpg_err_set_errno (EPERM);
     128             :     }
     129           0 :   if (!fp)
     130             :     {
     131             :       char xname[64];
     132             : 
     133           0 :       err = gpg_error_from_syserror ();
     134           0 :       snprintf (xname, sizeof xname, "[fd %d]", input_fd);
     135           0 :       log_error (_("can't open '%s': %s\n"), xname, gpg_strerror (err));
     136           0 :       release_progress_context (pfx);
     137           0 :       return err;
     138             :     }
     139             : 
     140             : #ifdef HAVE_W32CE_SYSTEM
     141             : #warning Need to fix this if we want to use g13
     142             :   opt.outfp = NULL;
     143             : #else
     144           0 :   opt.outfp = es_fdopen_nc (output_fd, "wb");
     145             : #endif
     146           0 :   if (!opt.outfp)
     147             :     {
     148             :       char xname[64];
     149             : 
     150           0 :       err = gpg_error_from_syserror ();
     151           0 :       snprintf (xname, sizeof xname, "[fd %d]", output_fd);
     152           0 :       log_error (_("can't open '%s': %s\n"), xname, gpg_strerror (err));
     153           0 :       iobuf_close (fp);
     154           0 :       release_progress_context (pfx);
     155           0 :       return err;
     156             :     }
     157             : 
     158           0 :   if (!opt.no_armor)
     159             :     {
     160           0 :       if (use_armor_filter (fp))
     161             :         {
     162           0 :           afx = new_armor_context ();
     163           0 :           push_armor_filter ( afx, fp );
     164             :         }
     165             :     }
     166             : 
     167           0 :   err = proc_encryption_packets (ctrl, NULL, fp );
     168             : 
     169           0 :   iobuf_close (fp);
     170           0 :   es_fclose (opt.outfp);
     171           0 :   opt.outfp = NULL;
     172           0 :   release_armor_context (afx);
     173           0 :   release_progress_context (pfx);
     174           0 :   return err;
     175             : #endif
     176             : }
     177             : 
     178             : 
     179             : void
     180           1 : decrypt_messages (ctrl_t ctrl, int nfiles, char *files[])
     181             : {
     182             :   IOBUF fp;
     183           1 :   armor_filter_context_t *afx = NULL;
     184             :   progress_filter_context_t *pfx;
     185           1 :   char *p, *output = NULL;
     186           1 :   int rc=0,use_stdin=0;
     187           1 :   unsigned int lno=0;
     188             : 
     189           1 :   if (opt.outfile)
     190             :     {
     191           0 :       log_error(_("--output doesn't work for this command\n"));
     192           0 :       return;
     193             :     }
     194             : 
     195           1 :   pfx = new_progress_context ();
     196             : 
     197           1 :   if(!nfiles)
     198           0 :     use_stdin=1;
     199             : 
     200             :   for(;;)
     201             :     {
     202             :       char line[2048];
     203           4 :       char *filename=NULL;
     204             : 
     205           4 :       if(use_stdin)
     206             :         {
     207           0 :           if(fgets(line, DIM(line), stdin))
     208             :             {
     209           0 :               lno++;
     210           0 :               if (!*line || line[strlen(line)-1] != '\n')
     211           0 :                 log_error("input line %u too long or missing LF\n", lno);
     212             :               else
     213             :                 {
     214           0 :                   line[strlen(line)-1] = '\0';
     215           0 :                   filename=line;
     216             :                 }
     217             :             }
     218             :         }
     219             :       else
     220             :         {
     221           4 :           if(nfiles)
     222             :             {
     223           3 :               filename=*files;
     224           3 :               nfiles--;
     225           3 :               files++;
     226             :             }
     227             :         }
     228             : 
     229           4 :       if(filename==NULL)
     230           1 :         break;
     231             : 
     232           3 :       print_file_status(STATUS_FILE_START, filename, 3);
     233           3 :       output = make_outfile_name(filename);
     234           3 :       if (!output)
     235           0 :         goto next_file;
     236           3 :       fp = iobuf_open(filename);
     237           3 :       if (fp)
     238           3 :         iobuf_ioctl (fp, IOBUF_IOCTL_NO_CACHE, 1, NULL);
     239           3 :       if (fp && is_secured_file (iobuf_get_fd (fp)))
     240             :         {
     241           0 :           iobuf_close (fp);
     242           0 :           fp = NULL;
     243           0 :           gpg_err_set_errno (EPERM);
     244             :         }
     245           3 :       if (!fp)
     246             :         {
     247           0 :           log_error(_("can't open '%s'\n"), print_fname_stdin(filename));
     248           0 :           goto next_file;
     249             :         }
     250             : 
     251           3 :       handle_progress (pfx, fp, filename);
     252             : 
     253           3 :       if (!opt.no_armor)
     254             :         {
     255           3 :           if (use_armor_filter(fp))
     256             :             {
     257           3 :               afx = new_armor_context ();
     258           3 :               push_armor_filter ( afx, fp );
     259             :             }
     260             :         }
     261           3 :       rc = proc_packets (ctrl,NULL, fp);
     262           3 :       iobuf_close(fp);
     263           3 :       if (rc)
     264           0 :         log_error("%s: decryption failed: %s\n", print_fname_stdin(filename),
     265             :                   gpg_strerror (rc));
     266           3 :       p = get_last_passphrase();
     267           3 :       set_next_passphrase(p);
     268           3 :       xfree (p);
     269             : 
     270             :     next_file:
     271             :       /* Note that we emit file_done even after an error. */
     272           3 :       write_status( STATUS_FILE_DONE );
     273           3 :       xfree(output);
     274           3 :       reset_literals_seen();
     275           3 :     }
     276             : 
     277           1 :   set_next_passphrase(NULL);
     278           1 :   release_armor_context (afx);
     279           1 :   release_progress_context (pfx);
     280             : }

Generated by: LCOV version 1.11