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>
19 #include "pgppacket.h"
22 #include <lib-ui/curses.h>
24 #include <lib-mime/mime.h>
29 } HashAlgorithms[] = {
33 3, "pgp-ripemd160"}, {
36 7, "pgp-haval-5-160"}, {
43 static const char *pgp_hash_to_micalg (short id)
47 for (i = 0; HashAlgorithms[i].id >= 0; i++)
48 if (HashAlgorithms[i].id == id)
49 return HashAlgorithms[i].name;
53 static void pgp_dearmor (FILE * in, FILE * out)
55 char line[HUGE_STRING];
66 /* find the beginning of ASCII armor */
68 while ((r = fgets (line, sizeof (line), in)) != NULL) {
69 if (!strncmp (line, "-----BEGIN", 10))
76 /* skip the armor header */
78 while ((r = fgets (line, sizeof (line), in)) != NULL) {
87 /* actual data starts here */
90 /* find the checksum */
92 while ((r = fgets (line, sizeof (line), in)) != NULL) {
93 if (*line == '=' || !strncmp (line, "-----END", 8))
100 if ((end = ftello (in) - m_strlen(line)) < start) {
104 if (fseeko (in, start, SEEK_SET) == -1) {
108 mutt_decode_base64 (&state, end - start, 0, MUTT_ICONV_ERROR);
111 static short pgp_mic_from_packet (unsigned char *p, size_t len)
114 if ((p[0] & 0x3f) != PT_SIG) {
118 if (len >= 18 && p[1] == 3)
119 /* version 3 signature */
120 return (short) p[17];
121 else if (len >= 5 && p[1] == 4)
122 /* version 4 signature */
129 static short pgp_find_hash (const char *fname)
134 char tempfile[_POSIX_PATH_MAX];
141 mutt_mktemp (tempfile);
142 if ((out = safe_fopen (tempfile, "w+")) == NULL) {
143 mutt_perror (tempfile);
148 if ((in = fopen (fname, "r")) == NULL) {
153 pgp_dearmor (in, out);
156 if ((p = pgp_read_packet (out, &l)) != NULL) {
157 rv = pgp_mic_from_packet (p, l);
164 pgp_release_packet ();
168 const char *pgp_micalg (const char *fname)
170 return pgp_hash_to_micalg (pgp_find_hash (fname));