verify: search keyid in hashed signature subpackets (repost)
PATCH 1/1 verify: search keyid in hashed signature subpackets (repost)
Reposting this, as requested by Daniel and rebasing on current tree.
Currently GRUB2 verify logic searches PGP keyid only in unhashed subpackets of PGP signature packet. As a result, signatures generated with GoLang openpgp package (https://godoc.org/golang.org/x/crypto/openpgp) could not be verified, because this package puts keyid in hashed subpackets and GRUB code never initializes the keyid variable, therefore is not able to find "verification key" with id 0x0.
--- grub-core/commands/verify.c | 115 +++++++++++++++++++++++++++++--------------- 1 file changed, 76 insertions(+), 39 deletions(-)
grub-core/commands/verify.c +76 -39
@@ -445,6 +445,38 @@ rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval,return ret; } +static grub_uint64_t +grub_subpacket_keyid_search (const grub_uint8_t * sub,grub_ssize_t sub_len) +{ +const grub_uint8_t *ptr; +grub_uint32_t l; +grub_uint64_t keyid =0 ; + +for (ptr = sub; ptr < sub + sub_len; ptr += l) + { +if (*ptr <192 ) + l = *ptr++; +else if (*ptr <255 ) + { +if (ptr +1 >= sub + sub_len) +break ; + l = (((ptr[0 ] & ~192 ) <<GRUB_CHAR_BIT ) | ptr[1 ]) +192 ; + ptr +=2 ; + } +else + { +if (ptr +5 >= sub + sub_len) +break ; + l =grub_be_to_cpu32 (grub_get_unaligned32 (ptr +1 )); + ptr +=5 ; + } +if (*ptr ==0x10 && l >=8 ) + keyid =grub_get_unaligned64 (ptr +1 ); + } + +return keyid; +} + static grub_err_t grub_verify_signature_real (char *buf, grub_size_t size, grub_file_t f, grub_file_t sig,
@@ -529,20 +561,31 @@ grub_verify_signature_real (char *buf, grub_size_t size,break ; hash->write (context, readbuf, r); } +grub_free (readbuf); + + readbuf =grub_malloc (rem); +if (!readbuf) + goto fail; hash->write (context, &v,sizeof (v)); hash->write (context, &v4,sizeof (v4)); -while (rem) + + r =0 ; +while (r < rem) { - r =grub_file_read (sig, readbuf, - rem <READBUF_SIZE ? rem :READBUF_SIZE ); -if (r <0 ) - goto fail; -if (r ==0 ) +grub_ssize_t rr =grub_file_read (sig, readbuf + r, rem - r); +if (rr <0 ) + goto fail; +if (rr ==0 )break ; - hash->write (context, readbuf, r); - rem -= r; + r += rr; } +if (r != rem) + goto fail; + hash->write (context, readbuf, rem); + keyid =grub_subpacket_keyid_search (readbuf, rem); +grub_free (readbuf); + hash->write (context, &v,sizeof (v)); s =0xff ; hash->write (context, &s,sizeof (s));
@@ -550,40 +593,34 @@ grub_verify_signature_real (char *buf, grub_size_t size, r =grub_file_read (sig, &unhashed_sub,sizeof (unhashed_sub));if (r !=sizeof (unhashed_sub)) goto fail; - { -grub_uint8_t *ptr; -grub_uint32_t l; - rem =grub_be_to_cpu16 (unhashed_sub); -if (rem >READBUF_SIZE ) - goto fail; - r =grub_file_read (sig, readbuf, rem); -if (r != rem) - goto fail; -for (ptr = readbuf; ptr < readbuf + rem; ptr += l) - { -if (*ptr <192 ) - l = *ptr++; -else if (*ptr <255 ) - { -if (ptr +1 >= readbuf + rem) -break ; - l = (((ptr[0 ] & ~192 ) <<GRUB_CHAR_BIT ) | ptr[1 ]) +192 ; - ptr +=2 ; - } -else - { -if (ptr +5 >= readbuf + rem) -break ; - l =grub_be_to_cpu32 (grub_get_unaligned32 (ptr +1 )); - ptr +=5 ; - } -if (*ptr ==0x10 && l >=8 ) - keyid =grub_get_unaligned64 (ptr +1 ); - } - } + rem =grub_be_to_cpu16 (unhashed_sub); + readbuf =grub_malloc (rem); +if (!readbuf) + goto fail; + + r =0 ; +while (r < rem) + { +grub_ssize_t rr =grub_file_read (sig, readbuf + r, rem - r); +if (rr <0 ) + goto fail; +if (rr ==0 ) +break ; + r += rr; + } +if (r != rem) + goto fail; + +if (keyid ==0 ) + keyid =grub_subpacket_keyid_search (readbuf, rem); +grub_free (readbuf); hash->final (context); + readbuf =grub_zalloc (READBUF_SIZE ); +if (!readbuf) + goto fail; +grub_dprintf ("crypt" ,"alive\n" ); hval = hash->read (context);
Signature · 1 line
2.1.4
Quoted 17 lines · Ignat Korchagin
Reposting this, as requested by Daniel and rebasing on current tree.
Currently GRUB2 verify logic searches PGP keyid only in unhashed subpackets of PGP signature packet. As a result, signatures generated with GoLang openpgp package (https://godoc.org/golang.org/x/crypto/openpgp) could not be verified, because this package puts keyid in hashed subpackets and GRUB code never initializes the keyid variable, therefore is not able to find "verification key" with id
0x0.Signed-off-by Ignat Korchagin <[email protected]>--- grub-core/commands/verify.c | 115 +++++++++++++++++++++++++++++--------------- 1 file changed, 76 insertions(+), 39 deletions(-)grub-core/commands/verify.c +0
-0@@ -445,6 +445,38 @@ rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval,return ret; }
grub-core/commands/verify.c rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval, · line 457
Quoted 10 lines
grub-core/commands/verify.c +10
-0+static grub_uint64_t +grub_subpacket_keyid_search (const grub_uint8_t * sub,grub_ssize_t sub_len) +{ +const grub_uint8_t *ptr; +grub_uint32_t l; +grub_uint64_t keyid =0 ; + +for (ptr = sub; ptr < sub + sub_len; ptr += l) + { +if (*ptr <192 )
Please define some constants and use them properly...
grub-core/commands/verify.c rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval, · line 459
Quoted 2 lines
grub-core/commands/verify.c +2
-0+ l = *ptr++; + else if (*ptr < 255)
Ditto.
grub-core/commands/verify.c rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval, · line 463
Quoted 4 lines
grub-core/commands/verify.c +4
-0+ { +if (ptr +1 >= sub + sub_len) +break ; + l = (((ptr[0 ] & ~192 ) <<GRUB_CHAR_BIT ) | ptr[1 ]) +192 ;
Ditto.
grub-core/commands/verify.c rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval, · line 464
Quoted 1 line
grub-core/commands/verify.c +1
-0+ ptr +=2 ;
Ditto and below...
Quoted 19 lines
grub-core/commands/verify.c +15
-0+ } +else + { +if (ptr +5 >= sub + sub_len) +break ; + l =grub_be_to_cpu32 (grub_get_unaligned32 (ptr +1 )); + ptr +=5 ; + } +if (*ptr ==0x10 && l >=8 ) + keyid =grub_get_unaligned64 (ptr +1 ); + } + +return keyid; +} + static grub_err_t grub_verify_signature_real (char *buf, grub_size_t size, grub_file_t f, grub_file_t sig,
grub-core/commands/verify.c grub_verify_signature_real (char *buf, grub_size_t size, · line 566
Quoted 6 lines
grub-core/commands/verify.c +3
-0@@ -529,20 +561,31 @@ grub_verify_signature_real (char *buf, grub_size_t size,break ; hash->write (context, readbuf, r); } +grub_free (readbuf); + + readbuf =grub_malloc (rem);
grub_zalloc()?
Quoted 60 lines
grub-core/commands/verify.c +16
-31+if (!readbuf) + goto fail; hash->write (context, &v,sizeof (v)); hash->write (context, &v4,sizeof (v4)); -while (rem) + + r =0 ; +while (r < rem) { - r =grub_file_read (sig, readbuf, - rem <READBUF_SIZE ? rem :READBUF_SIZE ); -if (r <0 ) - goto fail; -if (r ==0 ) +grub_ssize_t rr =grub_file_read (sig, readbuf + r, rem - r); +if (rr <0 ) + goto fail; +if (rr ==0 )break ; - hash->write (context, readbuf, r); - rem -= r; + r += rr; } +if (r != rem) + goto fail; + hash->write (context, readbuf, rem); + keyid =grub_subpacket_keyid_search (readbuf, rem); +grub_free (readbuf); + hash->write (context, &v,sizeof (v)); s =0xff ; hash->write (context, &s,sizeof (s));@@ -550,40 +593,34 @@ grub_verify_signature_real (char *buf, grub_size_t size, r =grub_file_read (sig, &unhashed_sub,sizeof (unhashed_sub));if (r !=sizeof (unhashed_sub)) goto fail; - { -grub_uint8_t *ptr; -grub_uint32_t l; - rem =grub_be_to_cpu16 (unhashed_sub); -if (rem >READBUF_SIZE ) - goto fail; - r =grub_file_read (sig, readbuf, rem); -if (r != rem) - goto fail; -for (ptr = readbuf; ptr < readbuf + rem; ptr += l) - { -if (*ptr <192 ) - l = *ptr++; -else if (*ptr <255 ) - { -if (ptr +1 >= readbuf + rem) -break ; - l = (((ptr[0 ] & ~192 ) <<GRUB_CHAR_BIT ) | ptr[1 ]) +192 ; - ptr +=2 ; - } -else - { -if (ptr +5 >= readbuf + rem)
grub-core/commands/verify.c grub_verify_signature_real (char *buf, grub_size_t size, · line 597
Quoted 10 lines
grub-core/commands/verify.c +2
-8-break ; - l =grub_be_to_cpu32 (grub_get_unaligned32 (ptr +1 )); - ptr +=5 ; - } -if (*ptr ==0x10 && l >=8 ) - keyid =grub_get_unaligned64 (ptr +1 ); - } - } + rem =grub_be_to_cpu16 (unhashed_sub); + readbuf =grub_malloc (rem);
grub_zalloc()?
Daniel
Quoted 10 lines · Daniel Kiper
Quoted 3 lines · Ignat Korchagin
Reposting this, as requested by Daniel and rebasing on current tree.
Currently GRUB2 verify logic searches PGP keyid only in unhashed
subpackets of PGP signature packet. As a result, signatures generated with GoLang openpgp package (https://godoc.org/golang.org/x/crypto/openpgp)
could not be verified, because this package puts keyid in hashed subpackets and GRUB code never initializes the keyid variable, therefore is not able to find "verification key" with id0x0.
I think it would be wise to include a brief argument citing the OpenPGP RFC that this change is compliant. Compatibility with an existing implementation is valuable, but let's make sure the appropriate code is being changed. (I haven't looked carefully myself.)
Thanks,
-Jon
Quoted 13 lines · Jon McCune
Quoted 10 lines · Daniel Kiper
Quoted 8 lines · Ignat Korchagin
Reposting this, as requested by Daniel and rebasing on current tree.
Currently GRUB2 verify logic searches PGP keyid only in unhashed subpackets of PGP signature packet. As a result, signatures generated with GoLang openpgp package (https://godoc.org/golang.org/x/crypto/openpgp) could not be verified, because this package puts keyid in hashed subpackets and GRUB code never initializes the keyid variable, therefore is not able to find "verification key" with id
0x0.
Quoted 9 lines
I think it would be wise to include a brief argument citing the OpenPGP RFC that this change is compliant. Compatibility with an existing implementation is valuable, but let's make sure the appropriate code is being changed. (I haven't looked carefully myself.)
Thanks,
-Jon
This change is compliant with RFC 4880. According to p 5.2.3 only "Signature Creation Time" subpacket "MUST be present in the hashed area". All other subpacket types may be present either in hashed or unhashed areas. Currently, GRUB assumes, that the "Issuer" subpacket is in unhashed area (by default put there by gpg tool), but other PGP implementations like (https://godoc.org/golang.org/x/crypto/openpgp)
may put it in the hashed area.
Quoted 24 lines · Ignat Korchagin
Quoted 13 lines · Jon McCune
Quoted 10 lines · Daniel Kiper
Quoted 8 lines · Ignat Korchagin
Reposting this, as requested by Daniel and rebasing on current tree.
Currently GRUB2 verify logic searches PGP keyid only in unhashed subpackets of PGP signature packet. As a result, signatures generated with GoLang openpgp package (https://godoc.org/golang.org/x/crypto/openpgp) could not be verified, because this package puts keyid in hashed subpackets and GRUB code never initializes the keyid variable, therefore is not able to find "verification key" with id
0x0.Quoted 9 lines
I think it would be wise to include a brief argument citing the OpenPGP RFC that this change is compliant. Compatibility with an existing implementation is valuable, but let's make sure the appropriate code is being changed. (I haven't looked carefully myself.)
Thanks,
-Jon
Quoted 7 lines
This change is compliant with RFC 4880. According to p 5.2.3 only "Signature Creation Time" subpacket "MUST be present in the hashed area". All other subpacket types may be present either in hashed or unhashed areas. Currently, GRUB assumes, that the "Issuer" subpacket is in unhashed area (by default put there by gpg tool), but other PGP implementations like (https://godoc.org/golang.org/x/crypto/openpgp)
may put it in the hashed area.
Please add this to commit message.
Daniel
PATCH v2 1/1 verify: search keyid in hashed signature subpackets
According to RFC 4880 5.2.3 only "Signature Creation Time" subpacket "MUST" be present in the hashed area. All other subpacket types may be present either in hashed or unhashed areas. Currently GRUB assumes, that the "Issuer" subpacket is in unhashed area (by default put there by gpg tool), but other PGP implementations (like https://godoc.org/golang.org/x/crypto/openpgp)
may put it in the hashed area.
--- grub-core/commands/verify.c | 122 ++++++++++++++++++++++++++++++-------------- 1 file changed, 83 insertions(+), 39 deletions(-)
grub-core/commands/verify.c +83 -39
@@ -33,6 +33,9 @@GRUB_MOD_LICENSE ("GPLv3+" ); +/* RFC 4880 5.2.3.1 */ +#define OPENPGP_SIGNATURE_SUBPACKET_TYPE 16 +struct grub_verified {grub_file_t file;
@@ -445,6 +448,42 @@ rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval,return ret; } +/* +* Parsing algorithm from RFC 4880 5.2.3.1 +*/ + +static grub_uint64_t +grub_subpacket_keyid_search (const grub_uint8_t * sub,grub_ssize_t sub_len) +{ +const grub_uint8_t *ptr; +grub_uint32_t l; +grub_uint64_t keyid =0 ; + +for (ptr = sub; ptr < sub + sub_len; ptr += l) + { +if (*ptr <192 ) + l = *ptr++; +else if (*ptr <255 ) + { +if (ptr +1 >= sub + sub_len) +break ; + l = (((ptr[0 ] & ~192 ) <<GRUB_CHAR_BIT ) | ptr[1 ]) +192 ; + ptr +=2 ; + } +else + { +if (ptr +5 >= sub + sub_len) +break ; + l =grub_be_to_cpu32 (grub_get_unaligned32 (ptr +1 )); + ptr +=5 ; + } +if (*ptr ==OPENPGP_SIGNATURE_SUBPACKET_TYPE && l >=8 ) + keyid =grub_get_unaligned64 (ptr +1 ); + } + +return keyid; +} + static grub_err_t grub_verify_signature_real (char *buf, grub_size_t size, grub_file_t f, grub_file_t sig,
@@ -529,20 +568,31 @@ grub_verify_signature_real (char *buf, grub_size_t size,break ; hash->write (context, readbuf, r); } +grub_free (readbuf); + + readbuf =grub_malloc (rem); +if (!readbuf) + goto fail; hash->write (context, &v,sizeof (v)); hash->write (context, &v4,sizeof (v4)); -while (rem) + + r =0 ; +while (r < rem) { - r =grub_file_read (sig, readbuf, - rem <READBUF_SIZE ? rem :READBUF_SIZE ); -if (r <0 ) - goto fail; -if (r ==0 ) +grub_ssize_t rr =grub_file_read (sig, readbuf + r, rem - r); +if (rr <0 ) + goto fail; +if (rr ==0 )break ; - hash->write (context, readbuf, r); - rem -= r; + r += rr; } +if (r != rem) + goto fail; + hash->write (context, readbuf, rem); + keyid =grub_subpacket_keyid_search (readbuf, rem); +grub_free (readbuf); + hash->write (context, &v,sizeof (v)); s =0xff ; hash->write (context, &s,sizeof (s));
@@ -550,40 +600,34 @@ grub_verify_signature_real (char *buf, grub_size_t size, r =grub_file_read (sig, &unhashed_sub,sizeof (unhashed_sub));if (r !=sizeof (unhashed_sub)) goto fail; - { -grub_uint8_t *ptr; -grub_uint32_t l; - rem =grub_be_to_cpu16 (unhashed_sub); -if (rem >READBUF_SIZE ) - goto fail; - r =grub_file_read (sig, readbuf, rem); -if (r != rem) - goto fail; -for (ptr = readbuf; ptr < readbuf + rem; ptr += l) - { -if (*ptr <192 ) - l = *ptr++; -else if (*ptr <255 ) - { -if (ptr +1 >= readbuf + rem) -break ; - l = (((ptr[0 ] & ~192 ) <<GRUB_CHAR_BIT ) | ptr[1 ]) +192 ; - ptr +=2 ; - } -else - { -if (ptr +5 >= readbuf + rem) -break ; - l =grub_be_to_cpu32 (grub_get_unaligned32 (ptr +1 )); - ptr +=5 ; - } -if (*ptr ==0x10 && l >=8 ) - keyid =grub_get_unaligned64 (ptr +1 ); - } - } + rem =grub_be_to_cpu16 (unhashed_sub); + readbuf =grub_malloc (rem); +if (!readbuf) + goto fail; + + r =0 ; +while (r < rem) + { +grub_ssize_t rr =grub_file_read (sig, readbuf + r, rem - r); +if (rr <0 ) + goto fail; +if (rr ==0 ) +break ; + r += rr; + } +if (r != rem) + goto fail; + +if (keyid ==0 ) + keyid =grub_subpacket_keyid_search (readbuf, rem); +grub_free (readbuf); hash->final (context); + readbuf =grub_zalloc (READBUF_SIZE ); +if (!readbuf) + goto fail; +grub_dprintf ("crypt" ,"alive\n" ); hval = hash->read (context);
Signature · 1 line
2.1.4
Re: [PATCH v2] verify: search keyid in hashed signature subpackets
Quoted 6 lines · Ignat Korchagin
According to RFC 4880 5.2.3 only "Signature Creation Time" subpacket "MUST" be present in the hashed area. All other subpacket types may be present either in hashed or unhashed areas. Currently GRUB assumes, that the "Issuer" subpacket is in unhashed area (by default put there by gpg tool), but other PGP implementations (like https://godoc.org/golang.org/x/crypto/openpgp)
may put it in the hashed area.
In general LGTM. One nitpick, may I add your SOB (Signed-off-by) here? Otherwise if there are no objections in 2-3 days I will apply it.
Daniel
Re: [PATCH v2] verify: search keyid in hashed signature subpackets
Quoted 1 line
One nitpick, may I add your SOB (Signed-off-by) here?
Sorry, somehow I missed it this time (probably forgot to specify the git option).
Yes, please add it.
Quoted 12 lines · Daniel Kiper
Quoted 6 lines · Ignat Korchagin
According to RFC 4880 5.2.3 only "Signature Creation Time" subpacket "MUST" be present in the hashed area. All other subpacket types may be present either in hashed or unhashed areas. Currently GRUB assumes, that the "Issuer" subpacket is in unhashed area (by default put there by gpg tool), but other PGP implementations (like https://godoc.org/golang.org/x/crypto/openpgp)
may put it in the hashed area.In general LGTM. One nitpick, may I add your SOB (Signed-off-by) here? Otherwise if there are no objections in 2-3 days I will apply it.
Daniel
Re: [PATCH v2] verify: search keyid in hashed signature subpackets
In reply to Ignat Korchagin02.12.2016 19:58, Ignat Korchagin пишет:
Quoted 48 lines
According to RFC 4880 5.2.3 only "Signature Creation Time" subpacket "MUST" be present in the hashed area. All other subpacket types may be present either in hashed or unhashed areas. Currently GRUB assumes, that the "Issuer" subpacket is in unhashed area (by default put there by gpg tool), but other PGP implementations (like https://godoc.org/golang.org/x/crypto/openpgp)
may put it in the hashed area.--- grub-core/commands/verify.c | 122 ++++++++++++++++++++++++++++++-------------- 1 file changed, 83 insertions(+), 39 deletions(-)grub-core/commands/verify.c +23
-0@@ -33,6 +33,9 @@GRUB_MOD_LICENSE ("GPLv3+" ); +/* RFC 4880 5.2.3.1 */ +#define OPENPGP_SIGNATURE_SUBPACKET_TYPE 16 +struct grub_verified {grub_file_t file;@@ -445,6 +448,42 @@ rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval,return ret; } +/* +* Parsing algorithm from RFC 4880 5.2.3.1 +*/ + +static grub_uint64_t +grub_subpacket_keyid_search (const grub_uint8_t * sub,grub_ssize_t sub_len) +{ +const grub_uint8_t *ptr; +grub_uint32_t l; +grub_uint64_t keyid =0 ; + +for (ptr = sub; ptr < sub + sub_len; ptr += l) + { +if (*ptr <192 ) + l = *ptr++; +else if (*ptr <255 ) + { +if (ptr +1 >= sub + sub_len) +break ; + l = (((ptr[0 ] & ~192 ) <<GRUB_CHAR_BIT ) | ptr[1 ]) +192 ;
grub-core/commands/verify.c rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval, · line 480
Quoted 10 lines
grub-core/commands/verify.c +10
-0+ ptr +=2 ; + } +else + { +if (ptr +5 >= sub + sub_len) +break ; + l =grub_be_to_cpu32 (grub_get_unaligned32 (ptr +1 )); + ptr +=5 ; + } +if (*ptr ==OPENPGP_SIGNATURE_SUBPACKET_TYPE && l >=8 )
Overflow check? ptr + 8 < ptr + sub_len
Quoted 32 lines
grub-core/commands/verify.c +15
-6+ keyid =grub_get_unaligned64 (ptr +1 ); + } + +return keyid; +} + static grub_err_t grub_verify_signature_real (char *buf, grub_size_t size, grub_file_t f, grub_file_t sig,@@ -529,20 +568,31 @@ grub_verify_signature_real (char *buf, grub_size_t size,break ; hash->write (context, readbuf, r); } +grub_free (readbuf); + + readbuf =grub_malloc (rem); +if (!readbuf) + goto fail; hash->write (context, &v,sizeof (v)); hash->write (context, &v4,sizeof (v4)); -while (rem) + + r =0 ; +while (r < rem) { - r =grub_file_read (sig, readbuf, - rem <READBUF_SIZE ? rem :READBUF_SIZE ); -if (r <0 ) - goto fail; -if (r ==0 ) +grub_ssize_t rr =grub_file_read (sig, readbuf + r, rem - r);
grub-core/commands/verify.c grub_verify_signature_real (char *buf, grub_size_t size, · line 591
Quoted 10 lines
grub-core/commands/verify.c +6
-2+if (rr <0 ) + goto fail; +if (rr ==0 )break ; - hash->write (context, readbuf, r); - rem -= r; + r += rr; } +if (r != rem) + goto fail;
I think this loop is overcomplicated. In all other places we assume that short read from grub_file_read means error.
Quoted 50 lines
grub-core/commands/verify.c +12
-31+ hash->write (context, readbuf, rem); + keyid =grub_subpacket_keyid_search (readbuf, rem); +grub_free (readbuf); + hash->write (context, &v,sizeof (v)); s =0xff ; hash->write (context, &s,sizeof (s));@@ -550,40 +600,34 @@ grub_verify_signature_real (char *buf, grub_size_t size, r =grub_file_read (sig, &unhashed_sub,sizeof (unhashed_sub));if (r !=sizeof (unhashed_sub)) goto fail; - { -grub_uint8_t *ptr; -grub_uint32_t l; - rem =grub_be_to_cpu16 (unhashed_sub); -if (rem >READBUF_SIZE ) - goto fail; - r =grub_file_read (sig, readbuf, rem); -if (r != rem) - goto fail; -for (ptr = readbuf; ptr < readbuf + rem; ptr += l) - { -if (*ptr <192 ) - l = *ptr++; -else if (*ptr <255 ) - { -if (ptr +1 >= readbuf + rem) -break ; - l = (((ptr[0 ] & ~192 ) <<GRUB_CHAR_BIT ) | ptr[1 ]) +192 ; - ptr +=2 ; - } -else - { -if (ptr +5 >= readbuf + rem) -break ; - l =grub_be_to_cpu32 (grub_get_unaligned32 (ptr +1 )); - ptr +=5 ; - } -if (*ptr ==0x10 && l >=8 ) - keyid =grub_get_unaligned64 (ptr +1 ); - } - } + rem =grub_be_to_cpu16 (unhashed_sub); + readbuf =grub_malloc (rem); +if (!readbuf) + goto fail; + + r =0 ; +while (r < rem) + {
grub-core/commands/verify.c grub_verify_signature_real (char *buf, grub_size_t size, · line 620
Quoted 10 lines
grub-core/commands/verify.c +10
-0+grub_ssize_t rr =grub_file_read (sig, readbuf + r, rem - r); +if (rr <0 ) + goto fail; +if (rr ==0 ) +break ; + r += rr; + } +if (r != rem) + goto fail; +
Ditto.
grub-core/commands/verify.c grub_verify_signature_real (char *buf, grub_size_t size, · line 627
Quoted 7 lines
grub-core/commands/verify.c +4
-0+if (keyid ==0 ) + keyid =grub_subpacket_keyid_search (readbuf, rem); +grub_free (readbuf); hash->final (context); + readbuf =grub_zalloc (READBUF_SIZE );
No need to use grub_zalloc here, we did not zero buffer before as well.
Quoted 7 lines
grub-core/commands/verify.c +3
-0+if (!readbuf) + goto fail; +grub_dprintf ("crypt" ,"alive\n" ); hval = hash->read (context);
Re: [PATCH v2] verify: search keyid in hashed signature subpackets
General thoughts:
Just a reminder: this patch tries to fix a BUG in code, which was present from the introduction of this functionality and acknowledged for more than 9 month now. The goal of this patch is to fix it without introducing too much change. We are spending too much time "improving" things and forgetting that basic functionality is broken.
Probably, most of us have experience working in software companies and the basic flow of any software development is:
- fix bugs
- improve
- goto step 1;
So fixing bugs and improvements should be in separate steps. I do not think it is an excuse not to fix bugs for 9 month in the hope of improving code readability along the way. That should be a separate task. Unfortunately, I do not have time to continuously rebase the PR for each 1 month delay - GRUB is not my primary and even second responsibility at work. I'm just trying to be nice and contribute back what I found broken. But I'm just a regular user and I have to cut this time from my family to do this.
Actually, IMHO, this whole file needs to be redone, so comments regarding "this or that unclear" applies to everything there. As I said multiple times, I almost did not introduce new code, but just refactored the existing one fix the issue. I'm not aware of any decisions made prior to this patch.
Many times in this mailing list there were requests to be considerate that GRUB maintainers are very busy and cannot always act/respond quickly. But, guess what: this goes the other way around - GRUB contributors can be busy as well. If you make me rebase the patch each month with new not very relevant comments I will give up essentially. If you think the code needs improvements, you are free to ask me for a follow-up with general directions/requirements for what you think is wrong. But, please, make a little effort to fix the work already done. The patch is not that big, but to prepare it I had to spend time to read and understand the whole PGP RFC (and their data formats and encodings are not that simple I would say). And after 9 month I do not even remember the specifics.
General thoughts on improving GRUB development:
- let's be tolerant to each other
- for God's sake, let's move to a modern code hosting with
convenient modern workflows (PRs, reviews etc). Yes, previously you said that you want to be able to review patches on the phone, but I do not imagine you guys having not being able to do this with a reasonable smartphone. Because fighting with git send-mail and subverting my email 2-factor auth just to send a patch makes me (and probably many others) very sad (and insecure BTW)
- there should be no comments, like (taken from real-life) "I do not
think this is a right name for this function". The right form would be "I do not think this is a right name for this function, how about <PUT YOUR PROPOSAL HERE>". We are not mind-readers and do not have full visibility into GRUB project.
- ...
Sorry for the noise, but this is something which came up working on this... Specific patch comments below.
Quoted 53 lines · Andrei Borzenkov
02.12.2016 19:58, Ignat Korchagin пишет:
Quoted 52 lines
According to RFC 4880 5.2.3 only "Signature Creation Time" subpacket "MUST" be present in the hashed area. All other subpacket types may be present either in hashed or unhashed areas. Currently GRUB assumes, that the "Issuer" subpacket is in unhashed area (by default put there by gpg tool), but other PGP implementations (like https://godoc.org/golang.org/x/crypto/openpgp)
may put it in the hashed area.--- grub-core/commands/verify.c | 122 ++++++++++++++++++++++++++++++-------------- 1 file changed, 83 insertions(+), 39 deletions(-)grub-core/commands/verify.c +27
-0@@ -33,6 +33,9 @@GRUB_MOD_LICENSE ("GPLv3+" ); +/* RFC 4880 5.2.3.1 */ +#define OPENPGP_SIGNATURE_SUBPACKET_TYPE 16 +struct grub_verified {grub_file_t file;@@ -445,6 +448,42 @@ rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval,return ret; } +/* +* Parsing algorithm from RFC 4880 5.2.3.1 +*/ + +static grub_uint64_t +grub_subpacket_keyid_search (const grub_uint8_t * sub,grub_ssize_t sub_len) +{ +const grub_uint8_t *ptr; +grub_uint32_t l; +grub_uint64_t keyid =0 ; + +for (ptr = sub; ptr < sub + sub_len; ptr += l) + { +if (*ptr <192 ) + l = *ptr++; +else if (*ptr <255 ) + { +if (ptr +1 >= sub + sub_len) +break ; + l = (((ptr[0 ] & ~192 ) <<GRUB_CHAR_BIT ) | ptr[1 ]) +192 ; + ptr +=2 ; + } +else + {
Quoted 8 lines
grub-core/commands/verify.c rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval, · line 480
Quoted 6 lines
grub-core/commands/verify.c +6
-0+if (ptr +5 >= sub + sub_len) +break ; + l =grub_be_to_cpu32 (grub_get_unaligned32 (ptr +1 )); + ptr +=5 ; + } +if (*ptr ==OPENPGP_SIGNATURE_SUBPACKET_TYPE && l >=8 )Overflow check? ptr + 8 < ptr +
sub_len
This and more: this specific packet can be in the middle of "packet collection", but its length should be >=8. We do not want to potentially read data from subsequent packets (no overflow, but getting invalid data).
Quoted 37 lines
Quoted 37 lines
grub-core/commands/verify.c +18
-7+ keyid =grub_get_unaligned64 (ptr +1 ); + } + +return keyid; +} + static grub_err_t grub_verify_signature_real (char *buf, grub_size_t size, grub_file_t f, grub_file_t sig,@@ -529,20 +568,31 @@ grub_verify_signature_real (char *buf, grub_size_t size,break ; hash->write (context, readbuf, r); } +grub_free (readbuf); + + readbuf =grub_malloc (rem); +if (!readbuf) + goto fail; hash->write (context, &v,sizeof (v)); hash->write (context, &v4,sizeof (v4)); -while (rem) + + r =0 ; +while (r < rem) { - r =grub_file_read (sig, readbuf, - rem <READBUF_SIZE ? rem :READBUF_SIZE ); -if (r <0 ) - goto fail; -if (r ==0 ) +grub_ssize_t rr =grub_file_read (sig, readbuf + r, rem - r); +if (rr <0 ) + goto fail; +if (rr ==0 )break ; - hash->write (context, readbuf, r);
Quoted 9 lines
grub-core/commands/verify.c grub_verify_signature_real (char *buf, grub_size_t size, · line 591
Quoted 5 lines
grub-core/commands/verify.c +3
-1- rem -= r; + r += rr; } +if (r != rem) + goto fail;I think this loop is overcomplicated. In all other places we assume that short read from
grub_file_readmeans error.
I remember I had justification for it, but do not remember what it is, because I wrote this 9 month ago.
Quoted 54 lines
Quoted 54 lines
grub-core/commands/verify.c +16
-31+ hash->write (context, readbuf, rem); + keyid =grub_subpacket_keyid_search (readbuf, rem); +grub_free (readbuf); + hash->write (context, &v,sizeof (v)); s =0xff ; hash->write (context, &s,sizeof (s));@@ -550,40 +600,34 @@ grub_verify_signature_real (char *buf, grub_size_t size, r =grub_file_read (sig, &unhashed_sub,sizeof (unhashed_sub));if (r !=sizeof (unhashed_sub)) goto fail; - { -grub_uint8_t *ptr; -grub_uint32_t l; - rem =grub_be_to_cpu16 (unhashed_sub); -if (rem >READBUF_SIZE ) - goto fail; - r =grub_file_read (sig, readbuf, rem); -if (r != rem) - goto fail; -for (ptr = readbuf; ptr < readbuf + rem; ptr += l) - { -if (*ptr <192 ) - l = *ptr++; -else if (*ptr <255 ) - { -if (ptr +1 >= readbuf + rem) -break ; - l = (((ptr[0 ] & ~192 ) <<GRUB_CHAR_BIT ) | ptr[1 ]) +192 ; - ptr +=2 ; - } -else - { -if (ptr +5 >= readbuf + rem) -break ; - l =grub_be_to_cpu32 (grub_get_unaligned32 (ptr +1 )); - ptr +=5 ; - } -if (*ptr ==0x10 && l >=8 ) - keyid =grub_get_unaligned64 (ptr +1 ); - } - } + rem =grub_be_to_cpu16 (unhashed_sub); + readbuf =grub_malloc (rem); +if (!readbuf) + goto fail; + + r =0 ; +while (r < rem) + { +grub_ssize_t rr =grub_file_read (sig, readbuf + r, rem - r); +if (rr <0 ) + goto fail; +if (rr ==0 )
Quoted 9 lines
grub-core/commands/verify.c grub_verify_signature_real (char *buf, grub_size_t size, · line 620
Quoted 6 lines
grub-core/commands/verify.c +6
-0+break ; + r += rr; + } +if (r != rem) + goto fail; +Ditto.
Ditto.
Quoted 10 lines
grub-core/commands/verify.c grub_verify_signature_real (char *buf, grub_size_t size, · line 627
Quoted 7 lines
grub-core/commands/verify.c +4
-0+if (keyid ==0 ) + keyid =grub_subpacket_keyid_search (readbuf, rem); +grub_free (readbuf); hash->final (context); + readbuf =grub_zalloc (READBUF_SIZE );No need to use
grub_zallochere, we did not zero buffer before as well.
We agreed previously that we should keep "what is correct" (your comment from 30 March 2016). Currently the function allocates the buffer with grub_zalloc (READBUF_SIZE). I changed buffer allocation for my code, but I have no idea about the assumptions of the code which follows. So, if previously, that code used buffer allocalted with grub_zalloc I do the same for compatibility.
Quoted 8 lines
Quoted 7 lines
grub-core/commands/verify.c +3
-0+if (!readbuf) + goto fail; +grub_dprintf ("crypt" ,"alive\n" ); hval = hash->read (context);
Re: [PATCH v2] verify: search keyid in hashed signature subpackets
Hi Ignat,
Quoted 6 lines · Ignat Korchagin
General thoughts:
Just a reminder: this patch tries to fix a BUG in code, which was present from the introduction of this functionality and acknowledged for more than 9 month now. The goal of this patch is to fix it without introducing too much change. We are spending too much time "improving" things and forgetting that basic functionality is broken.
[...]
Thank you for your work. I understand your POV but I think that Andrei's questions are valid too. I know that this sounds stupid but please be patient. We are trying to rectify all GRUB2 maintenance issues. Unfortunately it takes time especially if backlog is huge. Personally I can promise that I will do my best to get your fixes into 2.02 release. However, we need your support and a bit of understanding too.
Daniel
Re: [PATCH v2] verify: search keyid in hashed signature subpackets
Hi,
Quoted 1 line
please be patient
I'm not in a hurry. Like probably everyone else I have a fork repo where all changes are present. Just wanted to rely more on upstream in the future
Quoted 1 line
Unfortunately it takes time especially if backlog is huge
That is my point: if environment is more friendly, probably you would get more help in working through backlog
But, anyway, back to the patch: I recovered some of the context of my code, so here are the details
Quoted 2 lines
I think this loop is overcomplicated. In all other places we assume that short read from
grub_file_readmeans error.
This loop validates incorrect (or even bogus) signature format. The format should be (simplified) |total len|subpack1|subpack2|.... Each subpacket has its own length specified as well
This loop tries to verify that the overall processed packet length match. Since we we process arbitrary length here, I do not see a better approach
As for other concerns I commented in my previous reply to the patch.
Thank you.
Quoted 25 lines · Daniel Kiper
Hi Ignat,
Quoted 6 lines · Ignat Korchagin
General thoughts:
Just a reminder: this patch tries to fix a BUG in code, which was present from the introduction of this functionality and acknowledged for more than 9 month now. The goal of this patch is to fix it without introducing too much change. We are spending too much time "improving" things and forgetting that basic functionality is broken.[...]
Thank you for your work. I understand your POV but I think that Andrei's questions
are valid too. I know that this sounds stupid but please be patient. We are trying
to rectify all GRUB2 maintenance issues. Unfortunately it takes time especially if
backlog is huge. Personally I can promise that I will do my best to get your fixes
into 2.02 release. However, we need your support and a bit of understanding too.Daniel
Quoted 18 lines · Daniel Kiper
Quoted 17 lines · Ignat Korchagin
Reposting this, as requested by Daniel and rebasing on current tree.
Currently GRUB2 verify logic searches PGP keyid only in unhashed subpackets of PGP signature packet. As a result, signatures generated with GoLang openpgp package (https://godoc.org/golang.org/x/crypto/openpgp) could not be verified, because this package puts keyid in hashed subpackets and GRUB code never initializes the keyid variable, therefore is not able to find "verification key" with id
0x0.Signed-off-by Ignat Korchagin <[email protected]>--- grub-core/commands/verify.c | 115 +++++++++++++++++++++++++++++--------------- 1 file changed, 76 insertions(+), 39 deletions(-)grub-core/commands/verify.c +0
-0@@ -445,6 +445,38 @@ rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval,return ret; }
Quoted 12 lines
grub-core/commands/verify.c rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval, · line 457
Quoted 10 lines
grub-core/commands/verify.c +10
-0+static grub_uint64_t +grub_subpacket_keyid_search (const grub_uint8_t * sub,grub_ssize_t sub_len) +{ +const grub_uint8_t *ptr; +grub_uint32_t l; +grub_uint64_t keyid =0 ; + +for (ptr = sub; ptr < sub + sub_len; ptr += l) + { +if (*ptr <192 )Please define some constants and use them properly...
This is original GRUB code here. It was just moved to a separate function. See below.
Also, defining constants here is probably more confusing. I do not know for sure, but suspect this algorithm is taken directly from RFC 4880 5.2.3.1 and there are no names there
Quoted 36 lines
grub-core/commands/verify.c rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval, · line 459
Quoted 3 lines
grub-core/commands/verify.c +2
-0+ l = *ptr++; + else if (*ptr < 255)Ditto.
grub-core/commands/verify.c rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval, · line 463
Quoted 4 lines
grub-core/commands/verify.c +4
-0+ { +if (ptr +1 >= sub + sub_len) +break ; + l = (((ptr[0 ] & ~192 ) <<GRUB_CHAR_BIT ) | ptr[1 ]) +192 ;Ditto.
grub-core/commands/verify.c rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval, · line 464
Quoted 1 line
grub-core/commands/verify.c +1
-0+ ptr +=2 ;Ditto and below...
Quoted 19 lines
grub-core/commands/verify.c +15
-0+ } +else + { +if (ptr +5 >= sub + sub_len) +break ; + l =grub_be_to_cpu32 (grub_get_unaligned32 (ptr +1 )); + ptr +=5 ; + } +if (*ptr ==0x10 && l >=8 ) + keyid =grub_get_unaligned64 (ptr +1 ); + } + +return keyid; +} + static grub_err_t grub_verify_signature_real (char *buf, grub_size_t size, grub_file_t f, grub_file_t sig,@@ -529,20 +561,31 @@ grub_verify_signature_real (char *buf, grub_size_t size,
Quoted 8 lines
grub-core/commands/verify.c grub_verify_signature_real (char *buf, grub_size_t size, · line 566
Quoted 6 lines
grub-core/commands/verify.c +3
-0break ; hash->write (context, readbuf, r); } +grub_free (readbuf); + + readbuf =grub_malloc (rem);
grub_zalloc()?
This buffer is filled below by grub_file_read, so no need to waste cycles zeroing it.
Quoted 65 lines
Quoted 65 lines
grub-core/commands/verify.c +16
-35+if (!readbuf) + goto fail; hash->write (context, &v,sizeof (v)); hash->write (context, &v4,sizeof (v4)); -while (rem) + + r =0 ; +while (r < rem) { - r =grub_file_read (sig, readbuf, - rem <READBUF_SIZE ? rem :READBUF_SIZE ); -if (r <0 ) - goto fail; -if (r ==0 ) +grub_ssize_t rr =grub_file_read (sig, readbuf + r, rem - r); +if (rr <0 ) + goto fail; +if (rr ==0 )break ; - hash->write (context, readbuf, r); - rem -= r; + r += rr; } +if (r != rem) + goto fail; + hash->write (context, readbuf, rem); + keyid =grub_subpacket_keyid_search (readbuf, rem); +grub_free (readbuf); + hash->write (context, &v,sizeof (v)); s =0xff ; hash->write (context, &s,sizeof (s));@@ -550,40 +593,34 @@ grub_verify_signature_real (char *buf, grub_size_t size, r =grub_file_read (sig, &unhashed_sub,sizeof (unhashed_sub));if (r !=sizeof (unhashed_sub)) goto fail; - { -grub_uint8_t *ptr; -grub_uint32_t l; - rem =grub_be_to_cpu16 (unhashed_sub); -if (rem >READBUF_SIZE ) - goto fail; - r =grub_file_read (sig, readbuf, rem); -if (r != rem) - goto fail; -for (ptr = readbuf; ptr < readbuf + rem; ptr += l) - { -if (*ptr <192 ) - l = *ptr++; -else if (*ptr <255 ) - { -if (ptr +1 >= readbuf + rem) -break ; - l = (((ptr[0 ] & ~192 ) <<GRUB_CHAR_BIT ) | ptr[1 ]) +192 ; - ptr +=2 ; - } -else - { -if (ptr +5 >= readbuf + rem) -break ; - l =grub_be_to_cpu32 (grub_get_unaligned32 (ptr +1 )); - ptr +=5 ; - }
Quoted 8 lines
grub-core/commands/verify.c grub_verify_signature_real (char *buf, grub_size_t size, · line 597
Quoted 6 lines
grub-core/commands/verify.c +2
-4-if (*ptr ==0x10 && l >=8 ) - keyid =grub_get_unaligned64 (ptr +1 ); - } - } + rem =grub_be_to_cpu16 (unhashed_sub); + readbuf =grub_malloc (rem);
grub_zalloc()?
Same as above
Quoted 2 lines
Daniel
Quoted 31 lines · Ignat Korchagin
Quoted 18 lines · Daniel Kiper
Quoted 17 lines · Ignat Korchagin
Reposting this, as requested by Daniel and rebasing on current tree.
Currently GRUB2 verify logic searches PGP keyid only in unhashed subpackets of PGP signature packet. As a result, signatures generated with GoLang openpgp package (https://godoc.org/golang.org/x/crypto/openpgp) could not be verified, because this package puts keyid in hashed subpackets and GRUB code never initializes the keyid variable, therefore is not able to find "verification key" with id
0x0.Signed-off-by Ignat Korchagin <[email protected]>--- grub-core/commands/verify.c | 115 +++++++++++++++++++++++++++++--------------- 1 file changed, 76 insertions(+), 39 deletions(-)grub-core/commands/verify.c +0
-0@@ -445,6 +445,38 @@ rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval,return ret; }Quoted 12 lines
grub-core/commands/verify.c rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval, · line 457
Quoted 10 lines
grub-core/commands/verify.c +10
-0+static grub_uint64_t +grub_subpacket_keyid_search (const grub_uint8_t * sub,grub_ssize_t sub_len) +{ +const grub_uint8_t *ptr; +grub_uint32_t l; +grub_uint64_t keyid =0 ; + +for (ptr = sub; ptr < sub + sub_len; ptr += l) + { +if (*ptr <192 )Please define some constants and use them properly...
Quoted 2 lines
This is original GRUB code here. It was just moved to a separate function. See below.
OK, however, I am against leaving unreadable code if we touch it anyway.
Quoted 3 lines
Also, defining constants here is probably more confusing. I do not know for sure, but suspect this algorithm is taken directly from RFC 4880 5.2.3.1 and there are no names there
So, say this thing in comment before grub_subpacket_keyid_search() function. This will improve situation a bit.
Quoted 43 lines
Quoted 35 lines
grub-core/commands/verify.c rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval, · line 459
Quoted 2 lines
grub-core/commands/verify.c +2
-0+ l = *ptr++; + else if (*ptr < 255)Ditto.
grub-core/commands/verify.c rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval, · line 463
Quoted 4 lines
grub-core/commands/verify.c +4
-0+ { +if (ptr +1 >= sub + sub_len) +break ; + l = (((ptr[0 ] & ~192 ) <<GRUB_CHAR_BIT ) | ptr[1 ]) +192 ;Ditto.
grub-core/commands/verify.c rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval, · line 464
Quoted 1 line
grub-core/commands/verify.c +1
-0+ ptr +=2 ;Ditto and below...
Quoted 19 lines
grub-core/commands/verify.c +15
-0+ } +else + { +if (ptr +5 >= sub + sub_len) +break ; + l =grub_be_to_cpu32 (grub_get_unaligned32 (ptr +1 )); + ptr +=5 ; + } +if (*ptr ==0x10 && l >=8 ) + keyid =grub_get_unaligned64 (ptr +1 ); + } + +return keyid; +} + static grub_err_t grub_verify_signature_real (char *buf, grub_size_t size, grub_file_t f, grub_file_t sig,@@ -529,20 +561,31 @@ grub_verify_signature_real (char *buf, grub_size_t size,Quoted 8 lines
grub-core/commands/verify.c grub_verify_signature_real (char *buf, grub_size_t size, · line 566
Quoted 6 lines
grub-core/commands/verify.c +3
-0break ; hash->write (context, readbuf, r); } +grub_free (readbuf); + + readbuf =grub_malloc (rem);
grub_zalloc()?
Quoted 2 lines
This buffer is filled below by
grub_file_read, so no need to waste cycles zeroing it.
Right, I realized that after posting my email. Hence, let's leave grub_malloc() as is here and below.
Daniel