From 120d3fdd9dbe73309d4db35bfc35fd099db9f90d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Eckel?= Date: Wed, 6 Sep 2023 15:41:39 +0200 Subject: [PATCH] fixed signing (tests) and signing verification of the machine ID Signature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jürgen Eckel --- docs/static/openapi.yml | 6 ++++++ testutil/sample/sample.go | 2 +- util/validate_signature.go | 15 +++++++++++++++ x/machine/keeper/msg_server_attest_machine.go | 2 +- 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/docs/static/openapi.yml b/docs/static/openapi.yml index a33eee0..b30243e 100644 --- a/docs/static/openapi.yml +++ b/docs/static/openapi.yml @@ -46556,6 +46556,8 @@ paths: type: type: integer format: int64 + machineIdSignature: + type: string default: description: An unexpected error response. schema: @@ -75415,6 +75417,8 @@ definitions: type: type: integer format: int64 + machineIdSignature: + type: string planetmintgo.machine.Metadata: type: object properties: @@ -75473,6 +75477,8 @@ definitions: type: type: integer format: int64 + machineIdSignature: + type: string planetmintgo.machine.QueryParamsResponse: type: object properties: diff --git a/testutil/sample/sample.go b/testutil/sample/sample.go index f924ad3..f98764e 100644 --- a/testutil/sample/sample.go +++ b/testutil/sample/sample.go @@ -61,7 +61,7 @@ func Machine(name, pubKey string, prvKey string) machinetypes.Machine { prvKeyBytes, _ := hex.DecodeString(prvKey) sk := &secp256k1.PrivKey{Key: prvKeyBytes} - pubKeyBytes := []byte(pubKey) + pubKeyBytes, _ := hex.DecodeString(pubKey) sign, _ := sk.Sign(pubKeyBytes) signatureHex := hex.EncodeToString(sign) diff --git a/util/validate_signature.go b/util/validate_signature.go index 72959b8..6e088a4 100644 --- a/util/validate_signature.go +++ b/util/validate_signature.go @@ -20,3 +20,18 @@ func ValidateSignature(message string, signature string, publicKey string) bool return isValid } + +func ValidateSignature_hexstring(message string, signature string, publicKey string) bool { + // Convert the message, signature, and public key from hex to bytes + messageBytes, _ := hex.DecodeString(message) + signatureBytes, _ := hex.DecodeString(signature) + publicKeyBytes, _ := hex.DecodeString(publicKey) + + // Create a secp256k1 public key object + pubKey := &secp256k1.PubKey{Key: publicKeyBytes} + + // Verify the signature + isValid := pubKey.VerifySignature(messageBytes, signatureBytes) + + return isValid +} diff --git a/x/machine/keeper/msg_server_attest_machine.go b/x/machine/keeper/msg_server_attest_machine.go index 61bb9a9..f8014de 100644 --- a/x/machine/keeper/msg_server_attest_machine.go +++ b/x/machine/keeper/msg_server_attest_machine.go @@ -33,7 +33,7 @@ func (k msgServer) AttestMachine(goCtx context.Context, msg *types.MsgAttestMach return nil, errors.New("trust anchor has already been used for attestation") } - isValidMachineId := util.ValidateSignature(msg.Machine.MachineId, msg.Machine.MachineIdSignature, msg.Machine.MachineId) + isValidMachineId := util.ValidateSignature_hexstring(msg.Machine.MachineId, msg.Machine.MachineIdSignature, msg.Machine.MachineId) if !isValidMachineId { return nil, errors.New("invalid machine id") }