Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1 | <!-- |
| 2 | # |
| 3 | # Licensed to the Apache Software Foundation (ASF) under one |
| 4 | # or more contributor license agreements. See the NOTICE file |
| 5 | # distributed with this work for additional information |
| 6 | # regarding copyright ownership. The ASF licenses this file |
| 7 | # to you under the Apache License, Version 2.0 (the |
| 8 | # "License"); you may not use this file except in compliance |
| 9 | # with the License. You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, |
| 14 | # software distributed under the License is distributed on an |
| 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | # KIND, either express or implied. See the License for the |
| 17 | # specific language governing permissions and limitations |
| 18 | # under the License. |
| 19 | # |
| 20 | --> |
| 21 | |
| 22 | ## Image signing |
| 23 | |
| 24 | This signs the image by computing hash over the image, and then |
| 25 | signing that hash. Signature is computed by newt tool when it's |
| 26 | creating the image. This signature is placed in the image trailer. |
| 27 | |
| 28 | The public key of this keypair must be included in the bootloader, |
| 29 | as it verifies it before allowing the image to run. |
| 30 | |
| 31 | This facility allows you to use multiple signing keys. This would |
| 32 | be useful when you want to prevent production units from booting |
| 33 | development images, but want development units to be able to boot |
| 34 | both production images and development images. |
| 35 | |
| 36 | ## Creating signing keys |
| 37 | First you need a keypair to use for signing. You can create |
| 38 | one with openssl command line tool. |
| 39 | |
| 40 | openssl genrsa -out image_sign.pem 2048 |
| 41 | |
| 42 | This created a file which contains both the private and public key, |
| 43 | and will be used when signing images. |
| 44 | |
| 45 | Then you need to extract the public key from this to include it |
| 46 | in the bootloader. Bootloader need to keep key parsing minimal, |
| 47 | so it expects simple key format. |
| 48 | |
| 49 | openssl rsa -in image_sign.pem -pubout -out image_sign_pub.der -outform DER -RSAPublicKey_out |
| 50 | |
| 51 | Now the public key is in file called image_sign_pub.der. |
| 52 | |
Marko Kiiskila | 8eeba12 | 2016-12-29 17:38:54 -0800 | [diff] [blame] | 53 | For ECDSA224 these commands are similar. |
Marko Kiiskila | 919eaf4 | 2016-12-28 17:39:45 -0800 | [diff] [blame] | 54 | |
| 55 | openssl ecparam -name secp224r1 -genkey -noout -out image_sign.pem |
| 56 | openssl ec -in image_sign.pem -pubout -outform DER -out image_sign_pub.der |
| 57 | |
Marko Kiiskila | 8eeba12 | 2016-12-29 17:38:54 -0800 | [diff] [blame] | 58 | And then the ECDSA256. |
| 59 | openssl ecparam -name prime256v1 -genkey -noout -out image_sign.pem |
| 60 | openssl ec -in image_sign.pem -pubout -outform DER -out image_sign_pub.der |
| 61 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 62 | ## Creating a key package |
| 63 | |
| 64 | xxd -i image_sign_pub.der image_sign_pub.c.import |
| 65 | |
| 66 | Then you need to create a package containing this key, or keys. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 67 | |
| 68 | ## Sample pkg.yml |
| 69 | This gets bootutil to turn on image signature validation. |
| 70 | |
| 71 | pkg.name: libs/mykeys |
| 72 | pkg.deps: |
Marko Kiiskila | bf986da | 2016-12-13 17:15:24 -0800 | [diff] [blame] | 73 | - "@apache-mynewt-core/boot/bootutil" |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 74 | |
| 75 | ## Sample source file |
| 76 | This exports the keys. |
| 77 | |
| 78 | #include <bootutil/sign_key.h> |
| 79 | |
| 80 | #include "image_sign_pub.c.import" |
| 81 | |
| 82 | const struct bootutil_key bootutil_keys[] = { |
| 83 | [0] = { |
| 84 | .key = image_sign_pub_der, |
| 85 | .len = &image_sign_pub_der_len, |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | const int bootutil_key_cnt = sizeof(bootutil_keys) / sizeof(bootutil_keys[0]); |
| 90 | |
| 91 | ## Building bootloader |
| 92 | |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame^] | 93 | Enable the BOOTUTIL_SIGN_RSA syscfg setting in your app or target syscfg.yml |
| 94 | file |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 95 | |
| 96 | syscfg.vals: |
Fabio Utzig | 32d68f0 | 2017-07-25 22:05:38 -0300 | [diff] [blame] | 97 | BOOTUTIL_SIGN_RSA: 1 |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 98 | |
| 99 | After you've created the key package, you must include it in the build |
| 100 | for bootloader. So modify the pkg.yml for apps/boot to include it. |
Marko Kiiskila | 919eaf4 | 2016-12-28 17:39:45 -0800 | [diff] [blame] | 101 | |
Fabio Utzig | 32d68f0 | 2017-07-25 22:05:38 -0300 | [diff] [blame] | 102 | The syscfg variable to enable ECDSA224 is BOOTUTIL_SIGN_EC, and |
| 103 | BOOTUTIL_SIGN_EC256 for ECDS256. |