~ubuntu-branches/ubuntu/precise/gnupg2/precise-proposed

« back to all changes in this revision

Viewing changes to cipher/rndegd.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Urlichs
  • Date: 2006-01-24 04:31:42 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060124043142-pbg192or6qxv3yk2
Tags: 1.9.20-1
* New Upstream version. Closes:#306890,#344530
  * Closes:#320490: gpg-protect-tool fails to decrypt PKCS-12 files 
* Depend on libopensc2-dev, not -1-. Closes:#348106

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* rndegd.c  -  interface to the EGD
 
2
 *      Copyright (C) 1999, 2000, 2001, 2002 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 2 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, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
 
 
23
#ifdef USE_RNDEGD
 
24
 
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <assert.h>
 
28
#include <errno.h>
 
29
#include <sys/time.h>
 
30
#include <sys/stat.h>
 
31
#include <string.h>
 
32
#include <unistd.h>
 
33
#include <sys/types.h>
 
34
#include <sys/socket.h>
 
35
#include <sys/un.h>
 
36
#include "types.h"
 
37
#include "util.h"
 
38
#include "ttyio.h"
 
39
#include "algorithms.h"
 
40
#include "cipher.h"
 
41
#include "i18n.h"
 
42
 
 
43
 
 
44
#ifndef offsetof
 
45
#define offsetof(type, member) ((size_t) &((type *)0)->member)
 
46
#endif
 
47
 
 
48
static int egd_socket = -1;
 
49
 
 
50
static int
 
51
do_write( int fd, void *buf, size_t nbytes )
 
52
{
 
53
    size_t nleft = nbytes;
 
54
    int nwritten;
 
55
 
 
56
    while( nleft > 0 ) {
 
57
        nwritten = write( fd, buf, nleft);
 
58
        if( nwritten < 0 ) {
 
59
            if( errno == EINTR )
 
60
                continue;
 
61
            return -1;
 
62
        }
 
63
        nleft -= nwritten;
 
64
        buf = (char*)buf + nwritten;
 
65
    }
 
66
    return 0;
 
67
}
 
68
 
 
69
static int
 
70
do_read( int fd, void *buf, size_t nbytes )
 
71
{
 
72
  int n, nread = 0;
 
73
  
 
74
  while (nbytes)
 
75
    {
 
76
      do {
 
77
        n = read(fd, (char*)buf + nread, nbytes );
 
78
      } while( n == -1 && errno == EINTR );
 
79
      if( n == -1 )
 
80
        return nread? nread:-1;
 
81
      else if( n == 0 ) {
 
82
        /* EGD probably died. */
 
83
        errno = ECONNRESET;
 
84
        return -1;
 
85
      }
 
86
      nread += n;
 
87
      nbytes -= n;
 
88
    } 
 
89
  return nread;
 
90
}
 
91
 
 
92
/* Connect to the EGD and return the file descriptor.  Return -1 on
 
93
   error.  With NOFAIL set to true, silently fail and return the
 
94
   error, otherwise print an error message and die. */
 
95
int
 
96
rndegd_connect_socket (int nofail)
 
97
{
 
98
  int fd;
 
99
  const char *bname = NULL;
 
100
  char *name;
 
101
  struct sockaddr_un addr;
 
102
  int addr_len;
 
103
 
 
104
  if (egd_socket != -1)
 
105
    {
 
106
      close (egd_socket);
 
107
      egd_socket = -1;
 
108
    }
 
109
 
 
110
#ifdef EGD_SOCKET_NAME
 
111
  bname = EGD_SOCKET_NAME;
 
112
#endif
 
113
  if ( !bname || !*bname )
 
114
    bname = "=entropy";
 
115
  
 
116
  if ( *bname == '=' && bname[1] )
 
117
    name = make_filename( g10_opt_homedir, bname+1 , NULL );
 
118
  else
 
119
    name = make_filename( bname , NULL );
 
120
  
 
121
  if ( strlen(name)+1 >= sizeof addr.sun_path ) 
 
122
    g10_log_fatal ("EGD socketname is too long\n");
 
123
  
 
124
  memset( &addr, 0, sizeof addr );
 
125
  addr.sun_family = AF_UNIX;
 
126
  strcpy( addr.sun_path, name );          
 
127
  addr_len = (offsetof( struct sockaddr_un, sun_path )
 
128
              + strlen( addr.sun_path ));
 
129
  
 
130
  fd = socket(AF_UNIX, SOCK_STREAM, 0);
 
131
  if (fd == -1 && !nofail)
 
132
    g10_log_fatal("can't create unix domain socket: %s\n",
 
133
                  strerror(errno) );
 
134
  else if (connect (fd, (struct sockaddr*)&addr, addr_len) == -1)
 
135
    {
 
136
      if (!nofail)
 
137
        g10_log_fatal("can't connect to `%s': %s\n",
 
138
                      name, strerror(errno) );
 
139
      close (fd);
 
140
      fd = -1;
 
141
    }
 
142
  m_free(name);
 
143
  if (fd != -1)
 
144
    egd_socket = fd;
 
145
  return fd;
 
146
}
 
147
 
 
148
 
 
149
/****************
 
150
 * Note: we always use the highest level.
 
151
 * TO boost the performance we may want to add some
 
152
 * additional code for level 1
 
153
 *
 
154
 * Using a level of 0 should never block and better add nothing
 
155
 * to the pool.  So this is just a dummy for EGD.
 
156
 */
 
157
int
 
158
rndegd_gather_random( void (*add)(const void*, size_t, int), int requester,
 
159
                                          size_t length, int level )
 
160
{
 
161
    int fd = egd_socket;
 
162
    int n;
 
163
    byte buffer[256+2];
 
164
    int nbytes;
 
165
    int do_restart = 0;
 
166
 
 
167
    if( !length )
 
168
        return 0;
 
169
    if( !level )
 
170
        return 0;
 
171
 
 
172
  restart:
 
173
    if (fd == -1 || do_restart)
 
174
      fd = rndegd_connect_socket (0);
 
175
 
 
176
    do_restart = 0;
 
177
 
 
178
    nbytes = length < 255? length : 255;
 
179
    /* first time we do it with a non blocking request */
 
180
    buffer[0] = 1; /* non blocking */
 
181
    buffer[1] = nbytes;
 
182
    if( do_write( fd, buffer, 2 ) == -1 )
 
183
        g10_log_fatal("can't write to the EGD: %s\n", strerror(errno) );
 
184
    n = do_read( fd, buffer, 1 );
 
185
    if( n == -1 ) {
 
186
        g10_log_error("read error on EGD: %s\n", strerror(errno));
 
187
        do_restart = 1;
 
188
        goto restart;
 
189
    }
 
190
    n = buffer[0];
 
191
    if( n ) {
 
192
        n = do_read( fd, buffer, n );
 
193
        if( n == -1 ) {
 
194
            g10_log_error("read error on EGD: %s\n", strerror(errno));
 
195
            do_restart = 1;
 
196
            goto restart;
 
197
        }
 
198
        (*add)( buffer, n, requester );
 
199
        length -= n;
 
200
    }
 
201
 
 
202
    if( length ) {
 
203
        tty_printf(
 
204
         _("Please wait, entropy is being gathered. Do some work if it would\n"
 
205
           "keep you from getting bored, because it will improve the quality\n"
 
206
           "of the entropy.\n") );
 
207
    }
 
208
    while( length ) {
 
209
        nbytes = length < 255? length : 255;
 
210
 
 
211
        buffer[0] = 2; /* blocking */
 
212
        buffer[1] = nbytes;
 
213
        if( do_write( fd, buffer, 2 ) == -1 )
 
214
            g10_log_fatal("can't write to the EGD: %s\n", strerror(errno) );
 
215
        n = do_read( fd, buffer, nbytes );
 
216
        if( n == -1 ) {
 
217
            g10_log_error("read error on EGD: %s\n", strerror(errno));
 
218
            do_restart = 1;
 
219
            goto restart;
 
220
        }
 
221
        (*add)( buffer, n, requester );
 
222
        length -= n;
 
223
    }
 
224
    memset(buffer, 0, sizeof(buffer) );
 
225
 
 
226
    return 0; /* success */
 
227
}
 
228
 
 
229
#endif /*USE_RNDEGD*/