GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add support for recipient for KMS Decrypt by mboorstin-circle · Pull Request #13343 · localstack/localstack

This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Conversation

@mboorstin-circle

@mboorstin-circle mboorstin-circle commented Nov 6, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Motivation

Add support for the KMS Decrypt Recipient field. This is used by Nitro Enclaves to decrypt sensitive without their host being able to read it, by KMS re-encrypting the decrypted data to the provided RSA key before it is returned to the caller. This flow is described here.

Changes

I've added basic support to the KMS Decrypt command for the Recipient field by looking for the attestation, parsing it to extract the recipient public key, and returning CiphertextForRecipient instead of Plaintext. It's not clear to me how to simulate verifying the attestation (which involves a signature from AWS that the PCR values are correct) without a significant effort to add proper support for Nitro to Localstack, and I think this PR as-is provides valuable functionality by allowing systems that use Nitro and KMS to be tested against Localstack without having to special-case their calls to Localstack's KMS implementation. So, I don't attempt to do any validation on the provided attestation document beyond what is required to extract the recipient public key.

I've unfortunately had to do a significant amount of manual crypto operations here with asn1crypto and PyCryptoDome because cryptography.hazmat doesn't appear to support the exact algorithms that AWS uses; I apologize for the verbosity and am happy to change it to cryptography.hazmat if there is a way to use it.

I've taken a compromise approach to ensuring that this change is backwards-compatible; if callers pass in a nonsense value to Recipient it will be logged and ignored and Plaintext will be returned, but if an attestation with a valid public key is passed in then CiphertextForRecipient instead of Plaintext will be returned. It is more to spec to error if a nonsense value is passed in for Recipient but that feels a bit more breaking. I'm happy to change to erroring if the maintainers would prefer that now, or it could be done as part of the next major release or similar.

Tests

I've added basic tests for the functionality. I've had to mark them as only_localstack because it is quite complex to call AWS to set up an actual Nitro Enclave, and until Localstack has broader Nitro support I think this will suffice to test. They do test that the flow works end to end. In addition, I have manually tested that this implementation produces a PKCS7 envelope similar to what actual AWS produces for an actual Nitro Enclave, and I have manually tested that my application that works on actual Nitro and KMS works against this PR.

Related

Addresses #12901.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

sannya-singal and k-a-il reacted with rocket emoji sannya-singal and k-a-il reacted with eyes emoji
All reactions
  • 🚀 2 reactions
  • 👀 2 reactions
@mboorstin-circle
Add support for recipient for KMS Decrypt
05d6765
@mboorstin-circle
mboorstin-circle requested review from alexrashed, bentsku, k-a-il, sannya-singal and silv-io as code owners November 6, 2025 03:48
@localstack-bot

localstack-bot commented Nov 6, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

localstack-bot
localstack-bot reviewed Nov 6, 2025
View reviewed changes

@localstack-bot localstack-bot left a comment

Copy link
Copy Markdown
Contributor

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.

Welcome to LocalStack! Thanks for raising your first Pull Request and landing in your contributions. Our team will reach out with any reviews or feedbacks that we have shortly. We recommend joining our Slack Community and share your PR on the #community channel to share your contributions with us. Please make sure you are following our contributing guidelines and our Code of Conduct.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mboorstin-circle

mboorstin-circle commented Nov 6, 2025

Copy link
Copy Markdown
Contributor Author

(I know that I need to sign the CLA; I just need to consult with my employer's legal department before doing so).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mboorstin-circle

mboorstin-circle commented Nov 6, 2025

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mboorstin-circle

mboorstin-circle commented Nov 6, 2025

Copy link
Copy Markdown
Contributor Author

recheck

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

localstack-bot added a commit that referenced this pull request Nov 6, 2025
@localstack-bot
@mboorstin-circle has signed the CLA in #13343
947390b
@mboorstin-circle

mboorstin-circle commented Nov 6, 2025

Copy link
Copy Markdown
Contributor Author

recheck

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mboorstin-circle

mboorstin-circle commented Nov 6, 2025

Copy link
Copy Markdown
Contributor Author

@sannya-singal @alexrashed @bentsku (tagging some of the automatically selected reviewers) I believe this should be ready for review. Thank you in advance!

sannya-singal reacted with thumbs up emoji sannya-singal reacted with eyes emoji
All reactions
  • 👍 1 reaction
  • 👀 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@alexrashed alexrashed assigned sannya-singal Nov 7, 2025
@alexrashed alexrashed added semver: minor Non-breaking changes which can be included in minor releases, but not in patch releases docs: skip Pull request does not require documentation changes notes: needed Pull request should be mentioned in the release notes labels Nov 7, 2025
@alexrashed alexrashed added this to the 4.11 milestone Nov 7, 2025
sannya-singal
sannya-singal suggested changes Nov 10, 2025
View reviewed changes

@sannya-singal sannya-singal left a comment

Copy link
Copy Markdown
Contributor

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.

Awesome! Thanks for adding this functionality 🚀 The implementation looks good and the approach of gracefully handling invalid attestations is really great. I just have a few suggestions to improve it, please let me know if you require some help or have questions.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread localstack-core/localstack/services/kms/provider.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread localstack-core/localstack/services/kms/provider.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread localstack-core/localstack/utils/crypto.py Outdated
Comment on lines +9 to +11
from Crypto.Cipher import AES, PKCS1_OAEP
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA as CryptoRSA

@sannya-singal sannya-singal Nov 10, 2025

Copy link
Copy Markdown
Contributor

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.

In my opinion we could eliminate the use of Crypto here and use dependencies from cryptography.hazmat.primitives.

So something like:

encrypted_session_key = recipient_pubkey.encrypt(
        session_key,
        padding.OAEP(
            mgf=padding.MGF1(algorithm=hashes.SHA256()),
            algorithm=hashes.SHA256(),
            label=None
        )
    )
cipher = Cipher(algorithms.AES(session_key), modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()

padder = sym_padding.PKCS7(algorithms.AES.block_size).padder()
padded_plaintext = padder.update(plaintext) + padder.finalize()
encrypted_content = encryptor.update(padded_plaintext) + encryptor.finalize()

Please let me know if you would like to know more context here.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

k-a-il reacted with rocket emoji
All reactions
  • 🚀 1 reaction

@mboorstin-circle mboorstin-circle Nov 10, 2025

Copy link
Copy Markdown
Contributor Author

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.

Yes, thank you! This all makes sense. I went down this path because Hazmat's one-shot PKCS7 only supports RSA-PKCS1.5 rather than the RSA-OAEP that AWS provides, but yes, I can still do the individual encryption operations with Hazmat and then just use the asn1crypto library to build them into a PKCS7 envelope. So I've updated things to follow your example and remove the PyCryptoDome usage here and in the test file. Let me know if things now look better. Thank you!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

sannya-singal reacted with thumbs up emoji sannya-singal reacted with rocket emoji
All reactions
  • 👍 1 reaction
  • 🚀 1 reaction
Comment thread localstack-core/localstack/utils/crypto.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread localstack-core/localstack/utils/crypto.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread localstack-core/localstack/utils/crypto.py Outdated
return decrypted


def pkcs7_envelope_encrypt(plaintext: bytes, recipient_pubkey) -> bytes:

@sannya-singal sannya-singal Nov 10, 2025

Copy link
Copy Markdown
Contributor

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.

Parameter recipient_pubkey lacks type hint here, can you please add it?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mboorstin-circle mboorstin-circle Nov 10, 2025

Copy link
Copy Markdown
Contributor Author

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.

Yes, added.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/aws/services/kms/test_kms.py Outdated
Comment on lines +14 to +17
from Crypto.Cipher import AES, PKCS1_OAEP
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA as CryptoRSA
from Crypto.Util.Padding import unpad as crypto_unpad

@sannya-singal sannya-singal Nov 10, 2025

Copy link
Copy Markdown
Contributor

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.

Same as suggested in the crypto.py most of this can be done with cryptography library.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mboorstin-circle mboorstin-circle Nov 10, 2025

Copy link
Copy Markdown
Contributor Author

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.

Yes, thank you! I've updated it in the same way of still using as1ncrypto to deserialize the envelope, but then using the normal Hazmat methods to do the actual decryption. Thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

sannya-singal reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread tests/aws/services/kms/test_kms.py Outdated
assert base64.b64decode(plaintext) == message

@markers.aws.only_localstack
def test_decrypt_recipient_invalid_attestation(self, kms_create_key, snapshot, aws_client):

@sannya-singal sannya-singal Nov 10, 2025

Copy link
Copy Markdown
Contributor

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.

We can safely remove snapshot fixture as its not being used in this test.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mboorstin-circle mboorstin-circle Nov 10, 2025

Copy link
Copy Markdown
Contributor Author

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/aws/services/kms/test_kms.py Outdated
snapshot.match("decrypt_response_with_invalid_ciphertext", e.value.response)

@markers.aws.only_localstack
def test_decrypt_recipient(self, kms_create_key, snapshot, aws_client):

@sannya-singal sannya-singal Nov 10, 2025

Copy link
Copy Markdown
Contributor

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.

We can safely remove snapshot fixture as its not being used in this test.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mboorstin-circle mboorstin-circle Nov 10, 2025

Copy link
Copy Markdown
Contributor Author

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mboorstin-circle
Update for code review comments
6d5101d
@mboorstin-circle

mboorstin-circle commented Nov 10, 2025

Copy link
Copy Markdown
Contributor Author

Thanks @sannya-singal ! All of your comments make sense and I have updated the diff appropriately. As you requested, I changed back to using cryptography.hazmat for the individual encryption / decryption operations (I think I still need asn1crypto to do the envelope serialization / deserialization, as like I said Hazmat's one-stop PKCS7 doesn't support RSA-OAEP). Let me know if things now look better. Thanks!

sannya-singal reacted with thumbs up emoji sannya-singal and k-a-il reacted with rocket emoji
All reactions
  • 👍 1 reaction
  • 🚀 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mboorstin-circle
mboorstin-circle requested a review from sannya-singal November 10, 2025 16:52
sannya-singal
sannya-singal approved these changes Nov 11, 2025
View reviewed changes

@sannya-singal sannya-singal left a comment

Copy link
Copy Markdown
Contributor

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.

Thanks @mboorstin-circle for addressing all the comments so quickly 🚀 🎉 Changes LGTM, tagging @k-a-il for a quick sanity check on the dependency file changes.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

k-a-il reacted with rocket emoji
All reactions
  • 🚀 1 reaction
@mboorstin-circle

mboorstin-circle commented Nov 11, 2025

Copy link
Copy Markdown
Contributor Author

Sounds great. Thanks @sannya-singal and (in advance) @k-a-il !

sannya-singal reacted with rocket emoji
All reactions
  • 🚀 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

k-a-il
k-a-il approved these changes Nov 13, 2025
View reviewed changes

@k-a-il k-a-il left a comment

Copy link
Copy Markdown
Collaborator

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.

The PR looks great, thank you for adding this new functionality to KMS @mboorstin-circle 🚀

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mboorstin-circle

mboorstin-circle commented Nov 13, 2025

Copy link
Copy Markdown
Contributor Author

Great, thanks @k-a-il ! I do not have merge permissions on this repository, so can you or @sannya-singal please merge this PR for me? Thank you!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@k-a-il
k-a-il merged commit 3deb89b into localstack:main Nov 14, 2025
43 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@k-a-il

k-a-il commented Nov 14, 2025

Copy link
Copy Markdown
Collaborator

Great, thanks @k-a-il ! I do not have merge permissions on this repository, so can you or @sannya-singal please merge this PR for me? Thank you!

All merged! Thanks once more for your contribution 🎉

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@alexrashed alexrashed Awaiting requested review from alexrashed alexrashed is a code owner
@silv-io silv-io Awaiting requested review from silv-io
@bentsku bentsku Awaiting requested review from bentsku
3 more reviewers
@localstack-bot localstack-bot localstack-bot left review comments
@sannya-singal sannya-singal sannya-singal approved these changes
@k-a-il k-a-il k-a-il approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@sannya-singal sannya-singal

Labels

docs: skip Pull request does not require documentation changes notes: needed Pull request should be mentioned in the release notes semver: minor Non-breaking changes which can be included in minor releases, but not in patch releases

Projects

None yet

Milestone

4.11

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@mboorstin-circle @localstack-bot @k-a-il @sannya-singal @alexrashed
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.