You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2020/02/29 01:12:25 UTC
[mynewt-imgmod] branch master updated (408e217 -> fa5c279)
This is an automated email from the ASF dual-hosted git repository.
ccollins pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-imgmod.git.
from 408e217 .gitignore: Ignore `imgmod` binary
new fbb10f8 Pull in latest mynewt-artifact
new c8887e4 Fix bad copy+paste in help text
new 9e6ead5 image hashable: Include padding and protected TLVs
new 296fca7 image addsig: Allow sig type to be in hexadecimal
new ec8fdbb New command: image decrypthw
new 14683ca New command: image setbody
new 0f5f9ea New command: image extractbody
new fa5c279 New command: image rehash
The 8 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
cli/image_cmds.go | 202 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
go.mod | 2 +-
go.sum | 2 +
iimg/iimg.go | 26 +++++--
4 files changed, 222 insertions(+), 10 deletions(-)
[mynewt-imgmod] 03/08: image hashable: Include padding and
protected TLVs
Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-imgmod.git
commit 9e6ead5589ee76080c961ab86c0f24a0bafd1056
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri Feb 28 16:23:30 2020 -0800
image hashable: Include padding and protected TLVs
Header padding and protected TLVs are inputs during hash calculation.
Update the `image hashable` command to account for this.
---
cli/image_cmds.go | 31 ++++++++++++++++++++++++++++++-
1 file changed, 30 insertions(+), 1 deletion(-)
diff --git a/cli/image_cmds.go b/cli/image_cmds.go
index 2b75b59..9dcb171 100644
--- a/cli/image_cmds.go
+++ b/cli/image_cmds.go
@@ -323,14 +323,43 @@ func runHashableCmd(cmd *cobra.Command, args []string) {
}
defer f.Close()
- if err := binary.Write(f, binary.LittleEndian, &img.Header); err != nil {
+ err = binary.Write(f, binary.LittleEndian, &img.Header)
+ if err != nil {
ImgmodUsage(nil, errors.Wrapf(err, "error writing image header"))
}
+
+ _, err = f.Write(img.Pad)
+ if err != nil {
+ ImgmodUsage(nil, errors.Wrapf(err, "error writing image padding"))
+ }
+
_, err = f.Write(img.Body)
if err != nil {
ImgmodUsage(nil, errors.Wrapf(err, "error writing image body"))
}
+ if len(img.ProtTlvs) > 0 {
+ trailer := image.ImageTrailer{
+ Magic: image.IMAGE_PROT_TRAILER_MAGIC,
+ TlvTotLen: img.Header.ProtSz,
+ }
+ err = binary.Write(f, binary.LittleEndian, trailer)
+ if err != nil {
+ ImgmodUsage(nil, errors.Wrapf(err, "error writing image protected TLV trailer"))
+ }
+
+ for _, t := range img.ProtTlvs {
+ err = binary.Write(f, binary.LittleEndian, t.Header)
+ if err != nil {
+ ImgmodUsage(nil, errors.Wrapf(err, "error writing image protected TLVs"))
+ }
+ _, err = f.Write(t.Data)
+ if err != nil {
+ ImgmodUsage(nil, errors.Wrapf(err, "error writing image protected TLVs"))
+ }
+ }
+ }
+
iutil.Printf("Wrote hashable content to %s\n", outFilename)
}
[mynewt-imgmod] 06/08: New command: image setbody
Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-imgmod.git
commit 14683ca04c0778be383806534ef46d6f8f65e977
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri Feb 28 16:26:41 2020 -0800
New command: image setbody
---
cli/image_cmds.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
iimg/iimg.go | 7 +++++++
2 files changed, 56 insertions(+)
diff --git a/cli/image_cmds.go b/cli/image_cmds.go
index 882277b..5aa1315 100644
--- a/cli/image_cmds.go
+++ b/cli/image_cmds.go
@@ -591,6 +591,42 @@ func runEncryptFullCmd(cmd *cobra.Command, args []string) {
}
}
+func runSetBodyCmd(cmd *cobra.Command, args []string) {
+ if len(args) < 2 {
+ ImgmodUsage(cmd, nil)
+ }
+
+ imgFilename := args[0]
+ bodyFilename := args[1]
+
+ if bodyFilename == "" {
+ ImgmodUsage(cmd, errors.Errorf("image body required"))
+ }
+ bodyBytes, err := ioutil.ReadFile(bodyFilename)
+ if err != nil {
+ ImgmodUsage(cmd, errors.Wrapf(err, "error reading image body file"))
+ }
+
+ outFilename, err := CalcOutFilename(imgFilename)
+ if err != nil {
+ ImgmodUsage(cmd, err)
+ }
+
+ img, err := readImage(imgFilename)
+ if err != nil {
+ ImgmodUsage(cmd, err)
+ }
+
+ img, err = iimg.ReplaceBody(img, bodyBytes)
+ if err != nil {
+ ImgmodUsage(nil, err)
+ }
+
+ if err := writeImage(img, outFilename); err != nil {
+ ImgmodUsage(nil, err)
+ }
+}
+
func runVerifyCmd(cmd *cobra.Command, args []string) {
if len(args) < 1 {
ImgmodUsage(cmd, nil)
@@ -820,6 +856,19 @@ func AddImageCommands(cmd *cobra.Command) {
imageCmd.AddCommand(encryptFullCmd)
+ setBodyCmd := &cobra.Command{
+ Use: "setbody <image> <body-filename>",
+ Short: "Replaces an image's body with a file's contents",
+ Run: runSetBodyCmd,
+ }
+
+ setBodyCmd.PersistentFlags().StringVarP(&OptOutFilename, "outfile", "o",
+ "", "File to write to")
+ setBodyCmd.PersistentFlags().BoolVarP(&OptInPlace, "inplace", "i", false,
+ "Replace input file")
+
+ imageCmd.AddCommand(setBodyCmd)
+
verifyCmd := &cobra.Command{
Use: "verify <image>",
Short: "Verifies an Mynewt image's integrity",
diff --git a/iimg/iimg.go b/iimg/iimg.go
index 9fc53fd..72e86b3 100644
--- a/iimg/iimg.go
+++ b/iimg/iimg.go
@@ -220,3 +220,10 @@ func EncryptImageFull(img image.Image,
return img, nil
}
+
+func ReplaceBody(img image.Image, body []byte) (image.Image, error) {
+ delta := len(body) - len(img.Body)
+ img.Body = body
+ img.Header.ImgSz += uint32(delta)
+ return img, nil
+}
[mynewt-imgmod] 05/08: New command: image decrypthw
Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-imgmod.git
commit ec8fdbbd990773f05b5f619eeb046310c7c2da20
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri Feb 28 16:26:10 2020 -0800
New command: image decrypthw
---
cli/image_cmds.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
iimg/iimg.go | 11 +++++++++++
2 files changed, 59 insertions(+)
diff --git a/cli/image_cmds.go b/cli/image_cmds.go
index a4ea984..882277b 100644
--- a/cli/image_cmds.go
+++ b/cli/image_cmds.go
@@ -492,6 +492,39 @@ func runDecryptFullCmd(cmd *cobra.Command, args []string) {
}
}
+func runDecryptHwCmd(cmd *cobra.Command, args []string) {
+ if len(args) < 2 {
+ ImgmodUsage(cmd, nil)
+ }
+
+ imgFilename := args[0]
+ secretFilename := args[1]
+
+ outFilename, err := CalcOutFilename(imgFilename)
+ if err != nil {
+ ImgmodUsage(cmd, err)
+ }
+
+ img, err := readImage(imgFilename)
+ if err != nil {
+ ImgmodUsage(cmd, err)
+ }
+
+ secretBytes, err := ioutil.ReadFile(secretFilename)
+ if err != nil {
+ ImgmodUsage(cmd, errors.Wrapf(err, "error reading secret file"))
+ }
+
+ img, err = iimg.DecryptImageHw(img, secretBytes)
+ if err != nil {
+ ImgmodUsage(nil, err)
+ }
+
+ if err := writeImage(img, outFilename); err != nil {
+ ImgmodUsage(nil, err)
+ }
+}
+
func runEncryptCmd(cmd *cobra.Command, args []string) {
if len(args) < 2 {
ImgmodUsage(cmd, nil)
@@ -733,6 +766,21 @@ func AddImageCommands(cmd *cobra.Command) {
Run: runDecryptFullCmd,
}
+ decryptHwCmd := &cobra.Command{
+ Use: "decrypthw <image> <aes-secret>",
+ Short: "Decrypts an hardware-encrypted Mynewt image file",
+ Long: "Decrypts the body of a hardware-encrypted Mynewt image file and " +
+ "removes the encryption TLVs. The aes-secret can be 64-encoded " +
+ "or raw.",
+ Run: runDecryptHwCmd,
+ }
+
+ decryptHwCmd.PersistentFlags().StringVarP(&OptOutFilename, "outfile", "o",
+ "", "File to write to")
+ decryptHwCmd.PersistentFlags().BoolVarP(&OptInPlace, "inplace", "i", false,
+ "Replace input file")
+ imageCmd.AddCommand(decryptHwCmd)
+
decryptFullCmd.PersistentFlags().StringVarP(&OptOutFilename, "outfile", "o",
"", "File to write to")
decryptFullCmd.PersistentFlags().BoolVarP(&OptInPlace, "inplace", "i", false,
diff --git a/iimg/iimg.go b/iimg/iimg.go
index d4e7c9a..9fc53fd 100644
--- a/iimg/iimg.go
+++ b/iimg/iimg.go
@@ -20,6 +20,7 @@
package iimg
import (
+ "encoding/base64"
"encoding/hex"
"fmt"
"strings"
@@ -181,6 +182,16 @@ func DecryptImageFull(img image.Image,
return img, nil
}
+func DecryptImageHw(img image.Image, secretBytes []byte) (image.Image, error) {
+ secret, err := base64.StdEncoding.DecodeString(string(secretBytes))
+ if err != nil {
+ // Not base64 encoded. Assume this is a raw AES secret.
+ secret = secretBytes
+ }
+
+ return image.DecryptHw(img, secret)
+}
+
func EncryptImage(img image.Image, pubKeBytes []byte) (image.Image, error) {
key, err := sec.ParsePubEncKey(pubKeBytes)
if err != nil {
[mynewt-imgmod] 04/08: image addsig: Allow sig type to be in
hexadecimal
Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-imgmod.git
commit 296fca75b9a0504469dd79b2749b1465dc88dd2d
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri Feb 28 16:24:28 2020 -0800
image addsig: Allow sig type to be in hexadecimal
---
cli/image_cmds.go | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/cli/image_cmds.go b/cli/image_cmds.go
index 9dcb171..a4ea984 100644
--- a/cli/image_cmds.go
+++ b/cli/image_cmds.go
@@ -372,9 +372,8 @@ func runAddsigCmd(cmd *cobra.Command, args []string) {
keyFilename := args[1]
sigFilename := args[2]
- sigType, err := strconv.Atoi(args[3])
- if err != nil || sigType < 0 || sigType > 255 ||
- !image.ImageTlvTypeIsSig(uint8(sigType)) {
+ sigType, err := strconv.ParseUint(args[3], 0, 8)
+ if err != nil || !image.ImageTlvTypeIsSig(uint8(sigType)) {
ImgmodUsage(cmd, errors.Errorf("invalid signature type: %s", args[3]))
}
[mynewt-imgmod] 07/08: New command: image extractbody
Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-imgmod.git
commit 0f5f9eaead163a6fa61fb4ed75613260b60a964b
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri Feb 28 16:27:31 2020 -0800
New command: image extractbody
---
cli/image_cmds.go | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/cli/image_cmds.go b/cli/image_cmds.go
index 5aa1315..6ef308e 100644
--- a/cli/image_cmds.go
+++ b/cli/image_cmds.go
@@ -674,6 +674,25 @@ func runVerifyCmd(cmd *cobra.Command, args []string) {
}
}
+func runExtractBodyCmd(cmd *cobra.Command, args []string) {
+ if len(args) < 1 {
+ ImgmodUsage(cmd, nil)
+ }
+
+ imgFilename := args[0]
+ outFilename := args[1]
+
+ img, err := readImage(imgFilename)
+ if err != nil {
+ ImgmodUsage(cmd, err)
+ }
+
+ err = WriteFile(img.Body, outFilename)
+ if err != nil {
+ ImgmodUsage(nil, err)
+ }
+}
+
func AddImageCommands(cmd *cobra.Command) {
imageCmd := &cobra.Command{
Use: "image",
@@ -883,4 +902,12 @@ func AddImageCommands(cmd *cobra.Command) {
"", "Manifest file")
imageCmd.AddCommand(verifyCmd)
+
+ extractBodyCmd := &cobra.Command{
+ Use: "extractbody <image> <out-file>",
+ Short: "Extracts a Mynewt image's body",
+ Run: runExtractBodyCmd,
+ }
+
+ imageCmd.AddCommand(extractBodyCmd)
}
[mynewt-imgmod] 02/08: Fix bad copy+paste in help text
Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-imgmod.git
commit c8887e4ee5ef34ca7e4554429783d12348e10705
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri Feb 28 16:28:10 2020 -0800
Fix bad copy+paste in help text
---
cli/image_cmds.go | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/cli/image_cmds.go b/cli/image_cmds.go
index 4decd4c..2b75b59 100644
--- a/cli/image_cmds.go
+++ b/cli/image_cmds.go
@@ -656,7 +656,7 @@ func AddImageCommands(cmd *cobra.Command) {
hashableCmd := &cobra.Command{
Use: "hashable <img-file>",
- Short: "Removes all signatures from a Mynewt image file",
+ Short: "Extracts an image's hashable content",
Run: runHashableCmd,
}
[mynewt-imgmod] 01/08: Pull in latest mynewt-artifact
Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-imgmod.git
commit fbb10f8f0c2b76060bbfacff64f415d825ee073b
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri Feb 28 16:49:12 2020 -0800
Pull in latest mynewt-artifact
---
go.mod | 2 +-
go.sum | 2 ++
2 files changed, 3 insertions(+), 1 deletion(-)
diff --git a/go.mod b/go.mod
index 4b623b9..ef1091a 100644
--- a/go.mod
+++ b/go.mod
@@ -1,7 +1,7 @@
module mynewt.apache.org/imgmod
require (
- github.com/apache/mynewt-artifact v0.0.15
+ github.com/apache/mynewt-artifact v0.0.17
github.com/otiai10/copy v1.0.1
github.com/pkg/errors v0.8.1
github.com/sirupsen/logrus v1.4.2
diff --git a/go.sum b/go.sum
index 03e1893..88014b7 100644
--- a/go.sum
+++ b/go.sum
@@ -9,6 +9,8 @@ github.com/apache/mynewt-artifact v0.0.3/go.mod h1:vFUd47t74KPQMzSBhQ2qp5Hc7D29O
github.com/apache/mynewt-artifact v0.0.9/go.mod h1:vFUd47t74KPQMzSBhQ2qp5Hc7D29OU/Tl3xHtFwN3k8=
github.com/apache/mynewt-artifact v0.0.15 h1:NOAdYAMjVBGVm/4iOs70+oQnb4StlD2tavkvwJiTbhI=
github.com/apache/mynewt-artifact v0.0.15/go.mod h1:vFUd47t74KPQMzSBhQ2qp5Hc7D29OU/Tl3xHtFwN3k8=
+github.com/apache/mynewt-artifact v0.0.17 h1:O1lqIEygow3vFHHF9PllPoBWIK51aV0RsXPxNVFgPXY=
+github.com/apache/mynewt-artifact v0.0.17/go.mod h1:vFUd47t74KPQMzSBhQ2qp5Hc7D29OU/Tl3xHtFwN3k8=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
[mynewt-imgmod] 08/08: New command: image rehash
Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-imgmod.git
commit fa5c27974afdf16ae53453446de0449439a11b2f
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri Feb 28 16:27:56 2020 -0800
New command: image rehash
---
cli/image_cmds.go | 40 ++++++++++++++++++++++++++++++++++++++++
iimg/iimg.go | 8 ++++----
2 files changed, 44 insertions(+), 4 deletions(-)
diff --git a/cli/image_cmds.go b/cli/image_cmds.go
index 6ef308e..13f2564 100644
--- a/cli/image_cmds.go
+++ b/cli/image_cmds.go
@@ -693,6 +693,33 @@ func runExtractBodyCmd(cmd *cobra.Command, args []string) {
}
}
+func runRehashImgCmd(cmd *cobra.Command, args []string) {
+ if len(args) < 1 {
+ ImgmodUsage(cmd, nil)
+ }
+
+ imgFilename := args[0]
+
+ outFilename, err := CalcOutFilename(imgFilename)
+ if err != nil {
+ ImgmodUsage(cmd, err)
+ }
+
+ img, err := readImage(imgFilename)
+ if err != nil {
+ ImgmodUsage(cmd, err)
+ }
+
+ img, err = iimg.RecalcHash(img)
+ if err != nil {
+ ImgmodUsage(nil, err)
+ }
+
+ if err := writeImage(img, outFilename); err != nil {
+ ImgmodUsage(nil, err)
+ }
+}
+
func AddImageCommands(cmd *cobra.Command) {
imageCmd := &cobra.Command{
Use: "image",
@@ -910,4 +937,17 @@ func AddImageCommands(cmd *cobra.Command) {
}
imageCmd.AddCommand(extractBodyCmd)
+
+ rehashImgCmd := &cobra.Command{
+ Use: "rehash <image>",
+ Short: "Calculates an image's hash and replaces its SHA256 TLV",
+ Run: runRehashImgCmd,
+ }
+
+ rehashImgCmd.PersistentFlags().StringVarP(&OptOutFilename, "outfile", "o",
+ "", "File to write to")
+ rehashImgCmd.PersistentFlags().BoolVarP(&OptInPlace, "inplace", "i", false,
+ "Replace input file")
+
+ imageCmd.AddCommand(rehashImgCmd)
}
diff --git a/iimg/iimg.go b/iimg/iimg.go
index 72e86b3..ea9d436 100644
--- a/iimg/iimg.go
+++ b/iimg/iimg.go
@@ -129,8 +129,8 @@ func ExtractSecret(img *image.Image) ([]byte, error) {
return tlvs[0].Data, nil
}
-func recalcHash(img image.Image) (image.Image, error) {
- hash, err := img.CalcHash()
+func RecalcHash(img image.Image) (image.Image, error) {
+ hash, err := img.CalcHash(nil)
if err != nil {
return img, err
}
@@ -174,7 +174,7 @@ func DecryptImageFull(img image.Image,
img.Header.Flags &^= image.IMAGE_F_ENCRYPTED
// The hash needs to be recalculated now that the header has changed.
- img, err = recalcHash(img)
+ img, err = RecalcHash(img)
if err != nil {
return img, err
}
@@ -208,7 +208,7 @@ func EncryptImageFull(img image.Image,
// The hash needs to be recalculated now that the header has changed.
var err error
- img, err = recalcHash(img)
+ img, err = RecalcHash(img)
if err != nil {
return img, err
}