Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
15e7968
feat(sdk): Enable base key support.
c-r33d Jun 10, 2025
1e3acd3
changes.
c-r33d Jun 10, 2025
3e8b84b
check,
c-r33d Jun 10, 2025
914a069
change public_key.
c-r33d Jun 10, 2025
4cad58f
feat(sdk): Base key support.
c-r33d Jun 11, 2025
d7c56a8
Merge branch 'main' into feat/DSPX-1132-base-keys-sdk
c-r33d Jun 11, 2025
01f474a
upgrade go.
c-r33d Jun 11, 2025
85b398e
upgrade go.
c-r33d Jun 11, 2025
1d38d46
linting.
c-r33d Jun 11, 2025
98096a0
linting.
c-r33d Jun 11, 2025
e790de5
remove file.
c-r33d Jun 11, 2025
13ecb7e
linting
c-r33d Jun 11, 2025
b22e072
fix tests.
c-r33d Jun 11, 2025
ef61431
tidy
c-r33d Jun 11, 2025
af54977
test.
c-r33d Jun 12, 2025
594e68a
Merge branch 'main' into feat/DSPX-1132-base-keys-sdk
c-r33d Jun 12, 2025
ad667fb
Merge branch 'main' into feat/DSPX-1132-base-keys-sdk
c-r33d Jun 12, 2025
f8fea08
fix small keys bug.
c-r33d Jun 13, 2025
b7d9a28
linting.
c-r33d Jun 13, 2025
d35e9d6
fix underflow?
c-r33d Jun 13, 2025
e9bfc93
fix tests.
c-r33d Jun 13, 2025
d6d63cb
fix linting./
c-r33d Jun 13, 2025
4711f7c
fix conditional.
c-r33d Jun 16, 2025
d072160
fix conditional.
c-r33d Jun 16, 2025
e84d98d
fix conditional.
c-r33d Jun 16, 2025
9cf2de7
fix test.
c-r33d Jun 16, 2025
eb5dad7
refactor.
c-r33d Jun 16, 2025
c00a90f
refactor.
c-r33d Jun 16, 2025
5ed7eb6
refactor.
c-r33d Jun 16, 2025
c5922f3
update.
c-r33d Jun 16, 2025
c3c5d17
update to enum.
c-r33d Jun 16, 2025
8d76873
linting
c-r33d Jun 16, 2025
347caa8
fix issues with nano.
c-r33d Jun 17, 2025
9359071
Merge branch 'main' into feat/DSPX-1132-base-keys-sdk
c-r33d Jun 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions sdk/basekey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package sdk

import (
"context"
"encoding/json"
"errors"
"fmt"

"github.com/opentdf/platform/lib/ocrypto"
"github.com/opentdf/platform/protocol/go/policy"
"github.com/opentdf/platform/protocol/go/wellknownconfiguration"
"google.golang.org/protobuf/encoding/protojson"
)

// Should match:
// https://github.com/opentdf/platform/blob/main/service/wellknownconfiguration/wellknown_configuration.go#L25
const (
baseKeyWellKnown = "base_key"
baseKeyAlg = "algorithm"
baseKeyPublicKey = "public_key"
wellKnownConfigKey = "configuration"
)

var (
errWellKnownConfigFormat = errors.New("well-known configuration has invalid format")
errBaseKeyNotFound = errors.New("base key not found in well-known configuration")
errBaseKeyInvalidFormat = errors.New("base key has invalid format")
errBaseKeyEmpty = errors.New("base key is empty or not provided")
errMarshalBaseKeyFailed = errors.New("failed to marshal base key configuration")
errUnmarshalBaseKeyFailed = errors.New("failed to unmarshal base key configuration")
)

// TODO: Move this function to ocrypto?
func getKasKeyAlg(alg string) policy.Algorithm {
switch alg {
case string(ocrypto.RSA2048Key):
return policy.Algorithm_ALGORITHM_RSA_2048
case "rsa:4096":
return policy.Algorithm_ALGORITHM_RSA_4096
case string(ocrypto.EC256Key):
return policy.Algorithm_ALGORITHM_EC_P256
case string(ocrypto.EC384Key):
return policy.Algorithm_ALGORITHM_EC_P384
case string(ocrypto.EC521Key):
return policy.Algorithm_ALGORITHM_EC_P521
default:
return policy.Algorithm_ALGORITHM_UNSPECIFIED
}
}

// TODO: Move this function to ocrypto?
func formatAlg(alg policy.Algorithm) (string, error) {
switch alg {
case policy.Algorithm_ALGORITHM_RSA_2048:
return string(ocrypto.RSA2048Key), nil
case policy.Algorithm_ALGORITHM_RSA_4096:
return "rsa:4096", nil
case policy.Algorithm_ALGORITHM_EC_P256:
return string(ocrypto.EC256Key), nil
case policy.Algorithm_ALGORITHM_EC_P384:
return string(ocrypto.EC384Key), nil
case policy.Algorithm_ALGORITHM_EC_P521:
return string(ocrypto.EC521Key), nil
case policy.Algorithm_ALGORITHM_UNSPECIFIED:
fallthrough
default:
return "", fmt.Errorf("unsupported algorithm: %s", alg)
}
}

func getBaseKey(ctx context.Context, s SDK) (*policy.SimpleKasKey, error) {
simpleKasKey := &policy.SimpleKasKey{}

req := &wellknownconfiguration.GetWellKnownConfigurationRequest{}
response, err := s.wellknownConfiguration.GetWellKnownConfiguration(ctx, req)
if err != nil {
return nil, errors.Join(errors.New("unable to retrieve config information, and none was provided"), err)
}
configuration := response.GetConfiguration()
if configuration == nil {
return nil, ErrWellKnowConfigEmpty
}
configStructure, ok := configuration.AsMap()[wellKnownConfigKey]
if !ok {
return nil, err
}

configMap, ok := configStructure.(map[string]interface{})
if !ok {
return nil, errWellKnownConfigFormat
}

simpleKasKey, err = parseSimpleKasKey(configMap)
if err != nil {
return nil, err
}

return simpleKasKey, nil
}

func parseSimpleKasKey(configMap map[string]interface{}) (*policy.SimpleKasKey, error) {
simpleKasKey := &policy.SimpleKasKey{}
baseKey, ok := configMap[baseKeyWellKnown]
if !ok {
return nil, errBaseKeyNotFound
}

baseKeyMap, ok := baseKey.(map[string]interface{})
if !ok {
return nil, errBaseKeyInvalidFormat
}
if len(baseKeyMap) == 0 {
return nil, errBaseKeyEmpty
}

publicKey, ok := baseKeyMap[baseKeyPublicKey].(map[string]interface{})
if !ok {
return nil, errBaseKeyInvalidFormat
}

alg, ok := publicKey[baseKeyAlg].(string)
if !ok {
return nil, errBaseKeyInvalidFormat
}
publicKey[baseKeyAlg] = getKasKeyAlg(alg)
baseKeyMap[baseKeyPublicKey] = publicKey
configJSON, err := json.Marshal(baseKey)
if err != nil {
return nil, errors.Join(errMarshalBaseKeyFailed, err)
}

err = protojson.Unmarshal(configJSON, simpleKasKey)
if err != nil {
return nil, errors.Join(errUnmarshalBaseKeyFailed, err)
}
return simpleKasKey, nil
}
Loading
Loading