Skip to content

Commit b924cae

Browse files
committed
change approach: override keys using builderCtx.CustomBuildProperties
1 parent 5737199 commit b924cae

File tree

4 files changed

+10
-102
lines changed

4 files changed

+10
-102
lines changed

commands/compile/compile.go

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
129129
// so, if the flags to override the default keys are used, we try override the corresponding platform property nonetheless.
130130
// It's not possible to use the default name for the keys since there could be more tools to sign and encrypt.
131131
// So it's mandatory to use all the tree flags to sign and encrypt the binary
132+
securityKeysOverride := []string{}
132133
if req.KeysKeychain != "" && req.SignKey != "" && req.EncryptKey != "" {
133134
keysDirPath := paths.New(req.KeysKeychain)
134135
if !keysDirPath.IsDir() {
@@ -142,8 +143,8 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
142143
if !encryptKeyPath.Exist() {
143144
return nil, &arduino.NotFoundError{Message: tr("The path of the specified encryption key does not exist: %s", encryptKeyPath), Cause: err}
144145
}
145-
InstalledPlatformRelease := pm.GetInstalledPlatformRelease(targetPlatform)
146-
ReplaceSecurityKeys(InstalledPlatformRelease.Properties, req.KeysKeychain, req.SignKey, req.EncryptKey)
146+
securityKeysOverride = append(securityKeysOverride, "build.keys.keychain="+req.KeysKeychain, "build.keys.sign_key="+req.GetSignKey(), "build.keys.encrypt_key="+req.EncryptKey)
147+
// ReplaceSecurityKeys(req.KeysKeychain, req.SignKey, req.EncryptKey)
147148
}
148149

149150
builderCtx := &types.Context{}
@@ -186,6 +187,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
186187
builderCtx.WarningsLevel = req.GetWarnings()
187188

188189
builderCtx.CustomBuildProperties = append(req.GetBuildProperties(), "build.warn_data_percentage=75")
190+
builderCtx.CustomBuildProperties = append(req.GetBuildProperties(), securityKeysOverride...)
189191

190192
if req.GetBuildCachePath() != "" {
191193
builderCtx.BuildCachePath = paths.New(req.GetBuildCachePath())
@@ -317,28 +319,3 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
317319

318320
return r, nil
319321
}
320-
321-
// ReplaceSecurityKeys function will override the properties representing the security keys specified in the platform.txt file of a platform with the ones provided by the user.
322-
// The keys are stored in the keyPath
323-
// signKey is the key used to sign a binary
324-
// encryptKey is the key used to encrypt it
325-
func ReplaceSecurityKeys(properties *properties.Map, keysKKeysKeychain, signKey, encryptKey string) {
326-
toolsProps := properties.SubTree("tools").FirstLevelOf()
327-
for toolName, toolProps := range toolsProps {
328-
if toolProps.ContainsKey("keys.path") {
329-
key := "tools." + toolName + ".keys.path"
330-
properties.Set(key, keysKKeysKeychain)
331-
logrus.Tracef("Overriding Property: %s: %s", key, keysKKeysKeychain)
332-
}
333-
if toolProps.ContainsKey("sign.name") {
334-
key := "tools." + toolName + ".sign.name"
335-
properties.Set(key, signKey)
336-
logrus.Tracef("Overriding Property: %s: %s", key, signKey)
337-
}
338-
if toolProps.ContainsKey("encrypt.name") {
339-
key := "tools." + toolName + ".encrypt.name"
340-
properties.Set(key, encryptKey)
341-
logrus.Tracef("Overriding Property: %s: %s", key, encryptKey)
342-
}
343-
}
344-
}

commands/compile/compile_test.go

Lines changed: 0 additions & 70 deletions
This file was deleted.

test/testdata/platform_with_secure_boot/boards.local.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ uno.menu.security.sien=Signature + Encryption
55

66
uno.menu.security.sien.build.postbuild.cmd="{tools.imgtool.cmd}" {tools.imgtool.build.pattern}
77
uno.menu.security.none.build.postbuild.cmd="{tools.imgtool.cmd}" exit
8+
9+
uno.menu.security.sien.build.keys.type=public_keys
10+
uno.menu.security.sien.build.keys.keychain={runtime.hardware.path}/Default_Keys
11+
uno.menu.security.sien.build.keys.sign_key=default-signing-key.pem
12+
uno.menu.security.sien.build.keys.encrypt_key=default-encrypt-key.pem

test/testdata/platform_with_secure_boot/platform.local.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,4 @@ recipe.hooks.objcopy.postobjcopy.1.pattern={build.postbuild.cmd}
55
#
66

77
tools.imgtool.cmd=echo
8-
tools.imgtool.keys.path={runtime.hardware.path}/Default_Keys
9-
tools.imgtool.sign.name=default-signing-key.pem
10-
tools.imgtool.encrypt.name=default-encrypt-key.pem
11-
12-
tools.imgtool.build.pattern=sign --key "{tools.imgtool.keys.path}/{tools.imgtool.sign.name}" --encrypt "{tools.imgtool.keys.path}/{tools.imgtool.encrypt.name}" "{build.path}/{build.project_name}.bin" "{build.path}/{build.project_name}.bin" --align {build.alignment} --max-align {build.alignment} --version {build.version} --header-size {build.header_size} --pad-header --slot-size {build.slot_size}
8+
tools.imgtool.build.pattern=sign --key "{build.keys.keychain}/{build.keys.sign_key}" --encrypt "{build.keys.keychain}/{build.keys.encrypt_key}" "{build.path}/{build.project_name}.bin" "{build.path}/{build.project_name}.bin" --align {build.alignment} --max-align {build.alignment} --version {build.version} --header-size {build.header_size} --pad-header --slot-size {build.slot_size}

0 commit comments

Comments
 (0)