~ubuntu-branches/ubuntu/precise/gnupg2/precise-security

« back to all changes in this revision

Viewing changes to kbx/keybox-search.c

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2015-03-27 08:20:03 UTC
  • Revision ID: package-import@ubuntu.com-20150327082003-doob38ydqdxubnf1
Tags: 2.0.17-2ubuntu2.12.04.6
* Screen responses from keyservers (LP: #1409117)
  - d/p/0001-Screen-keyserver-responses.patch
  - d/p/0002-Make-screening-of-keyserver-result-work-with-multi-k.patch
  - d/p/0003-Add-kbnode_t-for-easier-backporting.patch
  - d/p/0004-gpg-Fix-regression-due-to-the-keyserver-import-filte.patch
* Fix large key size regression from CVE-2014-5270 changes (LP: #1371766)
  - d/p/Add-build-and-runtime-support-for-larger-RSA-key.patch
  - debian/rules: build with --enable-large-secmem
* SECURITY UPDATE: invalid memory read via invalid keyring
  - debian/patches/CVE-2015-1606.patch: skip all packets not allowed in
    a keyring in g10/keyring.c.
  - CVE-2015-1606
* SECURITY UPDATE: memcpy with overlapping ranges
  - debian/patches/CVE-2015-1607.patch: use inline functions to convert
    buffer data to scalars in common/iobuf.c, g10/build-packet.c,
    g10/getkey.c, g10/keygen.c, g10/keyid.c, g10/main.h, g10/misc.c,
    g10/parse-packet.c, g10/tdbio.c, g10/trustdb.c, include/host2net.h,
    kbx/keybox-dump.c, kbx/keybox-openpgp.c, kbx/keybox-search.c,
    kbx/keybox-update.c, scd/apdu.c, scd/app-openpgp.c,
    scd/ccid-driver.c, scd/pcsc-wrapper.c, tools/ccidmon.c.
  - CVE-2015-1607

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include <errno.h>
26
26
 
27
27
#include "../jnlib/stringhelp.h" /* ascii_xxxx() */
 
28
#include "../include/host2net.h"
28
29
 
29
30
#include "keybox-defs.h"
30
31
#include <gcrypt.h>
45
46
static inline ulong
46
47
get32 (const byte *buffer)
47
48
{
48
 
  ulong a;
49
 
  a =  *buffer << 24;
50
 
  a |= buffer[1] << 16;
51
 
  a |= buffer[2] << 8;
52
 
  a |= buffer[3];
53
 
  return a;
 
49
  return buf32_to_ulong (buffer);
54
50
}
55
51
 
56
52
static inline ulong
57
53
get16 (const byte *buffer)
58
54
{
59
 
  ulong a;
60
 
  a =  *buffer << 8;
61
 
  a |= buffer[1];
62
 
  return a;
 
55
  return buf16_to_ulong (buffer);
63
56
}
64
57
 
65
58
 
112
105
      *flag_off = 6;
113
106
      *flag_size = 2;
114
107
      break;
115
 
    
 
108
 
116
109
    case KEYBOX_FLAG_OWNERTRUST:
117
110
    case KEYBOX_FLAG_VALIDITY:
118
111
    case KEYBOX_FLAG_CREATED_AT:
127
120
      if (pos+2 > length)
128
121
        return GPG_ERR_INV_OBJ; /* Out of bounds. */
129
122
      /* Serial number. */
130
 
      nserial = get16 (buffer+pos); 
 
123
      nserial = get16 (buffer+pos);
131
124
      pos += 2 + nserial;
132
125
      if (pos+4 > length)
133
126
        return GPG_ERR_INV_OBJ; /* Out of bounds. */
135
128
      nuids = get16 (buffer + pos); pos += 2;
136
129
      uidinfolen = get16 (buffer + pos); pos += 2;
137
130
      if (uidinfolen < 12 )
138
 
        return GPG_ERR_INV_OBJ; 
 
131
        return GPG_ERR_INV_OBJ;
139
132
      pos += uidinfolen*nuids;
140
133
      if (pos+4 > length)
141
134
        return GPG_ERR_INV_OBJ ; /* Out of bounds. */
143
136
      nsigs = get16 (buffer + pos); pos += 2;
144
137
      siginfolen = get16 (buffer + pos); pos += 2;
145
138
      if (siginfolen < 4 )
146
 
        return GPG_ERR_INV_OBJ; 
 
139
        return GPG_ERR_INV_OBJ;
147
140
      pos += siginfolen*nsigs;
148
141
      if (pos+1+1+2+4+4+4+4 > length)
149
142
        return GPG_ERR_INV_OBJ ; /* Out of bounds. */
190
183
      case 4: *value = get32 (buffer + pos); break;
191
184
      default: ec = GPG_ERR_BUG; break;
192
185
      }
193
 
  
 
186
 
194
187
  return ec;
195
188
}
196
189
 
218
211
    return 0; /* out of bounds */
219
212
 
220
213
  /*serial*/
221
 
  nserial = get16 (buffer+pos); 
 
214
  nserial = get16 (buffer+pos);
222
215
  off = pos + 2;
223
216
  if (off+nserial > length)
224
217
    return 0; /* out of bounds */
316
309
    return 0; /* out of bounds */
317
310
 
318
311
  /*serial*/
319
 
  nserial = get16 (buffer+pos); 
 
312
  nserial = get16 (buffer+pos);
320
313
  pos += 2 + nserial;
321
314
  if (pos+4 > length)
322
315
    return 0; /* out of bounds */
332
325
  if (idx < 0)
333
326
    { /* compare all names starting with that (negated) index */
334
327
      idx = -idx;
335
 
      
 
328
 
336
329
      for ( ;idx < nuids; idx++)
337
330
        {
338
331
          size_t mypos = pos;
409
402
    return 0; /* out of bounds */
410
403
 
411
404
  /*serial*/
412
 
  nserial = get16 (buffer+pos); 
 
405
  nserial = get16 (buffer+pos);
413
406
  pos += 2 + nserial;
414
407
  if (pos+4 > length)
415
408
    return 0; /* out of bounds */
428
421
  for (idx=1 ;idx < nuids; idx++)
429
422
    {
430
423
      size_t mypos = pos;
431
 
      
 
424
 
432
425
      mypos += idx*uidinfolen;
433
426
      off = get32 (buffer+mypos);
434
427
      len = get32 (buffer+mypos+4);
439
432
      len--; /* one back */
440
433
      if ( len < 3 || buffer[off+len] != '>')
441
434
        continue; /* not a proper email address */
442
 
      len--; 
 
435
      len--;
443
436
      if (substr)
444
437
        {
445
438
          if (ascii_memcasemem (buffer+off+1, len, name, namelen))
474
467
  unsigned char array[20];
475
468
  unsigned char *rcp;
476
469
  size_t n;
477
 
  
 
470
 
478
471
  buffer = _keybox_get_blob_image (blob, &length);
479
472
  if (length < 40)
480
473
    return 0; /* Too short. */
527
520
 
528
521
 
529
522
/*
530
 
  The has_foo functions are used as helpers for search 
 
523
  The has_foo functions are used as helpers for search
531
524
*/
532
525
static inline int
533
526
has_short_kid (KEYBOXBLOB blob, const unsigned char *kid)
585
578
    return 0;
586
579
 
587
580
  namelen = strlen (name);
588
 
  
 
581
 
589
582
  return (blob_cmp_sn (blob, sn, snlen)
590
583
          && blob_cmp_name (blob, 0 /* issuer */, name, namelen, 0));
591
584
}
664
657
 
665
658
*/
666
659
 
667
 
int 
 
660
int
668
661
keybox_search_reset (KEYBOX_HANDLE hd)
669
662
{
670
663
  if (!hd)
683
676
    }
684
677
  hd->error = 0;
685
678
  hd->eof = 0;
686
 
  return 0;   
 
679
  return 0;
687
680
}
688
681
 
689
682
 
690
683
/* Note: When in ephemeral mode the search function does visit all
691
684
   blobs but in standard mode, blobs flagged as ephemeral are ignored.  */
692
 
int 
 
685
int
693
686
keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc)
694
687
{
695
688
  int rc;
708
701
      hd->found.blob = NULL;
709
702
    }
710
703
 
711
 
  if (hd->error)  
 
704
  if (hd->error)
712
705
    return hd->error; /* still in error state */
713
 
  if (hd->eof)  
 
706
  if (hd->eof)
714
707
    return -1; /* still EOF */
715
708
 
716
709
  /* figure out what information we need */
717
710
  need_words = any_skip = 0;
718
 
  for (n=0; n < ndesc; n++) 
 
711
  for (n=0; n < ndesc; n++)
719
712
    {
720
 
      switch (desc[n].mode) 
 
713
      switch (desc[n].mode)
721
714
        {
722
 
        case KEYDB_SEARCH_MODE_WORDS: 
 
715
        case KEYDB_SEARCH_MODE_WORDS:
723
716
          need_words = 1;
724
717
          break;
725
718
        case KEYDB_SEARCH_MODE_FIRST:
729
722
        default:
730
723
          break;
731
724
        }
732
 
      if (desc[n].skipfnc) 
 
725
      if (desc[n].skipfnc)
733
726
        any_skip = 1;
734
727
      if (desc[n].snlen == -1 && !sn_array)
735
728
        {
762
755
      int i, odd;
763
756
      size_t snlen;
764
757
 
765
 
      for (n=0; n < ndesc; n++) 
 
758
      for (n=0; n < ndesc; n++)
766
759
        {
767
760
          if (!desc[n].sn)
768
761
            ;
830
823
      if (!hd->ephemeral && (blobflags & 2))
831
824
        continue; /* Not in ephemeral mode but blob is flagged ephemeral.  */
832
825
 
833
 
      for (n=0; n < ndesc; n++) 
 
826
      for (n=0; n < ndesc; n++)
834
827
        {
835
828
          switch (desc[n].mode)
836
829
            {
837
 
            case KEYDB_SEARCH_MODE_NONE: 
 
830
            case KEYDB_SEARCH_MODE_NONE:
838
831
              never_reached ();
839
832
              break;
840
 
            case KEYDB_SEARCH_MODE_EXACT: 
 
833
            case KEYDB_SEARCH_MODE_EXACT:
841
834
              if (has_subject_or_alt (blob, desc[n].u.name, 0))
842
835
                goto found;
843
836
              break;
854
847
                goto found;
855
848
              break;
856
849
            case KEYDB_SEARCH_MODE_MAILEND:
857
 
            case KEYDB_SEARCH_MODE_WORDS: 
 
850
            case KEYDB_SEARCH_MODE_WORDS:
858
851
              never_reached (); /* not yet implemented */
859
852
              break;
860
853
            case KEYDB_SEARCH_MODE_ISSUER:
876
869
              if (has_subject (blob, desc[n].u.name))
877
870
                goto found;
878
871
              break;
879
 
            case KEYDB_SEARCH_MODE_SHORT_KID: 
 
872
            case KEYDB_SEARCH_MODE_SHORT_KID:
880
873
              if (has_short_kid (blob, desc[n].u.kid))
881
874
                goto found;
882
875
              break;
893
886
              if (has_keygrip (blob, desc[n].u.grip))
894
887
                goto found;
895
888
              break;
896
 
            case KEYDB_SEARCH_MODE_FIRST: 
897
 
              goto found;
898
 
              break;
899
 
            case KEYDB_SEARCH_MODE_NEXT: 
900
 
              goto found;
901
 
              break;
902
 
            default: 
 
889
            case KEYDB_SEARCH_MODE_FIRST:
 
890
              goto found;
 
891
              break;
 
892
            case KEYDB_SEARCH_MODE_NEXT:
 
893
              goto found;
 
894
              break;
 
895
            default:
903
896
              rc = gpg_error (GPG_ERR_INV_VALUE);
904
897
              goto found;
905
898
            }
906
899
        }
907
900
      continue;
908
 
    found:  
909
 
      for (n=any_skip?0:ndesc; n < ndesc; n++) 
 
901
    found:
 
902
      for (n=any_skip?0:ndesc; n < ndesc; n++)
910
903
        {
911
904
/*            if (desc[n].skipfnc */
912
905
/*                && desc[n].skipfnc (desc[n].skipfncvalue, aki)) */
915
908
      if (n == ndesc)
916
909
        break; /* got it */
917
910
    }
918
 
  
 
911
 
919
912
  if (!rc)
920
913
    {
921
914
      hd->found.blob = blob;
925
918
      _keybox_release_blob (blob);
926
919
      hd->eof = 1;
927
920
    }
928
 
  else 
 
921
  else
929
922
    {
930
923
      _keybox_release_blob (blob);
931
924
      hd->error = rc;