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