Skip to content

Commit 2adb817

Browse files
committed
ssh-tpm-keygen: guard against invalid bit lengths a bit better
Signed-off-by: Morten Linderud <[email protected]>
1 parent 93c8703 commit 2adb817

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

cmd/ssh-tpm-keygen/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"os"
1414
"os/user"
1515
"path"
16+
"slices"
1617
"strings"
1718
"syscall"
1819

@@ -141,6 +142,8 @@ func main() {
141142
}
142143
defer tpm.Close()
143144

145+
supportedECCBitsizes := key.SupportedECCAlgorithms(tpm)
146+
144147
if listsupported {
145148
fmt.Printf("ecdsa bit lengths:")
146149
for _, alg := range key.SupportedECCAlgorithms(tpm) {
@@ -207,6 +210,11 @@ func main() {
207210
case "ecdsa":
208211
tpmkeyType = tpm2.TPMAlgECC
209212
filename = "id_ecdsa"
213+
214+
if !slices.Contains(supportedECCBitsizes, bits) {
215+
log.Fatalf("invalid ecdsa key length: TPM does not support %v bits", bits)
216+
}
217+
210218
case "rsa":
211219
tpmkeyType = tpm2.TPMAlgRSA
212220
filename = "id_rsa"
@@ -257,6 +265,9 @@ func main() {
257265
switch key := rawKey.(type) {
258266
case *ecdsa.PrivateKey:
259267
toImportKey = *key
268+
if !slices.Contains(supportedECCBitsizes, key.Params().BitSize) {
269+
log.Fatalf("invalid ecdsa key length: TPM does not support %v bits", key.Params().BitSize)
270+
}
260271
case *rsa.PrivateKey:
261272
if key.N.BitLen() != 2048 {
262273
log.Fatal("can only support 2048 bit RSA")

0 commit comments

Comments
 (0)