Skip to content

pkcs11 aes decrypt - #665

Open
mlohvynenko wants to merge 10 commits into
aosedge:developfrom
mlohvynenko:feature_pkcs11_aes_decrypt
Open

mlohvynenko wants to merge 10 commits into
aosedge:developfrom
mlohvynenko:feature_pkcs11_aes_decrypt

Conversation

@mlohvynenko

Copy link
Copy Markdown
Member

No description provided.

Copilot AI lite review requested due to automatic review settings September 25, 2026 14:08

Copilot AI left a comment

Copy link
Copy Markdown

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

Unresolved critical and moderate issues affect plaintext safety, crash recovery, key handling, configuration, and resource accounting.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity · 3 Medium severity

Open (4)
What changed in this PR

Adds AES-256-GCM encrypted OCI layer decryption using PKCS#11-backed keys.

Changes:

  • Adds PKCS#11 AES-GCM key lookup and decryption.
  • Integrates staged blob decryption into ImageManager.
  • Adds tests, documentation, interfaces, and build updates.
File Summary Review notes
src/​core/​sm/​imagemanager/​tests/​mocks/​blobdecryptormock.hpp Blob decryptor mock —
src/​core/​sm/​imagemanager/​tests/​keyprovider.cpp Key provider tests —
src/​core/​sm/​imagemanager/​tests/​imagemanager.cpp Encrypted-layer integration tests —
src/​core/​sm/​imagemanager/​tests/​CMakeLists.txt Test configuration —
src/​core/​sm/​imagemanager/​tests/​blobdecryptor.cpp Decryption and PKCS#11 tests —
src/​core/​sm/​imagemanager/​README.md Encrypted-layer documentation Nit: document decrypt usage provisioning.
src/​core/​sm/​imagemanager/​keyprovider.hpp Key provider declaration —
src/​core/​sm/​imagemanager/​keyprovider.cpp Key loading and caching —
src/​core/​sm/​imagemanager/​itf/​keyprovider.hpp Key provider interface —
src/​core/​sm/​imagemanager/​itf/​blobdecryptor.hpp Blob decryptor interface —
src/​core/​sm/​imagemanager/​imagemanager.hpp ImageManager decryption integration —
src/​core/​sm/​imagemanager/​imagemanager.cpp Decryption workflow Moderate: account for decrypted staging space.
src/​core/​sm/​imagemanager/​CMakeLists.txt Module build updates —
src/​core/​sm/​imagemanager/​blobdecryptor.hpp Blob decryptor declaration —
src/​core/​sm/​imagemanager/​blobdecryptor.cpp File staging and decryption Critical: remove stale plaintext output on failure. Moderate: make staging crash-safe.
src/​core/​iam/​certhandler/​certmodule.cpp Certificate URL handling Moderate: use configured certificate storage instead of hard-coded values.
src/​core/​common/​tests/​mocks/​cryptomock.hpp Private-key mock —
src/​core/​common/​pkcs11/​privatekey.hpp AES PKCS#11 declarations —
src/​core/​common/​pkcs11/​privatekey.cpp AES-GCM PKCS#11 operations —
src/​core/​common/​pkcs11/​pkcs11.hpp Secret-key lookup documentation —
src/​core/​common/​pkcs11/​pkcs11.cpp Secret-key discovery and wrapping Moderate: validate 32-byte AES keys and handle symmetric-key removal correctly.
src/​core/​common/​ocispec/​itf/​imagespec.hpp Encrypted media type —
src/​core/​common/​crypto/​tests/​certloader.cpp Secret-key loading tests —
src/​core/​common/​crypto/​openssl/​cryptoprovider.cpp RSA GCM handling —
src/​core/​common/​crypto/​mbedtls/​cryptoprovider.cpp RSA GCM handling —
src/​core/​common/​crypto/​itf/​privkey.hpp GCM decryption options Nit: update public API documentation.
CMakeLists.txt Coverage configuration —

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

Comment on lines +108 to +112
auto cleanUp = DeferRelease(&stagedPath, [](const auto* path) { (void)fs::Remove(*path); });

fs::File output;

// owner-only: it briefly holds plaintext that is already authenticated by this point, but stays private
{}, AOS_ERROR_WRAP(Error(ErrorEnum::eInvalidArgument, "id/label matches more than one secret key"))};
}

return ExportPrivateKey(secretKeys[0], 0, keyTypeAES);
Comment thread src/core/iam/certhandler/certmodule.cpp
Comment on lines +102 to +108
StaticString<cFilePathLen> stagedPath;

if (auto err = stagedPath.Format("%s.gcmtmp", decryptedPath.CStr()); !err.IsNone()) {
return AOS_ERROR_WRAP(err);
}

auto cleanUp = DeferRelease(&stagedPath, [](const auto* path) { (void)fs::Remove(*path); });
Copilot AI review requested due to automatic review settings September 25, 2026 14:29
@mlohvynenko
mlohvynenko force-pushed the feature_pkcs11_aes_decrypt branch from 8cc5cf2 to 166d3a7 Compare September 25, 2026 14:29

Copilot AI left a comment

Copy link
Copy Markdown

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

Critical PKCS#11 compatibility and secret-handling issues, plus unresolved correctness and resource-management findings, block approval.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 3 High severity · 4 Medium severity · 1 Low severity

Open (8)

Comment on lines +214 to +217
mGCMParams.pIv = const_cast<uint8_t*>(options.mIV.Get());
mGCMParams.ulIvLen = static_cast<CK_ULONG>(options.mIV.Size());
mGCMParams.ulIvBits = static_cast<CK_ULONG>(options.mIV.Size() * 8);
mGCMParams.ulTagBits = crypto::AESCipherItf::cGCMTagSize * 8;
std::filesystem::remove_all(cTestDir);
std::filesystem::create_directories(cTestDir);

ASSERT_TRUE(fs::WriteStringToFile(mPINSource.c_str(), mPIN, 0664).IsNone());
Comment on lines +1180 to +1183
(void)secretKeyTempl.PushBack({CKA_CLASS, ConvertToAttributeValue(secretKeyClass)});
(void)secretKeyTempl.PushBack({CKA_KEY_TYPE, ConvertToAttributeValue(keyTypeAES)});
(void)secretKeyTempl.PushBack({CKA_ID, id});
(void)secretKeyTempl.PushBack({CKA_LABEL, ConvertToAttributeValue(label)});
```

Publish `layer.tar.gz.enc` as the layer blob with
`mediaType: application/vnd.aos.image.layer.enc.v1.aes256gcm+tar+gz`; its digest (SHA-256 of the `.enc`
Copilot AI review requested due to automatic review settings September 25, 2026 15:41
@mlohvynenko
mlohvynenko force-pushed the feature_pkcs11_aes_decrypt branch from 166d3a7 to f0ca135 Compare September 25, 2026 15:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot AI review requested due to automatic review settings September 25, 2026 17:08
@mlohvynenko
mlohvynenko force-pushed the feature_pkcs11_aes_decrypt branch from f0ca135 to 6802f85 Compare September 25, 2026 17:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings September 28, 2026 10:15
@mlohvynenko
mlohvynenko force-pushed the feature_pkcs11_aes_decrypt branch from 6802f85 to 34d1435 Compare September 28, 2026 10:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@mlohvynenko
mlohvynenko force-pushed the feature_pkcs11_aes_decrypt branch from 34d1435 to 015098e Compare September 28, 2026 10:20
Signed-off-by: Mykhailo Lohvynenko <mykhailo_lohvynenko@epam.com>
Signed-off-by: Mykhailo Lohvynenko <mykhailo_lohvynenko@epam.com>
To be removed once cloud side issue is resolved.

Signed-off-by: Mykhailo Lohvynenko <mykhailo_lohvynenko@epam.com>
Signed-off-by: Mykhailo Lohvynenko <mykhailo_lohvynenko@epam.com>
@mlohvynenko
mlohvynenko force-pushed the feature_pkcs11_aes_decrypt branch from 015098e to e94f7b2 Compare September 28, 2026 12:23
Signed-off-by: Mykhailo Lohvynenko <mykhailo_lohvynenko@epam.com>
Signed-off-by: Mykhailo Lohvynenko <mykhailo_lohvynenko@epam.com>
@mlohvynenko
mlohvynenko force-pushed the feature_pkcs11_aes_decrypt branch from e94f7b2 to dd5f94b Compare September 28, 2026 20:59
Copilot AI review requested due to automatic review settings September 28, 2026 20:59

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Signed-off-by: Mykhailo Lohvynenko <mykhailo_lohvynenko@epam.com>
Copilot AI lite review requested due to automatic review settings October 2, 2026 12:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment on lines +810 to +814
// C_DecryptUpdate, not at C_DecryptInit. Nothing has been consumed from chunkProvider
// except this one chunk, and nothing has been handed to chunkReceiver, so if it's the very
// first, the caller can safely retry via a fresh, single-shot decrypt instead.
if (!anyChunkDecrypted) {
return AOS_ERROR_WRAP(ErrorEnum::eNotSupported);
Comment thread src/core/sm/imagemanager/blobdecryptor.cpp
sized for the whole file regardless: empirically (against SoftHSM2) and per most PKCS11 modules, AEAD
decryption only releases data once the authentication tag has been verified, all at once, at
`C_DecryptFinal` — chunking only bounds the ciphertext side, not the plaintext side.
- `cDecryptChunkSize` (`AOS_CONFIG_IMAGEMANAGER_DECRYPT_CHUNK_SIZE`, default 16 KiB) is deliberately
Signed-off-by: Mykhailo Lohvynenko <mykhailo_lohvynenko@epam.com>
Copilot AI lite review requested due to automatic review settings October 2, 2026 12:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment on lines +25 to +27
* Decrypts a blob file using the device-local symmetric key. The encrypted file's first 12 bytes are
* read as the IV; the remainder is the ciphertext followed by the authentication tag. Fails, leaving no
* output, if the data doesn't authenticate (wrong key or modified blob).
Signed-off-by: Mykhailo Lohvynenko <mykhailo_lohvynenko@epam.com>
Copilot AI lite review requested due to automatic review settings October 2, 2026 12:47
Signed-off-by: Mykhailo Lohvynenko <mykhailo_lohvynenko@epam.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment on lines +295 to +306
// C_DecryptInit rejects the parameters before any data is consumed, so retrying is safe here.
if (mech.mechanism == CKM_AES_CTR && err.Errno() == static_cast<int32_t>(CKR_MECHANISM_PARAM_INVALID)) {
LOG_DBG() << "Token rejected CTR counter bits, retrying" << Log::Field("counterBits", cOPTEECTRCounterBits);

PKCS11AESMechConverter fallbackVisitor(cOPTEECTRCounterBits);

Tie(mech, err) = options.ApplyVisitor(fallbackVisitor);
if (!err.IsNone()) {
return AOS_ERROR_WRAP(err);
}

err = op(mech);
Comment on lines +321 to +325
if (auto err = stagedPath.Format("%s.gcmtmp", decryptedPath.CStr()); !err.IsNone()) {
return AOS_ERROR_WRAP(err);
}

auto cleanUp = DeferRelease(&stagedPath, [](const auto* path) { (void)fs::Remove(*path); });
Copilot AI lite review requested due to automatic review settings October 2, 2026 12:53
@sonarqubecloud

sonarqubecloud Bot commented Oct 2, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
74.4% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

* more (or needs less).
*/
#ifndef AOS_CONFIG_IMAGEMANAGER_DECRYPT_CHUNK_SIZE
#define AOS_CONFIG_IMAGEMANAGER_DECRYPT_CHUNK_SIZE 64 * 1024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants