-
Notifications
You must be signed in to change notification settings - Fork 969
PKCS #11: implement identifying keys by label #3416
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
63c3258
PKCS #11: implement identifying keys by label
SparkiDev 10397c0
PKCS #11 SSL: detect key size when certificate set
SparkiDev a49d901
PKCS#11: Label fixes and add support for checking private key
SparkiDev 9ddc799
Fix dynamic type name
SparkiDev deda3e5
PKCS #11: only open/close session when performing op, use C_Sign for RSA
SparkiDev 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1649,6 +1649,10 @@ int InitSSL_Ctx(WOLFSSL_CTX* ctx, WOLFSSL_METHOD* method, void* heap) | |
| return BAD_MUTEX_E; | ||
| } | ||
|
|
||
| #ifndef NO_CERTS | ||
| ctx->privateKeyDevId = INVALID_DEVID; | ||
| #endif | ||
|
|
||
| #ifndef NO_DH | ||
| ctx->minDhKeySz = MIN_DHKEY_SZ; | ||
| ctx->maxDhKeySz = MAX_DHKEY_SZ; | ||
|
|
@@ -5343,6 +5347,7 @@ int SetSSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup) | |
| ssl->buffers.key = ctx->privateKey; | ||
| ssl->buffers.keyType = ctx->privateKeyType; | ||
| ssl->buffers.keyId = ctx->privateKeyId; | ||
| ssl->buffers.keyLabel = ctx->privateKeyLabel; | ||
| ssl->buffers.keySz = ctx->privateKeySz; | ||
| ssl->buffers.keyDevId = ctx->privateKeyDevId; | ||
| #endif | ||
|
|
@@ -19880,6 +19885,76 @@ int PickHashSigAlgo(WOLFSSL* ssl, const byte* hashSigAlgo, word32 hashSigAlgoSz) | |
|
|
||
| #if !defined(NO_CERTS) | ||
|
|
||
| #ifdef WOLF_CRYPTO_CB | ||
| /* Create a private key for a device. | ||
| * | ||
| * pkey Key object. | ||
| * data Data to identify key. | ||
| * length Length of data. | ||
| * hsType Type of the key to create. | ||
| * heap Custom heap to use for mallocs/frees | ||
| * devId Id for device. | ||
| * return 0 on success. | ||
| * return NOT_COMPILED_IN if algorithm type not supported. | ||
| * return MEMORY_E on memory allocation failure. | ||
| * return other internal error | ||
| */ | ||
| int CreateDevPrivateKey(void** pkey, byte* data, word32 length, int hsType, | ||
| int label, int id, void* heap, int devId) | ||
| { | ||
| int ret = NOT_COMPILED_IN; | ||
|
|
||
| if (hsType == DYNAMIC_TYPE_RSA) { | ||
| #ifndef NO_RSA | ||
| RsaKey* rsaKey; | ||
|
|
||
| rsaKey = XMALLOC(sizeof(RsaKey), heap, DYNAMIC_TYPE_RSA); | ||
| if (rsaKey == NULL) { | ||
| return MEMORY_E; | ||
| } | ||
|
|
||
| if (label) { | ||
| ret = wc_InitRsaKey_Label(rsaKey, (char*)data, heap, devId); | ||
| } | ||
| else if (id) { | ||
| ret = wc_InitRsaKey_Id(rsaKey, data, length, heap, devId); | ||
| } | ||
| if (ret == 0) { | ||
| *pkey = (void*)rsaKey; | ||
| } | ||
| else { | ||
| XFREE(rsaKey, heap, DYNAMIC_TYPE_RSA); | ||
| } | ||
| #endif | ||
| } | ||
| else if (hsType == DYNAMIC_TYPE_ECC) { | ||
| #ifdef HAVE_ECC | ||
| ecc_key* ecKey; | ||
|
|
||
| ecKey = XMALLOC(sizeof(ecc_key), heap, DYNAMIC_TYPE_ECC); | ||
| if (ecKey == NULL) { | ||
| return MEMORY_E; | ||
| } | ||
|
|
||
| if (label) { | ||
| ret = wc_ecc_init_label(ecKey, (char*)data, heap, devId); | ||
| } | ||
| else if (id) { | ||
| ret = wc_ecc_init_id(ecKey, data, length, heap, devId); | ||
| } | ||
| if (ret == 0) { | ||
| *pkey = (void*)ecKey; | ||
| } | ||
| else { | ||
| XFREE(ecKey, heap, DYNAMIC_TYPE_ECC); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DYNAMIC_TYPE_ECC?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. Thanks! |
||
| } | ||
| #endif | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
| #endif | ||
|
|
||
| /* Decode the private key - RSA/ECC/Ed25519/Ed448 - and creates a key object. | ||
| * The signature type is set as well. | ||
| * The maximum length of a signature is returned. | ||
|
|
@@ -19910,7 +19985,8 @@ int DecodePrivateKey(WOLFSSL *ssl, word16* length) | |
| } | ||
|
|
||
| #ifdef HAVE_PKCS11 | ||
| if (ssl->buffers.keyDevId != INVALID_DEVID && ssl->buffers.keyId) { | ||
| if (ssl->buffers.keyDevId != INVALID_DEVID && (ssl->buffers.keyId || | ||
| ssl->buffers.keyLabel)) { | ||
| if (ssl->buffers.keyType == rsa_sa_algo) | ||
| ssl->hsType = DYNAMIC_TYPE_RSA; | ||
| else if (ssl->buffers.keyType == ecc_dsa_sa_algo) | ||
|
|
@@ -19922,9 +19998,17 @@ int DecodePrivateKey(WOLFSSL *ssl, word16* length) | |
|
|
||
| if (ssl->buffers.keyType == rsa_sa_algo) { | ||
| #ifndef NO_RSA | ||
| ret = wc_InitRsaKey_Id((RsaKey*)ssl->hsKey, | ||
| ssl->buffers.key->buffer, ssl->buffers.key->length, | ||
| ssl->heap, ssl->buffers.keyDevId); | ||
| if (ssl->buffers.keyLabel) { | ||
| ret = wc_InitRsaKey_Label((RsaKey*)ssl->hsKey, | ||
| (char*)ssl->buffers.key->buffer, | ||
| ssl->heap, ssl->buffers.keyDevId); | ||
| } | ||
| else if (ssl->buffers.keyId) { | ||
| ret = wc_InitRsaKey_Id((RsaKey*)ssl->hsKey, | ||
| ssl->buffers.key->buffer, | ||
| ssl->buffers.key->length, ssl->heap, | ||
| ssl->buffers.keyDevId); | ||
| } | ||
| if (ret == 0) { | ||
| if (ssl->buffers.keySz < ssl->options.minRsaKeySz) { | ||
| WOLFSSL_MSG("RSA key size too small"); | ||
|
|
@@ -19940,9 +20024,17 @@ int DecodePrivateKey(WOLFSSL *ssl, word16* length) | |
| } | ||
| else if (ssl->buffers.keyType == ecc_dsa_sa_algo) { | ||
| #ifdef HAVE_ECC | ||
| ret = wc_ecc_init_id((ecc_key*)ssl->hsKey, ssl->buffers.key->buffer, | ||
| ssl->buffers.key->length, ssl->heap, | ||
| ssl->buffers.keyDevId); | ||
| if (ssl->buffers.keyLabel) { | ||
| ret = wc_ecc_init_label((ecc_key*)ssl->hsKey, | ||
| (char*)ssl->buffers.key->buffer, | ||
| ssl->heap, ssl->buffers.keyDevId); | ||
| } | ||
| else if (ssl->buffers.keyId) { | ||
| ret = wc_ecc_init_id((ecc_key*)ssl->hsKey, | ||
| ssl->buffers.key->buffer, | ||
| ssl->buffers.key->length, ssl->heap, | ||
| ssl->buffers.keyDevId); | ||
| } | ||
| if (ret == 0) { | ||
| if (ssl->buffers.keySz < ssl->options.minEccKeySz) { | ||
| WOLFSSL_MSG("ECC key size too small"); | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be DYNAMIC_TYPE_RSA?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Thanks!