Skip to content

Commit 6f28677

Browse files
utzigd3zd3z
authored andcommitted
imgtool: add option to export public PEM
Update `getpub` with new `lang` option, "pem", which allows exporting a public key as a PEM file. This can later be distributed to be used for encrypting an image, and gets away with having to use openssl for this step. Signed-off-by: Fabio Utzig <[email protected]>
1 parent 3fd4cd4 commit 6f28677

File tree

6 files changed

+23
-2
lines changed

6 files changed

+23
-2
lines changed

docs/encrypted_images.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ or `ed25519`. This will generate a keypair or private key.
153153

154154
To extract the public key in source file form, use
155155
`imgtool getpub -k <input.pem> -l <lang>`, where lang can be one of `c` or
156-
`rust` (defaults to `c`).
156+
`rust` (defaults to `c`). To extract a public key in PEM form, use
157+
`imgtool getpub -k <input.pem> -l pem`.
157158

158159
If using AES-KW, follow the steps in the next section to generate the
159160
required keys.

scripts/imgtool/keys/ecdsa.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ def get_public_bytes(self):
3333
encoding=serialization.Encoding.DER,
3434
format=serialization.PublicFormat.SubjectPublicKeyInfo)
3535

36+
def get_public_pem(self):
37+
return self._get_public().public_bytes(
38+
encoding=serialization.Encoding.PEM,
39+
format=serialization.PublicFormat.SubjectPublicKeyInfo)
40+
3641
def get_private_bytes(self, minimal):
3742
self._unsupported('get_private_bytes')
3843

scripts/imgtool/keys/general.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ def emit_rust_public(self, file=sys.stdout):
3737
indent=" ",
3838
file=file)
3939

40+
def emit_public_pem(self, file=sys.stdout):
41+
print(str(self.get_public_pem(), 'utf-8'), file=file, end='')
42+
4043
def emit_private(self, minimal, file=sys.stdout):
4144
self._emit(
4245
header="const unsigned char enc_priv_key[] = {",

scripts/imgtool/keys/rsa.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ def get_public_bytes(self):
4444
encoding=serialization.Encoding.DER,
4545
format=serialization.PublicFormat.PKCS1)
4646

47+
def get_public_pem(self):
48+
return self._get_public().public_bytes(
49+
encoding=serialization.Encoding.PEM,
50+
format=serialization.PublicFormat.SubjectPublicKeyInfo)
51+
4752
def get_private_bytes(self, minimal):
4853
self._unsupported('get_private_bytes')
4954

scripts/imgtool/keys/x25519.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ def get_public_bytes(self):
3434
encoding=serialization.Encoding.DER,
3535
format=serialization.PublicFormat.SubjectPublicKeyInfo)
3636

37+
def get_public_pem(self):
38+
return self._get_public().public_bytes(
39+
encoding=serialization.Encoding.PEM,
40+
format=serialization.PublicFormat.SubjectPublicKeyInfo)
41+
3742
def get_private_bytes(self, minimal):
3843
self._unsupported('get_private_bytes')
3944

scripts/imgtool/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def gen_x25519(keyfile, passwd):
5959
keys.X25519.generate().export_private(path=keyfile, passwd=passwd)
6060

6161

62-
valid_langs = ['c', 'rust']
62+
valid_langs = ['c', 'rust', 'pem']
6363
keygens = {
6464
'rsa-2048': gen_rsa2048,
6565
'rsa-3072': gen_rsa3072,
@@ -125,6 +125,8 @@ def getpub(key, lang):
125125
key.emit_c_public()
126126
elif lang == 'rust':
127127
key.emit_rust_public()
128+
elif lang == 'pem':
129+
key.emit_public_pem()
128130
else:
129131
raise ValueError("BUG: should never get here!")
130132

0 commit comments

Comments
 (0)