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
14 #include <lib-lib/lib-lib.h>
16 #include <lib-mime/mime.h>
17 #include <lib-ui/curses.h>
21 #include "pgppacket.h"
28 } HashAlgorithms[] = {
32 3, "pgp-ripemd160"}, {
35 7, "pgp-haval-5-160"}, {
42 static const char *pgp_hash_to_micalg (short id)
46 for (i = 0; HashAlgorithms[i].id >= 0; i++)
47 if (HashAlgorithms[i].id == id)
48 return HashAlgorithms[i].name;
52 static void pgp_dearmor (FILE * in, FILE * out)
54 char line[HUGE_STRING];
65 /* find the beginning of ASCII armor */
67 while ((r = fgets (line, sizeof (line), in)) != NULL) {
68 if (!strncmp (line, "-----BEGIN", 10))
75 /* skip the armor header */
77 while ((r = fgets (line, sizeof (line), in)) != NULL) {
86 /* actual data starts here */
89 /* find the checksum */
91 while ((r = fgets (line, sizeof (line), in)) != NULL) {
92 if (*line == '=' || !strncmp (line, "-----END", 8))
99 if ((end = ftello (in) - m_strlen(line)) < start) {
103 if (fseeko (in, start, SEEK_SET) == -1) {
107 mutt_decode_base64 (&state, end - start, 0, MUTT_ICONV_ERROR);
110 static short pgp_mic_from_packet (unsigned char *p, size_t len)
113 if ((p[0] & 0x3f) != PT_SIG) {
117 if (len >= 18 && p[1] == 3)
118 /* version 3 signature */
119 return (short) p[17];
120 else if (len >= 5 && p[1] == 4)
121 /* version 4 signature */
128 static short pgp_find_hash (const char *fname)
133 char tempfile[_POSIX_PATH_MAX];
140 out = m_tempfile (tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
142 mutt_perror (_("Can't create temporary file"));
147 if ((in = fopen (fname, "r")) == NULL) {
148 mutt_perror (_("Can't create temporary file"));
152 pgp_dearmor (in, out);
155 if ((p = pgp_read_packet (out, &l)) != NULL) {
156 rv = pgp_mic_from_packet (p, l);
163 pgp_release_packet ();
167 const char *pgp_micalg (const char *fname)
169 return pgp_hash_to_micalg (pgp_find_hash (fname));