support other engines for module signing
PATCH v2 1/1 support other engines for module signing
Allow sign-file to use any available OpenSSL engine for signing by specifying the "-e engine" option.
sign-file.c renamed from sign-file.c-orig +11 -7
@@ -70,7 +70,7 @@ static __attribute__((noreturn))void format (void ) {fprintf (stderr, -"Usage: scripts/sign-file [-dp] <hash algo> <key> <x509> <module> [<dest>]\n" ); +"Usage: scripts/sign-file [-dp] [-e engine] <hash algo> <key> <x509> <module> [<dest>]\n" );fprintf (stderr," scripts/sign-file -s <raw sig> <hash algo> <x509> <module> [<dest>]\n" );exit (2 );
@@ -132,17 +132,20 @@ static int pem_pw_cb(char *buf, int len,return pwlen; } -static EVP_PKEY *read_private_key (const char *private_key_name) +static EVP_PKEY *read_private_key (char *engine_name,const char *private_key_name) {EVP_PKEY *private_key;if (!strncmp (private_key_name,"pkcs11:" ,7 )) { + engine_name ="pkcs11" ; + } +if (engine_name) {ENGINE *e;ENGINE_load_builtin_engines ();drain_openssl_errors (); - e =ENGINE_by_id ("pkcs11" ); -ERR (!e,"Load PKCS#11 ENGINE" ); + e =ENGINE_by_id (engine_name); +ERR (!e,"Load %s ENGINE" , engine_name);if (ENGINE_init (e))drain_openssl_errors ();else
@@ -210,7 +213,7 @@ int main(int argc, char **argv) {struct module_signature sig_info = { .id_type =PKEY_ID_PKCS7 };char *hash_algo =NULL ; -char *private_key_name =NULL , *raw_sig_name =NULL ; +char *private_key_name =NULL , *raw_sig_name =NULL , *engine_name =NULL ;char *x509_name, *module_name, *dest_name;bool save_sig = false, replace_orig;bool sign_only = false;
@@ -242,7 +245,7 @@ int main(int argc, char **argv)#endif do { - opt = getopt(argc, argv, "sdpk"); + opt = getopt(argc, argv, "sdpke:"); switch (opt) { case 's': raw_sig = true; break; case 'p': save_sig = true; break;
@@ -250,6 +253,7 @@ int main(int argc, char **argv)#ifndef USE_PKCS7 case 'k' : use_keyid =CMS_USE_KEYID ;break ;#endif +case 'e' : engine_name = optarg;break ;case -1 :break ;default :format (); }
@@ -294,7 +298,7 @@ int main(int argc, char **argv)/* Read the private key and the X.509 cert the PKCS#7 message * will point to. */ - private_key =read_private_key (private_key_name); + private_key =read_private_key (engine_name, private_key_name); x509 =read_x509 (x509_name);/* Digest the module data. */
Quoted 2 lines · Mark J Cox
Allow sign-file to use any available OpenSSL engine for signing by specifying the "-e engine" option.
That's less egregiously wrong; thanks :)
But it's still not great. What engine are you actually trying to use here? Can't you make it Just Work without the user having to get involved in knowing the details of the crypto library you happen to be using today? Recognise its storage file format if it has one (like the TPM engine has distinctive PEM tags), or use STORE, or recognise its URI format if it has one (like RFC7512 for PKCS#11, or Nikos's draft for tpmkey: at least for TPMv1.2).
Quoted 72 lines
Signed-off-by Mark J Cox <[email protected]>sign-file.c renamed from sign-file.c-orig +11
-7@@ -70,7 +70,7 @@ static __attribute__((noreturn))void format (void ) {fprintf (stderr, -"Usage: scripts/sign-file [-dp] <hash algo> <key> <x509> <module> [<dest>]\n" ); +"Usage: scripts/sign-file [-dp] [-e engine] <hash algo> <key> <x509> <module> [<dest>]\n" );fprintf (stderr," scripts/sign-file -s <raw sig> <hash algo> <x509> <module> [<dest>]\n" );exit (2 );@@ -132,17 +132,20 @@ static int pem_pw_cb(char *buf, int len,return pwlen; } -static EVP_PKEY *read_private_key (const char *private_key_name) +static EVP_PKEY *read_private_key (char *engine_name,const char *private_key_name) {EVP_PKEY *private_key;if (!strncmp (private_key_name,"pkcs11:" ,7 )) { + engine_name ="pkcs11" ; + } +if (engine_name) {ENGINE *e;ENGINE_load_builtin_engines ();drain_openssl_errors (); - e =ENGINE_by_id ("pkcs11" ); -ERR (!e,"Load PKCS#11 ENGINE" ); + e =ENGINE_by_id (engine_name); +ERR (!e,"Load %s ENGINE" , engine_name);if (ENGINE_init (e))drain_openssl_errors ();else @@ -210,7 +213,7 @@ int main(int argc, char **argv) {struct module_signature sig_info = { .id_type =PKEY_ID_PKCS7 };char *hash_algo =NULL ; -char *private_key_name =NULL , *raw_sig_name =NULL ; +char *private_key_name =NULL , *raw_sig_name =NULL , *engine_name =NULL ;char *x509_name, *module_name, *dest_name;bool save_sig = false, replace_orig;bool sign_only = false;@@ -242,7 +245,7 @@ int main(int argc, char **argv)#endif do { - opt = getopt(argc, argv, "sdpk"); + opt = getopt(argc, argv, "sdpke:"); switch (opt) { case 's': raw_sig = true; break; case 'p': save_sig = true; break;@@ -250,6 +253,7 @@ int main(int argc, char **argv)#ifndef USE_PKCS7 case 'k' : use_keyid =CMS_USE_KEYID ;break ;#endif +case 'e' : engine_name = optarg;break ;case -1 :break ;default :format (); }@@ -294,7 +298,7 @@ int main(int argc, char **argv)/* Read the private key and the X.509 cert the PKCS#7 message * will point to. */ - private_key =read_private_key (private_key_name); + private_key =read_private_key (engine_name, private_key_name); x509 =read_x509 (x509_name);/* Digest the module data. */
Quoted 7 lines
But it's still not great. What engine are you actually trying to use here? Can't you make it Just Work without the user having to get involved in knowing the details of the crypto library you happen to be using today? Recognise its storage file format if it has one (like the TPM engine has distinctive PEM tags), or use STORE, or recognise its URI format if it has one (like RFC7512 for PKCS#11, or Nikos's draft for tpmkey: at least for TPMv1.2).
There are many OpenSSL engines available besides PKCS-11, for example a number of hardware signing modules provide OpenSSL engine modules. This patch allows a user to use them to sign kernel modules using sign-file.c.
For example, if you are using a Thales HSM they provide an OpenSSL engine called "Chil" and therefore to sign kernel modules using such a module you simply now have to do:
sign-file -e chil -d sha256 nameofthekey certfile.pem kernelmodule.ko
The user will know that they want to sign using their Thales HSM protected key, and therefore this interface is obvious (in the same way they'd use openssl with the -engine option to do any other random functions).
Mark
Quoted 16 lines · Mark J Cox
Quoted 7 lines
But it's still not great. What engine are you actually trying to use here? Can't you make it Just Work without the user having to get involved in knowing the details of the crypto library you happen to be using today? Recognise its storage file format if it has one (like the TPM engine has distinctive PEM tags), or use STORE, or recognise its URI format if it has one (like RFC7512 for PKCS#11, or Nikos's draft for tpmkey: at least for TPMv1.2).
There are many OpenSSL engines available besides PKCS-11, for example a number of hardware signing modules provide OpenSSL engine modules. This patch allows a user to use them to sign kernel modules using
sign-file.c.For example, if you are using a Thales HSM they provide an OpenSSL engine called "Chil" and therefore to sign kernel modules using such a module you simply now have to do:
Quoted 5 lines
sign-file -e chil -d sha256 nameofthekey certfile.pem kernelmodule.ko
The user will know that they want to sign using their Thales HSM protected key, and therefore this interface is obvious (in the same way they'd use openssl with the -engine option to do any other random functions).
OK, if you really are doing something OpenSSL-specific that has no place in http://david.woodhou.se/draft-woodhouse-cert-best-practice.html then that's tolerable, I suppose.
I just *don't* want it getting used for TPM and other things that really ought to JustWork™.