12dc23587SToomas Soome /*
2*703ced38SDan McDonald * This file and its contents are supplied under the terms of the
3*703ced38SDan McDonald * Common Development and Distribution License ("CDDL"), version 1.0.
4*703ced38SDan McDonald * You may only use this file in accordance with the terms of version
5*703ced38SDan McDonald * 1.0 of the CDDL.
62dc23587SToomas Soome *
7*703ced38SDan McDonald * A full copy of the text of the CDDL should have accompanied this
8*703ced38SDan McDonald * source. A copy of the CDDL is also available via the Internet at
9*703ced38SDan McDonald * http://www.illumos.org/license/CDDL.
102dc23587SToomas Soome */
11*703ced38SDan McDonald
122dc23587SToomas Soome /*
132dc23587SToomas Soome * Copyright 2016 Toomas Soome <tsoome@me.com>
14*703ced38SDan McDonald * Copyright 2017 OmniTI Computer Consulting, Inc. All rights reserved.
152dc23587SToomas Soome */
162dc23587SToomas Soome
172dc23587SToomas Soome /*
182dc23587SToomas Soome * Create sha1 hash for file.
19*703ced38SDan McDonald *
20*703ced38SDan McDonald * NOTE: This is hardwired for now, so use libmd's SHA1 for simplicity.
212dc23587SToomas Soome */
222dc23587SToomas Soome
232dc23587SToomas Soome #include <stdio.h>
242dc23587SToomas Soome #include <errno.h>
252dc23587SToomas Soome #include <string.h>
262dc23587SToomas Soome #include <sys/types.h>
272dc23587SToomas Soome #include <sys/stat.h>
282dc23587SToomas Soome #include <fcntl.h>
292dc23587SToomas Soome #include <locale.h>
30*703ced38SDan McDonald #include <sha1.h>
31*703ced38SDan McDonald #include <cryptoutil.h>
322dc23587SToomas Soome #include "bootadm.h"
332dc23587SToomas Soome
34*703ced38SDan McDonald #define BUFFERSIZE (64 * 1024)
35*703ced38SDan McDonald static uint8_t buf[BUFFERSIZE];
362dc23587SToomas Soome
372dc23587SToomas Soome int
bootadm_digest(const char * filename,char ** result)382dc23587SToomas Soome bootadm_digest(const char *filename, char **result)
392dc23587SToomas Soome {
402dc23587SToomas Soome int fd;
412dc23587SToomas Soome char *resultstr = NULL;
42*703ced38SDan McDonald uint8_t *resultbuf;
43*703ced38SDan McDonald int resultstrlen, resultlen, exitcode;
44*703ced38SDan McDonald SHA1_CTX sha1_ctx;
45*703ced38SDan McDonald ssize_t nread;
462dc23587SToomas Soome
472dc23587SToomas Soome /* Allocate a buffer to store result. */
48*703ced38SDan McDonald resultlen = SHA1_DIGEST_LENGTH;
492dc23587SToomas Soome if ((resultbuf = malloc(resultlen)) == NULL) {
502dc23587SToomas Soome bam_print(gettext("out of memory\n"));
512dc23587SToomas Soome exitcode = BAM_ERROR;
522dc23587SToomas Soome goto cleanup;
532dc23587SToomas Soome }
542dc23587SToomas Soome
552dc23587SToomas Soome if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) == -1) {
562dc23587SToomas Soome bam_print(gettext("can not open input file %s\n"), filename);
572dc23587SToomas Soome exitcode = BAM_ERROR;
582dc23587SToomas Soome goto cleanup;
592dc23587SToomas Soome }
602dc23587SToomas Soome
61*703ced38SDan McDonald SHA1Init(&sha1_ctx);
62*703ced38SDan McDonald while ((nread = read(fd, buf, sizeof (buf))) > 0)
63*703ced38SDan McDonald SHA1Update(&sha1_ctx, buf, nread);
64*703ced38SDan McDonald if (nread == -1) {
65*703ced38SDan McDonald bam_print(gettext("error reading file: %s\n"), strerror(errno));
662dc23587SToomas Soome exitcode = BAM_ERROR;
672dc23587SToomas Soome goto cleanup;
682dc23587SToomas Soome }
69*703ced38SDan McDonald SHA1Final(resultbuf, &sha1_ctx);
702dc23587SToomas Soome
712dc23587SToomas Soome /* Allocate a buffer to store result string */
72*703ced38SDan McDonald resultstrlen = 2 * resultlen + 1; /* Two hex chars per byte. */
732dc23587SToomas Soome if ((resultstr = malloc(resultstrlen)) == NULL) {
742dc23587SToomas Soome bam_print(gettext("out of memory\n"));
752dc23587SToomas Soome exitcode = BAM_ERROR;
762dc23587SToomas Soome goto cleanup;
772dc23587SToomas Soome }
782dc23587SToomas Soome
792dc23587SToomas Soome tohexstr(resultbuf, resultlen, resultstr, resultstrlen);
80*703ced38SDan McDonald exitcode = BAM_SUCCESS;
812dc23587SToomas Soome (void) close(fd);
822dc23587SToomas Soome cleanup:
832dc23587SToomas Soome if (exitcode == BAM_ERROR) {
842dc23587SToomas Soome free(resultstr);
852dc23587SToomas Soome resultstr = NULL;
862dc23587SToomas Soome }
872dc23587SToomas Soome
882dc23587SToomas Soome free(resultbuf);
892dc23587SToomas Soome
902dc23587SToomas Soome *result = resultstr;
912dc23587SToomas Soome return (exitcode);
922dc23587SToomas Soome }
93