xref: /illumos-gate/usr/src/lib/libcryptoutil/common/keyfile.c (revision f03fb72c9f13560757b95b26631635b5d2a6d6c5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <stdio.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <errno.h>
32 #include <locale.h>
33 #include <cryptoutil.h>
34 
35 /*
36  * Read file into buffer.  Used to read raw key data or initialization
37  * vector data.  Buffer must be freed by caller using free().
38  *
39  * If file is a regular file, entire file is read and dlen is set
40  * to the number of bytes read.  Otherwise, dlen should first be set
41  * to the number of bytes requested and will be reset to actual number
42  * of bytes returned.
43  *
44  * Return 0 on success and errno on error.
45  */
46 int
47 pkcs11_read_data(char *filename, void **dbuf, size_t *dlen)
48 {
49 	int	fd = -1;
50 	struct stat statbuf;
51 	boolean_t plain_file;
52 	void	*filebuf = NULL;
53 	size_t	filesize = 0;
54 	int ret = 0;
55 
56 	if (filename == NULL || dbuf == NULL || dlen == NULL)
57 		return (-1);
58 
59 	if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) == -1) {
60 		ret = errno;
61 		cryptoerror(LOG_STDERR, gettext("cannot open %s"), filename);
62 		goto error;
63 	}
64 
65 	if (fstat(fd, &statbuf) == -1) {
66 		ret = errno;
67 		cryptoerror(LOG_STDERR, gettext("cannot stat %s"), filename);
68 		goto error;
69 	}
70 
71 	if (S_ISREG(statbuf.st_mode)) {
72 		/* read the entire regular file */
73 		filesize = statbuf.st_size;
74 		plain_file = B_TRUE;
75 	} else {
76 		/* read requested bytes from special file */
77 		filesize = *dlen;
78 		plain_file = B_FALSE;
79 	}
80 
81 	if (filesize == 0) {
82 		/*
83 		 * for decrypt this is an error; for digest this is ok;
84 		 * make it ok here but also set dbuf = NULL and dlen = 0
85 		 * to indicate there was no data to read and caller can
86 		 * retranslate that to an error if it wishes.
87 		 */
88 		(void) close(fd);
89 		*dbuf = NULL;
90 		*dlen = 0;
91 		return (0);
92 	}
93 
94 	if ((filebuf = malloc(filesize)) == NULL) {
95 		ret = errno;
96 		cryptoerror(LOG_STDERR, gettext("malloc: %s"), strerror(ret));
97 		goto error;
98 	}
99 
100 	if (plain_file) {
101 		/* either it got read or it didn't */
102 		if (read(fd, filebuf, filesize) != filesize) {
103 			ret = errno;
104 			cryptoerror(LOG_STDERR,
105 			    gettext("error reading file %s: %s"), filename,
106 			    strerror(ret));
107 			goto error;
108 		}
109 	} else {
110 		/* reading from special file may need some coaxing */
111 		char	*marker = (char *)filebuf;
112 		size_t	left = filesize;
113 		ssize_t	nread;
114 
115 		for (/* */; left > 0; marker += nread, left -= nread) {
116 			/* keep reading it's going well */
117 			nread = read(fd, marker, left);
118 			if (nread > 0 || (nread == 0 && errno == EINTR)) {
119 				errno = 0;
120 				continue;
121 			}
122 
123 			/* might have to be good enough for caller */
124 			if (nread == 0 && errno == EAGAIN)
125 				break;
126 
127 			/* anything else is an error */
128 			if (errno) {
129 				ret = errno;
130 				cryptoerror(LOG_STDERR,
131 				    gettext("error reading file %s: %s"),
132 				    filename, strerror(ret));
133 				goto error;
134 			}
135 		}
136 		/* reset to actual number of bytes read */
137 		filesize -= left;
138 	}
139 
140 	(void) close(fd);
141 	*dbuf = filebuf;
142 	*dlen = filesize;
143 	return (0);
144 
145 error:
146 	if (fd != -1)
147 		(void) close(fd);
148 
149 	return (ret);
150 }
151