File: pgppacket.c

package info (click to toggle)
neomutt 20250510%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 23,372 kB
  • sloc: ansic: 171,730; sh: 5,357; perl: 1,121; tcl: 1,065; python: 443; makefile: 48
file content (235 lines) | stat: -rw-r--r-- 4,903 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
 * @file
 * Parse PGP data packets
 *
 * @authors
 * Copyright (C) 2017-2023 Richard Russon <rich@flatcap.org>
 * Copyright (C) 2021 Pietro Cerutti <gahr@gahr.ch>
 *
 * @copyright
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 2 of the License, or (at your option) any later
 * version.
 *
 * 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/>.
 */

/**
 * @page crypt_pgppacket Parse PGP data packets
 *
 * Parse PGP data packets
 */

#include "config.h"
#include <stdbool.h>
#include <stdio.h>
#include "mutt/lib.h"
#include "pgppacket.h"

#define CHUNK_SIZE 1024 ///< Amount of data to read at once

static unsigned char *PacketBuf = NULL; ///< Cached PGP data packet
static size_t PacketBufLen = 0;         ///< Length of cached packet

/**
 * read_material - Read PGP data into a buffer
 * @param[in]     material Number of bytes to read
 * @param[in,out] used     Number of bytes already read
 * @param[in]     fp       File to read from
 * @retval  0 Success
 * @retval -1 Failure (see errno)
 *
 * This function uses a cache to store the data: #PacketBuf, #PacketBufLen.
 */
static int read_material(size_t material, size_t *used, FILE *fp)
{
  if ((*used + material) >= PacketBufLen)
  {
    PacketBufLen = *used + material + CHUNK_SIZE;

    MUTT_MEM_REALLOC(&PacketBuf, PacketBufLen, unsigned char);
  }

  if (fread(PacketBuf + *used, 1, material, fp) < material)
  {
    mutt_perror("fread");
    return -1;
  }

  *used += material;
  return 0;
}

/**
 * pgp_read_packet - Read a PGP packet from a file
 * @param[in]  fp  File to read from
 * @param[out] len Number of bytes read
 * @retval ptr PGP data packet
 *
 * This function uses a cache to store the data: #PacketBuf, #PacketBufLen.
 */
unsigned char *pgp_read_packet(FILE *fp, size_t *len)
{
  size_t used = 0;
  LOFF_T startpos;
  unsigned char ctb;
  unsigned char b;
  size_t material;

  startpos = ftello(fp);
  if (startpos < 0)
    return NULL;

  if (PacketBufLen == 0)
  {
    PacketBufLen = CHUNK_SIZE;
    PacketBuf = MUTT_MEM_MALLOC(PacketBufLen, unsigned char);
  }

  if (fread(&ctb, 1, 1, fp) < 1)
  {
    if (!feof(fp))
      mutt_perror("fread");
    goto bail;
  }

  if (!(ctb & 0x80))
  {
    goto bail;
  }

  if (ctb & 0x40) /* handle PGP 5.0 packets. */
  {
    bool partial = false;
    PacketBuf[0] = ctb;
    used++;

    do
    {
      if (fread(&b, 1, 1, fp) < 1)
      {
        mutt_perror("fread");
        goto bail;
      }

      if (b < 192)
      {
        material = b;
        partial = false;
      }
      else if (b <= 223)
      {
        material = (b - 192) * 256;
        if (fread(&b, 1, 1, fp) < 1)
        {
          mutt_perror("fread");
          goto bail;
        }
        material += b + 192;
        partial = false;
      }
      else if (b < 255)
      {
        material = 1 << (b & 0x1f);
        partial = true;
      }
      else /* b == 255 */
      {
        unsigned char buf[4];
        if (fread(buf, 4, 1, fp) < 1)
        {
          mutt_perror("fread");
          goto bail;
        }
        material = (size_t) buf[0] << 24;
        material |= buf[1] << 16;
        material |= buf[2] << 8;
        material |= buf[3];
        partial = false;
      }

      if (read_material(material, &used, fp) == -1)
        goto bail;

    } while (partial);
  }
  else /* Old-Style PGP */
  {
    int bytes = 0;
    PacketBuf[0] = 0x80 | ((ctb >> 2) & 0x0f);
    used++;

    switch (ctb & 0x03)
    {
      case 0:
      {
        if (fread(&b, 1, 1, fp) < 1)
        {
          mutt_perror("fread");
          goto bail;
        }

        material = b;
        break;
      }

      case 1:
        bytes = 2;
        FALLTHROUGH;

      case 2:
      {
        if (!bytes)
          bytes = 4;

        material = 0;

        for (int i = 0; i < bytes; i++)
        {
          if (fread(&b, 1, 1, fp) < 1)
          {
            mutt_perror("fread");
            goto bail;
          }

          material = (material << 8) + b;
        }
        break;
      }

      default:
        goto bail;
    }

    if (read_material(material, &used, fp) == -1)
      goto bail;
  }

  if (len)
    *len = used;

  return PacketBuf;

bail:

  (void) mutt_file_seek(fp, startpos, SEEK_SET);
  return NULL;
}

/**
 * pgp_release_packet - Free the cached PGP packet
 *
 * Free the data stored in #PacketBuf.
 */
void pgp_release_packet(void)
{
  PacketBufLen = 0;
  FREE(&PacketBuf);
}