Skip to content

Commit

Permalink
pkcs11test is passing all tests
Browse files Browse the repository at this point in the history
Changes
    - wc_HmacInit aware of devId
    - Creation and calling of WP11_Ec_ProvisionedKey_Sign
    - pkcs11test.c changes required due to provisioned key.
    - pkcs11test.c macros guards for SHA224

Stuff that might need to be reverted.
    - NewObject AddObject un-static-ed
    - wp11_Slot_Init disabled code.
    - Call wolfSSL_Init and wolfSSL_CTX_new() in WP11_Library_Init()
    - WP11_Slot_OpenSession disabled code.

This is a work in progress.
  • Loading branch information
anhu committed Oct 22, 2024
1 parent a30e5fd commit 88ae71f
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 14 deletions.
49 changes: 40 additions & 9 deletions src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#endif
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/asn_public.h>

#include <wolfpkcs11/pkcs11.h>
#include <wolfpkcs11/internal.h>
Expand Down Expand Up @@ -424,9 +425,9 @@ static CK_RV SetAttributeValue(WP11_Session* session, WP11_Object* obj,
* CKR_FUNCTION_FAILED when setting an attribute fails.
* CKR_OK on success.
*/
static CK_RV NewObject(WP11_Session* session, CK_KEY_TYPE keyType,
CK_OBJECT_CLASS keyClass, CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount, WP11_Object** object)
CK_RV NewObject(WP11_Session* session, CK_KEY_TYPE keyType,
CK_OBJECT_CLASS keyClass, CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount, WP11_Object** object)
{
int ret;
CK_RV rv;
Expand Down Expand Up @@ -466,9 +467,9 @@ static CK_RV NewObject(WP11_Session* session, CK_KEY_TYPE keyType,
* CKR_FUNCTION_FAILED when setting an attribute fails.
* CKR_OK on success.
*/
static CK_RV AddObject(WP11_Session* session, WP11_Object* object,
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
CK_OBJECT_HANDLE_PTR phKey)
CK_RV AddObject(WP11_Session* session, WP11_Object* object,
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
CK_OBJECT_HANDLE_PTR phKey)
{
int ret;
CK_ATTRIBUTE* attr;
Expand Down Expand Up @@ -2419,7 +2420,7 @@ CK_RV C_DigestFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pDigest,
CK_RV C_SignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey)
{
int ret;
int ret = 0;
WP11_Session* session;
WP11_Object* obj = NULL;
CK_KEY_TYPE type;
Expand All @@ -2433,10 +2434,28 @@ CK_RV C_SignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
return CKR_ARGUMENTS_BAD;

ret = WP11_Object_Find(session, hKey, &obj);
if (ret != 0)
#if 1
if ((ret != 0) && (hKey == 0) && (pMechanism->mechanism == CKM_ECDSA)) {
if (pMechanism->pParameter != NULL || pMechanism->ulParameterLen != 0) {
return CKR_MECHANISM_PARAM_INVALID;
}

/* Do not worry; the private key is pre-provisioned, but note there is
* no object to set and we do not initialize because we want to get
* the MAXQ to sign. */
init = WP11_INIT_ECDSA_SIGN;
WP11_Session_SetMechanism(session, pMechanism->mechanism);
//WP11_Session_SetOpInitialized(session, init);

return CKR_OK;
} else
#endif
if (ret != 0) {
return CKR_OBJECT_HANDLE_INVALID;
}

type = WP11_Object_GetType(obj);

switch (pMechanism->mechanism) {
#ifndef NO_RSA
case CKM_RSA_X_509:
Expand Down Expand Up @@ -2635,8 +2654,20 @@ CK_RV C_Sign(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,
#endif
#ifdef HAVE_ECC
case CKM_ECDSA:
if (!WP11_Session_IsOpInitialized(session, WP11_INIT_ECDSA_SIGN))
if (!WP11_Session_IsOpInitialized(session, WP11_INIT_ECDSA_SIGN)) {
#if 1
sigLen = (word32)*pulSignatureLen;
/* The MAXQ1065 has its own preprovisioned private key. Use that
* to sign it. */
ret = WP11_Ec_ProvisionedKey_Sign(session,
pData, (int)ulDataLen,
pSignature, &sigLen);
*pulSignatureLen = sigLen;
break;
#else
return CKR_OPERATION_NOT_INITIALIZED;
#endif
}

sigLen = WP11_Ec_SigLen(obj);
if (pSignature == NULL) {
Expand Down
154 changes: 152 additions & 2 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <wolfssl/wolfcrypt/rsa.h>
#include <wolfssl/wolfcrypt/asn_public.h>
#include <wolfssl/wolfcrypt/aes.h>
#include <wolfssl/ssl.h>

#include <wolfpkcs11/internal.h>
#include <wolfpkcs11/store.h>
Expand Down Expand Up @@ -3271,9 +3272,9 @@ static void wp11_Slot_Final(WP11_Slot* slot)
static int wp11_Slot_Init(WP11_Slot* slot, int id)
{
int ret = 0;
char label[LABEL_SZ] = { 0, };
int i;
WP11_Session* curr;
char label[LABEL_SZ] = { 0, };

XMEMSET(slot, 0, sizeof(*slot));
slot->id = id;
Expand All @@ -3293,6 +3294,67 @@ static int wp11_Slot_Init(WP11_Slot* slot, int id)
ret = wp11_Token_Init(&slot->token, label);
slot->token.state = WP11_TOKEN_STATE_UNKNOWN;
}
#if 0
//#ifdef WOLFSSL_MAXQ10XX_CRYPTO
{
CK_BBOOL ckFalse = CK_FALSE;
CK_BBOOL ckTrue = CK_TRUE;
CK_OBJECT_HANDLE priv_handle = CK_INVALID_HANDLE;
CK_OBJECT_CLASS keyClass = CKO_PRIVATE_KEY;
CK_KEY_TYPE keyType =CKK_EC;
unsigned char kid[2] = { 0x10, 0x04 };

WP11_Object* priv_object = NULL;
CK_ATTRIBUTE privKeyTmpl[] = {
{ CKA_CLASS, &keyClass, sizeof(keyClass) },
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) },
{ CKA_DECRYPT, &ckFalse, sizeof(ckFalse) },
{ CKA_SIGN, &ckTrue, sizeof(ckTrue) },
{ CKA_ID, kid, sizeof(kid) },
{ CKA_TOKEN, &ckTrue, sizeof(ckTrue) }
};
int privKeyTmplCnt = 6;
CK_SESSION_HANDLE session;

if (ret == 0) {
ret = WP11_Slot_OpenSession(slot,
CKF_SERIAL_SESSION | CKF_RW_SESSION,
NULL, NULL, &session);
}

if (ret == 0) {
ret = WP11_Session_Get(session, &curr);
}

/* Create a private key object with ID 1004. This is created during the
* init_tls scripts in the MAXQ10XX SDK and is the private key used for
* for client auth. */

if (ret == 0) {
/* this is just using last session created. Is this ok? Should
* I access through some API? ....same with AddObject() */
rv = NewObject(curr, CKK_EC, CKO_PRIVATE_KEY,
privKeyTmpl, privKeyTmplCnt,
&priv_object);
}

/*TODO: Populate priv and the template. See WP11_Ec_GenerateKeyPair
for priv and whatever calls C_GenerateKeyPair for the template.
Might not need to do anything....we'll see.
*/

if (rv == CKR_OK) {
rv = AddObject(curr, priv_object,
privKeyTmpl, privKeyTmplCnt,
&priv_handle);
}

if (rv == CKR_OK) {
//WP11_Slot_CloseSession(slot, &session);
WP11_Slot_CloseSession(slot, curr);
}
}
#endif

if (ret != 0) {
wp11_Slot_Final(slot);
Expand Down Expand Up @@ -3352,6 +3414,9 @@ int WP11_Library_Init(void)
if (ret == 0) {
#ifdef WOLFSSL_MAXQ10XX_CRYPTO
ret = wolfCrypt_Init();
wolfSSL_Init();
wolfSSL_Debugging_ON();
wolfSSL_CTX_new(wolfTLSv1_2_client_method());
if (ret == 0) {
ret = wc_InitRng_ex(&globalRandom, NULL, MAXQ_DEVICE_ID);
}
Expand Down Expand Up @@ -3546,6 +3611,51 @@ int WP11_Slot_OpenSession(WP11_Slot* slot, unsigned long flags, void* app,
}
WP11_Lock_UnlockRW(&slot->lock);


/* Total hack!! But figure out how to do it properly later. */
#if 0
// #ifdef WOLFSSL_MAXQ10XX_CRYPTO
{
CK_BBOOL ckFalse = CK_FALSE;
CK_BBOOL ckTrue = CK_TRUE;
CK_OBJECT_HANDLE priv_handle = CK_INVALID_HANDLE;
CK_OBJECT_CLASS keyClass = CKO_PRIVATE_KEY;
CK_KEY_TYPE keyType =CKK_EC;
unsigned char kid[2] = { 0x10, 0x04 };
CK_RV rv;

WP11_Object* priv_object = NULL;
CK_ATTRIBUTE privKeyTmpl[] = {
{ CKA_CLASS, &keyClass, sizeof(keyClass) },
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) },
{ CKA_DECRYPT, &ckFalse, sizeof(ckFalse) },
{ CKA_SIGN, &ckTrue, sizeof(ckTrue) },
{ CKA_ID, kid, sizeof(kid) },
};
int privKeyTmplCnt = 5;

/* Create a private key object with ID 1004. This is created during the
* init_tls scripts in the MAXQ10XX SDK and is the private key used for
* for client auth. */

if (ret == 0) {
rv = NewObject(curr, CKK_EC, CKO_PRIVATE_KEY,
privKeyTmpl, privKeyTmplCnt,
&priv_object);
}

/*TODO: Populate priv and the template. See WP11_Ec_GenerateKeyPair
for priv and whatever calls C_GenerateKeyPair for the template.
Might not need to do anything....we'll see.
*/

if (rv == CKR_OK) {
rv = AddObject(curr, priv_object,
privKeyTmpl, privKeyTmplCnt,
&priv_handle);
}
}
#endif
/* Ignored at this time. */
(void)app;
(void)notify;
Expand Down Expand Up @@ -7219,6 +7329,46 @@ int WP11_Ec_Sign(unsigned char* hash, word32 hashLen, unsigned char* sig,
return ret;
}

#if 1
int WP11_Ec_ProvisionedKey_Sign(WP11_Session* session,
unsigned char *hash, word32 hashLen,
unsigned char *sig, word32* sigLen)
{
/* The public key registered as a private key in the TLS client is not sent down
* to wolfPKCS11. So, we do it again here. This is from wolfssl's
* certs/ecc-client-keyPub.der.
* TODO: move to internal.c. It knows about devId = 1065.
* TODO: check siglen */

byte dummyBytes[] = {
0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4, 0x0f, 0x44,
0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5, 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e,
0x19, 0x80, 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda, 0xef, 0xa2, 0x35, 0x12,
0x43, 0x84, 0x76, 0x16, 0xc6, 0x56, 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f, 0xb4
};
ecc_key dummyKey;
word32 idx = 0;
word32 localSigLen = *sigLen;
WC_RNG rng;
int ret;
WP11_Slot* slot = WP11_Session_GetSlot(session);
ret = wc_ecc_init_ex(&dummyKey, NULL, slot->devId);
//ret = wc_ecc_init_ex(&dummyKey, NULL, 1065);

ret = wc_EccPublicKeyDecode(dummyBytes, &idx, &dummyKey,
sizeof(dummyBytes));
ret = Rng_New(&slot->token.rng, &slot->token.rngLock, &rng);
ret = wc_ecc_sign_hash(hash, (int)hashLen,
sig, &localSigLen, &rng,
&dummyKey);
Rng_Free(&rng);
wc_ecc_free(&dummyKey);
*sigLen = localSigLen;
return ret;
}
#endif
/**
* ECDSA verify encoded hash with public key.
*
Expand Down Expand Up @@ -8215,7 +8365,7 @@ int WP11_Hmac_Init(CK_MECHANISM_TYPE mechanism, WP11_Object* secret,
if (ret == 0)
hmac->hmacSz = wc_HmacSizeByType(hashType);
if (ret == 0)
ret = wc_HmacInit(&hmac->hmac, NULL, INVALID_DEVID);
ret = wc_HmacInit(&hmac->hmac, NULL, secret->slot->devId);
if (ret == 0) {
if (secret->onToken)
WP11_Lock_LockRO(secret->lock);
Expand Down
26 changes: 23 additions & 3 deletions tests/pkcs11test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2929,12 +2929,22 @@ static CK_RV test_pubkey_sig_fail(CK_SESSION_HANDLE session, CK_MECHANISM* mech,
CHECK_CKR_FAIL(ret, CKR_OPERATION_NOT_INITIALIZED, "Verify wrong init");
}
if (ret == CKR_OK) {
ret = funcList->C_VerifyInit(session, mech, pub);
CHECK_CKR(ret, "Verify Init");
ret = funcList->C_VerifyInit(session, mech, pub);
CHECK_CKR(ret, "Verify Init");
}
if (ret == CKR_OK) {
ret = funcList->C_Sign(session, hash, hashSz, out, &outSz);
CHECK_CKR_FAIL(ret, CKR_OPERATION_NOT_INITIALIZED, "Sign wrong init");
#ifdef WOLFSSL_MAXQ10XX_CRYPTO
if (mech->mechanism == CKM_ECDSA) {
/* In the case of MAXQ10XX the ECC private key is pre-provisioned
* so its fine to call it in this state */
CHECK_CKR(ret, "Sign");
} else
#endif
{
CHECK_CKR_FAIL(ret, CKR_OPERATION_NOT_INITIALIZED,
"Sign wrong init");
}
}

return ret;
Expand Down Expand Up @@ -3793,11 +3803,13 @@ static CK_RV test_rsa_fixed_keys_oaep(void* args)
0);
CHECK_CKR(ret, "SHA1 No AAD");
}
#ifdef WOLFSSL_SHA224
if (ret == CKR_OK) {
ret = rsa_oaep_test(session, priv, pub, CKM_SHA224, CKG_MGF1_SHA224,
NULL, 0);
CHECK_CKR(ret, "SHA224 No AAD");
}
#endif
if (ret == CKR_OK) {
ret = rsa_oaep_test(session, priv, pub, CKM_SHA384, CKG_MGF1_SHA384,
NULL, 0);
Expand Down Expand Up @@ -3892,10 +3904,12 @@ static CK_RV test_rsa_fixed_keys_pss(void* args)
ret = rsa_pss_test(session, priv, pub, CKM_SHA1, CKG_MGF1_SHA1, 20);
CHECK_CKR(ret, "RSA PKCS#1 PSS - SHA1");
}
#ifdef WOLFSSL_SHA224
if (ret == CKR_OK) {
ret = rsa_pss_test(session, priv, pub, CKM_SHA224, CKG_MGF1_SHA224, 28);
CHECK_CKR(ret, "RSA PKCS#1 PSS - SHA224");
}
#endif
if (ret == CKR_OK) {
ret = rsa_pss_test(session, priv, pub, CKM_SHA384, CKG_MGF1_SHA384, 48);
CHECK_CKR(ret, "RSA PKCS#1 PSS - SHA384");
Expand Down Expand Up @@ -4516,6 +4530,7 @@ static CK_RV find_ecc_priv_key(CK_SESSION_HANDLE session,
int idLen)
{
CK_RV ret = CKR_OK;

CK_ATTRIBUTE privKeyTmpl[] = {
{ CKA_CLASS, &privKeyClass, sizeof(privKeyClass) },
{ CKA_KEY_TYPE, &eccKeyType, sizeof(eccKeyType) },
Expand Down Expand Up @@ -4818,10 +4833,15 @@ static CK_RV ecdsa_test(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE privKey,
ret = funcList->C_VerifyInit(session, &mech, pubKey);
CHECK_CKR(ret, "ECDSA Verify Init");
}
#ifndef WOLFSSL_MAXQ10XX_CRYPTO
/* In the case of MAXQ1065 it will be signed by the pre-provisioned private
* key so verify operation will fail as this is NOT the corresponding
* public key. */
if (ret == CKR_OK) {
ret = funcList->C_Verify(session, hash, hashSz, out, outSz);
CHECK_CKR(ret, "ECDSA Verify");
}
#endif
if (ret == CKR_OK) {
ret = funcList->C_Verify(session, hash, hashSz - 1, out, outSz);
CHECK_CKR_FAIL(ret, CKR_SIGNATURE_INVALID, "ECDSA Verify bad hash");
Expand Down
Loading

0 comments on commit 88ae71f

Please sign in to comment.