-
Notifications
You must be signed in to change notification settings - Fork 24
feat(kas): support HSM and standard crypto #497
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
3cb3e5e
feat(kas): support HSM and standard crypto
sujankota b993e3c
fix the integeration test
sujankota d6e0600
fix test
sujankota ddd1b94
rename lib/crypto to lib/ocrypto
sujankota c1c7c5c
after merge
sujankota f7955dd
Fix Dockerfile
sujankota b40a49e
Fix build
sujankota b920593
fix the build
sujankota eb94914
go lint fix
sujankota 4ab185f
fix lint errors
sujankota 8214acd
fix lint errors
sujankota dcc1435
Fix lint error
sujankota 1f372a5
merge main
sujankota 3483990
Fix lint errors
sujankota 98307e2
github flow fixes
sujankota 4602072
Merge branch 'main' into support-standard-crypto
sujankota 1d200d4
fix the build
sujankota d4c4a10
Fix lint error
sujankota 9a13e79
Fixed lint errors
sujankota 9a5987e
Fix the integeration test
sujankota 30deeda
Fix lint errors
sujankota a4c210d
fix the lint error
sujankota b69212a
fix the lint error
sujankota 70d18be
fix the test
sujankota 00844d1
Fix services tests
sujankota b9508b7
Merge branch 'main' into support-standard-crypto
sujankota 6ed2fd8
Fix lint error
sujankota acdbccf
Fix test
sujankota 299cd98
Fix service tests
sujankota 955c1f8
Fix service tests
sujankota 2e29d88
Fix service test
sujankota 49185c8
Fix service test
sujankota 199987d
Fix service tests
sujankota 342432a
Fix service tests
sujankota cdcc223
Fix service tests
sujankota 373a137
Fix service test
sujankota c7d192d
Disable HSM
sujankota a48949b
Fix the ocrypto package
sujankota 08e2e41
Merge branch 'main' into support-standard-crypto
sujankota 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,26 @@ | ||
| package security | ||
|
|
||
| type Config struct { | ||
| Type string `yaml:"type" default:"standard"` | ||
| // HSMConfig is the configuration for the HSM | ||
| HSMConfig HSMConfig `yaml:"hsm,omitempty" mapstructure:"hsm"` | ||
| // StandardConfig is the configuration for the standard key provider | ||
| StandardConfig StandardConfig `yaml:"standard,omitempty" mapstructure:"standard"` | ||
| } | ||
|
|
||
| type CryptoProvider interface { | ||
| RSAPublicKey(keyId string) (string, error) | ||
| ECPublicKey(keyId string) (string, error) | ||
| RSADecrypt(hashFunction string, keyId string, keyLabel string, ciphertext []byte) ([]byte, error) | ||
| } | ||
|
|
||
| func NewCryptoProvider(cfg Config) (CryptoProvider, error) { | ||
| switch cfg.Type { | ||
| case "hsm": | ||
| return New(&cfg.HSMConfig) | ||
| case "standard": | ||
| return NewStandardCrypto(cfg.StandardConfig) | ||
| default: | ||
| return NewStandardCrypto(cfg.StandardConfig) | ||
| } | ||
| } |
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,114 @@ | ||
| package security | ||
|
|
||
| import ( | ||
| "crypto/ecdh" | ||
| "errors" | ||
| "fmt" | ||
| "github.com/opentdf/platform/lib/crypto" | ||
| "log/slog" | ||
| "os" | ||
| ) | ||
|
|
||
| var ( | ||
| errStandardCryptoNotEnabled = errors.New("standard crypto flag is enabled in the config") | ||
| errStandardCryptoObjIsInvalid = errors.New("standard crypto object is invalid") | ||
| ) | ||
|
|
||
| type StandardConfig struct { | ||
| RSAKeys map[string]StandardKeyInfo `yaml:"rsa,omitempty" mapstructure:"rsa"` | ||
| ECKeys map[string]StandardKeyInfo `yaml:"ec,omitempty" mapstructure:"ec"` | ||
| } | ||
|
|
||
| type StandardKeyInfo struct { | ||
| PrivateKeyPath string `yaml:"privateKeyPath" mapstructure:"privateKeyPath"` | ||
| PublicKeyPath string `yaml:"publicKeyPath" mapstructure:"publicKeyPath"` | ||
| } | ||
|
|
||
| type StandardRSACrypto struct { | ||
| Identifier string | ||
| asymDecryption crypto.AsymDecryption | ||
| asymEncryption crypto.AsymEncryption | ||
| } | ||
|
|
||
| type StandardECCrypto struct { | ||
| Identifier string | ||
| ecPublicKey *ecdh.PublicKey | ||
| ecPrivateKey *ecdh.PrivateKey | ||
| } | ||
|
|
||
| type StandardCrypto struct { | ||
| rsaKeys []StandardRSACrypto | ||
| ecKeys []StandardECCrypto | ||
| } | ||
|
|
||
| // NewStandardCrypto Create a new instance of standard crypto | ||
| func NewStandardCrypto(cfg StandardConfig) (*StandardCrypto, error) { | ||
| standardCrypto := &StandardCrypto{} | ||
| for id, kasInfo := range cfg.RSAKeys { | ||
| privatePemData, err := os.ReadFile(kasInfo.PrivateKeyPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to rsa private key file: %w", err) | ||
| } | ||
|
|
||
| asymDecryption, err := crypto.NewAsymDecryption(string(privatePemData)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("crypto.NewAsymDecryption failed: %w", err) | ||
| } | ||
|
|
||
| publicPemData, err := os.ReadFile(kasInfo.PublicKeyPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to rsa public key file: %w", err) | ||
| } | ||
|
|
||
| asymEncryption, err := crypto.NewAsymEncryption(string(publicPemData)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("crypto.NewAsymEncryption failed: %w", err) | ||
| } | ||
|
|
||
| standardCrypto.rsaKeys = append(standardCrypto.rsaKeys, StandardRSACrypto{ | ||
| Identifier: id, | ||
| asymDecryption: asymDecryption, | ||
| asymEncryption: asymEncryption, | ||
| }) | ||
| } | ||
|
|
||
| return standardCrypto, nil | ||
| } | ||
|
|
||
| func (s StandardCrypto) RSAPublicKey(keyId string) (string, error) { | ||
|
|
||
| if len(s.rsaKeys) == 0 { | ||
| return "", errStandardCryptoObjIsInvalid | ||
| } | ||
|
|
||
| // TODO: For now ignore the key id | ||
| slog.Info("⚠️ Ignoring the", slog.String("key id", keyId)) | ||
|
|
||
| pem, err := s.rsaKeys[0].asymEncryption.PublicKeyInPemFormat() | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to retrive rsa public key file: %w", err) | ||
| } | ||
|
|
||
| return pem, nil | ||
| } | ||
|
|
||
| func (s StandardCrypto) ECPublicKey(keyId string) (string, error) { | ||
| return "", nil | ||
| } | ||
|
|
||
| func (s StandardCrypto) RSADecrypt(hashFunction string, keyId string, keyLabel string, ciphertext []byte) ([]byte, error) { | ||
|
|
||
| if len(s.rsaKeys) == 0 { | ||
| return nil, errStandardCryptoObjIsInvalid | ||
| } | ||
|
|
||
| // TODO: For now ignore the key id | ||
| slog.Info("⚠️ Ignoring the", slog.String("key id", keyId)) | ||
|
|
||
| data, err := s.rsaKeys[0].asymDecryption.Decrypt(ciphertext) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error decrypting data: %w", err) | ||
| } | ||
|
|
||
| return data, nil | ||
| } |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.