Skip to content

Validate pss params. #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 67 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
b4252fb
Add EVP_PKEY_RSA_PSS ameth.
bryce-shang Jan 22, 2021
2b13160
Add test to verify SubjectPublicKeyInfo decode.
bryce-shang Jan 22, 2021
7ef61e1
Add EVP_PKEY_RSA_PSS pmeth.
bryce-shang Jan 22, 2021
f50ceef
Import RSA_pkey_ctx_ctrl -- OpenSSL 1.1.1@e5e04ee.
bryce-shang Jan 23, 2021
ed2d72d
Support pad mode get/set for PSS keys. OpenSSL 1.1.1@a300c72.
bryce-shang Jan 25, 2021
68e7ca8
Fix TODO comment.
bryce-shang Jan 25, 2021
9e767ce
Add RSASSA-PSS-params decode.
bryce-shang Jan 26, 2021
a0b5e92
Remove trailer field related TODO.
bryce-shang Feb 2, 2021
0cafd57
Fix leaked mem.
bryce-shang Feb 2, 2021
be955ce
Remove unsupported sha oid.
bryce-shang Feb 5, 2021
4cab636
Revert "Remove unsupported sha oid."
bryce-shang Feb 5, 2021
b46e6a1
Merge branch 'rsa-pss' into pss-restriction
bryce-shang Feb 9, 2021
934181f
Check PSS parameters restrictions.
bryce-shang Feb 4, 2021
98fe315
Move rsassa_pss from rsa.h to rsassa_pss.h.
bryce-shang Feb 9, 2021
3ce3ce8
Merge branch 'rsa-pss' into pssparams-decode
bryce-shang Feb 9, 2021
bb39de5
Merge branch 'pssparams-decode' into pss-restriction
bryce-shang Feb 9, 2021
360d7b9
Fix merge conflict.
bryce-shang Feb 9, 2021
ff63b16
Move rsassapss conversion to rsassa_pss_asn1.c.
bryce-shang Feb 9, 2021
0c0ee9b
Correct error prefix.
bryce-shang Feb 9, 2021
0d16311
Remove unused import.
bryce-shang Feb 9, 2021
84dd9db
Merge branch 'pssparams-decode' into pss-restriction
bryce-shang Feb 9, 2021
66db90b
Modify comment.
bryce-shang Feb 9, 2021
3f96be0
Improve comment.
bryce-shang Feb 9, 2021
6f3fe46
Merge branch 'pssparams-decode' into pss-restriction
bryce-shang Feb 9, 2021
7a1a715
Rename some functions.
bryce-shang Feb 9, 2021
8057734
Add more tests.
bryce-shang Feb 9, 2021
a08637a
Add more tests.
bryce-shang Feb 11, 2021
2c5f371
Merge branch 'rsa-pss' into pssparams-decode
bryce-shang Feb 14, 2021
37e61ce
Merge branch 'pssparams-decode' into pss-restriction
bryce-shang Feb 14, 2021
0200c70
Resolve TODO: fix X509Test.TestRsaSsaPss.
bryce-shang Feb 15, 2021
3e0aa60
Use JDK11 generated DER-encoded RSASSA-PSS-params for test.
bryce-shang Feb 16, 2021
d283630
Disable rsassaPss encrypt and decrypt.
bryce-shang Feb 20, 2021
85fee03
Remove unused comment.
bryce-shang Feb 22, 2021
bf90322
Update crypto/rsa_extra/rsassa_pss.h
bryce-shang Feb 23, 2021
ab34ddb
Update crypto/rsa_extra/rsassa_pss.h
bryce-shang Feb 23, 2021
e8b8a09
Update crypto/rsa_extra/rsassa_pss_asn1.c
bryce-shang Feb 23, 2021
ebde70e
Update crypto/rsa_extra/rsassa_pss_asn1.c
bryce-shang Feb 23, 2021
3528e7a
Update crypto/rsa_extra/rsassa_pss_asn1.c
bryce-shang Feb 23, 2021
cb11033
Apply suggestions from code review
bryce-shang Feb 24, 2021
e4d8987
Enhance code comments.
bryce-shang Feb 24, 2021
f8003f4
Define tag value index macros.
bryce-shang Feb 24, 2021
230b1a6
Condense code.
bryce-shang Feb 25, 2021
51c3299
Update crypto/rsa_extra/rsassa_pss_asn1_test.cc
bryce-shang Feb 26, 2021
a29acce
Check EVP_PKEY_assign return value.
bryce-shang Mar 5, 2021
b0acd97
Check NID_undef value.
bryce-shang Mar 5, 2021
9c8ae6a
Correct comments.
bryce-shang Mar 5, 2021
fdad835
Add more tests.
bryce-shang Mar 5, 2021
e252bfc
Add more tests on trailer field and salt length.
bryce-shang Mar 6, 2021
25fb10c
Merge branch 'pssparams-decode' of https://github.com/bryce-shang/aws…
bryce-shang Mar 6, 2021
8ea5c1d
Rename rsassa_pss.h to internal.h.
bryce-shang Mar 8, 2021
cab16a1
Merge branch 'pssparams-decode' into pss-restriction
bryce-shang Mar 8, 2021
662d190
Removed unused test data.
bryce-shang Mar 8, 2021
7c0af70
Merge remote-tracking branch 'upstream/rsa-pss' into pss-restriction
bryce-shang Mar 8, 2021
1f12c84
Modify comments.
bryce-shang Mar 8, 2021
d45ab9e
Explicitly check NULL.
bryce-shang Mar 10, 2021
347335d
Change method comment.
bryce-shang Mar 10, 2021
b22bc77
Address error code comment.
bryce-shang Mar 10, 2021
d6f21d4
Apply suggestions from code review
bryce-shang Mar 11, 2021
0ea0d91
Return EVP_PKEY_assign_* value.
bryce-shang Mar 11, 2021
e47fa75
Fix incorrect error code.
bryce-shang Mar 11, 2021
c823fa3
Adjust comment.
bryce-shang Mar 11, 2021
87c5a71
Define macro for default value of salt len and trailer field.
bryce-shang Mar 11, 2021
4b6731e
Fix max salt len.
bryce-shang Mar 12, 2021
a6ad056
Add more comments.
bryce-shang Mar 15, 2021
7dd40fc
Check inputs are not NULL.
bryce-shang Apr 28, 2021
492d3d4
Remove saltlen == 0 check in pss_saltlen_create.
bryce-shang Apr 28, 2021
92457f0
Add ctx->pkey NULL check in pkey_pss_init.
bryce-shang Apr 28, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions crypto/evp/evp_ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,11 @@ int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx) {
}

ctx->operation = EVP_PKEY_OP_SIGN;
return 1;
if ((ctx->pmeth->sign_init == NULL) || (ctx->pmeth->sign_init(ctx))) {
return 1;
}
ctx->operation = EVP_PKEY_OP_UNDEFINED;
return 0;
}

int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *sig_len,
Expand All @@ -243,7 +247,11 @@ int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx) {
return 0;
}
ctx->operation = EVP_PKEY_OP_VERIFY;
return 1;
if ((ctx->pmeth->verify_init == NULL) || (ctx->pmeth->verify_init(ctx))) {
return 1;
}
ctx->operation = EVP_PKEY_OP_UNDEFINED;
return 0;
}

int EVP_PKEY_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t sig_len,
Expand Down
213 changes: 211 additions & 2 deletions crypto/evp/evp_extra_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,9 @@ static const uint8_t kExampleRSAKeyPKCS8[] = {
0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
};

// kExampleRSAPSSKeyPKCS8 is encoded in a PKCS #8 PrivateKeyInfo.
// kExampleRSAPSSKeyPKCS8 contains pss params for decoding params test.
// kExampleRSAPSSKeyPKCS8 contains a DER-encoded RSASSA-PSS-params:
// Hash Algorithm: sha256
// Mask Algorithm: mgf1 with sha256
static const uint8_t kExampleRSAPSSKeyPKCS8[] = {
0x30, 0x82, 0x04, 0xea, 0x02, 0x01, 0x00, 0x30, 0x38, 0x06, 0x09, 0x2a,
0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0a, 0x30, 0x2b, 0xa0, 0x0d,
Expand Down Expand Up @@ -348,6 +349,119 @@ static const uint8_t kExampleRSAPSSKeyPKCS8[] = {
0xe5, 0x7a, 0xa5, 0x8c, 0x31, 0xe5, 0xae, 0xe3, 0x27, 0xa7, 0x8e, 0xa8,
0x94, 0xc2};

// badRSAPSSKeyPKCS8_SaltLengthTooLarge is encoded in a PKCS #8 PrivateKeyInfo.
// badRSAPSSKeyPKCS8_SaltLengthTooLarge contains a DER-encoded RSASSA-PSS-params:
// Hash Algorithm: sha256
// Mask Algorithm: mgf1 with sha256
// Salt Length: 511
static const uint8_t badRSAPSSKeyPKCS8_SaltLengthTooLarge[] = {
0x30, 0x82, 0x04, 0xed, 0x02, 0x01, 0x00, 0x30, 0x3e, 0x06, 0x09, 0x2a,
0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0a, 0x30, 0x31, 0xa0, 0x0d,
0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02,
0x01, 0xa1, 0x1a, 0x30, 0x18, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
0x0d, 0x01, 0x01, 0x08, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
0x65, 0x03, 0x04, 0x02, 0x01, 0xa2, 0x04, 0x02, 0x02, 0x01, 0xff, 0x04,
0x82, 0x04, 0xa6, 0x30, 0x82, 0x04, 0xa2, 0x02, 0x01, 0x00, 0x02, 0x82,
0x01, 0x01, 0x00, 0xc4, 0xd8, 0xc8, 0x45, 0x0c, 0x9a, 0xc6, 0x5a, 0xf0,
0xe8, 0x34, 0x3b, 0x9d, 0x73, 0x01, 0xe0, 0x71, 0xb5, 0x3a, 0x55, 0x9e,
0xc6, 0xcd, 0xdc, 0xdb, 0x98, 0x29, 0xea, 0x75, 0x4f, 0xf9, 0xa9, 0x5c,
0xc1, 0x40, 0x1d, 0x7c, 0x7d, 0x60, 0x8f, 0x1e, 0x1d, 0xde, 0xb1, 0x9b,
0xca, 0x8d, 0x7d, 0x83, 0x09, 0xce, 0x56, 0x94, 0x22, 0x5e, 0xce, 0xa3,
0x5f, 0x66, 0x3e, 0x24, 0x08, 0x78, 0x05, 0xc1, 0xe2, 0x11, 0x08, 0x0d,
0x58, 0x1c, 0xec, 0xd3, 0xfd, 0xdb, 0x36, 0x39, 0x74, 0x7d, 0x79, 0xef,
0xd2, 0x32, 0x99, 0xb2, 0x44, 0xa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 0xe0, 0x53, 0xd8, 0xd5, 0x7d, 0xe4, 0x9b, 0x50, 0x3f,
0xa9, 0xee, 0x42, 0x2c, 0x97, 0x99, 0x7c, 0xf8, 0x2c, 0x59, 0xea, 0x70,
0x01, 0xe1, 0x5c, 0x98, 0x2e, 0x05, 0x24, 0xcb, 0x2a, 0xb3, 0x73, 0x24,
0x28, 0xa9, 0xec, 0xb7, 0x05, 0x5d, 0x02, 0x81, 0x80, 0x51, 0x25, 0xc5,
0xc4, 0x8c, 0xa1, 0xdc, 0x5a, 0x7f, 0xf2, 0xe3, 0xf7, 0x3f, 0xb8, 0x10,
0x6e, 0xea, 0x71, 0x19, 0x24, 0x58, 0x85, 0xb8, 0x08, 0x06, 0x66, 0x86,
0x70, 0x7f, 0xcb, 0x0e, 0xe4, 0xa8, 0xd8, 0x20, 0xf3, 0xd2, 0xa8, 0xaa,
0xac, 0xe6, 0x31, 0x05, 0x91, 0x46, 0x23, 0xe3, 0xb9, 0x1c, 0xb8, 0x5e,
0x46, 0x4f, 0xc6, 0x28, 0x13, 0x3f, 0xa6, 0xd5, 0x43, 0xe5, 0xa0, 0xab,
0xa2, 0xce, 0xa9, 0x62, 0x6c, 0x39, 0x93, 0x75, 0x7b, 0xdc, 0x98, 0x36,
0xec, 0x7c, 0x83, 0x27, 0xbe, 0x39, 0x99, 0x1d, 0xce, 0x44, 0x28, 0xcf,
0xc0, 0xeb, 0xa8, 0x37, 0x3b, 0x5f, 0xbc, 0x8c, 0x4e, 0x96, 0x25, 0x5c,
0x1e, 0xb1, 0x35, 0x33, 0x2f, 0xfd, 0x55, 0x32, 0x9b, 0xd3, 0xdd, 0x1d,
0xe1, 0x8a, 0x4b, 0x1f, 0xf9, 0xd4, 0x0a, 0xa9, 0xf7, 0xdc, 0xae, 0x75,
0x85, 0x32, 0x66, 0xed, 0x53};

// kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
// structure.
static const uint8_t kExampleECKeyDER[] = {
Expand Down Expand Up @@ -898,3 +1012,98 @@ TEST(EVPExtraTest, Ed25519Keygen) {
ASSERT_TRUE(EVP_DigestVerify(ctx.get(), sig, len,
reinterpret_cast<const uint8_t *>("hello"), 5));
}

struct RsassaPssParamsMatchTestInput {
const uint8_t *der;
size_t der_len;
const EVP_MD *signature_md;
const EVP_MD *rsa_mgf1_md;
int match;
} kRsassaPssParamsMatchTestInputs[] = {
{kExampleRSAPSSKeyPKCS8, sizeof(kExampleRSAPSSKeyPKCS8), EVP_sha1(),
EVP_sha1(), 0},
{kExampleRSAPSSKeyPKCS8, sizeof(kExampleRSAPSSKeyPKCS8), EVP_sha224(),
EVP_sha224(), 0},
{kExampleRSAPSSKeyPKCS8, sizeof(kExampleRSAPSSKeyPKCS8), EVP_sha256(),
EVP_sha256(), 1},
{kExampleRSAPSSKeyPKCS8, sizeof(kExampleRSAPSSKeyPKCS8), EVP_sha384(),
EVP_sha384(), 0},
{kExampleRSAPSSKeyPKCS8, sizeof(kExampleRSAPSSKeyPKCS8), EVP_sha512(),
EVP_sha512(), 0},
{kExampleRSAPSSKeyPKCS8, sizeof(kExampleRSAPSSKeyPKCS8), EVP_sha512_256(),
EVP_sha512_256(), 0},
};

class EVPRsaPssExtraTest
: public testing::TestWithParam<RsassaPssParamsMatchTestInput> {};

// This test checks params match.
// All parameters in the signature structure algorithm identifier MUST
// match the parameters in the key structure algorithm identifier except
// the saltLength field.
// See 3.3. https://tools.ietf.org/html/rfc4055#section-3.3
TEST_P(EVPRsaPssExtraTest, PssParamsMatch) {
const auto &param = GetParam();
const uint8_t *p = param.der;
const EVP_MD *signature_md = param.signature_md;
const EVP_MD *rsa_mgf1_md = param.rsa_mgf1_md;
// Holds ownership of heap-allocated EVP_PKEY.
bssl::UniquePtr<EVP_PKEY> pkey_up(
d2i_AutoPrivateKey(nullptr, &p, param.der_len));
ASSERT_TRUE(pkey_up);
EXPECT_EQ(param.der + param.der_len, p);
EXPECT_EQ(EVP_PKEY_RSA_PSS, EVP_PKEY_id(pkey_up.get()));
// Holds ownership of heap-allocated EVP_PKEY_CTX.
bssl::UniquePtr<EVP_PKEY_CTX> pkey_ctx_up(
EVP_PKEY_CTX_new(pkey_up.get(), nullptr));
ASSERT_TRUE(pkey_ctx_up);
EVP_PKEY_CTX *pkey_ctx = pkey_ctx_up.get();
// Init pss params by calling |EVP_PKEY_sign_init|.
// These pss params are fetched from the key structure.
ASSERT_TRUE(EVP_PKEY_sign_init(pkey_ctx));
EXPECT_TRUE(EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING));
// Compare one way hash func.
EXPECT_EQ(EVP_PKEY_CTX_set_signature_md(pkey_ctx, signature_md), param.match);
// Compare one way hash func of mask gen.
EXPECT_EQ(EVP_PKEY_CTX_set_rsa_mgf1_md(pkey_ctx, rsa_mgf1_md), param.match);
EXPECT_TRUE(
EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, RSA_PSS_SALTLEN_DIGEST));
ERR_clear_error();
}

INSTANTIATE_TEST_SUITE_P(All, EVPRsaPssExtraTest,
testing::ValuesIn(kRsassaPssParamsMatchTestInputs));

struct BadPssKeyTestInput {
const uint8_t *der;
size_t der_len;
} kBadPssKeyTestInputs[] = {
{badRSAPSSKeyPKCS8_SaltLengthTooLarge, sizeof(badRSAPSSKeyPKCS8_SaltLengthTooLarge)},
};

class EVPRsaPssBadKeyTest : public testing::TestWithParam<BadPssKeyTestInput> {
};

// This test checks pss salt length.
TEST_P(EVPRsaPssBadKeyTest, InvalidSaltLength) {
const auto &param = GetParam();
const uint8_t *p = param.der;
// Holds ownership of heap-allocated EVP_PKEY.
bssl::UniquePtr<EVP_PKEY> pkey_up(
d2i_AutoPrivateKey(nullptr, &p, param.der_len));
ASSERT_TRUE(pkey_up);
EXPECT_EQ(param.der + param.der_len, p);
EXPECT_EQ(EVP_PKEY_RSA_PSS, EVP_PKEY_id(pkey_up.get()));
// Holds ownership of heap-allocated EVP_PKEY_CTX.
bssl::UniquePtr<EVP_PKEY_CTX> pkey_ctx_up(
EVP_PKEY_CTX_new(pkey_up.get(), nullptr));
ASSERT_TRUE(pkey_ctx_up);
EVP_PKEY_CTX *pkey_ctx = pkey_ctx_up.get();
// Init pss params by calling |EVP_PKEY_sign_init|.
// During initialization, the salt length is validated.
ASSERT_FALSE(EVP_PKEY_sign_init(pkey_ctx));
ERR_clear_error();
}

INSTANTIATE_TEST_SUITE_P(All, EVPRsaPssBadKeyTest,
testing::ValuesIn(kBadPssKeyTestInputs));
3 changes: 2 additions & 1 deletion crypto/evp/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,13 @@ struct evp_pkey_method_st {

int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);

int (*sign_init)(EVP_PKEY_CTX *ctx);
int (*sign)(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen,
const uint8_t *tbs, size_t tbslen);

int (*sign_message)(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen,
const uint8_t *tbs, size_t tbslen);

int (*verify_init)(EVP_PKEY_CTX *ctx);
int (*verify)(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t siglen,
const uint8_t *tbs, size_t tbslen);

Expand Down
2 changes: 2 additions & 0 deletions crypto/evp/p_ec.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,10 @@ const EVP_PKEY_METHOD ec_pkey_meth = {
pkey_ec_copy,
pkey_ec_cleanup,
pkey_ec_keygen,
NULL /* sign_init */,
pkey_ec_sign,
NULL /* sign_message */,
NULL /* verify_init */,
pkey_ec_verify,
NULL /* verify_message */,
NULL /* verify_recover */,
Expand Down
2 changes: 2 additions & 0 deletions crypto/evp/p_ed25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ const EVP_PKEY_METHOD ed25519_pkey_meth = {
pkey_ed25519_copy,
NULL /* cleanup */,
pkey_ed25519_keygen,
NULL /* sign_init */,
NULL /* sign */,
pkey_ed25519_sign_message,
NULL /* verify_init */,
NULL /* verify */,
pkey_ed25519_verify_message,
NULL /* verify_recover */,
Expand Down
Loading