michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include "secrng.h" michael@0: #include "secerr.h" michael@0: #include "prerror.h" michael@0: #include "prthread.h" michael@0: #include "prprf.h" michael@0: michael@0: size_t RNG_FileUpdate(const char *fileName, size_t limit); michael@0: michael@0: /* michael@0: * When copying data to the buffer we want the least signicant bytes michael@0: * from the input since those bits are changing the fastest. The address michael@0: * of least significant byte depends upon whether we are running on michael@0: * a big-endian or little-endian machine. michael@0: * michael@0: * Does this mean the least signicant bytes are the most significant michael@0: * to us? :-) michael@0: */ michael@0: michael@0: static size_t CopyLowBits(void *dst, size_t dstlen, void *src, size_t srclen) michael@0: { michael@0: union endianness { michael@0: PRInt32 i; michael@0: char c[4]; michael@0: } u; michael@0: michael@0: if (srclen <= dstlen) { michael@0: memcpy(dst, src, srclen); michael@0: return srclen; michael@0: } michael@0: u.i = 0x01020304; michael@0: if (u.c[0] == 0x01) { michael@0: /* big-endian case */ michael@0: memcpy(dst, (char*)src + (srclen - dstlen), dstlen); michael@0: } else { michael@0: /* little-endian case */ michael@0: memcpy(dst, src, dstlen); michael@0: } michael@0: return dstlen; michael@0: } michael@0: michael@0: #ifdef SOLARIS michael@0: michael@0: #include michael@0: michael@0: static const PRUint32 entropy_buf_len = 4096; /* buffer up to 4 KB */ michael@0: michael@0: /* Buffer entropy data, and feed it to the RNG, entropy_buf_len bytes at a time. michael@0: * Returns error if RNG_RandomUpdate fails. Also increments *total_fed michael@0: * by the number of bytes successfully buffered. michael@0: */ michael@0: static SECStatus BufferEntropy(char* inbuf, PRUint32 inlen, michael@0: char* entropy_buf, PRUint32* entropy_buffered, michael@0: PRUint32* total_fed) michael@0: { michael@0: PRUint32 tocopy = 0; michael@0: PRUint32 avail = 0; michael@0: SECStatus rv = SECSuccess; michael@0: michael@0: while (inlen) { michael@0: avail = entropy_buf_len - *entropy_buffered; michael@0: if (!avail) { michael@0: /* Buffer is full, time to feed it to the RNG. */ michael@0: rv = RNG_RandomUpdate(entropy_buf, entropy_buf_len); michael@0: if (SECSuccess != rv) { michael@0: break; michael@0: } michael@0: *entropy_buffered = 0; michael@0: avail = entropy_buf_len; michael@0: } michael@0: tocopy = PR_MIN(avail, inlen); michael@0: memcpy(entropy_buf + *entropy_buffered, inbuf, tocopy); michael@0: *entropy_buffered += tocopy; michael@0: inlen -= tocopy; michael@0: inbuf += tocopy; michael@0: *total_fed += tocopy; michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: /* Feed kernel statistics structures and ks_data field to the RNG. michael@0: * Returns status as well as the number of bytes successfully fed to the RNG. michael@0: */ michael@0: static SECStatus RNG_kstat(PRUint32* fed) michael@0: { michael@0: kstat_ctl_t* kc = NULL; michael@0: kstat_t* ksp = NULL; michael@0: PRUint32 entropy_buffered = 0; michael@0: char* entropy_buf = NULL; michael@0: SECStatus rv = SECSuccess; michael@0: michael@0: PORT_Assert(fed); michael@0: if (!fed) { michael@0: return SECFailure; michael@0: } michael@0: *fed = 0; michael@0: michael@0: kc = kstat_open(); michael@0: PORT_Assert(kc); michael@0: if (!kc) { michael@0: return SECFailure; michael@0: } michael@0: entropy_buf = (char*) PORT_Alloc(entropy_buf_len); michael@0: PORT_Assert(entropy_buf); michael@0: if (entropy_buf) { michael@0: for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) { michael@0: if (-1 == kstat_read(kc, ksp, NULL)) { michael@0: /* missing data from a single kstat shouldn't be fatal */ michael@0: continue; michael@0: } michael@0: rv = BufferEntropy((char*)ksp, sizeof(kstat_t), michael@0: entropy_buf, &entropy_buffered, michael@0: fed); michael@0: if (SECSuccess != rv) { michael@0: break; michael@0: } michael@0: michael@0: if (ksp->ks_data && ksp->ks_data_size>0 && ksp->ks_ndata>0) { michael@0: rv = BufferEntropy((char*)ksp->ks_data, ksp->ks_data_size, michael@0: entropy_buf, &entropy_buffered, michael@0: fed); michael@0: if (SECSuccess != rv) { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: if (SECSuccess == rv && entropy_buffered) { michael@0: /* Buffer is not empty, time to feed it to the RNG */ michael@0: rv = RNG_RandomUpdate(entropy_buf, entropy_buffered); michael@0: } michael@0: PORT_Free(entropy_buf); michael@0: } else { michael@0: rv = SECFailure; michael@0: } michael@0: if (kstat_close(kc)) { michael@0: PORT_Assert(0); michael@0: rv = SECFailure; michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: #endif michael@0: michael@0: #if defined(SCO) || defined(UNIXWARE) || defined(BSDI) || defined(FREEBSD) \ michael@0: || defined(NETBSD) || defined(DARWIN) || defined(OPENBSD) \ michael@0: || defined(NTO) || defined(__riscos__) michael@0: #include michael@0: michael@0: #define getdtablesize() sysconf(_SC_OPEN_MAX) michael@0: michael@0: static size_t michael@0: GetHighResClock(void *buf, size_t maxbytes) michael@0: { michael@0: int ticks; michael@0: struct tms buffer; michael@0: michael@0: ticks=times(&buffer); michael@0: return CopyLowBits(buf, maxbytes, &ticks, sizeof(ticks)); michael@0: } michael@0: michael@0: static void michael@0: GiveSystemInfo(void) michael@0: { michael@0: long si; michael@0: michael@0: /* michael@0: * Is this really necessary? Why not use rand48 or something? michael@0: */ michael@0: si = sysconf(_SC_CHILD_MAX); michael@0: RNG_RandomUpdate(&si, sizeof(si)); michael@0: michael@0: si = sysconf(_SC_STREAM_MAX); michael@0: RNG_RandomUpdate(&si, sizeof(si)); michael@0: michael@0: si = sysconf(_SC_OPEN_MAX); michael@0: RNG_RandomUpdate(&si, sizeof(si)); michael@0: } michael@0: #endif michael@0: michael@0: #if defined(__sun) michael@0: #if defined(__svr4) || defined(SVR4) michael@0: #include michael@0: michael@0: #define getdtablesize() sysconf(_SC_OPEN_MAX) michael@0: michael@0: static void michael@0: GiveSystemInfo(void) michael@0: { michael@0: int rv; michael@0: char buf[2000]; michael@0: michael@0: rv = sysinfo(SI_MACHINE, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: rv = sysinfo(SI_RELEASE, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: rv = sysinfo(SI_HW_SERIAL, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: } michael@0: michael@0: static size_t michael@0: GetHighResClock(void *buf, size_t maxbytes) michael@0: { michael@0: hrtime_t t; michael@0: t = gethrtime(); michael@0: if (t) { michael@0: return CopyLowBits(buf, maxbytes, &t, sizeof(t)); michael@0: } michael@0: return 0; michael@0: } michael@0: #else /* SunOS (Sun, but not SVR4) */ michael@0: michael@0: extern long sysconf(int name); michael@0: michael@0: static size_t michael@0: GetHighResClock(void *buf, size_t maxbytes) michael@0: { michael@0: return 0; michael@0: } michael@0: michael@0: static void michael@0: GiveSystemInfo(void) michael@0: { michael@0: long si; michael@0: michael@0: /* This is not very good */ michael@0: si = sysconf(_SC_CHILD_MAX); michael@0: RNG_RandomUpdate(&si, sizeof(si)); michael@0: } michael@0: #endif michael@0: #endif /* Sun */ michael@0: michael@0: #if defined(__hpux) michael@0: #include michael@0: michael@0: #define getdtablesize() sysconf(_SC_OPEN_MAX) michael@0: michael@0: #if defined(__ia64) michael@0: #include michael@0: michael@0: static size_t michael@0: GetHighResClock(void *buf, size_t maxbytes) michael@0: { michael@0: PRUint64 t; michael@0: michael@0: t = _Asm_mov_from_ar(_AREG44); michael@0: return CopyLowBits(buf, maxbytes, &t, sizeof(t)); michael@0: } michael@0: #else michael@0: static size_t michael@0: GetHighResClock(void *buf, size_t maxbytes) michael@0: { michael@0: extern int ret_cr16(); michael@0: int cr16val; michael@0: michael@0: cr16val = ret_cr16(); michael@0: return CopyLowBits(buf, maxbytes, &cr16val, sizeof(cr16val)); michael@0: } michael@0: #endif michael@0: michael@0: static void michael@0: GiveSystemInfo(void) michael@0: { michael@0: long si; michael@0: michael@0: /* This is not very good */ michael@0: si = sysconf(_AES_OS_VERSION); michael@0: RNG_RandomUpdate(&si, sizeof(si)); michael@0: si = sysconf(_SC_CPU_VERSION); michael@0: RNG_RandomUpdate(&si, sizeof(si)); michael@0: } michael@0: #endif /* HPUX */ michael@0: michael@0: #if defined(OSF1) michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: static void michael@0: GiveSystemInfo(void) michael@0: { michael@0: char buf[BUFSIZ]; michael@0: int rv; michael@0: int off = 0; michael@0: michael@0: rv = sysinfo(SI_MACHINE, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: rv = sysinfo(SI_RELEASE, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: rv = sysinfo(SI_HW_SERIAL, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * Use the "get the cycle counter" instruction on the alpha. michael@0: * The low 32 bits completely turn over in less than a minute. michael@0: * The high 32 bits are some non-counter gunk that changes sometimes. michael@0: */ michael@0: static size_t michael@0: GetHighResClock(void *buf, size_t maxbytes) michael@0: { michael@0: unsigned long t; michael@0: michael@0: t = asm("rpcc %v0"); michael@0: return CopyLowBits(buf, maxbytes, &t, sizeof(t)); michael@0: } michael@0: michael@0: #endif /* Alpha */ michael@0: michael@0: #if defined(_IBMR2) michael@0: static size_t michael@0: GetHighResClock(void *buf, size_t maxbytes) michael@0: { michael@0: return 0; michael@0: } michael@0: michael@0: static void michael@0: GiveSystemInfo(void) michael@0: { michael@0: /* XXX haven't found any yet! */ michael@0: } michael@0: #endif /* IBM R2 */ michael@0: michael@0: #if defined(LINUX) michael@0: #include michael@0: michael@0: static size_t michael@0: GetHighResClock(void *buf, size_t maxbytes) michael@0: { michael@0: return 0; michael@0: } michael@0: michael@0: static void michael@0: GiveSystemInfo(void) michael@0: { michael@0: #ifndef NO_SYSINFO michael@0: struct sysinfo si; michael@0: if (sysinfo(&si) == 0) { michael@0: RNG_RandomUpdate(&si, sizeof(si)); michael@0: } michael@0: #endif michael@0: } michael@0: #endif /* LINUX */ michael@0: michael@0: #if defined(NCR) michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #define getdtablesize() sysconf(_SC_OPEN_MAX) michael@0: michael@0: static size_t michael@0: GetHighResClock(void *buf, size_t maxbytes) michael@0: { michael@0: return 0; michael@0: } michael@0: michael@0: static void michael@0: GiveSystemInfo(void) michael@0: { michael@0: int rv; michael@0: char buf[2000]; michael@0: michael@0: rv = sysinfo(SI_MACHINE, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: rv = sysinfo(SI_RELEASE, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: rv = sysinfo(SI_HW_SERIAL, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: } michael@0: michael@0: #endif /* NCR */ michael@0: michael@0: #if defined(sgi) michael@0: #include michael@0: #undef PRIVATE michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: static void michael@0: GiveSystemInfo(void) michael@0: { michael@0: int rv; michael@0: char buf[4096]; michael@0: michael@0: rv = syssgi(SGI_SYSID, &buf[0]); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, MAXSYSIDSIZE); michael@0: } michael@0: #ifdef SGI_RDUBLK michael@0: rv = syssgi(SGI_RDUBLK, getpid(), &buf[0], sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, sizeof(buf)); michael@0: } michael@0: #endif /* SGI_RDUBLK */ michael@0: rv = syssgi(SGI_INVENT, SGI_INV_READ, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, sizeof(buf)); michael@0: } michael@0: rv = sysinfo(SI_MACHINE, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: rv = sysinfo(SI_RELEASE, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: rv = sysinfo(SI_HW_SERIAL, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: } michael@0: michael@0: static size_t GetHighResClock(void *buf, size_t maxbuf) michael@0: { michael@0: unsigned phys_addr, raddr, cycleval; michael@0: static volatile unsigned *iotimer_addr = NULL; michael@0: static int tries = 0; michael@0: static int cntr_size; michael@0: int mfd; michael@0: long s0[2]; michael@0: struct timeval tv; michael@0: michael@0: #ifndef SGI_CYCLECNTR_SIZE michael@0: #define SGI_CYCLECNTR_SIZE 165 /* Size user needs to use to read CC */ michael@0: #endif michael@0: michael@0: if (iotimer_addr == NULL) { michael@0: if (tries++ > 1) { michael@0: /* Don't keep trying if it didn't work */ michael@0: return 0; michael@0: } michael@0: michael@0: /* michael@0: ** For SGI machines we can use the cycle counter, if it has one, michael@0: ** to generate some truly random numbers michael@0: */ michael@0: phys_addr = syssgi(SGI_QUERY_CYCLECNTR, &cycleval); michael@0: if (phys_addr) { michael@0: int pgsz = getpagesize(); michael@0: int pgoffmask = pgsz - 1; michael@0: michael@0: raddr = phys_addr & ~pgoffmask; michael@0: mfd = open("/dev/mmem", O_RDONLY); michael@0: if (mfd < 0) { michael@0: return 0; michael@0: } michael@0: iotimer_addr = (unsigned *) michael@0: mmap(0, pgoffmask, PROT_READ, MAP_PRIVATE, mfd, (int)raddr); michael@0: if (iotimer_addr == (void*)-1) { michael@0: close(mfd); michael@0: iotimer_addr = NULL; michael@0: return 0; michael@0: } michael@0: iotimer_addr = (unsigned*) michael@0: ((__psint_t)iotimer_addr | (phys_addr & pgoffmask)); michael@0: /* michael@0: * The file 'mfd' is purposefully not closed. michael@0: */ michael@0: cntr_size = syssgi(SGI_CYCLECNTR_SIZE); michael@0: if (cntr_size < 0) { michael@0: struct utsname utsinfo; michael@0: michael@0: /* michael@0: * We must be executing on a 6.0 or earlier system, since the michael@0: * SGI_CYCLECNTR_SIZE call is not supported. michael@0: * michael@0: * The only pre-6.1 platforms with 64-bit counters are michael@0: * IP19 and IP21 (Challenge, PowerChallenge, Onyx). michael@0: */ michael@0: uname(&utsinfo); michael@0: if (!strncmp(utsinfo.machine, "IP19", 4) || michael@0: !strncmp(utsinfo.machine, "IP21", 4)) michael@0: cntr_size = 64; michael@0: else michael@0: cntr_size = 32; michael@0: } michael@0: cntr_size /= 8; /* Convert from bits to bytes */ michael@0: } michael@0: } michael@0: michael@0: s0[0] = *iotimer_addr; michael@0: if (cntr_size > 4) michael@0: s0[1] = *(iotimer_addr + 1); michael@0: memcpy(buf, (char *)&s0[0], cntr_size); michael@0: return CopyLowBits(buf, maxbuf, &s0, cntr_size); michael@0: } michael@0: #endif michael@0: michael@0: #if defined(sony) michael@0: #include michael@0: michael@0: #define getdtablesize() sysconf(_SC_OPEN_MAX) michael@0: michael@0: static size_t michael@0: GetHighResClock(void *buf, size_t maxbytes) michael@0: { michael@0: return 0; michael@0: } michael@0: michael@0: static void michael@0: GiveSystemInfo(void) michael@0: { michael@0: int rv; michael@0: char buf[2000]; michael@0: michael@0: rv = sysinfo(SI_MACHINE, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: rv = sysinfo(SI_RELEASE, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: rv = sysinfo(SI_HW_SERIAL, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: } michael@0: #endif /* sony */ michael@0: michael@0: #if defined(sinix) michael@0: #include michael@0: #include michael@0: michael@0: int gettimeofday(struct timeval *, struct timezone *); michael@0: int gethostname(char *, int); michael@0: michael@0: #define getdtablesize() sysconf(_SC_OPEN_MAX) michael@0: michael@0: static size_t michael@0: GetHighResClock(void *buf, size_t maxbytes) michael@0: { michael@0: int ticks; michael@0: struct tms buffer; michael@0: michael@0: ticks=times(&buffer); michael@0: return CopyLowBits(buf, maxbytes, &ticks, sizeof(ticks)); michael@0: } michael@0: michael@0: static void michael@0: GiveSystemInfo(void) michael@0: { michael@0: int rv; michael@0: char buf[2000]; michael@0: michael@0: rv = sysinfo(SI_MACHINE, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: rv = sysinfo(SI_RELEASE, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: rv = sysinfo(SI_HW_SERIAL, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: } michael@0: #endif /* sinix */ michael@0: michael@0: michael@0: #ifdef BEOS michael@0: #include michael@0: michael@0: static size_t michael@0: GetHighResClock(void *buf, size_t maxbytes) michael@0: { michael@0: bigtime_t bigtime; /* Actually a int64 */ michael@0: michael@0: bigtime = real_time_clock_usecs(); michael@0: return CopyLowBits(buf, maxbytes, &bigtime, sizeof(bigtime)); michael@0: } michael@0: michael@0: static void michael@0: GiveSystemInfo(void) michael@0: { michael@0: system_info *info = NULL; michael@0: PRInt32 val; michael@0: get_system_info(info); michael@0: if (info) { michael@0: val = info->boot_time; michael@0: RNG_RandomUpdate(&val, sizeof(val)); michael@0: val = info->used_pages; michael@0: RNG_RandomUpdate(&val, sizeof(val)); michael@0: val = info->used_ports; michael@0: RNG_RandomUpdate(&val, sizeof(val)); michael@0: val = info->used_threads; michael@0: RNG_RandomUpdate(&val, sizeof(val)); michael@0: val = info->used_teams; michael@0: RNG_RandomUpdate(&val, sizeof(val)); michael@0: } michael@0: } michael@0: #endif /* BEOS */ michael@0: michael@0: #if defined(nec_ews) michael@0: #include michael@0: michael@0: #define getdtablesize() sysconf(_SC_OPEN_MAX) michael@0: michael@0: static size_t michael@0: GetHighResClock(void *buf, size_t maxbytes) michael@0: { michael@0: return 0; michael@0: } michael@0: michael@0: static void michael@0: GiveSystemInfo(void) michael@0: { michael@0: int rv; michael@0: char buf[2000]; michael@0: michael@0: rv = sysinfo(SI_MACHINE, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: rv = sysinfo(SI_RELEASE, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: rv = sysinfo(SI_HW_SERIAL, buf, sizeof(buf)); michael@0: if (rv > 0) { michael@0: RNG_RandomUpdate(buf, rv); michael@0: } michael@0: } michael@0: #endif /* nec_ews */ michael@0: michael@0: size_t RNG_GetNoise(void *buf, size_t maxbytes) michael@0: { michael@0: struct timeval tv; michael@0: int n = 0; michael@0: int c; michael@0: michael@0: n = GetHighResClock(buf, maxbytes); michael@0: maxbytes -= n; michael@0: michael@0: (void)gettimeofday(&tv, 0); michael@0: c = CopyLowBits((char*)buf+n, maxbytes, &tv.tv_usec, sizeof(tv.tv_usec)); michael@0: n += c; michael@0: maxbytes -= c; michael@0: c = CopyLowBits((char*)buf+n, maxbytes, &tv.tv_sec, sizeof(tv.tv_sec)); michael@0: n += c; michael@0: return n; michael@0: } michael@0: michael@0: #define SAFE_POPEN_MAXARGS 10 /* must be at least 2 */ michael@0: michael@0: /* michael@0: * safe_popen is static to this module and we know what arguments it is michael@0: * called with. Note that this version only supports a single open child michael@0: * process at any time. michael@0: */ michael@0: static pid_t safe_popen_pid; michael@0: static struct sigaction oldact; michael@0: michael@0: static FILE * michael@0: safe_popen(char *cmd) michael@0: { michael@0: int p[2], fd, argc; michael@0: pid_t pid; michael@0: char *argv[SAFE_POPEN_MAXARGS + 1]; michael@0: FILE *fp; michael@0: static char blank[] = " \t"; michael@0: static struct sigaction newact; michael@0: michael@0: if (pipe(p) < 0) michael@0: return 0; michael@0: michael@0: fp = fdopen(p[0], "r"); michael@0: if (fp == 0) { michael@0: close(p[0]); michael@0: close(p[1]); michael@0: return 0; michael@0: } michael@0: michael@0: /* Setup signals so that SIGCHLD is ignored as we want to do waitpid */ michael@0: newact.sa_handler = SIG_DFL; michael@0: newact.sa_flags = 0; michael@0: sigfillset(&newact.sa_mask); michael@0: sigaction (SIGCHLD, &newact, &oldact); michael@0: michael@0: pid = fork(); michael@0: switch (pid) { michael@0: int ndesc; michael@0: michael@0: case -1: michael@0: fclose(fp); /* this closes p[0], the fd associated with fp */ michael@0: close(p[1]); michael@0: sigaction (SIGCHLD, &oldact, NULL); michael@0: return 0; michael@0: michael@0: case 0: michael@0: /* dup write-side of pipe to stderr and stdout */ michael@0: if (p[1] != 1) dup2(p[1], 1); michael@0: if (p[1] != 2) dup2(p[1], 2); michael@0: michael@0: /* michael@0: * close the other file descriptors, except stdin which we michael@0: * try reassociating with /dev/null, first (bug 174993) michael@0: */ michael@0: if (!freopen("/dev/null", "r", stdin)) michael@0: close(0); michael@0: ndesc = getdtablesize(); michael@0: for (fd = PR_MIN(65536, ndesc); --fd > 2; close(fd)); michael@0: michael@0: /* clean up environment in the child process */ michael@0: putenv("PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc:/usr/etc"); michael@0: putenv("SHELL=/bin/sh"); michael@0: putenv("IFS= \t"); michael@0: michael@0: /* michael@0: * The caller may have passed us a string that is in text michael@0: * space. It may be illegal to modify the string michael@0: */ michael@0: cmd = strdup(cmd); michael@0: /* format argv */ michael@0: argv[0] = strtok(cmd, blank); michael@0: argc = 1; michael@0: while ((argv[argc] = strtok(0, blank)) != 0) { michael@0: if (++argc == SAFE_POPEN_MAXARGS) { michael@0: argv[argc] = 0; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: /* and away we go */ michael@0: execvp(argv[0], argv); michael@0: exit(127); michael@0: break; michael@0: michael@0: default: michael@0: close(p[1]); michael@0: break; michael@0: } michael@0: michael@0: /* non-zero means there's a cmd running */ michael@0: safe_popen_pid = pid; michael@0: return fp; michael@0: } michael@0: michael@0: static int michael@0: safe_pclose(FILE *fp) michael@0: { michael@0: pid_t pid; michael@0: int status = -1, rv; michael@0: michael@0: if ((pid = safe_popen_pid) == 0) michael@0: return -1; michael@0: safe_popen_pid = 0; michael@0: michael@0: fclose(fp); michael@0: michael@0: /* yield the processor so the child gets some time to exit normally */ michael@0: PR_Sleep(PR_INTERVAL_NO_WAIT); michael@0: michael@0: /* if the child hasn't exited, kill it -- we're done with its output */ michael@0: while ((rv = waitpid(pid, &status, WNOHANG)) == -1 && errno == EINTR) michael@0: ; michael@0: if (rv == 0) { michael@0: kill(pid, SIGKILL); michael@0: while ((rv = waitpid(pid, &status, 0)) == -1 && errno == EINTR) michael@0: ; michael@0: } michael@0: michael@0: /* Reset SIGCHLD signal hander before returning */ michael@0: sigaction(SIGCHLD, &oldact, NULL); michael@0: michael@0: return status; michael@0: } michael@0: michael@0: #ifdef DARWIN michael@0: #include michael@0: #if !TARGET_OS_IPHONE michael@0: #include michael@0: #endif michael@0: #endif michael@0: michael@0: /* Fork netstat to collect its output by default. Do not unset this unless michael@0: * another source of entropy is available michael@0: */ michael@0: #define DO_NETSTAT 1 michael@0: michael@0: void RNG_SystemInfoForRNG(void) michael@0: { michael@0: FILE *fp; michael@0: char buf[BUFSIZ]; michael@0: size_t bytes; michael@0: const char * const *cp; michael@0: char *randfile; michael@0: #ifdef DARWIN michael@0: #if TARGET_OS_IPHONE michael@0: /* iOS does not expose a way to access environ. */ michael@0: char **environ = NULL; michael@0: #else michael@0: char **environ = *_NSGetEnviron(); michael@0: #endif michael@0: #else michael@0: extern char **environ; michael@0: #endif michael@0: #ifdef BEOS michael@0: static const char * const files[] = { michael@0: "/boot/var/swap", michael@0: "/boot/var/log/syslog", michael@0: "/boot/var/tmp", michael@0: "/boot/home/config/settings", michael@0: "/boot/home", michael@0: 0 michael@0: }; michael@0: #else michael@0: static const char * const files[] = { michael@0: "/etc/passwd", michael@0: "/etc/utmp", michael@0: "/tmp", michael@0: "/var/tmp", michael@0: "/usr/tmp", michael@0: 0 michael@0: }; michael@0: #endif michael@0: michael@0: #if defined(BSDI) michael@0: static char netstat_ni_cmd[] = "netstat -nis"; michael@0: #else michael@0: static char netstat_ni_cmd[] = "netstat -ni"; michael@0: #endif michael@0: michael@0: GiveSystemInfo(); michael@0: michael@0: bytes = RNG_GetNoise(buf, sizeof(buf)); michael@0: RNG_RandomUpdate(buf, bytes); michael@0: michael@0: /* michael@0: * Pass the C environment and the addresses of the pointers to the michael@0: * hash function. This makes the random number function depend on the michael@0: * execution environment of the user and on the platform the program michael@0: * is running on. michael@0: */ michael@0: if (environ != NULL) { michael@0: cp = (const char * const *) environ; michael@0: while (*cp) { michael@0: RNG_RandomUpdate(*cp, strlen(*cp)); michael@0: cp++; michael@0: } michael@0: RNG_RandomUpdate(environ, (char*)cp - (char*)environ); michael@0: } michael@0: michael@0: /* Give in system information */ michael@0: if (gethostname(buf, sizeof(buf)) == 0) { michael@0: RNG_RandomUpdate(buf, strlen(buf)); michael@0: } michael@0: GiveSystemInfo(); michael@0: michael@0: /* grab some data from system's PRNG before any other files. */ michael@0: bytes = RNG_FileUpdate("/dev/urandom", SYSTEM_RNG_SEED_COUNT); michael@0: michael@0: /* If the user points us to a random file, pass it through the rng */ michael@0: randfile = getenv("NSRANDFILE"); michael@0: if ( ( randfile != NULL ) && ( randfile[0] != '\0') ) { michael@0: char *randCountString = getenv("NSRANDCOUNT"); michael@0: int randCount = randCountString ? atoi(randCountString) : 0; michael@0: if (randCount != 0) { michael@0: RNG_FileUpdate(randfile, randCount); michael@0: } else { michael@0: RNG_FileForRNG(randfile); michael@0: } michael@0: } michael@0: michael@0: /* pass other files through */ michael@0: for (cp = files; *cp; cp++) michael@0: RNG_FileForRNG(*cp); michael@0: michael@0: /* michael@0: * Bug 100447: On BSD/OS 4.2 and 4.3, we have problem calling safe_popen michael@0: * in a pthreads environment. Therefore, we call safe_popen last and on michael@0: * BSD/OS we do not call safe_popen when we succeeded in getting data michael@0: * from /dev/urandom. michael@0: * michael@0: * Bug 174993: On platforms providing /dev/urandom, don't fork netstat michael@0: * either, if data has been gathered successfully. michael@0: */ michael@0: michael@0: #if defined(BSDI) || defined(FREEBSD) || defined(NETBSD) \ michael@0: || defined(OPENBSD) || defined(DARWIN) || defined(LINUX) \ michael@0: || defined(HPUX) michael@0: if (bytes) michael@0: return; michael@0: #endif michael@0: michael@0: #ifdef SOLARIS michael@0: michael@0: /* michael@0: * On Solaris, NSS may be initialized automatically from libldap in michael@0: * applications that are unaware of the use of NSS. safe_popen forks, and michael@0: * sometimes creates issues with some applications' pthread_atfork handlers. michael@0: * We always have /dev/urandom on Solaris 9 and above as an entropy source, michael@0: * and for Solaris 8 we have the libkstat interface, so we don't need to michael@0: * fork netstat. michael@0: */ michael@0: michael@0: #undef DO_NETSTAT michael@0: if (!bytes) { michael@0: /* On Solaris 8, /dev/urandom isn't available, so we use libkstat. */ michael@0: PRUint32 kstat_bytes = 0; michael@0: if (SECSuccess != RNG_kstat(&kstat_bytes)) { michael@0: PORT_Assert(0); michael@0: } michael@0: bytes += kstat_bytes; michael@0: PORT_Assert(bytes); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef DO_NETSTAT michael@0: fp = safe_popen(netstat_ni_cmd); michael@0: if (fp != NULL) { michael@0: while ((bytes = fread(buf, 1, sizeof(buf), fp)) > 0) michael@0: RNG_RandomUpdate(buf, bytes); michael@0: safe_pclose(fp); michael@0: } michael@0: #endif michael@0: michael@0: } michael@0: michael@0: #define TOTAL_FILE_LIMIT 1000000 /* one million */ michael@0: michael@0: size_t RNG_FileUpdate(const char *fileName, size_t limit) michael@0: { michael@0: FILE * file; michael@0: int fd; michael@0: int bytes; michael@0: size_t fileBytes = 0; michael@0: struct stat stat_buf; michael@0: unsigned char buffer[BUFSIZ]; michael@0: static size_t totalFileBytes = 0; michael@0: michael@0: /* suppress valgrind warnings due to holes in struct stat */ michael@0: memset(&stat_buf, 0, sizeof(stat_buf)); michael@0: michael@0: if (stat((char *)fileName, &stat_buf) < 0) michael@0: return fileBytes; michael@0: RNG_RandomUpdate(&stat_buf, sizeof(stat_buf)); michael@0: michael@0: file = fopen(fileName, "r"); michael@0: if (file != NULL) { michael@0: /* Read from the underlying file descriptor directly to bypass stdio michael@0: * buffering and avoid reading more bytes than we need from michael@0: * /dev/urandom. NOTE: we can't use fread with unbuffered I/O because michael@0: * fread may return EOF in unbuffered I/O mode on Android. michael@0: * michael@0: * Moreover, we read into a buffer of size BUFSIZ, so buffered I/O michael@0: * has no performance advantage. */ michael@0: fd = fileno(file); michael@0: /* 'file' was just opened, so this should not fail. */ michael@0: PORT_Assert(fd != -1); michael@0: while (limit > fileBytes) { michael@0: bytes = PR_MIN(sizeof buffer, limit - fileBytes); michael@0: bytes = read(fd, buffer, bytes); michael@0: if (bytes <= 0) michael@0: break; michael@0: RNG_RandomUpdate(buffer, bytes); michael@0: fileBytes += bytes; michael@0: totalFileBytes += bytes; michael@0: /* after TOTAL_FILE_LIMIT has been reached, only read in first michael@0: ** buffer of data from each subsequent file. michael@0: */ michael@0: if (totalFileBytes > TOTAL_FILE_LIMIT) michael@0: break; michael@0: } michael@0: fclose(file); michael@0: } michael@0: /* michael@0: * Pass yet another snapshot of our highest resolution clock into michael@0: * the hash function. michael@0: */ michael@0: bytes = RNG_GetNoise(buffer, sizeof(buffer)); michael@0: RNG_RandomUpdate(buffer, bytes); michael@0: return fileBytes; michael@0: } michael@0: michael@0: void RNG_FileForRNG(const char *fileName) michael@0: { michael@0: RNG_FileUpdate(fileName, TOTAL_FILE_LIMIT); michael@0: } michael@0: michael@0: void ReadSingleFile(const char *fileName) michael@0: { michael@0: FILE * file; michael@0: unsigned char buffer[BUFSIZ]; michael@0: michael@0: file = fopen(fileName, "rb"); michael@0: if (file != NULL) { michael@0: while (fread(buffer, 1, sizeof(buffer), file) > 0) michael@0: ; michael@0: fclose(file); michael@0: } michael@0: } michael@0: michael@0: #define _POSIX_PTHREAD_SEMANTICS michael@0: #include michael@0: michael@0: PRBool michael@0: ReadFileOK(char *dir, char *file) michael@0: { michael@0: struct stat stat_buf; michael@0: char filename[PATH_MAX]; michael@0: int count = snprintf(filename, sizeof filename, "%s/%s",dir, file); michael@0: michael@0: if (count <= 0) { michael@0: return PR_FALSE; /* name too long, can't read it anyway */ michael@0: } michael@0: michael@0: if (stat(filename, &stat_buf) < 0) michael@0: return PR_FALSE; /* can't stat, probably can't read it then as well */ michael@0: return S_ISREG(stat_buf.st_mode) ? PR_TRUE : PR_FALSE; michael@0: } michael@0: michael@0: /* michael@0: * read one file out of either /etc or the user's home directory. michael@0: * fileToRead tells which file to read. michael@0: * michael@0: * return 1 if it's time to reset the fileToRead (no more files to read). michael@0: */ michael@0: int ReadOneFile(int fileToRead) michael@0: { michael@0: char *dir = "/etc"; michael@0: DIR *fd = opendir(dir); michael@0: int resetCount = 0; michael@0: #ifdef SOLARIS michael@0: /* grumble, Solaris does not define struct dirent to be the full length */ michael@0: typedef union { michael@0: unsigned char space[sizeof(struct dirent) + MAXNAMELEN]; michael@0: struct dirent dir; michael@0: } dirent_hack; michael@0: dirent_hack entry, firstEntry; michael@0: michael@0: #define entry_dir entry.dir michael@0: #else michael@0: struct dirent entry, firstEntry; michael@0: #define entry_dir entry michael@0: #endif michael@0: michael@0: int i, error = -1; michael@0: michael@0: if (fd == NULL) { michael@0: dir = getenv("HOME"); michael@0: if (dir) { michael@0: fd = opendir(dir); michael@0: } michael@0: } michael@0: if (fd == NULL) { michael@0: return 1; michael@0: } michael@0: michael@0: for (i=0; i <= fileToRead; i++) { michael@0: struct dirent *result = NULL; michael@0: do { michael@0: error = readdir_r(fd, &entry_dir, &result); michael@0: } while (error == 0 && result != NULL && michael@0: !ReadFileOK(dir,&result->d_name[0])); michael@0: if (error != 0 || result == NULL) { michael@0: resetCount = 1; /* read to the end, start again at the beginning */ michael@0: if (i != 0) { michael@0: /* ran out of entries in the directory, use the first one */ michael@0: entry = firstEntry; michael@0: error = 0; michael@0: break; michael@0: } michael@0: /* if i== 0, there were no readable entries in the directory */ michael@0: break; michael@0: } michael@0: if (i==0) { michael@0: /* save the first entry in case we run out of entries */ michael@0: firstEntry = entry; michael@0: } michael@0: } michael@0: michael@0: if (error == 0) { michael@0: char filename[PATH_MAX]; michael@0: int count = snprintf(filename, sizeof filename, michael@0: "%s/%s",dir, &entry_dir.d_name[0]); michael@0: if (count >= 1) { michael@0: ReadSingleFile(filename); michael@0: } michael@0: } michael@0: michael@0: closedir(fd); michael@0: return resetCount; michael@0: } michael@0: michael@0: /* michael@0: * do something to try to introduce more noise into the 'GetNoise' call michael@0: */ michael@0: static void rng_systemJitter(void) michael@0: { michael@0: static int fileToRead = 1; michael@0: michael@0: if (ReadOneFile(fileToRead)) { michael@0: fileToRead = 1; michael@0: } else { michael@0: fileToRead++; michael@0: } michael@0: } michael@0: michael@0: size_t RNG_SystemRNG(void *dest, size_t maxLen) michael@0: { michael@0: FILE *file; michael@0: int fd; michael@0: int bytes; michael@0: size_t fileBytes = 0; michael@0: unsigned char *buffer = dest; michael@0: michael@0: file = fopen("/dev/urandom", "r"); michael@0: if (file == NULL) { michael@0: return rng_systemFromNoise(dest, maxLen); michael@0: } michael@0: /* Read from the underlying file descriptor directly to bypass stdio michael@0: * buffering and avoid reading more bytes than we need from /dev/urandom. michael@0: * NOTE: we can't use fread with unbuffered I/O because fread may return michael@0: * EOF in unbuffered I/O mode on Android. michael@0: */ michael@0: fd = fileno(file); michael@0: /* 'file' was just opened, so this should not fail. */ michael@0: PORT_Assert(fd != -1); michael@0: while (maxLen > fileBytes) { michael@0: bytes = maxLen - fileBytes; michael@0: bytes = read(fd, buffer, bytes); michael@0: if (bytes <= 0) michael@0: break; michael@0: fileBytes += bytes; michael@0: buffer += bytes; michael@0: } michael@0: fclose(file); michael@0: if (fileBytes != maxLen) { michael@0: PORT_SetError(SEC_ERROR_NEED_RANDOM); /* system RNG failed */ michael@0: fileBytes = 0; michael@0: } michael@0: return fileBytes; michael@0: }