You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2016/04/28 02:09:42 UTC

[15/32] incubator-mynewt-newt git commit: Support multiple keys for image signing.

Support multiple keys for image signing.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/546cec1f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/546cec1f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/546cec1f

Branch: refs/heads/master
Commit: 546cec1f14bda4714218930f07f4ecc83a32ca2e
Parents: bb2b7fb
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Mon Apr 4 12:05:05 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Apr 4 12:05:05 2016 -0700

----------------------------------------------------------------------
 newt/cli/image_cmds.go | 13 ++++++++++++-
 newt/image/image.go    | 19 ++++++++++++++-----
 2 files changed, 26 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/546cec1f/newt/cli/image_cmds.go
----------------------------------------------------------------------
diff --git a/newt/cli/image_cmds.go b/newt/cli/image_cmds.go
index 249577a..5057486 100644
--- a/newt/cli/image_cmds.go
+++ b/newt/cli/image_cmds.go
@@ -20,6 +20,8 @@
 package cli
 
 import (
+	"strconv"
+
 	"github.com/spf13/cobra"
 	"mynewt.apache.org/newt/newt/builder"
 	"mynewt.apache.org/newt/newt/image"
@@ -65,7 +67,16 @@ func createImageRunCmd(cmd *cobra.Command, args []string) {
 	}
 
 	if len(args) > 2 {
-		err = image.SetSigningKey(args[2])
+		var keyId uint8 = 0
+		if len(args) > 3 {
+			keyId64, err := strconv.ParseUint(args[3], 10, 8)
+			if err != nil {
+				NewtUsage(cmd,
+					util.NewNewtError("Key ID must be between 0-255"))
+			}
+			keyId = uint8(keyId64)
+		}
+		err = image.SetSigningKey(args[2], keyId)
 		if err != nil {
 			NewtUsage(cmd, err)
 		}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/546cec1f/newt/image/image.go
----------------------------------------------------------------------
diff --git a/newt/image/image.go b/newt/image/image.go
index b466f76..75f3216 100644
--- a/newt/image/image.go
+++ b/newt/image/image.go
@@ -61,17 +61,21 @@ type Image struct {
 	manifestFile string
 	version      ImageVersion
 	signingKey   *rsa.PrivateKey
+	keyId        uint8
 	hash         []byte
 }
 
 type ImageHdr struct {
 	Magic uint32
-	TlvSz uint32
-	HdrSz uint32
+	TlvSz uint16
+	KeyId uint8
+	Pad1  uint8
+	HdrSz uint16
+	Pad2  uint16
 	ImgSz uint32
 	Flags uint32
 	Vers  ImageVersion
-	Pad   uint32
+	Pad3  uint32
 }
 
 type ImageTrailerTlv struct {
@@ -192,7 +196,7 @@ func (image *Image) SetVersion(versStr string) error {
 	return nil
 }
 
-func (image *Image) SetSigningKey(fileName string) error {
+func (image *Image) SetSigningKey(fileName string, keyId uint8) error {
 	data, err := ioutil.ReadFile(fileName)
 	if err != nil {
 		return util.NewNewtError(fmt.Sprintf("Error reading key file: %s", err))
@@ -213,6 +217,7 @@ func (image *Image) SetSigningKey(fileName string) error {
 			err))
 	}
 	image.signingKey = privateKey
+	image.keyId = keyId
 
 	return nil
 }
@@ -250,15 +255,19 @@ func (image *Image) Generate() error {
 	hdr := &ImageHdr{
 		Magic: IMAGE_MAGIC,
 		TlvSz: 0,
+		KeyId: 0,
+		Pad1:  0,
 		HdrSz: IMAGE_HEADER_SIZE,
+		Pad2:  0,
 		ImgSz: uint32(binInfo.Size()),
 		Flags: 0,
 		Vers:  image.version,
-		Pad:   0,
+		Pad3:  0,
 	}
 	if image.signingKey != nil {
 		hdr.TlvSz = 4 + 256
 		hdr.Flags = IMAGE_F_PKCS15_RSA2048_SHA256
+		hdr.KeyId = image.keyId
 	} else {
 		hdr.TlvSz = 4 + 32
 		hdr.Flags = IMAGE_F_SHA256