check and make keystore if required on receiving device

This commit is contained in:
Samuel Hawksby-Robinson 2022-10-04 16:30:11 +01:00
parent 9bb1e75939
commit 61904182c5
2 changed files with 34 additions and 4 deletions

View File

@ -348,6 +348,26 @@ func (ppr *PairingPayloadRepository) validateKeys(password string) error {
} }
func (ppr *PairingPayloadRepository) storeKeys(keyStorePath string) error { func (ppr *PairingPayloadRepository) storeKeys(keyStorePath string) error {
if keyStorePath == "" {
return fmt.Errorf("keyStorePath can not be empty")
}
_, lastDir := filepath.Split(keyStorePath)
// If lastDir == "keystore" we presume we need to create the rest of the keystore path
// else we presume the provided keystore is valid
if lastDir == "keystore" {
if ppr.multiaccount == nil || ppr.multiaccount.KeyUID == "" {
return fmt.Errorf("no known Key UID")
}
keyStorePath = filepath.Join(keyStorePath, ppr.multiaccount.KeyUID)
err := os.MkdirAll(keyStorePath, 0777)
if err != nil {
return err
}
}
for name, data := range ppr.keys { for name, data := range ppr.keys {
accountKey := new(keystore.EncryptedKeyJSONV3) accountKey := new(keystore.EncryptedKeyJSONV3)
if err := json.Unmarshal(data, &accountKey); err != nil { if err := json.Unmarshal(data, &accountKey); err != nil {

View File

@ -20,7 +20,7 @@ import (
var ( var (
password = "password" password = "password"
keyUID = "0xdeadbeef" keyUID = "0x6b9a74f33316e02479c33ed23cf16e0408dca3e1b9ab8f361630859543eb0d46"
expected = multiaccounts.Account{ expected = multiaccounts.Account{
Name: "cool account", Name: "cool account",
KeyUID: keyUID, KeyUID: keyUID,
@ -66,6 +66,16 @@ func makeKeystores(t *testing.T) (string, string, func()) {
emptyKeyStoreDir, err := ioutil.TempDir(os.TempDir(), "accounts_empty") emptyKeyStoreDir, err := ioutil.TempDir(os.TempDir(), "accounts_empty")
require.NoError(t, err) require.NoError(t, err)
keyStoreDir = filepath.Join(keyStoreDir, "keystore", keyUID)
// TODO test case where the keystore dir does not yet exist because the device is new
emptyKeyStoreDir = filepath.Join(emptyKeyStoreDir, "keystore", keyUID)
err = os.MkdirAll(keyStoreDir, 0777)
require.NoError(t, err)
err = os.MkdirAll(emptyKeyStoreDir, 0777)
require.NoError(t, err)
return keyStoreDir, emptyKeyStoreDir, func() { return keyStoreDir, emptyKeyStoreDir, func() {
os.RemoveAll(keyStoreDir) os.RemoveAll(keyStoreDir)
os.RemoveAll(emptyKeyStoreDir) os.RemoveAll(emptyKeyStoreDir)
@ -189,12 +199,12 @@ func (pms *PayloadMarshallerSuite) TestPayloadMarshaller_MarshalToProtobuf() {
// TEST PairingPayloadMarshaller 1 MarshalToProtobuf() // TEST PairingPayloadMarshaller 1 MarshalToProtobuf()
pb, err := ppm.MarshalToProtobuf() pb, err := ppm.MarshalToProtobuf()
pms.Require().NoError(err) pms.Require().NoError(err)
pms.Require().Len(pb, 1216) pms.Require().Len(pb, 1384)
h := sha256.New() h := sha256.New()
h.Write(pb) h.Write(pb)
hashA := []byte{0x70, 0xf2, 0xe5, 0x37, 0xff, 0x7d, 0x2d, 0x7b, 0x8a, 0x4b, 0x53, 0x1f, 0xfe, 0x3e, 0xea, 0x5e, 0x4d, 0xe1, 0xad, 0x44, 0xe8, 0x22, 0x5c, 0x84, 0x30, 0xd6, 0x75, 0x1a, 0xbd, 0x53, 0x59, 0xce} hashA := []byte{0xe5, 0x34, 0x2e, 0xf1, 0x81, 0x72, 0xab, 0xc3, 0xde, 0x54, 0xbc, 0x8e, 0xd8, 0x34, 0xbe, 0xab, 0xd, 0xe8, 0x84, 0x53, 0xa2, 0x14, 0x9b, 0xbe, 0xc5, 0xe5, 0xce, 0xa5, 0xe9, 0x6d, 0xbc, 0xdd}
hashB := []byte{0xeb, 0xb7, 0x34, 0x94, 0x1d, 0x8d, 0x88, 0xdf, 0xa2, 0xfa, 0xc2, 0x9e, 0x11, 0xba, 0xa5, 0xc5, 0x95, 0x51, 0x73, 0xb, 0x9a, 0xb1, 0x92, 0xf9, 0xa2, 0x55, 0x5f, 0x50, 0x81, 0xe2, 0xf9, 0x46} hashB := []byte{0x98, 0x2b, 0x3d, 0x8b, 0x7c, 0x6a, 0x3e, 0xdc, 0x3, 0xb1, 0xbf, 0xf1, 0x50, 0x15, 0xa5, 0x0, 0xa8, 0xba, 0xae, 0xf9, 0x38, 0xa8, 0x65, 0xd8, 0xf0, 0x93, 0xca, 0xbc, 0x47, 0x5d, 0x84, 0x23}
// Because file-walk will pull files in an unpredictable order from a target dir // Because file-walk will pull files in an unpredictable order from a target dir
// there are 2 potential valid hashes, because there are 2 key files in the test dir // there are 2 potential valid hashes, because there are 2 key files in the test dir