Skip to content
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

feat(KSA): KMS Decrypt/Encrypt Strategy #1020

Merged
merged 10 commits into from
Nov 25, 2024

Conversation

texastony
Copy link
Contributor

Issue #, if available:

Description of changes:
Closes #1013

Squash/merge commit message, if applicable:

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Copy link

Detected changes to the release files or to the check-files action

Copy link

Changes to the release files or the check-files action requires 2 approvals from CODEOWNERS

@texastony texastony changed the title Mutations/decrypt encrypt strat feat(KSA): KMS Decrypt/Encrypt Strategy Nov 18, 2024
Copy link

Detected changes to the release files or to the check-files action

Copy link

Changes to the release files or the check-files action requires 2 approvals from CODEOWNERS

Copy link

Detected changes to the release files or to the check-files action

Copy link

Changes to the release files or the check-files action requires 2 approvals from CODEOWNERS

Copy link

Detected changes to the release files or to the check-files action

Copy link

Changes to the release files or the check-files action requires 2 approvals from CODEOWNERS

@texastony texastony force-pushed the mutations/mutations branch 3 times, most recently from d972522 to 8dc3711 Compare November 20, 2024 20:40
@josecorella josecorella force-pushed the mutations/decrypt-encrypt-strat branch 2 times, most recently from 36fbc86 to bd60751 Compare November 20, 2024 20:56
Copy link

Detected changes to the release files or to the check-files action

Copy link

Changes to the release files or the check-files action requires 2 approvals from CODEOWNERS

@texastony texastony force-pushed the mutations/decrypt-encrypt-strat branch from cddc657 to a1800b5 Compare November 24, 2024 16:22
Copy link

Detected changes to the release files or to the check-files action

Copy link

Changes to the release files or the check-files action requires 2 approvals from CODEOWNERS

Copy link
Contributor Author

@texastony texastony left a comment

Choose a reason for hiding this comment

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

Oh no... I cannot approve this PR...
But I would if I could.

Comment on lines +294 to +301
method VerifyViaDecryptEncryptKey(
ciphertext: seq<uint8>,
sourceEncryptionContext: Structure.BranchKeyContext,
destinationEncryptionContext: Structure.BranchKeyContext,
kmsConfiguration: Types.KMSConfiguration,
decryptGrantTokens: KMS.GrantTokenList,
decryptKmsClient: KMS.IKMSClient
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggestion: A little bit of source comments may help those without context.
These will also help us write the Spec.

Suggested change
method VerifyViaDecryptEncryptKey(
ciphertext: seq<uint8>,
sourceEncryptionContext: Structure.BranchKeyContext,
destinationEncryptionContext: Structure.BranchKeyContext,
kmsConfiguration: Types.KMSConfiguration,
decryptGrantTokens: KMS.GrantTokenList,
decryptKmsClient: KMS.IKMSClient
)
/* Decrypt is assumed to be the "source", or "original", client.
The Decrypt KMS Client is used for both a Decrypt and Encrypt call.
*/
method VerifyViaDecryptEncryptKey(
ciphertext: seq<uint8>,
sourceEncryptionContext: Structure.BranchKeyContext,
destinationEncryptionContext: Structure.BranchKeyContext,
kmsConfiguration: Types.KMSConfiguration,
decryptGrantTokens: KMS.GrantTokenList,
decryptKmsClient: KMS.IKMSClient
)

message := "Invalid response from AWS KMS Encrypt :: Invalid KMS Key Id"
));

return Success(encryptResponse);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

TODO: Based on the error cases I have seen,
we will need to map these errors, in Mutations,
we will need to map these errors to the following failures:

  • Opaque KMS that matches Decrypt or Encrypt => MutationFromException
  • KMS InvalidCiphertextException => We need to create a new exception for broken branch keys...
  • All other exceptions => KeyStoreAdminException for un-expected errors

The ReEncrypt strategy probably also needs these error refinements.
I focused my error refinements on the critical case of KMS Access Denied.
But InvalidCiphertext is a separate issue.

requires KmsArn.ValidKmsArn?(sourceKmsArn) && KmsArn.ValidKmsArn?(destinationKmsArn)
requires decryptKmsClient.Modifies !! encryptKmsClient.Modifies
requires decryptKmsClient.ValidState() && encryptKmsClient.ValidState()
modifies decryptKmsClient.Modifies + encryptKmsClient.Modifies
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggestion: I have a thesis, that is probably wrong,
but that using , instead of concatenation (+) creates a multiset instead of a union of two sets.

I need to look into more thesis more,
but , has resolved some of my problems.

Suggested change
modifies decryptKmsClient.Modifies + encryptKmsClient.Modifies
modifies decryptKmsClient.Modifies, encryptKmsClient.Modifies

&& decryptResponse.KeyId.Some?
&& decryptResponse.KeyId.value == sourceKmsArn,
Types.KeyManagementException(
message := "Invalid response from AWS KMS Decrypt :: Invalid KMS Key Id"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nit/non-blocking: this error message is not quite accurate,
since the plaintext could be invalid.

I am not sure this matters at all.

Comment on lines +726 to +731
function ValidateCommitmentAndIndexStructures(
input: Types.ApplyMutationInput,
fetchMutation: KeyStoreTypes.GetMutationOutput,
commitment: KeyStoreTypes.MutationCommitment,
index: KeyStoreTypes.MutationIndex
): (output: Result<(), Types.Error>)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: I also refactored this method.
I removed the fetch type entirely,
along with some other changes to facilitate System Key verification.

requires forall item <- items :: item.item.Type.HierarchicalSymmetricVersion?
requires forall item <- items :: KmsArn.ValidKmsArn?(item.item.KmsArn)
requires forall item <- items :: Structure.EncryptedHierarchicalKey?(item.item)
requires forall item <- items :: item.itemOriginal? ==> item.item.KmsArn == mutationToApply.Original.kmsArn
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: it really fascinates me that we need to prove the original condition's KMS ARN,
but not the terminal condition's,
nor do we need to make any statements on the Encryption Context...

Comment on lines +97 to +99
// We have initialized the mutation. Instead of continuing with the Decrypt/Encrypt Strategy we will
// go to the ReEncrypt strategy bc as of today (11-20-2023) Decrypt/Encrypt Strategy is not supported for
// Apply Mutation.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Issue: This is no longer true, correct? This is the only comment so far that MUST be addressed.

Resolved by test below.
Still, we should remove this comment.

Comment on lines +254 to +256
// We have initialized the mutation. Instead of continuing with the Decrypt/Encrypt Strategy we will
// go to the ReEncrypt strategy bc as of today (11-20-2023) Decrypt/Encrypt Strategy is not supported for
// Apply Mutation.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh good. I know this is no longer true because this test literally proves this.

// -- All items have been mutated


module {:options "/functionSyntax:4" } TestDecryptEncryptStrat {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

TODO: Between @texastony and @josecorella , we should do a major refactor of the Mutation testing.

There is a ton of duplicate code that could be consolidated to:

  • Start Mutation
  • Verify Start
  • Work Mutation
  • Verify Work
  • Complete Mutation
  • Verify Complete
  • Clean Up everything

Or something along those lines.

Copy link

@texastony and @texastony, I noticed you are updating the smithy model files.
Does this update need new or updated user documentation?
Are you adding constraints inside list, map or union? Do you know about this issue: smithy-lang/smithy-dafny#491?

@josecorella josecorella marked this pull request as ready for review November 25, 2024 22:01
@josecorella josecorella requested a review from a team as a code owner November 25, 2024 22:01
@josecorella josecorella merged commit f882c63 into mutations/mutations Nov 25, 2024
111 of 116 checks passed
texastony added a commit that referenced this pull request Nov 26, 2024
texastony added a commit that referenced this pull request Dec 2, 2024
texastony added a commit that referenced this pull request Dec 4, 2024
texastony added a commit that referenced this pull request Dec 9, 2024
texastony added a commit that referenced this pull request Dec 11, 2024
texastony added a commit that referenced this pull request Dec 17, 2024
texastony added a commit that referenced this pull request Jan 22, 2025
texastony added a commit that referenced this pull request Jan 22, 2025
texastony added a commit that referenced this pull request Mar 14, 2025
josecorella added a commit that referenced this pull request Mar 31, 2025
josecorella added a commit that referenced this pull request Mar 31, 2025
josecorella added a commit that referenced this pull request Mar 31, 2025
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