8fe6a5edb41a329deeea08e7944453ecf56e12db
[apps/madmutt.git] / lib-crypt / pgpmicalg.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 2001 Thomas Roessler <roessler@does-not-exist.org>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 /* This module peeks at a PGP signature and figures out the hash
11  * algorithm.
12  */
13
14 #if HAVE_CONFIG_H
15 # include "config.h"
16 #endif
17
18 #include "mutt.h"
19 #include "handler.h"
20 #include "pgp.h"
21 #include "pgppacket.h"
22 #include "charset.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28
29 #include <lib-lib/file.h>
30
31 #include <lib-mime/mime.h>
32
33 static struct {
34   short id;
35   const char *name;
36 } HashAlgorithms[] = {
37   {
38   1, "pgp-md5"}, {
39   2, "pgp-sha1"}, {
40   3, "pgp-ripemd160"}, {
41   5, "pgp-md2"}, {
42   6, "pgp-tiger192"}, {
43   7, "pgp-haval-5-160"}, {
44   8, "pgp-sha256"}, {
45   9, "pgp-sha384"}, {
46   10, "pgp-sha512"}, {
47   -1, NULL}
48 };
49
50 static const char *pgp_hash_to_micalg (short id)
51 {
52   int i;
53
54   for (i = 0; HashAlgorithms[i].id >= 0; i++)
55     if (HashAlgorithms[i].id == id)
56       return HashAlgorithms[i].name;
57   return "x-unknown";
58 }
59
60 static void pgp_dearmor (FILE * in, FILE * out)
61 {
62   char line[HUGE_STRING];
63   off_t start;
64   off_t end;
65   char *r;
66
67   STATE state;
68
69   p_clear(&state, 1);
70   state.fpin = in;
71   state.fpout = out;
72
73   /* find the beginning of ASCII armor */
74
75   while ((r = fgets (line, sizeof (line), in)) != NULL) {
76     if (!strncmp (line, "-----BEGIN", 10))
77       break;
78   }
79   if (r == NULL) {
80     return;
81   }
82
83   /* skip the armor header */
84
85   while ((r = fgets (line, sizeof (line), in)) != NULL) {
86     r = vskipspaces(r);
87     if (!*r)
88       break;
89   }
90   if (r == NULL) {
91     return;
92   }
93
94   /* actual data starts here */
95   start = ftello (in);
96
97   /* find the checksum */
98
99   while ((r = fgets (line, sizeof (line), in)) != NULL) {
100     if (*line == '=' || !strncmp (line, "-----END", 8))
101       break;
102   }
103   if (r == NULL) {
104     return;
105   }
106
107   if ((end = ftello (in) - m_strlen(line)) < start) {
108     return;
109   }
110
111   if (fseeko (in, start, SEEK_SET) == -1) {
112     return;
113   }
114
115   mutt_decode_base64 (&state, end - start, 0, (iconv_t) - 1);
116 }
117
118 static short pgp_mic_from_packet (unsigned char *p, size_t len)
119 {
120   /* is signature? */
121   if ((p[0] & 0x3f) != PT_SIG) {
122     return -1;
123   }
124
125   if (len >= 18 && p[1] == 3)
126     /* version 3 signature */
127     return (short) p[17];
128   else if (len >= 5 && p[1] == 4)
129     /* version 4 signature */
130     return (short) p[4];
131   else {
132     return -1;
133   }
134 }
135
136 static short pgp_find_hash (const char *fname)
137 {
138   FILE *in = NULL;
139   FILE *out = NULL;
140
141   char tempfile[_POSIX_PATH_MAX];
142
143   unsigned char *p;
144   size_t l;
145
146   short rv = -1;
147
148   mutt_mktemp (tempfile);
149   if ((out = safe_fopen (tempfile, "w+")) == NULL) {
150     mutt_perror (tempfile);
151     goto bye;
152   }
153   unlink (tempfile);
154
155   if ((in = fopen (fname, "r")) == NULL) {
156     mutt_perror (fname);
157     goto bye;
158   }
159
160   pgp_dearmor (in, out);
161   rewind (out);
162
163   if ((p = pgp_read_packet (out, &l)) != NULL) {
164     rv = pgp_mic_from_packet (p, l);
165   }
166
167 bye:
168
169   safe_fclose (&in);
170   safe_fclose (&out);
171   pgp_release_packet ();
172   return rv;
173 }
174
175 const char *pgp_micalg (const char *fname)
176 {
177   return pgp_hash_to_micalg (pgp_find_hash (fname));
178 }