@@ -12,7 +12,6 @@ import (
12
12
"log/slog"
13
13
14
14
"github.com/opentdf/platform/lib/ocrypto"
15
- "github.com/opentdf/platform/protocol/go/policy"
16
15
"github.com/opentdf/platform/service/logger"
17
16
"github.com/opentdf/platform/service/pkg/cache"
18
17
"github.com/opentdf/platform/service/trust"
@@ -50,7 +49,7 @@ func (b *BasicManager) Name() string {
50
49
return BasicManagerName
51
50
}
52
51
53
- func (b * BasicManager ) Decrypt (ctx context.Context , keyDetails trust.KeyDetails , ciphertext []byte , ephemeralPublicKey []byte ) (trust .ProtectedKey , error ) {
52
+ func (b * BasicManager ) Decrypt (ctx context.Context , keyDetails trust.KeyDetails , ciphertext []byte , ephemeralPublicKey []byte ) (ocrypto .ProtectedKey , error ) {
54
53
// Implementation of Decrypt method
55
54
56
55
// Get Private Key
@@ -75,8 +74,12 @@ func (b *BasicManager) Decrypt(ctx context.Context, keyDetails trust.KeyDetails,
75
74
if err != nil {
76
75
return nil , fmt .Errorf ("failed to decrypt with RSA: %w" , err )
77
76
}
78
- return ocrypto .NewAESProtectedKey (plaintext ), nil
79
- case policy .Algorithm_ALGORITHM_EC_P256 .String (), policy .Algorithm_ALGORITHM_EC_P384 .String (), policy .Algorithm_ALGORITHM_EC_P521 .String ():
77
+ protectedKey , err := ocrypto .NewAESProtectedKey (plaintext )
78
+ if err != nil {
79
+ return nil , fmt .Errorf ("failed to create protected key: %w" , err )
80
+ }
81
+ return protectedKey , nil
82
+ case ocrypto .EC256Key , ocrypto .EC384Key , ocrypto .EC521Key :
80
83
ecPrivKey , err := ocrypto .ECPrivateKeyFromPem (privKey )
81
84
if err != nil {
82
85
return nil , fmt .Errorf ("failed to create EC private key from PEM: %w" , err )
@@ -89,13 +92,17 @@ func (b *BasicManager) Decrypt(ctx context.Context, keyDetails trust.KeyDetails,
89
92
if err != nil {
90
93
return nil , fmt .Errorf ("failed to decrypt with ephemeral key: %w" , err )
91
94
}
92
- return ocrypto .NewAESProtectedKey (plaintext ), nil
95
+ protectedKey , err := ocrypto .NewAESProtectedKey (plaintext )
96
+ if err != nil {
97
+ return nil , fmt .Errorf ("failed to create protected key: %w" , err )
98
+ }
99
+ return protectedKey , nil
93
100
}
94
101
95
102
return nil , fmt .Errorf ("unsupported algorithm: %s" , keyDetails .Algorithm ())
96
103
}
97
104
98
- func (b * BasicManager ) DeriveKey (ctx context.Context , keyDetails trust.KeyDetails , ephemeralPublicKeyBytes []byte , curve elliptic.Curve ) (trust .ProtectedKey , error ) {
105
+ func (b * BasicManager ) DeriveKey (ctx context.Context , keyDetails trust.KeyDetails , ephemeralPublicKeyBytes []byte , curve elliptic.Curve ) (ocrypto .ProtectedKey , error ) {
99
106
// Implementation of DeriveKey method
100
107
privateKeyCtx , err := keyDetails .ExportPrivateKey (ctx )
101
108
if err != nil {
@@ -131,22 +138,35 @@ func (b *BasicManager) DeriveKey(ctx context.Context, keyDetails trust.KeyDetail
131
138
if err != nil {
132
139
return nil , fmt .Errorf ("failed to calculate HKDF: %w" , err )
133
140
}
134
- return ocrypto .NewAESProtectedKey (key ), nil
141
+ protectedKey , err := ocrypto .NewAESProtectedKey (key )
142
+ if err != nil {
143
+ return nil , fmt .Errorf ("failed to create protected key: %w" , err )
144
+ }
145
+ return protectedKey , nil
135
146
}
136
147
137
148
type OCEncapsulator struct {
138
149
ocrypto.PublicKeyEncryptor
139
150
}
140
151
141
- func (e * OCEncapsulator ) Encapsulate (dek trust .ProtectedKey ) ([]byte , error ) {
142
- ipk , ok := dek .(* InProcessAESKey )
152
+ func (e * OCEncapsulator ) Encapsulate (dek ocrypto .ProtectedKey ) ([]byte , error ) {
153
+ ipk , ok := dek .(* ocrypto. AESProtectedKey )
143
154
if ! ok {
144
155
return nil , errors .New ("invalid DEK type for encapsulation" )
145
156
}
146
- return e .Encrypt (ipk .rawKey )
157
+ // Export the raw key without encryption
158
+ rawKey , err := ipk .Export (nil )
159
+ if err != nil {
160
+ return nil , fmt .Errorf ("failed to export raw key: %w" , err )
161
+ }
162
+ return e .Encrypt (rawKey )
163
+ }
164
+
165
+ func (e * OCEncapsulator ) PublicKeyAsPEM () (string , error ) {
166
+ return e .PublicKeyEncryptor .PublicKeyInPemFormat ()
147
167
}
148
168
149
- func (b * BasicManager ) GenerateECSessionKey (_ context.Context , ephemeralPublicKey string ) (trust .Encapsulator , error ) {
169
+ func (b * BasicManager ) GenerateECSessionKey (_ context.Context , ephemeralPublicKey string ) (ocrypto .Encapsulator , error ) {
150
170
pke , err := ocrypto .FromPublicPEMWithSalt (ephemeralPublicKey , NanoVersionSalt (), nil )
151
171
if err != nil {
152
172
return nil , fmt .Errorf ("failed to create public key encryptor: %w" , err )
0 commit comments