-
Notifications
You must be signed in to change notification settings - Fork 1k
Reject empty PKCS11 ECDSA integers #11339
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
Open
aidangarske
wants to merge
3
commits into
wolfSSL:master
Choose a base branch
from
aidangarske:fenrir-fixes-12259
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,272 @@ | ||
| /* test_pkcs11.c | ||
| * | ||
| * Copyright (C) 2006-2026 wolfSSL Inc. | ||
| * | ||
| * This file is part of wolfSSL. | ||
| * | ||
| * wolfSSL is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation; either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * wolfSSL is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program; if not, write to the Free Software | ||
| * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA | ||
| */ | ||
|
|
||
| #include <tests/unit.h> | ||
|
|
||
| #include <tests/api/api.h> | ||
| #include <tests/api/test_pkcs11.h> | ||
|
|
||
| #if defined(HAVE_PKCS11) && defined(HAVE_ECC) && \ | ||
| defined(HAVE_ECC_VERIFY) && !defined(WC_NO_RNG) && \ | ||
| !defined(NO_ECC256) && !defined(NO_ECC_SECP) | ||
|
|
||
| #include <wolfssl/wolfcrypt/ecc.h> | ||
| #include <wolfssl/wolfcrypt/random.h> | ||
| #include <wolfssl/wolfcrypt/wc_pkcs11.h> | ||
|
|
||
| static int test_pkcs11_verify_init_calls = 0; | ||
| static int test_pkcs11_verify_calls = 0; | ||
| static const byte* test_pkcs11_expected_sig = NULL; | ||
| static word32 test_pkcs11_expected_sig_len = 0; | ||
|
|
||
| static CK_RV test_pkcs11_get_mechanism_info(CK_SLOT_ID slotId, | ||
| CK_MECHANISM_TYPE type, CK_MECHANISM_INFO_PTR info) | ||
| { | ||
| (void)slotId; | ||
|
|
||
| if (type != CKM_ECDSA || info == NULL) | ||
| return CKR_MECHANISM_INVALID; | ||
|
|
||
| XMEMSET(info, 0, sizeof(*info)); | ||
| info->flags = CKF_VERIFY; | ||
| return CKR_OK; | ||
| } | ||
|
|
||
| static CK_RV test_pkcs11_create_object(CK_SESSION_HANDLE session, | ||
| CK_ATTRIBUTE_PTR template, CK_ULONG count, CK_OBJECT_HANDLE_PTR object) | ||
| { | ||
| (void)session; | ||
| (void)template; | ||
| (void)count; | ||
|
|
||
| if (object == NULL) | ||
| return CKR_MECHANISM_INVALID; | ||
|
|
||
| *object = 1; | ||
| return CKR_OK; | ||
| } | ||
|
|
||
| static CK_RV test_pkcs11_destroy_object(CK_SESSION_HANDLE session, | ||
| CK_OBJECT_HANDLE object) | ||
| { | ||
| (void)session; | ||
| (void)object; | ||
|
|
||
| return CKR_OK; | ||
| } | ||
|
|
||
| static CK_RV test_pkcs11_verify_init(CK_SESSION_HANDLE session, | ||
| CK_MECHANISM_PTR mechanism, CK_OBJECT_HANDLE key) | ||
| { | ||
| (void)session; | ||
| (void)mechanism; | ||
| (void)key; | ||
|
|
||
| test_pkcs11_verify_init_calls++; | ||
| return CKR_OK; | ||
| } | ||
|
|
||
| static CK_RV test_pkcs11_verify(CK_SESSION_HANDLE session, CK_BYTE_PTR data, | ||
| CK_ULONG dataLen, CK_BYTE_PTR signature, CK_ULONG signatureLen) | ||
| { | ||
| (void)session; | ||
| (void)data; | ||
| (void)dataLen; | ||
|
|
||
| test_pkcs11_verify_calls++; | ||
| if (test_pkcs11_expected_sig == NULL || | ||
| signatureLen != test_pkcs11_expected_sig_len || | ||
| XMEMCMP(signature, test_pkcs11_expected_sig, signatureLen) != 0) { | ||
| return CKR_SIGNATURE_INVALID; | ||
| } | ||
|
|
||
| return CKR_OK; | ||
| } | ||
|
|
||
| int test_wc_Pkcs11_EcdsaSigDecode(void) | ||
| { | ||
| static const byte emptyR[] = { | ||
| 0x30, 0x05, 0x02, 0x00, 0x02, 0x01, 0x01 | ||
| }; | ||
| static const byte emptyS[] = { | ||
|
aidangarske marked this conversation as resolved.
aidangarske marked this conversation as resolved.
|
||
| 0x30, 0x05, 0x02, 0x01, 0x01, 0x02, 0x00 | ||
| }; | ||
| static const byte hash[] = { 0x00 }; | ||
| byte oversizedR[40]; | ||
| byte oversizedS[40]; | ||
| byte paddedSig[72]; | ||
| byte expectedSig[64]; | ||
| Pkcs11Token token; | ||
| CK_FUNCTION_LIST func; | ||
| wc_CryptoInfo info; | ||
| ecc_key key; | ||
| WC_RNG rng; | ||
| int ret; | ||
| int res = 0; | ||
| int haveKey = 0; | ||
| int haveRng = 0; | ||
| EXPECT_DECLS; | ||
|
|
||
| XMEMSET(&token, 0, sizeof(token)); | ||
| XMEMSET(&func, 0, sizeof(func)); | ||
| XMEMSET(&info, 0, sizeof(info)); | ||
| XMEMSET(&key, 0, sizeof(key)); | ||
| XMEMSET(&rng, 0, sizeof(rng)); | ||
| XMEMSET(oversizedR, 0, sizeof(oversizedR)); | ||
| oversizedR[0] = 0x30; | ||
| oversizedR[1] = 0x26; | ||
| oversizedR[2] = 0x02; | ||
| oversizedR[3] = 0x21; | ||
| oversizedR[4] = 0x01; | ||
| oversizedR[37] = 0x02; | ||
| oversizedR[38] = 0x01; | ||
| oversizedR[39] = 0x01; | ||
| XMEMSET(oversizedS, 0, sizeof(oversizedS)); | ||
| oversizedS[0] = 0x30; | ||
| oversizedS[1] = 0x26; | ||
| oversizedS[2] = 0x02; | ||
| oversizedS[3] = 0x01; | ||
| oversizedS[4] = 0x01; | ||
| oversizedS[5] = 0x02; | ||
| oversizedS[6] = 0x21; | ||
| oversizedS[7] = 0x01; | ||
| XMEMSET(paddedSig, 0, sizeof(paddedSig)); | ||
| paddedSig[0] = 0x30; | ||
| paddedSig[1] = 0x46; | ||
| paddedSig[2] = 0x02; | ||
| paddedSig[3] = 0x21; | ||
| paddedSig[5] = 0x80; | ||
| paddedSig[37] = 0x02; | ||
| paddedSig[38] = 0x21; | ||
| paddedSig[40] = 0x81; | ||
| XMEMSET(expectedSig, 0, sizeof(expectedSig)); | ||
| expectedSig[0] = 0x80; | ||
| expectedSig[32] = 0x81; | ||
|
|
||
| func.C_GetMechanismInfo = test_pkcs11_get_mechanism_info; | ||
| func.C_CreateObject = test_pkcs11_create_object; | ||
| func.C_DestroyObject = test_pkcs11_destroy_object; | ||
| func.C_VerifyInit = test_pkcs11_verify_init; | ||
| func.C_Verify = test_pkcs11_verify; | ||
| token.func = &func; | ||
| token.slotId = 1; | ||
| token.handle = 1; | ||
| token.version = WC_PCKS11VERSION_2_40; | ||
|
|
||
| ret = wc_InitRng(&rng); | ||
| ExpectIntEQ(ret, 0); | ||
| if (ret == 0) | ||
| haveRng = 1; | ||
| if (ret == 0) | ||
| ret = wc_ecc_init(&key); | ||
| ExpectIntEQ(ret, 0); | ||
| if (ret == 0) | ||
| haveKey = 1; | ||
| if (ret == 0) | ||
| ret = wc_ecc_make_key(&rng, 32, &key); | ||
| #ifdef WOLFSSL_ASYNC_CRYPT | ||
| if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) | ||
| ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE); | ||
| #endif | ||
| ExpectIntEQ(ret, 0); | ||
|
|
||
| info.algo_type = WC_ALGO_TYPE_PK; | ||
| info.pk.type = WC_PK_TYPE_ECDSA_VERIFY; | ||
| info.pk.eccverify.hash = hash; | ||
| info.pk.eccverify.hashlen = sizeof(hash); | ||
| info.pk.eccverify.key = &key; | ||
| info.pk.eccverify.res = &res; | ||
| test_pkcs11_expected_sig = NULL; | ||
| test_pkcs11_expected_sig_len = 0; | ||
|
|
||
| if (ret == 0) { | ||
| info.pk.eccverify.sig = emptyR; | ||
| info.pk.eccverify.siglen = sizeof(emptyR); | ||
| res = 0; | ||
| test_pkcs11_verify_init_calls = 0; | ||
| test_pkcs11_verify_calls = 0; | ||
| ExpectIntEQ(wc_Pkcs11_CryptoDevCb(1, &info, &token), | ||
| WC_NO_ERR_TRACE(ASN_PARSE_E)); | ||
| ExpectIntEQ(test_pkcs11_verify_init_calls, 0); | ||
| ExpectIntEQ(test_pkcs11_verify_calls, 0); | ||
| } | ||
|
|
||
| if (ret == 0) { | ||
| info.pk.eccverify.sig = emptyS; | ||
| info.pk.eccverify.siglen = sizeof(emptyS); | ||
| res = 0; | ||
| test_pkcs11_verify_init_calls = 0; | ||
| test_pkcs11_verify_calls = 0; | ||
| ExpectIntEQ(wc_Pkcs11_CryptoDevCb(1, &info, &token), | ||
| WC_NO_ERR_TRACE(ASN_PARSE_E)); | ||
| ExpectIntEQ(test_pkcs11_verify_init_calls, 0); | ||
| ExpectIntEQ(test_pkcs11_verify_calls, 0); | ||
| } | ||
|
|
||
| if (ret == 0) { | ||
| info.pk.eccverify.sig = oversizedR; | ||
| info.pk.eccverify.siglen = sizeof(oversizedR); | ||
| res = 0; | ||
| test_pkcs11_verify_init_calls = 0; | ||
| test_pkcs11_verify_calls = 0; | ||
| ExpectIntEQ(wc_Pkcs11_CryptoDevCb(1, &info, &token), | ||
| WC_NO_ERR_TRACE(ASN_PARSE_E)); | ||
| ExpectIntEQ(test_pkcs11_verify_init_calls, 0); | ||
| ExpectIntEQ(test_pkcs11_verify_calls, 0); | ||
| } | ||
|
|
||
| if (ret == 0) { | ||
| info.pk.eccverify.sig = oversizedS; | ||
| info.pk.eccverify.siglen = sizeof(oversizedS); | ||
| res = 0; | ||
| test_pkcs11_verify_init_calls = 0; | ||
| test_pkcs11_verify_calls = 0; | ||
| ExpectIntEQ(wc_Pkcs11_CryptoDevCb(1, &info, &token), | ||
| WC_NO_ERR_TRACE(ASN_PARSE_E)); | ||
| ExpectIntEQ(test_pkcs11_verify_init_calls, 0); | ||
| ExpectIntEQ(test_pkcs11_verify_calls, 0); | ||
| } | ||
|
|
||
| if (ret == 0) { | ||
| info.pk.eccverify.sig = paddedSig; | ||
| info.pk.eccverify.siglen = sizeof(paddedSig); | ||
| res = 0; | ||
| test_pkcs11_verify_init_calls = 0; | ||
| test_pkcs11_verify_calls = 0; | ||
| test_pkcs11_expected_sig = expectedSig; | ||
| test_pkcs11_expected_sig_len = sizeof(expectedSig); | ||
| ExpectIntEQ(wc_Pkcs11_CryptoDevCb(1, &info, &token), 0); | ||
| ExpectIntEQ(res, 1); | ||
| ExpectIntEQ(test_pkcs11_verify_init_calls, 1); | ||
| ExpectIntEQ(test_pkcs11_verify_calls, 1); | ||
| } | ||
|
|
||
| test_pkcs11_expected_sig = NULL; | ||
| test_pkcs11_expected_sig_len = 0; | ||
| if (haveKey) | ||
| wc_ecc_free(&key); | ||
| if (haveRng) | ||
| DoExpectIntEQ(wc_FreeRng(&rng), 0); | ||
|
|
||
| return EXPECT_RESULT(); | ||
| } | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* test_pkcs11.h | ||
| * | ||
| * Copyright (C) 2006-2026 wolfSSL Inc. | ||
| * | ||
| * This file is part of wolfSSL. | ||
| * | ||
| * wolfSSL is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation; either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * wolfSSL is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program; if not, write to the Free Software | ||
| * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA | ||
| */ | ||
|
|
||
| #ifndef WOLFCRYPT_TEST_PKCS11_H | ||
| #define WOLFCRYPT_TEST_PKCS11_H | ||
|
|
||
| #include <tests/api/api_decl.h> | ||
|
|
||
| #if defined(HAVE_PKCS11) && defined(HAVE_ECC) && \ | ||
| defined(HAVE_ECC_VERIFY) && !defined(WC_NO_RNG) && \ | ||
| !defined(NO_ECC256) && !defined(NO_ECC_SECP) | ||
|
|
||
| int test_wc_Pkcs11_EcdsaSigDecode(void); | ||
|
|
||
| #define TEST_PKCS11_DECLS \ | ||
| TEST_DECL_GROUP("pkcs11", test_wc_Pkcs11_EcdsaSigDecode) | ||
|
|
||
| #else | ||
|
|
||
| #define TEST_PKCS11_DECLS | ||
|
|
||
| #endif | ||
|
|
||
| #endif /* WOLFCRYPT_TEST_PKCS11_H */ |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.