2 * Copyright (c) 2014 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #if __FreeBSD_version >= 1100000
36 #include <sys/capsicum.h>
38 #include <sys/capability.h>
40 #include <sys/types.h>
50 #include <openssl/evp.h>
51 #include <openssl/err.h>
52 #include <openssl/pem.h>
57 load(struct executable *x)
66 error = fstat(fd, &sb);
68 err(1, "%s: fstat", x->x_path);
72 errx(1, "%s: file is empty", x->x_path);
76 err(1, "%s: cannot malloc %zd bytes", x->x_path, len);
78 nread = fread(buf, len, 1, x->x_fp);
80 err(1, "%s: fread", x->x_path);
87 digest_range(struct executable *x, EVP_MD_CTX *mdctx, off_t off, size_t len)
91 range_check(x, off, len, "chunk");
93 ok = EVP_DigestUpdate(mdctx, x->x_buf + off, len);
95 ERR_print_errors_fp(stderr);
96 errx(1, "EVP_DigestUpdate(3) failed");
101 digest(struct executable *x)
105 size_t sum_of_bytes_hashed;
109 * Windows Authenticode Portable Executable Signature Format
110 * spec version 1.0 specifies MD5 and SHA1. However, pesign
111 * and sbsign both use SHA256, so do the same.
113 md = EVP_get_digestbyname(DIGEST);
115 ERR_print_errors_fp(stderr);
116 errx(1, "EVP_get_digestbyname(\"%s\") failed", DIGEST);
119 mdctx = EVP_MD_CTX_create();
121 ERR_print_errors_fp(stderr);
122 errx(1, "EVP_MD_CTX_create(3) failed");
125 ok = EVP_DigestInit_ex(mdctx, md, NULL);
127 ERR_print_errors_fp(stderr);
128 errx(1, "EVP_DigestInit_ex(3) failed");
132 * According to the Authenticode spec, we need to compute
133 * the digest in a rather... specific manner; see "Calculating
134 * the PE Image Hash" part of the spec for details.
136 * First, everything from 0 to before the PE checksum.
138 digest_range(x, mdctx, 0, x->x_checksum_off);
141 * Second, from after the PE checksum to before the Certificate
142 * entry in Data Directory.
144 digest_range(x, mdctx, x->x_checksum_off + x->x_checksum_len,
145 x->x_certificate_entry_off -
146 (x->x_checksum_off + x->x_checksum_len));
149 * Then, from after the Certificate entry to the end of headers.
151 digest_range(x, mdctx,
152 x->x_certificate_entry_off + x->x_certificate_entry_len,
154 (x->x_certificate_entry_off + x->x_certificate_entry_len));
157 * Then, each section in turn, as specified in the PE Section Table.
161 sum_of_bytes_hashed = x->x_headers_len;
162 for (i = 0; i < x->x_nsections; i++) {
163 digest_range(x, mdctx,
164 x->x_section_off[i], x->x_section_len[i]);
165 sum_of_bytes_hashed += x->x_section_len[i];
169 * I believe this can happen with overlapping sections.
171 if (sum_of_bytes_hashed > x->x_len)
172 errx(1, "number of bytes hashed is larger than file size");
175 * I can't really explain this one; just do what the spec says.
177 if (sum_of_bytes_hashed < x->x_len) {
178 digest_range(x, mdctx, sum_of_bytes_hashed,
179 x->x_len - (signature_size(x) + sum_of_bytes_hashed));
182 ok = EVP_DigestFinal_ex(mdctx, x->x_digest, &x->x_digest_len);
184 ERR_print_errors_fp(stderr);
185 errx(1, "EVP_DigestFinal_ex(3) failed");
188 EVP_MD_CTX_destroy(mdctx);
192 show_digest(const struct executable *x)
196 printf("computed %s digest ", DIGEST);
197 for (i = 0; i < (int)x->x_digest_len; i++)
198 printf("%02x", (unsigned char)x->x_digest[i]);
199 printf("; digest len %u\n", x->x_digest_len);
203 send_digest(const struct executable *x, int pipefd)
206 send_chunk(x->x_digest, x->x_digest_len, pipefd);
210 receive_signature(struct executable *x, int pipefd)
213 receive_chunk(&x->x_signature, &x->x_signature_len, pipefd);
217 save(struct executable *x, FILE *fp, const char *path)
222 assert(path != NULL);
224 nwritten = fwrite(x->x_buf, x->x_len, 1, fp);
226 err(1, "%s: fwrite", path);
230 child(const char *inpath, const char *outpath, int pipefd,
231 bool Vflag, bool vflag)
234 FILE *outfp = NULL, *infp = NULL;
235 struct executable *x;
237 infp = checked_fopen(inpath, "r");
239 outfp = checked_fopen(outpath, "w");
242 if (error != 0 && errno != ENOSYS)
245 x = calloc(1, sizeof(*x));
254 if (signature_size(x) == 0)
255 errx(1, "file not signed");
257 printf("file contains signature\n");
264 if (signature_size(x) != 0)
265 errx(1, "file already signed");
270 send_digest(x, pipefd);
271 receive_signature(x, pipefd);
273 save(x, outfp, outpath);