NeoMutt  2025-12-11-911-gd8d604
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: NcryptModuleData::packet_buf

Definition at line 78 of file pgppacket.c.

79{
81 size_t used = 0;
82 LOFF_T startpos;
83 unsigned char ctb;
84 unsigned char b;
85 size_t material;
86
87 startpos = ftello(fp);
88 if (startpos < 0)
89 return NULL;
90
91 if (mod_data->packet_buf_len == 0)
92 {
93 mod_data->packet_buf_len = CHUNK_SIZE;
94 mod_data->packet_buf = MUTT_MEM_MALLOC(mod_data->packet_buf_len, unsigned char);
95 }
96
97 if (fread(&ctb, 1, 1, fp) < 1)
98 {
99 if (!feof(fp))
100 mutt_perror("fread");
101 goto bail;
102 }
103
104 if (!(ctb & 0x80))
105 {
106 goto bail;
107 }
108
109 if (ctb & 0x40) /* handle PGP 5.0 packets. */
110 {
111 bool partial = false;
112 mod_data->packet_buf[0] = ctb;
113 used++;
114
115 do
116 {
117 if (fread(&b, 1, 1, fp) < 1)
118 {
119 mutt_perror("fread");
120 goto bail;
121 }
122
123 if (b < 192)
124 {
125 material = b;
126 partial = false;
127 }
128 else if (b <= 223)
129 {
130 material = (b - 192) * 256;
131 if (fread(&b, 1, 1, fp) < 1)
132 {
133 mutt_perror("fread");
134 goto bail;
135 }
136 material += b + 192;
137 partial = false;
138 }
139 else if (b < 255)
140 {
141 material = 1 << (b & 0x1f);
142 partial = true;
143 }
144 else /* b == 255 */
145 {
146 unsigned char buf[4];
147 if (fread(buf, 4, 1, fp) < 1)
148 {
149 mutt_perror("fread");
150 goto bail;
151 }
152 material = (size_t) buf[0] << 24;
153 material |= buf[1] << 16;
154 material |= buf[2] << 8;
155 material |= buf[3];
156 partial = false;
157 }
158
159 if (read_material(material, &used, fp) == -1)
160 goto bail;
161
162 } while (partial);
163 }
164 else /* Old-Style PGP */
165 {
166 int bytes = 0;
167 mod_data->packet_buf[0] = 0x80 | ((ctb >> 2) & 0x0f);
168 used++;
169
170 switch (ctb & 0x03)
171 {
172 case 0:
173 {
174 if (fread(&b, 1, 1, fp) < 1)
175 {
176 mutt_perror("fread");
177 goto bail;
178 }
179
180 material = b;
181 break;
182 }
183
184 case 1:
185 bytes = 2;
187
188 case 2:
189 {
190 if (!bytes)
191 bytes = 4;
192
193 material = 0;
194
195 for (int i = 0; i < bytes; i++)
196 {
197 if (fread(&b, 1, 1, fp) < 1)
198 {
199 mutt_perror("fread");
200 goto bail;
201 }
202
203 material = (material << 8) + b;
204 }
205 break;
206 }
207
208 default:
209 goto bail;
210 }
211
212 if (read_material(material, &used, fp) == -1)
213 goto bail;
214 }
215
216 if (len)
217 *len = used;
218
219 return mod_data->packet_buf;
220
221bail:
222
223 (void) mutt_file_seek(fp, startpos, SEEK_SET);
224 return NULL;
225}
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition file.c:648
#define mutt_perror(...)
Definition logging2.h:95
#define MUTT_MEM_MALLOC(n, type)
Definition memory.h:53
@ MODULE_ID_NCRYPT
ModuleNcrypt, Ncrypt
Definition module_api.h:80
#define FALLTHROUGH
Definition lib.h:117
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:663
static int read_material(size_t material, size_t *used, FILE *fp)
Read PGP data into a buffer.
Definition pgppacket.c:50
#define CHUNK_SIZE
Amount of data to read at once.
Definition pgppacket.c:38
Ncrypt private Module data.
Definition module_data.h:38
unsigned char * packet_buf
Cached PGP data packet.
Definition module_data.h:53
size_t packet_buf_len
Length of cached packet.
Definition module_data.h:54
Container for Accounts, Notifications.
Definition neomutt.h:41
+ 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 NcryptModuleData::packet_buf

Definition at line 232 of file pgppacket.c.

233{
235 mod_data->packet_buf_len = 0;
236 FREE(&mod_data->packet_buf);
237}
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
+ Here is the call graph for this function:
+ Here is the caller graph for this function: