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"
30 #include <lib-lib/file.h>
31 #include "lib/debug.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))
80 debug_print (1, ("Can't find begin of ASCII armor.\n"));
84 /* skip the armor header */
86 while ((r = fgets (line, sizeof (line), in)) != NULL) {
92 debug_print (1, ("Armor header doesn't end.\n"));
96 /* actual data starts here */
99 /* find the checksum */
101 while ((r = fgets (line, sizeof (line), in)) != NULL) {
102 if (*line == '=' || !strncmp (line, "-----END", 8))
106 debug_print (1, ("Can't find end of ASCII armor.\n"));
110 if ((end = ftello (in) - m_strlen(line)) < start) {
111 debug_print (1, ("end < start???\n"));
115 if (fseeko (in, start, SEEK_SET) == -1) {
116 debug_print (1, ("Can't seekto start.\n"));
120 mutt_decode_base64 (&state, end - start, 0, (iconv_t) - 1);
123 static short pgp_mic_from_packet (unsigned char *p, size_t len)
126 if ((p[0] & 0x3f) != PT_SIG) {
127 debug_print (1, ("tag = %d, want %d.\n", p[0] & 0x3f, PT_SIG));
131 if (len >= 18 && p[1] == 3)
132 /* version 3 signature */
133 return (short) p[17];
134 else if (len >= 5 && p[1] == 4)
135 /* version 4 signature */
138 debug_print (1, ("Bad signature packet.\n"));
143 static short pgp_find_hash (const char *fname)
148 char tempfile[_POSIX_PATH_MAX];
155 mutt_mktemp (tempfile);
156 if ((out = safe_fopen (tempfile, "w+")) == NULL) {
157 mutt_perror (tempfile);
162 if ((in = fopen (fname, "r")) == NULL) {
167 pgp_dearmor (in, out);
170 if ((p = pgp_read_packet (out, &l)) != NULL) {
171 rv = pgp_mic_from_packet (p, l);
174 debug_print (1, ("No packet.\n"));
181 pgp_release_packet ();
185 const char *pgp_micalg (const char *fname)
187 return pgp_hash_to_micalg (pgp_find_hash (fname));