1eeb2c267SToomas Soome /*
2*59bbc19bSDan McDonald * This file and its contents are supplied under the terms of the
3*59bbc19bSDan McDonald * Common Development and Distribution License ("CDDL"), version 1.0.
4*59bbc19bSDan McDonald * You may only use this file in accordance with the terms of version
5*59bbc19bSDan McDonald * 1.0 of the CDDL.
6eeb2c267SToomas Soome *
7*59bbc19bSDan McDonald * A full copy of the text of the CDDL should have accompanied this
8*59bbc19bSDan McDonald * source. A copy of the CDDL is also available via the Internet at
9*59bbc19bSDan McDonald * http://www.illumos.org/license/CDDL.
10eeb2c267SToomas Soome */
11*59bbc19bSDan McDonald
12eeb2c267SToomas Soome /*
13eeb2c267SToomas Soome * Copyright 2016 Toomas Soome <tsoome@me.com>
14*59bbc19bSDan McDonald * Copyright 2017 OmniTI Computer Consulting, Inc. All rights reserved.
15eeb2c267SToomas Soome */
16eeb2c267SToomas Soome
17eeb2c267SToomas Soome /*
18eeb2c267SToomas Soome * Create sha1 hash for file.
19*59bbc19bSDan McDonald *
20*59bbc19bSDan McDonald * NOTE: This is hardwired for now, so use libmd's SHA1 for simplicity.
21eeb2c267SToomas Soome */
22eeb2c267SToomas Soome
23eeb2c267SToomas Soome #include <stdio.h>
24eeb2c267SToomas Soome #include <errno.h>
25eeb2c267SToomas Soome #include <string.h>
26eeb2c267SToomas Soome #include <sys/types.h>
27eeb2c267SToomas Soome #include <sys/stat.h>
28eeb2c267SToomas Soome #include <fcntl.h>
29eeb2c267SToomas Soome #include <locale.h>
30*59bbc19bSDan McDonald #include <sha1.h>
31*59bbc19bSDan McDonald #include <cryptoutil.h>
32eeb2c267SToomas Soome #include "bootadm.h"
33eeb2c267SToomas Soome
34*59bbc19bSDan McDonald #define BUFFERSIZE (64 * 1024)
35*59bbc19bSDan McDonald static uint8_t buf[BUFFERSIZE];
36eeb2c267SToomas Soome
37eeb2c267SToomas Soome int
bootadm_digest(const char * filename,char ** result)38eeb2c267SToomas Soome bootadm_digest(const char *filename, char **result)
39eeb2c267SToomas Soome {
40eeb2c267SToomas Soome int fd;
41eeb2c267SToomas Soome char *resultstr = NULL;
42*59bbc19bSDan McDonald uint8_t *resultbuf;
43*59bbc19bSDan McDonald int resultstrlen, resultlen, exitcode;
44*59bbc19bSDan McDonald SHA1_CTX sha1_ctx;
45*59bbc19bSDan McDonald ssize_t nread;
46eeb2c267SToomas Soome
47eeb2c267SToomas Soome /* Allocate a buffer to store result. */
48*59bbc19bSDan McDonald resultlen = SHA1_DIGEST_LENGTH;
49eeb2c267SToomas Soome if ((resultbuf = malloc(resultlen)) == NULL) {
50eeb2c267SToomas Soome bam_print(gettext("out of memory\n"));
51eeb2c267SToomas Soome exitcode = BAM_ERROR;
52eeb2c267SToomas Soome goto cleanup;
53eeb2c267SToomas Soome }
54eeb2c267SToomas Soome
55eeb2c267SToomas Soome if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) == -1) {
56eeb2c267SToomas Soome bam_print(gettext("can not open input file %s\n"), filename);
57eeb2c267SToomas Soome exitcode = BAM_ERROR;
58eeb2c267SToomas Soome goto cleanup;
59eeb2c267SToomas Soome }
60eeb2c267SToomas Soome
61*59bbc19bSDan McDonald SHA1Init(&sha1_ctx);
62*59bbc19bSDan McDonald while ((nread = read(fd, buf, sizeof (buf))) > 0)
63*59bbc19bSDan McDonald SHA1Update(&sha1_ctx, buf, nread);
64*59bbc19bSDan McDonald if (nread == -1) {
65*59bbc19bSDan McDonald bam_print(gettext("error reading file: %s\n"), strerror(errno));
66eeb2c267SToomas Soome exitcode = BAM_ERROR;
67eeb2c267SToomas Soome goto cleanup;
68eeb2c267SToomas Soome }
69*59bbc19bSDan McDonald SHA1Final(resultbuf, &sha1_ctx);
70eeb2c267SToomas Soome
71eeb2c267SToomas Soome /* Allocate a buffer to store result string */
72*59bbc19bSDan McDonald resultstrlen = 2 * resultlen + 1; /* Two hex chars per byte. */
73eeb2c267SToomas Soome if ((resultstr = malloc(resultstrlen)) == NULL) {
74eeb2c267SToomas Soome bam_print(gettext("out of memory\n"));
75eeb2c267SToomas Soome exitcode = BAM_ERROR;
76eeb2c267SToomas Soome goto cleanup;
77eeb2c267SToomas Soome }
78eeb2c267SToomas Soome
79eeb2c267SToomas Soome tohexstr(resultbuf, resultlen, resultstr, resultstrlen);
80*59bbc19bSDan McDonald exitcode = BAM_SUCCESS;
81eeb2c267SToomas Soome (void) close(fd);
82eeb2c267SToomas Soome cleanup:
83eeb2c267SToomas Soome if (exitcode == BAM_ERROR) {
84eeb2c267SToomas Soome free(resultstr);
85eeb2c267SToomas Soome resultstr = NULL;
86eeb2c267SToomas Soome }
87eeb2c267SToomas Soome
88eeb2c267SToomas Soome free(resultbuf);
89eeb2c267SToomas Soome
90eeb2c267SToomas Soome *result = resultstr;
91eeb2c267SToomas Soome return (exitcode);
92eeb2c267SToomas Soome }
93