Repository navigation
fix(sdk): GAT-2345 Inconsistent key is used for TDF wrapping in base key usage scenario - #444
Conversation
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
There was a problem hiding this comment.
I don't think that this goes far enough. The main issue is that there are two ways to reference a key.
- by reference:
grantsrefer to keys by KAS and KeyType. This means that:- We don't have the key material when we resolve an attribute so we need to retrieve it
- We should resolve these keys periodically because they can change
- by value: for base keys and mapped keys we get the key material in its entirety. It does not make sense to try to resolve the key once it has been retrieved
- We have no API to do so: we can retrieve from a particular KAS by algorithm but there may be another key with the same algorithm
- A KID should be associated with one cryptographic key. it's not a reference to anything. It's an immutable object so retrieving it again should yield the same object
The problem now is that we retrieve both classes of key. This is incorrect since it's impossible to retrieve a key that is provided by value. Only references should be retrieved.
The current pull fixes the issue with the base key but the same thing can happen with mapped keys. We should at the very least write up an issue describing that problem but I'd prefer to fix it now.
Thanks for catching this. I initilly scoped the fix to the base key because mapped keys resolve correctly today, but only because Autoconfigure puts them into KASKeyCache before planning. I agree that correctness should not depend on that. The cache has a 5-minute TTL, a public clear(), and an unsynchronized shared map, and a custom KAS implementation might not use it at all. Any miss falls back to an algorithm-only PublicKey call which would led to the same issue with the wrong-key. I'll update this PR to pass keys through the plan by value:
As a follow-up, it would be great to deprecate KASKeyCache / getKeyCache() and remove the code that depends on it. Both are public API, so they'd be removed in a later major version. |
Ah, that's interesting. I guess if we seed the cache then they do resolve correctly. So maybe we just need to comment that? It's a bit confusing I guess but making this minimal change along with some tests to catch regressions might be enough. |
…ough a new Plan object instead of caching baseKey. Also added more regression tests.
|
mkleene
left a comment
There was a problem hiding this comment.
I think that this fixes the immediate problem and when we remove grants we can come back and get rid of the cache and any retrieval of keys over the network which will simplify things



Summary
Fixes a bug where autoconfigure could wrap a TDF with the wrong key when it fell back to the platform's base key:
https://virtru.atlassian.net/jira/browse/GAT-2345
When the attributes have no mapped keys or grants (or no attributes are given),
Granter.getSplitsfalls back to the base key from the well-known configuration. It returns a single split with the base key's KAS URL, KID and algorithm.Planner.resolveKeysthen looked up that split's public key withservices.kas().getPublicKey(...). On a cache miss, KAS is asked by algorithm only, so it returns its default key for that algorithm, which might not be the base key. For example, KAS returnedswe-rsa-2048while the base key wasr2. The TDF was then wrapped with a key that didn't match the KID the planner meant to use.Changes in
Planner:getAutoconfigurePlannow returns a privatePlanobject: the splits plus the base key the granter fetched, ornullif it never asked for one. The granter calls the base-key supplier only when there are no mapped keys or grants, so the well-known endpoint is still called only when needed.Plannerhas no mutable state.resolveKeystakes the base key as a@Nullableparameter. When a split needs a public key and matches the base key, itsKASInfois built withConfig.KASInfo.fromSimpleKasKey(baseKey)and KAS is not called. This keeps the base key's URL, KID, PEM and algorithm.isBaseKeySplitas a defensive check. A split counts as the base key only if its KAS URL equals the base key's KAS URI and its KID equals the base key's KID. Today the granter returns only the base-key split when it uses the base key, but the check makes sure the base key's PEM is never applied to any other split.Scope
planFromAttributes): unchanged. They still resolve throughgetPublicKeyand come back correct becauseAutoconfigureseedsKASKeyCachewith the full key before planning, so the URL + algorithm + KID lookup hits the cache.planUsingGrants): unchanged. Grants reference keys by KAS URL and algorithm, so fetching from KAS is correct, and the cache saves repeated calls.kasInfoList/ explicit split plan: unchanged. No base key is involved, andresolveKeysgetsnull.Follow-up: mapped keys rely on the cache having been seeded. A cache miss (TTL expiry,
clear(), or a customSDK.KAS) would fall back to an algorithm-only lookup. Will be tracked in later JIRA tickets / PRs.Compatibility
PlanandisBaseKeySplitare private. ThePlannerconstructor andgetSplits()are unchanged.resolveKeysis package-private and gained a@Nullable SimpleKasKey baseKeyparameter.tdfConfig.wrappingKeyType.fetchBaseKeyreturns empty,resolveKeysgetsnull, and behavior is the same as before.Testing
Updated
PlannerTest:Replaced
autoconfigureUsesFetchedBaseKeyForUnmappedAttribute, which asserted the old behavior (KAS called for the base-key split), withautoconfigureWrapsWithDiscoveredBaseKeyWithoutCallingKas. It's parameterized overrsa:2048,rsa:4096,ec:secp256r1,ec:secp384r1andec:secp521r1:wrappingKeyTypeisRSA2048Key, so losing the base key's algorithm would show up.KeyType.fromString, that KAS is never called, and that the well-known config is read.Added
resolveKeystests with a non-null base key:resolveKeysUsesBaseKeyForMatchingSplit: a matching split resolves to the base key without calling KAS.resolveKeysUsesKasForNonBaseKeySplit: these splits still go throughgetPublicKey: same KAS with a different KID, same KAS with no KID, and a different KAS with the base key's KID.resolveKeysUsesBaseKeyWhenSeededEntryHasDifferentKid: akasInfoListentry for the same KAS with a different KID doesn't override the base key.Updated
testFillingInKeysWithAutoConfigureto passnullfor the new parameter.mvn -pl sdk test -Dtest=PlannerTest