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/debug.h"
35 } HashAlgorithms[] = {
39 3, "pgp-ripemd160"}, {
42 7, "pgp-haval-5-160"}, {
49 static const char *pgp_hash_to_micalg (short id)
53 for (i = 0; HashAlgorithms[i].id >= 0; i++)
54 if (HashAlgorithms[i].id == id)
55 return HashAlgorithms[i].name;
59 static void pgp_dearmor (FILE * in, FILE * out)
61 char line[HUGE_STRING];
68 memset (&state, 0, sizeof (STATE));
72 /* find the beginning of ASCII armor */
74 while ((r = fgets (line, sizeof (line), in)) != NULL) {
75 if (!strncmp (line, "-----BEGIN", 10))
79 debug_print (1, ("Can't find begin of ASCII armor.\n"));
83 /* skip the armor header */
85 while ((r = fgets (line, sizeof (line), in)) != NULL) {
91 debug_print (1, ("Armor header doesn't end.\n"));
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))
105 debug_print (1, ("Can't find end of ASCII armor.\n"));
109 if ((end = ftello (in) - str_len (line)) < start) {
110 debug_print (1, ("end < start???\n"));
114 if (fseeko (in, start, SEEK_SET) == -1) {
115 debug_print (1, ("Can't seekto start.\n"));
119 mutt_decode_base64 (&state, end - start, 0, (iconv_t) - 1);
122 static short pgp_mic_from_packet (unsigned char *p, size_t len)
125 if ((p[0] & 0x3f) != PT_SIG) {
126 debug_print (1, ("tag = %d, want %d.\n", p[0] & 0x3f, PT_SIG));
130 if (len >= 18 && p[1] == 3)
131 /* version 3 signature */
132 return (short) p[17];
133 else if (len >= 5 && p[1] == 4)
134 /* version 4 signature */
137 debug_print (1, ("Bad signature packet.\n"));
142 static short pgp_find_hash (const char *fname)
147 char tempfile[_POSIX_PATH_MAX];
154 mutt_mktemp (tempfile);
155 if ((out = safe_fopen (tempfile, "w+")) == NULL) {
156 mutt_perror (tempfile);
161 if ((in = fopen (fname, "r")) == NULL) {
166 pgp_dearmor (in, out);
169 if ((p = pgp_read_packet (out, &l)) != NULL) {
170 rv = pgp_mic_from_packet (p, l);
173 debug_print (1, ("No packet.\n"));
180 pgp_release_packet ();
184 const char *pgp_micalg (const char *fname)
186 return pgp_hash_to_micalg (pgp_find_hash (fname));