2 * Copyright notice from original mutt:
3 * Copyright (C) 2001 Thomas Roessler <roessler@does-not-exist.org>
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.
10 /* This module peeks at a PGP signature and figures out the hash
21 #include "pgppacket.h"
29 #include <lib-lib/file.h>
31 #include <lib-mime/mime.h>
33 #include "lib/debug.h"
38 } HashAlgorithms[] = {
42 3, "pgp-ripemd160"}, {
45 7, "pgp-haval-5-160"}, {
52 static const char *pgp_hash_to_micalg (short id)
56 for (i = 0; HashAlgorithms[i].id >= 0; i++)
57 if (HashAlgorithms[i].id == id)
58 return HashAlgorithms[i].name;
62 static void pgp_dearmor (FILE * in, FILE * out)
64 char line[HUGE_STRING];
75 /* find the beginning of ASCII armor */
77 while ((r = fgets (line, sizeof (line), in)) != NULL) {
78 if (!strncmp (line, "-----BEGIN", 10))
82 debug_print (1, ("Can't find begin of ASCII armor.\n"));
86 /* skip the armor header */
88 while ((r = fgets (line, sizeof (line), in)) != NULL) {
94 debug_print (1, ("Armor header doesn't end.\n"));
98 /* actual data starts here */
101 /* find the checksum */
103 while ((r = fgets (line, sizeof (line), in)) != NULL) {
104 if (*line == '=' || !strncmp (line, "-----END", 8))
108 debug_print (1, ("Can't find end of ASCII armor.\n"));
112 if ((end = ftello (in) - m_strlen(line)) < start) {
113 debug_print (1, ("end < start???\n"));
117 if (fseeko (in, start, SEEK_SET) == -1) {
118 debug_print (1, ("Can't seekto start.\n"));
122 mutt_decode_base64 (&state, end - start, 0, (iconv_t) - 1);
125 static short pgp_mic_from_packet (unsigned char *p, size_t len)
128 if ((p[0] & 0x3f) != PT_SIG) {
129 debug_print (1, ("tag = %d, want %d.\n", p[0] & 0x3f, PT_SIG));
133 if (len >= 18 && p[1] == 3)
134 /* version 3 signature */
135 return (short) p[17];
136 else if (len >= 5 && p[1] == 4)
137 /* version 4 signature */
140 debug_print (1, ("Bad signature packet.\n"));
145 static short pgp_find_hash (const char *fname)
150 char tempfile[_POSIX_PATH_MAX];
157 mutt_mktemp (tempfile);
158 if ((out = safe_fopen (tempfile, "w+")) == NULL) {
159 mutt_perror (tempfile);
164 if ((in = fopen (fname, "r")) == NULL) {
169 pgp_dearmor (in, out);
172 if ((p = pgp_read_packet (out, &l)) != NULL) {
173 rv = pgp_mic_from_packet (p, l);
176 debug_print (1, ("No packet.\n"));
183 pgp_release_packet ();
187 const char *pgp_micalg (const char *fname)
189 return pgp_hash_to_micalg (pgp_find_hash (fname));