chiark / gitweb /
dirmngr: Drop useless housekeeping.
[gnupg2.git] / kbx / keybox-openpgp.c
1 /* keybox-openpgp.c - OpenPGP key parsing
2  * Copyright (C) 2001, 2003, 2011 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuPG is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <https://www.gnu.org/licenses/>.
18  */
19
20 /* This is a simple OpenPGP parser suitable for all OpenPGP key
21    material.  It just provides the functionality required to build and
22    parse an KBX OpenPGP key blob.  Thus it is not a complete parser.
23    However it is self-contained and optimized for fast in-memory
24    parsing.  Note that we don't support old ElGamal v3 keys
25    anymore. */
26
27 #include <config.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <assert.h>
33
34 #include "keybox-defs.h"
35
36 #include <gcrypt.h>
37
38 #include "../common/openpgpdefs.h"
39 #include "host2net.h"
40
41 /* Assume a valid OpenPGP packet at the address pointed to by BUFBTR
42    which has a maximum length as stored at BUFLEN.  Return the header
43    information of that packet and advance the pointer stored at BUFPTR
44    to the next packet; also adjust the length stored at BUFLEN to
45    match the remaining bytes. If there are no more packets, store NULL
46    at BUFPTR.  Return an non-zero error code on failure or the
47    following data on success:
48
49    R_DATAPKT = Pointer to the begin of the packet data.
50    R_DATALEN = Length of this data.  This has already been checked to fit
51                into the buffer.
52    R_PKTTYPE = The packet type.
53    R_NTOTAL  = The total number of bytes of this packet
54
55    Note that these values are only updated on success.
56 */
57 static gpg_error_t
58 next_packet (unsigned char const **bufptr, size_t *buflen,
59              unsigned char const **r_data, size_t *r_datalen, int *r_pkttype,
60              size_t *r_ntotal)
61 {
62   const unsigned char *buf = *bufptr;
63   size_t len = *buflen;
64   int c, ctb, pkttype;
65   unsigned long pktlen;
66
67   if (!len)
68     return gpg_error (GPG_ERR_NO_DATA);
69
70   ctb = *buf++; len--;
71   if ( !(ctb & 0x80) )
72     return gpg_error (GPG_ERR_INV_PACKET); /* Invalid CTB. */
73
74   if ((ctb & 0x40))  /* New style (OpenPGP) CTB.  */
75     {
76       pkttype = (ctb & 0x3f);
77       if (!len)
78         return gpg_error (GPG_ERR_INV_PACKET); /* No 1st length byte. */
79       c = *buf++; len--;
80       if (pkttype == PKT_COMPRESSED)
81         return gpg_error (GPG_ERR_UNEXPECTED); /* ... packet in a keyblock. */
82       if ( c < 192 )
83         pktlen = c;
84       else if ( c < 224 )
85         {
86           pktlen = (c - 192) * 256;
87           if (!len)
88             return gpg_error (GPG_ERR_INV_PACKET); /* No 2nd length byte. */
89           c = *buf++; len--;
90           pktlen += c + 192;
91         }
92       else if (c == 255)
93         {
94           if (len <4 )
95             return gpg_error (GPG_ERR_INV_PACKET); /* No length bytes. */
96           pktlen = buf32_to_ulong (buf);
97           buf += 4;
98           len -= 4;
99       }
100       else /* Partial length encoding is not allowed for key packets. */
101         return gpg_error (GPG_ERR_UNEXPECTED);
102     }
103   else /* Old style CTB.  */
104     {
105       int lenbytes;
106
107       pktlen = 0;
108       pkttype = (ctb>>2)&0xf;
109       lenbytes = ((ctb&3)==3)? 0 : (1<<(ctb & 3));
110       if (!lenbytes) /* Not allowed in key packets.  */
111         return gpg_error (GPG_ERR_UNEXPECTED);
112       if (len < lenbytes)
113         return gpg_error (GPG_ERR_INV_PACKET); /* Not enough length bytes.  */
114       for (; lenbytes; lenbytes--)
115         {
116           pktlen <<= 8;
117           pktlen |= *buf++; len--;
118         }
119     }
120
121   /* Do some basic sanity check.  */
122   switch (pkttype)
123     {
124     case PKT_SIGNATURE:
125     case PKT_SECRET_KEY:
126     case PKT_PUBLIC_KEY:
127     case PKT_SECRET_SUBKEY:
128     case PKT_MARKER:
129     case PKT_RING_TRUST:
130     case PKT_USER_ID:
131     case PKT_PUBLIC_SUBKEY:
132     case PKT_OLD_COMMENT:
133     case PKT_ATTRIBUTE:
134     case PKT_COMMENT:
135     case PKT_GPG_CONTROL:
136       break; /* Okay these are allowed packets. */
137     default:
138       return gpg_error (GPG_ERR_UNEXPECTED);
139     }
140
141   if (pkttype == 63 && pktlen == 0xFFFFFFFF)
142     /* Sometimes the decompressing layer enters an error state in
143        which it simply outputs 0xff for every byte read.  If we have a
144        stream of 0xff bytes, then it will be detected as a new format
145        packet with type 63 and a 4-byte encoded length that is 4G-1.
146        Since packets with type 63 are private and we use them as a
147        control packet, which won't be 4 GB, we reject such packets as
148        invalid.  */
149     return gpg_error (GPG_ERR_INV_PACKET);
150
151   if (pktlen > len)
152     return gpg_error (GPG_ERR_INV_PACKET); /* Packet length header too long. */
153
154   *r_data = buf;
155   *r_datalen = pktlen;
156   *r_pkttype = pkttype;
157   *r_ntotal = (buf - *bufptr) + pktlen;
158
159   *bufptr = buf + pktlen;
160   *buflen = len - pktlen;
161   if (!*buflen)
162     *bufptr = NULL;
163
164   return 0;
165 }
166
167
168 /* Parse a key packet and store the information in KI. */
169 static gpg_error_t
170 parse_key (const unsigned char *data, size_t datalen,
171            struct _keybox_openpgp_key_info *ki)
172 {
173   gpg_error_t err;
174   const unsigned char *data_start = data;
175   int i, version, algorithm;
176   size_t n;
177   int npkey;
178   unsigned char hashbuffer[768];
179   const unsigned char *mpi_n = NULL;
180   size_t mpi_n_len = 0, mpi_e_len = 0;
181   gcry_md_hd_t md;
182   int is_ecc = 0;
183
184   if (datalen < 5)
185     return gpg_error (GPG_ERR_INV_PACKET);
186   version = *data++; datalen--;
187   if (version < 2 || version > 4 )
188     return gpg_error (GPG_ERR_INV_PACKET); /* Invalid version. */
189
190   /*timestamp = ((data[0]<<24)|(data[1]<<16)|(data[2]<<8)|(data[3]));*/
191   data +=4; datalen -=4;
192
193   if (version < 4)
194     {
195       if (datalen < 2)
196         return gpg_error (GPG_ERR_INV_PACKET);
197       data +=2; datalen -= 2;
198     }
199
200   if (!datalen)
201     return gpg_error (GPG_ERR_INV_PACKET);
202   algorithm = *data++; datalen--;
203
204   switch (algorithm)
205     {
206     case PUBKEY_ALGO_RSA:
207     case PUBKEY_ALGO_RSA_E:
208     case PUBKEY_ALGO_RSA_S:
209       npkey = 2;
210       break;
211     case PUBKEY_ALGO_ELGAMAL_E:
212     case PUBKEY_ALGO_ELGAMAL:
213       npkey = 3;
214       break;
215     case PUBKEY_ALGO_DSA:
216       npkey = 4;
217       break;
218     case PUBKEY_ALGO_ECDH:
219       npkey = 3;
220       is_ecc = 1;
221       break;
222     case PUBKEY_ALGO_ECDSA:
223     case PUBKEY_ALGO_EDDSA:
224       npkey = 2;
225       is_ecc = 1;
226       break;
227     default: /* Unknown algorithm. */
228       return gpg_error (GPG_ERR_UNKNOWN_ALGORITHM);
229     }
230
231   ki->algo = algorithm;
232
233   for (i=0; i < npkey; i++ )
234     {
235       unsigned int nbits, nbytes;
236
237       if (datalen < 2)
238         return gpg_error (GPG_ERR_INV_PACKET);
239
240       if (is_ecc && (i == 0 || i == 2))
241         {
242           nbytes = data[0];
243           if (nbytes < 2 || nbytes > 254)
244             return gpg_error (GPG_ERR_INV_PACKET);
245           nbytes++; /* The size byte itself.  */
246           if (datalen < nbytes)
247             return gpg_error (GPG_ERR_INV_PACKET);
248         }
249       else
250         {
251           nbits = ((data[0]<<8)|(data[1]));
252           data += 2;
253           datalen -= 2;
254           nbytes = (nbits+7) / 8;
255           if (datalen < nbytes)
256             return gpg_error (GPG_ERR_INV_PACKET);
257           /* For use by v3 fingerprint calculation we need to know the RSA
258              modulus and exponent. */
259           if (i==0)
260             {
261               mpi_n = data;
262               mpi_n_len = nbytes;
263             }
264           else if (i==1)
265             mpi_e_len = nbytes;
266         }
267
268       data += nbytes; datalen -= nbytes;
269     }
270   n = data - data_start;
271
272   if (version < 4)
273     {
274       /* We do not support any other algorithm than RSA in v3
275          packets. */
276       if (algorithm < 1 || algorithm > 3)
277         return gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM);
278
279       err = gcry_md_open (&md, GCRY_MD_MD5, 0);
280       if (err)
281         return err; /* Oops */
282       gcry_md_write (md, mpi_n, mpi_n_len);
283       gcry_md_write (md, mpi_n+mpi_n_len+2, mpi_e_len);
284       memcpy (ki->fpr, gcry_md_read (md, 0), 16);
285       gcry_md_close (md);
286       ki->fprlen = 16;
287
288       if (mpi_n_len < 8)
289         {
290           /* Moduli less than 64 bit are out of the specs scope.  Zero
291              them out because this is what gpg does too. */
292           memset (ki->keyid, 0, 8);
293         }
294       else
295         memcpy (ki->keyid, mpi_n + mpi_n_len - 8, 8);
296     }
297   else
298     {
299       /* Its a pitty that we need to prefix the buffer with the tag
300          and a length header: We can't simply pass it to the fast
301          hashing function for that reason.  It might be a good idea to
302          have a scatter-gather enabled hash function. What we do here
303          is to use a static buffer if this one is large enough and
304          only use the regular hash functions if this buffer is not
305          large enough. */
306       if ( 3 + n < sizeof hashbuffer )
307         {
308           hashbuffer[0] = 0x99;     /* CTB */
309           hashbuffer[1] = (n >> 8); /* 2 byte length header. */
310           hashbuffer[2] = n;
311           memcpy (hashbuffer + 3, data_start, n);
312           gcry_md_hash_buffer (GCRY_MD_SHA1, ki->fpr, hashbuffer, 3 + n);
313         }
314       else
315         {
316           err = gcry_md_open (&md, GCRY_MD_SHA1, 0);
317           if (err)
318             return err; /* Oops */
319           gcry_md_putc (md, 0x99 );     /* CTB */
320           gcry_md_putc (md, (n >> 8) ); /* 2 byte length header. */
321           gcry_md_putc (md, n );
322           gcry_md_write (md, data_start, n);
323           memcpy (ki->fpr, gcry_md_read (md, 0), 20);
324           gcry_md_close (md);
325         }
326       ki->fprlen = 20;
327       memcpy (ki->keyid, ki->fpr+12, 8);
328     }
329
330   return 0;
331 }
332
333
334
335 /* The caller must pass the address of an INFO structure which will
336    get filled on success with information pertaining to the OpenPGP
337    keyblock IMAGE of length IMAGELEN.  Note that a caller does only
338    need to release this INFO structure if the function returns
339    success.  If NPARSED is not NULL the actual number of bytes parsed
340    will be stored at this address.  */
341 gpg_error_t
342 _keybox_parse_openpgp (const unsigned char *image, size_t imagelen,
343                        size_t *nparsed, keybox_openpgp_info_t info)
344 {
345   gpg_error_t err = 0;
346   const unsigned char *image_start, *data;
347   size_t n, datalen;
348   int pkttype;
349   int first = 1;
350   int read_error = 0;
351   struct _keybox_openpgp_key_info *k, **ktail = NULL;
352   struct _keybox_openpgp_uid_info *u, **utail = NULL;
353
354   memset (info, 0, sizeof *info);
355   if (nparsed)
356     *nparsed = 0;
357
358   image_start = image;
359   while (image)
360     {
361       err = next_packet (&image, &imagelen, &data, &datalen, &pkttype, &n);
362       if (err)
363         {
364           read_error = 1;
365           break;
366         }
367
368       if (first)
369         {
370           if (pkttype == PKT_PUBLIC_KEY)
371             ;
372           else if (pkttype == PKT_SECRET_KEY)
373             info->is_secret = 1;
374           else
375             {
376               err = gpg_error (GPG_ERR_UNEXPECTED);
377               if (nparsed)
378                 *nparsed += n;
379               break;
380             }
381           first = 0;
382         }
383       else if (pkttype == PKT_PUBLIC_KEY || pkttype == PKT_SECRET_KEY)
384         break; /* Next keyblock encountered - ready. */
385
386       if (nparsed)
387         *nparsed += n;
388
389       if (pkttype == PKT_SIGNATURE)
390         {
391           /* For now we only count the total number of signatures. */
392           info->nsigs++;
393         }
394       else if (pkttype == PKT_USER_ID)
395         {
396           info->nuids++;
397           if (info->nuids == 1)
398             {
399               info->uids.off = data - image_start;
400               info->uids.len = datalen;
401               utail = &info->uids.next;
402             }
403           else
404             {
405               u = xtrycalloc (1, sizeof *u);
406               if (!u)
407                 {
408                   err = gpg_error_from_syserror ();
409                   break;
410                 }
411               u->off = data - image_start;
412               u->len = datalen;
413               *utail = u;
414               utail = &u->next;
415             }
416         }
417       else if (pkttype == PKT_PUBLIC_KEY || pkttype == PKT_SECRET_KEY)
418         {
419           err = parse_key (data, datalen, &info->primary);
420           if (err)
421             break;
422         }
423       else if( pkttype == PKT_PUBLIC_SUBKEY && datalen && *data == '#' )
424         {
425           /* Early versions of GnuPG used old PGP comment packets;
426            * luckily all those comments are prefixed by a hash
427            * sign - ignore these packets. */
428         }
429       else if (pkttype == PKT_PUBLIC_SUBKEY || pkttype == PKT_SECRET_SUBKEY)
430         {
431           info->nsubkeys++;
432           if (info->nsubkeys == 1)
433             {
434               err = parse_key (data, datalen, &info->subkeys);
435               if (err)
436                 {
437                   info->nsubkeys--;
438                   /* We ignore subkeys with unknown algorithms. */
439                   if (gpg_err_code (err) == GPG_ERR_UNKNOWN_ALGORITHM
440                       || gpg_err_code (err) == GPG_ERR_UNSUPPORTED_ALGORITHM)
441                     err = 0;
442                   if (err)
443                     break;
444                 }
445               else
446                 ktail = &info->subkeys.next;
447             }
448           else
449             {
450               k = xtrycalloc (1, sizeof *k);
451               if (!k)
452                 {
453                   err = gpg_error_from_syserror ();
454                   break;
455                 }
456               err = parse_key (data, datalen, k);
457               if (err)
458                 {
459                   xfree (k);
460                   info->nsubkeys--;
461                   /* We ignore subkeys with unknown algorithms. */
462                   if (gpg_err_code (err) == GPG_ERR_UNKNOWN_ALGORITHM
463                       || gpg_err_code (err) == GPG_ERR_UNSUPPORTED_ALGORITHM)
464                     err = 0;
465                   if (err)
466                     break;
467                 }
468               else
469                 {
470                   *ktail = k;
471                   ktail = &k->next;
472                 }
473             }
474         }
475     }
476
477   if (err)
478     {
479       _keybox_destroy_openpgp_info (info);
480       if (!read_error)
481         {
482           /* Packet parsing worked, thus we should be able to skip the
483              rest of the keyblock.  */
484           while (image)
485             {
486               if (next_packet (&image, &imagelen,
487                                &data, &datalen, &pkttype, &n) )
488                 break; /* Another error - stop here. */
489
490               if (pkttype == PKT_PUBLIC_KEY || pkttype == PKT_SECRET_KEY)
491                 break; /* Next keyblock encountered - ready. */
492
493               if (nparsed)
494                 *nparsed += n;
495             }
496         }
497     }
498
499   return err;
500 }
501
502
503 /* Release any malloced data in INFO but not INFO itself! */
504 void
505 _keybox_destroy_openpgp_info (keybox_openpgp_info_t info)
506 {
507   struct _keybox_openpgp_key_info *k, *k2;
508   struct _keybox_openpgp_uid_info *u, *u2;
509
510   assert (!info->primary.next);
511   for (k=info->subkeys.next; k; k = k2)
512     {
513       k2 = k->next;
514       xfree (k);
515     }
516
517   for (u=info->uids.next; u; u = u2)
518     {
519       u2 = u->next;
520       xfree (u);
521     }
522 }