MODSIGN: Use PKCS#7 for module signatures [ver #7]
Subsystems (7)
David Howells
maintainercover MODSIGN: Use PKCS#7 for module signatures [ver #7]
Here's a set of patches that does the following:
(1) Extracts both parts of an X.509 AuthorityKeyIdentifier (AKID) extension. We already extract the bit that can match the subjectKeyIdentifier (SKID) of the parent X.509 cert, but we currently ignore the bits that can match the issuer and serialNumber.
Looks up an X.509 cert by issuer and serialNumber if those are provided in the AKID. If the keyIdentifier is also provided, checks that the subjectKeyIdentifier of the cert found matches that also.
If no issuer and serialNumber are provided in the AKID, looks up an X.509 cert by SKID using the AKID keyIdentifier.
This allows module signing to be done with certificates that don't have an SKID by which they can be looked up.
(2) Makes use of the PKCS#7 facility to provide module signatures.
sign-file is replaced with a program that generates a PKCS#7 message that has no X.509 certs embedded and that has detached data (the module content) and adds it onto the message with magic string and descriptor.
(3) The PKCS#7 message supplies all the information that is needed to select the X.509 cert to be used to verify the signature by standard means (including selection of digest algorithm and public key algorithm). No kernel-specific magic values are required.
(4) Makes it possible to get sign-file to just write out a file containing the PKCS#7 signature blob. This can be used for debugging and potentially for firmware signing.
(5) Extracts the function that does PKCS#7 signature verification on a blob from the module signing code and put it somewhere more general so that other things, such as firmware signing, can make use of it without depending on module config options.
(6) Adds support for CMS messages in place of PKCS#7 (they're very similar ASN.1) and makes sign-file create CMS messages instead of PKCS#7. This allows signatures to refer to the verifying key by X.509 cert SKID instead of X.509 cert issuer and serial number.
(7) Provides support for providing a password/pin for an encrypted private key to sign-file.
(8) Makes it possible to use PKCS#11 with sign-file, thus allowing the use of cryptographic hardware.
(9) Overhauls the way the module signing key is handled. If the name in CONFIG_MODULE_SIG_KEY is "signing_key.pem" then a key will be automatically generated and placed in the build directory. If the name is different, autogeneration is suppressed and the file is presumed to be a PEM file containing both the private key and X.509 certificate.
(10) Overhauls the way auxiliary trusted keys are added to the kernel. Files matching the pattern "*.x509" are no longer just gathered up and cat'd together. Now CONFIG_SYSTEM_TRUSTED_KEYS must be set to point to a single PEM file containing a set of X.509 certs cat'd together if this facility is desired.
(11) Severely restricts what authenticateAttributes are permitted in a PKCS#7 or CMS message and what content type may be used. This is selected by the in-kernel user with the appropriate VERIFYING_*_SIGNATURE constant.
Note that the revised sign-file program no longer supports the "-s <signature>" option to add an externally generated signature. This is deprecated in favour of using PKCS#11. Note also that the format of the signature file that would be passed to -s has changed.
David Woodhouse also has stated an intention to overhaul the makefile magic he added to deal with quotes and quoting encountered when using CONFIG_* option strings in the makefile.
The patches can be found here also:
http://git.kernel.org/cgit/linux/kernel/git/dhowells/linux-fs.git/log/?h=modsign-pkcs7
and are tagged with:
modsign-pkcs7-20150805
David
---
David Howells (17):
ASN.1: Add an ASN.1 compiler option to dump the element tree ASN.1: Copy string names to tokens in ASN.1 compiler
X.509: Extract both parts of the AuthorityKeyIdentifier X.509: Support X.509 lookup by Issuer+Serial form AuthorityKeyIdentifier PKCS#7: Allow detached data to be supplied for signature checking purposes
MODSIGN: Provide a utility to append a PKCS#7 signature to a module
MODSIGN: Use PKCS#7 messages as module signatures system_keyring.c doesn't need to #include module-internal.h
MODSIGN: Extract the blob PKCS#7 signature verifier from module signing PKCS#7: Check content type and versions
X.509: Change recorded SKID & AKID to not include Subject or Issuer PKCS#7: Support CMS messages also [RFC5652]
sign-file: Generate CMS message as signature instead of PKCS#7 PKCS#7: Improve and export the X.509 ASN.1 time object decoder PKCS#7: Appropriately require or forbid authenticated attributes
KEYS: Add a name for PKEY_ID_PKCS7
PKCS#7: Restrict content type and authenticated attributes by purpose
David Woodhouse (9):
modsign: Abort modules_install when signing fails
modsign: Allow password to be specified for signing key
modsign: Allow signing key to be PKCS#11
modsign: Allow external signing key to be specified
modsign: Extract signing cert from CONFIG_MODULE_SIG_KEY if needed
modsign: Use single PEM file for autogenerated key
modsign: Add explicit CONFIG_SYSTEM_TRUSTED_KEYS option
extract-cert: Cope with multiple X.509 certificates in a single file
modsign: Use extract-cert to process CONFIG_SYSTEM_TRUSTED_KEYS
Luis R. Rodriguez (1):
sign-file: Add option to only create signature file
.gitignore | 1 Documentation/kbuild/kbuild.txt | 5 Documentation/module-signing.txt | 51 +++- Makefile | 8 - arch/x86/kernel/kexec-bzimage64.c | 4 crypto/asymmetric_keys/Makefile | 8 - crypto/asymmetric_keys/asymmetric_type.c | 11 + crypto/asymmetric_keys/pkcs7.asn1 | 22 +- crypto/asymmetric_keys/pkcs7_key_type.c | 14 + crypto/asymmetric_keys/pkcs7_parser.c | 269 ++++++++++++++++++- crypto/asymmetric_keys/pkcs7_parser.h | 20 + crypto/asymmetric_keys/pkcs7_trust.c | 10 - crypto/asymmetric_keys/pkcs7_verify.c | 145 +++++++++- crypto/asymmetric_keys/public_key.c | 1 crypto/asymmetric_keys/verify_pefile.c | 7 crypto/asymmetric_keys/x509_akid.asn1 | 35 ++ crypto/asymmetric_keys/x509_cert_parser.c | 231 +++++++++++----- crypto/asymmetric_keys/x509_parser.h | 12 + crypto/asymmetric_keys/x509_public_key.c | 95 ++++--- include/crypto/pkcs7.h | 13 + include/crypto/public_key.h | 18 + include/keys/system_keyring.h | 7 include/linux/oid_registry.h | 4 include/linux/verify_pefile.h | 6 init/Kconfig | 55 +++- kernel/Makefile | 112 +++++--- kernel/module_signing.c | 213 +-------------- kernel/system_certificates.S | 3 kernel/system_keyring.c | 53 ++++ scripts/Makefile | 4 scripts/Makefile.modinst | 2 scripts/asn1_compiler.c | 229 ++++++++++------ scripts/extract-cert.c | 166 +++++++++++ scripts/sign-file | 421 ----------------------------- scripts/sign-file.c | 260 ++++++++++++++++++ 35 files changed, 1587 insertions(+), 928 deletions(-) create mode 100644 crypto/asymmetric_keys/x509_akid.asn1 create mode 100644 scripts/extract-cert.c delete mode 100755 scripts/sign-file create mode 100755 scripts/sign-file.c
David Howells
maintainerPATCH 1/27 ASN.1: Add an ASN.1 compiler option to dump the element tree [ver #7]
Add an ASN.1 compiler option to dump the element tree to stdout.
---
scripts/asn1_compiler.c | 88 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 76 insertions(+), 12 deletions(-)
scripts/asn1_compiler.c +76 -12
@@ -13,6 +13,7 @@#include <stdio.h> #include <stdlib.h> #include <stdint.h> +#include <stdbool.h> #include <string.h> #include <ctype.h> #include <unistd.h>
@@ -311,9 +312,11 @@ struct token {static struct token *token_list;static unsigned nr_tokens; -static _Bool verbose; +static bool verbose_opt; +static bool debug_opt; -#define debug (fmt, ...) do { if (verbose) printf(fmt, ## __VA_ARGS__); } while (0) +#define verbose (fmt, ...) do { if (verbose_opt) printf(fmt, ## __VA_ARGS__); } while (0) +#define debug (fmt, ...) do { if (debug_opt) printf(fmt, ## __VA_ARGS__); } while (0)static int directive_compare (const void *_key,const void *_pdir) {
@@ -518,7 +521,7 @@ static void tokenise(char *buffer, char *end) } nr_tokens = tix; -debug ("Extracted %u tokens\n" , nr_tokens); +verbose ("Extracted %u tokens\n" , nr_tokens);#if 0 {
@@ -534,6 +537,7 @@ static void tokenise(char *buffer, char *end)static void build_type_list (void );static void parse (void ); +static void dump_elements (void );static void render (FILE *out,FILE *hdr);/*
@@ -548,16 +552,27 @@ int main(int argc, char **argv)char *kbuild_verbose;int fd; + kbuild_verbose =getenv ("KBUILD_VERBOSE" ); +if (kbuild_verbose) + verbose_opt =atoi (kbuild_verbose); + +while (argc >4 ) { +if (strcmp (argv[1 ],"-v" ) ==0 ) + verbose_opt = true; +else if (strcmp (argv[1 ],"-d" ) ==0 ) + debug_opt = true; +else +break ; +memmove (&argv[1 ], &argv[2 ], (argc -2 ) *sizeof (char *)); + argc--; + } +if (argc !=4 ) { -fprintf (stderr,"Format: %s <grammar-file> <c-file> <hdr-file>\n" , +fprintf (stderr,"Format: %s [-v] [-d] <grammar-file> <c-file> <hdr-file>\n" , argv[0 ]);exit (2 ); } - kbuild_verbose =getenv ("KBUILD_VERBOSE" ); -if (kbuild_verbose) - verbose =atoi (kbuild_verbose); - filename = argv[1 ]; outputname = argv[2 ]; headername = argv[3 ];
@@ -608,6 +623,7 @@ int main(int argc, char **argv)tokenise (buffer, buffer + readlen);build_type_list ();parse (); +dump_elements (); out =fopen (outputname,"w" );if (!out) {
@@ -756,7 +772,7 @@ static void build_type_list(void)qsort (type_index, nr,sizeof (type_index[0 ]), type_index_compare); -debug ("Extracted %u types\n" , nr_types); +verbose ("Extracted %u types\n" , nr_types);#if 0 for (n =0 ; n < nr_types; n++) {struct type *type = type_index[n];
@@ -801,7 +817,7 @@ static void parse(void) }while (type++, !(type->flags &TYPE_STOP_MARKER )); -debug ("Extracted %u actions\n" , nr_actions); +verbose ("Extracted %u actions\n" , nr_actions); }static struct element *element_list;
@@ -1192,6 +1208,54 @@ overrun_error:exit (1 ); } +static void dump_element (const struct element *e,int level) +{ +const struct element *c; +const struct type *t = e->type_def; +const char *name = e->name ? e->name->value :"." ; +int nsize = e->name ? e->name->size :1 ; +const char *tname = t && t->name ? t->name->value :"." ; +int tnsize = t && t->name ? t->name->size :1 ; +char tag[32 ]; + +if (e->class ==0 && e->method ==0 && e->tag ==0 ) +strcpy (tag,"<...>" ); +else if (e->class ==ASN1_UNIV ) +sprintf (tag,"%s %s %s" , + asn1_classes[e->class], + asn1_methods[e->method], + asn1_universal_tags[e->tag]); +else +sprintf (tag,"%s %s %u" , + asn1_classes[e->class], + asn1_methods[e->method], + e->tag); + +printf ("%c%c%c%c%c %c %*s[*] \e[33m%s\e[m %*.*s %*.*s \e[35m%s\e[m\n" , + e->flags &ELEMENT_IMPLICIT ?'I' :'-' , + e->flags &ELEMENT_EXPLICIT ?'E' :'-' , + e->flags &ELEMENT_TAG_SPECIFIED ?'T' :'-' , + e->flags &ELEMENT_SKIPPABLE ?'S' :'-' , + e->flags &ELEMENT_CONDITIONAL ?'C' :'-' , +"-tTqQcaro" [e->compound], + level,"" , + tag, + tnsize, tnsize, tname, + nsize, nsize, name, + e->action ? e->action->name :"" ); +if (e->compound ==TYPE_REF ) +dump_element (e->type->type->element, level +3 ); +else +for (c = e->children; c; c = c->next) +dump_element (c, level +3 ); +} + +static void dump_elements (void ) +{ +if (debug_opt) +dump_element (type_list[0 ].element,0 ); +} +static void render_element (FILE *out,struct element *e,struct element *tag);static void render_out_of_line_list (FILE *out);
@@ -1293,7 +1357,7 @@ static void render(FILE *out, FILE *hdr) }/* We do two passes - the first one calculates all the offsets */ -debug ("Pass 1\n" ); +verbose ("Pass 1\n" ); nr_entries =0 ; root = &type_list[0 ];render_element (NULL , root->element,NULL );
@@ -1304,7 +1368,7 @@ static void render(FILE *out, FILE *hdr) e->flags &= ~ELEMENT_RENDERED ;/* And then we actually render */ -debug ("Pass 2\n" ); +verbose ("Pass 2\n" );fprintf (out,"\n" );fprintf (out,"static const unsigned char %s_machine[] = {\n" , grammar_name);
David Howells
maintainerPATCH 2/27 ASN.1: Copy string names to tokens in ASN.1 compiler [ver #7]
In reply to David HowellsCopy string names to tokens in ASN.1 compiler rather than storing a pointer into the source text. This means we don't have to use "%*.*s" all over the place.
---
scripts/asn1_compiler.c | 155 ++++++++++++++++++++++------------------------- 1 file changed, 73 insertions(+), 82 deletions(-)
scripts/asn1_compiler.c +73 -82
@@ -294,8 +294,8 @@ static const char *const directives[NR__DIRECTIVES] = {struct action {struct action *next; +char *name;unsigned char index; -char name[]; };static struct action *action_list;
@@ -306,7 +306,7 @@ struct token { enum token_type token_type : 8;unsigned char size;struct action *action; -const char *value; +char *content;struct type *type; };
@@ -328,11 +328,9 @@ static int directive_compare(const void *_key, const void *_pdir) dlen =strlen (dir); clen = (dlen < token->size) ? dlen : token->size; -//debug("cmp(%*.*s,%s) = ", -// (int)token->size, (int)token->size, token->value, -// dir); +//debug("cmp(%s,%s) = ", token->content, dir); - val =memcmp (token->value, dir, clen); + val =memcmp (token->content, dir, clen);if (val !=0 ) {//debug("%d [cmp]\n", val); return val;
@@ -352,7 +350,7 @@ static int directive_compare(const void *_key, const void *_pdir)static void tokenise (char *buffer,char *end) {struct token *tokens; -char *line, *nl, *p, *q; +char *line, *nl, *start, *p, *q;unsigned tix, lineno;/* Assume we're going to have half as many tokens as we have
@@ -411,11 +409,11 @@ static void tokenise(char *buffer, char *end)break ; tokens[tix].line = lineno; - tokens[tix].value = p; + start = p;/* Handle string tokens */ if (isalpha (*p)) { -const char **dir; +const char **dir, *start = p;/* Can be a directive, type name or element * name. Find the end of the name.
@@ -426,10 +424,18 @@ static void tokenise(char *buffer, char *end) tokens[tix].size = q - p; p = q; + tokens[tix].content =malloc (tokens[tix].size +1 ); +if (!tokens[tix].content) { +perror (NULL ); +exit (1 ); + } +memcpy (tokens[tix].content, start, tokens[tix].size); + tokens[tix].content[tokens[tix].size] =0 ; +/* If it begins with a lowercase letter then * it's an element name */ -if (islower (tokens[tix].value[0 ])) { +if (islower (tokens[tix].content[0 ])) { tokens[tix++].token_type =TOKEN_ELEMENT_NAME ;continue ; }
@@ -458,6 +464,13 @@ static void tokenise(char *buffer, char *end) q++; tokens[tix].size = q - p; p = q; + tokens[tix].content =malloc (tokens[tix].size +1 ); +if (!tokens[tix].content) { +perror (NULL ); +exit (1 ); + } +memcpy (tokens[tix].content, start, tokens[tix].size); + tokens[tix].content[tokens[tix].size] =0 ; tokens[tix++].token_type =TOKEN_NUMBER ;continue ; }
@@ -466,6 +479,7 @@ static void tokenise(char *buffer, char *end)if (memcmp (p,"::=" ,3 ) ==0 ) { p +=3 ; tokens[tix].size =3 ; + tokens[tix].content ="::=" ; tokens[tix++].token_type =TOKEN_ASSIGNMENT ;continue ; }
@@ -475,12 +489,14 @@ static void tokenise(char *buffer, char *end)if (memcmp (p,"({" ,2 ) ==0 ) { p +=2 ; tokens[tix].size =2 ; + tokens[tix].content ="({" ; tokens[tix++].token_type =TOKEN_OPEN_ACTION ;continue ; }if (memcmp (p,"})" ,2 ) ==0 ) { p +=2 ; tokens[tix].size =2 ; + tokens[tix].content ="})" ; tokens[tix++].token_type =TOKEN_CLOSE_ACTION ;continue ; }
@@ -491,22 +507,27 @@ static void tokenise(char *buffer, char *end)switch (*p) {case '{' : p +=1 ; + tokens[tix].content ="{" ; tokens[tix++].token_type =TOKEN_OPEN_CURLY ;continue ;case '}' : p +=1 ; + tokens[tix].content ="}" ; tokens[tix++].token_type =TOKEN_CLOSE_CURLY ;continue ;case '[' : p +=1 ; + tokens[tix].content ="[" ; tokens[tix++].token_type =TOKEN_OPEN_SQUARE ;continue ;case ']' : p +=1 ; + tokens[tix].content ="]" ; tokens[tix++].token_type =TOKEN_CLOSE_SQUARE ;continue ;case ',' : p +=1 ; + tokens[tix].content ="," ; tokens[tix++].token_type =TOKEN_COMMA ;continue ;default :
@@ -527,10 +548,7 @@ static void tokenise(char *buffer, char *end) {int n;for (n =0 ; n < nr_tokens; n++) -debug ("Token %3u: '%*.*s'\n" , - n, - (int )token_list[n].size, (int )token_list[n].size, - token_list[n].value); +debug ("Token %3u: '%s'\n" , n, token_list[n].content); }#endif }
@@ -709,7 +727,7 @@ static int type_index_compare(const void *_a, const void *_b)if ((*a)->name->size != (*b)->name->size)return (*a)->name->size - (*b)->name->size;else -return memcmp ((*a)->name->value, (*b)->name->value, +return memcmp ((*a)->name->content, (*b)->name->content, (*a)->name->size); }
@@ -722,7 +740,7 @@ static int type_finder(const void *_key, const void *_ti)if (token->size != type->name->size)return token->size - type->name->size;else -return memcmp (token->value, type->name->value, +return memcmp (token->content, type->name->content, token->size); }
@@ -776,10 +794,7 @@ static void build_type_list(void)#if 0 for (n =0 ; n < nr_types; n++) {struct type *type = type_index[n]; -debug ("- %*.*s\n" , - (int )type->name->size, - (int )type->name->size, - type->name->value); +debug ("- %*.*s\n" , type->name->content); }#endif }
@@ -809,9 +824,8 @@ static void parse(void) type->element->type_def = type;if (cursor != type[1 ].name) { -fprintf (stderr,"%s:%d: Parse error at token '%*.*s'\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Parse error at token '%s'\n" , + filename, cursor->line, cursor->content);exit (1 ); }
@@ -878,34 +892,31 @@ static struct element *parse_type(struct token **_cursor, struct token *end, cursor++;break ;default : -fprintf (stderr,"%s:%d: Unrecognised tag class token '%*.*s'\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Unrecognised tag class token '%s'\n" , + filename, cursor->line, cursor->content);exit (1 ); }if (cursor >= end) goto overrun_error;if (cursor->token_type !=TOKEN_NUMBER ) { -fprintf (stderr,"%s:%d: Missing tag number '%*.*s'\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Missing tag number '%s'\n" , + filename, cursor->line, cursor->content);exit (1 ); } element->tag &= ~0x1f ; - element->tag |=strtoul (cursor->value, &p,10 ); + element->tag |=strtoul (cursor->content, &p,10 ); element->flags |=ELEMENT_TAG_SPECIFIED ; -if (p - cursor->value != cursor->size) +if (p - cursor->content != cursor->size)abort (); cursor++;if (cursor >= end) goto overrun_error;if (cursor->token_type !=TOKEN_CLOSE_SQUARE ) { -fprintf (stderr,"%s:%d: Missing closing square bracket '%*.*s'\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Missing closing square bracket '%s'\n" , + filename, cursor->line, cursor->content);exit (1 ); } cursor++;
@@ -1005,9 +1016,8 @@ static struct element *parse_type(struct token **_cursor, struct token *end, ref =bsearch (cursor, type_index, nr_types,sizeof (type_index[0 ]), type_finder);if (!ref) { -fprintf (stderr,"%s:%d: Type '%*.*s' undefined\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Type '%s' undefined\n" , + filename, cursor->line, cursor->content);exit (1 ); } cursor->type = *ref;
@@ -1056,9 +1066,8 @@ static struct element *parse_type(struct token **_cursor, struct token *end,break ;default : -fprintf (stderr,"%s:%d: Token '%*.*s' does not introduce a type\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Token '%s' does not introduce a type\n" , + filename, cursor->line, cursor->content);exit (1 ); }
@@ -1075,20 +1084,18 @@ static struct element *parse_type(struct token **_cursor, struct token *end,if (cursor >= end) goto overrun_error;if (cursor->token_type !=TOKEN_ELEMENT_NAME ) { -fprintf (stderr,"%s:%d: Token '%*.*s' is not an action function name\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Token '%s' is not an action function name\n" , + filename, cursor->line, cursor->content);exit (1 ); } - action =malloc (sizeof (struct action ) + cursor->size +1 ); + action =malloc (sizeof (struct action ));if (!action) {perror (NULL );exit (1 ); } action->index =0 ; -memcpy (action->name, cursor->value, cursor->size); - action->name[cursor->size] =0 ; + action->name = cursor->content; for (ppaction = &action_list; *ppaction;
@@ -1118,9 +1125,8 @@ static struct element *parse_type(struct token **_cursor, struct token *end,if (cursor >= end) goto overrun_error;if (cursor->token_type !=TOKEN_CLOSE_ACTION ) { -fprintf (stderr,"%s:%d: Missing close action, got '%*.*s'\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Missing close action, got '%s'\n" , + filename, cursor->line, cursor->content);exit (1 ); } cursor++;
@@ -1130,9 +1136,8 @@ static struct element *parse_type(struct token **_cursor, struct token *end,return top; parse_error: -fprintf (stderr,"%s:%d: Unexpected token '%*.*s'\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Unexpected token '%s'\n" , + filename, cursor->line, cursor->content);exit (1 ); overrun_error:
@@ -1150,9 +1155,8 @@ static struct element *parse_compound(struct token **_cursor, struct token *end,struct token *cursor = *_cursor, *name;if (cursor->token_type !=TOKEN_OPEN_CURLY ) { -fprintf (stderr,"%s:%d: Expected compound to start with brace not '%*.*s'\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Expected compound to start with brace not '%s'\n" , + filename, cursor->line, cursor->content);exit (1 ); } cursor++;
@@ -1193,9 +1197,8 @@ static struct element *parse_compound(struct token **_cursor, struct token *end, children->flags &= ~ELEMENT_CONDITIONAL ;if (cursor->token_type !=TOKEN_CLOSE_CURLY ) { -fprintf (stderr,"%s:%d: Expected compound closure, got '%*.*s'\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Expected compound closure, got '%s'\n" , + filename, cursor->line, cursor->content);exit (1 ); } cursor++;
@@ -1212,10 +1215,8 @@ static void dump_element(const struct element *e, int level) {const struct element *c;const struct type *t = e->type_def; -const char *name = e->name ? e->name->value :"." ; -int nsize = e->name ? e->name->size :1 ; -const char *tname = t && t->name ? t->name->value :"." ; -int tnsize = t && t->name ? t->name->size :1 ; +const char *name = e->name ? e->name->content :"." ; +const char *tname = t && t->name ? t->name->content :"." ;char tag[32 ];if (e->class ==0 && e->method ==0 && e->tag ==0 )
@@ -1231,7 +1232,7 @@ static void dump_element(const struct element *e, int level) asn1_methods[e->method], e->tag); - printf("%c%c%c%c%c %c %*s[*] \e[33m%s\e[m %*.*s %*.*s \e[35m%s\e[m\n", + printf("%c%c%c%c%c %c %*s[*] \e[33m%s\e[m %s %s \e[35m%s\e[m\n", e->flags & ELEMENT_IMPLICIT ? 'I' : '-', e->flags & ELEMENT_EXPLICIT ? 'E' : '-', e->flags & ELEMENT_TAG_SPECIFIED ? 'T' : '-',
@@ -1240,8 +1241,8 @@ static void dump_element(const struct element *e, int level)"-tTqQcaro" [e->compound], level,"" , tag, - tnsize, tnsize, tname, - nsize, nsize, name, + tname, + name, e->action ? e->action->name : "");if (e->compound ==TYPE_REF )dump_element (e->type->type->element, level +3 );
@@ -1454,9 +1455,7 @@ static void render_element(FILE *out, struct element *e, struct element *tag) outofline =1 ;if (e->type_def && out) { -render_more (out,"\t// %*.*s\n" , - (int )e->type_def->name->size, (int )e->type_def->name->size, - e->type_def->name->value); +render_more (out,"\t// %s\n" , e->type_def->name->content); }/* Render the operation */
@@ -1468,9 +1467,7 @@ static void render_element(FILE *out, struct element *e, struct element *tag)render_opcode (out,"ASN1_OP_%sMATCH_ANY%s%s," , cond, act, skippable ?"_OR_SKIP" :"" );if (e->name) -render_more (out,"\t\t// %*.*s" , - (int )e->name->size, (int )e->name->size, - e->name->value); +render_more (out,"\t\t// %s" , e->name->content);render_more (out,"\n" ); goto dont_render_tag;
@@ -1503,9 +1500,7 @@ static void render_element(FILE *out, struct element *e, struct element *tag) x = tag ?: e;if (x->name) -render_more (out,"\t\t// %*.*s" , - (int )x->name->size, (int )x->name->size, - x->name->value); +render_more (out,"\t\t// %s" , x->name->content);render_more (out,"\n" );/* Render the tag */
@@ -1543,10 +1538,8 @@ dont_render_tag:* skipability */ render_opcode (out,"_jump_target(%u)," , e->entry_index);if (e->type_def && e->type_def->name) -render_more (out,"\t\t// --> %*.*s" , - (int )e->type_def->name->size, - (int )e->type_def->name->size, - e->type_def->name->value); +render_more (out,"\t\t// --> %s" , + e->type_def->name->content);render_more (out,"\n" );if (!(e->flags &ELEMENT_RENDERED )) { e->flags |=ELEMENT_RENDERED ;
@@ -1571,10 +1564,8 @@ dont_render_tag:* skipability */ render_opcode (out,"_jump_target(%u)," , e->entry_index);if (e->type_def && e->type_def->name) -render_more (out,"\t\t// --> %*.*s" , - (int )e->type_def->name->size, - (int )e->type_def->name->size, - e->type_def->name->value); +render_more (out,"\t\t// --> %s" , + e->type_def->name->content);render_more (out,"\n" );if (!(e->flags &ELEMENT_RENDERED )) { e->flags |=ELEMENT_RENDERED ;
Re: [PATCH 02/27] ASN.1: Copy string names to tokens in ASN.1 compiler [ver #7]
Hi David,
This patch isn't applying properly against linux-security/next. The rest seem to be fine.
Mimi
Quoted 443 lines · David Howells
Copy string names to tokens in ASN.1 compiler rather than storing a pointer into the source text. This means we don't have to use "%*.*s" all over the place.
Signed-off-by David Howells <[email protected]>Reviewed-by David Woodhouse <[email protected]>---
scripts/asn1_compiler.c | 155 ++++++++++++++++++++++------------------------- 1 file changed, 73 insertions(+), 82 deletions(-)scripts/asn1_compiler.c +73
-82@@ -294,8 +294,8 @@ static const char *const directives[NR__DIRECTIVES] = {struct action {struct action *next; +char *name;unsigned char index; -char name[]; };static struct action *action_list;@@ -306,7 +306,7 @@ struct token { enum token_type token_type : 8;unsigned char size;struct action *action; -const char *value; +char *content;struct type *type; };@@ -328,11 +328,9 @@ static int directive_compare(const void *_key, const void *_pdir) dlen =strlen (dir); clen = (dlen < token->size) ? dlen : token->size; -//debug("cmp(%*.*s,%s) = ", -// (int)token->size, (int)token->size, token->value, -// dir); +//debug("cmp(%s,%s) = ", token->content, dir); - val =memcmp (token->value, dir, clen); + val =memcmp (token->content, dir, clen);if (val !=0 ) {//debug("%d [cmp]\n", val); return val;@@ -352,7 +350,7 @@ static int directive_compare(const void *_key, const void *_pdir)static void tokenise (char *buffer,char *end) {struct token *tokens; -char *line, *nl, *p, *q; +char *line, *nl, *start, *p, *q;unsigned tix, lineno;/* Assume we're going to have half as many tokens as we have @@ -411,11 +409,11 @@ static void tokenise(char *buffer, char *end)break ; tokens[tix].line = lineno; - tokens[tix].value = p; + start = p;/* Handle string tokens */ if (isalpha (*p)) { -const char **dir; +const char **dir, *start = p;/* Can be a directive, type name or element * name. Find the end of the name. @@ -426,10 +424,18 @@ static void tokenise(char *buffer, char *end) tokens[tix].size = q - p; p = q; + tokens[tix].content =malloc (tokens[tix].size +1 ); +if (!tokens[tix].content) { +perror (NULL ); +exit (1 ); + } +memcpy (tokens[tix].content, start, tokens[tix].size); + tokens[tix].content[tokens[tix].size] =0 ; +/* If it begins with a lowercase letter then * it's an element name */ -if (islower (tokens[tix].value[0 ])) { +if (islower (tokens[tix].content[0 ])) { tokens[tix++].token_type =TOKEN_ELEMENT_NAME ;continue ; }@@ -458,6 +464,13 @@ static void tokenise(char *buffer, char *end) q++; tokens[tix].size = q - p; p = q; + tokens[tix].content =malloc (tokens[tix].size +1 ); +if (!tokens[tix].content) { +perror (NULL ); +exit (1 ); + } +memcpy (tokens[tix].content, start, tokens[tix].size); + tokens[tix].content[tokens[tix].size] =0 ; tokens[tix++].token_type =TOKEN_NUMBER ;continue ; }@@ -466,6 +479,7 @@ static void tokenise(char *buffer, char *end)if (memcmp (p,"::=" ,3 ) ==0 ) { p +=3 ; tokens[tix].size =3 ; + tokens[tix].content ="::=" ; tokens[tix++].token_type =TOKEN_ASSIGNMENT ;continue ; }@@ -475,12 +489,14 @@ static void tokenise(char *buffer, char *end)if (memcmp (p,"({" ,2 ) ==0 ) { p +=2 ; tokens[tix].size =2 ; + tokens[tix].content ="({" ; tokens[tix++].token_type =TOKEN_OPEN_ACTION ;continue ; }if (memcmp (p,"})" ,2 ) ==0 ) { p +=2 ; tokens[tix].size =2 ; + tokens[tix].content ="})" ; tokens[tix++].token_type =TOKEN_CLOSE_ACTION ;continue ; }@@ -491,22 +507,27 @@ static void tokenise(char *buffer, char *end)switch (*p) {case '{' : p +=1 ; + tokens[tix].content ="{" ; tokens[tix++].token_type =TOKEN_OPEN_CURLY ;continue ;case '}' : p +=1 ; + tokens[tix].content ="}" ; tokens[tix++].token_type =TOKEN_CLOSE_CURLY ;continue ;case '[' : p +=1 ; + tokens[tix].content ="[" ; tokens[tix++].token_type =TOKEN_OPEN_SQUARE ;continue ;case ']' : p +=1 ; + tokens[tix].content ="]" ; tokens[tix++].token_type =TOKEN_CLOSE_SQUARE ;continue ;case ',' : p +=1 ; + tokens[tix].content ="," ; tokens[tix++].token_type =TOKEN_COMMA ;continue ;default :@@ -527,10 +548,7 @@ static void tokenise(char *buffer, char *end) {int n;for (n =0 ; n < nr_tokens; n++) -debug ("Token %3u: '%*.*s'\n" , - n, - (int )token_list[n].size, (int )token_list[n].size, - token_list[n].value); +debug ("Token %3u: '%s'\n" , n, token_list[n].content); }#endif }@@ -709,7 +727,7 @@ static int type_index_compare(const void *_a, const void *_b)if ((*a)->name->size != (*b)->name->size)return (*a)->name->size - (*b)->name->size;else -return memcmp ((*a)->name->value, (*b)->name->value, +return memcmp ((*a)->name->content, (*b)->name->content, (*a)->name->size); }@@ -722,7 +740,7 @@ static int type_finder(const void *_key, const void *_ti)if (token->size != type->name->size)return token->size - type->name->size;else -return memcmp (token->value, type->name->value, +return memcmp (token->content, type->name->content, token->size); }@@ -776,10 +794,7 @@ static void build_type_list(void)#if 0 for (n =0 ; n < nr_types; n++) {struct type *type = type_index[n]; -debug ("- %*.*s\n" , - (int )type->name->size, - (int )type->name->size, - type->name->value); +debug ("- %*.*s\n" , type->name->content); }#endif }@@ -809,9 +824,8 @@ static void parse(void) type->element->type_def = type;if (cursor != type[1 ].name) { -fprintf (stderr,"%s:%d: Parse error at token '%*.*s'\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Parse error at token '%s'\n" , + filename, cursor->line, cursor->content);exit (1 ); }@@ -878,34 +892,31 @@ static struct element *parse_type(struct token **_cursor, struct token *end, cursor++;break ;default : -fprintf (stderr,"%s:%d: Unrecognised tag class token '%*.*s'\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Unrecognised tag class token '%s'\n" , + filename, cursor->line, cursor->content);exit (1 ); }if (cursor >= end) goto overrun_error;if (cursor->token_type !=TOKEN_NUMBER ) { -fprintf (stderr,"%s:%d: Missing tag number '%*.*s'\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Missing tag number '%s'\n" , + filename, cursor->line, cursor->content);exit (1 ); } element->tag &= ~0x1f ; - element->tag |=strtoul (cursor->value, &p,10 ); + element->tag |=strtoul (cursor->content, &p,10 ); element->flags |=ELEMENT_TAG_SPECIFIED ; -if (p - cursor->value != cursor->size) +if (p - cursor->content != cursor->size)abort (); cursor++;if (cursor >= end) goto overrun_error;if (cursor->token_type !=TOKEN_CLOSE_SQUARE ) { -fprintf (stderr,"%s:%d: Missing closing square bracket '%*.*s'\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Missing closing square bracket '%s'\n" , + filename, cursor->line, cursor->content);exit (1 ); } cursor++;@@ -1005,9 +1016,8 @@ static struct element *parse_type(struct token **_cursor, struct token *end, ref =bsearch (cursor, type_index, nr_types,sizeof (type_index[0 ]), type_finder);if (!ref) { -fprintf (stderr,"%s:%d: Type '%*.*s' undefined\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Type '%s' undefined\n" , + filename, cursor->line, cursor->content);exit (1 ); } cursor->type = *ref;@@ -1056,9 +1066,8 @@ static struct element *parse_type(struct token **_cursor, struct token *end,break ;default : -fprintf (stderr,"%s:%d: Token '%*.*s' does not introduce a type\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Token '%s' does not introduce a type\n" , + filename, cursor->line, cursor->content);exit (1 ); }@@ -1075,20 +1084,18 @@ static struct element *parse_type(struct token **_cursor, struct token *end,if (cursor >= end) goto overrun_error;if (cursor->token_type !=TOKEN_ELEMENT_NAME ) { -fprintf (stderr,"%s:%d: Token '%*.*s' is not an action function name\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Token '%s' is not an action function name\n" , + filename, cursor->line, cursor->content);exit (1 ); } - action =malloc (sizeof (struct action ) + cursor->size +1 ); + action =malloc (sizeof (struct action ));if (!action) {perror (NULL );exit (1 ); } action->index =0 ; -memcpy (action->name, cursor->value, cursor->size); - action->name[cursor->size] =0 ; + action->name = cursor->content; for (ppaction = &action_list; *ppaction;@@ -1118,9 +1125,8 @@ static struct element *parse_type(struct token **_cursor, struct token *end,if (cursor >= end) goto overrun_error;if (cursor->token_type !=TOKEN_CLOSE_ACTION ) { -fprintf (stderr,"%s:%d: Missing close action, got '%*.*s'\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Missing close action, got '%s'\n" , + filename, cursor->line, cursor->content);exit (1 ); } cursor++;@@ -1130,9 +1136,8 @@ static struct element *parse_type(struct token **_cursor, struct token *end,return top; parse_error: -fprintf (stderr,"%s:%d: Unexpected token '%*.*s'\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Unexpected token '%s'\n" , + filename, cursor->line, cursor->content);exit (1 ); overrun_error:@@ -1150,9 +1155,8 @@ static struct element *parse_compound(struct token **_cursor, struct token *end,struct token *cursor = *_cursor, *name;if (cursor->token_type !=TOKEN_OPEN_CURLY ) { -fprintf (stderr,"%s:%d: Expected compound to start with brace not '%*.*s'\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Expected compound to start with brace not '%s'\n" , + filename, cursor->line, cursor->content);exit (1 ); } cursor++;@@ -1193,9 +1197,8 @@ static struct element *parse_compound(struct token **_cursor, struct token *end, children->flags &= ~ELEMENT_CONDITIONAL ;if (cursor->token_type !=TOKEN_CLOSE_CURLY ) { -fprintf (stderr,"%s:%d: Expected compound closure, got '%*.*s'\n" , - filename, cursor->line, - (int )cursor->size, (int )cursor->size, cursor->value); +fprintf (stderr,"%s:%d: Expected compound closure, got '%s'\n" , + filename, cursor->line, cursor->content);exit (1 ); } cursor++;@@ -1212,10 +1215,8 @@ static void dump_element(const struct element *e, int level) {const struct element *c;const struct type *t = e->type_def; -const char *name = e->name ? e->name->value :"." ; -int nsize = e->name ? e->name->size :1 ; -const char *tname = t && t->name ? t->name->value :"." ; -int tnsize = t && t->name ? t->name->size :1 ; +const char *name = e->name ? e->name->content :"." ; +const char *tname = t && t->name ? t->name->content :"." ;char tag[32 ];if (e->class ==0 && e->method ==0 && e->tag ==0 )@@ -1231,7 +1232,7 @@ static void dump_element(const struct element *e, int level) asn1_methods[e->method], e->tag); - printf("%c%c%c%c%c %c %*s[*] \e[33m%s\e[m %*.*s %*.*s \e[35m%s\e[m\n", + printf("%c%c%c%c%c %c %*s[*] \e[33m%s\e[m %s %s \e[35m%s\e[m\n", e->flags & ELEMENT_IMPLICIT ? 'I' : '-', e->flags & ELEMENT_EXPLICIT ? 'E' : '-', e->flags & ELEMENT_TAG_SPECIFIED ? 'T' : '-',@@ -1240,8 +1241,8 @@ static void dump_element(const struct element *e, int level)"-tTqQcaro" [e->compound], level,"" , tag, - tnsize, tnsize, tname, - nsize, nsize, name, + tname, + name, e->action ? e->action->name : "");if (e->compound ==TYPE_REF )dump_element (e->type->type->element, level +3 );@@ -1454,9 +1455,7 @@ static void render_element(FILE *out, struct element *e, struct element *tag) outofline =1 ;if (e->type_def && out) { -render_more (out,"\t// %*.*s\n" , - (int )e->type_def->name->size, (int )e->type_def->name->size, - e->type_def->name->value); +render_more (out,"\t// %s\n" , e->type_def->name->content); }/* Render the operation */ @@ -1468,9 +1467,7 @@ static void render_element(FILE *out, struct element *e, struct element *tag)render_opcode (out,"ASN1_OP_%sMATCH_ANY%s%s," , cond, act, skippable ?"_OR_SKIP" :"" );if (e->name) -render_more (out,"\t\t// %*.*s" , - (int )e->name->size, (int )e->name->size, - e->name->value); +render_more (out,"\t\t// %s" , e->name->content);render_more (out,"\n" ); goto dont_render_tag;@@ -1503,9 +1500,7 @@ static void render_element(FILE *out, struct element *e, struct element *tag) x = tag ?: e;if (x->name) -render_more (out,"\t\t// %*.*s" , - (int )x->name->size, (int )x->name->size, - x->name->value); +render_more (out,"\t\t// %s" , x->name->content);render_more (out,"\n" );/* Render the tag */ @@ -1543,10 +1538,8 @@ dont_render_tag:* skipability */ render_opcode (out,"_jump_target(%u)," , e->entry_index);if (e->type_def && e->type_def->name) -render_more (out,"\t\t// --> %*.*s" , - (int )e->type_def->name->size, - (int )e->type_def->name->size, - e->type_def->name->value); +render_more (out,"\t\t// --> %s" , + e->type_def->name->content);render_more (out,"\n" );if (!(e->flags &ELEMENT_RENDERED )) { e->flags |=ELEMENT_RENDERED ;@@ -1571,10 +1564,8 @@ dont_render_tag:* skipability */ render_opcode (out,"_jump_target(%u)," , e->entry_index);if (e->type_def && e->type_def->name) -render_more (out,"\t\t// --> %*.*s" , - (int )e->type_def->name->size, - (int )e->type_def->name->size, - e->type_def->name->value); +render_more (out,"\t\t// --> %s" , + e->type_def->name->content);render_more (out,"\n" );if (!(e->flags &ELEMENT_RENDERED )) { e->flags |=ELEMENT_RENDERED ;
David Howells
maintainerRe: [PATCH 02/27] ASN.1: Copy string names to tokens in ASN.1 compiler [ver #7]
Quoted 2 lines · Mimi Zohar
This patch isn't applying properly against linux-security/next. The rest seem to be fine.
Did you apply the asn1-fixes-20150805 first and patch 1?
Note that the branch is based on security/next, with my asn1-fixes-20150805 tag merged on top - so it *ought* to apply.
David
Re: [PATCH 02/27] ASN.1: Copy string names to tokens in ASN.1 compiler [ver #7]
Quoted 9 lines · David Howells
Quoted 2 lines · Mimi Zohar
This patch isn't applying properly against linux-security/next. The rest seem to be fine.
Did you apply the asn1-fixes-20150805 first and patch 1?
Note that the branch is based on
security/next, with my asn1-fixes-20150805 tag merged on top - so it *ought* to apply.
Yes, that works.
Thanks!
Mimi
David Howells
maintainerPATCH 3/27 X.509: Extract both parts of the AuthorityKeyIdentifier [ver #7]
In reply to David HowellsExtract both parts of the AuthorityKeyIdentifier, not just the keyIdentifier, as the second part can be used to match X.509 certificates by issuer and serialNumber.
---
crypto/asymmetric_keys/Makefile | 8 +- crypto/asymmetric_keys/pkcs7_trust.c | 4 - crypto/asymmetric_keys/pkcs7_verify.c | 12 +- crypto/asymmetric_keys/x509_akid.asn1 | 35 +++++++ crypto/asymmetric_keys/x509_cert_parser.c | 142 ++++++++++++++++++----------- crypto/asymmetric_keys/x509_parser.h | 5 + crypto/asymmetric_keys/x509_public_key.c | 8 +- 7 files changed, 145 insertions(+), 69 deletions(-) create mode 100644 crypto/asymmetric_keys/x509_akid.asn1
crypto/asymmetric_keys/Makefile +7 -1
@@ -15,15 +15,21 @@ obj-$(CONFIG_PUBLIC_KEY_ALGO_RSA) += rsa.o obj-$(CONFIG_X509_CERTIFICATE_PARSER) += x509_key_parser.o x509_key_parser-y := \ x509-asn1.o \ + x509_akid-asn1.o \ x509_rsakey-asn1.o \ x509_cert_parser.o \ x509_public_key.o -$(obj)/x509_cert_parser.o: $(obj)/x509-asn1.h $(obj)/x509_rsakey-asn1.h +$(obj)/x509_cert_parser.o: \ + $(obj)/x509-asn1.h \ + $(obj)/x509_akid-asn1.h \ + $(obj)/x509_rsakey-asn1.h $(obj)/x509-asn1.o: $(obj)/x509-asn1.c $(obj)/x509-asn1.h +$(obj)/x509_akid-asn1.o: $(obj)/x509_akid-asn1.c $(obj)/x509_akid-asn1.h $(obj)/x509_rsakey-asn1.o: $(obj)/x509_rsakey-asn1.c $(obj)/x509_rsakey-asn1.h clean-files += x509-asn1.c x509-asn1.h +clean-files += x509_akid-asn1.c x509_akid-asn1.h clean-files += x509_rsakey-asn1.c x509_rsakey-asn1.h #
crypto/asymmetric_keys/pkcs7_trust.c +2 -2
@@ -85,8 +85,8 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,/* No match - see if the root certificate has a signer amongst the * trusted keys. */ -if (last && last->authority) { - key =x509_request_asymmetric_key (trust_keyring, last->authority, +if (last && last->akid_skid) { + key =x509_request_asymmetric_key (trust_keyring, last->akid_skid, false);if (!IS_ERR (key)) { x509 = last;
crypto/asymmetric_keys/pkcs7_verify.c +5 -7
@@ -187,11 +187,11 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7, goto maybe_missing_crypto_in_x509;pr_debug ("- issuer %s\n" , x509->issuer); -if (x509->authority) +if (x509->akid_skid)pr_debug ("- authkeyid %*phN\n" , - x509->authority->len, x509->authority->data); + x509->akid_skid->len, x509->akid_skid->data); -if (!x509->authority || +if (!x509->akid_skid ||strcmp (x509->subject, x509->issuer) ==0 ) {/* If there's no authority certificate specified, then * the certificate must be self-signed and is the root
@@ -216,13 +216,13 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,* list to see if the next one is there. */ pr_debug ("- want %*phN\n" , - x509->authority->len, x509->authority->data); + x509->akid_skid->len, x509->akid_skid->data);for (p = pkcs7->certs; p; p = p->next) {if (!p->skid)continue ;pr_debug ("- cmp [%u] %*phN\n" , p->index, p->skid->len, p->skid->data); -if (asymmetric_key_id_same (p->skid, x509->authority)) +if (asymmetric_key_id_same (p->skid, x509->akid_skid)) goto found_issuer; }
@@ -338,8 +338,6 @@ int pkcs7_verify(struct pkcs7_message *pkcs7) ret =x509_get_sig_params (x509);if (ret <0 )return ret; -pr_debug ("X.509[%u] %*phN\n" , - n, x509->authority->len, x509->authority->data); }for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {
crypto/asymmetric_keys/x509_akid.asn1 new file +35 -0
@@ -0,0 +1,35 @@ +-- X.509 AuthorityKeyIdentifier +-- rfc5280 section 4.2.1.1 + +AuthorityKeyIdentifier ::= SEQUENCE { + keyIdentifier [0] IMPLICIT KeyIdentifier OPTIONAL, + authorityCertIssuer [1] IMPLICIT GeneralNames OPTIONAL, + authorityCertSerialNumber [2] IMPLICIT CertificateSerialNumber OPTIONAL + } + +KeyIdentifier ::= OCTET STRING ({ x509_akid_note_kid }) + +CertificateSerialNumber ::= INTEGER ({ x509_akid_note_serial }) + +GeneralNames ::= SEQUENCE OF GeneralName + +GeneralName ::= CHOICE { + otherName [0] ANY, + rfc822Name [1] IA5String, + dNSName [2] IA5String, + x400Address [3] ANY, + directoryName [4] Name ({ x509_akid_note_name }), + ediPartyName [5] ANY, + uniformResourceIdentifier [6] IA5String, + iPAddress [7] OCTET STRING, + registeredID [8] OBJECT IDENTIFIER + } + +Name ::= SEQUENCE OF RelativeDistinguishedName + +RelativeDistinguishedName ::= SET OF AttributeValueAssertion + +AttributeValueAssertion ::= SEQUENCE { + attributeType OBJECT IDENTIFIER ({ x509_note_OID }), + attributeValue ANY ({ x509_extract_name_segment }) + }
crypto/asymmetric_keys/x509_cert_parser.c +89 -53
181 lines not on this page · raw message
crypto/asymmetric_keys/x509_parser.h +3 -2
@@ -19,9 +19,10 @@ struct x509_certificate {struct public_key_signature sig;/* Signature parameters */ char *issuer;/* Name of certificate issuer */ char *subject;/* Name of certificate subject */ -struct asymmetric_key_id *id;/* Serial number + issuer */ +struct asymmetric_key_id *id;/* Issuer + Serial number */ struct asymmetric_key_id *skid;/* Subject + subjectKeyId (optional) */ -struct asymmetric_key_id *authority;/* Authority key identifier (optional) */ +struct asymmetric_key_id *akid_id;/* CA AuthKeyId matching ->id (optional) */ +struct asymmetric_key_id *akid_skid;/* CA AuthKeyId matching ->skid (optional) */ struct tm valid_from;struct tm valid_to;const void *tbs;/* Signed data */
crypto/asymmetric_keys/x509_public_key.c +4 -4
@@ -227,10 +227,10 @@ static int x509_validate_trust(struct x509_certificate *cert,if (!trust_keyring)return -EOPNOTSUPP ; -if (ca_keyid && !asymmetric_key_id_partial (cert->authority, ca_keyid)) +if (ca_keyid && !asymmetric_key_id_partial (cert->akid_skid, ca_keyid))return -EPERM ; - key =x509_request_asymmetric_key (trust_keyring, cert->authority, + key =x509_request_asymmetric_key (trust_keyring, cert->akid_skid, false);if (!IS_ERR (key)) { if (!use_builtin_keys
@@ -287,8 +287,8 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) cert->pub->id_type =PKEY_ID_X509 ;/* Check the signature on the key if it appears to be self-signed */ -if (!cert->authority || -asymmetric_key_id_same (cert->skid, cert->authority)) { +if (!cert->akid_skid || +asymmetric_key_id_same (cert->skid, cert->akid_skid)) { ret =x509_check_signature (cert->pub, cert);/* self-signed */ if (ret <0 ) goto error_free_cert;
David Howells
maintainerPATCH 4/27 X.509: Support X.509 lookup by Issuer+Serial form AuthorityKeyIdentifier [ver #7]
In reply to David HowellsIf an X.509 certificate has an AuthorityKeyIdentifier extension that provides an issuer and serialNumber, then make it so that these are used in preference to the keyIdentifier field also held therein for searching for the signing certificate.
If both the issuer+serialNumber and the keyIdentifier are supplied, then the certificate is looked up by the former but the latter is checked as well. If the latter doesn't match the subjectKeyIdentifier of the parent certificate, EKEYREJECTED is returned.
This makes it possible to chain X.509 certificates based on the issuer and serialNumber fields rather than on subjectKeyIdentifier. This is necessary as we are having to deal with keys that are represented by X.509 certificates that lack a subjectKeyIdentifier.
---
crypto/asymmetric_keys/pkcs7_trust.c | 10 +++- crypto/asymmetric_keys/pkcs7_verify.c | 47 +++++++++++++---- crypto/asymmetric_keys/x509_public_key.c | 84 +++++++++++++++++++++--------- include/crypto/public_key.h | 3 + 4 files changed, 103 insertions(+), 41 deletions(-)
crypto/asymmetric_keys/pkcs7_trust.c +7 -3
@@ -54,7 +54,8 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,/* Look to see if this certificate is present in the trusted * keys. */ - key =x509_request_asymmetric_key (trust_keyring, x509->id, + key =x509_request_asymmetric_key (trust_keyring, + x509->id, x509->skid, false);if (!IS_ERR (key)) {/* One of the X.509 certificates in the PKCS#7 message
@@ -85,8 +86,10 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,/* No match - see if the root certificate has a signer amongst the * trusted keys. */ -if (last && last->akid_skid) { - key =x509_request_asymmetric_key (trust_keyring, last->akid_skid, +if (last && (last->akid_id || last->akid_skid)) { + key =x509_request_asymmetric_key (trust_keyring, + last->akid_id, + last->akid_skid, false);if (!IS_ERR (key)) { x509 = last;
@@ -103,6 +106,7 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,*/ key =x509_request_asymmetric_key (trust_keyring, sinfo->signing_cert_id, +NULL , false);if (!IS_ERR (key)) { pr_devel("sinfo %u: Direct signer is key %x\n",
crypto/asymmetric_keys/pkcs7_verify.c +36 -11
74 lines not on this page · raw message
crypto/asymmetric_keys/x509_public_key.c +58 -26
128 lines not on this page · raw message
include/crypto/public_key.h +2 -1
@@ -101,7 +101,8 @@ extern int verify_signature(const struct key *key,struct asymmetric_key_id ;extern struct key *x509_request_asymmetric_key (struct key *keyring, -const struct asymmetric_key_id *kid, +const struct asymmetric_key_id *id, +const struct asymmetric_key_id *skid,bool partial);#endif /* _LINUX_PUBLIC_KEY_H */
David Howells
maintainerPATCH 5/27 PKCS#7: Allow detached data to be supplied for signature checking purposes [ver #7]
In reply to David HowellsIt is possible for a PKCS#7 message to have detached data. However, to verify the signatures on a PKCS#7 message, we have to be able to digest the data. Provide a function to supply that data. An error is given if the PKCS#7 message included embedded data.
This is used in a subsequent patch to supply the data to module signing where the signature is in the form of a PKCS#7 message with detached data, whereby the detached data is the module content that is signed.
---
crypto/asymmetric_keys/pkcs7_verify.c | 25 +++++++++++++++++++++++++ include/crypto/pkcs7.h | 3 +++ 2 files changed, 28 insertions(+)
crypto/asymmetric_keys/pkcs7_verify.c +25 -0
@@ -382,3 +382,28 @@ int pkcs7_verify(struct pkcs7_message *pkcs7)return enopkg; }EXPORT_SYMBOL_GPL (pkcs7_verify); + +/** +* pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message +* @pkcs7: The PKCS#7 message +* @data: The data to be verified +* @datalen: The amount of data +* +* Supply the detached data needed to verify a PKCS#7 message. Note that no +* attempt to retain/pin the data is made. That is left to the caller. The +* data will not be modified by pkcs7_verify() and will not be freed when the +* PKCS#7 message is freed. +* +* Returns -EINVAL if data is already supplied in the message, 0 otherwise. +*/ +int pkcs7_supply_detached_data (struct pkcs7_message *pkcs7, +const void *data,size_t datalen) +{ +if (pkcs7->data) { +pr_debug ("Data already supplied\n" ); +return -EINVAL ; + } + pkcs7->data = data; + pkcs7->data_len = datalen; +return 0 ; +}
include/crypto/pkcs7.h +3 -0
@@ -34,3 +34,6 @@ extern int pkcs7_validate_trust(struct pkcs7_message *pkcs7,* pkcs7_verify.c */ extern int pkcs7_verify (struct pkcs7_message *pkcs7); + +extern int pkcs7_supply_detached_data (struct pkcs7_message *pkcs7, +const void *data,size_t datalen);
David Howells
maintainerPATCH 6/27 MODSIGN: Provide a utility to append a PKCS#7 signature to a module [ver #7]
In reply to David HowellsProvide a utility that:
(1) Digests a module using the specified hash algorithm (typically sha256).
[The digest can be dumped into a file by passing the '-d' flag]
(2) Generates a PKCS#7 message that:
(a) Has detached data (ie. the module content).
(b) Is signed with the specified private key.
(c) Refers to the specified X.509 certificate.
(d) Has an empty X.509 certificate list.
[The PKCS#7 message can be dumped into a file by passing the '-p' flag]
(3) Generates a signed module by concatenating the old module, the PKCS#7 message, a descriptor and a magic string. The descriptor contains the size of the PKCS#7 message and indicates the id_type as PKEY_ID_PKCS7.
(4) Either writes the signed module to the specified destination or renames it over the source module.
This allows module signing to reuse the PKCS#7 handling code that was added for PE file parsing for signed kexec.
Note that the utility is written in C and must be linked against the OpenSSL crypto library.
Note further that I have temporarily dropped support for handling externally created signatures until we can work out the best way to do those. Hopefully, whoever creates the signature can give me a PKCS#7 certificate.
---
include/crypto/public_key.h | 1 scripts/sign-file.c | 205 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100755 scripts/sign-file.c
include/crypto/public_key.h +1 -0
@@ -33,6 +33,7 @@ extern const struct public_key_algorithm *pkey_algo[PKEY_ALGO__LAST];enum pkey_id_type {PKEY_ID_PGP ,/* OpenPGP generated key ID */ PKEY_ID_X509 ,/* X.509 arbitrary subjectKeyIdentifier */ +PKEY_ID_PKCS7 ,/* Signature in PKCS#7 message */ PKEY_ID_TYPE__LAST };
scripts/sign-file.c new file +205 -0
205 lines not on this page · raw message
David Howells
maintainerPATCH 7/27 MODSIGN: Use PKCS#7 messages as module signatures [ver #7]
In reply to David HowellsMove to using PKCS#7 messages as module signatures because:
(1) We have to be able to support the use of X.509 certificates that don't have a subjKeyId set. We're currently relying on this to look up the X.509 certificate in the trusted keyring list.
(2) PKCS#7 message signed information blocks have a field that supplies the data required to match with the X.509 certificate that signed it.
(3) The PKCS#7 certificate carries fields that specify the digest algorithm used to generate the signature in a standardised way and the X.509 certificates specify the public key algorithm in a standardised way - so we don't need our own methods of specifying these.
(4) We now have PKCS#7 message support in the kernel for signed kexec purposes and we can make use of this.
To make this work, the old sign-file script has been replaced with a program that needs compiling in a previous patch. The rules to build it are added here.
---
Makefile | 2 init/Kconfig | 1 kernel/module_signing.c | 220 +++++-------------------- scripts/Makefile | 2 scripts/sign-file | 421 ----------------------------------------------- 5 files changed, 48 insertions(+), 598 deletions(-) delete mode 100755 scripts/sign-file
Makefile +1 -1
@@ -873,7 +873,7 @@ ifdef CONFIG_MODULE_SIG_ALL MODSECKEY = ./signing_key.priv MODPUBKEY = ./signing_key.x509 export MODPUBKEY -mod_sign_cmd = perl $(srctree)/scripts/sign-file $(CONFIG_MODULE_SIG_HASH) $(MODSECKEY) $(MODPUBKEY) +mod_sign_cmd = scripts/sign-file $(CONFIG_MODULE_SIG_HASH) $(MODSECKEY) $(MODPUBKEY) else mod_sign_cmd = true endif
init/Kconfig +1 -0
@@ -1869,6 +1869,7 @@ config MODULE_SIG select ASN1 select OID_REGISTRY select X509_CERTIFICATE_PARSER + select PKCS7_MESSAGE_PARSER help Check modules for valid signatures upon load: the signature is simply appended to the module. For more information see
kernel/module_signing.c +44 -176
269 lines not on this page · raw message
scripts/Makefile +2 -0
@@ -16,9 +16,11 @@ hostprogs-$(CONFIG_VT) += conmakehash hostprogs-$(BUILD_C_RECORDMCOUNT) += recordmcount hostprogs-$(CONFIG_BUILDTIME_EXTABLE_SORT) += sortextable hostprogs-$(CONFIG_ASN1) += asn1_compiler +hostprogs-$(CONFIG_MODULE_SIG) += sign-file HOSTCFLAGS_sortextable.o = -I$(srctree)/tools/include HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include +HOSTLOADLIBES_sign-file = -lcrypto always := $(hostprogs-y) $(hostprogs-m)
scripts/sign-file deleted +0 -421
421 lines not on this page · raw message
David Howells
maintainerPATCH 8/27 sign-file: Add option to only create signature file [ver #7]
In reply to David HowellsFrom: Luis R. Rodriguez <[email protected]>
Make the -d option (which currently isn't actually wired to anything) write out the PKCS#7 message as per the -p option and then exit without either modifying the source or writing out a compound file of the source, signature and metadata.
This will be useful when firmware signature support is added upstream as firmware will be left intact, and we'll only require the signature file. The descriptor is implicit by file extension and the file's own size.
---
scripts/sign-file.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
scripts/sign-file.c +10 -3
@@ -86,13 +86,14 @@ int main(int argc, char **argv)char *hash_algo =NULL ;char *private_key_name, *x509_name, *module_name, *dest_name;bool save_pkcs7 = false, replace_orig; +bool sign_only = false;unsigned char buf[4096 ];unsigned long module_size, pkcs7_size;const EVP_MD *digest_algo;EVP_PKEY *private_key;PKCS7 *pkcs7;X509 *x509; -BIO *b, *bd, *bm; +BIO *b, *bd =NULL , *bm;int opt, n;ERR_load_crypto_strings ();
@@ -102,6 +103,7 @@ int main(int argc, char **argv) opt =getopt (argc, argv,"dp" );switch (opt) {case 'p' : save_pkcs7 = true;break ; +case 'd' : sign_only = true; save_pkcs7 = true;break ;case -1 :break ;default :format (); }
@@ -148,8 +150,10 @@ int main(int argc, char **argv)/* Open the destination file now so that we can shovel the module data * across as we read it. */ - bd =BIO_new_file (dest_name,"wb" ); -ERR (!bd,"%s" , dest_name); +if (!sign_only) { + bd =BIO_new_file (dest_name,"wb" ); +ERR (!bd,"%s" , dest_name); + }/* Digest the module data. */ OpenSSL_add_all_digests ();
@@ -180,6 +184,9 @@ int main(int argc, char **argv)BIO_free (b); } +if (sign_only) +return 0 ; +/* Append the marker and the PKCS#7 message to the destination file */ ERR (BIO_reset (bm) <0 ,"%s" , module_name); while ((n = BIO_read(bm, buf, sizeof(buf))),
David Howells
maintainerPATCH 9/27 system_keyring.c doesn't need to #include module-internal.h [ver #7]
In reply to David Howellssystem_keyring.c doesn't need to #include module-internal.h as it doesn't use the one thing that exports. Remove the inclusion.
---
kernel/system_keyring.c | 1 - 1 file changed, 1 deletion(-)
kernel/system_keyring.c +0 -1
@@ -16,7 +16,6 @@#include <linux/err.h> #include <keys/asymmetric-type.h> #include <keys/system_keyring.h> -#include "module-internal.h" struct key *system_trusted_keyring;EXPORT_SYMBOL_GPL (system_trusted_keyring);
David Howells
maintainerPATCH 10/27 MODSIGN: Extract the blob PKCS#7 signature verifier from module signing [ver #7]
In reply to David HowellsExtract the function that drives the PKCS#7 signature verification given a data blob and a PKCS#7 blob out from the module signing code and lump it with the system keyring code as it's generic. This makes it independent of module config options and opens it to use by the firmware loader.
---
include/keys/system_keyring.h | 5 ++++ init/Kconfig | 29 ++++++++++++++++-------- kernel/module_signing.c | 44 +----------------------------------- kernel/system_keyring.c | 50 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 53 deletions(-)
include/keys/system_keyring.h +5 -0
@@ -28,4 +28,9 @@ static inline struct key *get_system_trusted_keyring(void) }#endif +#ifdef CONFIG_SYSTEM_DATA_VERIFICATION +extern int system_verify_data (const void *data,unsigned long len, +const void *raw_pkcs7,size_t pkcs7_len); +#endif +#endif /* _KEYS_SYSTEM_KEYRING_H */
init/Kconfig +19 -10
@@ -1752,6 +1752,24 @@ config SYSTEM_TRUSTED_KEYRING Keys in this keyring are used by module signature checking. +config SYSTEM_DATA_VERIFICATION + def_bool n + select SYSTEM_TRUSTED_KEYRING + select KEYS + select CRYPTO + select ASYMMETRIC_KEY_TYPE + select ASYMMETRIC_PUBLIC_KEY_SUBTYPE + select PUBLIC_KEY_ALGO_RSA + select ASN1 + select OID_REGISTRY + select X509_CERTIFICATE_PARSER + select PKCS7_MESSAGE_PARSER + help + Provide PKCS#7 message verification using the contents of the system + trusted keyring to provide public keys. This then can be used for + module verification, kexec image verification and firmware blob + verification. + config PROFILING bool "Profiling support" help
@@ -1860,16 +1878,7 @@ config MODULE_SRCVERSION_ALL config MODULE_SIG bool "Module signature verification" depends on MODULES - select SYSTEM_TRUSTED_KEYRING - select KEYS - select CRYPTO - select ASYMMETRIC_KEY_TYPE - select ASYMMETRIC_PUBLIC_KEY_SUBTYPE - select PUBLIC_KEY_ALGO_RSA - select ASN1 - select OID_REGISTRY - select X509_CERTIFICATE_PARSER - select PKCS7_MESSAGE_PARSER + select SYSTEM_DATA_VERIFICATION help Check modules for valid signatures upon load: the signature is simply appended to the module. For more information see
kernel/module_signing.c +1 -43
62 lines not on this page · raw message
kernel/system_keyring.c +50 -0
59 lines not on this page · raw message
David Howells
maintainerPATCH 11/27 modsign: Abort modules_install when signing fails [ver #7]
In reply to David HowellsFrom: David Woodhouse <[email protected]>
---
scripts/Makefile.modinst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/Makefile.modinst +1 -1
@@ -22,7 +22,7 @@ quiet_cmd_modules_install = INSTALL $@ mkdir -p $(2) ; \ cp $@ $(2) ; \ $(mod_strip_cmd) $(2)/$(notdir $@) ; \ - $(mod_sign_cmd) $(2)/$(notdir $@) $(patsubst %,|| true,$(KBUILD_EXTMOD)) ; \ + $(mod_sign_cmd) $(2)/$(notdir $@) $(patsubst %,|| true,$(KBUILD_EXTMOD)) && \ $(mod_compress_cmd) $(2)/$(notdir $@) # Modules built outside the kernel source tree go into extra by default
David Howells
maintainerPATCH 12/27 modsign: Allow password to be specified for signing key [ver #7]
In reply to David HowellsFrom: David Woodhouse <[email protected]>
We don't want this in the Kconfig since it might then get exposed in /proc/config.gz. So make it a parameter to Kbuild instead. This also means we don't have to jump through hoops to strip quotes from it, as we would if it was a config option.
---
Documentation/kbuild/kbuild.txt | 5 +++++ Documentation/module-signing.txt | 3 +++ scripts/sign-file.c | 27 ++++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 1 deletion(-)
Documentation/kbuild/kbuild.txt +5 -0
@@ -174,6 +174,11 @@ The output directory is often set using "O=..." on the commandline. The value can be overridden in which case the default value is ignored. +KBUILD_SIGN_PIN +-------------------------------------------------- +This variable allows a passphrase or PIN to be passed to the sign-file +utility when signing kernel modules, if the private key requires such. + KBUILD_MODPOST_WARN -------------------------------------------------- KBUILD_MODPOST_WARN can be set to avoid errors in case of undefined
Documentation/module-signing.txt +3 -0
@@ -194,6 +194,9 @@ The hash algorithm used does not have to match the one configured, but if it doesn't, you should make sure that hash algorithm is either built into the kernel or can be loaded without requiring itself. +If the private key requires a passphrase or PIN, it can be provided in the +$KBUILD_SIGN_PIN environment variable. + ============================ SIGNED MODULES AND STRIPPING
scripts/sign-file.c +26 -1
@@ -80,6 +80,27 @@ static void drain_openssl_errors(void) } \ }while (0 ) +static const char *key_pass; + +static int pem_pw_cb (char *buf,int len,int w,void *v) +{ +int pwlen; + +if (!key_pass) +return -1 ; + + pwlen =strlen (key_pass); +if (pwlen >= len) +return -1 ; + +strcpy (buf, key_pass); + +/* If it's wrong, don't keep trying it. */ + key_pass =NULL ; + +return pwlen; +} +int main (int argc,char **argv) {struct module_signature sig_info = { .id_type =PKEY_ID_PKCS7 };
@@ -96,9 +117,12 @@ int main(int argc, char **argv)BIO *b, *bd =NULL , *bm;int opt, n; +OpenSSL_add_all_algorithms ();ERR_load_crypto_strings ();ERR_clear_error (); + key_pass =getenv ("KBUILD_SIGN_PIN" ); + do { opt = getopt(argc, argv, "dp"); switch (opt) {
@@ -132,7 +156,8 @@ int main(int argc, char **argv)*/ b =BIO_new_file (private_key_name,"rb" );ERR (!b,"%s" , private_key_name); - private_key =PEM_read_bio_PrivateKey (b,NULL ,NULL ,NULL ); + private_key =PEM_read_bio_PrivateKey (b,NULL , pem_pw_cb,NULL ); +ERR (!private_key,"%s" , private_key_name);BIO_free (b); b =BIO_new_file (x509_name,"rb" );
David Howells
maintainerPATCH 13/27 modsign: Allow signing key to be PKCS#11 [ver #7]
In reply to David HowellsFrom: David Woodhouse <[email protected]>
This is only the key; the corresponding *cert* still needs to be in $(topdir)/signing_key.x509. And there's no way to actually use this from the build system yet.
---
scripts/sign-file.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-)
scripts/sign-file.c +24 -5
@@ -22,6 +22,7 @@#include <openssl/pem.h> #include <openssl/pkcs7.h> #include <openssl/err.h> +#include <openssl/engine.h> struct module_signature {uint8_t algo;/* Public-key crypto algorithm [0] */
@@ -154,11 +155,29 @@ int main(int argc, char **argv)/* Read the private key and the X.509 cert the PKCS#7 message * will point to. */ - b =BIO_new_file (private_key_name,"rb" ); -ERR (!b,"%s" , private_key_name); - private_key =PEM_read_bio_PrivateKey (b,NULL , pem_pw_cb,NULL ); -ERR (!private_key,"%s" , private_key_name); -BIO_free (b); +if (!strncmp (private_key_name,"pkcs11:" ,7 )) { +ENGINE *e; + +ENGINE_load_builtin_engines (); +drain_openssl_errors (); + e =ENGINE_by_id ("pkcs11" ); +ERR (!e,"Load PKCS#11 ENGINE" ); +if (ENGINE_init (e)) +drain_openssl_errors (); +else +ERR (1 ,"ENGINE_init" ); +if (key_pass) +ERR (!ENGINE_ctrl_cmd_string (e,"PIN" , key_pass,0 ),"Set PKCS#11 PIN" ); + private_key =ENGINE_load_private_key (e, private_key_name,NULL , +NULL ); +ERR (!private_key,"%s" , private_key_name); + }else { + b =BIO_new_file (private_key_name,"rb" ); +ERR (!b,"%s" , private_key_name); + private_key =PEM_read_bio_PrivateKey (b,NULL , pem_pw_cb,NULL ); +ERR (!private_key,"%s" , private_key_name); +BIO_free (b); + } b =BIO_new_file (x509_name,"rb" );ERR (!b,"%s" , x509_name);
David Howells
maintainerPATCH 14/27 modsign: Allow external signing key to be specified [ver #7]
In reply to David HowellsFrom: David Woodhouse <[email protected]>
---
Documentation/module-signing.txt | 31 ++++++++++++++++++++++++++----- Makefile | 2 +- init/Kconfig | 14 ++++++++++++++ kernel/Makefile | 5 +++++ 4 files changed, 46 insertions(+), 6 deletions(-)
Documentation/module-signing.txt +26 -5
@@ -88,6 +88,22 @@ This has a number of options available: than being a module) so that modules signed with that algorithm can have their signatures checked without causing a dependency loop. + (4) "File name or PKCS#11 URI of module signing key" (CONFIG_MODULE_SIG_KEY) + + Setting this option to something other than its default of + "signing_key.priv" will disable the autogeneration of signing keys and + allow the kernel modules to be signed with a key of your choosing. + The string provided should identify a file containing a private key + in PEM form, or — on systems where the OpenSSL ENGINE_pkcs11 is + appropriately installed — a PKCS#11 URI as defined by RFC7512. + + If the PEM file containing the private key is encrypted, or if the + PKCS#11 token requries a PIN, this can be provided at build time by + means of the KBUILD_SIGN_PIN variable. + + The corresponding X.509 certificate in DER form should still be placed + in a file named signing_key.x509 in the top-level build directory. + ======================= GENERATING SIGNING KEYS
@@ -100,8 +116,9 @@ it can be deleted or stored securely. The public key gets built into the kernel so that it can be used to check the signatures as the modules are loaded. -Under normal conditions, the kernel build will automatically generate a new -keypair using openssl if one does not exist in the files: +Under normal conditions, when CONFIG_MODULE_SIG_KEY is unchanged from its +default of "signing_key.priv", the kernel build will automatically generate +a new keypair using openssl if one does not exist in the files: signing_key.priv signing_key.x509
@@ -135,8 +152,12 @@ kernel sources tree and the openssl command. The following is an example to generate the public/private key files: openssl req -new -nodes -utf8 -sha256 -days 36500 -batch -x509 \ - -config x509.genkey -outform DER -out signing_key.x509 \ - -keyout signing_key.priv + -config x509.genkey -outform PEM -out kernel_key.pem \ + -keyout kernel_key.pem + +The full pathname for the resulting kernel_key.pem file can then be specified +in the CONFIG_MODULE_SIG_KEY option, and the certificate and key therein will +be used instead of an autogenerated keypair. =========================
@@ -181,7 +202,7 @@ To manually sign a module, use the scripts/sign-file tool available in the Linux kernel source tree. The script requires 4 arguments: 1. The hash algorithm (e.g., sha256) - 2. The private key filename + 2. The private key filename or PKCS#11 URI 3. The public key filename 4. The kernel module to be signed
Makefile +1 -1
@@ -870,7 +870,7 @@ INITRD_COMPRESS-$(CONFIG_RD_LZ4) := lz4 # export INITRD_COMPRESS := $(INITRD_COMPRESS-y) ifdef CONFIG_MODULE_SIG_ALL -MODSECKEY = ./signing_key.priv +MODSECKEY = $(CONFIG_MODULE_SIG_KEY) MODPUBKEY = ./signing_key.x509 export MODPUBKEY mod_sign_cmd = scripts/sign-file $(CONFIG_MODULE_SIG_HASH) $(MODSECKEY) $(MODPUBKEY)
init/Kconfig +14 -0
@@ -1948,6 +1948,20 @@ config MODULE_SIG_HASH default "sha384" if MODULE_SIG_SHA384 default "sha512" if MODULE_SIG_SHA512 +config MODULE_SIG_KEY + string "File name or PKCS#11 URI of module signing key" + default "signing_key.priv" + depends on MODULE_SIG + help + Provide the file name of a private key in PKCS#8 PEM format, or + a PKCS#11 URI according to RFC7512. The corresponding X.509 + certificate in DER form should be present in signing_key.x509 + in the top-level build directory. + + If this option is unchanged from its default "signing_key.priv", + then the kernel will automatically generate the private key and + certificate as described in Documentation/module-signing.txt + config MODULE_COMPRESS bool "Compress modules on installation" depends on MODULES
kernel/Makefile +5 -0
@@ -170,6 +170,10 @@ ifndef CONFIG_MODULE_SIG_HASH $(error Could not determine digest type to use from kernel config) endif +# We do it this way rather than having a boolean option for enabling an +# external private key, because 'make randconfig' might enable such a +# boolean option and we unfortunately can't make it depend on !RANDCONFIG. +ifeq ($(CONFIG_MODULE_SIG_KEY),"signing_key.priv") signing_key.priv signing_key.x509: x509.genkey @echo "###" @echo "### Now generating an X.509 key pair to be used for signing modules."
@@ -207,3 +211,4 @@ x509.genkey: @echo >>x509.genkey "subjectKeyIdentifier=hash" @echo >>x509.genkey "authorityKeyIdentifier=keyid" endif +endif
David Howells
maintainerPATCH 15/27 modsign: Extract signing cert from CONFIG_MODULE_SIG_KEY if needed [ver #7]
In reply to David HowellsFrom: David Woodhouse <[email protected]>
Where an external PEM file or PKCS#11 URI is given, we can get the cert from it for ourselves instead of making the user drop signing_key.x509 in place for us.
---
Documentation/module-signing.txt | 11 +-- init/Kconfig | 8 +- kernel/Makefile | 38 +++++++++++ scripts/Makefile | 3 + scripts/extract-cert.c | 132 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 181 insertions(+), 11 deletions(-) create mode 100644 scripts/extract-cert.c
Documentation/module-signing.txt +5 -6
@@ -93,17 +93,16 @@ This has a number of options available: Setting this option to something other than its default of "signing_key.priv" will disable the autogeneration of signing keys and allow the kernel modules to be signed with a key of your choosing. - The string provided should identify a file containing a private key - in PEM form, or — on systems where the OpenSSL ENGINE_pkcs11 is - appropriately installed — a PKCS#11 URI as defined by RFC7512. + The string provided should identify a file containing both a private + key and its corresponding X.509 certificate in PEM form, or — on + systems where the OpenSSL ENGINE_pkcs11 is functional — a PKCS#11 URI + as defined by RFC7512. In the latter case, the PKCS#11 URI should + reference both a certificate and a private key. If the PEM file containing the private key is encrypted, or if the PKCS#11 token requries a PIN, this can be provided at build time by means of the KBUILD_SIGN_PIN variable. - The corresponding X.509 certificate in DER form should still be placed - in a file named signing_key.x509 in the top-level build directory. - ======================= GENERATING SIGNING KEYS
init/Kconfig +4 -4
@@ -1953,10 +1953,10 @@ config MODULE_SIG_KEY default "signing_key.priv" depends on MODULE_SIG help - Provide the file name of a private key in PKCS#8 PEM format, or - a PKCS#11 URI according to RFC7512. The corresponding X.509 - certificate in DER form should be present in signing_key.x509 - in the top-level build directory. + Provide the file name of a private key/certificate in PEM format, + or a PKCS#11 URI according to RFC7512. The file should contain, or + the URI should identify, both the certificate and its corresponding + private key. If this option is unchanged from its default "signing_key.priv", then the kernel will automatically generate the private key and
kernel/Makefile +38 -0
@@ -210,5 +210,43 @@ x509.genkey: @echo >>x509.genkey "keyUsage=digitalSignature" @echo >>x509.genkey "subjectKeyIdentifier=hash" @echo >>x509.genkey "authorityKeyIdentifier=keyid" +else +# For external (PKCS#11 or PEM) key, we need to obtain the certificate from +# CONFIG_MODULE_SIG_KEY automatically. +quiet_cmd_extract_der = CERT_DER $(2) + cmd_extract_der = scripts/extract-cert "$(2)" signing_key.x509 + +# CONFIG_MODULE_SIG_KEY is either a PKCS#11 URI or a filename. It is +# surrounded by quotes, and may contain spaces. To strip the quotes +# with $(patsubst) we need to turn the spaces into something else. +# And if it's a filename, those spaces need to be escaped as '\ ' in +# order to use it in dependencies or $(wildcard). +space := +space += +space_escape := %%%SPACE%%% +X509_SOURCE_temp := $(subst $(space),$(space_escape),$(CONFIG_MODULE_SIG_KEY)) +# We need this to check for absolute paths or PKCS#11 URIs. +X509_SOURCE_ONEWORD := $(patsubst "%",%,$(X509_SOURCE_temp)) +# This is the actual source filename/URI without the quotes +X509_SOURCE := $(subst $(space_escape),$(space),$(X509_SOURCE_ONEWORD)) +# This\ version\ with\ spaces\ escaped\ for\ $(wildcard)\ and\ dependencies +X509_SOURCE_ESCAPED := $(subst $(space_escape),\$(space),$(X509_SOURCE_ONEWORD)) + +ifeq ($(patsubst pkcs11:%,%,$(X509_SOURCE_ONEWORD)),$(X509_SOURCE_ONEWORD)) +# If it's a filename, depend on it. +X509_DEP := $(X509_SOURCE_ESCAPED) +ifeq ($(patsubst /%,%,$(X509_SOURCE_ONEWORD)),$(X509_SOURCE_ONEWORD)) +ifeq ($(wildcard $(X509_SOURCE_ESCAPED)),) +ifneq ($(wildcard $(srctree)/$(X509_SOURCE_ESCAPED)),) +# Non-absolute filename, found in source tree and not build tree +X509_SOURCE := $(srctree)/$(X509_SOURCE) +X509_DEP := $(srctree)/$(X509_SOURCE_ESCAPED) +endif +endif +endif +endif + +signing_key.x509: scripts/extract-cert include/config/module/sig/key.h $(X509_DEP) + $(call cmd,extract_der,$(X509_SOURCE)) endif endif
scripts/Makefile +2 -1
@@ -16,11 +16,12 @@ hostprogs-$(CONFIG_VT) += conmakehash hostprogs-$(BUILD_C_RECORDMCOUNT) += recordmcount hostprogs-$(CONFIG_BUILDTIME_EXTABLE_SORT) += sortextable hostprogs-$(CONFIG_ASN1) += asn1_compiler -hostprogs-$(CONFIG_MODULE_SIG) += sign-file +hostprogs-$(CONFIG_MODULE_SIG) += sign-file extract-cert HOSTCFLAGS_sortextable.o = -I$(srctree)/tools/include HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include HOSTLOADLIBES_sign-file = -lcrypto +HOSTLOADLIBES_extract-cert = -lcrypto always := $(hostprogs-y) $(hostprogs-m)
scripts/extract-cert.c new file +132 -0
132 lines not on this page · raw message
David Howells
maintainerPATCH 16/27 modsign: Use single PEM file for autogenerated key [ver #7]
In reply to David HowellsFrom: David Woodhouse <[email protected]>
The current rule for generating signing_key.priv and signing_key.x509 is a classic example of a bad rule which has a tendency to break parallel make. When invoked to create *either* target, it generates the other target as a side-effect that make didn't predict.
So let's switch to using a single file signing_key.pem which contains both key and certificate. That matches what we do in the case of an external key specified by CONFIG_MODULE_SIG_KEY anyway, so it's also slightly cleaner.
---
.gitignore | 1 + Documentation/module-signing.txt | 9 ++++----- Makefile | 4 ++-- init/Kconfig | 4 ++-- kernel/Makefile | 15 +++++++-------- 5 files changed, 16 insertions(+), 17 deletions(-)
.gitignore +1 -0
@@ -97,6 +97,7 @@ GTAGS # Leavings from module signing # extra_certificates +signing_key.pem signing_key.priv signing_key.x509 x509.genkey
Documentation/module-signing.txt +4 -5
@@ -91,7 +91,7 @@ This has a number of options available: (4) "File name or PKCS#11 URI of module signing key" (CONFIG_MODULE_SIG_KEY) Setting this option to something other than its default of - "signing_key.priv" will disable the autogeneration of signing keys and + "signing_key.pem" will disable the autogeneration of signing keys and allow the kernel modules to be signed with a key of your choosing. The string provided should identify a file containing both a private key and its corresponding X.509 certificate in PEM form, or — on
@@ -116,11 +116,10 @@ kernel so that it can be used to check the signatures as the modules are loaded. Under normal conditions, when CONFIG_MODULE_SIG_KEY is unchanged from its -default of "signing_key.priv", the kernel build will automatically generate -a new keypair using openssl if one does not exist in the files: +default, the kernel build will automatically generate a new keypair using +openssl if one does not exist in the file: - signing_key.priv - signing_key.x509 + signing_key.pem during the building of vmlinux (the public part of the key needs to be built into vmlinux) using parameters in the:
Makefile +2 -2
@@ -1173,8 +1173,8 @@ MRPROPER_DIRS += include/config usr/include include/generated \ arch/*/include/generated .tmp_objdiff MRPROPER_FILES += .config .config.old .version .old_version \ Module.symvers tags TAGS cscope* GPATH GTAGS GRTAGS GSYMS \ - signing_key.priv signing_key.x509 x509.genkey \ - extra_certificates signing_key.x509.keyid \ + signing_key.pem signing_key.priv signing_key.x509 \ + x509.genkey extra_certificates signing_key.x509.keyid \ signing_key.x509.signer vmlinux-gdb.py # clean - Delete most, but leave enough to build external modules
init/Kconfig +2 -2
@@ -1950,7 +1950,7 @@ config MODULE_SIG_HASH config MODULE_SIG_KEY string "File name or PKCS#11 URI of module signing key" - default "signing_key.priv" + default "signing_key.pem" depends on MODULE_SIG help Provide the file name of a private key/certificate in PEM format,
@@ -1958,7 +1958,7 @@ config MODULE_SIG_KEY the URI should identify, both the certificate and its corresponding private key. - If this option is unchanged from its default "signing_key.priv", + If this option is unchanged from its default "signing_key.pem", then the kernel will automatically generate the private key and certificate as described in Documentation/module-signing.txt
kernel/Makefile +7 -8
@@ -173,8 +173,8 @@ endif # We do it this way rather than having a boolean option for enabling an # external private key, because 'make randconfig' might enable such a # boolean option and we unfortunately can't make it depend on !RANDCONFIG. -ifeq ($(CONFIG_MODULE_SIG_KEY),"signing_key.priv") -signing_key.priv signing_key.x509: x509.genkey +ifeq ($(CONFIG_MODULE_SIG_KEY),"signing_key.pem") +signing_key.pem: x509.genkey @echo "###" @echo "### Now generating an X.509 key pair to be used for signing modules." @echo "###"
@@ -185,8 +185,8 @@ signing_key.priv signing_key.x509: x509.genkey @echo "###" openssl req -new -nodes -utf8 -$(CONFIG_MODULE_SIG_HASH) -days 36500 \ -batch -x509 -config x509.genkey \ - -outform DER -out signing_key.x509 \ - -keyout signing_key.priv 2>&1 + -outform PEM -out signing_key.pem \ + -keyout signing_key.pem 2>&1 @echo "###" @echo "### Key pair generated." @echo "###"
@@ -210,9 +210,9 @@ x509.genkey: @echo >>x509.genkey "keyUsage=digitalSignature" @echo >>x509.genkey "subjectKeyIdentifier=hash" @echo >>x509.genkey "authorityKeyIdentifier=keyid" -else -# For external (PKCS#11 or PEM) key, we need to obtain the certificate from -# CONFIG_MODULE_SIG_KEY automatically. +endif + +# We need to obtain the certificate from CONFIG_MODULE_SIG_KEY. quiet_cmd_extract_der = CERT_DER $(2) cmd_extract_der = scripts/extract-cert "$(2)" signing_key.x509
@@ -249,4 +249,3 @@ endif signing_key.x509: scripts/extract-cert include/config/module/sig/key.h $(X509_DEP) $(call cmd,extract_der,$(X509_SOURCE)) endif -endif
David Howells
maintainerPATCH 17/27 modsign: Add explicit CONFIG_SYSTEM_TRUSTED_KEYS option [ver #7]
In reply to David HowellsFrom: David Woodhouse <[email protected]>
Let the user explicitly provide a file containing trusted keys, instead of just automatically finding files matching *.x509 in the build tree and trusting whatever we find. This really ought to be an *explicit* configuration, and the build rules for dealing with the files were fairly painful too.
---
Documentation/module-signing.txt | 15 +++-- init/Kconfig | 13 ++++ kernel/Makefile | 125 ++++++++++++++++++++------------------ 3 files changed, 89 insertions(+), 64 deletions(-)
Documentation/module-signing.txt +11 -4
@@ -88,6 +88,7 @@ This has a number of options available: than being a module) so that modules signed with that algorithm can have their signatures checked without causing a dependency loop. + (4) "File name or PKCS#11 URI of module signing key" (CONFIG_MODULE_SIG_KEY) Setting this option to something other than its default of
@@ -104,6 +105,13 @@ This has a number of options available: means of the KBUILD_SIGN_PIN variable. + (5) "Additional X.509 keys for default system keyring" (CONFIG_SYSTEM_TRUSTED_KEYS) + + This option can be set to the filename of a PEM-encoded file containing + additional certificates which will be included in the system keyring by + default. + + ======================= GENERATING SIGNING KEYS =======================
@@ -171,10 +179,9 @@ in a keyring called ".system_keyring" that can be seen by: 302d2d52 I------ 1 perm 1f010000 0 0 asymmetri Fedora kernel signing key: d69a84e6bce3d216b979e9505b3e3ef9a7118079: X509.RSA a7118079 [] ... -Beyond the public key generated specifically for module signing, any file -placed in the kernel source root directory or the kernel build root directory -whose name is suffixed with ".x509" will be assumed to be an X.509 public key -and will be added to the keyring. +Beyond the public key generated specifically for module signing, additional +trusted certificates can be provided in a PEM-encoded file referenced by the +CONFIG_SYSTEM_TRUSTED_KEYS configuration option. Further, the architecture code may take public keys from a hardware store and add those in also (e.g. from the UEFI key database).
init/Kconfig +13 -0
@@ -1752,6 +1752,19 @@ config SYSTEM_TRUSTED_KEYRING Keys in this keyring are used by module signature checking. +config SYSTEM_TRUSTED_KEYS + string "Additional X.509 keys for default system keyring" + depends on SYSTEM_TRUSTED_KEYRING + help + If set, this option should be the filename of a PEM-formatted file + containing trusted X.509 certificates to be included in the default + system keyring. Any certificate used for module signing is implicitly + also trusted. + + NOTE: If you previously provided keys for the system keyring in the + form of DER-encoded *.x509 files in the top-level build directory, + those are no longer used. You will need to set this option instead. + config SYSTEM_DATA_VERIFICATION def_bool n select SYSTEM_TRUSTED_KEYRING
kernel/Makefile +65 -60
151 lines not on this page · raw message
David Howells
maintainerPATCH 18/27 PKCS#7: Check content type and versions [ver #7]
In reply to David HowellsWe only support PKCS#7 signed-data [RFC2315 sec 9] content at the top level, so reject anything else. Further, check that the version numbers in SignedData and SignerInfo are 1 in both cases.
Note that we don't restrict the inner content type. In the PKCS#7 code we don't parse the data attached there, but merely verify the signature over it.
---
crypto/asymmetric_keys/pkcs7.asn1 | 6 +-- crypto/asymmetric_keys/pkcs7_parser.c | 75 +++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 4 deletions(-)
crypto/asymmetric_keys/pkcs7.asn1 +3 -3
@@ -1,12 +1,12 @@ PKCS7ContentInfo ::= SEQUENCE { - contentType ContentType, + contentType ContentType ({ pkcs7_check_content_type }), content [0] EXPLICIT SignedData OPTIONAL } ContentType ::= OBJECT IDENTIFIER ({ pkcs7_note_OID }) SignedData ::= SEQUENCE { - version INTEGER, + version INTEGER ({ pkcs7_note_signeddata_version }), digestAlgorithms DigestAlgorithmIdentifiers, contentInfo ContentInfo, certificates CHOICE {
@@ -68,7 +68,7 @@ SignerInfos ::= CHOICE { } SignerInfo ::= SEQUENCE { - version INTEGER, + version INTEGER ({ pkcs7_note_signerinfo_version }), issuerAndSerialNumber IssuerAndSerialNumber, digestAlgorithm DigestAlgorithmIdentifier ({ pkcs7_sig_note_digest_algo }), authenticatedAttributes CHOICE {
crypto/asymmetric_keys/pkcs7_parser.c +74 -1
87 lines not on this page · raw message
David Howells
maintainerPATCH 19/27 X.509: Change recorded SKID & AKID to not include Subject or Issuer [ver #7]
In reply to David HowellsThe key identifiers fabricated from an X.509 certificate are currently:
(A) Concatenation of serial number and issuer
(B) Concatenation of subject and subjectKeyID (SKID)
When verifying one X.509 certificate with another, the AKID in the target can be used to match the authoritative certificate. The AKID can specify the match in one or both of two ways:
(1) Compare authorityCertSerialNumber and authorityCertIssuer from the AKID to identifier (A) above.
(2) Compare keyIdentifier from the AKID plus the issuer from the target certificate to identifier (B) above.
When verifying a PKCS#7 message, the only available comparison is between the IssuerAndSerialNumber field and identifier (A) above.
However, a subsequent patch adds CMS support. Whilst CMS still supports a match on IssuerAndSerialNumber as for PKCS#7, it also supports an alternative - which is the SubjectKeyIdentifier field. This is used to match to an X.509 certificate on the SKID alone. No subject information is available to be used.
To this end change the fabrication of (B) above to be from the X.509 SKID alone. The AKID in keyIdentifier form then only matches on that and does not include the issuer.
---
crypto/asymmetric_keys/x509_cert_parser.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
crypto/asymmetric_keys/x509_cert_parser.c +2 -6
@@ -454,9 +454,7 @@ int x509_process_extension(void *context, size_t hdrlen, ctx->cert->raw_skid_size = vlen; ctx->cert->raw_skid = v; - kid =asymmetric_key_generate_id (ctx->cert->raw_subject, - ctx->cert->raw_subject_size, - v, vlen); + kid =asymmetric_key_generate_id (v, vlen,"" ,0 );if (IS_ERR (kid))return PTR_ERR (kid); ctx->cert->skid = kid;
@@ -553,9 +551,7 @@ int x509_akid_note_kid(void *context, size_t hdrlen,if (ctx->cert->akid_skid)return 0 ; - kid =asymmetric_key_generate_id (ctx->cert->raw_issuer, - ctx->cert->raw_issuer_size, - value, vlen); + kid =asymmetric_key_generate_id (value, vlen,"" ,0 );if (IS_ERR (kid))return PTR_ERR (kid);pr_debug ("authkeyid %*phN\n" , kid->len, kid->data);
David Howells
maintainerPATCH 20/27 PKCS#7: Support CMS messages also [RFC5652] [ver #7]
In reply to David HowellsSince CMS is an evolution of PKCS#7, with much of the ASN.1 being compatible, add support for CMS signed-data messages also [RFC5652 sec 5].
---
crypto/asymmetric_keys/pkcs7.asn1 | 10 +++++ crypto/asymmetric_keys/pkcs7_parser.c | 62 +++++++++++++++++++++++++++++---- crypto/asymmetric_keys/pkcs7_parser.h | 5 ++- 3 files changed, 68 insertions(+), 9 deletions(-)
crypto/asymmetric_keys/pkcs7.asn1 +9 -1
@@ -69,7 +69,7 @@ SignerInfos ::= CHOICE { SignerInfo ::= SEQUENCE { version INTEGER ({ pkcs7_note_signerinfo_version }), - issuerAndSerialNumber IssuerAndSerialNumber, + sid SignerIdentifier, -- CMS variant, not PKCS#7 digestAlgorithm DigestAlgorithmIdentifier ({ pkcs7_sig_note_digest_algo }), authenticatedAttributes CHOICE { aaSet [0] IMPLICIT SetOfAuthenticatedAttribute
@@ -88,6 +88,12 @@ SignerInfo ::= SEQUENCE { } OPTIONAL } ({ pkcs7_note_signed_info }) +SignerIdentifier ::= CHOICE { + -- RFC5652 sec 5.3 + issuerAndSerialNumber IssuerAndSerialNumber, + subjectKeyIdentifier [0] IMPLICIT SubjectKeyIdentifier +} + IssuerAndSerialNumber ::= SEQUENCE { issuer Name ({ pkcs7_sig_note_issuer }), serialNumber CertificateSerialNumber ({ pkcs7_sig_note_serial })
@@ -95,6 +101,8 @@ IssuerAndSerialNumber ::= SEQUENCE { CertificateSerialNumber ::= INTEGER +SubjectKeyIdentifier ::= OCTET STRING ({ pkcs7_sig_note_skid }) + SetOfAuthenticatedAttribute ::= SET OF AuthenticatedAttribute AuthenticatedAttribute ::= SEQUENCE {
crypto/asymmetric_keys/pkcs7_parser.c +55 -7
114 lines not on this page · raw message
crypto/asymmetric_keys/pkcs7_parser.h +4 -1
@@ -33,7 +33,9 @@ struct pkcs7_signed_info {unsigned authattrs_len;const void *authattrs; -/* Issuing cert serial number and issuer's name */ +/* Issuing cert serial number and issuer's name [PKCS#7 or CMS ver 1] +* or issuing cert's SKID [CMS ver 3]. +*/ struct asymmetric_key_id *signing_cert_id;/* Message signature.
@@ -50,6 +52,7 @@ struct pkcs7_message {struct x509_certificate *certs;/* Certificate list */ struct x509_certificate *crl;/* Revocation list */ struct pkcs7_signed_info *signed_infos; +u8 version;/* Version of cert (1 -> PKCS#7 or CMS; 3 -> CMS) */ /* Content Data (or NULL) */ enum OID data_type;/* Type of Data */
David Howells
maintainerPATCH 21/27 sign-file: Generate CMS message as signature instead of PKCS#7 [ver #7]
In reply to David HowellsMake sign-file use the OpenSSL CMS routines to generate a message to be used as the signature blob instead of the PKCS#7 routines. This allows us to change how the matching X.509 certificate is selected. With PKCS#7 the only option is to match on the serial number and issuer fields of an X.509 certificate; with CMS, we also have the option of matching by subjectKeyId extension. The new behaviour is selected with the "-k" flag.
Without the -k flag specified, the output is pretty much identical to the PKCS#7 output.
Whilst we're at it, don't include the S/MIME capability list in the message as it's irrelevant to us.
---
scripts/sign-file.c | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-)
scripts/sign-file.c +27 -24
@@ -20,7 +20,7 @@#include <openssl/bio.h> #include <openssl/evp.h> #include <openssl/pem.h> -#include <openssl/pkcs7.h> +#include <openssl/cms.h> #include <openssl/err.h> #include <openssl/engine.h>
@@ -107,13 +107,14 @@ int main(int argc, char **argv)struct module_signature sig_info = { .id_type =PKEY_ID_PKCS7 };char *hash_algo =NULL ;char *private_key_name, *x509_name, *module_name, *dest_name; -bool save_pkcs7 = false, replace_orig; +bool save_cms = false, replace_orig;bool sign_only = false;unsigned char buf[4096 ]; -unsigned long module_size, pkcs7_size; +unsigned long module_size, cms_size; +unsigned int use_keyid =0 ;const EVP_MD *digest_algo;EVP_PKEY *private_key; -PKCS7 *pkcs7; +CMS_ContentInfo *cms;X509 *x509;BIO *b, *bd =NULL , *bm;int opt, n;
@@ -125,10 +126,11 @@ int main(int argc, char **argv) key_pass =getenv ("KBUILD_SIGN_PIN" ); do { - opt = getopt(argc, argv, "dp"); + opt = getopt(argc, argv, "dpk"); switch (opt) { - case 'p': save_pkcs7 = true; break; - case 'd': sign_only = true; save_pkcs7 = true; break; + case 'p': save_cms = true; break; + case 'd': sign_only = true; save_cms = true; break; + case 'k': use_keyid = CMS_USE_KEYID; break; case -1: break; default: format(); }
@@ -208,23 +210,24 @@ int main(int argc, char **argv) bm =BIO_new_file (module_name,"rb" );ERR (!bm,"%s" , module_name); -/* Load the PKCS#7 message from the digest buffer. */ - pkcs7 =PKCS7_sign (NULL ,NULL ,NULL ,NULL , -PKCS7_NOCERTS |PKCS7_PARTIAL |PKCS7_BINARY |PKCS7_DETACHED |PKCS7_STREAM ); -ERR (!pkcs7,"PKCS7_sign" ); +/* Load the CMS message from the digest buffer. */ + cms =CMS_sign (NULL ,NULL ,NULL ,NULL , +CMS_NOCERTS |CMS_PARTIAL |CMS_BINARY |CMS_DETACHED |CMS_STREAM ); +ERR (!cms,"CMS_sign" ); -ERR (!PKCS7_sign_add_signer (pkcs7, x509, private_key, digest_algo,PKCS7_NOCERTS |PKCS7_BINARY ), -"PKCS7_sign_add_signer" ); -ERR (PKCS7_final (pkcs7, bm,PKCS7_NOCERTS |PKCS7_BINARY ) <0 , -"PKCS7_final" ); +ERR (!CMS_add1_signer (cms, x509, private_key, digest_algo, +CMS_NOCERTS |CMS_BINARY |CMS_NOSMIMECAP | use_keyid), +"CMS_sign_add_signer" ); +ERR (CMS_final (cms, bm,NULL ,CMS_NOCERTS |CMS_BINARY ) <0 , +"CMS_final" ); -if (save_pkcs7) { -char *pkcs7_name; +if (save_cms) { +char *cms_name; -ERR (asprintf (&pkcs7_name,"%s.pkcs7" , module_name) <0 ,"asprintf" ); - b =BIO_new_file (pkcs7_name,"wb" ); -ERR (!b,"%s" , pkcs7_name); -ERR (i2d_PKCS7_bio_stream (b, pkcs7,NULL ,0 ) <0 ,"%s" , pkcs7_name); +ERR (asprintf (&cms_name,"%s.p7s" , module_name) <0 ,"asprintf" ); + b =BIO_new_file (cms_name,"wb" ); +ERR (!b,"%s" , cms_name); +ERR (i2d_CMS_bio_stream (b, cms,NULL ,0 ) <0 ,"%s" , cms_name);BIO_free (b); }
@@ -240,9 +243,9 @@ int main(int argc, char **argv)ERR (n <0 ,"%s" , module_name); module_size =BIO_number_written (bd); -ERR (i2d_PKCS7_bio_stream (bd, pkcs7,NULL ,0 ) <0 ,"%s" , dest_name); - pkcs7_size =BIO_number_written (bd) - module_size; - sig_info.sig_len =htonl (pkcs7_size); +ERR (i2d_CMS_bio_stream (bd, cms,NULL ,0 ) <0 ,"%s" , dest_name); + cms_size =BIO_number_written (bd) - module_size; + sig_info.sig_len =htonl (cms_size);ERR (BIO_write (bd, &sig_info,sizeof (sig_info)) <0 ,"%s" , dest_name);ERR (BIO_write (bd, magic_number,sizeof (magic_number) -1 ) <0 ,"%s" , dest_name);
David Howells
maintainerPATCH 22/27 extract-cert: Cope with multiple X.509 certificates in a single file [ver #7]
In reply to David HowellsFrom: David Woodhouse <[email protected]>
This is not required for the module signing key, although it doesn't do any harm — it just means that any additional certs in the PEM file are also trusted by the kernel.
But it does allow us to use the extract-cert tool for processing the extra certs from CONFIG_SYSTEM_TRUSTED_KEYS, instead of that horrid awk|base64 hack.
Also cope with being invoked with no input file, creating an empty output file as a result.
---
scripts/extract-cert.c | 58 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 12 deletions(-)
scripts/extract-cert.c +46 -12
@@ -73,17 +73,34 @@ static void drain_openssl_errors(void) }while (0 ) static const char *key_pass; +static BIO *wb; +static char *cert_dst; +int kbuild_verbose; + +static void write_cert (X509 *x509) +{ +char buf[200 ]; + +if (!wb) { + wb =BIO_new_file (cert_dst,"wb" ); +ERR (!wb,"%s" , cert_dst); + } +X509_NAME_oneline (X509_get_subject_name (x509), buf,sizeof (buf)); +ERR (!i2d_X509_bio (wb, x509), cert_dst); +if (kbuild_verbose) +fprintf (stderr,"Extracted cert: %s\n" , buf); +}int main (int argc,char **argv) { -char *cert_src, *cert_dst; -X509 *x509; -BIO *b; +char *cert_src;OpenSSL_add_all_algorithms ();ERR_load_crypto_strings ();ERR_clear_error (); + kbuild_verbose =atoi (getenv ("KBUILD_VERBOSE" )?:"0" ); + key_pass =getenv ("KBUILD_SIGN_PIN" );if (argc !=3 )
@@ -92,7 +109,13 @@ int main(int argc, char **argv) cert_src = argv[1 ]; cert_dst = argv[2 ]; -if (!strncmp (cert_src,"pkcs11:" ,7 )) { +if (!cert_src[0 ]) { +/* Invoked with no input; create empty file */ +FILE *f =fopen (cert_dst,"wb" ); +ERR (!f,"%s" , cert_dst); +fclose (f); +exit (0 ); + }else if (!strncmp (cert_src,"pkcs11:" ,7 )) {ENGINE *e;struct {const char *cert_id;
@@ -114,19 +137,30 @@ int main(int argc, char **argv)ERR (!ENGINE_ctrl_cmd_string (e,"PIN" , key_pass,0 ),"Set PKCS#11 PIN" );ENGINE_ctrl_cmd (e,"LOAD_CERT_CTRL" ,0 , &parms,NULL ,1 );ERR (!parms.cert,"Get X.509 from PKCS#11" ); - x509 = parms.cert; +write_cert (parms.cert); }else { +BIO *b; +X509 *x509; + b =BIO_new_file (cert_src,"rb" );ERR (!b,"%s" , cert_src); - x509 =PEM_read_bio_X509 (b,NULL ,NULL ,NULL ); -ERR (!x509,"%s" , cert_src); -BIO_free (b); + +while (1 ) { + x509 =PEM_read_bio_X509 (b,NULL ,NULL ,NULL ); +if (wb && !x509) { +unsigned long err =ERR_peek_last_error (); +if (ERR_GET_LIB (err) ==ERR_LIB_PEM && +ERR_GET_REASON (err) ==PEM_R_NO_START_LINE ) { +ERR_clear_error (); +break ; + } + } +ERR (!x509,"%s" , cert_src); +write_cert (x509); + } } - b =BIO_new_file (cert_dst,"wb" ); -ERR (!b,"%s" , cert_dst); -ERR (!i2d_X509_bio (b, x509), cert_dst); -BIO_free (b); +BIO_free (wb);return 0 ; }
David Howells
maintainerPATCH 23/27 modsign: Use extract-cert to process CONFIG_SYSTEM_TRUSTED_KEYS [ver #7]
In reply to David HowellsFrom: David Woodhouse <[email protected]>
Fix up the dependencies somewhat too, while we're at it.
---
kernel/Makefile | 25 ++++++++++++------------- kernel/system_certificates.S | 3 +++ scripts/Makefile | 3 ++- 3 files changed, 17 insertions(+), 14 deletions(-)
kernel/Makefile +12 -13
@@ -166,23 +166,22 @@ endef # ############################################################################### - ifeq ($(CONFIG_SYSTEM_TRUSTED_KEYRING),y) $(eval $(call config_filename,SYSTEM_TRUSTED_KEYS)) -SIGNING_X509-$(CONFIG_MODULE_SIG) += signing_key.x509 - -kernel/system_certificates.o: $(obj)/x509_certificate_list +# GCC doesn't include .incbin files in -MD generated dependencies (PR#66871) +$(obj)/system_certificates.o: $(obj)/x509_certificate_list -quiet_cmd_x509certs = CERTS $(SIGNING_X509-y) $(patsubst "%",%,$(2)) - cmd_x509certs = ( cat $(SIGNING_X509-y) /dev/null; \ - awk '/-----BEGIN CERTIFICATE-----/{flag=1;next}/-----END CERTIFICATE-----/{flag=0}flag' $(2) /dev/null | base64 -d ) > $@ || ( rm $@; exit 1) +# Cope with signing_key.x509 existing in $(srctree) not $(objtree) +AFLAGS_system_certificates.o := -I$(srctree) -targets += $(obj)/x509_certificate_list -$(obj)/x509_certificate_list: $(SIGNING_X509-y) include/config/system/trusted/keys.h $(wildcard include/config/module/sig.h) $(SYSTEM_TRUSTED_KEYS_SRCPREFIX)$(SYSTEM_TRUSTED_KEYS_FILENAME) - $(call if_changed,x509certs,$(SYSTEM_TRUSTED_KEYS_SRCPREFIX)$(CONFIG_SYSTEM_TRUSTED_KEYS)) +quiet_cmd_extract_certs = EXTRACT_CERTS $(patsubst "%",%,$(2)) + cmd_extract_certs = scripts/extract-cert $(2) $@ || ( rm $@; exit 1) +targets += x509_certificate_list +$(obj)/x509_certificate_list: scripts/extract-cert $(SYSTEM_TRUSTED_KEYS_SRCPREFIX)$(SYSTEM_TRUSTED_KEYS_FILENAME) FORCE + $(call if_changed,extract_certs,$(SYSTEM_TRUSTED_KEYS_SRCPREFIX)$(CONFIG_SYSTEM_TRUSTED_KEYS)) endif clean-files := x509_certificate_list .x509.list
@@ -248,9 +247,9 @@ ifeq ($(patsubst pkcs11:%,%,$(firstword $(MODULE_SIG_KEY_FILENAME))),$(firstword X509_DEP := $(MODULE_SIG_KEY_SRCPREFIX)$(MODULE_SIG_KEY_FILENAME) endif -quiet_cmd_extract_der = SIGNING_CERT $(patsubst "%",%,$(2)) - cmd_extract_der = scripts/extract-cert $(2) signing_key.x509 +# GCC PR#66871 again. +$(obj)/system_certificates.o: signing_key.x509 signing_key.x509: scripts/extract-cert include/config/module/sig/key.h $(X509_DEP) - $(call cmd,extract_der,$(MODULE_SIG_KEY_SRCPREFIX)$(CONFIG_MODULE_SIG_KEY)) + $(call cmd,extract_certs,$(MODULE_SIG_KEY_SRCPREFIX)$(CONFIG_MODULE_SIG_KEY)) endif
kernel/system_certificates.S +3 -0
@@ -7,6 +7,9 @@ .globl VMLINUX_SYMBOL(system_certificate_list) VMLINUX_SYMBOL(system_certificate_list): __cert_list_start: +#ifdef CONFIG_MODULE_SIG + .incbin "signing_key.x509" +#endif .incbin "kernel/x509_certificate_list" __cert_list_end:
scripts/Makefile +2 -1
@@ -16,7 +16,8 @@ hostprogs-$(CONFIG_VT) += conmakehash hostprogs-$(BUILD_C_RECORDMCOUNT) += recordmcount hostprogs-$(CONFIG_BUILDTIME_EXTABLE_SORT) += sortextable hostprogs-$(CONFIG_ASN1) += asn1_compiler -hostprogs-$(CONFIG_MODULE_SIG) += sign-file extract-cert +hostprogs-$(CONFIG_MODULE_SIG) += sign-file +hostprogs-$(CONFIG_SYSTEM_TRUSTED_KEYRING) += extract-cert HOSTCFLAGS_sortextable.o = -I$(srctree)/tools/include HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include
David Howells
maintainerPATCH 24/27 PKCS#7: Improve and export the X.509 ASN.1 time object decoder [ver #7]
In reply to David HowellsMake the X.509 ASN.1 time object decoder fill in a time64_t rather than a struct tm to make comparison easier (unfortunately, this makes readable display less easy) and export it so that it can be used by the PKCS#7 code too.
Further, tighten up its parsing to reject invalid dates (eg. weird characters, non-existent hour numbers) and unsupported dates (eg. timezones other than 'Z' or dates earlier than 1970).
---
crypto/asymmetric_keys/x509_cert_parser.c | 87 ++++++++++++++++++++++------- crypto/asymmetric_keys/x509_parser.h | 7 ++ crypto/asymmetric_keys/x509_public_key.c | 9 --- 3 files changed, 72 insertions(+), 31 deletions(-)
crypto/asymmetric_keys/x509_cert_parser.c +66 -21
@@ -472,60 +472,105 @@ int x509_process_extension(void *context, size_t hdrlen,return 0 ; } -/* -* Record a certificate time. +/** +* x509_decode_time - Decode an X.509 time ASN.1 object +* @_t: The time to fill in +* @hdrlen: The length of the object header +* @tag: The object tag +* @value: The object value +* @vlen: The size of the object value +* +* Decode an ASN.1 universal time or generalised time field into a struct the +* kernel can handle and check it for validity. The time is decoded thus: +* +* [RFC5280 §4.1.2.5] +* CAs conforming to this profile MUST always encode certificate validity +* dates through the year 2049 as UTCTime; certificate validity dates in +* 2050 or later MUST be encoded as GeneralizedTime. Conforming +* applications MUST be able to process validity dates that are encoded in +* either UTCTime or GeneralizedTime. */ -static int x509_note_time (struct tm *tm,size_t hdrlen, -unsigned char tag, -const unsigned char *value,size_t vlen) +int x509_decode_time (time64_t *_t,size_t hdrlen, +unsigned char tag, +const unsigned char *value,size_t vlen) { +static const unsigned char month_lengths[] = {31 ,29 ,31 ,30 ,31 ,30 , +31 ,31 ,30 ,31 ,30 ,31 };const unsigned char *p = value; +unsigned year, mon, day, hour, min, sec, mon_len; -#define dec2bin (X ) ((X) - '0') +#define dec2bin (X ) ({ unsigned char x = (X) - '0'; if (x > 9) goto invalid_time; x; })#define DD2bin (P ) ({ unsigned x = dec2bin(P[0]) * 10 + dec2bin(P[1]); P += 2; x; })if (tag ==ASN1_UNITIM ) {/* UTCTime: YYMMDDHHMMSSZ */ if (vlen !=13 ) goto unsupported_time; - tm->tm_year =DD2bin (p); -if (tm->tm_year >=50 ) - tm->tm_year +=1900 ; + year =DD2bin (p); +if (year >=50 ) + year +=1900 ;else - tm->tm_year +=2000 ; + year +=2000 ; }else if (tag ==ASN1_GENTIM ) {/* GenTime: YYYYMMDDHHMMSSZ */ if (vlen !=15 ) goto unsupported_time; - tm->tm_year =DD2bin (p) *100 +DD2bin (p); + year =DD2bin (p) *100 +DD2bin (p); +if (year >=1950 && year <=2049 ) + goto invalid_time; }else { goto unsupported_time; } - tm->tm_year -=1900 ; - tm->tm_mon =DD2bin (p) -1 ; - tm->tm_mday =DD2bin (p); - tm->tm_hour =DD2bin (p); - tm->tm_min =DD2bin (p); - tm->tm_sec =DD2bin (p); + mon =DD2bin (p); + day =DD2bin (p); + hour =DD2bin (p); + min =DD2bin (p); + sec =DD2bin (p);if (*p !='Z' ) goto unsupported_time; + mon_len = month_lengths[mon]; +if (mon ==2 ) { +if (year %4 ==0 ) { + mon_len =29 ; +if (year %100 ==0 ) { + year /=100 ; +if (year %4 !=0 ) + mon_len =28 ; + } + } + } + +if (year <1970 || + mon <1 || mon >12 || + day <1 || day > mon_len || + hour <0 || hour >23 || + min <0 || min >59 || + sec <0 || sec >59 ) + goto invalid_time; + + *_t =mktime64 (year, mon, day, hour, min, sec);return 0 ; unsupported_time: -pr_debug ("Got unsupported time [tag %02x]: '%*.*s'\n" , - tag, (int )vlen, (int )vlen, value); +pr_debug ("Got unsupported time [tag %02x]: '%*phN'\n" , + tag, (int )vlen, value); +return -EBADMSG ; +invalid_time: +pr_debug ("Got invalid time [tag %02x]: '%*phN'\n" , + tag, (int )vlen, value);return -EBADMSG ; } +EXPORT_SYMBOL_GPL (x509_decode_time);int x509_note_not_before (void *context,size_t hdrlen,unsigned char tag,const void *value,size_t vlen) {struct x509_parse_context *ctx = context; -return x509_note_time (&ctx->cert->valid_from, hdrlen, tag, value, vlen); +return x509_decode_time (&ctx->cert->valid_from, hdrlen, tag, value, vlen); } int x509_note_not_after(void *context, size_t hdrlen,
@@ -533,7 +578,7 @@ int x509_note_not_after(void *context, size_t hdrlen, const void *value, size_t vlen) {struct x509_parse_context *ctx = context; -return x509_note_time (&ctx->cert->valid_to, hdrlen, tag, value, vlen); +return x509_decode_time (&ctx->cert->valid_to, hdrlen, tag, value, vlen); }/*
crypto/asymmetric_keys/x509_parser.h +5 -2
@@ -23,8 +23,8 @@ struct x509_certificate {struct asymmetric_key_id *skid;/* Subject + subjectKeyId (optional) */ struct asymmetric_key_id *akid_id;/* CA AuthKeyId matching ->id (optional) */ struct asymmetric_key_id *akid_skid;/* CA AuthKeyId matching ->skid (optional) */ -struct tm valid_from; -struct tm valid_to; +time64_t valid_from; +time64_t valid_to;const void *tbs;/* Signed data */ unsigned tbs_size;/* Size of signed data */ unsigned raw_sig_size;/* Size of sigature */
@@ -49,6 +49,9 @@ struct x509_certificate {*/ extern void x509_free_certificate (struct x509_certificate *cert);extern struct x509_certificate *x509_cert_parse (const void *data,size_t datalen); +extern int x509_decode_time (time64_t *_t,size_t hdrlen, +unsigned char tag, +const unsigned char *value,size_t vlen);/* * x509_public_key.c
crypto/asymmetric_keys/x509_public_key.c +1 -8
@@ -302,14 +302,7 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) }pr_devel ("Cert Key Algo: %s\n" , pkey_algo_name[cert->pub->pkey_algo]); -pr_devel ("Cert Valid From: %04ld-%02d-%02d %02d:%02d:%02d\n" , - cert->valid_from.tm_year +1900 , cert->valid_from.tm_mon +1 , - cert->valid_from.tm_mday, cert->valid_from.tm_hour, - cert->valid_from.tm_min, cert->valid_from.tm_sec); -pr_devel ("Cert Valid To: %04ld-%02d-%02d %02d:%02d:%02d\n" , - cert->valid_to.tm_year +1900 , cert->valid_to.tm_mon +1 , - cert->valid_to.tm_mday, cert->valid_to.tm_hour, - cert->valid_to.tm_min, cert->valid_to.tm_sec); +pr_devel ("Cert Valid period: %lld-%lld\n" , cert->valid_from, cert->valid_to);pr_devel ("Cert Signature: %s + %s\n" , pkey_algo_name[cert->sig.pkey_algo], hash_algo_name[cert->sig.pkey_hash_algo]);
David Howells
maintainerPATCH 25/27 PKCS#7: Appropriately require or forbid authenticated attributes [ver #7]
In reply to David HowellsA PKCS#7 or CMS message can have per-signature authenticated attributes that are digested as a lump and signed by the authorising key for that signature. If such attributes exist, the content digest isn't itself signed, but rather it is included in a special authattr which then contributes to the signature.
Further, we already require the master message content type to be pkcs7_signedData - but there's also a separate content type for the data itself within the SignedData object and this must be repeated inside the authattrs for each signer [RFC2315 9.2, RFC5652 11.1].
We should really validate the authattrs if they exist or forbid them entirely as appropriate. To this end:
(1) Alter the PKCS#7 parser to reject any message that has more than one signature where at least one signature has authattrs and at least one that does not.
(2) Validate authattrs if they are present and strongly restrict them. Only the following authattrs are permitted and all others are rejected:
(a) contentType. This is checked to be an OID that matches the content type in the SignedData object.
(b) messageDigest. This must match the crypto digest of the data.
(c) signingTime. If present, we check that this is a valid, parseable UTCTime or GeneralTime and that the date it encodes fits within the validity window of the matching X.509 cert.
(d) S/MIME capabilities. We don't check the contents.
(e) Authenticode SP Opus Info. We don't check the contents.
(f) Authenticode Statement Type. We don't check the contents.
The message is rejected if (a) or (b) are missing. If the message is an Authenticode type, the message is rejected if (e) is missing; if not Authenticode, the message is rejected if (d) - (f) are present.
The S/MIME capabilities authattr (d) unfortunately has to be allowed to support kernels already signed by the pesign program. This only affects kexec. sign-file suppresses them (CMS_NOSMIMECAP).
The message is also rejected if an authattr is given more than once or if it contains more than one element in its set of values.
(3) Add a parameter to pkcs7_verify() to select one of three options:
(A) Require pkcs7_data type content and forbid authattrs entirely.
(B) Require pkcs7_data type content and require authattrs but to reject Microsoft Authenticode authattrs.
(C) Require an Authenticode type content (only indirect data is currently supported) and permit/require certain Authenticode authattrs.
(4) Have module verification choose option (A) above. This means that with module signing, the signature is over the content digest only. sign-file sets CMS_NOATTR to suppress authattrs. We could be more flexible and permit (A) or (B) for module signing.
(5) Have kexec verification of PE executables choose option (C) above. We may need to relax this if signed PE files may be encountered that don't have authattrs in their PKCS#7 message.
Kexec does not currently verify non-PE executables - but if this is added in the future it should probably use option (A).
(6) The pkcs7_test key type is given a module parameter to select between the above options for testing purposes.
Future patches add firmware signing support. Firmware signing will require a signed authattr containing the firmware name as passed to request_firmware() and so would pick option (B) above, but we would also need to permit the firmware name authattr.
---
crypto/asymmetric_keys/pkcs7.asn1 | 6 + crypto/asymmetric_keys/pkcs7_key_type.c | 9 ++ crypto/asymmetric_keys/pkcs7_parser.c | 138 ++++++++++++++++++++++++++++++- crypto/asymmetric_keys/pkcs7_parser.h | 15 +++ crypto/asymmetric_keys/pkcs7_verify.c | 53 +++++++++++- crypto/asymmetric_keys/verify_pefile.c | 2 include/crypto/pkcs7.h | 13 +++ include/linux/oid_registry.h | 4 + kernel/system_keyring.c | 2 scripts/sign-file.c | 5 + 10 files changed, 226 insertions(+), 21 deletions(-)
crypto/asymmetric_keys/pkcs7.asn1 +3 -3
@@ -8,7 +8,7 @@ ContentType ::= OBJECT IDENTIFIER ({ pkcs7_note_OID }) SignedData ::= SEQUENCE { version INTEGER ({ pkcs7_note_signeddata_version }), digestAlgorithms DigestAlgorithmIdentifiers, - contentInfo ContentInfo, + contentInfo ContentInfo ({ pkcs7_note_content }), certificates CHOICE { certSet [0] IMPLICIT ExtendedCertificatesAndCertificates, certSequence [2] IMPLICIT Certificates
@@ -21,7 +21,7 @@ SignedData ::= SEQUENCE { } ContentInfo ::= SEQUENCE { - contentType ContentType, + contentType ContentType ({ pkcs7_note_OID }), content [0] EXPLICIT Data OPTIONAL }
@@ -111,7 +111,7 @@ AuthenticatedAttribute ::= SEQUENCE { } UnauthenticatedAttribute ::= SEQUENCE { - type OBJECT IDENTIFIER ({ pkcs7_note_OID }), + type OBJECT IDENTIFIER, values SET OF ANY }
crypto/asymmetric_keys/pkcs7_key_type.c +8 -1
@@ -19,6 +19,11 @@#include <keys/system_keyring.h> #include "pkcs7_parser.h" +static unsigned pkcs7_want_authattrs; +module_param_named (authattrs, pkcs7_want_authattrs, uint,S_IWUSR |S_IRUGO ); +MODULE_PARM_DESC (pkcs7_want_authattrs, +"Whether or not a PKCS#7 message should contain authattrs (0-2)" ); +/* * Preparse a PKCS#7 wrapped and validated data blob. */
@@ -40,7 +45,9 @@ static int pkcs7_preparse(struct key_preparsed_payload *prep) goto error; } - ret =pkcs7_verify (pkcs7); +if (pkcs7_want_authattrs >2 ) + pkcs7_want_authattrs =0 ; + ret =pkcs7_verify (pkcs7, pkcs7_want_authattrs);if (ret <0 ) goto error_free;
crypto/asymmetric_keys/pkcs7_parser.c +132 -6
197 lines not on this page · raw message
crypto/asymmetric_keys/pkcs7_parser.h +12 -3
@@ -21,9 +21,9 @@struct pkcs7_signed_info {struct pkcs7_signed_info *next;struct x509_certificate *signer;/* Signing certificate (in msg->certs) */ -unsigned index; -bool trusted; -bool unsupported_crypto;/* T if not usable due to missing crypto */ +unsigned index; +bool trusted; +bool unsupported_crypto;/* T if not usable due to missing crypto */ /* Message digest - the digest of the Content Data (or NULL) */ const void *msgdigest;
@@ -32,6 +32,14 @@ struct pkcs7_signed_info {/* Authenticated Attribute data (or NULL) */ unsigned authattrs_len;const void *authattrs; +unsigned long aa_set; +#define sinfo_has_content_type 0 +#define sinfo_has_signing_time 1 +#define sinfo_has_message_digest 2 +#define sinfo_has_smime_caps 3 +#define sinfo_has_ms_opus_info 4 +#define sinfo_has_ms_statement_type 5 +time64_t signing_time;/* Issuing cert serial number and issuer's name [PKCS#7 or CMS ver 1] * or issuing cert's SKID [CMS ver 3].
@@ -53,6 +61,7 @@ struct pkcs7_message {struct x509_certificate *crl;/* Revocation list */ struct pkcs7_signed_info *signed_infos;u8 version;/* Version of cert (1 -> PKCS#7 or CMS; 3 -> CMS) */ +bool have_authattrs;/* T if have authattrs */ /* Content Data (or NULL) */ enum OID data_type;/* Type of Data */
crypto/asymmetric_keys/pkcs7_verify.c +51 -2
85 lines not on this page · raw message
crypto/asymmetric_keys/verify_pefile.c +1 -1
@@ -462,7 +462,7 @@ int verify_pefile_signature(const void *pebuf, unsigned pelen,if (ret <0 ) goto error; - ret =pkcs7_verify (pkcs7); + ret =pkcs7_verify (pkcs7,PKCS7_AUTHENTICODE_AUTHATTRS );if (ret <0 ) goto error;
include/crypto/pkcs7.h +12 -1
@@ -9,6 +9,9 @@* 2 of the Licence, or (at your option) any later version. */ +#ifndef _CRYPTO_PKCS7_H +#define _CRYPTO_PKCS7_H +struct key ;struct pkcs7_message ;
@@ -33,7 +36,15 @@ extern int pkcs7_validate_trust(struct pkcs7_message *pkcs7,/* * pkcs7_verify.c */ -extern int pkcs7_verify (struct pkcs7_message *pkcs7); +enum pkcs7_attr_style { +PKCS7_REJECT_AUTHATTRS , +PKCS7_REQUIRE_AUTHATTRS , +PKCS7_AUTHENTICODE_AUTHATTRS , +}; +extern int pkcs7_verify (struct pkcs7_message *pkcs7, +enum pkcs7_attr_style attr_style);extern int pkcs7_supply_detached_data (struct pkcs7_message *pkcs7,const void *data,size_t datalen); + +#endif /* _CRYPTO_PKCS7_H */
include/linux/oid_registry.h +3 -1
@@ -41,7 +41,7 @@ enum OID { OID_signed_data,/* 1.2.840.113549.1.7.2 */ /* PKCS#9 {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-9(9)} */ OID_email_address,/* 1.2.840.113549.1.9.1 */ - OID_content_type,/* 1.2.840.113549.1.9.3 */ + OID_contentType,/* 1.2.840.113549.1.9.3 */ OID_messageDigest,/* 1.2.840.113549.1.9.4 */ OID_signingTime,/* 1.2.840.113549.1.9.5 */ OID_smimeCapabilites, /* 1.2.840.113549.1.9.15 */
@@ -54,6 +54,8 @@ enum OID {/* Microsoft Authenticode & Software Publishing */ OID_msIndirectData,/* 1.3.6.1.4.1.311.2.1.4 */ + OID_msStatementType,/* 1.3.6.1.4.1.311.2.1.11 */ + OID_msSpOpusInfo,/* 1.3.6.1.4.1.311.2.1.12 */ OID_msPeImageDataObjId,/* 1.3.6.1.4.1.311.2.1.15 */ OID_msIndividualSPKeyPurpose,/* 1.3.6.1.4.1.311.2.1.21 */ OID_msOutlookExpress, /* 1.3.6.1.4.1.311.16.4 */
kernel/system_keyring.c +1 -1
@@ -132,7 +132,7 @@ int system_verify_data(const void *data, unsigned long len, goto error; } - ret =pkcs7_verify (pkcs7); + ret =pkcs7_verify (pkcs7,PKCS7_REJECT_AUTHATTRS );if (ret <0 ) goto error;
scripts/sign-file.c +3 -2
@@ -111,7 +111,7 @@ int main(int argc, char **argv)bool sign_only = false;unsigned char buf[4096 ];unsigned long module_size, cms_size; -unsigned int use_keyid =0 ; +unsigned int use_keyid =0 , use_signed_attrs =CMS_NOATTR ;const EVP_MD *digest_algo;EVP_PKEY *private_key;CMS_ContentInfo *cms;
@@ -216,7 +216,8 @@ int main(int argc, char **argv)ERR (!cms,"CMS_sign" );ERR (!CMS_add1_signer (cms, x509, private_key, digest_algo, -CMS_NOCERTS |CMS_BINARY |CMS_NOSMIMECAP | use_keyid), +CMS_NOCERTS |CMS_BINARY |CMS_NOSMIMECAP | + use_keyid | use_signed_attrs),"CMS_sign_add_signer" );ERR (CMS_final (cms, bm,NULL ,CMS_NOCERTS |CMS_BINARY ) <0 ,"CMS_final" );
David Howells
maintainerRe: [PATCH 25/27] PKCS#7: Appropriately require or forbid authenticated attributes [ver #7]
I've squashed together patches 25 and 27, merged the descriptions and put the result after patch 26. Here's the resulting squashed patch.
David
---
PKCS#7: Appropriately restrict authenticated attributes and content type
A PKCS#7 or CMS message can have per-signature authenticated attributes that are digested as a lump and signed by the authorising key for that signature. If such attributes exist, the content digest isn't itself signed, but rather it is included in a special authattr which then contributes to the signature.
Further, we already require the master message content type to be pkcs7_signedData - but there's also a separate content type for the data itself within the SignedData object and this must be repeated inside the authattrs for each signer [RFC2315 9.2, RFC5652 11.1].
We should really validate the authattrs if they exist or forbid them entirely as appropriate. To this end:
(1) Alter the PKCS#7 parser to reject any message that has more than one signature where at least one signature has authattrs and at least one that does not.
(2) Validate authattrs if they are present and strongly restrict them. Only the following authattrs are permitted and all others are rejected:
(a) contentType. This is checked to be an OID that matches the content type in the SignedData object.
(b) messageDigest. This must match the crypto digest of the data.
(c) signingTime. If present, we check that this is a valid, parseable UTCTime or GeneralTime and that the date it encodes fits within the validity window of the matching X.509 cert.
(d) S/MIME capabilities. We don't check the contents.
(e) Authenticode SP Opus Info. We don't check the contents.
(f) Authenticode Statement Type. We don't check the contents.
The message is rejected if (a) or (b) are missing. If the message is an Authenticode type, the message is rejected if (e) is missing; if not Authenticode, the message is rejected if (d) - (f) are present.
The S/MIME capabilities authattr (d) unfortunately has to be allowed to support kernels already signed by the pesign program. This only affects kexec. sign-file suppresses them (CMS_NOSMIMECAP).
The message is also rejected if an authattr is given more than once or if it contains more than one element in its set of values.
(3) Add a parameter to pkcs7_verify() to select one of the following restrictions and pass in the appropriate option from the callers:
(*) VERIFYING_MODULE_SIGNATURE
This requires that the SignedData content type be pkcs7-data and forbids authattrs. sign-file sets CMS_NOATTR. We could be more flexible and permit authattrs optionally, but only permit minimal content.
(*) VERIFYING_FIRMWARE_SIGNATURE
This requires that the SignedData content type be pkcs7-data and requires authattrs. In future, this will require an attribute holding the target firmware name in addition to the minimal set.
(*) VERIFYING_UNSPECIFIED_SIGNATURE
This requires that the SignedData content type be pkcs7-data but allows either no authattrs or only permits the minimal set.
(*) VERIFYING_KEXEC_PE_SIGNATURE
This only supports the Authenticode SPC_INDIRECT_DATA content type and requires at least an SpcSpOpusInfo authattr in addition to the minimal set. It also permits an SPC_STATEMENT_TYPE authattr (and an S/MIME capabilities authattr because the pesign program doesn't remove these).
(*) VERIFYING_KEY_SIGNATURE
(*) VERIFYING_KEY_SELF_SIGNATURE
These are invalid in this context but are included for later use when limiting the use of X.509 certs.
(4) The pkcs7_test key type is given a module parameter to select between the above options for testing purposes. For example:
echo 1 >/sys/module/pkcs7_test_key/parameters/usage keyctl padd pkcs7_test foo @s </tmp/stuff.pkcs7
will attempt to check the signature on stuff.pkcs7 as if it contains a firmware blob (1 being VERIFYING_FIRMWARE_SIGNATURE).
--- arch/x86/kernel/kexec-bzimage64.c | 4 crypto/asymmetric_keys/asymmetric_type.c | 11 ++ crypto/asymmetric_keys/pkcs7.asn1 | 6 - crypto/asymmetric_keys/pkcs7_key_type.c | 14 ++- crypto/asymmetric_keys/pkcs7_parser.c | 138 +++++++++++++++++++++++++++++-- crypto/asymmetric_keys/pkcs7_parser.h | 15 ++- crypto/asymmetric_keys/pkcs7_verify.c | 65 ++++++++++++++ crypto/asymmetric_keys/verify_pefile.c | 7 + include/crypto/pkcs7.h | 10 ++ include/crypto/public_key.h | 14 +++ include/keys/system_keyring.h | 4 include/linux/oid_registry.h | 4 include/linux/verify_pefile.h | 6 + kernel/module_signing.c | 3 kernel/system_keyring.c | 6 - scripts/sign-file.c | 5 - 16 files changed, 285 insertions(+), 27 deletions(-)
arch/x86/kernel/kexec-bzimage64.c +3 -1
@@ -536,7 +536,9 @@ static int bzImage64_verify_sig(const char *kernel, unsigned long kernel_len)int ret; ret =verify_pefile_signature (kernel, kernel_len, - system_trusted_keyring, &trusted); + system_trusted_keyring, +VERIFYING_KEXEC_PE_SIGNATURE , + &trusted);if (ret <0 )return ret;if (!trusted)
crypto/asymmetric_keys/asymmetric_type.c +11 -0
@@ -12,6 +12,7 @@*/ #include <keys/asymmetric-subtype.h> #include <keys/asymmetric-parser.h> +#include <crypto/public_key.h> #include <linux/seq_file.h> #include <linux/module.h> #include <linux/slab.h>
@@ -20,6 +21,16 @@MODULE_LICENSE ("GPL" ); +const char *const key_being_used_for[NR__KEY_BEING_USED_FOR ] = { + [VERIFYING_MODULE_SIGNATURE ] ="mod sig" , + [VERIFYING_FIRMWARE_SIGNATURE ] ="firmware sig" , + [VERIFYING_KEXEC_PE_SIGNATURE ] ="kexec PE sig" , + [VERIFYING_KEY_SIGNATURE ] ="key sig" , + [VERIFYING_KEY_SELF_SIGNATURE ] ="key self sig" , + [VERIFYING_UNSPECIFIED_SIGNATURE ] ="unspec sig" , +}; +EXPORT_SYMBOL_GPL (key_being_used_for); +static LIST_HEAD (asymmetric_key_parsers);static DECLARE_RWSEM (asymmetric_key_parsers_sem);
crypto/asymmetric_keys/pkcs7.asn1 +3 -3
@@ -8,7 +8,7 @@ ContentType ::= OBJECT IDENTIFIER ({ pkcs7_note_OID }) SignedData ::= SEQUENCE { version INTEGER ({ pkcs7_note_signeddata_version }), digestAlgorithms DigestAlgorithmIdentifiers, - contentInfo ContentInfo, + contentInfo ContentInfo ({ pkcs7_note_content }), certificates CHOICE { certSet [0] IMPLICIT ExtendedCertificatesAndCertificates, certSequence [2] IMPLICIT Certificates
@@ -21,7 +21,7 @@ SignedData ::= SEQUENCE { } ContentInfo ::= SEQUENCE { - contentType ContentType, + contentType ContentType ({ pkcs7_note_OID }), content [0] EXPLICIT Data OPTIONAL }
@@ -111,7 +111,7 @@ AuthenticatedAttribute ::= SEQUENCE { } UnauthenticatedAttribute ::= SEQUENCE { - type OBJECT IDENTIFIER ({ pkcs7_note_OID }), + type OBJECT IDENTIFIER, values SET OF ANY }
crypto/asymmetric_keys/pkcs7_key_type.c +13 -1
@@ -14,16 +14,23 @@#include <linux/err.h> #include <linux/module.h> #include <linux/key-type.h> +#include <keys/asymmetric-type.h> #include <crypto/pkcs7.h> #include <keys/user-type.h> #include <keys/system_keyring.h> #include "pkcs7_parser.h" +static unsigned pkcs7_usage; +module_param_named (usage, pkcs7_usage, uint,S_IWUSR |S_IRUGO ); +MODULE_PARM_DESC (pkcs7_usage, +"Usage to specify when verifying the PKCS#7 message" ); +/* * Preparse a PKCS#7 wrapped and validated data blob. */ static int pkcs7_preparse (struct key_preparsed_payload *prep) { +enum key_being_used_for usage = pkcs7_usage;struct pkcs7_message *pkcs7;const void *data, *saved_prep_data;size_t datalen, saved_prep_datalen;
@@ -32,6 +39,11 @@ static int pkcs7_preparse(struct key_preparsed_payload *prep)kenter ("" ); +if (usage >=NR__KEY_BEING_USED_FOR ) { +pr_err ("Invalid usage type %d\n" , usage); +return -EINVAL ; + } + saved_prep_data = prep->data; saved_prep_datalen = prep->datalen; pkcs7 =pkcs7_parse_message (saved_prep_data, saved_prep_datalen);
@@ -40,7 +52,7 @@ static int pkcs7_preparse(struct key_preparsed_payload *prep) goto error; } - ret =pkcs7_verify (pkcs7); + ret =pkcs7_verify (pkcs7, usage);if (ret <0 ) goto error_free;
crypto/asymmetric_keys/pkcs7_parser.c +132 -6
197 lines not on this page · raw message
crypto/asymmetric_keys/pkcs7_parser.h +12 -3
@@ -21,9 +21,9 @@struct pkcs7_signed_info {struct pkcs7_signed_info *next;struct x509_certificate *signer;/* Signing certificate (in msg->certs) */ -unsigned index; -bool trusted; -bool unsupported_crypto;/* T if not usable due to missing crypto */ +unsigned index; +bool trusted; +bool unsupported_crypto;/* T if not usable due to missing crypto */ /* Message digest - the digest of the Content Data (or NULL) */ const void *msgdigest;
@@ -32,6 +32,14 @@ struct pkcs7_signed_info {/* Authenticated Attribute data (or NULL) */ unsigned authattrs_len;const void *authattrs; +unsigned long aa_set; +#define sinfo_has_content_type 0 +#define sinfo_has_signing_time 1 +#define sinfo_has_message_digest 2 +#define sinfo_has_smime_caps 3 +#define sinfo_has_ms_opus_info 4 +#define sinfo_has_ms_statement_type 5 +time64_t signing_time;/* Issuing cert serial number and issuer's name [PKCS#7 or CMS ver 1] * or issuing cert's SKID [CMS ver 3].
@@ -53,6 +61,7 @@ struct pkcs7_message {struct x509_certificate *crl;/* Revocation list */ struct pkcs7_signed_info *signed_infos;u8 version;/* Version of cert (1 -> PKCS#7 or CMS; 3 -> CMS) */ +bool have_authattrs;/* T if have authattrs */ /* Content Data (or NULL) */ enum OID data_type;/* Type of Data */
crypto/asymmetric_keys/pkcs7_verify.c +63 -2
103 lines not on this page · raw message
crypto/asymmetric_keys/verify_pefile.c +5 -2
@@ -393,6 +393,7 @@ error_no_desc:* @pebuf: Buffer containing the PE binary image * @pelen: Length of the binary image * @trust_keyring: Signing certificates to use as starting points +* @usage: The use to which the key is being put. * @_trusted: Set to true if trustworth, false otherwise * * Validate that the certificate chain inside the PKCS#7 message inside the PE
@@ -417,7 +418,9 @@ error_no_desc:* May also return -ENOMEM. */ int verify_pefile_signature (const void *pebuf,unsigned pelen, -struct key *trusted_keyring,bool *_trusted) +struct key *trusted_keyring, +enum key_being_used_for usage, +bool *_trusted) {struct pkcs7_message *pkcs7;struct pefile_context ctx;
@@ -462,7 +465,7 @@ int verify_pefile_signature(const void *pebuf, unsigned pelen,if (ret <0 ) goto error; - ret =pkcs7_verify (pkcs7); + ret =pkcs7_verify (pkcs7, usage);if (ret <0 ) goto error;
include/crypto/pkcs7.h +9 -1
@@ -9,6 +9,11 @@* 2 of the Licence, or (at your option) any later version. */ +#ifndef _CRYPTO_PKCS7_H +#define _CRYPTO_PKCS7_H + +#include <crypto/public_key.h> +struct key ;struct pkcs7_message ;
@@ -33,7 +38,10 @@ extern int pkcs7_validate_trust(struct pkcs7_message *pkcs7,/* * pkcs7_verify.c */ -extern int pkcs7_verify (struct pkcs7_message *pkcs7); +extern int pkcs7_verify (struct pkcs7_message *pkcs7, +enum key_being_used_for usage);extern int pkcs7_supply_detached_data (struct pkcs7_message *pkcs7,const void *data,size_t datalen); + +#endif /* _CRYPTO_PKCS7_H */
include/crypto/public_key.h +14 -0
@@ -40,6 +40,20 @@ enum pkey_id_type {extern const char *const pkey_id_type_name[PKEY_ID_TYPE__LAST ];/* +* The use to which an asymmetric key is being put. +*/ +enum key_being_used_for { +VERIFYING_MODULE_SIGNATURE , +VERIFYING_FIRMWARE_SIGNATURE , +VERIFYING_KEXEC_PE_SIGNATURE , +VERIFYING_KEY_SIGNATURE , +VERIFYING_KEY_SELF_SIGNATURE , +VERIFYING_UNSPECIFIED_SIGNATURE , +NR__KEY_BEING_USED_FOR +}; +extern const char *const key_being_used_for[NR__KEY_BEING_USED_FOR ]; + +/* * Cryptographic data for the public-key subtype of the asymmetric key type. * * Note that this may include private part of the key as well as the public
include/keys/system_keyring.h +3 -1
@@ -15,6 +15,7 @@#ifdef CONFIG_SYSTEM_TRUSTED_KEYRING #include <linux/key.h> +#include <crypto/public_key.h> extern struct key *system_trusted_keyring;static inline struct key *get_system_trusted_keyring (void )
@@ -30,7 +31,8 @@ static inline struct key *get_system_trusted_keyring(void)#ifdef CONFIG_SYSTEM_DATA_VERIFICATION extern int system_verify_data (const void *data,unsigned long len, -const void *raw_pkcs7,size_t pkcs7_len); +const void *raw_pkcs7,size_t pkcs7_len, +enum key_being_used_for usage);#endif #endif /* _KEYS_SYSTEM_KEYRING_H */
include/linux/oid_registry.h +3 -1
@@ -41,7 +41,7 @@ enum OID { OID_signed_data,/* 1.2.840.113549.1.7.2 */ /* PKCS#9 {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-9(9)} */ OID_email_address,/* 1.2.840.113549.1.9.1 */ - OID_content_type,/* 1.2.840.113549.1.9.3 */ + OID_contentType,/* 1.2.840.113549.1.9.3 */ OID_messageDigest,/* 1.2.840.113549.1.9.4 */ OID_signingTime,/* 1.2.840.113549.1.9.5 */ OID_smimeCapabilites, /* 1.2.840.113549.1.9.15 */
@@ -54,6 +54,8 @@ enum OID {/* Microsoft Authenticode & Software Publishing */ OID_msIndirectData,/* 1.3.6.1.4.1.311.2.1.4 */ + OID_msStatementType,/* 1.3.6.1.4.1.311.2.1.11 */ + OID_msSpOpusInfo,/* 1.3.6.1.4.1.311.2.1.12 */ OID_msPeImageDataObjId,/* 1.3.6.1.4.1.311.2.1.15 */ OID_msIndividualSPKeyPurpose,/* 1.3.6.1.4.1.311.2.1.21 */ OID_msOutlookExpress, /* 1.3.6.1.4.1.311.16.4 */
include/linux/verify_pefile.h +5 -1
@@ -12,7 +12,11 @@#ifndef _LINUX_VERIFY_PEFILE_H#define _LINUX_VERIFY_PEFILE_H +#include <crypto/public_key.h> +extern int verify_pefile_signature (const void *pebuf,unsigned pelen, -struct key *trusted_keyring,bool *_trusted); +struct key *trusted_keyring, +enum key_being_used_for usage, +bool *_trusted);#endif /* _LINUX_VERIFY_PEFILE_H */
kernel/module_signing.c +2 -1
@@ -72,5 +72,6 @@ int mod_verify_sig(const void *mod, unsigned long *_modlen)return -EBADMSG ; } -return system_verify_data (mod, modlen, mod + modlen, sig_len); +return system_verify_data (mod, modlen, mod + modlen, sig_len, +VERIFYING_MODULE_SIGNATURE ); }
kernel/system_keyring.c +4 -2
@@ -113,9 +113,11 @@ late_initcall(load_system_certificate_list);* @len: Size of @data. * @raw_pkcs7: The PKCS#7 message that is the signature. * @pkcs7_len: The size of @raw_pkcs7. +* @usage: The use to which the key is being put. */ int system_verify_data (const void *data,unsigned long len, -const void *raw_pkcs7,size_t pkcs7_len) +const void *raw_pkcs7,size_t pkcs7_len, +enum key_being_used_for usage) {struct pkcs7_message *pkcs7;bool trusted;
@@ -132,7 +134,7 @@ int system_verify_data(const void *data, unsigned long len, goto error; } - ret =pkcs7_verify (pkcs7); + ret =pkcs7_verify (pkcs7, usage);if (ret <0 ) goto error;
scripts/sign-file.c +3 -2
@@ -111,7 +111,7 @@ int main(int argc, char **argv)bool sign_only = false;unsigned char buf[4096 ];unsigned long module_size, cms_size; -unsigned int use_keyid =0 ; +unsigned int use_keyid =0 , use_signed_attrs =CMS_NOATTR ;const EVP_MD *digest_algo;EVP_PKEY *private_key;CMS_ContentInfo *cms;
@@ -216,7 +216,8 @@ int main(int argc, char **argv)ERR (!cms,"CMS_sign" );ERR (!CMS_add1_signer (cms, x509, private_key, digest_algo, -CMS_NOCERTS |CMS_BINARY |CMS_NOSMIMECAP | use_keyid), +CMS_NOCERTS |CMS_BINARY |CMS_NOSMIMECAP | + use_keyid | use_signed_attrs),"CMS_sign_add_signer" );ERR (CMS_final (cms, bm,NULL ,CMS_NOCERTS |CMS_BINARY ) <0 ,"CMS_final" );
David Howells
maintainerPATCH 26/27 KEYS: Add a name for PKEY_ID_PKCS7 [ver #7]
In reply to David HowellsAdd a name for PKEY_ID_PKCS7 into the pkey_id_type_name array.
---
crypto/asymmetric_keys/public_key.c | 1 + 1 file changed, 1 insertion(+)
crypto/asymmetric_keys/public_key.c +1 -0
@@ -39,6 +39,7 @@ EXPORT_SYMBOL_GPL(pkey_algo);const char *const pkey_id_type_name[PKEY_ID_TYPE__LAST ] = { [PKEY_ID_PGP ] ="PGP" , [PKEY_ID_X509 ] ="X509" , + [PKEY_ID_PKCS7 ] ="PKCS#7" , };EXPORT_SYMBOL_GPL (pkey_id_type_name);
David Howells
maintainerPATCH 27/27 PKCS#7: Restrict content type and authenticated attributes by purpose [ver #7]
In reply to David HowellsRestrict the content type in the and the authenticated attributes permitted in a PKCS#7 SignedData object according the purpose to which the message is being put:
(*) VERIFYING_MODULE_SIGNATURE
(*) VERIFYING_FIRMWARE_SIGNATURE
(*) VERIFYING_UNSPECIFIED_SIGNATURE
These three all require the SignedData content type to be pkcs7-data. The first also forbids authattrs, the second requires them and the third is okay either way.
(*) VERIFYING_KEXEC_PE_SIGNATURE
This only supports the Authenticode SPC_INDIRECT_DATA content type and requires at least an SpcSpOpusInfo authattr but also permits an SPC_STATEMENT_TYPE authattr (and an S/MIME capabilities authattr because the pesign program doesn't remove these).
(*) VERIFYING_KEY_SIGNATURE
(*) VERIFYING_KEY_SELF_SIGNATURE
These are invalid in this context but are included for later use when limiting the use of X.509 certs.
The PKCS#7 test key type is given the usage to specify in a module parameter. For example:
echo 1 >/sys/module/pkcs7_test_key/parameters/usage keyctl padd pkcs7_test foo @s </tmp/stuff.pkcs7
will attempt to check the signature on stuff.pkcs7 as if it contains a firmware blob (1 being VERIFYING_FIRMWARE_SIGNATURE).
---
arch/x86/kernel/kexec-bzimage64.c | 4 +++- crypto/asymmetric_keys/asymmetric_type.c | 11 ++++++++++ crypto/asymmetric_keys/pkcs7_key_type.c | 19 +++++++++++------ crypto/asymmetric_keys/pkcs7_verify.c | 34 ++++++++++++++++++++---------- crypto/asymmetric_keys/verify_pefile.c | 7 ++++-- include/crypto/pkcs7.h | 9 +++----- include/crypto/public_key.h | 14 ++++++++++++ include/keys/system_keyring.h | 4 +++- include/linux/verify_pefile.h | 6 ++++- kernel/module_signing.c | 3 ++- kernel/system_keyring.c | 6 ++++- 11 files changed, 85 insertions(+), 32 deletions(-)
arch/x86/kernel/kexec-bzimage64.c +3 -1
@@ -536,7 +536,9 @@ static int bzImage64_verify_sig(const char *kernel, unsigned long kernel_len)int ret; ret =verify_pefile_signature (kernel, kernel_len, - system_trusted_keyring, &trusted); + system_trusted_keyring, +VERIFYING_KEXEC_PE_SIGNATURE , + &trusted);if (ret <0 )return ret;if (!trusted)
crypto/asymmetric_keys/asymmetric_type.c +11 -0
@@ -12,6 +12,7 @@*/ #include <keys/asymmetric-subtype.h> #include <keys/asymmetric-parser.h> +#include <crypto/public_key.h> #include <linux/seq_file.h> #include <linux/module.h> #include <linux/slab.h>
@@ -20,6 +21,16 @@MODULE_LICENSE ("GPL" ); +const char *const key_being_used_for[NR__KEY_BEING_USED_FOR ] = { + [VERIFYING_MODULE_SIGNATURE ] ="mod sig" , + [VERIFYING_FIRMWARE_SIGNATURE ] ="firmware sig" , + [VERIFYING_KEXEC_PE_SIGNATURE ] ="kexec PE sig" , + [VERIFYING_KEY_SIGNATURE ] ="key sig" , + [VERIFYING_KEY_SELF_SIGNATURE ] ="key self sig" , + [VERIFYING_UNSPECIFIED_SIGNATURE ] ="unspec sig" , +}; +EXPORT_SYMBOL_GPL (key_being_used_for); +static LIST_HEAD (asymmetric_key_parsers);static DECLARE_RWSEM (asymmetric_key_parsers_sem);
crypto/asymmetric_keys/pkcs7_key_type.c +12 -7
@@ -14,21 +14,23 @@#include <linux/err.h> #include <linux/module.h> #include <linux/key-type.h> +#include <keys/asymmetric-type.h> #include <crypto/pkcs7.h> #include <keys/user-type.h> #include <keys/system_keyring.h> #include "pkcs7_parser.h" -static unsigned pkcs7_want_authattrs; -module_param_named (authattrs, pkcs7_want_authattrs, uint,S_IWUSR |S_IRUGO ); -MODULE_PARM_DESC (pkcs7_want_authattrs, -"Whether or not a PKCS#7 message should contain authattrs (0-2)" ); +static unsigned pkcs7_usage; +module_param_named (usage, pkcs7_usage, uint,S_IWUSR |S_IRUGO ); +MODULE_PARM_DESC (pkcs7_usage, +"Usage to specify when verifying the PKCS#7 message" );/* * Preparse a PKCS#7 wrapped and validated data blob. */ static int pkcs7_preparse (struct key_preparsed_payload *prep) { +enum key_being_used_for usage = pkcs7_usage;struct pkcs7_message *pkcs7;const void *data, *saved_prep_data;size_t datalen, saved_prep_datalen;
@@ -37,6 +39,11 @@ static int pkcs7_preparse(struct key_preparsed_payload *prep)kenter ("" ); +if (usage >=NR__KEY_BEING_USED_FOR ) { +pr_err ("Invalid usage type %d\n" , usage); +return -EINVAL ; + } + saved_prep_data = prep->data; saved_prep_datalen = prep->datalen; pkcs7 =pkcs7_parse_message (saved_prep_data, saved_prep_datalen);
@@ -45,9 +52,7 @@ static int pkcs7_preparse(struct key_preparsed_payload *prep) goto error; } -if (pkcs7_want_authattrs >2 ) - pkcs7_want_authattrs =0 ; - ret =pkcs7_verify (pkcs7, pkcs7_want_authattrs); + ret =pkcs7_verify (pkcs7, usage);if (ret <0 ) goto error_free;
crypto/asymmetric_keys/pkcs7_verify.c +23 -11
@@ -346,7 +346,7 @@ static int pkcs7_verify_one(struct pkcs7_message *pkcs7,/** * pkcs7_verify - Verify a PKCS#7 message * @pkcs7: The PKCS#7 message to be verified -* @attr_style: Whether we want authenticatedAttributes or not. +* @usage: The use to which the key is being put * * Verify a PKCS#7 message is internally consistent - that is, the data digest * matches the digest in the AuthAttrs and any signature in the message or one
@@ -358,6 +358,9 @@ static int pkcs7_verify_one(struct pkcs7_message *pkcs7,* * Returns, in order of descending priority: * +* (*) -EKEYREJECTED if a key was selected that had a usage restriction at +* odds with the specified usage, or: +* * (*) -EKEYREJECTED if a signature failed to match for which we found an * appropriate X.509 certificate, or: *
@@ -369,7 +372,8 @@ static int pkcs7_verify_one(struct pkcs7_message *pkcs7,* (*) 0 if all the signature chains that don't incur -ENOPKG can be verified * (note that a signature chain may be of zero length), or: */ -int pkcs7_verify (struct pkcs7_message *pkcs7,enum pkcs7_attr_style attr_style) +int pkcs7_verify (struct pkcs7_message *pkcs7, +enum key_being_used_for usage) {struct pkcs7_signed_info *sinfo;struct x509_certificate *x509;
@@ -378,34 +382,42 @@ int pkcs7_verify(struct pkcs7_message *pkcs7, enum pkcs7_attr_style attr_style)kenter ("" ); -switch (attr_style) { -case PKCS7_REJECT_AUTHATTRS : +switch (usage) { +case VERIFYING_MODULE_SIGNATURE :if (pkcs7->data_type != OID_data) { -pr_warn ("Signed message is not ordinary data\n" ); +pr_warn ("Invalid module sig (not pkcs7-data)\n" );return -EKEYREJECTED ; }if (pkcs7->have_authattrs) { -pr_warn ("Message contains unwanted authAttrs\n" ); +pr_warn ("Invalid module sig (has authattrs)\n" );return -EKEYREJECTED ; }break ; -case PKCS7_REQUIRE_AUTHATTRS : +case VERIFYING_FIRMWARE_SIGNATURE :if (pkcs7->data_type != OID_data) { -pr_warn ("Signed message is not ordinary data\n" ); +pr_warn ("Invalid firmware sig (not pkcs7-data)\n" );return -EKEYREJECTED ; }if (!pkcs7->have_authattrs) { -pr_warn ("Message does not contain authAttrs\n" ); +pr_warn ("Invalid firmware sig (missing authattrs)\n" );return -EKEYREJECTED ; }break ; -case PKCS7_AUTHENTICODE_AUTHATTRS : +case VERIFYING_KEXEC_PE_SIGNATURE :if (pkcs7->data_type != OID_msIndirectData) { -pr_warn ("Signed message is not Authenticode\n" ); +pr_warn ("Invalid kexec sig (not Authenticode)\n" );return -EKEYREJECTED ; }/* Authattr presence checked in parser */ break ; +case VERIFYING_UNSPECIFIED_SIGNATURE : +if (pkcs7->data_type != OID_data) { +pr_warn ("Invalid unspecified sig (not pkcs7-data)\n" ); +return -EKEYREJECTED ; + } +break ; +default : +return -EINVAL ; }for (n =0 , x509 = pkcs7->certs; x509; x509 = x509->next, n++) {
crypto/asymmetric_keys/verify_pefile.c +5 -2
@@ -393,6 +393,7 @@ error_no_desc:* @pebuf: Buffer containing the PE binary image * @pelen: Length of the binary image * @trust_keyring: Signing certificates to use as starting points +* @usage: The use to which the key is being put. * @_trusted: Set to true if trustworth, false otherwise * * Validate that the certificate chain inside the PKCS#7 message inside the PE
@@ -417,7 +418,9 @@ error_no_desc:* May also return -ENOMEM. */ int verify_pefile_signature (const void *pebuf,unsigned pelen, -struct key *trusted_keyring,bool *_trusted) +struct key *trusted_keyring, +enum key_being_used_for usage, +bool *_trusted) {struct pkcs7_message *pkcs7;struct pefile_context ctx;
@@ -462,7 +465,7 @@ int verify_pefile_signature(const void *pebuf, unsigned pelen,if (ret <0 ) goto error; - ret =pkcs7_verify (pkcs7,PKCS7_AUTHENTICODE_AUTHATTRS ); + ret =pkcs7_verify (pkcs7, usage);if (ret <0 ) goto error;
include/crypto/pkcs7.h +3 -6
@@ -12,6 +12,8 @@#ifndef _CRYPTO_PKCS7_H#define _CRYPTO_PKCS7_H +#include <crypto/public_key.h> +struct key ;struct pkcs7_message ;
@@ -36,13 +38,8 @@ extern int pkcs7_validate_trust(struct pkcs7_message *pkcs7,/* * pkcs7_verify.c */ -enum pkcs7_attr_style { -PKCS7_REJECT_AUTHATTRS , -PKCS7_REQUIRE_AUTHATTRS , -PKCS7_AUTHENTICODE_AUTHATTRS , -};extern int pkcs7_verify (struct pkcs7_message *pkcs7, -enum pkcs7_attr_style attr_style); +enum key_being_used_for usage);extern int pkcs7_supply_detached_data (struct pkcs7_message *pkcs7,const void *data,size_t datalen);
include/crypto/public_key.h +14 -0
@@ -40,6 +40,20 @@ enum pkey_id_type {extern const char *const pkey_id_type_name[PKEY_ID_TYPE__LAST ];/* +* The use to which an asymmetric key is being put. +*/ +enum key_being_used_for { +VERIFYING_MODULE_SIGNATURE , +VERIFYING_FIRMWARE_SIGNATURE , +VERIFYING_KEXEC_PE_SIGNATURE , +VERIFYING_KEY_SIGNATURE , +VERIFYING_KEY_SELF_SIGNATURE , +VERIFYING_UNSPECIFIED_SIGNATURE , +NR__KEY_BEING_USED_FOR +}; +extern const char *const key_being_used_for[NR__KEY_BEING_USED_FOR ]; + +/* * Cryptographic data for the public-key subtype of the asymmetric key type. * * Note that this may include private part of the key as well as the public
include/keys/system_keyring.h +3 -1
@@ -15,6 +15,7 @@#ifdef CONFIG_SYSTEM_TRUSTED_KEYRING #include <linux/key.h> +#include <crypto/public_key.h> extern struct key *system_trusted_keyring;static inline struct key *get_system_trusted_keyring (void )
@@ -30,7 +31,8 @@ static inline struct key *get_system_trusted_keyring(void)#ifdef CONFIG_SYSTEM_DATA_VERIFICATION extern int system_verify_data (const void *data,unsigned long len, -const void *raw_pkcs7,size_t pkcs7_len); +const void *raw_pkcs7,size_t pkcs7_len, +enum key_being_used_for usage);#endif #endif /* _KEYS_SYSTEM_KEYRING_H */
include/linux/verify_pefile.h +5 -1
@@ -12,7 +12,11 @@#ifndef _LINUX_VERIFY_PEFILE_H#define _LINUX_VERIFY_PEFILE_H +#include <crypto/public_key.h> +extern int verify_pefile_signature (const void *pebuf,unsigned pelen, -struct key *trusted_keyring,bool *_trusted); +struct key *trusted_keyring, +enum key_being_used_for usage, +bool *_trusted);#endif /* _LINUX_VERIFY_PEFILE_H */
kernel/module_signing.c +2 -1
@@ -72,5 +72,6 @@ int mod_verify_sig(const void *mod, unsigned long *_modlen)return -EBADMSG ; } -return system_verify_data (mod, modlen, mod + modlen, sig_len); +return system_verify_data (mod, modlen, mod + modlen, sig_len, +VERIFYING_MODULE_SIGNATURE ); }
kernel/system_keyring.c +4 -2
@@ -113,9 +113,11 @@ late_initcall(load_system_certificate_list);* @len: Size of @data. * @raw_pkcs7: The PKCS#7 message that is the signature. * @pkcs7_len: The size of @raw_pkcs7. +* @usage: The use to which the key is being put. */ int system_verify_data (const void *data,unsigned long len, -const void *raw_pkcs7,size_t pkcs7_len) +const void *raw_pkcs7,size_t pkcs7_len, +enum key_being_used_for usage) {struct pkcs7_message *pkcs7;bool trusted;
@@ -132,7 +134,7 @@ int system_verify_data(const void *data, unsigned long len, goto error; } - ret =pkcs7_verify (pkcs7,PKCS7_REJECT_AUTHATTRS ); + ret =pkcs7_verify (pkcs7, usage);if (ret <0 ) goto error;