Repository navigation
Reject Curve25519/Curve448 keys with the wrong length - #825
dlazobnaKocka wants to merge 3 commits into
Conversation
The *_import_raw() functions checked the key length with LTC_ARGCHK. A certificate or SSH public key with a short Ed25519 key could reach them, so the default build called abort(). With ARGTYPE 2 or 3 the memcpy read past the end of the input buffer. The PKCS#8 and SubjectPublicKeyInfo importers only checked the upper bound. A 1 byte key returned CRYPT_OK and the rest of the key buffer kept whatever was in it before. Return CRYPT_PK_INVALID_SIZE in all of these cases. Fixes: 52a24ca ("add Curve25519 API") Fixes: 076a110 ("Ed448 + X448") Fixes: 8d19047 ("Add support for more PEM file types + some fixes") Fixes: 208ee2b ("Refactor SubjectPublicKeyInfo import")
|
Looks good to me small correction: the default build via plain make or CMake Release uses ARGTYPE=4 not ARGTYPE=0 |
sjaeckel
left a comment
There was a problem hiding this comment.
Thanks for the PR!
Basically good changes, just some suggestions to improve the implementation.
There was a problem hiding this comment.
[0]: Sorry, but I don't like that style. Too difficult to understand IMO. Still I very much like the effort to keep the remaining code as is :)
| if ((err = der_decode_octet_string(priv_key->data, priv_key->size, key->priv, &key_len)) != CRYPT_OK) { | |
| goto LBL_ERR; | |
| } | |
| if (key_len != sizeof(key->priv)) { | |
| err = CRYPT_PK_INVALID_SIZE; | |
| goto LBL_ERR; | |
| } | |
| fp(key->pub, key->priv); | |
| key->type = PK_PRIVATE; | |
| err = pk_get_pka_id(id, &key->pka); | |
| LBL_ERR: | |
| return err; |
| if ((err = der_decode_octet_string(priv_key->data, priv_key->size, key->priv, &key_len)) == CRYPT_OK && key_len != priv_len) { | ||
| err = CRYPT_PK_INVALID_SIZE; | ||
| } | ||
| if (err == CRYPT_OK) { |
| if ((err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_ED25519, key->pub, &key_len, LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK && key_len != sizeof(key->pub)) { | ||
| err = CRYPT_PK_INVALID_SIZE; | ||
| } | ||
| if (err == CRYPT_OK) { |
There was a problem hiding this comment.
[1]: I'd suggest to add a unsigned long public_key_len_should argument to the API of x509_decode_subject_public_key_info() (after public_key_len) and verify inside that function before copying the data.
In case public_key_len_should == 0 don't check.
WDYT?
| if ((err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_ED448, key->pub, &key_len, LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK && key_len != sizeof(key->pub)) { | ||
| err = CRYPT_PK_INVALID_SIZE; | ||
| } | ||
| if (err == CRYPT_OK) { |
| if ((err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_X25519, key->pub, &key_len, LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK && key_len != sizeof(key->pub)) { | ||
| err = CRYPT_PK_INVALID_SIZE; | ||
| } | ||
| if (err == CRYPT_OK) { |
| if ((err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_X448, key->pub, &key_len, LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK && key_len != 56uL) { | ||
| err = CRYPT_PK_INVALID_SIZE; | ||
| } | ||
| if (err == CRYPT_OK) { |
|
|
||
| if (which == PK_PRIVATE) { | ||
| LTC_ARGCHK(inlen == 32uL || inlen == 64uL); | ||
| if (inlen != 32uL && inlen != 64uL) return CRYPT_PK_INVALID_SIZE; |
There was a problem hiding this comment.
IIRC it was decided that those statements stay LTC_ARGCHK'd since release builds don't call abort() and it's helpful when debug builds bail out loud.
This applies to all such changes.
@karel-m please correct me if I'm wrong!
There was a problem hiding this comment.
it seems to me that LTC_ARGCHK here would be a bit more appropriate
|
Thanks for the review, and thanks Karel for the ARGTYPE=4 note. You're right, I was looking at the fallback in tomcrypt_cfg.h, but the default Release build returns CRYPT_INVALID_ARG and doesn't abort. I'll fix the description. I'll switch the PKCS#8 checks to the if/goto form, also in the Curve448 file. Adding public_key_len_should to x509_decode_subject_public_key_info() sounds good, it's internal so the extra arguement is fine. I'll check the length there before the copy, with 0 meaning no check, and update the Curve25519/448 callers. About LTC_ARGCHK in *_import_raw: I get the usual rule. I returned an error because x509_import_spki() and the SSH parser pass key bytes from a cert or SSH key into those functions. With ARGTYPE 0 that aborts, and with ARGTYPE 2 or 3 it reads past the end of the buffer. I'd like to keep a real check there, but if you'd rather keep only LTC_ARGCHK, I can move the length check up into the SPKI and SSH callers. What do you prefer? |
Add a public_key_len_should argument, 0 means no check. The Curve25519/Curve448 importers pass their key size, so the check after the call is not needed anymore. Also use plain if/goto for the PKCS#8 length checks.
sjaeckel
left a comment
There was a problem hiding this comment.
Thanks for the update!
[...] with ARGTYPE 2 or 3 [...]
We ignore those two, since they're basically the crazy footgun modes. So there's no need to do any further checks if I'm not missing something and those checks can be reverted to LTC_ARGCHK.
|
Thanks, that answers it. I reverted the length checks in the *_import_raw funtions back to LTC_ARGCHK. I also dropped the unit checks that expected those to return an error. |
A certificate or SSH2 public key with a short Ed25519, X25519, Ed448 or X448 key reaches the *_import_raw() functions through x509_import_spki() or pem_ssh.c. Those check the length with LTC_ARGCHK. With ARGTYPE 0 that calls abort(), and with ARGTYPE 2 or 3 the memcpy reads past the end of the input buffer. The default Release build (ARGTYPE=4) returns CRYPT_INVALID_ARG.
This SSH2 key with a 1 byte Ed25519 key is enough, passed to pem_decode_openssh() in a build with ARGTYPE 0:
The PKCS#8 and SubjectPublicKeyInfo importers had a related problem. They only checked the upper bound, so a 1 byte key returned CRYPT_OK and the rest of the key buffer kept whatever was in it before.
The fix returns CRYPT_PK_INVALID_SIZE in all of these cases. Each curve gets a small test with a short PKCS#8, SPKI and raw key. The tests fail on develop and pass with the fix.
Checklist
Fixes:tag to the commit message