initremote: Avoid creating a remote that is not encrypted when gpg is broken

checksize was applied lazily, so the exception didn't happen until the
remote was set up.

Sponsored-by: k0ld on Patreon
This commit is contained in:
Joey Hess 2023-05-01 13:00:05 -04:00
parent 23719bc91b
commit 57c1b4f5e5
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
3 changed files with 11 additions and 4 deletions

View file

@ -302,7 +302,10 @@ genSecretKey (GpgCmd cmd) keytype passphrase userid keysize =
- It is armored, to avoid newlines, since gpg only reads ciphers up to the
- first newline. -}
genRandom :: GpgCmd -> Bool -> Size -> IO String
genRandom cmd highQuality size = checksize <$> readStrict cmd params
genRandom cmd highQuality size = do
s <- readStrict cmd params
checksize s
return s
where
params =
[ Param "--gen-random"
@ -325,9 +328,8 @@ genRandom cmd highQuality size = checksize <$> readStrict cmd params
expectedlength = size * 8 `div` 6
checksize s = let len = length s in
if len >= expectedlength
then s
else shortread len
unless (len >= expectedlength) $
shortread len
shortread got = giveup $ unwords
[ "Not enough bytes returned from gpg", show params