-
Notifications
You must be signed in to change notification settings - Fork 26
feat(sdk): implement BaseTDF v4.4.0 algorithm registry and KAS hardening #3098
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
Draft
dmihalcik-virtru
wants to merge
1
commit into
main
Choose a base branch
from
basetdf/impl-v4.4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package ocrypto | ||
|
|
||
| // Algorithm identifiers for BaseTDF v4.4.0 Key Access Objects. | ||
| // These are the string values used in the "alg" field of KAOs. | ||
| const ( | ||
| AlgRSAOAEP = "RSA-OAEP" | ||
| AlgRSAOAEP256 = "RSA-OAEP-256" | ||
| AlgECDHHKDF = "ECDH-HKDF" | ||
| AlgMLKEM768 = "ML-KEM-768" | ||
| AlgMLKEM1024 = "ML-KEM-1024" | ||
| AlgHybridECDH = "X-ECDH-ML-KEM-768" | ||
| ) | ||
|
|
||
| // AlgForKeyType returns the BaseTDF algorithm identifier for the given KeyType. | ||
| // This maps internal key types to the explicit algorithm strings used in v4.4.0 KAOs. | ||
| func AlgForKeyType(kt KeyType) string { | ||
| switch kt { | ||
| case RSA2048Key, RSA4096Key: | ||
| return AlgRSAOAEP | ||
| case EC256Key, EC384Key, EC521Key: | ||
| return AlgECDHHKDF | ||
| default: | ||
| return string(kt) | ||
| } | ||
| } | ||
|
|
||
| // KeyTypeForAlg returns the KeyType for a given BaseTDF algorithm identifier. | ||
| // Returns the algorithm string as a KeyType if no specific mapping exists. | ||
| func KeyTypeForAlg(alg string) KeyType { | ||
| switch alg { | ||
| case AlgRSAOAEP, AlgRSAOAEP256: | ||
| return RSA2048Key | ||
| case AlgECDHHKDF: | ||
| return EC256Key | ||
| default: | ||
| return KeyType(alg) | ||
| } | ||
| } | ||
|
|
||
| // AlgForLegacyType maps the v4.3.0 "type" field values to algorithm identifiers. | ||
| func AlgForLegacyType(legacyType string) string { | ||
| switch legacyType { | ||
| case "wrapped": | ||
| return AlgRSAOAEP | ||
| case "ec-wrapped": | ||
| return AlgECDHHKDF | ||
| default: | ||
| return "" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package ocrypto | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestAlgForKeyType(t *testing.T) { | ||
| tests := []struct { | ||
| keyType KeyType | ||
| want string | ||
| }{ | ||
| {RSA2048Key, AlgRSAOAEP}, | ||
| {RSA4096Key, AlgRSAOAEP}, | ||
| {EC256Key, AlgECDHHKDF}, | ||
| {EC384Key, AlgECDHHKDF}, | ||
| {EC521Key, AlgECDHHKDF}, | ||
| {"", ""}, | ||
| {KeyType("unknown"), "unknown"}, | ||
| } | ||
| for _, tt := range tests { | ||
| if got := AlgForKeyType(tt.keyType); got != tt.want { | ||
| t.Errorf("AlgForKeyType(%q) = %q, want %q", tt.keyType, got, tt.want) | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+5
to
+23
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For better test readability and output, consider using table-driven subtests with Example: func TestAlgForKeyType(t *testing.T) {
tests := []struct {
name string
keyType KeyType
want string
}{
{"RSA2048Key", RSA2048Key, AlgRSAOAEP},
{"RSA4096Key", RSA4096Key, AlgRSAOAEP},
// ... other test cases
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := AlgForKeyType(tt.keyType); got != tt.want {
t.Errorf("AlgForKeyType(%q) = %q, want %q", tt.keyType, got, tt.want)
}
})
}
}This can be applied to the other test functions in this file as well. |
||
|
|
||
| func TestKeyTypeForAlg(t *testing.T) { | ||
| tests := []struct { | ||
| alg string | ||
| want KeyType | ||
| }{ | ||
| {AlgRSAOAEP, RSA2048Key}, | ||
| {AlgRSAOAEP256, RSA2048Key}, | ||
| {AlgECDHHKDF, EC256Key}, | ||
| {"", KeyType("")}, | ||
| {"unknown", KeyType("unknown")}, | ||
| } | ||
| for _, tt := range tests { | ||
| if got := KeyTypeForAlg(tt.alg); got != tt.want { | ||
| t.Errorf("KeyTypeForAlg(%q) = %q, want %q", tt.alg, got, tt.want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestAlgForLegacyType(t *testing.T) { | ||
| tests := []struct { | ||
| legacy string | ||
| want string | ||
| }{ | ||
| {"wrapped", AlgRSAOAEP}, | ||
| {"ec-wrapped", AlgECDHHKDF}, | ||
| {"", ""}, | ||
| {"unknown", ""}, | ||
| } | ||
| for _, tt := range tests { | ||
| if got := AlgForLegacyType(tt.legacy); got != tt.want { | ||
| t.Errorf("AlgForLegacyType(%q) = %q, want %q", tt.legacy, got, tt.want) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| package sdk | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestKeyAccessUnmarshalJSON_V43Compat(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| wantKeyType string | ||
| wantAlg string | ||
| }{ | ||
| { | ||
| name: "v4.3 wrapped infers RSA-OAEP", | ||
| input: `{"type":"wrapped","url":"https://kas.example.com","protocol":"kas","wrappedKey":"abc"}`, | ||
| wantKeyType: "wrapped", | ||
| wantAlg: "RSA-OAEP", | ||
| }, | ||
| { | ||
| name: "v4.3 ec-wrapped infers ECDH-HKDF", | ||
| input: `{"type":"ec-wrapped","url":"https://kas.example.com","protocol":"kas","wrappedKey":"abc"}`, | ||
| wantKeyType: "ec-wrapped", | ||
| wantAlg: "ECDH-HKDF", | ||
| }, | ||
| { | ||
| name: "v4.4 explicit alg preserved", | ||
| input: `{"type":"wrapped","alg":"RSA-OAEP-256","url":"https://kas.example.com","protocol":"kas","wrappedKey":"abc"}`, | ||
| wantKeyType: "wrapped", | ||
| wantAlg: "RSA-OAEP-256", | ||
| }, | ||
| { | ||
| name: "v4.4 ML-KEM-768 preserved", | ||
| input: `{"type":"","alg":"ML-KEM-768","url":"https://kas.example.com","wrappedKey":"abc"}`, | ||
| wantKeyType: "", | ||
| wantAlg: "ML-KEM-768", | ||
| }, | ||
| { | ||
| name: "unknown type does not infer alg", | ||
| input: `{"type":"remote","url":"https://kas.example.com","wrappedKey":"abc"}`, | ||
| wantKeyType: "remote", | ||
| wantAlg: "", | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| var ka KeyAccess | ||
| if err := json.Unmarshal([]byte(tt.input), &ka); err != nil { | ||
| t.Fatalf("UnmarshalJSON() error = %v", err) | ||
| } | ||
| if ka.KeyType != tt.wantKeyType { | ||
| t.Errorf("KeyType = %q, want %q", ka.KeyType, tt.wantKeyType) | ||
| } | ||
| if ka.Algorithm != tt.wantAlg { | ||
| t.Errorf("Algorithm = %q, want %q", ka.Algorithm, tt.wantAlg) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestKeyAccessMarshalJSON_V44Fields(t *testing.T) { | ||
| ka := KeyAccess{ | ||
| KeyType: "wrapped", | ||
| Algorithm: "RSA-OAEP", | ||
| KasURL: "https://kas.example.com", | ||
| Protocol: "kas", | ||
| WrappedKey: "abc", | ||
| PolicyBinding: PolicyBinding{Alg: "HS256", Hash: "def"}, | ||
| KID: "k1", | ||
| SplitID: "s1", | ||
| } | ||
|
|
||
| data, err := json.Marshal(ka) | ||
| if err != nil { | ||
| t.Fatalf("Marshal() error = %v", err) | ||
| } | ||
|
|
||
| var roundTrip KeyAccess | ||
| if err := json.Unmarshal(data, &roundTrip); err != nil { | ||
| t.Fatalf("Unmarshal() error = %v", err) | ||
| } | ||
| if roundTrip.Algorithm != "RSA-OAEP" { | ||
| t.Errorf("Algorithm = %q, want %q", roundTrip.Algorithm, "RSA-OAEP") | ||
| } | ||
| if roundTrip.KID != "k1" { | ||
| t.Errorf("KID = %q, want %q", roundTrip.KID, "k1") | ||
| } | ||
| if roundTrip.SplitID != "s1" { | ||
| t.Errorf("SplitID = %q, want %q", roundTrip.SplitID, "s1") | ||
| } | ||
| } | ||
|
|
||
| func TestKeyAccessMarshalJSON_OmitEmptyAlg(t *testing.T) { | ||
| ka := KeyAccess{ | ||
| KeyType: "wrapped", | ||
| KasURL: "https://kas.example.com", | ||
| Protocol: "kas", | ||
| WrappedKey: "abc", | ||
| PolicyBinding: "hash", | ||
| } | ||
|
|
||
| data, err := json.Marshal(ka) | ||
| if err != nil { | ||
| t.Fatalf("Marshal() error = %v", err) | ||
| } | ||
|
|
||
| // Verify "alg" is not present when empty | ||
| var raw map[string]any | ||
| if err := json.Unmarshal(data, &raw); err != nil { | ||
| t.Fatalf("Unmarshal() error = %v", err) | ||
| } | ||
| if _, ok := raw["alg"]; ok { | ||
| t.Error("expected alg field to be omitted when empty") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
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 mapping in
KeyTypeForAlgis lossy. For example,AlgForKeyTypemaps bothRSA2048KeyandRSA4096KeytoAlgRSAOAEP, but this function mapsAlgRSAOAEPback to onlyRSA2048Key. Similarly,EC256Key,EC384Key, andEC521Keyare all mapped toAlgECDHHKDF, which is then mapped back only toEC256Key.This loss of information about the key size could lead to unexpected behavior or bugs if the caller of this function relies on getting a precise key type.
If this lossy conversion is intentional and safe within the current design, it would be beneficial to add a comment explaining why it's acceptable. Otherwise, consider making the mapping more specific or returning an error for ambiguous cases.