NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
pgppacket.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <stdbool.h>
32#include <stdio.h>
33#include "mutt/lib.h"
34#include "pgppacket.h"
35
36#define CHUNK_SIZE 1024
37
38static unsigned char *PacketBuf = NULL;
39static size_t PacketBufLen = 0;
40
51static int read_material(size_t material, size_t *used, FILE *fp)
52{
53 if ((*used + material) >= PacketBufLen)
54 {
55 PacketBufLen = *used + material + CHUNK_SIZE;
56
57 MUTT_MEM_REALLOC(&PacketBuf, PacketBufLen, unsigned char);
58 }
59
60 if (fread(PacketBuf + *used, 1, material, fp) < material)
61 {
62 mutt_perror("fread");
63 return -1;
64 }
65
66 *used += material;
67 return 0;
68}
69
78unsigned char *pgp_read_packet(FILE *fp, size_t *len)
79{
80 size_t used = 0;
81 LOFF_T startpos;
82 unsigned char ctb;
83 unsigned char b;
84 size_t material;
85
86 startpos = ftello(fp);
87 if (startpos < 0)
88 return NULL;
89
90 if (PacketBufLen == 0)
91 {
93 PacketBuf = MUTT_MEM_MALLOC(PacketBufLen, unsigned char);
94 }
95
96 if (fread(&ctb, 1, 1, fp) < 1)
97 {
98 if (!feof(fp))
99 mutt_perror("fread");
100 goto bail;
101 }
102
103 if (!(ctb & 0x80))
104 {
105 goto bail;
106 }
107
108 if (ctb & 0x40) /* handle PGP 5.0 packets. */
109 {
110 bool partial = false;
111 PacketBuf[0] = ctb;
112 used++;
113
114 do
115 {
116 if (fread(&b, 1, 1, fp) < 1)
117 {
118 mutt_perror("fread");
119 goto bail;
120 }
121
122 if (b < 192)
123 {
124 material = b;
125 partial = false;
126 }
127 else if (b <= 223)
128 {
129 material = (b - 192) * 256;
130 if (fread(&b, 1, 1, fp) < 1)
131 {
132 mutt_perror("fread");
133 goto bail;
134 }
135 material += b + 192;
136 partial = false;
137 }
138 else if (b < 255)
139 {
140 material = 1 << (b & 0x1f);
141 partial = true;
142 }
143 else /* b == 255 */
144 {
145 unsigned char buf[4];
146 if (fread(buf, 4, 1, fp) < 1)
147 {
148 mutt_perror("fread");
149 goto bail;
150 }
151 material = (size_t) buf[0] << 24;
152 material |= buf[1] << 16;
153 material |= buf[2] << 8;
154 material |= buf[3];
155 partial = false;
156 }
157
158 if (read_material(material, &used, fp) == -1)
159 goto bail;
160
161 } while (partial);
162 }
163 else /* Old-Style PGP */
164 {
165 int bytes = 0;
166 PacketBuf[0] = 0x80 | ((ctb >> 2) & 0x0f);
167 used++;
168
169 switch (ctb & 0x03)
170 {
171 case 0:
172 {
173 if (fread(&b, 1, 1, fp) < 1)
174 {
175 mutt_perror("fread");
176 goto bail;
177 }
178
179 material = b;
180 break;
181 }
182
183 case 1:
184 bytes = 2;
186
187 case 2:
188 {
189 if (!bytes)
190 bytes = 4;
191
192 material = 0;
193
194 for (int i = 0; i < bytes; i++)
195 {
196 if (fread(&b, 1, 1, fp) < 1)
197 {
198 mutt_perror("fread");
199 goto bail;
200 }
201
202 material = (material << 8) + b;
203 }
204 break;
205 }
206
207 default:
208 goto bail;
209 }
210
211 if (read_material(material, &used, fp) == -1)
212 goto bail;
213 }
214
215 if (len)
216 *len = used;
217
218 return PacketBuf;
219
220bail:
221
222 (void) mutt_file_seek(fp, startpos, SEEK_SET);
223 return NULL;
224}
225
232{
233 PacketBufLen = 0;
234 FREE(&PacketBuf);
235}
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition: file.c:778
#define mutt_perror(...)
Definition: logging2.h:93
#define FREE(x)
Definition: memory.h:55
#define MUTT_MEM_REALLOC(pptr, n, type)
Definition: memory.h:43
#define MUTT_MEM_MALLOC(n, type)
Definition: memory.h:41
Convenience wrapper for the library headers.
#define FALLTHROUGH
Definition: lib.h:111
static unsigned char * PacketBuf
Cached PGP data packet.
Definition: pgppacket.c:38
void pgp_release_packet(void)
Free the cached PGP packet.
Definition: pgppacket.c:231
static size_t PacketBufLen
Length of cached packet.
Definition: pgppacket.c:39
static int read_material(size_t material, size_t *used, FILE *fp)
Read PGP data into a buffer.
Definition: pgppacket.c:51
#define CHUNK_SIZE
Amount of data to read at once.
Definition: pgppacket.c:36
unsigned char * pgp_read_packet(FILE *fp, size_t *len)
Read a PGP packet from a file.
Definition: pgppacket.c:78
Parse PGP data packets.