|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module SSHData |
| 4 | + class Signature |
| 5 | + PEM_TYPE = "SSH SIGNATURE" |
| 6 | + SIGNATURE_PREAMBLE = "SSHSIG" |
| 7 | + MIN_SUPPORTED_VERSION = 1 |
| 8 | + MAX_SUPPORTED_VERSION = 1 |
| 9 | + |
| 10 | + # Spec: no SHA1 or SHA384. In practice, OpenSSH is always going to use SHA512. |
| 11 | + # Note the actual signing / verify primitive may use a different hash algorithm. |
| 12 | + # https://github.com/openssh/openssh-portable/blob/b7ffbb17e37f59249c31f1ff59d6c5d80888f689/PROTOCOL.sshsig#L67 |
| 13 | + SUPPORTED_HASH_ALGORITHMS = { |
| 14 | + "sha256" => OpenSSL::Digest::SHA256, |
| 15 | + "sha512" => OpenSSL::Digest::SHA512, |
| 16 | + } |
| 17 | + |
| 18 | + PERMITTED_RSA_SIGNATURE_ALGORITHMS = [ |
| 19 | + PublicKey::ALGO_RSA_SHA2_256, |
| 20 | + PublicKey::ALGO_RSA_SHA2_512, |
| 21 | + ] |
| 22 | + |
| 23 | + attr_reader :sigversion, :namespace, :signature, :reserved, :hash_algorithm |
| 24 | + |
| 25 | + # Parses a PEM armored SSH signature. |
| 26 | + # pem - A PEM encoded SSH signature. |
| 27 | + # |
| 28 | + # Returns a Signature instance. |
| 29 | + def self.parse_pem(pem) |
| 30 | + pem_type = Encoding.pem_type(pem) |
| 31 | + |
| 32 | + if pem_type != PEM_TYPE |
| 33 | + raise DecodeError, "Mismatched PEM type. Expecting '#{PEM_TYPE}', actually '#{pem_type}'." |
| 34 | + end |
| 35 | + |
| 36 | + blob = Encoding.decode_pem(pem, pem_type) |
| 37 | + self.parse_blob(blob) |
| 38 | + end |
| 39 | + |
| 40 | + def self.parse_blob(blob) |
| 41 | + data, read = Encoding.decode_openssh_signature(blob) |
| 42 | + |
| 43 | + if read != blob.bytesize |
| 44 | + raise DecodeError, "unexpected trailing data" |
| 45 | + end |
| 46 | + |
| 47 | + new(**data) |
| 48 | + end |
| 49 | + |
| 50 | + def initialize(sigversion:, publickey:, namespace:, reserved:, hash_algorithm:, signature:) |
| 51 | + if sigversion > MAX_SUPPORTED_VERSION || sigversion < MIN_SUPPORTED_VERSION |
| 52 | + raise UnsupportedError, "Signature version is not supported" |
| 53 | + end |
| 54 | + |
| 55 | + unless SUPPORTED_HASH_ALGORITHMS.has_key?(hash_algorithm) |
| 56 | + raise UnsupportedError, "Hash algorithm #{hash_algorithm} is not supported." |
| 57 | + end |
| 58 | + |
| 59 | + # Spec: empty namespaces are not permitted. |
| 60 | + # https://github.com/openssh/openssh-portable/blob/b7ffbb17e37f59249c31f1ff59d6c5d80888f689/PROTOCOL.sshsig#L57 |
| 61 | + raise UnsupportedError, "A namespace is required." if namespace.empty? |
| 62 | + |
| 63 | + # Spec: ignore 'reserved', don't need to validate that it is empty. |
| 64 | + |
| 65 | + @sigversion = sigversion |
| 66 | + @publickey = publickey |
| 67 | + @namespace = namespace |
| 68 | + @reserved = reserved |
| 69 | + @hash_algorithm = hash_algorithm |
| 70 | + @signature = signature |
| 71 | + end |
| 72 | + |
| 73 | + def verify(signed_data, **opts) |
| 74 | + signing_key = public_key |
| 75 | + |
| 76 | + # Unwrap the signing key if this signature was created from a certificate. |
| 77 | + key = signing_key.is_a?(Certificate) ? signing_key.public_key : signing_key |
| 78 | + |
| 79 | + digest_algorithm = SUPPORTED_HASH_ALGORITHMS[@hash_algorithm] |
| 80 | + |
| 81 | + if key.is_a?(PublicKey::RSA) |
| 82 | + sig_algo, * = Encoding.decode_signature(@signature) |
| 83 | + |
| 84 | + # Spec: If the signature is an RSA signature, the legacy 'ssh-rsa' |
| 85 | + # identifer is not permitted. |
| 86 | + # https://github.com/openssh/openssh-portable/blob/b7ffbb17e37f59249c31f1ff59d6c5d80888f689/PROTOCOL.sshsig#L72 |
| 87 | + unless PERMITTED_RSA_SIGNATURE_ALGORITHMS.include?(sig_algo) |
| 88 | + raise UnsupportedError, "RSA signature #{sig_algo} is not supported." |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + message_digest = digest_algorithm.digest(signed_data) |
| 93 | + blob = |
| 94 | + SIGNATURE_PREAMBLE + |
| 95 | + Encoding.encode_string(@namespace) + |
| 96 | + Encoding.encode_string(@reserved || "") + |
| 97 | + Encoding.encode_string(@hash_algorithm) + |
| 98 | + Encoding.encode_string(message_digest) |
| 99 | + |
| 100 | + if key.class.include?(::SSHData::PublicKey::SecurityKey) |
| 101 | + key.verify(blob, @signature, **opts) |
| 102 | + else |
| 103 | + key.verify(blob, @signature) |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + # Gets the public key from the signature. |
| 108 | + # If the signature was created from a certificate, this will be an |
| 109 | + # SSHData::Certificate. Otherwise, this will be a PublicKey algorithm. |
| 110 | + def public_key |
| 111 | + public_key_algorithm, _ = Encoding.decode_string(@publickey) |
| 112 | + |
| 113 | + if PublicKey::ALGOS.include?(public_key_algorithm) |
| 114 | + PublicKey.parse_rfc4253(@publickey) |
| 115 | + elsif Certificate::ALGOS.include?(public_key_algorithm) |
| 116 | + Certificate.parse_rfc4253(@publickey) |
| 117 | + else |
| 118 | + raise UnsupportedError, "Public key algorithm #{public_key_algorithm} is not supported." |
| 119 | + end |
| 120 | + end |
| 121 | + end |
| 122 | +end |
0 commit comments