2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2014 The FreeBSD Foundation
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
44 #include <openssl/conf.h>
45 #include <openssl/evp.h>
46 #include <openssl/err.h>
47 #include <openssl/pem.h>
48 #include <openssl/pkcs7.h>
57 fprintf(stderr, "usage: uefisign -c cert -k key -o outfile [-v] file\n"
58 " uefisign -V [-c cert] [-v] file\n");
63 checked_strdup(const char *s)
74 checked_fopen(const char *path, const char *mode)
80 fp = fopen(path, mode);
87 send_chunk(const void *buf, size_t len, int pipefd)
91 ret = write(pipefd, &len, sizeof(len));
92 if (ret != sizeof(len))
94 ret = write(pipefd, buf, len);
95 if (ret != (ssize_t)len)
100 receive_chunk(void **bufp, size_t *lenp, int pipefd)
106 ret = read(pipefd, &len, sizeof(len));
107 if (ret != sizeof(len))
110 buf = calloc(1, len);
114 ret = read(pipefd, buf, len);
115 if (ret != (ssize_t)len)
123 bin2hex(const char *bin, size_t bin_len)
125 unsigned char *hex, *tmp, ch;
129 hex_len = bin_len * 2 + 1; /* +1 for '\0'. */
130 hex = malloc(hex_len);
135 for (i = 0; i < bin_len; i++) {
137 tmp += sprintf(tmp, "%02x", ch);
144 * We need to replace a standard chunk of PKCS7 signature with one mandated
145 * by Authenticode. Problem is, replacing it just like that and then calling
146 * PKCS7_final() would make OpenSSL segfault somewhere in PKCS7_dataFinal().
147 * So, instead, we call PKCS7_dataInit(), then put our Authenticode-specific
148 * data into BIO it returned, then call PKCS7_dataFinal() - which now somehow
149 * does not panic - and _then_ we replace it in the signature. This technique
150 * was used in sbsigntool by Jeremy Kerr, and might have originated in
154 magic(PKCS7 *pkcs7, const char *digest, size_t digest_len)
160 unsigned char *buf, *tmp;
161 char *digest_hex, *magic_conf, *str;
164 digest_hex = bin2hex(digest, digest_len);
167 * Construct the SpcIndirectDataContent chunk.
169 nid = OBJ_create("1.3.6.1.4.1.311.2.1.4", NULL, NULL);
171 asprintf(&magic_conf, magic_fmt, digest_hex);
172 if (magic_conf == NULL)
175 bio = BIO_new_mem_buf((void *)magic_conf, -1);
177 ERR_print_errors_fp(stderr);
178 errx(1, "BIO_new_mem_buf(3) failed");
181 cnf = NCONF_new(NULL);
183 ERR_print_errors_fp(stderr);
184 errx(1, "NCONF_new(3) failed");
187 ok = NCONF_load_bio(cnf, bio, NULL);
189 ERR_print_errors_fp(stderr);
190 errx(1, "NCONF_load_bio(3) failed");
193 str = NCONF_get_string(cnf, "default", "asn1");
195 ERR_print_errors_fp(stderr);
196 errx(1, "NCONF_get_string(3) failed");
199 t = ASN1_generate_nconf(str, cnf);
201 ERR_print_errors_fp(stderr);
202 errx(1, "ASN1_generate_nconf(3) failed");
206 * We now have our proprietary piece of ASN.1. Let's do
207 * the actual signing.
209 len = i2d_ASN1_TYPE(t, NULL);
210 tmp = buf = calloc(1, len);
213 i2d_ASN1_TYPE(t, &tmp);
216 * We now have contents of 't' stuffed into memory buffer 'buf'.
221 t_bio = PKCS7_dataInit(pkcs7, NULL);
223 ERR_print_errors_fp(stderr);
224 errx(1, "PKCS7_dataInit(3) failed");
227 BIO_write(t_bio, buf + 2, len - 2);
229 ok = PKCS7_dataFinal(pkcs7, t_bio);
231 ERR_print_errors_fp(stderr);
232 errx(1, "PKCS7_dataFinal(3) failed");
236 s = ASN1_STRING_new();
237 ASN1_STRING_set(s, buf, len);
238 ASN1_TYPE_set(t, V_ASN1_SEQUENCE, s);
240 PKCS7_set0_type_other(pkcs7->d.sign->contents, nid, t);
244 sign(X509 *cert, EVP_PKEY *key, int pipefd)
249 PKCS7_SIGNER_INFO *info;
250 void *digest, *signature;
251 size_t digest_len, signature_len;
254 assert(cert != NULL);
257 receive_chunk(&digest, &digest_len, pipefd);
259 bio = BIO_new_mem_buf(digest, digest_len);
261 ERR_print_errors_fp(stderr);
262 errx(1, "BIO_new_mem_buf(3) failed");
265 pkcs7 = PKCS7_sign(NULL, NULL, NULL, bio, PKCS7_BINARY | PKCS7_PARTIAL);
267 ERR_print_errors_fp(stderr);
268 errx(1, "PKCS7_sign(3) failed");
271 md = EVP_get_digestbyname(DIGEST);
273 ERR_print_errors_fp(stderr);
274 errx(1, "EVP_get_digestbyname(\"%s\") failed", DIGEST);
277 info = PKCS7_sign_add_signer(pkcs7, cert, key, md, 0);
279 ERR_print_errors_fp(stderr);
280 errx(1, "PKCS7_sign_add_signer(3) failed");
284 * XXX: All the signed binaries seem to have this, but where is it
285 * described in the spec?
287 PKCS7_add_signed_attribute(info, NID_pkcs9_contentType,
288 V_ASN1_OBJECT, OBJ_txt2obj("1.3.6.1.4.1.311.2.1.4", 1));
290 magic(pkcs7, digest, digest_len);
293 out = BIO_new(BIO_s_file());
294 BIO_set_fp(out, stdout, BIO_NOCLOSE);
295 PKCS7_print_ctx(out, pkcs7, 0, NULL);
297 i2d_PKCS7_bio(out, pkcs7);
300 out = BIO_new(BIO_s_mem());
302 ERR_print_errors_fp(stderr);
303 errx(1, "BIO_new(3) failed");
306 ok = i2d_PKCS7_bio(out, pkcs7);
308 ERR_print_errors_fp(stderr);
309 errx(1, "i2d_PKCS7_bio(3) failed");
312 signature_len = BIO_get_mem_data(out, &signature);
313 if (signature_len <= 0) {
314 ERR_print_errors_fp(stderr);
315 errx(1, "BIO_get_mem_data(3) failed");
318 (void)BIO_set_close(out, BIO_NOCLOSE);
321 send_chunk(signature, signature_len, pipefd);
325 wait_for_child(pid_t pid)
329 pid = waitpid(pid, &status, 0);
333 return (WEXITSTATUS(status));
337 main(int argc, char **argv)
340 bool Vflag = false, vflag = false;
341 const char *certpath = NULL, *keypath = NULL, *outpath = NULL, *inpath = NULL;
342 FILE *certfp = NULL, *keyfp = NULL;
344 EVP_PKEY *key = NULL;
348 while ((ch = getopt(argc, argv, "Vc:k:o:v")) != -1) {
354 certpath = checked_strdup(optarg);
357 keypath = checked_strdup(optarg);
360 outpath = checked_strdup(optarg);
376 if (certpath != NULL)
377 errx(1, "-V and -c are mutually exclusive");
379 errx(1, "-V and -k are mutually exclusive");
381 errx(1, "-V and -o are mutually exclusive");
383 if (certpath == NULL)
384 errx(1, "-c option is mandatory");
386 errx(1, "-k option is mandatory");
388 errx(1, "-o option is mandatory");
393 OPENSSL_config(NULL);
394 ERR_load_crypto_strings();
395 OpenSSL_add_all_algorithms();
397 error = pipe(pipefds);
406 return (child(inpath, outpath, pipefds[1], Vflag, vflag));
409 certfp = checked_fopen(certpath, "r");
410 cert = PEM_read_X509(certfp, NULL, NULL, NULL);
412 ERR_print_errors_fp(stderr);
413 errx(1, "failed to load certificate from %s", certpath);
416 keyfp = checked_fopen(keypath, "r");
417 key = PEM_read_PrivateKey(keyfp, NULL, NULL, NULL);
419 ERR_print_errors_fp(stderr);
420 errx(1, "failed to load private key from %s", keypath);
423 sign(cert, key, pipefds[0]);
426 return (wait_for_child(pid));