| @@ -1,263 +1,266 @@ | | | @@ -1,263 +1,266 @@ |
1 | /*- | | 1 | /*- |
2 | * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org> | | 2 | * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org> |
3 | * All rights reserved. | | 3 | * All rights reserved. |
4 | * | | 4 | * |
5 | * Redistribution and use in source and binary forms, with or without | | 5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions | | 6 | * modification, are permitted provided that the following conditions |
7 | * are met: | | 7 | * are met: |
8 | * 1. Redistributions of source code must retain the above copyright | | 8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. | | 9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright | | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | | 11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | | 12 | * documentation and/or other materials provided with the distribution. |
13 | * | | 13 | * |
14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | | 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | | 15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | | 16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | | 17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | | 18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | | 19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | | 20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | | 21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | | 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | | 23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ | | 24 | */ |
25 | #include "config.h" | | 25 | #include "config.h" |
26 | | | 26 | |
27 | #include <sys/types.h> | | 27 | #include <sys/types.h> |
28 | #include <sys/stat.h> | | 28 | #include <sys/stat.h> |
29 | #include <sys/mman.h> | | 29 | #include <sys/mman.h> |
30 | | | 30 | |
31 | #include <inttypes.h> | | 31 | #include <inttypes.h> |
32 | #include <stdio.h> | | 32 | #include <stdio.h> |
33 | #include <stdlib.h> | | 33 | #include <stdlib.h> |
34 | #include <string.h> | | 34 | #include <string.h> |
35 | #include <unistd.h> | | 35 | #include <unistd.h> |
36 | | | 36 | |
37 | #include "digest.h" | | 37 | #include "digest.h" |
38 | #include "pgpsum.h" | | 38 | #include "pgpsum.h" |
39 | | | 39 | |
40 | #ifndef USE_ARG | | 40 | #ifndef USE_ARG |
41 | #define USE_ARG(x) /*LINTED*/(void)&(x) | | 41 | #define USE_ARG(x) /*LINTED*/(void)&(x) |
42 | #endif | | 42 | #endif |
43 | | | 43 | |
| | | 44 | #undef swap16 |
| | | 45 | #undef swap32 |
| | | 46 | |
44 | /* add the ascii armor line endings (except for last line) */ | | 47 | /* add the ascii armor line endings (except for last line) */ |
45 | static size_t | | 48 | static size_t |
46 | don_armor(digest_t *hash, uint8_t *in, size_t insize, int doarmor) | | 49 | don_armor(digest_t *hash, uint8_t *in, size_t insize, int doarmor) |
47 | { | | 50 | { |
48 | uint8_t *from; | | 51 | uint8_t *from; |
49 | uint8_t *newp; | | 52 | uint8_t *newp; |
50 | uint8_t *p; | | 53 | uint8_t *p; |
51 | uint8_t dos_line_end[2]; | | 54 | uint8_t dos_line_end[2]; |
52 | | | 55 | |
53 | dos_line_end[0] = '\r'; | | 56 | dos_line_end[0] = '\r'; |
54 | dos_line_end[1] = '\n'; | | 57 | dos_line_end[1] = '\n'; |
55 | for (from = in ; (p = memchr(from, '\n', insize - (size_t)(from - in))) != NULL ; from = p + 1) { | | 58 | for (from = in ; (p = memchr(from, '\n', insize - (size_t)(from - in))) != NULL ; from = p + 1) { |
56 | for (newp = p ; doarmor == 'w' && newp > from ; --newp) { | | 59 | for (newp = p ; doarmor == 'w' && newp > from ; --newp) { |
57 | if (*(newp - 1) != ' ' && *(newp - 1) != '\t') { | | 60 | if (*(newp - 1) != ' ' && *(newp - 1) != '\t') { |
58 | break; | | 61 | break; |
59 | } | | 62 | } |
60 | } | | 63 | } |
61 | digest_update(hash, from, (size_t)(newp - from)); | | 64 | digest_update(hash, from, (size_t)(newp - from)); |
62 | digest_update(hash, dos_line_end, sizeof(dos_line_end)); | | 65 | digest_update(hash, dos_line_end, sizeof(dos_line_end)); |
63 | } | | 66 | } |
64 | digest_update(hash, from, insize - (size_t)(from - in)); | | 67 | digest_update(hash, from, insize - (size_t)(from - in)); |
65 | return 1; | | 68 | return 1; |
66 | } | | 69 | } |
67 | | | 70 | |
68 | #ifdef NETPGPV_DEBUG | | 71 | #ifdef NETPGPV_DEBUG |
69 | /* just for giggles, write what we're about to checksum */ | | 72 | /* just for giggles, write what we're about to checksum */ |
70 | static int | | 73 | static int |
71 | writefile(uint8_t *mem, size_t insize) | | 74 | writefile(uint8_t *mem, size_t insize) |
72 | { | | 75 | { |
73 | size_t cc; | | 76 | size_t cc; |
74 | size_t wc; | | 77 | size_t wc; |
75 | char template[256]; | | 78 | char template[256]; |
76 | int fd; | | 79 | int fd; |
77 | | | 80 | |
78 | snprintf(template, sizeof(template), "netpgpvmd.XXXXXX"); | | 81 | snprintf(template, sizeof(template), "netpgpvmd.XXXXXX"); |
79 | if ((fd = mkstemp(template)) < 0) { | | 82 | if ((fd = mkstemp(template)) < 0) { |
80 | fprintf(stderr, "can't mkstemp %s\n", template); | | 83 | fprintf(stderr, "can't mkstemp %s\n", template); |
81 | return 0; | | 84 | return 0; |
82 | } | | 85 | } |
83 | for (cc = 0 ; cc < insize ; cc += wc) { | | 86 | for (cc = 0 ; cc < insize ; cc += wc) { |
84 | if ((wc = write(fd, &mem[cc], insize - cc)) <= 0) { | | 87 | if ((wc = write(fd, &mem[cc], insize - cc)) <= 0) { |
85 | fprintf(stderr, "short write\n"); | | 88 | fprintf(stderr, "short write\n"); |
86 | break; | | 89 | break; |
87 | } | | 90 | } |
88 | } | | 91 | } |
89 | close(fd); | | 92 | close(fd); |
90 | return 1; | | 93 | return 1; |
91 | } | | 94 | } |
92 | #endif | | 95 | #endif |
93 | | | 96 | |
94 | /* return non-zero if this is actually an armored piece already */ | | 97 | /* return non-zero if this is actually an armored piece already */ |
95 | static int | | 98 | static int |
96 | already_armored(uint8_t *in, size_t insize) | | 99 | already_armored(uint8_t *in, size_t insize) |
97 | { | | 100 | { |
98 | uint8_t *from; | | 101 | uint8_t *from; |
99 | uint8_t *p; | | 102 | uint8_t *p; |
100 | | | 103 | |
101 | for (from = in ; (p = memchr(from, '\n', insize - (size_t)(from - in))) != NULL ; from = p + 1) { | | 104 | for (from = in ; (p = memchr(from, '\n', insize - (size_t)(from - in))) != NULL ; from = p + 1) { |
102 | if (*(p - 1) != '\r') { | | 105 | if (*(p - 1) != '\r') { |
103 | return 0; | | 106 | return 0; |
104 | } | | 107 | } |
105 | } | | 108 | } |
106 | return 1; | | 109 | return 1; |
107 | } | | 110 | } |
108 | | | 111 | |
109 | /* calculate the checksum for the data we have */ | | 112 | /* calculate the checksum for the data we have */ |
110 | static int | | 113 | static int |
111 | calcsum(uint8_t *out, size_t size, uint8_t *mem, size_t cc, const uint8_t *hashed, size_t hashsize, int doarmor) | | 114 | calcsum(uint8_t *out, size_t size, uint8_t *mem, size_t cc, const uint8_t *hashed, size_t hashsize, int doarmor) |
112 | { | | 115 | { |
113 | digest_t hash; | | 116 | digest_t hash; |
114 | uint32_t len32; | | 117 | uint32_t len32; |
115 | uint16_t len16; | | 118 | uint16_t len16; |
116 | uint8_t hashalg; | | 119 | uint8_t hashalg; |
117 | uint8_t trailer[6]; | | 120 | uint8_t trailer[6]; |
118 | | | 121 | |
119 | USE_ARG(size); | | 122 | USE_ARG(size); |
120 | /* hashed data is non-null (previously checked) */ | | 123 | /* hashed data is non-null (previously checked) */ |
121 | hashalg = hashed[3]; | | 124 | hashalg = hashed[3]; |
122 | memcpy(&len16, &hashed[4], sizeof(len16)); | | 125 | memcpy(&len16, &hashed[4], sizeof(len16)); |
123 | len32 = pgp_ntoh16(len16) + 6; | | 126 | len32 = pgp_ntoh16(len16) + 6; |
124 | len32 = pgp_hton32(len32); | | 127 | len32 = pgp_hton32(len32); |
125 | trailer[0] = 0x04; | | 128 | trailer[0] = 0x04; |
126 | trailer[1] = 0xff; | | 129 | trailer[1] = 0xff; |
127 | memcpy(&trailer[2], &len32, sizeof(len32)); | | 130 | memcpy(&trailer[2], &len32, sizeof(len32)); |
128 | #ifdef NETPGPV_DEBUG | | 131 | #ifdef NETPGPV_DEBUG |
129 | writefile(mem, cc); | | 132 | writefile(mem, cc); |
130 | #endif | | 133 | #endif |
131 | digest_init(&hash, (const unsigned)hashalg); | | 134 | digest_init(&hash, (const unsigned)hashalg); |
132 | if (strchr("tw", doarmor) != NULL && !already_armored(mem, cc)) { | | 135 | if (strchr("tw", doarmor) != NULL && !already_armored(mem, cc)) { |
133 | /* this took me ages to find - something causes gpg to truncate its input */ | | 136 | /* this took me ages to find - something causes gpg to truncate its input */ |
134 | don_armor(&hash, mem, cc - 1, doarmor); | | 137 | don_armor(&hash, mem, cc - 1, doarmor); |
135 | } else { | | 138 | } else { |
136 | digest_update(&hash, mem, cc); | | 139 | digest_update(&hash, mem, cc); |
137 | } | | 140 | } |
138 | if (hashed) { | | 141 | if (hashed) { |
139 | digest_update(&hash, hashed, hashsize); | | 142 | digest_update(&hash, hashed, hashsize); |
140 | } | | 143 | } |
141 | digest_update(&hash, trailer, sizeof(trailer)); | | 144 | digest_update(&hash, trailer, sizeof(trailer)); |
142 | return digest_final(out, &hash); | | 145 | return digest_final(out, &hash); |
143 | } | | 146 | } |
144 | | | 147 | |
145 | /* used to byteswap 16 bit words */ | | 148 | /* used to byteswap 16 bit words */ |
146 | typedef union { | | 149 | typedef union { |
147 | uint16_t i16; | | 150 | uint16_t i16; |
148 | uint8_t i8[2]; | | 151 | uint8_t i8[2]; |
149 | } u16; | | 152 | } u16; |
150 | | | 153 | |
151 | /* used to byte swap 32 bit words */ | | 154 | /* used to byte swap 32 bit words */ |
152 | typedef union { | | 155 | typedef union { |
153 | uint32_t i32; | | 156 | uint32_t i32; |
154 | uint8_t i8[4]; | | 157 | uint8_t i8[4]; |
155 | } u32; | | 158 | } u32; |
156 | | | 159 | |
157 | static inline uint16_t | | 160 | static inline uint16_t |
158 | swap16(uint16_t in) | | 161 | swap16(uint16_t in) |
159 | { | | 162 | { |
160 | u16 u; | | 163 | u16 u; |
161 | | | 164 | |
162 | u.i16 = in; | | 165 | u.i16 = in; |
163 | return (u.i8[0] << 8) | u.i8[1]; | | 166 | return (u.i8[0] << 8) | u.i8[1]; |
164 | } | | 167 | } |
165 | | | 168 | |
166 | static inline uint32_t | | 169 | static inline uint32_t |
167 | swap32(uint32_t in) | | 170 | swap32(uint32_t in) |
168 | { | | 171 | { |
169 | u32 u; | | 172 | u32 u; |
170 | | | 173 | |
171 | u.i32 = in; | | 174 | u.i32 = in; |
172 | return (u.i8[0] << 24) | (u.i8[1] << 16) | (u.i8[2] << 8) | u.i8[3]; | | 175 | return (u.i8[0] << 24) | (u.i8[1] << 16) | (u.i8[2] << 8) | u.i8[3]; |
173 | } | | 176 | } |
174 | | | 177 | |
175 | static inline int | | 178 | static inline int |
176 | is_little_endian(void) | | 179 | is_little_endian(void) |
177 | { | | 180 | { |
178 | static const int indian = 1; | | 181 | static const int indian = 1; |
179 | | | 182 | |
180 | return (*(const char *)(const void *)&indian != 0); | | 183 | return (*(const char *)(const void *)&indian != 0); |
181 | } | | 184 | } |
182 | | | 185 | |
183 | /************************************************************/ | | 186 | /************************************************************/ |
184 | | | 187 | |
185 | /* exportable routines */ | | 188 | /* exportable routines */ |
186 | | | 189 | |
187 | /* open the file, mmap it, and then get the checksum on that */ | | 190 | /* open the file, mmap it, and then get the checksum on that */ |
188 | int | | 191 | int |
189 | pgpv_digest_file(uint8_t *data, size_t size, const char *name, const uint8_t *hashed, size_t hashsize, int doarmor) | | 192 | pgpv_digest_file(uint8_t *data, size_t size, const char *name, const uint8_t *hashed, size_t hashsize, int doarmor) |
190 | { | | 193 | { |
191 | struct stat st; | | 194 | struct stat st; |
192 | uint8_t *mem; | | 195 | uint8_t *mem; |
193 | size_t cc; | | 196 | size_t cc; |
194 | FILE *fp; | | 197 | FILE *fp; |
195 | int ret; | | 198 | int ret; |
196 | | | 199 | |
197 | if (hashed == NULL || data == NULL || name == NULL) { | | 200 | if (hashed == NULL || data == NULL || name == NULL) { |
198 | fprintf(stderr, "no hashed data provided\n"); | | 201 | fprintf(stderr, "no hashed data provided\n"); |
199 | return 0; | | 202 | return 0; |
200 | } | | 203 | } |
201 | ret = 0; | | 204 | ret = 0; |
202 | mem = NULL; | | 205 | mem = NULL; |
203 | cc = 0; | | 206 | cc = 0; |
204 | if ((fp = fopen(name, "r")) == NULL) { | | 207 | if ((fp = fopen(name, "r")) == NULL) { |
205 | fprintf(stderr, "%s - not found", name); | | 208 | fprintf(stderr, "%s - not found", name); |
206 | return 0; | | 209 | return 0; |
207 | } | | 210 | } |
208 | if (fstat(fileno(fp), &st) < 0) { | | 211 | if (fstat(fileno(fp), &st) < 0) { |
209 | fprintf(stderr, "%s - can't stat", name); | | 212 | fprintf(stderr, "%s - can't stat", name); |
210 | goto done; | | 213 | goto done; |
211 | } | | 214 | } |
212 | cc = (size_t)(st.st_size); | | 215 | cc = (size_t)(st.st_size); |
213 | if ((mem = mmap(NULL, cc, PROT_READ, MAP_SHARED, fileno(fp), 0)) == MAP_FAILED) { | | 216 | if ((mem = mmap(NULL, cc, PROT_READ, MAP_SHARED, fileno(fp), 0)) == MAP_FAILED) { |
214 | fprintf(stderr, "%s - can't mmap", name); | | 217 | fprintf(stderr, "%s - can't mmap", name); |
215 | goto done; | | 218 | goto done; |
216 | } | | 219 | } |
217 | ret = calcsum(data, size, mem, cc, hashed, hashsize, doarmor); | | 220 | ret = calcsum(data, size, mem, cc, hashed, hashsize, doarmor); |
218 | done: | | 221 | done: |
219 | if (data) { | | 222 | if (data) { |
220 | munmap(mem, cc); | | 223 | munmap(mem, cc); |
221 | } | | 224 | } |
222 | fclose(fp); | | 225 | fclose(fp); |
223 | return ret; | | 226 | return ret; |
224 | } | | 227 | } |
225 | | | 228 | |
226 | /* calculate the digest over memory too */ | | 229 | /* calculate the digest over memory too */ |
227 | int | | 230 | int |
228 | pgpv_digest_memory(uint8_t *data, size_t size, void *mem, size_t cc, const uint8_t *hashed, size_t hashsize, int doarmor) | | 231 | pgpv_digest_memory(uint8_t *data, size_t size, void *mem, size_t cc, const uint8_t *hashed, size_t hashsize, int doarmor) |
229 | { | | 232 | { |
230 | if (hashed == NULL || data == NULL || mem == NULL) { | | 233 | if (hashed == NULL || data == NULL || mem == NULL) { |
231 | fprintf(stderr, "no hashed data provided\n"); | | 234 | fprintf(stderr, "no hashed data provided\n"); |
232 | return 0; | | 235 | return 0; |
233 | } | | 236 | } |
234 | return calcsum(data, size, mem, cc, hashed, hashsize, doarmor); | | 237 | return calcsum(data, size, mem, cc, hashed, hashsize, doarmor); |
235 | } | | 238 | } |
236 | | | 239 | |
237 | /* our 16bit byte swap if LE host */ | | 240 | /* our 16bit byte swap if LE host */ |
238 | uint16_t | | 241 | uint16_t |
239 | pgp_ntoh16(uint16_t in) | | 242 | pgp_ntoh16(uint16_t in) |
240 | { | | 243 | { |
241 | return (is_little_endian()) ? swap16(in) : in; | | 244 | return (is_little_endian()) ? swap16(in) : in; |
242 | } | | 245 | } |
243 | | | 246 | |
244 | /* our 16bit byte swap if LE host */ | | 247 | /* our 16bit byte swap if LE host */ |
245 | uint16_t | | 248 | uint16_t |
246 | pgp_hton16(uint16_t in) | | 249 | pgp_hton16(uint16_t in) |
247 | { | | 250 | { |
248 | return (is_little_endian()) ? swap16(in) : in; | | 251 | return (is_little_endian()) ? swap16(in) : in; |
249 | } | | 252 | } |
250 | | | 253 | |
251 | /* our 32bit byte swap if LE host */ | | 254 | /* our 32bit byte swap if LE host */ |
252 | uint32_t | | 255 | uint32_t |
253 | pgp_ntoh32(uint32_t in) | | 256 | pgp_ntoh32(uint32_t in) |
254 | { | | 257 | { |
255 | return (is_little_endian()) ? swap32(in) : in; | | 258 | return (is_little_endian()) ? swap32(in) : in; |
256 | } | | 259 | } |
257 | | | 260 | |
258 | /* our 32bit byte swap if LE host */ | | 261 | /* our 32bit byte swap if LE host */ |
259 | uint32_t | | 262 | uint32_t |
260 | pgp_hton32(uint32_t in) | | 263 | pgp_hton32(uint32_t in) |
261 | { | | 264 | { |
262 | return (is_little_endian()) ? swap32(in) : in; | | 265 | return (is_little_endian()) ? swap32(in) : in; |
263 | } | | 266 | } |