~ubuntu-branches/ubuntu/natty/libgcrypt11/natty-proposed

« back to all changes in this revision

Viewing changes to src/global.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2009-02-21 13:46:58 UTC
  • mto: (1.1.6 upstream) (2.1.3 squeeze)
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20090221134658-855twvcr4ezk2ron
ImportĀ upstreamĀ versionĀ 1.4.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* global.c  -  global control functions
2
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
3
 
 *               2004, 2005, 2006  Free Software Foundation, Inc.
 
3
 *               2004, 2005, 2006, 2008  Free Software Foundation, Inc.
4
4
 *
5
5
 * This file is part of Libgcrypt.
6
6
 *
15
15
 * GNU Lesser General Public License for more details.
16
16
 *
17
17
 * You should have received a copy of the GNU Lesser General Public
18
 
 * License along with this program; if not, write to the Free Software
19
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
18
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
20
19
 */
21
20
 
22
21
#include <config.h>
28
27
#include <ctype.h>
29
28
#include <limits.h>
30
29
#include <errno.h>
 
30
#include <unistd.h>
 
31
#ifdef HAVE_SYSLOG
 
32
# include <syslog.h>
 
33
#endif /*HAVE_SYSLOG*/
31
34
 
32
35
#include "g10lib.h"
33
36
#include "cipher.h"
43
46
 */
44
47
static unsigned int debug_flags;
45
48
 
 
49
/* gcry_control (GCRYCTL_SET_FIPS_MODE), sets this flag so that the
 
50
   intialization code swicthed fips mode on.  */
 
51
static int force_fips_mode;
 
52
 
46
53
/* Controlled by global_init().  */
47
54
static int any_init_done;
48
55
 
 
56
 
 
57
 
49
58
/* Memory management. */
50
59
 
51
60
static gcry_handler_alloc_t alloc_func;
57
66
static void *outofcore_handler_value;
58
67
static int no_secure_memory;
59
68
 
 
69
 
60
70
 
61
71
 
 
72
 
62
73
/* This is our handmade constructor.  It gets called by any function
63
74
   likely to be called at startup.  The suggested way for an
64
75
   application to make sure that this has been called is by using
72
83
    return;
73
84
  any_init_done = 1;
74
85
 
 
86
  /* Initialize our portable thread/mutex wrapper.  */
75
87
  err = ath_init ();
76
88
  if (err)
77
89
    goto fail;
 
90
  
 
91
  /* See whether the system is in FIPS mode.  This needs to come as
 
92
     early as possible put after the ATH has been initialized.  */
 
93
  _gcry_initialize_fips_mode (force_fips_mode);
78
94
 
79
95
  /* Before we do any other initialization we need to test available
80
96
     hardware features.  */
90
106
  if (err)
91
107
    goto fail;
92
108
#if 0
93
 
  /* FIXME? */
94
 
  err = _gcry_ac_init ();
95
 
  if (err)
96
 
    goto fail;
 
109
  /* Hmmm, as of now ac_init does nothing. */
 
110
  if ( !fips_mode () )
 
111
    {
 
112
      err = _gcry_ac_init ();
 
113
      if (err)
 
114
        goto fail;
 
115
    }
97
116
#endif
98
117
 
99
118
  return;
100
119
 
101
120
 fail:
102
 
  /* FIXME: use `err'?  */
103
121
  BUG ();
104
122
}
105
123
 
 
124
 
 
125
/* This function is called by the macro fips_is_operational and makes
 
126
   sure that the minimal initialization has been done.  This is far
 
127
   from a perfect solution and hides problems with an improper
 
128
   initialization but at least in single-threaded mode it should work
 
129
   reliable. 
 
130
 
 
131
   The reason we need this is that a lot of applications don't use
 
132
   Libgcrypt properly by not running any initialization code at all.
 
133
   They just call a Libgcrypt function and that is all what they want.
 
134
   Now with the FIPS mode, that has the side effect of entering FIPS
 
135
   mode (for security reasons, FIPS mode is the default if no
 
136
   initialization has been done) and bailing out immediately because
 
137
   the FSM is in the wrong state.  If we always run the init code,
 
138
   Libgcrypt can test for FIPS mode and at least if not in FIPS mode,
 
139
   it will behave as before.  Note that this on-the-fly initialization
 
140
   is only done for the cryptographic functions subject to FIPS mode
 
141
   and thus not all API calls will do such an initialization.  */
 
142
int
 
143
_gcry_global_is_operational (void)
 
144
{
 
145
  if (!any_init_done)
 
146
    {
 
147
#ifdef HAVE_SYSLOG
 
148
      syslog (LOG_USER|LOG_WARNING, "Libgcrypt warning: "
 
149
              "missing initialization - please fix the application");
 
150
#endif /*HAVE_SYSLOG*/
 
151
      global_init ();
 
152
    }
 
153
  return _gcry_fips_is_operational ();
 
154
}
 
155
 
 
156
 
 
157
 
106
158
 
107
 
 
108
159
/* Version number parsing.  */
109
160
 
110
161
/* This function parses the first portion of the version number S and
243
294
  if ( (hwf & hwflist[i].flag) )
244
295
    fnc (fp, "%s:", hwflist[i].desc);
245
296
  fnc (fp, "\n");
 
297
  /* We use y/n instead of 1/0 for the simple reason that Emacsen's
 
298
     compile error parser would accidently flag that line when printed
 
299
     during "make check" as an error.  */
 
300
  fnc (fp, "fips-mode:%c:%c:\n", 
 
301
       fips_mode ()? 'y':'n',
 
302
       _gcry_enforced_fips_mode ()? 'y':'n' );
246
303
}
247
304
 
248
305
 
267
324
      break;
268
325
 
269
326
    case GCRYCTL_FAKED_RANDOM_P:
270
 
      /* Return an error if the RNG is faked one (i.e. enabled by
 
327
      /* Return an error if the RNG is faked one (e.g. enabled by
271
328
         ENABLE_QUICK_RANDOM. */
272
329
      if (_gcry_random_is_faked ())
273
 
        err = GPG_ERR_GENERAL;
 
330
        err = GPG_ERR_GENERAL;  /* Use as TRUE value.  */
274
331
      break;
275
332
 
276
333
    case GCRYCTL_DUMP_RANDOM_STATS:
324
381
 
325
382
    case GCRYCTL_USE_SECURE_RNDPOOL:
326
383
      global_init ();
327
 
      _gcry_secure_random_alloc (); /* put random number into secure memory */
 
384
      _gcry_secure_random_alloc (); /* Put random number into secure memory. */
328
385
      break;
329
386
 
330
387
    case GCRYCTL_SET_RANDOM_SEED_FILE:
332
389
      break;
333
390
 
334
391
    case GCRYCTL_UPDATE_RANDOM_SEED_FILE:
335
 
      _gcry_update_random_seed_file ();
 
392
      if ( fips_is_operational () )
 
393
        _gcry_update_random_seed_file ();
336
394
      break;
337
395
 
338
396
    case GCRYCTL_SET_VERBOSITY:
348
406
      break;
349
407
 
350
408
    case GCRYCTL_DISABLE_INTERNAL_LOCKING:
 
409
      /* Not used anymore.  */
351
410
      global_init ();
352
411
      break;
353
412
 
358
417
 
359
418
    case GCRYCTL_INITIALIZATION_FINISHED_P:
360
419
      if (init_finished)
361
 
        err = GPG_ERR_GENERAL;
 
420
        err = GPG_ERR_GENERAL; /* Yes.  */
362
421
      break;
363
422
 
364
423
    case GCRYCTL_INITIALIZATION_FINISHED:
367
426
         are started.  It is not really needed but the only way to be
368
427
         really sure that all initialization for thread-safety has
369
428
         been done. */
370
 
        if (! init_finished)
371
 
          {
372
 
            global_init ();
373
 
            /* Do only a basic random initialization, i.e. init the
374
 
               mutexes. */
375
 
            _gcry_random_initialize (0);
376
 
            init_finished = 1;
377
 
          }
378
 
        break;
 
429
      if (! init_finished)
 
430
        {
 
431
          global_init ();
 
432
          /* Do only a basic random initialization, i.e. init the
 
433
             mutexes. */
 
434
          _gcry_random_initialize (0);
 
435
          init_finished = 1;
 
436
          /* Force us into operational state if in FIPS mode.  */
 
437
          (void)fips_is_operational ();
 
438
        }
 
439
      break;
379
440
 
380
441
    case GCRYCTL_SET_THREAD_CBS:
381
442
      err = ath_install (va_arg (arg_ptr, void *), any_init_done);
387
448
      /* We need to do make sure that the random pool is really
388
449
         initialized so that the poll function is not a NOP. */
389
450
      _gcry_random_initialize (1);
390
 
      _gcry_fast_random_poll (); 
 
451
 
 
452
      if ( fips_is_operational () )
 
453
        _gcry_fast_random_poll (); 
391
454
      break;
392
455
 
393
456
    case GCRYCTL_SET_RNDEGD_SOCKET:
420
483
      }
421
484
      break;
422
485
 
 
486
    case GCRYCTL_OPERATIONAL_P:
 
487
      /* Returns true if the library is in an operational state.  This
 
488
         is always true for non-fips mode.  */
 
489
      if (_gcry_fips_test_operational ())
 
490
        err = GPG_ERR_GENERAL; /* Used as TRUE value */
 
491
      break;
 
492
 
 
493
    case GCRYCTL_FIPS_MODE_P:
 
494
      if (fips_mode () 
 
495
          && !_gcry_is_fips_mode_inactive () 
 
496
          && !no_secure_memory)
 
497
        err = GPG_ERR_GENERAL; /* Used as TRUE value */
 
498
      break;
 
499
 
 
500
    case GCRYCTL_FORCE_FIPS_MODE:
 
501
      /* Performing this command puts the library into fips mode.  If
 
502
         the library has already been initialized into fips mode, a
 
503
         selftest is triggered.  it is not possible to put the libraty
 
504
         into fips mode after having passed the initialization. */
 
505
      if (!any_init_done)
 
506
        {
 
507
          /* Not yet intialized at all.  Set a flag so that we are put
 
508
             into fips mode during initialization.  */
 
509
          force_fips_mode = 1;
 
510
        }
 
511
      else 
 
512
        {
 
513
          /* Already initialized.  If we are already operational we
 
514
             run a selftest.  If not we use the is_operational call to
 
515
             force us into operational state if possible.  */
 
516
          if (_gcry_fips_test_error_or_operational ())
 
517
            _gcry_fips_run_selftests (1);
 
518
          if (_gcry_fips_is_operational ())
 
519
            err = GPG_ERR_GENERAL; /* Used as TRUE value */
 
520
      }
 
521
      break;
 
522
 
 
523
    case GCRYCTL_SELFTEST:
 
524
      /* Run a selftest.  This works in fips mode as well as in
 
525
         standard mode.  In contrast to the power-up tests, we use an
 
526
         extended version of the selftests. Returns 0 on success or an
 
527
         error code. */
 
528
      global_init ();
 
529
      err = _gcry_fips_run_selftests (1);
 
530
      break;
 
531
 
 
532
    case 58:  /* Init external random test.  */
 
533
      {
 
534
        void **rctx        = va_arg (arg_ptr, void **);
 
535
        unsigned int flags = va_arg (arg_ptr, unsigned int);
 
536
        const void *key    = va_arg (arg_ptr, const void *);
 
537
        size_t keylen      = va_arg (arg_ptr, size_t);
 
538
        const void *seed   = va_arg (arg_ptr, const void *);
 
539
        size_t seedlen     = va_arg (arg_ptr, size_t);
 
540
        const void *dt     = va_arg (arg_ptr, const void *);
 
541
        size_t dtlen       = va_arg (arg_ptr, size_t);
 
542
        if (!fips_is_operational ())
 
543
          err = fips_not_operational ();
 
544
        else
 
545
          err = _gcry_random_init_external_test (rctx, flags, key, keylen,
 
546
                                                 seed, seedlen, dt, dtlen);
 
547
      }
 
548
      break;
 
549
    case 59:  /* Run external random test.  */
 
550
      {
 
551
        void *ctx     = va_arg (arg_ptr, void *);
 
552
        void *buffer  = va_arg (arg_ptr, void *);
 
553
        size_t buflen = va_arg (arg_ptr, size_t);
 
554
        if (!fips_is_operational ())
 
555
          err = fips_not_operational ();
 
556
        else
 
557
          err = _gcry_random_run_external_test (ctx, buffer, buflen);
 
558
      }
 
559
      break;
 
560
    case 60:  /* Deinit external random test.  */
 
561
      {
 
562
        void *ctx = va_arg (arg_ptr, void *);
 
563
        _gcry_random_deinit_external_test (ctx);
 
564
      }
 
565
      break;
 
566
 
 
567
 
423
568
    default:
424
569
      err = GPG_ERR_INV_OP;
425
570
    }
495
640
  return gcry_error (gpg_err_code_from_errno (err));
496
641
}
497
642
 
498
 
/****************
 
643
 
 
644
/* Set custom allocation handlers.  This is in general not useful
 
645
 * because the libgcrypt allocation functions are guaranteed to
 
646
 * provide proper allocation handlers which zeroize memory if needed.
499
647
 * NOTE: All 5 functions should be set.  */
500
648
void
501
649
gcry_set_allocation_handler (gcry_handler_alloc_t new_alloc_func,
506
654
{
507
655
  global_init ();
508
656
 
 
657
  if (fips_mode ())
 
658
    {
 
659
      /* We do not want to enforce the fips mode, but merely set a
 
660
         flag so that the application may check whether it is still in
 
661
         fips mode.  */
 
662
      _gcry_inactivate_fips_mode ("custom allocation handler");
 
663
    }
 
664
 
509
665
  alloc_func = new_alloc_func;
510
666
  alloc_secure_func = new_alloc_secure_func;
511
667
  is_secure_func = new_is_secure_func;
533
689
gcry_set_outofcore_handler( int (*f)( void*, size_t, unsigned int ),
534
690
                                                        void *value )
535
691
{
536
 
    global_init ();
537
 
 
538
 
    outofcore_handler = f;
539
 
    outofcore_handler_value = value;
540
 
}
 
692
  global_init ();
 
693
 
 
694
  if (fips_mode () )
 
695
    {
 
696
      log_info ("out of core handler ignored in FIPS mode\n");
 
697
      return;
 
698
    }
 
699
  
 
700
  outofcore_handler = f;
 
701
  outofcore_handler_value = value;
 
702
}
 
703
 
 
704
/* Return the no_secure_memory flag.  */
 
705
static int
 
706
get_no_secure_memory (void)
 
707
{
 
708
  if (!no_secure_memory)
 
709
    return 0;
 
710
  if (_gcry_enforced_fips_mode ())
 
711
    {
 
712
      no_secure_memory = 0;
 
713
      return 0;
 
714
    }
 
715
  return no_secure_memory;
 
716
}
 
717
 
541
718
 
542
719
static gcry_err_code_t
543
720
do_malloc (size_t n, unsigned int flags, void **mem)
545
722
  gcry_err_code_t err = 0;
546
723
  void *m;
547
724
 
548
 
  if ((flags & GCRY_ALLOC_FLAG_SECURE) && !no_secure_memory)
 
725
  if ((flags & GCRY_ALLOC_FLAG_SECURE) && !get_no_secure_memory ())
549
726
    {
550
727
      if (alloc_secure_func)
551
728
        m = (*alloc_secure_func) (n);
597
774
int
598
775
gcry_is_secure (const void *a)
599
776
{
600
 
  if (no_secure_memory)
 
777
  if (get_no_secure_memory ())
601
778
    return 0;
602
779
  if (is_secure_func)
603
780
    return is_secure_func (a) ;
633
810
}
634
811
 
635
812
void
636
 
gcry_free( void *p )
 
813
gcry_free (void *p)
637
814
{
638
 
  if( !p )
 
815
  int save_errno;
 
816
 
 
817
  if (!p)
639
818
    return;
640
819
 
 
820
  /* In case ERRNO is set we better save it so that the free machinery
 
821
     may not accidently change ERRNO.  We restore it only if it was
 
822
     already set to comply with the usual C semantic for ERRNO.  */
 
823
  save_errno = errno;
641
824
  if (free_func)
642
825
    free_func (p);
643
826
  else
644
827
    _gcry_private_free (p);
 
828
 
 
829
  if (save_errno)
 
830
    errno = save_errno;
645
831
}
646
832
 
647
833
void *
712
898
void *
713
899
gcry_xmalloc( size_t n )
714
900
{
715
 
    void *p;
716
 
 
717
 
    while ( !(p = gcry_malloc( n )) ) {
718
 
        if( !outofcore_handler
719
 
            || !outofcore_handler( outofcore_handler_value, n, 0 ) ) {
720
 
            _gcry_fatal_error(gpg_err_code_from_errno (errno), NULL );
721
 
        }
 
901
  void *p;
 
902
  
 
903
  while ( !(p = gcry_malloc( n )) ) 
 
904
    {
 
905
      if ( fips_mode () 
 
906
           || !outofcore_handler
 
907
           || !outofcore_handler (outofcore_handler_value, n, 0) )
 
908
        {
 
909
          _gcry_fatal_error (gpg_err_code_from_errno (errno), NULL);
 
910
        }
722
911
    }
723
912
    return p;
724
913
}
726
915
void *
727
916
gcry_xrealloc( void *a, size_t n )
728
917
{
729
 
    void *p;
730
 
 
731
 
    while ( !(p = gcry_realloc( a, n )) ) {
732
 
        if( !outofcore_handler
733
 
            || !outofcore_handler( outofcore_handler_value, n,
734
 
                                   gcry_is_secure(a)? 3:2 ) ) {
735
 
            _gcry_fatal_error(gpg_err_code_from_errno (errno), NULL );
 
918
  void *p;
 
919
  
 
920
  while ( !(p = gcry_realloc( a, n )) )
 
921
    {
 
922
      if ( fips_mode ()
 
923
           || !outofcore_handler
 
924
           || !outofcore_handler (outofcore_handler_value, n,
 
925
                                   gcry_is_secure(a)? 3:2 ) )
 
926
        {
 
927
          _gcry_fatal_error (gpg_err_code_from_errno (errno), NULL );
736
928
        }
737
929
    }
738
930
    return p;
741
933
void *
742
934
gcry_xmalloc_secure( size_t n )
743
935
{
744
 
    void *p;
745
 
 
746
 
    while ( !(p = gcry_malloc_secure( n )) ) {
747
 
        if( !outofcore_handler
748
 
            || !outofcore_handler( outofcore_handler_value, n, 1 ) ) {
749
 
            _gcry_fatal_error(gpg_err_code_from_errno (errno),
750
 
                             _("out of core in secure memory"));
 
936
  void *p;
 
937
  
 
938
  while ( !(p = gcry_malloc_secure( n )) ) 
 
939
    {
 
940
      if ( fips_mode ()
 
941
           || !outofcore_handler
 
942
           || !outofcore_handler (outofcore_handler_value, n, 1) )
 
943
        {
 
944
          _gcry_fatal_error (gpg_err_code_from_errno (errno),
 
945
                             _("out of core in secure memory"));
751
946
        }
752
947
    }
753
 
    return p;
 
948
  return p;
754
949
}
755
950
 
756
951
 
794
989
gcry_xstrdup (const char *string)
795
990
{
796
991
  char *p;
797
 
 
 
992
  
798
993
  while ( !(p = gcry_strdup (string)) ) 
799
994
    {
800
995
      size_t n = strlen (string);
801
996
      int is_sec = !!gcry_is_secure (string);
802
 
 
803
 
      if (!outofcore_handler
 
997
      
 
998
      if (fips_mode ()
 
999
          || !outofcore_handler
804
1000
          || !outofcore_handler (outofcore_handler_value, n, is_sec) ) 
805
1001
        {
806
1002
          _gcry_fatal_error (gpg_err_code_from_errno (errno),
813
1009
 
814
1010
 
815
1011
int
816
 
_gcry_get_debug_flag( unsigned int mask )
 
1012
_gcry_get_debug_flag (unsigned int mask)
817
1013
{
818
 
    return debug_flags & mask;
 
1014
  if ( fips_mode () )
 
1015
    return 0;
 
1016
  return (debug_flags & mask);
819
1017
}
820
1018
 
821
1019