rand_egd.c revision 59191
159191Skris/* crypto/rand/rand_egd.c */
259191Skris/* Written by Ulf Moeller for the OpenSSL project. */
359191Skris/* ====================================================================
459191Skris * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
559191Skris *
659191Skris * Redistribution and use in source and binary forms, with or without
759191Skris * modification, are permitted provided that the following conditions
859191Skris * are met:
959191Skris *
1059191Skris * 1. Redistributions of source code must retain the above copyright
1159191Skris *    notice, this list of conditions and the following disclaimer.
1259191Skris *
1359191Skris * 2. Redistributions in binary form must reproduce the above copyright
1459191Skris *    notice, this list of conditions and the following disclaimer in
1559191Skris *    the documentation and/or other materials provided with the
1659191Skris *    distribution.
1759191Skris *
1859191Skris * 3. All advertising materials mentioning features or use of this
1959191Skris *    software must display the following acknowledgment:
2059191Skris *    "This product includes software developed by the OpenSSL Project
2159191Skris *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
2259191Skris *
2359191Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2459191Skris *    endorse or promote products derived from this software without
2559191Skris *    prior written permission. For written permission, please contact
2659191Skris *    openssl-core@openssl.org.
2759191Skris *
2859191Skris * 5. Products derived from this software may not be called "OpenSSL"
2959191Skris *    nor may "OpenSSL" appear in their names without prior written
3059191Skris *    permission of the OpenSSL Project.
3159191Skris *
3259191Skris * 6. Redistributions of any form whatsoever must retain the following
3359191Skris *    acknowledgment:
3459191Skris *    "This product includes software developed by the OpenSSL Project
3559191Skris *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
3659191Skris *
3759191Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
3859191Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3959191Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4059191Skris * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4159191Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4259191Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4359191Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4459191Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4559191Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4659191Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4759191Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
4859191Skris * OF THE POSSIBILITY OF SUCH DAMAGE.
4959191Skris * ====================================================================
5059191Skris *
5159191Skris * This product includes cryptographic software written by Eric Young
5259191Skris * (eay@cryptsoft.com).  This product includes software written by Tim
5359191Skris * Hudson (tjh@cryptsoft.com).
5459191Skris *
5559191Skris */
5659191Skris
5759191Skris#include <openssl/rand.h>
5859191Skris
5959191Skris/* Query the EGD <URL: http://www.lothar.com/tech/crypto/>.
6059191Skris */
6159191Skris
6259191Skris#if defined(WIN32) || defined(VMS) || defined(__VMS)
6359191Skrisint RAND_egd(const char *path)
6459191Skris	{
6559191Skris	return(-1);
6659191Skris	}
6759191Skris#else
6859191Skris#include <openssl/opensslconf.h>
6959191Skris#include OPENSSL_UNISTD
7059191Skris#include <sys/types.h>
7159191Skris#include <sys/socket.h>
7259191Skris#include <sys/un.h>
7359191Skris#include <string.h>
7459191Skris
7559191Skris#ifndef offsetof
7659191Skris#  define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
7759191Skris#endif
7859191Skris
7959191Skrisint RAND_egd(const char *path)
8059191Skris	{
8159191Skris	int ret = -1;
8259191Skris	struct sockaddr_un addr;
8359191Skris	int len, num;
8459191Skris	int fd = -1;
8559191Skris	unsigned char buf[256];
8659191Skris
8759191Skris	memset(&addr, 0, sizeof(addr));
8859191Skris	addr.sun_family = AF_UNIX;
8959191Skris	if (strlen(path) > sizeof(addr.sun_path))
9059191Skris		return (-1);
9159191Skris	strcpy(addr.sun_path,path);
9259191Skris	len = offsetof(struct sockaddr_un, sun_path) + strlen(path);
9359191Skris	fd = socket(AF_UNIX, SOCK_STREAM, 0);
9459191Skris	if (fd == -1) return (-1);
9559191Skris	if (connect(fd, (struct sockaddr *)&addr, len) == -1) goto err;
9659191Skris	buf[0] = 1;
9759191Skris	buf[1] = 255;
9859191Skris	write(fd, buf, 2);
9959191Skris	if (read(fd, buf, 1) != 1) goto err;
10059191Skris	if (buf[0] == 0) goto err;
10159191Skris	num = read(fd, buf, 255);
10259191Skris	if (num < 1) goto err;
10359191Skris	RAND_seed(buf, num);
10459191Skris	if (RAND_status() == 1)
10559191Skris		ret = num;
10659191Skris err:
10759191Skris	if (fd != -1) close(fd);
10859191Skris	return(ret);
10959191Skris	}
11059191Skris#endif
111