NeoMutt  2019-12-07-60-g0cfa53
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,
44  PT_SUBSECKEY,
46  PT_SKE,
47  PT_MARKER,
48  PT_LITERAL,
49  PT_TRUST,
50  PT_NAME,
51  PT_SUBKEY,
52  PT_RES15,
53  PT_COMMENT,
54 };
Marker Packet.
Definition: pgppacket.h:47
Secret Key Packet.
Definition: pgppacket.h:42
Literal Data Packet.
Definition: pgppacket.h:48
Conventionally Encrypted Session Key Packet.
Definition: pgppacket.h:40
Reserved.
Definition: pgppacket.h:52
Signature Packet.
Definition: pgppacket.h:39
Compressed Data Packet.
Definition: pgppacket.h:45
Encrypted Session Key.
Definition: pgppacket.h:38
Subkey Packet.
Definition: pgppacket.h:51
One-Pass Signature Packet.
Definition: pgppacket.h:41
Name Packet.
Definition: pgppacket.h:50
Public Key Packet.
Definition: pgppacket.h:43
Secret Subkey Packet.
Definition: pgppacket.h:44
Comment Packet.
Definition: pgppacket.h:53
Symmetrically Encrypted Data Packet.
Definition: pgppacket.h:46
Trust Packet.
Definition: pgppacket.h:49
reserved
Definition: pgppacket.h:37

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

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

Definition at line 84 of file pgppacket.c.

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

240 {
241  plen = 0;
242  FREE(&pbuf);
243 }
static size_t plen
Length of cached packet.
Definition: pgppacket.c:39
#define FREE(x)
Definition: memory.h:40
static unsigned char * pbuf
Cache PGP data packet.
Definition: pgppacket.c:38
+ Here is the caller graph for this function: