NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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.
 
void pgp_release_packet (void)
 Free the cached PGP packet.
 

Detailed Description

Parse PGP data packets.

Authors
  • Richard Russon

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: PacketBuf, PacketBufLen.

Definition at line 78 of file pgppacket.c.

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}
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 MUTT_MEM_MALLOC(n, type)
Definition: memory.h:41
#define FALLTHROUGH
Definition: lib.h:111
static unsigned char * PacketBuf
Cached PGP data packet.
Definition: pgppacket.c:38
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
+ 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 PacketBuf.

Definition at line 231 of file pgppacket.c.

232{
233 PacketBufLen = 0;
234 FREE(&PacketBuf);
235}
#define FREE(x)
Definition: memory.h:55
+ Here is the caller graph for this function: