Skip to content

C_WrapKey: end the encrypt op after a length query - #220

Open
MarkAtwood wants to merge 1 commit into
wolfSSL:masterfrom
MarkAtwood:wrapkey-length-query
Open

MarkAtwood wants to merge 1 commit into
wolfSSL:masterfrom
MarkAtwood:wrapkey-length-query

Conversation

@MarkAtwood

Copy link
Copy Markdown
Contributor

C_WrapKey with a NULL output buffer is a length query. wolfPKCS11 returns the length but leaves the internal encrypt operation active, so the real call that follows fails with CKR_OPERATION_ACTIVE. Same after CKR_BUFFER_TOO_SMALL. That breaks pk12util -o through NSS.

This ends the encrypt operation after the internal C_Encrypt, so a length query or a short buffer leaves no operation active.

Test: wrapkey_length_query_test, checked against an OpenSSL known answer. Fails on master, passes with the fix.

C_WrapKey with an AES mechanism runs C_EncryptInit and C_Encrypt
internally. A length query (pWrappedKey NULL) or a buffer that is too
small leaves that encrypt operation active, so the caller's next
C_WrapKey fails with CKR_OPERATION_ACTIVE. NSS queries the length first
when exporting a key, so pk12util -o failed. C_WrapKey is single-part;
end the operation after the internal C_Encrypt.

Add tests/wrapkey_length_query_test.c, with an OpenSSL known answer.
Copilot AI balanced review requested due to automatic review settings October 2, 2026 20:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The new test needs a serialization-support guard to avoid failing the no-keystore CI configuration.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Fixes C_WrapKey retries after length queries or short buffers, supporting NSS PKCS #12 exports.

Changes:

  • Clears the internal encryption operation after wrapping.
  • Adds a known-answer regression test and registers it with Automake.
File Description
tests/​wrapkey_length_query_test.c Tests length-query and short-buffer retries.
tests/​include.am Registers and links the regression test.
src/​crypto.c Clears encryption state after internal C_Encrypt.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.


#include "testdata.h"

#if !defined(NO_AES) && !defined(NO_AES_CBC) && defined(WOLFSSL_AES_256)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants