Skip to content

Commit 5f3cbdf

Browse files
committed
Code review feedback.
* Move some things in to a more specific module for security keys. * Comment on the security key defaults. * Rework code so that flag checks are independent for security keys.
1 parent 4233196 commit 5f3cbdf

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

lib/ssh_data/public_key.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@ module PublicKey
2020
ALGO_ED25519, ALGO_SKECDSA256, ALGO_SKED25519
2121
]
2222

23-
DEFAULT_SK_VERIFY_OPTS = {
24-
user_presence_required: true,
25-
user_verification_required: false
26-
}
27-
28-
SK_FLAG_USER_PRESENCE = 0b001
29-
SK_FLAG_USER_VERIFICATION = 0b100
30-
3123
# Parse an OpenSSH public key in authorized_keys format (see sshd(8) manual
3224
# page).
3325
#

lib/ssh_data/public_key/security_key.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
module SSHData
22
module PublicKey
33
module SecurityKey
4+
5+
# Defaults to match OpenSSH, user presence is required by verification is not.
6+
DEFAULT_SK_VERIFY_OPTS = {
7+
user_presence_required: true,
8+
user_verification_required: false
9+
}
10+
11+
SK_FLAG_USER_PRESENCE = 0b001
12+
SK_FLAG_USER_VERIFICATION = 0b100
13+
414
def build_signing_blob(application, signed_data, signature)
515
read = 0
616
sig_algo, raw_sig, signature_read = Encoding.decode_signature(signature)

lib/ssh_data/public_key/skecdsa.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,12 @@ def verify(signed_data, signature, **opts)
4848

4949
result = openssl.verify(digest.new, openssl_sig, blob)
5050

51-
if opts[:user_presence_required] && (sk_flags & SK_FLAG_USER_PRESENCE != SK_FLAG_USER_PRESENCE)
52-
false
53-
elsif opts[:user_verification_required] && (sk_flags & SK_FLAG_USER_VERIFICATION != SK_FLAG_USER_VERIFICATION)
54-
false
55-
else
56-
result
57-
end
51+
# We don't know that the flags are correct until after we've validated the signature
52+
# which embeds the flags, so always verify the signature first.
53+
return false if opts[:user_presence_required] && (sk_flags & SK_FLAG_USER_PRESENCE != SK_FLAG_USER_PRESENCE)
54+
return false if opts[:user_verification_required] && (sk_flags & SK_FLAG_USER_VERIFICATION != SK_FLAG_USER_VERIFICATION)
55+
56+
result
5857
end
5958

6059
def ==(other)

lib/ssh_data/public_key/sked25519.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,12 @@ def verify(signed_data, signature, **opts)
4242
false
4343
end
4444

45-
if opts[:user_presence_required] && (sk_flags & SK_FLAG_USER_PRESENCE != SK_FLAG_USER_PRESENCE)
46-
false
47-
elsif opts[:user_verification_required] && (sk_flags & SK_FLAG_USER_VERIFICATION != SK_FLAG_USER_VERIFICATION)
48-
false
49-
else
50-
result
51-
end
45+
# We don't know that the flags are correct until after we've validated the signature
46+
# which embeds the flags, so always verify the signature first.
47+
return false if opts[:user_presence_required] && (sk_flags & SK_FLAG_USER_PRESENCE != SK_FLAG_USER_PRESENCE)
48+
return false if opts[:user_verification_required] && (sk_flags & SK_FLAG_USER_VERIFICATION != SK_FLAG_USER_VERIFICATION)
49+
50+
result
5251
end
5352

5453
def ==(other)

0 commit comments

Comments
 (0)