sort out some prototypes, put them where they belong.
[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/lib-lib.h>
30 #include <lib-ui/curses.h>
31
32 #include <lib-mime/mime.h>
33
34 static struct {
35   short id;
36   const char *name;
37 } HashAlgorithms[] = {
38   {
39   1, "pgp-md5"}, {
40   2, "pgp-sha1"}, {
41   3, "pgp-ripemd160"}, {
42   5, "pgp-md2"}, {
43   6, "pgp-tiger192"}, {
44   7, "pgp-haval-5-160"}, {
45   8, "pgp-sha256"}, {
46   9, "pgp-sha384"}, {
47   10, "pgp-sha512"}, {
48   -1, NULL}
49 };
50
51 static const char *pgp_hash_to_micalg (short id)
52 {
53   int i;
54
55   for (i = 0; HashAlgorithms[i].id >= 0; i++)
56     if (HashAlgorithms[i].id == id)
57       return HashAlgorithms[i].name;
58   return "x-unknown";
59 }
60
61 static void pgp_dearmor (FILE * in, FILE * out)
62 {
63   char line[HUGE_STRING];
64   off_t start;
65   off_t end;
66   char *r;
67
68   STATE state;
69
70   p_clear(&state, 1);
71   state.fpin = in;
72   state.fpout = out;
73
74   /* find the beginning of ASCII armor */
75
76   while ((r = fgets (line, sizeof (line), in)) != NULL) {
77     if (!strncmp (line, "-----BEGIN", 10))
78       break;
79   }
80   if (r == NULL) {
81     return;
82   }
83
84   /* skip the armor header */
85
86   while ((r = fgets (line, sizeof (line), in)) != NULL) {
87     r = vskipspaces(r);
88     if (!*r)
89       break;
90   }
91   if (r == NULL) {
92     return;
93   }
94
95   /* actual data starts here */
96   start = ftello (in);
97
98   /* find the checksum */
99
100   while ((r = fgets (line, sizeof (line), in)) != NULL) {
101     if (*line == '=' || !strncmp (line, "-----END", 8))
102       break;
103   }
104   if (r == NULL) {
105     return;
106   }
107
108   if ((end = ftello (in) - m_strlen(line)) < start) {
109     return;
110   }
111
112   if (fseeko (in, start, SEEK_SET) == -1) {
113     return;
114   }
115
116   mutt_decode_base64 (&state, end - start, 0, MUTT_ICONV_ERROR);
117 }
118
119 static short pgp_mic_from_packet (unsigned char *p, size_t len)
120 {
121   /* is signature? */
122   if ((p[0] & 0x3f) != PT_SIG) {
123     return -1;
124   }
125
126   if (len >= 18 && p[1] == 3)
127     /* version 3 signature */
128     return (short) p[17];
129   else if (len >= 5 && p[1] == 4)
130     /* version 4 signature */
131     return (short) p[4];
132   else {
133     return -1;
134   }
135 }
136
137 static short pgp_find_hash (const char *fname)
138 {
139   FILE *in = NULL;
140   FILE *out = NULL;
141
142   char tempfile[_POSIX_PATH_MAX];
143
144   unsigned char *p;
145   size_t l;
146
147   short rv = -1;
148
149   mutt_mktemp (tempfile);
150   if ((out = safe_fopen (tempfile, "w+")) == NULL) {
151     mutt_perror (tempfile);
152     goto bye;
153   }
154   unlink (tempfile);
155
156   if ((in = fopen (fname, "r")) == NULL) {
157     mutt_perror (fname);
158     goto bye;
159   }
160
161   pgp_dearmor (in, out);
162   rewind (out);
163
164   if ((p = pgp_read_packet (out, &l)) != NULL) {
165     rv = pgp_mic_from_packet (p, l);
166   }
167
168 bye:
169
170   safe_fclose (&in);
171   safe_fclose (&out);
172   pgp_release_packet ();
173   return rv;
174 }
175
176 const char *pgp_micalg (const char *fname)
177 {
178   return pgp_hash_to_micalg (pgp_find_hash (fname));
179 }