File: pgpmicalg.c

package info (click to toggle)
mutt 1.7.2-1%2Bdeb9u3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 34,504 kB
  • sloc: ansic: 95,992; sh: 4,733; perl: 1,149; makefile: 706; yacc: 318; python: 33
file content (211 lines) | stat: -rw-r--r-- 4,450 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
52
53
54
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
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
/*
 * Copyright (C) 2001 Thomas Roessler <roessler@does-not-exist.org>
 * 
 * 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 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */

/* This module peeks at a PGP signature and figures out the hash
 * algorithm.
 */

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include "mutt.h"
#include "pgp.h"
#include "pgppacket.h"
#include "charset.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

static const struct 
{
  short id;
  const char *name;
} 
HashAlgorithms[] = 
{
  { 1,		"pgp-md5"  		},
  { 2,  	"pgp-sha1" 		},
  { 3,  	"pgp-ripemd160" 	},
  { 5,  	"pgp-md2"		},
  { 6,  	"pgp-tiger192"		},
  { 7,		"pgp-haval-5-160" 	},
  { 8,		"pgp-sha256"		},
  { 9,		"pgp-sha384"		},
  { 10,		"pgp-sha512"		},
  { 11,		"pgp-sha224"		},
  { -1, 	NULL }
};

static const char *pgp_hash_to_micalg (short id)
{
  int i;
  
  for (i = 0; HashAlgorithms[i].id >= 0; i++)
    if (HashAlgorithms[i].id == id)
      return HashAlgorithms[i].name;
  return "x-unknown";
}

static void pgp_dearmor (FILE *in, FILE *out)
{
  char line[HUGE_STRING];
  LOFF_T start;
  LOFF_T end;
  char *r;

  STATE state;
  
  memset (&state, 0, sizeof (STATE));
  state.fpin = in;
  state.fpout = out;
  
  /* find the beginning of ASCII armor */
  
  while ((r = fgets (line, sizeof (line), in)) != NULL)
  {
    if (!strncmp (line, "-----BEGIN", 10))
      break;
  }
  if (r == NULL)
  {
    dprint (1, (debugfile, "pgp_dearmor: Can't find begin of ASCII armor.\n"));
    return;
  }

  /* skip the armor header */
  
  while ((r = fgets (line, sizeof (line), in)) != NULL)
  {
    SKIPWS (r);
    if (!*r) break;
  }
  if (r == NULL)
  {
    dprint (1, (debugfile, "pgp_dearmor: Armor header doesn't end.\n"));
    return;
  }
  
  /* actual data starts here */
  start = ftello (in);
  
  /* find the checksum */
  
  while ((r = fgets (line, sizeof (line), in)) != NULL)
  {
    if (*line == '=' || !strncmp (line, "-----END", 8))
      break;
  }
  if (r == NULL)
  {
    dprint (1, (debugfile, "pgp_dearmor: Can't find end of ASCII armor.\n"));
    return;
  }
  
  if ((end = ftello (in) - strlen (line)) < start)
  {
    dprint (1, (debugfile, "pgp_dearmor: end < start???\n"));
    return;
  }
  
  if (fseeko (in, start, SEEK_SET) == -1)
  {
    dprint (1, (debugfile, "pgp_dearmor: Can't seekto start.\n"));
    return;
  }

  mutt_decode_base64 (&state, end - start, 0, (iconv_t) -1);
}

static short pgp_mic_from_packet (unsigned char *p, size_t len)
{
  /* is signature? */
  if ((p[0] & 0x3f) != PT_SIG)
  {
    dprint (1, (debugfile, "pgp_mic_from_packet: tag = %d, want %d.\n",
		p[0]&0x3f, PT_SIG));
    return -1;
  }
  
  if (len >= 18 && p[1] == 3)
    /* version 3 signature */
    return (short) p[17];
  else if (len >= 5 && p[1] == 4)
    /* version 4 signature */
    return (short) p[4];
  else
  {
    dprint (1, (debugfile, "pgp_mic_from_packet: Bad signature packet.\n"));
    return -1;
  }
}

static short pgp_find_hash (const char *fname)
{
  FILE *in = NULL;
  FILE *out = NULL;
  
  char tempfile[_POSIX_PATH_MAX];
  
  unsigned char *p;
  size_t l;
  
  short rv = -1;
  
  mutt_mktemp (tempfile, sizeof (tempfile));
  if ((out = safe_fopen (tempfile, "w+")) == NULL)
  {
    mutt_perror (tempfile);
    goto bye;
  }
  unlink (tempfile);
  
  if ((in = fopen (fname, "r")) == NULL)
  {
    mutt_perror (fname);
    goto bye;
  }
  
  pgp_dearmor (in, out);
  rewind (out);

  if ((p = pgp_read_packet (out, &l)) != NULL)
  {
    rv = pgp_mic_from_packet (p, l);
  }
  else
  {
    dprint (1, (debugfile, "pgp_find_hash: No packet.\n"));
  }
  
  bye:
  
  safe_fclose (&in);
  safe_fclose (&out);
  pgp_release_packet ();
  return rv;
}

const char *pgp_micalg (const char *fname)
{
  return pgp_hash_to_micalg (pgp_find_hash (fname));
}