sign-file: introduce check_module_sig
Subsystems (2)
PATCH 1/2 sign-file: introduce check_module_sig
This method checks if there's any existing signature. And it returns the actual module size, which excludes the signature.
--- scripts/sign-file.c | 70 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+)
scripts/sign-file.c +70 -0
@@ -62,9 +62,11 @@ struct module_signature {uint32_t sig_len;/* Length of signature data */ }; +#define SIGSIZ (sizeof(struct module_signature))#define PKEY_ID_PKCS7 2static char magic_number[] ="~Module signature appended~\n" ; +#define MAGIC_SIZE (sizeof(magic_number) - 1)static __attribute__((noreturn))void format (void )
@@ -132,6 +134,74 @@ static int pem_pw_cb(char *buf, int len, int w, void *v)return pwlen; } +/* +* Check if there is at least one valid PKCS#7 signature +*/ +static bool check_module_sig (char *module_name,unsigned long *module_size) +{ +int sig_offset, magic_offset; +unsigned long file_size, offset; +uint32_t sig_len; +char buf[4096 ]; +uint16_t type; +BIO *bm; + + bm =BIO_new_file (module_name,"rb" ); +ERR (!bm,"%s" , module_name); + +while (BIO_read (bm, buf,4096 ) >0 ) + ; + file_size = *module_size = offset =BIO_number_read (bm); + +for (;;) { + magic_offset = offset -MAGIC_SIZE ; +if (magic_offset <0 ) +break ; + +if (BIO_seek (bm, magic_offset)) +break ; + +if (BIO_read (bm, buf,MAGIC_SIZE ) !=MAGIC_SIZE ) +break ; + +if (memcmp (buf, magic_number,MAGIC_SIZE )) +break ; + +if (magic_offset <=SIGSIZ ) +break ; + + sig_offset = magic_offset -4 ; +if (BIO_seek (bm, sig_offset)) +break ; + +if (BIO_read (bm, &sig_len,4 ) !=4 ) +break ; + + sig_len =ntohl (sig_len); + + offset -=MAGIC_SIZE +SIGSIZ + sig_len; +if (offset <=0 ) +break ; + +if (BIO_seek (bm, offset)) +break ; + +if (BIO_read (bm, &type,2 ) !=2 ) +break ; + +/* The structured type must be a sequence. And the length of the +* content info must be at the next 2 bytes (0x82 - 0x80 = 0x2) +*/ +if (type !=0x8230 ) +break ; + + *module_size = offset; + } + +ERR (BIO_free (bm) <0 ,"%s" , module_name); +return *module_size < file_size; +} +static EVP_PKEY *read_private_key (const char *private_key_name) {EVP_PKEY *private_key;
Signature · 1 line
2.21.0
Quoted 7 lines · Yihao Wu
This method checks if there's any existing signature. And it returns the actual module size, which excludes the signature.
Signed-off-by Yihao Wu <[email protected]>--- scripts/sign-file.c | 70 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+)
I'm assuming your patch is based on the latest upstream.
Quoted 30 lines
scripts/sign-file.c +11
-0@@ -62,9 +62,11 @@ struct module_signature {uint32_t sig_len;/* Length of signature data */ }; +#define SIGSIZ (sizeof(struct module_signature))#define PKEY_ID_PKCS7 2static char magic_number[] ="~Module signature appended~\n" ; +#define MAGIC_SIZE (sizeof(magic_number) - 1)static __attribute__((noreturn))void format (void )@@ -132,6 +134,74 @@ static int pem_pw_cb(char *buf, int len, int w, void *v)return pwlen; } +/* +* Check if there is at least one valid PKCS#7 signature +*/ +static bool check_module_sig (char *module_name,unsigned long *module_size) +{ +int sig_offset, magic_offset; +unsigned long file_size, offset; +uint32_t sig_len; +char buf[4096 ];
scripts/sign-file.c static int pem_pw_cb(char *buf, int len, int w, void *v) · line 155
Quoted 10 lines
scripts/sign-file.c +9
-0+uint16_t type; +BIO *bm; + + bm =BIO_new_file (module_name,"rb" ); +ERR (!bm,"%s" , module_name); + +while (BIO_read (bm, buf,4096 ) >0 ) + ; + file_size = *module_size = offset =BIO_number_read (bm);
Use the combination of BIO_seek() and BIO_tell() to get file size instead.
Quoted 13 lines
scripts/sign-file.c +13
-0+ +for (;;) { + magic_offset = offset -MAGIC_SIZE ; +if (magic_offset <0 ) +break ; + +if (BIO_seek (bm, magic_offset)) +break ; + +if (BIO_read (bm, buf,MAGIC_SIZE ) !=MAGIC_SIZE ) +break ; + +if (memcmp (buf, magic_number,MAGIC_SIZE ))
scripts/sign-file.c static int pem_pw_cb(char *buf, int len, int w, void *v) · line 177
Quoted 10 lines
scripts/sign-file.c +10
-0+break ; + +if (magic_offset <=SIGSIZ ) +break ; + + sig_offset = magic_offset -4 ; +if (BIO_seek (bm, sig_offset)) +break ; + +if (BIO_read (bm, &sig_len,4 ) !=4 )
Change all hardcoding "4" to sizeof(sig_len).
scripts/sign-file.c static int pem_pw_cb(char *buf, int len, int w, void *v) · line 189
Quoted 12 lines
scripts/sign-file.c +12
-0+break ; + + sig_len =ntohl (sig_len); + + offset -=MAGIC_SIZE +SIGSIZ + sig_len; +if (offset <=0 ) +break ; + +if (BIO_seek (bm, offset)) +break ; + +if (BIO_read (bm, &type,2 ) !=2 )
Change all hardcoding "2" to sizeof(type).
Jia
Quoted 19 lines
scripts/sign-file.c +15
-0+break ; + +/* The structured type must be a sequence. And the length of the +* content info must be at the next 2 bytes (0x82 - 0x80 = 0x2) +*/ +if (type !=0x8230 ) +break ; + + *module_size = offset; + } + +ERR (BIO_free (bm) <0 ,"%s" , module_name); +return *module_size < file_size; +} +static EVP_PKEY *read_private_key (const char *private_key_name) {
EVP_PKEY*private_key;
Quoted 6 lines · Jia Zhang
Quoted 3 lines · Yihao Wu
This method checks if there's any existing signature. And it returns the actual module size, which excludes the signature.
Quoted 7 lines
Quoted 4 lines
Signed-off-by Yihao Wu <[email protected]>--- scripts/sign-file.c | 70 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+)I'm assuming your patch is based on the latest upstream.
Yes, it is.
Thanks,
Yihao
Quoted 38 lines
Quoted 37 lines
scripts/sign-file.c +18
-0@@ -62,9 +62,11 @@ struct module_signature {uint32_t sig_len;/* Length of signature data */ }; +#define SIGSIZ (sizeof(struct module_signature))#define PKEY_ID_PKCS7 2static char magic_number[] ="~Module signature appended~\n" ; +#define MAGIC_SIZE (sizeof(magic_number) - 1)static __attribute__((noreturn))void format (void )@@ -132,6 +134,74 @@ static int pem_pw_cb(char *buf, int len, int w, void *v)return pwlen; } +/* +* Check if there is at least one valid PKCS#7 signature +*/ +static bool check_module_sig (char *module_name,unsigned long *module_size) +{ +int sig_offset, magic_offset; +unsigned long file_size, offset; +uint32_t sig_len; +char buf[4096 ]; +uint16_t type; +BIO *bm; + + bm =BIO_new_file (module_name,"rb" ); +ERR (!bm,"%s" , module_name); + +while (BIO_read (bm, buf,4096 ) >0 )
Quoted 5 lines
scripts/sign-file.c static int pem_pw_cb(char *buf, int len, int w, void *v) · line 154
Quoted 2 lines
scripts/sign-file.c +2
-0+ ; + file_size = *module_size = offset =BIO_number_read (bm);Use the combination of
BIO_seek()andBIO_tell()to get file size instead.
There might be no elegant way to do this I suppose, since openssl BIOs doesn't support SEEK_END.
Maybe the best we can do is to use POSIX function lseek. Or could you help us decide on this, David?
Thanks,
Yihao
Quoted 18 lines
Quoted 18 lines
scripts/sign-file.c +18
-0+ +for (;;) { + magic_offset = offset -MAGIC_SIZE ; +if (magic_offset <0 ) +break ; + +if (BIO_seek (bm, magic_offset)) +break ; + +if (BIO_read (bm, buf,MAGIC_SIZE ) !=MAGIC_SIZE ) +break ; + +if (memcmp (buf, magic_number,MAGIC_SIZE )) +break ; + +if (magic_offset <=SIGSIZ ) +break ; +
Quoted 8 lines
scripts/sign-file.c static int pem_pw_cb(char *buf, int len, int w, void *v) · line 177
Quoted 5 lines
scripts/sign-file.c +5
-0+ sig_offset = magic_offset -4 ; +if (BIO_seek (bm, sig_offset)) +break ; + +if (BIO_read (bm, &sig_len,4 ) !=4 )Change all hardcoding "4" to
sizeof(sig_len).
Okay
Thanks,
Yihao
Quoted 16 lines
scripts/sign-file.c static int pem_pw_cb(char *buf, int len, int w, void *v) · line 189
Quoted 12 lines
scripts/sign-file.c +12
-0+break ; + + sig_len =ntohl (sig_len); + + offset -=MAGIC_SIZE +SIGSIZ + sig_len; +if (offset <=0 ) +break ; + +if (BIO_seek (bm, offset)) +break ; + +if (BIO_read (bm, &type,2 ) !=2 )Change all hardcoding "2" to sizeof(type).
Quoted 2 lines
Jia
Okay
Thanks,
Yihao
Quoted 19 lines
Quoted 19 lines
scripts/sign-file.c +15
-0+break ; + +/* The structured type must be a sequence. And the length of the +* content info must be at the next 2 bytes (0x82 - 0x80 = 0x2) +*/ +if (type !=0x8230 ) +break ; + + *module_size = offset; + } + +ERR (BIO_free (bm) <0 ,"%s" , module_name); +return *module_size < file_size; +} +static EVP_PKEY *read_private_key (const char *private_key_name) {EVP_PKEY *private_key;