David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 1 | /* Sign a module file using the given key. |
| 2 | * |
David Howells | 9552c7a | 2016-06-14 13:18:33 +0100 | [diff] [blame] | 3 | * Copyright © 2014-2016 Red Hat, Inc. All Rights Reserved. |
David Woodhouse | 09a77a8 | 2015-09-15 16:03:36 +0100 | [diff] [blame] | 4 | * Copyright © 2015 Intel Corporation. |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 5 | * Copyright © 2016 Hewlett Packard Enterprise Development LP |
David Woodhouse | 09a77a8 | 2015-09-15 16:03:36 +0100 | [diff] [blame] | 6 | * |
| 7 | * Authors: David Howells <dhowells@redhat.com> |
| 8 | * David Woodhouse <dwmw2@infradead.org> |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 9 | * Juerg Haefliger <juerg.haefliger@hpe.com> |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
David Woodhouse | 09a77a8 | 2015-09-15 16:03:36 +0100 | [diff] [blame] | 12 | * modify it under the terms of the GNU Lesser General Public License |
| 13 | * as published by the Free Software Foundation; either version 2.1 |
| 14 | * of the licence, or (at your option) any later version. |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 15 | */ |
| 16 | #define _GNU_SOURCE |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <stdint.h> |
| 20 | #include <stdbool.h> |
| 21 | #include <string.h> |
| 22 | #include <getopt.h> |
| 23 | #include <err.h> |
| 24 | #include <arpa/inet.h> |
David Howells | 283e8ba | 2015-09-25 16:31:46 +0100 | [diff] [blame] | 25 | #include <openssl/opensslv.h> |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 26 | #include <openssl/bio.h> |
| 27 | #include <openssl/evp.h> |
| 28 | #include <openssl/pem.h> |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 29 | #include <openssl/err.h> |
David Woodhouse | 6e3e281 | 2015-07-20 21:16:29 +0100 | [diff] [blame] | 30 | #include <openssl/engine.h> |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 31 | |
David Howells | 283e8ba | 2015-09-25 16:31:46 +0100 | [diff] [blame] | 32 | /* |
Linus Torvalds | 49eba53 | 2022-06-08 13:18:39 -0700 | [diff] [blame] | 33 | * OpenSSL 3.0 deprecates the OpenSSL's ENGINE API. |
| 34 | * |
| 35 | * Remove this if/when that API is no longer used |
| 36 | */ |
| 37 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 38 | |
| 39 | /* |
David Howells | 283e8ba | 2015-09-25 16:31:46 +0100 | [diff] [blame] | 40 | * Use CMS if we have openssl-1.0.0 or newer available - otherwise we have to |
| 41 | * assume that it's not available and its header file is missing and that we |
| 42 | * should use PKCS#7 instead. Switching to the older PKCS#7 format restricts |
| 43 | * the options we have on specifying the X.509 certificate we want. |
| 44 | * |
| 45 | * Further, older versions of OpenSSL don't support manually adding signers to |
| 46 | * the PKCS#7 message so have to accept that we get a certificate included in |
| 47 | * the signature message. Nor do such older versions of OpenSSL support |
| 48 | * signing with anything other than SHA1 - so we're stuck with that if such is |
| 49 | * the case. |
| 50 | */ |
Felix Fietkau | f868801 | 2017-02-09 17:17:45 +0000 | [diff] [blame] | 51 | #if defined(LIBRESSL_VERSION_NUMBER) || \ |
| 52 | OPENSSL_VERSION_NUMBER < 0x10000000L || \ |
| 53 | defined(OPENSSL_NO_CMS) |
David Howells | 283e8ba | 2015-09-25 16:31:46 +0100 | [diff] [blame] | 54 | #define USE_PKCS7 |
| 55 | #endif |
| 56 | #ifndef USE_PKCS7 |
| 57 | #include <openssl/cms.h> |
| 58 | #else |
| 59 | #include <openssl/pkcs7.h> |
| 60 | #endif |
| 61 | |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 62 | struct module_signature { |
| 63 | uint8_t algo; /* Public-key crypto algorithm [0] */ |
| 64 | uint8_t hash; /* Digest algorithm [0] */ |
| 65 | uint8_t id_type; /* Key identifier type [PKEY_ID_PKCS7] */ |
| 66 | uint8_t signer_len; /* Length of signer's name [0] */ |
| 67 | uint8_t key_id_len; /* Length of key identifier [0] */ |
| 68 | uint8_t __pad[3]; |
| 69 | uint32_t sig_len; /* Length of signature data */ |
| 70 | }; |
| 71 | |
| 72 | #define PKEY_ID_PKCS7 2 |
| 73 | |
| 74 | static char magic_number[] = "~Module signature appended~\n"; |
| 75 | |
| 76 | static __attribute__((noreturn)) |
| 77 | void format(void) |
| 78 | { |
| 79 | fprintf(stderr, |
| 80 | "Usage: scripts/sign-file [-dp] <hash algo> <key> <x509> <module> [<dest>]\n"); |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 81 | fprintf(stderr, |
| 82 | " scripts/sign-file -s <raw sig> <hash algo> <x509> <module> [<dest>]\n"); |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 83 | exit(2); |
| 84 | } |
| 85 | |
| 86 | static void display_openssl_errors(int l) |
| 87 | { |
| 88 | const char *file; |
| 89 | char buf[120]; |
| 90 | int e, line; |
| 91 | |
| 92 | if (ERR_peek_error() == 0) |
| 93 | return; |
| 94 | fprintf(stderr, "At main.c:%d:\n", l); |
| 95 | |
| 96 | while ((e = ERR_get_error_line(&file, &line))) { |
| 97 | ERR_error_string(e, buf); |
| 98 | fprintf(stderr, "- SSL %s: %s:%d\n", buf, file, line); |
| 99 | } |
| 100 | } |
| 101 | |
Lee Jones | fad1770 | 2021-10-07 11:32:12 +0100 | [diff] [blame] | 102 | #ifndef OPENSSL_NO_ENGINE |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 103 | static void drain_openssl_errors(void) |
| 104 | { |
| 105 | const char *file; |
| 106 | int line; |
| 107 | |
| 108 | if (ERR_peek_error() == 0) |
| 109 | return; |
| 110 | while (ERR_get_error_line(&file, &line)) {} |
| 111 | } |
Lee Jones | fad1770 | 2021-10-07 11:32:12 +0100 | [diff] [blame] | 112 | #endif |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 113 | |
| 114 | #define ERR(cond, fmt, ...) \ |
| 115 | do { \ |
| 116 | bool __cond = (cond); \ |
| 117 | display_openssl_errors(__LINE__); \ |
| 118 | if (__cond) { \ |
| 119 | err(1, fmt, ## __VA_ARGS__); \ |
| 120 | } \ |
| 121 | } while(0) |
| 122 | |
David Woodhouse | af1eb29 | 2015-07-20 21:16:28 +0100 | [diff] [blame] | 123 | static const char *key_pass; |
| 124 | |
| 125 | static int pem_pw_cb(char *buf, int len, int w, void *v) |
| 126 | { |
| 127 | int pwlen; |
| 128 | |
| 129 | if (!key_pass) |
| 130 | return -1; |
| 131 | |
| 132 | pwlen = strlen(key_pass); |
| 133 | if (pwlen >= len) |
| 134 | return -1; |
| 135 | |
| 136 | strcpy(buf, key_pass); |
| 137 | |
| 138 | /* If it's wrong, don't keep trying it. */ |
| 139 | key_pass = NULL; |
| 140 | |
| 141 | return pwlen; |
| 142 | } |
| 143 | |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 144 | static EVP_PKEY *read_private_key(const char *private_key_name) |
| 145 | { |
| 146 | EVP_PKEY *private_key; |
Lee Jones | e9d3963 | 2021-10-05 14:33:06 +0100 | [diff] [blame] | 147 | BIO *b; |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 148 | |
Lee Jones | e9d3963 | 2021-10-05 14:33:06 +0100 | [diff] [blame] | 149 | #ifndef OPENSSL_NO_ENGINE |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 150 | if (!strncmp(private_key_name, "pkcs11:", 7)) { |
| 151 | ENGINE *e; |
| 152 | |
| 153 | ENGINE_load_builtin_engines(); |
| 154 | drain_openssl_errors(); |
| 155 | e = ENGINE_by_id("pkcs11"); |
| 156 | ERR(!e, "Load PKCS#11 ENGINE"); |
| 157 | if (ENGINE_init(e)) |
| 158 | drain_openssl_errors(); |
| 159 | else |
| 160 | ERR(1, "ENGINE_init"); |
| 161 | if (key_pass) |
| 162 | ERR(!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0), |
| 163 | "Set PKCS#11 PIN"); |
| 164 | private_key = ENGINE_load_private_key(e, private_key_name, |
| 165 | NULL, NULL); |
| 166 | ERR(!private_key, "%s", private_key_name); |
Lee Jones | e9d3963 | 2021-10-05 14:33:06 +0100 | [diff] [blame] | 167 | return private_key; |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 168 | } |
Lee Jones | e9d3963 | 2021-10-05 14:33:06 +0100 | [diff] [blame] | 169 | #endif |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 170 | |
Lee Jones | e9d3963 | 2021-10-05 14:33:06 +0100 | [diff] [blame] | 171 | b = BIO_new_file(private_key_name, "rb"); |
| 172 | ERR(!b, "%s", private_key_name); |
| 173 | private_key = PEM_read_bio_PrivateKey(b, NULL, pem_pw_cb, |
| 174 | NULL); |
| 175 | ERR(!private_key, "%s", private_key_name); |
| 176 | BIO_free(b); |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 177 | return private_key; |
| 178 | } |
| 179 | |
| 180 | static X509 *read_x509(const char *x509_name) |
| 181 | { |
David Howells | 9552c7a | 2016-06-14 13:18:33 +0100 | [diff] [blame] | 182 | unsigned char buf[2]; |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 183 | X509 *x509; |
| 184 | BIO *b; |
David Howells | 9552c7a | 2016-06-14 13:18:33 +0100 | [diff] [blame] | 185 | int n; |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 186 | |
| 187 | b = BIO_new_file(x509_name, "rb"); |
| 188 | ERR(!b, "%s", x509_name); |
David Howells | 9552c7a | 2016-06-14 13:18:33 +0100 | [diff] [blame] | 189 | |
| 190 | /* Look at the first two bytes of the file to determine the encoding */ |
| 191 | n = BIO_read(b, buf, 2); |
| 192 | if (n != 2) { |
| 193 | if (BIO_should_retry(b)) { |
| 194 | fprintf(stderr, "%s: Read wanted retry\n", x509_name); |
| 195 | exit(1); |
| 196 | } |
| 197 | if (n >= 0) { |
| 198 | fprintf(stderr, "%s: Short read\n", x509_name); |
| 199 | exit(1); |
| 200 | } |
| 201 | ERR(1, "%s", x509_name); |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 202 | } |
David Howells | 9552c7a | 2016-06-14 13:18:33 +0100 | [diff] [blame] | 203 | |
| 204 | ERR(BIO_reset(b) != 0, "%s", x509_name); |
| 205 | |
| 206 | if (buf[0] == 0x30 && buf[1] >= 0x81 && buf[1] <= 0x84) |
| 207 | /* Assume raw DER encoded X.509 */ |
| 208 | x509 = d2i_X509_bio(b, NULL); |
| 209 | else |
| 210 | /* Assume PEM encoded X.509 */ |
| 211 | x509 = PEM_read_bio_X509(b, NULL, NULL, NULL); |
| 212 | |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 213 | BIO_free(b); |
| 214 | ERR(!x509, "%s", x509_name); |
| 215 | |
| 216 | return x509; |
| 217 | } |
| 218 | |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 219 | int main(int argc, char **argv) |
| 220 | { |
| 221 | struct module_signature sig_info = { .id_type = PKEY_ID_PKCS7 }; |
| 222 | char *hash_algo = NULL; |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 223 | char *private_key_name = NULL, *raw_sig_name = NULL; |
| 224 | char *x509_name, *module_name, *dest_name; |
David Howells | 283e8ba | 2015-09-25 16:31:46 +0100 | [diff] [blame] | 225 | bool save_sig = false, replace_orig; |
Luis R. Rodriguez | 23dfbba | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 226 | bool sign_only = false; |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 227 | bool raw_sig = false; |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 228 | unsigned char buf[4096]; |
David Howells | 283e8ba | 2015-09-25 16:31:46 +0100 | [diff] [blame] | 229 | unsigned long module_size, sig_size; |
| 230 | unsigned int use_signed_attrs; |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 231 | const EVP_MD *digest_algo; |
| 232 | EVP_PKEY *private_key; |
David Howells | 283e8ba | 2015-09-25 16:31:46 +0100 | [diff] [blame] | 233 | #ifndef USE_PKCS7 |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 234 | CMS_ContentInfo *cms = NULL; |
David Howells | 283e8ba | 2015-09-25 16:31:46 +0100 | [diff] [blame] | 235 | unsigned int use_keyid = 0; |
| 236 | #else |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 237 | PKCS7 *pkcs7 = NULL; |
David Howells | 283e8ba | 2015-09-25 16:31:46 +0100 | [diff] [blame] | 238 | #endif |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 239 | X509 *x509; |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 240 | BIO *bd, *bm; |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 241 | int opt, n; |
David Woodhouse | af1eb29 | 2015-07-20 21:16:28 +0100 | [diff] [blame] | 242 | OpenSSL_add_all_algorithms(); |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 243 | ERR_load_crypto_strings(); |
| 244 | ERR_clear_error(); |
| 245 | |
David Woodhouse | af1eb29 | 2015-07-20 21:16:28 +0100 | [diff] [blame] | 246 | key_pass = getenv("KBUILD_SIGN_PIN"); |
| 247 | |
David Howells | 283e8ba | 2015-09-25 16:31:46 +0100 | [diff] [blame] | 248 | #ifndef USE_PKCS7 |
| 249 | use_signed_attrs = CMS_NOATTR; |
| 250 | #else |
| 251 | use_signed_attrs = PKCS7_NOATTR; |
| 252 | #endif |
| 253 | |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 254 | do { |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 255 | opt = getopt(argc, argv, "sdpk"); |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 256 | switch (opt) { |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 257 | case 's': raw_sig = true; break; |
David Howells | 283e8ba | 2015-09-25 16:31:46 +0100 | [diff] [blame] | 258 | case 'p': save_sig = true; break; |
| 259 | case 'd': sign_only = true; save_sig = true; break; |
| 260 | #ifndef USE_PKCS7 |
David Howells | ed8c207 | 2015-07-20 21:16:33 +0100 | [diff] [blame] | 261 | case 'k': use_keyid = CMS_USE_KEYID; break; |
David Howells | 283e8ba | 2015-09-25 16:31:46 +0100 | [diff] [blame] | 262 | #endif |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 263 | case -1: break; |
| 264 | default: format(); |
| 265 | } |
| 266 | } while (opt != -1); |
| 267 | |
| 268 | argc -= optind; |
| 269 | argv += optind; |
| 270 | if (argc < 4 || argc > 5) |
| 271 | format(); |
| 272 | |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 273 | if (raw_sig) { |
| 274 | raw_sig_name = argv[0]; |
| 275 | hash_algo = argv[1]; |
| 276 | } else { |
| 277 | hash_algo = argv[0]; |
| 278 | private_key_name = argv[1]; |
| 279 | } |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 280 | x509_name = argv[2]; |
| 281 | module_name = argv[3]; |
Alex Yashchenko | efcae7c | 2016-12-13 09:26:25 +0000 | [diff] [blame] | 282 | if (argc == 5 && strcmp(argv[3], argv[4]) != 0) { |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 283 | dest_name = argv[4]; |
| 284 | replace_orig = false; |
| 285 | } else { |
| 286 | ERR(asprintf(&dest_name, "%s.~signed~", module_name) < 0, |
| 287 | "asprintf"); |
| 288 | replace_orig = true; |
| 289 | } |
| 290 | |
David Howells | 283e8ba | 2015-09-25 16:31:46 +0100 | [diff] [blame] | 291 | #ifdef USE_PKCS7 |
| 292 | if (strcmp(hash_algo, "sha1") != 0) { |
| 293 | fprintf(stderr, "sign-file: %s only supports SHA1 signing\n", |
| 294 | OPENSSL_VERSION_TEXT); |
| 295 | exit(3); |
| 296 | } |
| 297 | #endif |
| 298 | |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 299 | /* Open the module file */ |
| 300 | bm = BIO_new_file(module_name, "rb"); |
| 301 | ERR(!bm, "%s", module_name); |
David Woodhouse | 6e3e281 | 2015-07-20 21:16:29 +0100 | [diff] [blame] | 302 | |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 303 | if (!raw_sig) { |
| 304 | /* Read the private key and the X.509 cert the PKCS#7 message |
| 305 | * will point to. |
| 306 | */ |
| 307 | private_key = read_private_key(private_key_name); |
| 308 | x509 = read_x509(x509_name); |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 309 | |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 310 | /* Digest the module data. */ |
| 311 | OpenSSL_add_all_digests(); |
| 312 | display_openssl_errors(__LINE__); |
| 313 | digest_algo = EVP_get_digestbyname(hash_algo); |
| 314 | ERR(!digest_algo, "EVP_get_digestbyname"); |
| 315 | |
| 316 | #ifndef USE_PKCS7 |
| 317 | /* Load the signature message from the digest buffer. */ |
| 318 | cms = CMS_sign(NULL, NULL, NULL, NULL, |
| 319 | CMS_NOCERTS | CMS_PARTIAL | CMS_BINARY | |
| 320 | CMS_DETACHED | CMS_STREAM); |
| 321 | ERR(!cms, "CMS_sign"); |
| 322 | |
| 323 | ERR(!CMS_add1_signer(cms, x509, private_key, digest_algo, |
| 324 | CMS_NOCERTS | CMS_BINARY | |
| 325 | CMS_NOSMIMECAP | use_keyid | |
| 326 | use_signed_attrs), |
| 327 | "CMS_add1_signer"); |
Yusong Gao | a5ab70a | 2023-12-13 10:31:10 +0000 | [diff] [blame] | 328 | ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) != 1, |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 329 | "CMS_final"); |
| 330 | |
| 331 | #else |
| 332 | pkcs7 = PKCS7_sign(x509, private_key, NULL, bm, |
| 333 | PKCS7_NOCERTS | PKCS7_BINARY | |
| 334 | PKCS7_DETACHED | use_signed_attrs); |
| 335 | ERR(!pkcs7, "PKCS7_sign"); |
| 336 | #endif |
| 337 | |
| 338 | if (save_sig) { |
| 339 | char *sig_file_name; |
| 340 | BIO *b; |
| 341 | |
| 342 | ERR(asprintf(&sig_file_name, "%s.p7s", module_name) < 0, |
| 343 | "asprintf"); |
| 344 | b = BIO_new_file(sig_file_name, "wb"); |
| 345 | ERR(!b, "%s", sig_file_name); |
| 346 | #ifndef USE_PKCS7 |
Yusong Gao | a5ab70a | 2023-12-13 10:31:10 +0000 | [diff] [blame] | 347 | ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) != 1, |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 348 | "%s", sig_file_name); |
| 349 | #else |
Yusong Gao | a5ab70a | 2023-12-13 10:31:10 +0000 | [diff] [blame] | 350 | ERR(i2d_PKCS7_bio(b, pkcs7) != 1, |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 351 | "%s", sig_file_name); |
| 352 | #endif |
| 353 | BIO_free(b); |
| 354 | } |
| 355 | |
| 356 | if (sign_only) { |
| 357 | BIO_free(bm); |
| 358 | return 0; |
| 359 | } |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 360 | } |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 361 | |
| 362 | /* Open the destination file now so that we can shovel the module data |
| 363 | * across as we read it. |
| 364 | */ |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 365 | bd = BIO_new_file(dest_name, "wb"); |
| 366 | ERR(!bd, "%s", dest_name); |
Luis R. Rodriguez | 23dfbba | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 367 | |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 368 | /* Append the marker and the PKCS#7 message to the destination file */ |
| 369 | ERR(BIO_reset(bm) < 0, "%s", module_name); |
| 370 | while ((n = BIO_read(bm, buf, sizeof(buf))), |
| 371 | n > 0) { |
| 372 | ERR(BIO_write(bd, buf, n) < 0, "%s", dest_name); |
| 373 | } |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 374 | BIO_free(bm); |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 375 | ERR(n < 0, "%s", module_name); |
| 376 | module_size = BIO_number_written(bd); |
| 377 | |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 378 | if (!raw_sig) { |
David Howells | 283e8ba | 2015-09-25 16:31:46 +0100 | [diff] [blame] | 379 | #ifndef USE_PKCS7 |
Yusong Gao | a5ab70a | 2023-12-13 10:31:10 +0000 | [diff] [blame] | 380 | ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) != 1, "%s", dest_name); |
David Howells | 283e8ba | 2015-09-25 16:31:46 +0100 | [diff] [blame] | 381 | #else |
Yusong Gao | a5ab70a | 2023-12-13 10:31:10 +0000 | [diff] [blame] | 382 | ERR(i2d_PKCS7_bio(bd, pkcs7) != 1, "%s", dest_name); |
David Howells | 283e8ba | 2015-09-25 16:31:46 +0100 | [diff] [blame] | 383 | #endif |
Juerg Haefliger | e5a2e3c | 2016-02-04 12:09:25 +0100 | [diff] [blame] | 384 | } else { |
| 385 | BIO *b; |
| 386 | |
| 387 | /* Read the raw signature file and write the data to the |
| 388 | * destination file |
| 389 | */ |
| 390 | b = BIO_new_file(raw_sig_name, "rb"); |
| 391 | ERR(!b, "%s", raw_sig_name); |
| 392 | while ((n = BIO_read(b, buf, sizeof(buf))), n > 0) |
| 393 | ERR(BIO_write(bd, buf, n) < 0, "%s", dest_name); |
| 394 | BIO_free(b); |
| 395 | } |
| 396 | |
David Howells | 283e8ba | 2015-09-25 16:31:46 +0100 | [diff] [blame] | 397 | sig_size = BIO_number_written(bd) - module_size; |
| 398 | sig_info.sig_len = htonl(sig_size); |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 399 | ERR(BIO_write(bd, &sig_info, sizeof(sig_info)) < 0, "%s", dest_name); |
| 400 | ERR(BIO_write(bd, magic_number, sizeof(magic_number) - 1) < 0, "%s", dest_name); |
| 401 | |
Yusong Gao | a5ab70a | 2023-12-13 10:31:10 +0000 | [diff] [blame] | 402 | ERR(BIO_free(bd) != 1, "%s", dest_name); |
David Howells | bc1c373 | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 403 | |
| 404 | /* Finally, if we're signing in place, replace the original. */ |
| 405 | if (replace_orig) |
| 406 | ERR(rename(dest_name, module_name) < 0, "%s", dest_name); |
| 407 | |
| 408 | return 0; |
| 409 | } |