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>
36 } HashAlgorithms[] = {
40 3, "pgp-ripemd160"}, {
43 7, "pgp-haval-5-160"}, {
50 static const char *pgp_hash_to_micalg (short id)
54 for (i = 0; HashAlgorithms[i].id >= 0; i++)
55 if (HashAlgorithms[i].id == id)
56 return HashAlgorithms[i].name;
60 static void pgp_dearmor (FILE * in, FILE * out)
62 char line[HUGE_STRING];
73 /* find the beginning of ASCII armor */
75 while ((r = fgets (line, sizeof (line), in)) != NULL) {
76 if (!strncmp (line, "-----BEGIN", 10))
83 /* skip the armor header */
85 while ((r = fgets (line, sizeof (line), in)) != NULL) {
94 /* actual data starts here */
97 /* find the checksum */
99 while ((r = fgets (line, sizeof (line), in)) != NULL) {
100 if (*line == '=' || !strncmp (line, "-----END", 8))
107 if ((end = ftello (in) - m_strlen(line)) < start) {
111 if (fseeko (in, start, SEEK_SET) == -1) {
115 mutt_decode_base64 (&state, end - start, 0, MUTT_ICONV_ERROR);
118 static short pgp_mic_from_packet (unsigned char *p, size_t len)
121 if ((p[0] & 0x3f) != PT_SIG) {
125 if (len >= 18 && p[1] == 3)
126 /* version 3 signature */
127 return (short) p[17];
128 else if (len >= 5 && p[1] == 4)
129 /* version 4 signature */
136 static short pgp_find_hash (const char *fname)
141 char tempfile[_POSIX_PATH_MAX];
148 mutt_mktemp (tempfile);
149 if ((out = safe_fopen (tempfile, "w+")) == NULL) {
150 mutt_perror (tempfile);
155 if ((in = fopen (fname, "r")) == NULL) {
160 pgp_dearmor (in, out);
163 if ((p = pgp_read_packet (out, &l)) != NULL) {
164 rv = pgp_mic_from_packet (p, l);
171 pgp_release_packet ();
175 const char *pgp_micalg (const char *fname)
177 return pgp_hash_to_micalg (pgp_find_hash (fname));