2 * Copyright (c) 1999 Marc Espie.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Marc Espie for the OpenBSD
17 * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
21 * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * $OpenBSD: sha1.c,v 1.1 1999/10/04 21:46:29 espie Exp $
30 * $FreeBSD: src/usr.sbin/pkg_install/sign/sha1.c,v 1.5 2004/06/29 19:06:42 eik Exp $
31 * $DragonFly: src/usr.sbin/pkg_install/sign/Attic/sha1.c,v 1.3 2004/07/30 04:46:14 dillon Exp $
34 #include <sys/types.h>
45 /* private context for sha1 signature checker */
53 #define SHA1_TEMPLATE "SHA1 (%s) = "
54 #define BUFSIZE (MAXID+sizeof(SHA1_TEMPLATE)+2*SHA_DIGEST_LENGTH+1)
57 * Finalize SHA1 checksum for our sha1_context into result
58 * (size at least BUFSIZE). Returns the length of the checksum
59 * marker, e.g., SHA1 (id) = xxxxxxxxx
61 * Return 0 for errors.
64 sha1_build_checksum(result, n)
66 struct sha1_checker *n;
70 snprintf(result, BUFSIZE-2*SHA_DIGEST_LENGTH-1, SHA1_TEMPLATE, n->id);
71 length = strlen(result);
72 SHA1_End(&n->context, result + length);
79 new_sha1_checker(h, sign, userid, envp, filename)
80 struct mygzip_header *h;
81 struct signature *sign;
84 /*@observer@*/const char *filename;
86 struct sha1_checker *n;
88 assert(sign->type == TAG_SHA1);
89 /* make sure data conforms to what we can handle */
90 if (sign->length > MAXID || sign->data[sign->length-1] != '\0') {
91 warnx("Corrupted SHA1 header in %s", filename);
95 n = malloc(sizeof *n);
97 warnx("Can't allocate sha1_checker");
100 SHA1_Init(&n->context);
102 n->filename = filename;
104 /* copy header, as this is a checksum, we don't strip our own marker */
105 if (gzip_copy_header(h, sign, sha1_add, n) == 0) {
106 warnx("Unexpected header in %s", filename);
114 sha1_add(arg, buffer, length)
119 struct sha1_checker *n = arg;
120 SHA1_Update(&n->context, buffer, length);
127 struct sha1_checker *n = arg;
128 char buffer[BUFSIZE];
134 length = sha1_build_checksum(buffer, n);
135 f= fopen(SHA1_DB_NAME, "r");
139 warn("Can't access checksum file %s", SHA1_DB_NAME);
142 while (fgets(scan, sizeof(scan), f) != NULL) {
143 if (strcmp(scan, buffer) == 0) {
144 fprintf(stderr, "Checksum ok\n");
147 if (strncmp(scan, buffer, length) == 0)
152 warnx("Checksum incorrect for %s (%s)", n->filename, n->id);
155 warnx("No checksum found for %s (%s)", n->filename, n->id);
156 return PKG_SIGUNKNOWN;
161 retrieve_sha1_marker(filename, sign, userid)
162 const char *filename;
163 struct signature **sign;
167 struct mygzip_header h;
170 char result[BUFSIZE];
172 struct sha1_checker *checker;
179 * Create a blank signature and fill it with the userid.
181 n = malloc(sizeof *n);
184 n->length = strlen(userid)+1;
185 n->data = malloc(n->length);
186 if (n->data == NULL) {
190 memcpy(n->data, userid, n->length);
192 memcpy(n->tag, sha1tag, sizeof sha1tag);
196 * Read the gzip header and add our "userid" signature to it.
198 f = fopen(filename, "r");
203 if (gzip_read_header(f, &h, sign) == GZIP_NOT_GZIP) {
204 warnx("File %s is not a gzip file\n", filename);
213 * Calculate the SHA1 of the remaining data and write it to stderr.
215 checker = new_sha1_checker(&h, *sign, NULL, NULL, filename);
217 while ((length = fread(buffer, 1, sizeof buffer, f)) > 0)
218 sha1_add(checker, buffer, length);
219 if (fclose(f) != 0 || length == -1) {
220 warn("Problem checksumming %s", filename);
226 (void)sha1_build_checksum(result, checker);
227 fputs(result, stderr);