NeoMutt  2019-12-07-60-g0cfa53
Teaching an old dog new tricks
DOXYGEN
pgpmicalg.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <iconv.h>
31 #include <stdbool.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include "mutt/mutt.h"
35 #include "pgpmicalg.h"
36 #include "handler.h"
37 #include "pgppacket.h"
38 #include "state.h"
39 
43 static const struct
44 {
45  short id;
46  const char *name;
47 } HashAlgorithms[] = {
48  { 1, "pgp-md5" }, { 2, "pgp-sha1" }, { 3, "pgp-ripemd160" },
49  { 5, "pgp-md2" }, { 6, "pgp-tiger192" }, { 7, "pgp-haval-5-160" },
50  { 8, "pgp-sha256" }, { 9, "pgp-sha384" }, { 10, "pgp-sha512" },
51  { 11, "pgp-sha224" }, { -1, NULL },
52 };
53 
59 static const char *pgp_hash_to_micalg(short id)
60 {
61  for (int i = 0; HashAlgorithms[i].id >= 0; i++)
62  if (HashAlgorithms[i].id == id)
63  return HashAlgorithms[i].name;
64  return "x-unknown";
65 }
66 
72 static void pgp_dearmor(FILE *fp_in, FILE *fp_out)
73 {
74  char line[8192];
75  char *r = NULL;
76 
77  struct State state = { 0 };
78  state.fp_in = fp_in;
79  state.fp_out = fp_out;
80 
81  /* find the beginning of ASCII armor */
82 
83  while ((r = fgets(line, sizeof(line), fp_in)))
84  {
85  if (strncmp(line, "-----BEGIN", 10) == 0)
86  break;
87  }
88  if (!r)
89  {
90  mutt_debug(LL_DEBUG1, "Can't find begin of ASCII armor\n");
91  return;
92  }
93 
94  /* skip the armor header */
95 
96  while ((r = fgets(line, sizeof(line), fp_in)))
97  {
98  SKIPWS(r);
99  if (!*r)
100  break;
101  }
102  if (!r)
103  {
104  mutt_debug(LL_DEBUG1, "Armor header doesn't end\n");
105  return;
106  }
107 
108  /* actual data starts here */
109  LOFF_T start = ftello(fp_in);
110  if (start < 0)
111  return;
112 
113  /* find the checksum */
114 
115  while ((r = fgets(line, sizeof(line), fp_in)))
116  {
117  if ((*line == '=') || (strncmp(line, "-----END", 8) == 0))
118  break;
119  }
120  if (!r)
121  {
122  mutt_debug(LL_DEBUG1, "Can't find end of ASCII armor\n");
123  return;
124  }
125 
126  LOFF_T end = ftello(fp_in) - strlen(line);
127  if (end < start)
128  {
129  mutt_debug(LL_DEBUG1, "end < start???\n");
130  return;
131  }
132 
133  if (fseeko(fp_in, start, SEEK_SET) == -1)
134  {
135  mutt_debug(LL_DEBUG1, "Can't seekto start\n");
136  return;
137  }
138 
139  mutt_decode_base64(&state, end - start, false, (iconv_t) -1);
140 }
141 
148 static short pgp_mic_from_packet(unsigned char *p, size_t len)
149 {
150  /* is signature? */
151  if ((p[0] & 0x3f) != PT_SIG)
152  {
153  mutt_debug(LL_DEBUG1, "tag = %d, want %d\n", p[0] & 0x3f, PT_SIG);
154  return -1;
155  }
156 
157  if ((len >= 18) && (p[1] == 3))
158  {
159  /* version 3 signature */
160  return (short) p[17];
161  }
162  else if ((len >= 5) && (p[1] == 4))
163  {
164  /* version 4 signature */
165  return (short) p[4];
166  }
167  else
168  {
169  mutt_debug(LL_DEBUG1, "Bad signature packet\n");
170  return -1;
171  }
172 }
173 
179 static short pgp_find_hash(const char *fname)
180 {
181  size_t l;
182  short rc = -1;
183 
184  FILE *fp_out = mutt_file_mkstemp();
185  if (!fp_out)
186  {
187  mutt_perror(_("Can't create temporary file"));
188  goto bye;
189  }
190 
191  FILE *fp_in = fopen(fname, "r");
192  if (!fp_in)
193  {
194  mutt_perror(fname);
195  goto bye;
196  }
197 
198  pgp_dearmor(fp_in, fp_out);
199  rewind(fp_out);
200 
201  unsigned char *p = pgp_read_packet(fp_out, &l);
202  if (p)
203  {
204  rc = pgp_mic_from_packet(p, l);
205  }
206  else
207  {
208  mutt_debug(LL_DEBUG1, "No packet\n");
209  }
210 
211 bye:
212 
213  mutt_file_fclose(&fp_in);
214  mutt_file_fclose(&fp_out);
216  return rc;
217 }
218 
224 const char *pgp_micalg(const char *fname)
225 {
226  return pgp_hash_to_micalg(pgp_find_hash(fname));
227 }
#define mutt_perror(...)
Definition: logging.h:85
void pgp_release_packet(void)
Free the cached PGP packet.
Definition: pgppacket.c:239
static const struct @1 HashAlgorithms[]
PGP Hashing algorithms.
#define _(a)
Definition: message.h:28
FILE * fp_out
File to write to.
Definition: state.h:47
Signature Packet.
Definition: pgppacket.h:39
FILE * fp_in
File to read from.
Definition: state.h:46
void mutt_decode_base64(struct State *s, size_t len, bool istext, iconv_t cd)
Decode base64-encoded text.
Definition: handler.c:1487
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:152
const char * name
Definition: pgpmicalg.c:46
#define SKIPWS(ch)
Definition: string2.h:47
const char * line
Definition: common.c:36
#define mutt_file_mkstemp()
Definition: file.h:105
static const char * pgp_hash_to_micalg(short id)
Lookup a hash name, given its id.
Definition: pgpmicalg.c:59
Keep track when processing files.
Log at debug level 1.
Definition: logging.h:40
Keep track when processing files.
Definition: state.h:44
const char * pgp_micalg(const char *fname)
Find the hash algorithm of a file.
Definition: pgpmicalg.c:224
static void pgp_dearmor(FILE *fp_in, FILE *fp_out)
Unwrap an armoured PGP block.
Definition: pgpmicalg.c:72
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
Parse PGP data packets.
Decide how to display email content.
static short pgp_find_hash(const char *fname)
Find the hash algorithm of a file.
Definition: pgpmicalg.c:179
unsigned char * pgp_read_packet(FILE *fp, size_t *len)
Read a PGP packet from a file.
Definition: pgppacket.c:84
static short pgp_mic_from_packet(unsigned char *p, size_t len)
Get the hash algorithm from a PGP packet.
Definition: pgpmicalg.c:148
Identify the hash algorithm from a PGP signature.
short id
Definition: pgpmicalg.c:45