1c9bd843ebc00801cc418156a3893362a1dc872edinak * CDDL HEADER START
1c9bd843ebc00801cc418156a3893362a1dc872edinak * The contents of this file are subject to the terms of the
1c9bd843ebc00801cc418156a3893362a1dc872edinak * Common Development and Distribution License (the "License").
1c9bd843ebc00801cc418156a3893362a1dc872edinak * You may not use this file except in compliance with the License.
1c9bd843ebc00801cc418156a3893362a1dc872edinak * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1c9bd843ebc00801cc418156a3893362a1dc872edinak * See the License for the specific language governing permissions
1c9bd843ebc00801cc418156a3893362a1dc872edinak * and limitations under the License.
1c9bd843ebc00801cc418156a3893362a1dc872edinak * When distributing Covered Code, include this CDDL HEADER in each
1c9bd843ebc00801cc418156a3893362a1dc872edinak * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1c9bd843ebc00801cc418156a3893362a1dc872edinak * If applicable, add the following below this CDDL HEADER, with the
1c9bd843ebc00801cc418156a3893362a1dc872edinak * fields enclosed by brackets "[]" replaced with your own identifying
1c9bd843ebc00801cc418156a3893362a1dc872edinak * information: Portions Copyright [yyyy] [name of copyright owner]
1c9bd843ebc00801cc418156a3893362a1dc872edinak * CDDL HEADER END
a7e661a2db6ff10cd43a089e7f4dcbd0cfd609f1Anthony Scarpino * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
1c9bd843ebc00801cc418156a3893362a1dc872edinak * Use is subject to license terms.
1c9bd843ebc00801cc418156a3893362a1dc872edinak * Read file into buffer. Used to read raw key data or initialization
1c9bd843ebc00801cc418156a3893362a1dc872edinak * vector data. Buffer must be freed by caller using free().
1c9bd843ebc00801cc418156a3893362a1dc872edinak * If file is a regular file, entire file is read and dlen is set
1c9bd843ebc00801cc418156a3893362a1dc872edinak * to the number of bytes read. Otherwise, dlen should first be set
1c9bd843ebc00801cc418156a3893362a1dc872edinak * to the number of bytes requested and will be reset to actual number
1c9bd843ebc00801cc418156a3893362a1dc872edinak * of bytes returned.
a7e661a2db6ff10cd43a089e7f4dcbd0cfd609f1Anthony Scarpino * Return 0 on success and errno on error.
1c9bd843ebc00801cc418156a3893362a1dc872edinakpkcs11_read_data(char *filename, void **dbuf, size_t *dlen)
1c9bd843ebc00801cc418156a3893362a1dc872edinak return (-1);
1c9bd843ebc00801cc418156a3893362a1dc872edinak if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) == -1) {
1c9bd843ebc00801cc418156a3893362a1dc872edinak cryptoerror(LOG_STDERR, gettext("cannot open %s"), filename);
1c9bd843ebc00801cc418156a3893362a1dc872edinak cryptoerror(LOG_STDERR, gettext("cannot stat %s"), filename);
1c9bd843ebc00801cc418156a3893362a1dc872edinak /* read the entire regular file */
1c9bd843ebc00801cc418156a3893362a1dc872edinak /* read requested bytes from special file */
1c9bd843ebc00801cc418156a3893362a1dc872edinak * for decrypt this is an error; for digest this is ok;
1c9bd843ebc00801cc418156a3893362a1dc872edinak * make it ok here but also set dbuf = NULL and dlen = 0
1c9bd843ebc00801cc418156a3893362a1dc872edinak * to indicate there was no data to read and caller can
1c9bd843ebc00801cc418156a3893362a1dc872edinak * retranslate that to an error if it wishes.
1c9bd843ebc00801cc418156a3893362a1dc872edinak return (0);
a7e661a2db6ff10cd43a089e7f4dcbd0cfd609f1Anthony Scarpino cryptoerror(LOG_STDERR, gettext("malloc: %s"), strerror(ret));
1c9bd843ebc00801cc418156a3893362a1dc872edinak /* either it got read or it didn't */
1c9bd843ebc00801cc418156a3893362a1dc872edinak /* reading from special file may need some coaxing */
1c9bd843ebc00801cc418156a3893362a1dc872edinak for (/* */; left > 0; marker += nread, left -= nread) {
1c9bd843ebc00801cc418156a3893362a1dc872edinak /* keep reading it's going well */
a7e661a2db6ff10cd43a089e7f4dcbd0cfd609f1Anthony Scarpino if (nread > 0 || (nread == 0 && errno == EINTR)) {
1c9bd843ebc00801cc418156a3893362a1dc872edinak /* might have to be good enough for caller */
1c9bd843ebc00801cc418156a3893362a1dc872edinak /* anything else is an error */
1c9bd843ebc00801cc418156a3893362a1dc872edinak /* reset to actual number of bytes read */
1c9bd843ebc00801cc418156a3893362a1dc872edinak return (0);