NeoMutt  2022-04-29-178-g3b62e6
Teaching an old dog new tricks
DOXYGEN
pgppacket.h File Reference

Parse PGP data packets. More...

#include <stdio.h>
+ Include dependency graph for pgppacket.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  PacketTags {
  PT_RES0 = 0 , PT_ESK , PT_SIG , PT_CESK ,
  PT_OPS , PT_SECKEY , PT_PUBKEY , PT_SUBSECKEY ,
  PT_COMPRESSED , PT_SKE , PT_MARKER , PT_LITERAL ,
  PT_TRUST , PT_NAME , PT_SUBKEY , PT_RES15 ,
  PT_COMMENT
}
 PGP packet types. More...
 

Functions

unsigned char * pgp_read_packet (FILE *fp, size_t *len)
 Read a PGP packet from a file. More...
 
void pgp_release_packet (void)
 Free the cached PGP packet. More...
 

Detailed Description

Parse PGP data packets.

Authors
  • Thomas Roessler

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file pgppacket.h.

Enumeration Type Documentation

◆ PacketTags

enum PacketTags

PGP packet types.

Enumerator
PT_RES0 

reserved

PT_ESK 

Encrypted Session Key.

PT_SIG 

Signature Packet.

PT_CESK 

Conventionally Encrypted Session Key Packet.

PT_OPS 

One-Pass Signature Packet.

PT_SECKEY 

Secret Key Packet.

PT_PUBKEY 

Public Key Packet.

PT_SUBSECKEY 

Secret Subkey Packet.

PT_COMPRESSED 

Compressed Data Packet.

PT_SKE 

Symmetrically Encrypted Data Packet.

PT_MARKER 

Marker Packet.

PT_LITERAL 

Literal Data Packet.

PT_TRUST 

Trust Packet.

PT_NAME 

Name Packet.

PT_SUBKEY 

Subkey Packet.

PT_RES15 

Reserved.

PT_COMMENT 

Comment Packet.

Definition at line 35 of file pgppacket.h.

36{
37 PT_RES0 = 0,
38 PT_ESK,
39 PT_SIG,
40 PT_CESK,
41 PT_OPS,
42 PT_SECKEY,
43 PT_PUBKEY,
46 PT_SKE,
47 PT_MARKER,
49 PT_TRUST,
50 PT_NAME,
51 PT_SUBKEY,
52 PT_RES15,
54};
@ PT_MARKER
Marker Packet.
Definition: pgppacket.h:47
@ PT_PUBKEY
Public Key Packet.
Definition: pgppacket.h:43
@ PT_SUBKEY
Subkey Packet.
Definition: pgppacket.h:51
@ PT_SIG
Signature Packet.
Definition: pgppacket.h:39
@ PT_NAME
Name Packet.
Definition: pgppacket.h:50
@ PT_SECKEY
Secret Key Packet.
Definition: pgppacket.h:42
@ PT_RES0
reserved
Definition: pgppacket.h:37
@ PT_ESK
Encrypted Session Key.
Definition: pgppacket.h:38
@ PT_LITERAL
Literal Data Packet.
Definition: pgppacket.h:48
@ PT_COMMENT
Comment Packet.
Definition: pgppacket.h:53
@ PT_TRUST
Trust Packet.
Definition: pgppacket.h:49
@ PT_SUBSECKEY
Secret Subkey Packet.
Definition: pgppacket.h:44
@ PT_OPS
One-Pass Signature Packet.
Definition: pgppacket.h:41
@ PT_RES15
Reserved.
Definition: pgppacket.h:52
@ PT_COMPRESSED
Compressed Data Packet.
Definition: pgppacket.h:45
@ PT_SKE
Symmetrically Encrypted Data Packet.
Definition: pgppacket.h:46
@ PT_CESK
Conventionally Encrypted Session Key Packet.
Definition: pgppacket.h:40

Function Documentation

◆ pgp_read_packet()

unsigned char * pgp_read_packet ( FILE *  fp,
size_t *  len 
)

Read a PGP packet from a file.

Parameters
[in]fpFile to read from
[out]lenNumber of bytes read
Return values
ptrPGP data packet

This function uses a cache to store the data: pbuf, plen.

Definition at line 85 of file pgppacket.c.

86{
87 size_t used = 0;
88 LOFF_T startpos;
89 unsigned char ctb;
90 unsigned char b;
91 size_t material;
92
93 startpos = ftello(fp);
94 if (startpos < 0)
95 return NULL;
96
97 if (plen == 0)
98 {
101 }
102
103 if (fread(&ctb, 1, 1, fp) < 1)
104 {
105 if (!feof(fp))
106 perror("fread");
107 goto bail;
108 }
109
110 if (!(ctb & 0x80))
111 {
112 goto bail;
113 }
114
115 if (ctb & 0x40) /* handle PGP 5.0 packets. */
116 {
117 bool partial = false;
118 pbuf[0] = ctb;
119 used++;
120
121 do
122 {
123 if (fread(&b, 1, 1, fp) < 1)
124 {
125 perror("fread");
126 goto bail;
127 }
128
129 if (b < 192)
130 {
131 material = b;
132 partial = false;
133 }
134 else if (b <= 223)
135 {
136 material = (b - 192) * 256;
137 if (fread(&b, 1, 1, fp) < 1)
138 {
139 perror("fread");
140 goto bail;
141 }
142 material += b + 192;
143 partial = false;
144 }
145 else if (b < 255)
146 {
147 material = 1 << (b & 0x1f);
148 partial = true;
149 }
150 else
151 /* b == 255 */
152 {
153 unsigned char buf[4];
154 if (fread(buf, 4, 1, fp) < 1)
155 {
156 perror("fread");
157 goto bail;
158 }
159 material = (size_t) buf[0] << 24;
160 material |= buf[1] << 16;
161 material |= buf[2] << 8;
162 material |= buf[3];
163 partial = false;
164 }
165
166 if (read_material(material, &used, fp) == -1)
167 goto bail;
168
169 } while (partial);
170 }
171 else
172 /* Old-Style PGP */
173 {
174 int bytes = 0;
175 pbuf[0] = 0x80 | ((ctb >> 2) & 0x0f);
176 used++;
177
178 switch (ctb & 0x03)
179 {
180 case 0:
181 {
182 if (fread(&b, 1, 1, fp) < 1)
183 {
184 perror("fread");
185 goto bail;
186 }
187
188 material = b;
189 break;
190 }
191
192 case 1:
193 bytes = 2;
194 /* fallthrough */
195
196 case 2:
197 {
198 if (!bytes)
199 bytes = 4;
200
201 material = 0;
202
203 for (int i = 0; i < bytes; i++)
204 {
205 if (fread(&b, 1, 1, fp) < 1)
206 {
207 perror("fread");
208 goto bail;
209 }
210
211 material = (material << 8) + b;
212 }
213 break;
214 }
215
216 default:
217 goto bail;
218 }
219
220 if (read_material(material, &used, fp) == -1)
221 goto bail;
222 }
223
224 if (len)
225 *len = used;
226
227 return pbuf;
228
229bail:
230
231 (void) mutt_file_seek(fp, startpos, SEEK_SET);
232 return NULL;
233}
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition: file.c:690
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
static unsigned char * pbuf
Cached PGP data packet.
Definition: pgppacket.c:38
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
static size_t plen
Length of cached packet.
Definition: pgppacket.c:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_release_packet()

void pgp_release_packet ( void  )

Free the cached PGP packet.

Free the data stored in pbuf.

Definition at line 240 of file pgppacket.c.

241{
242 plen = 0;
243 FREE(&pbuf);
244}
#define FREE(x)
Definition: memory.h:43
+ Here is the caller graph for this function: