2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-21 06:30:03 -04:00
|
|
|
module Gitlab
|
|
|
|
class SSHPublicKey
|
2022-01-12 10:13:54 -05:00
|
|
|
Technology = Struct.new(:name, :key_class, :supported_sizes, :supported_algorithms)
|
2017-08-21 06:30:03 -04:00
|
|
|
|
2022-01-12 10:13:54 -05:00
|
|
|
# See https://man.openbsd.org/sshd#AUTHORIZED_KEYS_FILE_FORMAT for the list of
|
|
|
|
# supported algorithms.
|
2018-12-09 12:45:48 -05:00
|
|
|
TECHNOLOGIES = [
|
2022-01-20 13:14:18 -05:00
|
|
|
Technology.new(:rsa, SSHData::PublicKey::RSA, [1024, 2048, 3072, 4096], %w(ssh-rsa)),
|
|
|
|
Technology.new(:dsa, SSHData::PublicKey::DSA, [1024, 2048, 3072], %w(ssh-dss)),
|
|
|
|
Technology.new(:ecdsa, SSHData::PublicKey::ECDSA, [256, 384, 521], %w(ecdsa-sha2-nistp256 ecdsa-sha2-nistp384 ecdsa-sha2-nistp521)),
|
2022-02-04 01:15:28 -05:00
|
|
|
Technology.new(:ed25519, SSHData::PublicKey::ED25519, [256], %w(ssh-ed25519)),
|
|
|
|
Technology.new(:ecdsa_sk, SSHData::PublicKey::SKECDSA, [256], %w(sk-ecdsa-sha2-nistp256@openssh.com)),
|
|
|
|
Technology.new(:ed25519_sk, SSHData::PublicKey::SKED25519, [256], %w(sk-ssh-ed25519@openssh.com))
|
2017-08-21 06:30:03 -04:00
|
|
|
].freeze
|
|
|
|
|
2022-03-28 11:07:51 -04:00
|
|
|
def self.technologies
|
|
|
|
if Gitlab::FIPS.enabled?
|
|
|
|
Gitlab::FIPS::SSH_KEY_TECHNOLOGIES
|
|
|
|
else
|
|
|
|
TECHNOLOGIES
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-21 06:30:03 -04:00
|
|
|
def self.technology(name)
|
2022-03-28 11:07:51 -04:00
|
|
|
technologies.find { |tech| tech.name.to_s == name.to_s }
|
2017-08-21 06:30:03 -04:00
|
|
|
end
|
|
|
|
|
2017-08-28 16:33:35 -04:00
|
|
|
def self.technology_for_key(key)
|
2022-03-28 11:07:51 -04:00
|
|
|
technologies.find { |tech| key.instance_of?(tech.key_class) }
|
2017-08-28 16:33:35 -04:00
|
|
|
end
|
|
|
|
|
2021-12-30 07:14:01 -05:00
|
|
|
def self.supported_types
|
2022-03-28 11:07:51 -04:00
|
|
|
technologies.map(&:name)
|
2021-12-30 07:14:01 -05:00
|
|
|
end
|
|
|
|
|
2017-08-25 09:08:48 -04:00
|
|
|
def self.supported_sizes(name)
|
2022-01-12 10:13:54 -05:00
|
|
|
technology(name).supported_sizes
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.supported_algorithms
|
2022-03-28 11:07:51 -04:00
|
|
|
technologies.flat_map { |tech| tech.supported_algorithms }
|
2022-01-12 10:13:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.supported_algorithms_for_name(name)
|
|
|
|
technology(name).supported_algorithms
|
2017-08-21 06:30:03 -04:00
|
|
|
end
|
|
|
|
|
2018-02-15 09:50:19 -05:00
|
|
|
def self.sanitize(key_content)
|
|
|
|
ssh_type, *parts = key_content.strip.split
|
|
|
|
|
|
|
|
return key_content if parts.empty?
|
|
|
|
|
2018-10-22 03:00:50 -04:00
|
|
|
parts.each_with_object(+"#{ssh_type} ").with_index do |(part, content), index|
|
2018-02-15 09:50:19 -05:00
|
|
|
content << part
|
|
|
|
|
2022-01-20 13:14:18 -05:00
|
|
|
if self.new(content).valid?
|
2018-02-15 09:50:19 -05:00
|
|
|
break [content, parts[index + 1]].compact.join(' ') # Add the comment part if present
|
|
|
|
elsif parts.size == index + 1 # return original content if we've reached the last element
|
|
|
|
break key_content
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-21 06:30:03 -04:00
|
|
|
attr_reader :key_text, :key
|
|
|
|
|
|
|
|
def initialize(key_text)
|
|
|
|
@key_text = key_text
|
|
|
|
|
2022-01-20 13:14:18 -05:00
|
|
|
# We need to strip options to parse key with options or in known_hosts
|
|
|
|
# format. See https://man.openbsd.org/sshd#AUTHORIZED_KEYS_FILE_FORMAT
|
|
|
|
# and https://man.openbsd.org/sshd#SSH_KNOWN_HOSTS_FILE_FORMAT
|
|
|
|
key_text_without_options = @key_text.to_s.match(/(\A|\s)(#{self.class.supported_algorithms.join('|')}).*/).to_s
|
|
|
|
|
2017-08-21 06:30:03 -04:00
|
|
|
@key =
|
|
|
|
begin
|
2022-01-20 13:14:18 -05:00
|
|
|
SSHData::PublicKey.parse_openssh(key_text_without_options)
|
|
|
|
rescue SSHData::DecodeError
|
2017-08-21 06:30:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid?
|
2022-01-20 13:14:18 -05:00
|
|
|
key.present?
|
2017-08-21 06:30:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def type
|
2022-01-20 13:14:18 -05:00
|
|
|
technology.name if valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def fingerprint
|
|
|
|
key.fingerprint(md5: true) if valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def fingerprint_sha256
|
|
|
|
'SHA256:' + key.fingerprint(md5: false) if valid?
|
2017-08-21 06:30:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def bits
|
2022-01-20 13:14:18 -05:00
|
|
|
return unless valid?
|
2017-08-21 06:30:03 -04:00
|
|
|
|
|
|
|
case type
|
|
|
|
when :rsa
|
2022-01-20 13:14:18 -05:00
|
|
|
key.n.num_bits
|
2017-08-21 06:30:03 -04:00
|
|
|
when :dsa
|
2022-01-20 13:14:18 -05:00
|
|
|
key.p.num_bits
|
2017-08-21 06:30:03 -04:00
|
|
|
when :ecdsa
|
2022-01-20 13:14:18 -05:00
|
|
|
key.openssl.group.order.num_bits
|
2017-08-21 06:30:03 -04:00
|
|
|
when :ed25519
|
|
|
|
256
|
2022-02-04 01:15:28 -05:00
|
|
|
when :ecdsa_sk
|
|
|
|
256
|
|
|
|
when :ed25519_sk
|
|
|
|
256
|
2017-08-21 06:30:03 -04:00
|
|
|
end
|
|
|
|
end
|
2017-08-25 09:08:48 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def technology
|
|
|
|
@technology ||=
|
2022-01-20 13:14:18 -05:00
|
|
|
self.class.technology_for_key(key) || raise_unsupported_key_type_error
|
|
|
|
end
|
|
|
|
|
|
|
|
def raise_unsupported_key_type_error
|
|
|
|
raise("Unsupported key type: #{key.class}")
|
2017-08-25 09:08:48 -04:00
|
|
|
end
|
2017-08-21 06:30:03 -04:00
|
|
|
end
|
|
|
|
end
|