1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * Copyright (c) 2001 Damien Miller. All rights reserved.
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
5*7c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
6*7c478bd9Sstevel@tonic-gate * are met:
7*7c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
8*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
9*7c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
10*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
11*7c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
12*7c478bd9Sstevel@tonic-gate *
13*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14*7c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15*7c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16*7c478bd9Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17*7c478bd9Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18*7c478bd9Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19*7c478bd9Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20*7c478bd9Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21*7c478bd9Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*7c478bd9Sstevel@tonic-gate */
24*7c478bd9Sstevel@tonic-gate
25*7c478bd9Sstevel@tonic-gate #include "includes.h"
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate #include <openssl/rand.h>
28*7c478bd9Sstevel@tonic-gate #include <openssl/crypto.h>
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gate #include "ssh.h"
31*7c478bd9Sstevel@tonic-gate #include "misc.h"
32*7c478bd9Sstevel@tonic-gate #include "xmalloc.h"
33*7c478bd9Sstevel@tonic-gate #include "atomicio.h"
34*7c478bd9Sstevel@tonic-gate #include "pathnames.h"
35*7c478bd9Sstevel@tonic-gate #include "log.h"
36*7c478bd9Sstevel@tonic-gate
37*7c478bd9Sstevel@tonic-gate /*
38*7c478bd9Sstevel@tonic-gate * Portable OpenSSH PRNG seeding:
39*7c478bd9Sstevel@tonic-gate * If OpenSSL has not "internally seeded" itself (e.g. pulled data from
40*7c478bd9Sstevel@tonic-gate * /dev/random), then we execute a "ssh-rand-helper" program which
41*7c478bd9Sstevel@tonic-gate * collects entropy and writes it to stdout. The child program must
42*7c478bd9Sstevel@tonic-gate * write at least RANDOM_SEED_SIZE bytes. The child is run with stderr
43*7c478bd9Sstevel@tonic-gate * attached, so error/debugging output should be visible.
44*7c478bd9Sstevel@tonic-gate *
45*7c478bd9Sstevel@tonic-gate * XXX: we should tell the child how many bytes we need.
46*7c478bd9Sstevel@tonic-gate */
47*7c478bd9Sstevel@tonic-gate
48*7c478bd9Sstevel@tonic-gate RCSID("$Id: entropy.c,v 1.44 2002/06/09 19:41:48 mouring Exp $");
49*7c478bd9Sstevel@tonic-gate
50*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate #ifndef OPENSSL_PRNG_ONLY
53*7c478bd9Sstevel@tonic-gate #define RANDOM_SEED_SIZE 48
54*7c478bd9Sstevel@tonic-gate static uid_t original_uid, original_euid;
55*7c478bd9Sstevel@tonic-gate #endif
56*7c478bd9Sstevel@tonic-gate
57*7c478bd9Sstevel@tonic-gate void
seed_rng(void)58*7c478bd9Sstevel@tonic-gate seed_rng(void)
59*7c478bd9Sstevel@tonic-gate {
60*7c478bd9Sstevel@tonic-gate #ifndef OPENSSL_PRNG_ONLY
61*7c478bd9Sstevel@tonic-gate int devnull;
62*7c478bd9Sstevel@tonic-gate int p[2];
63*7c478bd9Sstevel@tonic-gate pid_t pid;
64*7c478bd9Sstevel@tonic-gate int ret;
65*7c478bd9Sstevel@tonic-gate unsigned char buf[RANDOM_SEED_SIZE];
66*7c478bd9Sstevel@tonic-gate mysig_t old_sigchld;
67*7c478bd9Sstevel@tonic-gate
68*7c478bd9Sstevel@tonic-gate if (RAND_status() == 1) {
69*7c478bd9Sstevel@tonic-gate debug3("RNG is ready, skipping seeding");
70*7c478bd9Sstevel@tonic-gate return;
71*7c478bd9Sstevel@tonic-gate }
72*7c478bd9Sstevel@tonic-gate
73*7c478bd9Sstevel@tonic-gate debug3("Seeding PRNG from %s", SSH_RAND_HELPER);
74*7c478bd9Sstevel@tonic-gate
75*7c478bd9Sstevel@tonic-gate if ((devnull = open("/dev/null", O_RDWR)) == -1)
76*7c478bd9Sstevel@tonic-gate fatal("Couldn't open /dev/null: %s", strerror(errno));
77*7c478bd9Sstevel@tonic-gate if (pipe(p) == -1)
78*7c478bd9Sstevel@tonic-gate fatal("pipe: %s", strerror(errno));
79*7c478bd9Sstevel@tonic-gate
80*7c478bd9Sstevel@tonic-gate old_sigchld = mysignal(SIGCHLD, SIG_DFL);
81*7c478bd9Sstevel@tonic-gate if ((pid = fork()) == -1)
82*7c478bd9Sstevel@tonic-gate fatal("Couldn't fork: %s", strerror(errno));
83*7c478bd9Sstevel@tonic-gate if (pid == 0) {
84*7c478bd9Sstevel@tonic-gate dup2(devnull, STDIN_FILENO);
85*7c478bd9Sstevel@tonic-gate dup2(p[1], STDOUT_FILENO);
86*7c478bd9Sstevel@tonic-gate /* Keep stderr open for errors */
87*7c478bd9Sstevel@tonic-gate close(p[0]);
88*7c478bd9Sstevel@tonic-gate close(p[1]);
89*7c478bd9Sstevel@tonic-gate close(devnull);
90*7c478bd9Sstevel@tonic-gate
91*7c478bd9Sstevel@tonic-gate if (original_uid != original_euid &&
92*7c478bd9Sstevel@tonic-gate ( seteuid(getuid()) == -1 ||
93*7c478bd9Sstevel@tonic-gate setuid(original_uid) == -1) ) {
94*7c478bd9Sstevel@tonic-gate fprintf(stderr, "(rand child) setuid(%d): %s\n",
95*7c478bd9Sstevel@tonic-gate original_uid, strerror(errno));
96*7c478bd9Sstevel@tonic-gate _exit(1);
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate
99*7c478bd9Sstevel@tonic-gate execl(SSH_RAND_HELPER, "ssh-rand-helper", NULL);
100*7c478bd9Sstevel@tonic-gate fprintf(stderr, "(rand child) Couldn't exec '%s': %s\n",
101*7c478bd9Sstevel@tonic-gate SSH_RAND_HELPER, strerror(errno));
102*7c478bd9Sstevel@tonic-gate _exit(1);
103*7c478bd9Sstevel@tonic-gate }
104*7c478bd9Sstevel@tonic-gate
105*7c478bd9Sstevel@tonic-gate close(devnull);
106*7c478bd9Sstevel@tonic-gate close(p[1]);
107*7c478bd9Sstevel@tonic-gate
108*7c478bd9Sstevel@tonic-gate memset(buf, '\0', sizeof(buf));
109*7c478bd9Sstevel@tonic-gate ret = atomicio(read, p[0], buf, sizeof(buf));
110*7c478bd9Sstevel@tonic-gate if (ret == -1)
111*7c478bd9Sstevel@tonic-gate fatal("Couldn't read from ssh-rand-helper: %s",
112*7c478bd9Sstevel@tonic-gate strerror(errno));
113*7c478bd9Sstevel@tonic-gate if (ret != sizeof(buf))
114*7c478bd9Sstevel@tonic-gate fatal("ssh-rand-helper child produced insufficient data");
115*7c478bd9Sstevel@tonic-gate
116*7c478bd9Sstevel@tonic-gate close(p[0]);
117*7c478bd9Sstevel@tonic-gate
118*7c478bd9Sstevel@tonic-gate if (waitpid(pid, &ret, 0) == -1)
119*7c478bd9Sstevel@tonic-gate fatal("Couldn't wait for ssh-rand-helper completion: %s",
120*7c478bd9Sstevel@tonic-gate strerror(errno));
121*7c478bd9Sstevel@tonic-gate mysignal(SIGCHLD, old_sigchld);
122*7c478bd9Sstevel@tonic-gate
123*7c478bd9Sstevel@tonic-gate /* We don't mind if the child exits upon a SIGPIPE */
124*7c478bd9Sstevel@tonic-gate if (!WIFEXITED(ret) &&
125*7c478bd9Sstevel@tonic-gate (!WIFSIGNALED(ret) || WTERMSIG(ret) != SIGPIPE))
126*7c478bd9Sstevel@tonic-gate fatal("ssh-rand-helper terminated abnormally");
127*7c478bd9Sstevel@tonic-gate if (WEXITSTATUS(ret) != 0)
128*7c478bd9Sstevel@tonic-gate fatal("ssh-rand-helper exit with exit status %d", ret);
129*7c478bd9Sstevel@tonic-gate
130*7c478bd9Sstevel@tonic-gate RAND_add(buf, sizeof(buf), sizeof(buf));
131*7c478bd9Sstevel@tonic-gate memset(buf, '\0', sizeof(buf));
132*7c478bd9Sstevel@tonic-gate
133*7c478bd9Sstevel@tonic-gate #endif /* OPENSSL_PRNG_ONLY */
134*7c478bd9Sstevel@tonic-gate if (RAND_status() != 1)
135*7c478bd9Sstevel@tonic-gate fatal("PRNG is not seeded");
136*7c478bd9Sstevel@tonic-gate }
137*7c478bd9Sstevel@tonic-gate
138*7c478bd9Sstevel@tonic-gate void
init_rng(void)139*7c478bd9Sstevel@tonic-gate init_rng(void)
140*7c478bd9Sstevel@tonic-gate {
141*7c478bd9Sstevel@tonic-gate /*
142*7c478bd9Sstevel@tonic-gate * OpenSSL version numbers: MNNFFPPS: major minor fix patch status
143*7c478bd9Sstevel@tonic-gate * We match major, minor, fix and status (not patch)
144*7c478bd9Sstevel@tonic-gate */
145*7c478bd9Sstevel@tonic-gate if ((SSLeay() ^ OPENSSL_VERSION_NUMBER) & ~0xff0L)
146*7c478bd9Sstevel@tonic-gate fatal("OpenSSL version mismatch. Built against %lx, you "
147*7c478bd9Sstevel@tonic-gate "have %lx", OPENSSL_VERSION_NUMBER, SSLeay());
148*7c478bd9Sstevel@tonic-gate
149*7c478bd9Sstevel@tonic-gate #ifndef OPENSSL_PRNG_ONLY
150*7c478bd9Sstevel@tonic-gate if ((original_uid = getuid()) == -1)
151*7c478bd9Sstevel@tonic-gate fatal("getuid: %s", strerror(errno));
152*7c478bd9Sstevel@tonic-gate if ((original_euid = geteuid()) == -1)
153*7c478bd9Sstevel@tonic-gate fatal("geteuid: %s", strerror(errno));
154*7c478bd9Sstevel@tonic-gate #endif
155*7c478bd9Sstevel@tonic-gate }
156*7c478bd9Sstevel@tonic-gate
157