~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/scripts/sign-file.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /scripts/sign-file.c (Version linux-6.11.5) and /scripts/sign-file.c (Version linux-5.9.16)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php