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/lib-lib.h>
30 #include <lib-ui/curses.h>
32 #include <lib-mime/mime.h>
37 } HashAlgorithms[] = {
41 3, "pgp-ripemd160"}, {
44 7, "pgp-haval-5-160"}, {
51 static const char *pgp_hash_to_micalg (short id)
55 for (i = 0; HashAlgorithms[i].id >= 0; i++)
56 if (HashAlgorithms[i].id == id)
57 return HashAlgorithms[i].name;
61 static void pgp_dearmor (FILE * in, FILE * out)
63 char line[HUGE_STRING];
74 /* find the beginning of ASCII armor */
76 while ((r = fgets (line, sizeof (line), in)) != NULL) {
77 if (!strncmp (line, "-----BEGIN", 10))
84 /* skip the armor header */
86 while ((r = fgets (line, sizeof (line), in)) != NULL) {
95 /* actual data starts here */
98 /* find the checksum */
100 while ((r = fgets (line, sizeof (line), in)) != NULL) {
101 if (*line == '=' || !strncmp (line, "-----END", 8))
108 if ((end = ftello (in) - m_strlen(line)) < start) {
112 if (fseeko (in, start, SEEK_SET) == -1) {
116 mutt_decode_base64 (&state, end - start, 0, MUTT_ICONV_ERROR);
119 static short pgp_mic_from_packet (unsigned char *p, size_t len)
122 if ((p[0] & 0x3f) != PT_SIG) {
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 */
137 static short pgp_find_hash (const char *fname)
142 char tempfile[_POSIX_PATH_MAX];
149 mutt_mktemp (tempfile);
150 if ((out = safe_fopen (tempfile, "w+")) == NULL) {
151 mutt_perror (tempfile);
156 if ((in = fopen (fname, "r")) == NULL) {
161 pgp_dearmor (in, out);
164 if ((p = pgp_read_packet (out, &l)) != NULL) {
165 rv = pgp_mic_from_packet (p, l);
172 pgp_release_packet ();
176 const char *pgp_micalg (const char *fname)
178 return pgp_hash_to_micalg (pgp_find_hash (fname));