~ubuntu-branches/ubuntu/precise/gnupg2/precise-proposed

« back to all changes in this revision

Viewing changes to g10/decrypt.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Mueller
  • Date: 2005-03-29 10:30:32 UTC
  • Revision ID: james.westby@ubuntu.com-20050329103032-sj42n2ain3ipx310
Tags: upstream-1.9.15
ImportĀ upstreamĀ versionĀ 1.9.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* decrypt.c - verify signed data
 
2
 * Copyright (C) 1998,1999,2000,2001,2002,2003 Free Software Foundation, Inc.
 
3
 *
 
4
 * This file is part of GnuPG.
 
5
 *
 
6
 * GnuPG is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * GnuPG is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
#include <errno.h>
 
26
#include <assert.h>
 
27
 
 
28
#include "options.h"
 
29
#include "packet.h"
 
30
#include "errors.h"
 
31
#include "iobuf.h"
 
32
#include "keydb.h"
 
33
#include "memory.h"
 
34
#include "util.h"
 
35
#include "main.h"
 
36
#include "status.h"
 
37
#include "i18n.h"
 
38
 
 
39
 
 
40
 
 
41
/****************
 
42
 * Assume that the input is an encrypted message and decrypt
 
43
 * (and if signed, verify the signature on) it.
 
44
 * This command differs from the default operation, as it never
 
45
 * writes to the filename which is included in the file and it
 
46
 * rejects files which don't begin with an encrypted message.
 
47
 */
 
48
 
 
49
int
 
50
decrypt_message( const char *filename )
 
51
{
 
52
    iobuf_t fp;
 
53
    armor_filter_context_t afx;
 
54
    progress_filter_context_t pfx;
 
55
    int rc;
 
56
    int no_out=0;
 
57
 
 
58
    /* open the message file */
 
59
    fp = iobuf_open(filename);
 
60
    if( !fp ) {
 
61
        rc = gpg_error_from_errno (errno);
 
62
        log_error(_("can't open `%s'\n"), print_fname_stdin(filename));
 
63
        return rc;
 
64
    }
 
65
 
 
66
    handle_progress (&pfx, fp, filename);
 
67
 
 
68
    if( !opt.no_armor ) {
 
69
        if( use_armor_filter( fp ) ) {
 
70
            memset( &afx, 0, sizeof afx);
 
71
            iobuf_push_filter( fp, armor_filter, &afx );
 
72
        }
 
73
    }
 
74
 
 
75
    if( !opt.outfile ) {
 
76
        no_out = 1;
 
77
        opt.outfile = "-";
 
78
    }
 
79
    rc = proc_encryption_packets( NULL, fp );
 
80
    if( no_out )
 
81
       opt.outfile = NULL;
 
82
    iobuf_close(fp);
 
83
    return rc;
 
84
}
 
85
 
 
86
void
 
87
decrypt_messages(int nfiles, char **files)
 
88
{
 
89
  iobuf_t fp;
 
90
  armor_filter_context_t afx;  
 
91
  progress_filter_context_t pfx;
 
92
  char *p, *output = NULL;
 
93
  int rc = 0;
 
94
  
 
95
  if (opt.outfile)
 
96
    {
 
97
      log_error(_("--output doesn't work for this command\n"));
 
98
      return;
 
99
        
 
100
    }
 
101
 
 
102
  while (nfiles--)
 
103
    {
 
104
      print_file_status(STATUS_FILE_START, *files, 3);      
 
105
      output = make_outfile_name(*files);
 
106
      if (!output)
 
107
        goto next_file;
 
108
      fp = iobuf_open(*files);
 
109
      if (!fp)
 
110
        {
 
111
          log_error(_("can't open `%s'\n"), print_fname_stdin(*files));
 
112
          goto next_file;
 
113
        }
 
114
 
 
115
      handle_progress (&pfx, fp, *files);
 
116
 
 
117
      if (!opt.no_armor)
 
118
        {
 
119
          if (use_armor_filter(fp))
 
120
            {
 
121
              memset(&afx, 0, sizeof afx);
 
122
              iobuf_push_filter(fp, armor_filter, &afx);
 
123
            }
 
124
        }
 
125
      rc = proc_packets(NULL, fp);
 
126
      iobuf_close(fp);
 
127
      if (rc)
 
128
        log_error("%s: decryption failed: %s\n", print_fname_stdin(*files),
 
129
                  gpg_strerror (rc));
 
130
      p = get_last_passphrase();
 
131
      set_next_passphrase(p);
 
132
      xfree (p);
 
133
 
 
134
    next_file:
 
135
      /* Note that we emit file_done even after an error. */
 
136
      write_status( STATUS_FILE_DONE );
 
137
      xfree (output);
 
138
      files++;
 
139
    }
 
140
  set_next_passphrase(NULL);  
 
141
}
 
142