xref: /netbsd-src/external/ibm-public/postfix/dist/src/tls/tls_prng_file.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1*e89934bbSchristos /*	$NetBSD: tls_prng_file.c,v 1.2 2017/02/14 01:16:48 christos Exp $	*/
241fbaed0Stron 
341fbaed0Stron /*++
441fbaed0Stron /* NAME
541fbaed0Stron /*	tls_prng_file 3
641fbaed0Stron /* SUMMARY
741fbaed0Stron /*	seed OpenSSL PRNG from entropy file
841fbaed0Stron /* SYNOPSIS
941fbaed0Stron /*	#include <tls_prng_src.h>
1041fbaed0Stron /*
1141fbaed0Stron /*	TLS_PRNG_SRC *tls_prng_file_open(name, timeout)
1241fbaed0Stron /*	const char *name;
1341fbaed0Stron /*	int	timeout;
1441fbaed0Stron /*
1541fbaed0Stron /*	ssize_t tls_prng_file_read(fh, length)
1641fbaed0Stron /*	TLS_PRNG_SRC *fh;
1741fbaed0Stron /*	size_t length;
1841fbaed0Stron /*
1941fbaed0Stron /*	int	tls_prng_file_close(fh)
2041fbaed0Stron /*	TLS_PRNG_SRC *fh;
2141fbaed0Stron /* DESCRIPTION
2241fbaed0Stron /*	tls_prng_file_open() open the specified file and returns
2341fbaed0Stron /*	a handle that should be used with all subsequent access.
2441fbaed0Stron /*
2541fbaed0Stron /*	tls_prng_file_read() reads the requested number of bytes from
2641fbaed0Stron /*	the entropy file and updates the OpenSSL PRNG. The file is not
2741fbaed0Stron /*	locked for shared or exclusive access.
2841fbaed0Stron /*
2941fbaed0Stron /*	tls_prng_file_close() closes the specified entropy file
3041fbaed0Stron /*	and releases memory that was allocated for the handle.
3141fbaed0Stron /*
3241fbaed0Stron /*	Arguments:
3341fbaed0Stron /* .IP name
3441fbaed0Stron /*	The pathname of the entropy file.
3541fbaed0Stron /* .IP length
3641fbaed0Stron /*	The number of bytes to read from the entropy file.
3741fbaed0Stron /* .IP timeout
3841fbaed0Stron /*	Time limit on individual I/O operations.
3941fbaed0Stron /* DIAGNOSTICS
4041fbaed0Stron /*	tls_prng_file_open() returns a null pointer on error.
4141fbaed0Stron /*
4241fbaed0Stron /*	tls_prng_file_read() returns -1 on error, the number
4341fbaed0Stron /*	of bytes received on success.
4441fbaed0Stron /*
4541fbaed0Stron /*	tls_prng_file_close() returns -1 on error, 0 on success.
4641fbaed0Stron /*
4741fbaed0Stron /*	In all cases the errno variable indicates the type of error.
4841fbaed0Stron /* LICENSE
4941fbaed0Stron /* .ad
5041fbaed0Stron /* .fi
5141fbaed0Stron /*	The Secure Mailer license must be distributed with this software.
5241fbaed0Stron /* AUTHOR(S)
5341fbaed0Stron /*	Wietse Venema
5441fbaed0Stron /*	IBM T.J. Watson Research
5541fbaed0Stron /*	P.O. Box 704
5641fbaed0Stron /*	Yorktown Heights, NY 10598, USA
5741fbaed0Stron /*--*/
5841fbaed0Stron 
5941fbaed0Stron /* System library. */
6041fbaed0Stron 
6141fbaed0Stron #include <sys_defs.h>
6241fbaed0Stron #include <fcntl.h>
6341fbaed0Stron #include <unistd.h>
6441fbaed0Stron #include <limits.h>
6541fbaed0Stron #include <errno.h>
6641fbaed0Stron 
6741fbaed0Stron /* OpenSSL library. */
6841fbaed0Stron 
6941fbaed0Stron #ifdef USE_TLS
7041fbaed0Stron #include <openssl/rand.h>		/* For the PRNG */
7141fbaed0Stron 
7241fbaed0Stron /* Utility library. */
7341fbaed0Stron 
7441fbaed0Stron #include <msg.h>
7541fbaed0Stron #include <mymalloc.h>
7641fbaed0Stron #include <connect.h>
7741fbaed0Stron #include <iostuff.h>
7841fbaed0Stron 
7941fbaed0Stron /* TLS library. */
8041fbaed0Stron 
8141fbaed0Stron #include <tls_prng.h>
8241fbaed0Stron 
8341fbaed0Stron /* tls_prng_file_open - open entropy file */
8441fbaed0Stron 
tls_prng_file_open(const char * name,int timeout)8541fbaed0Stron TLS_PRNG_SRC *tls_prng_file_open(const char *name, int timeout)
8641fbaed0Stron {
8741fbaed0Stron     const char *myname = "tls_prng_file_open";
8841fbaed0Stron     TLS_PRNG_SRC *fh;
8941fbaed0Stron     int     fd;
9041fbaed0Stron 
9141fbaed0Stron     if ((fd = open(name, O_RDONLY, 0)) < 0) {
9241fbaed0Stron 	if (msg_verbose)
9341fbaed0Stron 	    msg_info("%s: cannot open entropy file %s: %m", myname, name);
9441fbaed0Stron 	return (0);
9541fbaed0Stron     } else {
9641fbaed0Stron 	fh = (TLS_PRNG_SRC *) mymalloc(sizeof(*fh));
9741fbaed0Stron 	fh->fd = fd;
9841fbaed0Stron 	fh->name = mystrdup(name);
9941fbaed0Stron 	fh->timeout = timeout;
10041fbaed0Stron 	if (msg_verbose)
10141fbaed0Stron 	    msg_info("%s: opened entropy file %s", myname, name);
10241fbaed0Stron 	return (fh);
10341fbaed0Stron     }
10441fbaed0Stron }
10541fbaed0Stron 
10641fbaed0Stron /* tls_prng_file_read - update internal PRNG from entropy file */
10741fbaed0Stron 
tls_prng_file_read(TLS_PRNG_SRC * fh,size_t len)10841fbaed0Stron ssize_t tls_prng_file_read(TLS_PRNG_SRC *fh, size_t len)
10941fbaed0Stron {
11041fbaed0Stron     const char *myname = "tls_prng_file_read";
11141fbaed0Stron     char    buffer[8192];
11241fbaed0Stron     ssize_t to_read;
11341fbaed0Stron     ssize_t count;
11441fbaed0Stron 
11541fbaed0Stron     if (msg_verbose)
11641fbaed0Stron 	msg_info("%s: seed internal pool from file %s", myname, fh->name);
11741fbaed0Stron 
11841fbaed0Stron     if (lseek(fh->fd, 0, SEEK_SET) < 0) {
11941fbaed0Stron 	if (msg_verbose)
12041fbaed0Stron 	    msg_info("cannot seek entropy file %s: %m", fh->name);
12141fbaed0Stron 	return (-1);
12241fbaed0Stron     }
12341fbaed0Stron     errno = 0;
12441fbaed0Stron     for (to_read = len; to_read > 0; to_read -= count) {
12541fbaed0Stron 	if ((count = timed_read(fh->fd, buffer, to_read > sizeof(buffer) ?
12641fbaed0Stron 				sizeof(buffer) : to_read,
12741fbaed0Stron 				fh->timeout, (void *) 0)) < 0) {
12841fbaed0Stron 	    if (msg_verbose)
12941fbaed0Stron 		msg_info("cannot read entropy file %s: %m", fh->name);
13041fbaed0Stron 	    return (-1);
13141fbaed0Stron 	}
13241fbaed0Stron 	if (count == 0)
13341fbaed0Stron 	    break;
13441fbaed0Stron 	RAND_seed(buffer, count);
13541fbaed0Stron     }
13641fbaed0Stron     if (msg_verbose)
13741fbaed0Stron 	msg_info("read %ld bytes from entropy file %s: %m",
13841fbaed0Stron 		 (long) (len - to_read), fh->name);
13941fbaed0Stron     return (len - to_read);
14041fbaed0Stron }
14141fbaed0Stron 
14241fbaed0Stron /* tls_prng_file_close - close entropy file */
14341fbaed0Stron 
tls_prng_file_close(TLS_PRNG_SRC * fh)14441fbaed0Stron int     tls_prng_file_close(TLS_PRNG_SRC *fh)
14541fbaed0Stron {
14641fbaed0Stron     const char *myname = "tls_prng_file_close";
14741fbaed0Stron     int     err;
14841fbaed0Stron 
14941fbaed0Stron     if (msg_verbose)
15041fbaed0Stron 	msg_info("%s: close entropy file %s", myname, fh->name);
15141fbaed0Stron     err = close(fh->fd);
15241fbaed0Stron     myfree(fh->name);
153e262b48eSchristos     myfree((void *) fh);
15441fbaed0Stron     return (err);
15541fbaed0Stron }
15641fbaed0Stron 
15741fbaed0Stron #endif
158