Imported Upstream version 2.1.8
[platform/upstream/gpg2.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 <http://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   pktlen = 0;
75   if ((ctb & 0x40))  /* New style (OpenPGP) CTB.  */
76     {
77       pkttype = (ctb & 0x3f);
78       if (!len)
79         return gpg_error (GPG_ERR_INV_PACKET); /* No 1st length byte. */
80       c = *buf++; len--;
81       if (pkttype == PKT_COMPRESSED)
82         return gpg_error (GPG_ERR_UNEXPECTED); /* ... packet in a keyblock. */
83       if ( c < 192 )
84         pktlen = c;
85       else if ( c < 224 )
86         {
87           pktlen = (c - 192) * 256;
88           if (!len)
89             return gpg_error (GPG_ERR_INV_PACKET); /* No 2nd length byte. */
90           c = *buf++; len--;
91           pktlen += c + 192;
92         }
93       else if (c == 255)
94         {
95           if (len <4 )
96             return gpg_error (GPG_ERR_INV_PACKET); /* No length bytes. */
97           pktlen = buf32_to_ulong (buf);
98           buf += 4;
99           len -= 4;
100       }
101       else /* Partial length encoding is not allowed for key packets. */
102         return gpg_error (GPG_ERR_UNEXPECTED);
103     }
104   else /* Old style CTB.  */
105     {
106       int lenbytes;
107
108       pktlen = 0;
109       pkttype = (ctb>>2)&0xf;
110       lenbytes = ((ctb&3)==3)? 0 : (1<<(ctb & 3));
111       if (!lenbytes) /* Not allowed in key packets.  */
112         return gpg_error (GPG_ERR_UNEXPECTED);
113       if (len < lenbytes)
114         return gpg_error (GPG_ERR_INV_PACKET); /* Not enough length bytes.  */
115       for (; lenbytes; lenbytes--)
116         {
117           pktlen <<= 8;
118           pktlen |= *buf++; len--;
119         }
120     }
121
122   /* Do some basic sanity check.  */
123   switch (pkttype)
124     {
125     case PKT_SIGNATURE:
126     case PKT_SECRET_KEY:
127     case PKT_PUBLIC_KEY:
128     case PKT_SECRET_SUBKEY:
129     case PKT_MARKER:
130     case PKT_RING_TRUST:
131     case PKT_USER_ID:
132     case PKT_PUBLIC_SUBKEY:
133     case PKT_OLD_COMMENT:
134     case PKT_ATTRIBUTE:
135     case PKT_COMMENT:
136     case PKT_GPG_CONTROL:
137       break; /* Okay these are allowed packets. */
138     default:
139       return gpg_error (GPG_ERR_UNEXPECTED);
140     }
141
142   if (pkttype == 63 && pktlen == 0xFFFFFFFF)
143     /* Sometimes the decompressing layer enters an error state in
144        which it simply outputs 0xff for every byte read.  If we have a
145        stream of 0xff bytes, then it will be detected as a new format
146        packet with type 63 and a 4-byte encoded length that is 4G-1.
147        Since packets with type 63 are private and we use them as a
148        control packet, which won't be 4 GB, we reject such packets as
149        invalid.  */
150     return gpg_error (GPG_ERR_INV_PACKET);
151
152   if (pktlen > len)
153     return gpg_error (GPG_ERR_INV_PACKET); /* Packet length header too long. */
154
155   *r_data = buf;
156   *r_datalen = pktlen;
157   *r_pkttype = pkttype;
158   *r_ntotal = (buf - *bufptr) + pktlen;
159
160   *bufptr = buf + pktlen;
161   *buflen = len - pktlen;
162   if (!*buflen)
163     *bufptr = NULL;
164
165   return 0;
166 }
167
168
169 /* Parse a key packet and store the information in KI. */
170 static gpg_error_t
171 parse_key (const unsigned char *data, size_t datalen,
172            struct _keybox_openpgp_key_info *ki)
173 {
174   gpg_error_t err;
175   const unsigned char *data_start = data;
176   int i, version, algorithm;
177   size_t n;
178   int npkey;
179   unsigned char hashbuffer[768];
180   const unsigned char *mpi_n = NULL;
181   size_t mpi_n_len = 0, mpi_e_len = 0;
182   gcry_md_hd_t md;
183   int is_ecc = 0;
184
185   if (datalen < 5)
186     return gpg_error (GPG_ERR_INV_PACKET);
187   version = *data++; datalen--;
188   if (version < 2 || version > 4 )
189     return gpg_error (GPG_ERR_INV_PACKET); /* Invalid version. */
190
191   /*timestamp = ((data[0]<<24)|(data[1]<<16)|(data[2]<<8)|(data[3]));*/
192   data +=4; datalen -=4;
193
194   if (version < 4)
195     {
196       if (datalen < 2)
197         return gpg_error (GPG_ERR_INV_PACKET);
198       data +=2; datalen -= 2;
199     }
200
201   if (!datalen)
202     return gpg_error (GPG_ERR_INV_PACKET);
203   algorithm = *data++; datalen--;
204
205   switch (algorithm)
206     {
207     case PUBKEY_ALGO_RSA:
208     case PUBKEY_ALGO_RSA_E:
209     case PUBKEY_ALGO_RSA_S:
210       npkey = 2;
211       break;
212     case PUBKEY_ALGO_ELGAMAL_E:
213     case PUBKEY_ALGO_ELGAMAL:
214       npkey = 3;
215       break;
216     case PUBKEY_ALGO_DSA:
217       npkey = 4;
218       break;
219     case PUBKEY_ALGO_ECDH:
220       npkey = 3;
221       is_ecc = 1;
222       break;
223     case PUBKEY_ALGO_ECDSA:
224     case PUBKEY_ALGO_EDDSA:
225       npkey = 2;
226       is_ecc = 1;
227       break;
228     default: /* Unknown algorithm. */
229       return gpg_error (GPG_ERR_UNKNOWN_ALGORITHM);
230     }
231
232   ki->algo = algorithm;
233
234   for (i=0; i < npkey; i++ )
235     {
236       unsigned int nbits, nbytes;
237
238       if (datalen < 2)
239         return gpg_error (GPG_ERR_INV_PACKET);
240
241       if (is_ecc && (i == 0 || i == 2))
242         {
243           nbytes = data[0];
244           if (nbytes < 2 || nbytes > 254)
245             return gpg_error (GPG_ERR_INV_PACKET);
246           nbytes++; /* The size byte itself.  */
247           if (datalen < nbytes)
248             return gpg_error (GPG_ERR_INV_PACKET);
249         }
250       else
251         {
252           nbits = ((data[0]<<8)|(data[1]));
253           data += 2;
254           datalen -= 2;
255           nbytes = (nbits+7) / 8;
256           if (datalen < nbytes)
257             return gpg_error (GPG_ERR_INV_PACKET);
258           /* For use by v3 fingerprint calculation we need to know the RSA
259              modulus and exponent. */
260           if (i==0)
261             {
262               mpi_n = data;
263               mpi_n_len = nbytes;
264             }
265           else if (i==1)
266             mpi_e_len = nbytes;
267         }
268
269       data += nbytes; datalen -= nbytes;
270     }
271   n = data - data_start;
272
273   if (version < 4)
274     {
275       /* We do not support any other algorithm than RSA in v3
276          packets. */
277       if (algorithm < 1 || algorithm > 3)
278         return gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM);
279
280       err = gcry_md_open (&md, GCRY_MD_MD5, 0);
281       if (err)
282         return err; /* Oops */
283       gcry_md_write (md, mpi_n, mpi_n_len);
284       gcry_md_write (md, mpi_n+mpi_n_len+2, mpi_e_len);
285       memcpy (ki->fpr, gcry_md_read (md, 0), 16);
286       gcry_md_close (md);
287       ki->fprlen = 16;
288
289       if (mpi_n_len < 8)
290         {
291           /* Moduli less than 64 bit are out of the specs scope.  Zero
292              them out because this is what gpg does too. */
293           memset (ki->keyid, 0, 8);
294         }
295       else
296         memcpy (ki->keyid, mpi_n + mpi_n_len - 8, 8);
297     }
298   else
299     {
300       /* Its a pitty that we need to prefix the buffer with the tag
301          and a length header: We can't simply pass it to the fast
302          hashing function for that reason.  It might be a good idea to
303          have a scatter-gather enabled hash function. What we do here
304          is to use a static buffer if this one is large enough and
305          only use the regular hash functions if this buffer is not
306          large enough. */
307       if ( 3 + n < sizeof hashbuffer )
308         {
309           hashbuffer[0] = 0x99;     /* CTB */
310           hashbuffer[1] = (n >> 8); /* 2 byte length header. */
311           hashbuffer[2] = n;
312           memcpy (hashbuffer + 3, data_start, n);
313           gcry_md_hash_buffer (GCRY_MD_SHA1, ki->fpr, hashbuffer, 3 + n);
314         }
315       else
316         {
317           err = gcry_md_open (&md, GCRY_MD_SHA1, 0);
318           if (err)
319             return err; /* Oops */
320           gcry_md_putc (md, 0x99 );     /* CTB */
321           gcry_md_putc (md, (n >> 8) ); /* 2 byte length header. */
322           gcry_md_putc (md, n );
323           gcry_md_write (md, data_start, n);
324           memcpy (ki->fpr, gcry_md_read (md, 0), 20);
325           gcry_md_close (md);
326         }
327       ki->fprlen = 20;
328       memcpy (ki->keyid, ki->fpr+12, 8);
329     }
330
331   return 0;
332 }
333
334
335
336 /* The caller must pass the address of an INFO structure which will
337    get filled on success with information pertaining to the OpenPGP
338    keyblock IMAGE of length IMAGELEN.  Note that a caller does only
339    need to release this INFO structure if the function returns
340    success.  If NPARSED is not NULL the actual number of bytes parsed
341    will be stored at this address.  */
342 gpg_error_t
343 _keybox_parse_openpgp (const unsigned char *image, size_t imagelen,
344                        size_t *nparsed, keybox_openpgp_info_t info)
345 {
346   gpg_error_t err = 0;
347   const unsigned char *image_start, *data;
348   size_t n, datalen;
349   int pkttype;
350   int first = 1;
351   int read_error = 0;
352   struct _keybox_openpgp_key_info *k, **ktail = NULL;
353   struct _keybox_openpgp_uid_info *u, **utail = NULL;
354
355   memset (info, 0, sizeof *info);
356   if (nparsed)
357     *nparsed = 0;
358
359   image_start = image;
360   while (image)
361     {
362       err = next_packet (&image, &imagelen, &data, &datalen, &pkttype, &n);
363       if (err)
364         {
365           read_error = 1;
366           break;
367         }
368
369       if (first)
370         {
371           if (pkttype == PKT_PUBLIC_KEY)
372             ;
373           else if (pkttype == PKT_SECRET_KEY)
374             info->is_secret = 1;
375           else
376             {
377               err = gpg_error (GPG_ERR_UNEXPECTED);
378               if (nparsed)
379                 *nparsed += n;
380               break;
381             }
382           first = 0;
383         }
384       else if (pkttype == PKT_PUBLIC_KEY || pkttype == PKT_SECRET_KEY)
385         break; /* Next keyblock encountered - ready. */
386
387       if (nparsed)
388         *nparsed += n;
389
390       if (pkttype == PKT_SIGNATURE)
391         {
392           /* For now we only count the total number of signatures. */
393           info->nsigs++;
394         }
395       else if (pkttype == PKT_USER_ID)
396         {
397           info->nuids++;
398           if (info->nuids == 1)
399             {
400               info->uids.off = data - image_start;
401               info->uids.len = datalen;
402               utail = &info->uids.next;
403             }
404           else
405             {
406               u = xtrycalloc (1, sizeof *u);
407               if (!u)
408                 {
409                   err = gpg_error_from_syserror ();
410                   break;
411                 }
412               u->off = data - image_start;
413               u->len = datalen;
414               *utail = u;
415               utail = &u->next;
416             }
417         }
418       else if (pkttype == PKT_PUBLIC_KEY || pkttype == PKT_SECRET_KEY)
419         {
420           err = parse_key (data, datalen, &info->primary);
421           if (err)
422             break;
423         }
424       else if( pkttype == PKT_PUBLIC_SUBKEY && datalen && *data == '#' )
425         {
426           /* Early versions of GnuPG used old PGP comment packets;
427            * luckily all those comments are prefixed by a hash
428            * sign - ignore these packets. */
429         }
430       else if (pkttype == PKT_PUBLIC_SUBKEY || pkttype == PKT_SECRET_SUBKEY)
431         {
432           info->nsubkeys++;
433           if (info->nsubkeys == 1)
434             {
435               err = parse_key (data, datalen, &info->subkeys);
436               if (err)
437                 {
438                   info->nsubkeys--;
439                   /* We ignore subkeys with unknown algorithms. */
440                   if (gpg_err_code (err) == GPG_ERR_UNKNOWN_ALGORITHM
441                       || gpg_err_code (err) == GPG_ERR_UNSUPPORTED_ALGORITHM)
442                     err = 0;
443                   if (err)
444                     break;
445                 }
446               else
447                 ktail = &info->subkeys.next;
448             }
449           else
450             {
451               k = xtrycalloc (1, sizeof *k);
452               if (!k)
453                 {
454                   err = gpg_error_from_syserror ();
455                   break;
456                 }
457               err = parse_key (data, datalen, k);
458               if (err)
459                 {
460                   xfree (k);
461                   info->nsubkeys--;
462                   /* We ignore subkeys with unknown algorithms. */
463                   if (gpg_err_code (err) == GPG_ERR_UNKNOWN_ALGORITHM
464                       || gpg_err_code (err) == GPG_ERR_UNSUPPORTED_ALGORITHM)
465                     err = 0;
466                   if (err)
467                     break;
468                 }
469               else
470                 {
471                   *ktail = k;
472                   ktail = &k->next;
473                 }
474             }
475         }
476     }
477
478   if (err)
479     {
480       _keybox_destroy_openpgp_info (info);
481       if (!read_error)
482         {
483           /* Packet parsing worked, thus we should be able to skip the
484              rest of the keyblock.  */
485           while (image)
486             {
487               if (next_packet (&image, &imagelen,
488                                &data, &datalen, &pkttype, &n) )
489                 break; /* Another error - stop here. */
490
491               if (pkttype == PKT_PUBLIC_KEY || pkttype == PKT_SECRET_KEY)
492                 break; /* Next keyblock encountered - ready. */
493
494               if (nparsed)
495                 *nparsed += n;
496             }
497         }
498     }
499
500   return err;
501 }
502
503
504 /* Release any malloced data in INFO but not INFO itself! */
505 void
506 _keybox_destroy_openpgp_info (keybox_openpgp_info_t info)
507 {
508   struct _keybox_openpgp_key_info *k, *k2;
509   struct _keybox_openpgp_uid_info *u, *u2;
510
511   assert (!info->primary.next);
512   for (k=info->subkeys.next; k; k = k2)
513     {
514       k2 = k->next;
515       xfree (k);
516     }
517
518   for (u=info->uids.next; u; u = u2)
519     {
520       u2 = u->next;
521       xfree (u);
522     }
523 }