Skip to content

Commit d7afd65

Browse files
petrpavlusamitolvanen
authored andcommitted
sign-file: Use only the OpenSSL CMS API for signing
The USE_PKCS7 code in sign-file utilizes PKCS7_sign(), which allows signing only with SHA-1. Since SHA-1 support for module signing has been removed, drop the use of the OpenSSL PKCS7 API by the tool in favor of using only the newer CMS API. The use of the PKCS7 API is selected by the following: #if defined(LIBRESSL_VERSION_NUMBER) || \ OPENSSL_VERSION_NUMBER < 0x10000000L || \ defined(OPENSSL_NO_CMS) #define USE_PKCS7 #endif Looking at the individual ifdefs: * LIBRESSL_VERSION_NUMBER: LibreSSL added the CMS API implementation from OpenSSL in 3.1.0, making the ifdef no longer relevant. This version was released on April 8, 2020. * OPENSSL_VERSION_NUMBER < 0x10000000L: OpenSSL 1.0.0 was released on March 29, 2010. Supporting earlier versions should no longer be necessary. The file Documentation/process/changes.rst already states that at least version 1.0.0 is required to build the kernel. * OPENSSL_NO_CMS: OpenSSL can be configured with "no-cms" to disable CMS support. In this case, sign-file will no longer be usable. The CMS API support is now required. In practice, since distributions now typically sign modules with SHA-2, for which sign-file already required CMS API support, removing the USE_PKCS7 code shouldn't cause any issues. Signed-off-by: Petr Pavlu <petr.pavlu@suse.com> Reviewed-by: Sami Tolvanen <samitolvanen@google.com> [Sami: Used Petr's updated commit message] Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
1 parent 148519a commit d7afd65

1 file changed

Lines changed: 3 additions & 63 deletions

File tree

scripts/sign-file.c

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <arpa/inet.h>
2525
#include <openssl/opensslv.h>
2626
#include <openssl/bio.h>
27+
#include <openssl/cms.h>
2728
#include <openssl/evp.h>
2829
#include <openssl/pem.h>
2930
#include <openssl/err.h>
@@ -39,29 +40,6 @@
3940
#endif
4041
#include "ssl-common.h"
4142

42-
/*
43-
* Use CMS if we have openssl-1.0.0 or newer available - otherwise we have to
44-
* assume that it's not available and its header file is missing and that we
45-
* should use PKCS#7 instead. Switching to the older PKCS#7 format restricts
46-
* the options we have on specifying the X.509 certificate we want.
47-
*
48-
* Further, older versions of OpenSSL don't support manually adding signers to
49-
* the PKCS#7 message so have to accept that we get a certificate included in
50-
* the signature message. Nor do such older versions of OpenSSL support
51-
* signing with anything other than SHA1 - so we're stuck with that if such is
52-
* the case.
53-
*/
54-
#if defined(LIBRESSL_VERSION_NUMBER) || \
55-
OPENSSL_VERSION_NUMBER < 0x10000000L || \
56-
defined(OPENSSL_NO_CMS)
57-
#define USE_PKCS7
58-
#endif
59-
#ifndef USE_PKCS7
60-
#include <openssl/cms.h>
61-
#else
62-
#include <openssl/pkcs7.h>
63-
#endif
64-
6543
struct module_signature {
6644
uint8_t algo; /* Public-key crypto algorithm [0] */
6745
uint8_t hash; /* Digest algorithm [0] */
@@ -228,15 +206,10 @@ int main(int argc, char **argv)
228206
bool raw_sig = false;
229207
unsigned char buf[4096];
230208
unsigned long module_size, sig_size;
231-
unsigned int use_signed_attrs;
232209
const EVP_MD *digest_algo;
233210
EVP_PKEY *private_key;
234-
#ifndef USE_PKCS7
235211
CMS_ContentInfo *cms = NULL;
236212
unsigned int use_keyid = 0;
237-
#else
238-
PKCS7 *pkcs7 = NULL;
239-
#endif
240213
X509 *x509;
241214
BIO *bd, *bm;
242215
int opt, n;
@@ -246,21 +219,13 @@ int main(int argc, char **argv)
246219

247220
key_pass = getenv("KBUILD_SIGN_PIN");
248221

249-
#ifndef USE_PKCS7
250-
use_signed_attrs = CMS_NOATTR;
251-
#else
252-
use_signed_attrs = PKCS7_NOATTR;
253-
#endif
254-
255222
do {
256223
opt = getopt(argc, argv, "sdpk");
257224
switch (opt) {
258225
case 's': raw_sig = true; break;
259226
case 'p': save_sig = true; break;
260227
case 'd': sign_only = true; save_sig = true; break;
261-
#ifndef USE_PKCS7
262228
case 'k': use_keyid = CMS_USE_KEYID; break;
263-
#endif
264229
case -1: break;
265230
default: format();
266231
}
@@ -289,14 +254,6 @@ int main(int argc, char **argv)
289254
replace_orig = true;
290255
}
291256

292-
#ifdef USE_PKCS7
293-
if (strcmp(hash_algo, "sha1") != 0) {
294-
fprintf(stderr, "sign-file: %s only supports SHA1 signing\n",
295-
OPENSSL_VERSION_TEXT);
296-
exit(3);
297-
}
298-
#endif
299-
300257
/* Open the module file */
301258
bm = BIO_new_file(module_name, "rb");
302259
ERR(!bm, "%s", module_name);
@@ -314,7 +271,6 @@ int main(int argc, char **argv)
314271
digest_algo = EVP_get_digestbyname(hash_algo);
315272
ERR(!digest_algo, "EVP_get_digestbyname");
316273

317-
#ifndef USE_PKCS7
318274
/* Load the signature message from the digest buffer. */
319275
cms = CMS_sign(NULL, NULL, NULL, NULL,
320276
CMS_NOCERTS | CMS_PARTIAL | CMS_BINARY |
@@ -323,19 +279,12 @@ int main(int argc, char **argv)
323279

324280
ERR(!CMS_add1_signer(cms, x509, private_key, digest_algo,
325281
CMS_NOCERTS | CMS_BINARY |
326-
CMS_NOSMIMECAP | use_keyid |
327-
use_signed_attrs),
282+
CMS_NOSMIMECAP | CMS_NOATTR |
283+
use_keyid),
328284
"CMS_add1_signer");
329285
ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) != 1,
330286
"CMS_final");
331287

332-
#else
333-
pkcs7 = PKCS7_sign(x509, private_key, NULL, bm,
334-
PKCS7_NOCERTS | PKCS7_BINARY |
335-
PKCS7_DETACHED | use_signed_attrs);
336-
ERR(!pkcs7, "PKCS7_sign");
337-
#endif
338-
339288
if (save_sig) {
340289
char *sig_file_name;
341290
BIO *b;
@@ -344,13 +293,8 @@ int main(int argc, char **argv)
344293
"asprintf");
345294
b = BIO_new_file(sig_file_name, "wb");
346295
ERR(!b, "%s", sig_file_name);
347-
#ifndef USE_PKCS7
348296
ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) != 1,
349297
"%s", sig_file_name);
350-
#else
351-
ERR(i2d_PKCS7_bio(b, pkcs7) != 1,
352-
"%s", sig_file_name);
353-
#endif
354298
BIO_free(b);
355299
}
356300

@@ -377,11 +321,7 @@ int main(int argc, char **argv)
377321
module_size = BIO_number_written(bd);
378322

379323
if (!raw_sig) {
380-
#ifndef USE_PKCS7
381324
ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) != 1, "%s", dest_name);
382-
#else
383-
ERR(i2d_PKCS7_bio(bd, pkcs7) != 1, "%s", dest_name);
384-
#endif
385325
} else {
386326
BIO *b;
387327

0 commit comments

Comments
 (0)