summaryrefslogtreecommitdiff
path: root/security/netpgpverify/files/pgpsum.c
blob: 6bf6cc42538f896246739dfd9f5fa5a311797319 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*-
 * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "config.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "digest.h"
#include "pgpsum.h"

#ifndef USE_ARG
#define USE_ARG(x)	/*LINTED*/(void)&(x)
#endif

/* add the ascii armor line endings (except for last line) */
static size_t
don_armor(digest_t *hash, uint8_t *in, size_t insize, int doarmor)
{
	uint8_t	*from;
	uint8_t	*newp;
	uint8_t	*p;
	uint8_t	 dos_line_end[2];

	dos_line_end[0] = '\r';
	dos_line_end[1] = '\n';
	for (from = in ; (p = memchr(from, '\n', insize - (size_t)(from - in))) != NULL ; from = p + 1) {
		for (newp = p ; doarmor == 'w' && newp > from ; --newp) {
			if (*(newp - 1) != ' ' && *(newp - 1) != '\t') {
				break;
			}
		}
		digest_update(hash, from, (size_t)(newp - from));
		digest_update(hash, dos_line_end, sizeof(dos_line_end));
	}
	digest_update(hash, from, insize - (size_t)(from - in));
	return 1;
}

#ifdef NETPGPV_DEBUG
/* just for giggles, write what we're about to checksum */
static int
writefile(uint8_t *mem, size_t insize)
{
	size_t	cc;
	size_t	wc;
	char	template[256];
	int	fd;

	snprintf(template, sizeof(template), "netpgpvmd.XXXXXX");
	if ((fd = mkstemp(template)) < 0) {
		fprintf(stderr, "can't mkstemp %s\n", template);
		return 0;
	}
	for (cc = 0 ; cc < insize ; cc += wc) {
		if ((wc = write(fd, &mem[cc], insize - cc)) <= 0) {
			fprintf(stderr, "short write\n");
			break;
		}
	}
	close(fd);
	return 1;
}
#endif

/* return non-zero if this is actually an armored piece already */
static int
already_armored(uint8_t *in, size_t insize)
{
	uint8_t	*from;
	uint8_t	*p;

	for (from = in ; (p = memchr(from, '\n', insize - (size_t)(from - in))) != NULL ; from = p + 1) {
		if (*(p - 1) != '\r') {
			return 0;
		}
	}
	return 1;
}

/* calculate the checksum for the data we have */
static int
calcsum(uint8_t *out, size_t size, uint8_t *mem, size_t cc, const uint8_t *hashed, size_t hashsize, int doarmor)
{
	digest_t	 hash;
	uint32_t	 len32;
	uint16_t	 len16;
	uint8_t		 hashalg;
	uint8_t		 trailer[6];

	USE_ARG(size);
	/* hashed data is non-null (previously checked) */
	hashalg = hashed[3];
	memcpy(&len16, &hashed[4], sizeof(len16));
	len32 = ntohs(len16) + 6;
	len32 = htonl(len32);
	trailer[0] = 0x04;
	trailer[1] = 0xff;
	memcpy(&trailer[2], &len32, sizeof(len32));
#ifdef NETPGPV_DEBUG
	writefile(mem, cc);
#endif
	digest_init(&hash, (const unsigned)hashalg);
	if (strchr("tw", doarmor) != NULL && !already_armored(mem, cc)) {
		/* this took me ages to find - something causes gpg to truncate its input */
		don_armor(&hash, mem, cc - 1, doarmor);
	} else {
		digest_update(&hash, mem, cc);
	}
	if (hashed) {
		digest_update(&hash, hashed, hashsize);
	}
	digest_update(&hash, trailer, sizeof(trailer));
	return digest_final(out, &hash);
}

/* open the file, mmap it, and then get the checksum on that */
int
pgpv_digest_file(uint8_t *data, size_t size, const char *name, const uint8_t *hashed, size_t hashsize, int doarmor)
{
	struct stat	 st;
	uint8_t		*mem;
	size_t		 cc;
	FILE		*fp;
	int		 ret;

	if (hashed == NULL || data == NULL || name == NULL) {
		fprintf(stderr, "no hashed data provided\n");
		return 0;
	}
	ret = 0;
	mem = NULL;
	cc = 0;
	if ((fp = fopen(name, "r")) == NULL) {
		fprintf(stderr, "%s - not found", name);
		return 0;
	}
	if (fstat(fileno(fp), &st) < 0) {
		fprintf(stderr, "%s - can't stat", name);
		goto done;
	}
	cc = (size_t)(st.st_size);
	if ((mem = mmap(NULL, cc, PROT_READ, MAP_SHARED, fileno(fp), 0)) == MAP_FAILED) {
		fprintf(stderr, "%s - can't mmap", name);
		goto done;
	}
	ret = calcsum(data, size, mem, cc, hashed, hashsize, doarmor);
done:
	if (data) {
		munmap(mem, cc);
	}
	fclose(fp);
	return ret;
}

/* calculate the digest over memory too */
int
pgpv_digest_memory(uint8_t *data, size_t size, void *mem, size_t cc, const uint8_t *hashed, size_t hashsize, int doarmor)
{
	if (hashed == NULL || data == NULL || mem == NULL) {
		fprintf(stderr, "no hashed data provided\n");
		return 0;
	}
	return calcsum(data, size, mem, cc, hashed, hashsize, doarmor);
}