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>
30 #include <lib-lib/debug.h>
32 #include <lib-mime/mime.h>
37 } HashAlgorithms[] = {
41 3, "pgp-ripemd160"}, {
44 7, "pgp-haval-5-160"}, {
51 static const char *pgp_hash_to_micalg (short id)
55 for (i = 0; HashAlgorithms[i].id >= 0; i++)
56 if (HashAlgorithms[i].id == id)
57 return HashAlgorithms[i].name;
61 static void pgp_dearmor (FILE * in, FILE * out)
63 char line[HUGE_STRING];
74 /* find the beginning of ASCII armor */
76 while ((r = fgets (line, sizeof (line), in)) != NULL) {
77 if (!strncmp (line, "-----BEGIN", 10))
81 debug_print (1, ("Can't find begin of ASCII armor.\n"));
85 /* skip the armor header */
87 while ((r = fgets (line, sizeof (line), in)) != NULL) {
93 debug_print (1, ("Armor header doesn't end.\n"));
97 /* actual data starts here */
100 /* find the checksum */
102 while ((r = fgets (line, sizeof (line), in)) != NULL) {
103 if (*line == '=' || !strncmp (line, "-----END", 8))
107 debug_print (1, ("Can't find end of ASCII armor.\n"));
111 if ((end = ftello (in) - m_strlen(line)) < start) {
112 debug_print (1, ("end < start???\n"));
116 if (fseeko (in, start, SEEK_SET) == -1) {
117 debug_print (1, ("Can't seekto start.\n"));
121 mutt_decode_base64 (&state, end - start, 0, (iconv_t) - 1);
124 static short pgp_mic_from_packet (unsigned char *p, size_t len)
127 if ((p[0] & 0x3f) != PT_SIG) {
128 debug_print (1, ("tag = %d, want %d.\n", p[0] & 0x3f, PT_SIG));
132 if (len >= 18 && p[1] == 3)
133 /* version 3 signature */
134 return (short) p[17];
135 else if (len >= 5 && p[1] == 4)
136 /* version 4 signature */
139 debug_print (1, ("Bad signature packet.\n"));
144 static short pgp_find_hash (const char *fname)
149 char tempfile[_POSIX_PATH_MAX];
156 mutt_mktemp (tempfile);
157 if ((out = safe_fopen (tempfile, "w+")) == NULL) {
158 mutt_perror (tempfile);
163 if ((in = fopen (fname, "r")) == NULL) {
168 pgp_dearmor (in, out);
171 if ((p = pgp_read_packet (out, &l)) != NULL) {
172 rv = pgp_mic_from_packet (p, l);
175 debug_print (1, ("No packet.\n"));
182 pgp_release_packet ();
186 const char *pgp_micalg (const char *fname)
188 return pgp_hash_to_micalg (pgp_find_hash (fname));