Debian Codegraph @

src/OVAL/probes/crapi/digest.c

raw symbols SCIP
1 /*
2 * Copyright 2010 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * "Daniel Kopecek" <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdarg.h>
28 #include <stddef.h>
29 #include <stdint.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <sys/mman.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36
37 #include "crapi.h"
38 #include "digest.h"
39
40 #if defined(HAVE_NSS3)
41 #include <sechash.h>
42 #elif defined(HAVE_GCRYPT)
43 #include <gcrypt.h>
44 #else
45 #error "No crypto library available!"
46 #endif
47
48 #if defined(HAVE_NSS3)
49 static int crapi_alg_t_to_lib_arg(crapi_alg_t alg)
50 {
51 switch (alg) {
52 #ifdef OPENSCAP_ENABLE_MD5
53 case CRAPI_DIGEST_MD5:
54 return HASH_AlgMD5;
55 #endif
56 #ifdef OPENSCAP_ENABLE_SHA1
57 case CRAPI_DIGEST_SHA1:
58 return HASH_AlgSHA1;
59 #endif
60 case CRAPI_DIGEST_SHA224:
61 return HASH_AlgSHA224;
62 case CRAPI_DIGEST_SHA256:
63 return HASH_AlgSHA256;
64 case CRAPI_DIGEST_SHA384:
65 return HASH_AlgSHA384;
66 case CRAPI_DIGEST_SHA512:
67 return HASH_AlgSHA512;
68 default:
69 return -1;
70 }
71 }
72 #elif defined(HAVE_GCRYPT)
73 static int crapi_alg_t_to_lib_arg(crapi_alg_t alg)
74 {
75 switch (alg) {
76 #ifdef OPENSCAP_ENABLE_MD5
77 case CRAPI_DIGEST_MD5:
78 return GCRY_MD_MD5;
79 #endif
80 #ifdef OPENSCAP_ENABLE_SHA1
81 case CRAPI_DIGEST_SHA1:
82 return GCRY_MD_SHA1;
83 #endif
84 case CRAPI_DIGEST_SHA224:
85 return GCRY_MD_SHA224;
86 case CRAPI_DIGEST_SHA256:
87 return GCRY_MD_SHA256;
88 case CRAPI_DIGEST_SHA384:
89 return GCRY_MD_SHA384;
90 case CRAPI_DIGEST_SHA512:
91 return GCRY_MD_SHA512;
92 case CRAPI_DIGEST_RMD160:
93 return GCRY_MD_RMD160;
94 default:
95 return -1;
96 }
97 }
98 #endif
99
100 int crapi_digest_fd(int fd, crapi_alg_t alg, void *dst, size_t *size)
101 {
102 struct stat st;
103 void *buffer;
104 size_t buflen;
105
106 if (size == NULL || dst == NULL) {
107 errno = EFAULT;
108 return -1;
109 }
110 int lib_alg = crapi_alg_t_to_lib_arg(alg);
111 #if defined(HAVE_NSS3)
112 if (*size < HASH_ResultLen(lib_alg)) {
113 #elif defined(HAVE_GCRYPT)
114 if (*size < gcry_md_get_algo_dlen(lib_alg)) {
115 #endif
116 errno = ENOBUFS;
117 return -1;
118 }
119
120 if (fstat (fd, &st) != 0)
121 return (-1);
122 else {
123 #if _FILE_OFFSET_BITS == 32
124 buflen = st.st_size;
125 # if defined(OS_FREEBSD)
126 buffer = mmap(NULL, buflen, PROT_READ, MAP_SHARED | MAP_NOCORE, fd, 0);
127 # else
128 buffer = mmap(NULL, buflen, PROT_READ, MAP_SHARED, fd, 0);
129 # endif
130 if (buffer == NULL) {
131 #endif /* _FILE_OFFSET_BITS == 32 */
132 uint8_t _buffer[CRAPI_IO_BUFSZ];
133 ssize_t ret;
134
135 buffer = _buffer;
136 #if defined(HAVE_NSS3)
137 HASHContext *ctx = HASH_Create (lib_alg);
138
139 if (ctx == NULL)
140 return (-1);
141 #elif defined(HAVE_GCRYPT)
142 gcry_md_hd_t hd;
143 gcry_md_open (&hd, lib_alg, 0);
144 #endif
145
146 while ((ret = read(fd, buffer, sizeof _buffer)) == sizeof _buffer)
147 #if defined(HAVE_NSS3)
148 HASH_Update(ctx, (const unsigned char *)buffer, (unsigned int) sizeof _buffer);
149 #elif defined(HAVE_GCRYPT)
150 gcry_md_write(hd, (const void *)buffer, sizeof _buffer);
151 #endif
152
153 switch (ret) {
154 case 0:
155 break;
156 case -1:
157 return (-1);
158 default:
159 if (ret <= 0) {
160 #if defined(HAVE_NSS3)
161 HASH_Destroy(ctx);
162 #elif defined(HAVE_GCRYPT)
163 gcry_md_close(hd);
164 #endif
165 return -1;
166 }
167 #if defined(HAVE_NSS3)
168 HASH_Update(ctx, (const unsigned char *)buffer, (unsigned int) ret);
169 #elif defined(HAVE_GCRYPT)
170 gcry_md_write(hd, (const void *)buffer, (size_t)ret);
171 #endif
172 }
173
174 #if defined(HAVE_NSS3)
175 HASH_End(ctx, dst, (unsigned int *)size, *size);
176 HASH_Destroy(ctx);
177 #elif defined(HAVE_GCRYPT)
178 gcry_md_final(hd);
179
180 buffer = (void *)gcry_md_read(hd, lib_alg);
181 memcpy (dst, buffer, gcry_md_get_algo_dlen(lib_alg));
182 gcry_md_close (hd);
183 #endif
184
185 #if _FILE_OFFSET_BITS == 32
186 } else {
187 #if defined(HAVE_NSS3)
188 HASH_HashBuf(lib_alg, (unsigned char *)dst, (unsigned char *)buffer, (unsigned int)buflen);
189 #elif defined(HAVE_GCRYPT)
190 gcry_md_hash_buffer(lib_alg, dst, (const void *)buffer, buflen);
191 #endif
192 munmap(buffer, buflen);
193 }
194 #endif /* _FILE_OFFSET_BITS == 32 */
195 }
196 return (0);
197 }
198
199 struct crapi_digest_ctx {
200 #if defined(HAVE_NSS3)
201 HASHContext *ctx;
202 #elif defined(HAVE_GCRYPT)
203 gcry_md_hd_t ctx;
204 #endif
205 void *dst;
206 size_t *size;
207 };
208
209 struct digest_ctbl_t {
210 struct crapi_digest_ctx *ctx;
211 crapi_alg_t alg;
212 };
213
214 static void *crapi_digest_init (void *dst, void *size, crapi_alg_t alg)
215 {
216 struct crapi_digest_ctx *ctx = malloc(sizeof(struct crapi_digest_ctx));
217
218 int lib_alg = crapi_alg_t_to_lib_arg(alg);
219 #if defined(HAVE_NSS3)
220 ctx->ctx = HASH_Create(lib_alg);
221 #elif defined(HAVE_GCRYPT)
222 if (gcry_md_open(&ctx->ctx, lib_alg, 0) != 0) {
223 free(ctx);
224 return NULL;
225 }
226 #endif
227 ctx->dst = dst;
228 ctx->size = size;
229
230 #if defined(HAVE_NSS3)
231 if (ctx->ctx != NULL) {
232 HASH_Begin(ctx->ctx);
233 } else {
234 free(ctx);
235 ctx = NULL;
236 }
237 #endif
238
239 return (ctx);
240 }
241
242 static int crapi_digest_update(struct crapi_digest_ctx *ctx, void *bptr, size_t blen)
243 {
244 #if defined(HAVE_NSS3)
245 HASH_Update(ctx->ctx, (const unsigned char *)bptr, (unsigned int)blen);
246 #elif defined(HAVE_GCRYPT)
247 gcry_md_write(ctx->ctx, (const void *)bptr, blen);
248 #endif
249 return (0);
250 }
251
252 static int crapi_digest_fini(struct crapi_digest_ctx *ctx, crapi_alg_t alg)
253 {
254 #if defined(HAVE_NSS3)
255 unsigned int result_len;
256 HASH_End(ctx->ctx, ctx->dst, &result_len, *ctx->size);
257 *ctx->size = result_len;
258 HASH_Destroy (ctx->ctx);
259 #elif defined(HAVE_GCRYPT)
260 void *buffer;
261
262 gcry_md_final(ctx->ctx);
263 int lib_alg = crapi_alg_t_to_lib_arg(alg);
264 buffer = (void *)gcry_md_read(ctx->ctx, lib_alg);
265 memcpy(ctx->dst, buffer, gcry_md_get_algo_dlen(lib_alg));
266 gcry_md_close(ctx->ctx);
267 #endif
268 free (ctx);
269
270 return (0);
271 }
272
273 static void crapi_digest_free(struct crapi_digest_ctx *ctx)
274 {
275 #if defined(HAVE_NSS3)
276 HASH_Destroy(ctx->ctx);
277 free(ctx);
278 #endif
279 return;
280 }
281
282 int crapi_mdigest_fd (int fd, int num, ... /* crapi_alg_t alg, void *dst, size_t *size, ...*/)
283 {
284 register int i;
285 va_list ap;
286 struct digest_ctbl_t *ctbl = malloc(num * sizeof(struct digest_ctbl_t));
287
288 crapi_alg_t alg;
289 void *dst;
290 size_t *size;
291
292 uint8_t fd_buf[CRAPI_IO_BUFSZ];
293 ssize_t ret;
294
295 if (num <= 0 || fd <= 0) {
296 errno = EINVAL;
297 free(ctbl);
298 return -1;
299 }
300 for (i = 0; i < num; ++i)
301 ctbl[i].ctx = NULL;
302
303 va_start(ap, num);
304
305 for (i = 0; i < num; ++i) {
306 alg = va_arg(ap, crapi_alg_t);
307 dst = va_arg(ap, void *);
308 size = va_arg(ap, size_t *);
309
310 ctbl[i].alg = alg;
311 if ((ctbl[i].ctx = crapi_digest_init(dst, size, alg)) == NULL)
312 *size = 0;
313 }
314
315 va_end (ap);
316
317 while ((ret = read(fd, fd_buf, sizeof fd_buf)) == sizeof fd_buf) {
318 for (i = 0; i < num; ++i) {
319 if (ctbl[i].ctx == NULL)
320 continue;
321 if (crapi_digest_update(ctbl[i].ctx, fd_buf, sizeof fd_buf) != 0) {
322 goto fail;
323 }
324 }
325 }
326
327 switch (ret) {
328 case 0:
329 break;
330 case -1:
331 goto fail;
332 default:
333 if (ret <= 0) {
334 free(ctbl);
335 return -1;
336 }
337
338 for (i = 0; i < num; ++i) {
339 if (ctbl[i].ctx == NULL)
340 continue;
341 if (crapi_digest_update(ctbl[i].ctx, fd_buf, (size_t)ret) != 0) {
342 goto fail;
343 }
344 }
345 }
346
347 for (i = 0; i < num; ++i) {
348 if (ctbl[i].ctx == NULL)
349 continue;
350 crapi_digest_fini(ctbl[i].ctx, ctbl[i].alg);
351 }
352 free(ctbl);
353 return (0);
354 fail:
355 for (i = 0; i < num; ++i) {
356 if (ctbl[i].ctx != NULL)
357 crapi_digest_free(ctbl[i].ctx);
358 }
359
360 free(ctbl);
361 return (-1);
362 }