Skip to content

Commit

Permalink
re-read the enclave key
Browse files Browse the repository at this point in the history
  • Loading branch information
tudor-malene committed Dec 13, 2024
1 parent 27eeffe commit 1e03728
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions go/common/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"errors"
"fmt"
"math/big"

Expand Down Expand Up @@ -77,6 +78,9 @@ type (
EnclaveID = common.Address
)

// FailedDecryptErr - when the TEN enclave fails to decrypt an RPC request
var FailedDecryptErr = errors.New("failed to decrypt RPC payload. please use the correct enclave key")

// EncryptedRPCRequest - an encrypted request with extra plaintext metadata
type EncryptedRPCRequest struct {
Req EncryptedRequest
Expand Down
2 changes: 1 addition & 1 deletion go/enclave/rpc/vk_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func HandleEncryptedRPC(ctx context.Context,
// 1. Decrypt request
plaintextRequest, err := encManager.DecryptBytes(encReq)
if err != nil {
return responses.AsPlaintextError(fmt.Errorf("could not decrypt params - %w", err)), nil
return responses.AsPlaintextError(common.FailedDecryptErr), nil
}

// 2. Unmarshall
Expand Down
19 changes: 18 additions & 1 deletion go/rpc/encrypted_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"reflect"
"sync/atomic"
Expand Down Expand Up @@ -71,7 +72,23 @@ func (c *EncRPCClient) CallContext(ctx context.Context, result interface{}, meth
}

if rpc.IsEncryptedMethod(method) {
return c.executeEncryptedCall(ctx, result, method, args...)
err := c.executeEncryptedCall(ctx, result, method, args...)
// this should only be triggered during testing
if err != nil && errors.Is(err, common.FailedDecryptErr) {
c.logger.Warn("Reconnecting to new backend. Reading the enclave key.")
newKey, err := ReadEnclaveKey(c.obscuroClient)
if err != nil {
return fmt.Errorf("could not refresh enclave key: %w", err)
}
enclPubECDSA, err := crypto.DecompressPubkey(newKey)
if err != nil {
return fmt.Errorf("failed to decompress key for RPC client: %w", err)
}
c.enclavePublicKey = ecies.ImportECDSAPublic(enclPubECDSA)
// retry with the updated key
return c.executeEncryptedCall(ctx, result, method, args...)
}
return err
}

// for non-sensitive methods or when viewing keys are disabled we just delegate directly to the geth RPC client
Expand Down

0 comments on commit 1e03728

Please sign in to comment.