You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

546 lines
12 KiB

  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package knownhosts implements a parser for the OpenSSH
  5. // known_hosts host key database.
  6. package knownhosts
  7. import (
  8. "bufio"
  9. "bytes"
  10. "crypto/hmac"
  11. "crypto/rand"
  12. "crypto/sha1"
  13. "encoding/base64"
  14. "errors"
  15. "fmt"
  16. "io"
  17. "net"
  18. "os"
  19. "strings"
  20. "golang.org/x/crypto/ssh"
  21. )
  22. // See the sshd manpage
  23. // (http://man.openbsd.org/sshd#SSH_KNOWN_HOSTS_FILE_FORMAT) for
  24. // background.
  25. type addr struct{ host, port string }
  26. func (a *addr) String() string {
  27. h := a.host
  28. if strings.Contains(h, ":") {
  29. h = "[" + h + "]"
  30. }
  31. return h + ":" + a.port
  32. }
  33. type matcher interface {
  34. match([]addr) bool
  35. }
  36. type hostPattern struct {
  37. negate bool
  38. addr addr
  39. }
  40. func (p *hostPattern) String() string {
  41. n := ""
  42. if p.negate {
  43. n = "!"
  44. }
  45. return n + p.addr.String()
  46. }
  47. type hostPatterns []hostPattern
  48. func (ps hostPatterns) match(addrs []addr) bool {
  49. matched := false
  50. for _, p := range ps {
  51. for _, a := range addrs {
  52. m := p.match(a)
  53. if !m {
  54. continue
  55. }
  56. if p.negate {
  57. return false
  58. }
  59. matched = true
  60. }
  61. }
  62. return matched
  63. }
  64. // See
  65. // https://android.googlesource.com/platform/external/openssh/+/ab28f5495c85297e7a597c1ba62e996416da7c7e/addrmatch.c
  66. // The matching of * has no regard for separators, unlike filesystem globs
  67. func wildcardMatch(pat []byte, str []byte) bool {
  68. for {
  69. if len(pat) == 0 {
  70. return len(str) == 0
  71. }
  72. if len(str) == 0 {
  73. return false
  74. }
  75. if pat[0] == '*' {
  76. if len(pat) == 1 {
  77. return true
  78. }
  79. for j := range str {
  80. if wildcardMatch(pat[1:], str[j:]) {
  81. return true
  82. }
  83. }
  84. return false
  85. }
  86. if pat[0] == '?' || pat[0] == str[0] {
  87. pat = pat[1:]
  88. str = str[1:]
  89. } else {
  90. return false
  91. }
  92. }
  93. }
  94. func (p *hostPattern) match(a addr) bool {
  95. return wildcardMatch([]byte(p.addr.host), []byte(a.host)) && p.addr.port == a.port
  96. }
  97. type keyDBLine struct {
  98. cert bool
  99. matcher matcher
  100. knownKey KnownKey
  101. }
  102. func serialize(k ssh.PublicKey) string {
  103. return k.Type() + " " + base64.StdEncoding.EncodeToString(k.Marshal())
  104. }
  105. func (l *keyDBLine) match(addrs []addr) bool {
  106. return l.matcher.match(addrs)
  107. }
  108. type hostKeyDB struct {
  109. // Serialized version of revoked keys
  110. revoked map[string]*KnownKey
  111. lines []keyDBLine
  112. }
  113. func newHostKeyDB() *hostKeyDB {
  114. db := &hostKeyDB{
  115. revoked: make(map[string]*KnownKey),
  116. }
  117. return db
  118. }
  119. func keyEq(a, b ssh.PublicKey) bool {
  120. return bytes.Equal(a.Marshal(), b.Marshal())
  121. }
  122. // IsAuthorityForHost can be used as a callback in ssh.CertChecker
  123. func (db *hostKeyDB) IsHostAuthority(remote ssh.PublicKey, address string) bool {
  124. h, p, err := net.SplitHostPort(address)
  125. if err != nil {
  126. return false
  127. }
  128. a := addr{host: h, port: p}
  129. for _, l := range db.lines {
  130. if l.cert && keyEq(l.knownKey.Key, remote) && l.match([]addr{a}) {
  131. return true
  132. }
  133. }
  134. return false
  135. }
  136. // IsRevoked can be used as a callback in ssh.CertChecker
  137. func (db *hostKeyDB) IsRevoked(key *ssh.Certificate) bool {
  138. _, ok := db.revoked[string(key.Marshal())]
  139. return ok
  140. }
  141. const markerCert = "@cert-authority"
  142. const markerRevoked = "@revoked"
  143. func nextWord(line []byte) (string, []byte) {
  144. i := bytes.IndexAny(line, "\t ")
  145. if i == -1 {
  146. return string(line), nil
  147. }
  148. return string(line[:i]), bytes.TrimSpace(line[i:])
  149. }
  150. func parseLine(line []byte) (marker, host string, key ssh.PublicKey, err error) {
  151. if w, next := nextWord(line); w == markerCert || w == markerRevoked {
  152. marker = w
  153. line = next
  154. }
  155. host, line = nextWord(line)
  156. if len(line) == 0 {
  157. return "", "", nil, errors.New("knownhosts: missing host pattern")
  158. }
  159. // ignore the keytype as it's in the key blob anyway.
  160. _, line = nextWord(line)
  161. if len(line) == 0 {
  162. return "", "", nil, errors.New("knownhosts: missing key type pattern")
  163. }
  164. keyBlob, _ := nextWord(line)
  165. keyBytes, err := base64.StdEncoding.DecodeString(keyBlob)
  166. if err != nil {
  167. return "", "", nil, err
  168. }
  169. key, err = ssh.ParsePublicKey(keyBytes)
  170. if err != nil {
  171. return "", "", nil, err
  172. }
  173. return marker, host, key, nil
  174. }
  175. func (db *hostKeyDB) parseLine(line []byte, filename string, linenum int) error {
  176. marker, pattern, key, err := parseLine(line)
  177. if err != nil {
  178. return err
  179. }
  180. if marker == markerRevoked {
  181. db.revoked[string(key.Marshal())] = &KnownKey{
  182. Key: key,
  183. Filename: filename,
  184. Line: linenum,
  185. }
  186. return nil
  187. }
  188. entry := keyDBLine{
  189. cert: marker == markerCert,
  190. knownKey: KnownKey{
  191. Filename: filename,
  192. Line: linenum,
  193. Key: key,
  194. },
  195. }
  196. if pattern[0] == '|' {
  197. entry.matcher, err = newHashedHost(pattern)
  198. } else {
  199. entry.matcher, err = newHostnameMatcher(pattern)
  200. }
  201. if err != nil {
  202. return err
  203. }
  204. db.lines = append(db.lines, entry)
  205. return nil
  206. }
  207. func newHostnameMatcher(pattern string) (matcher, error) {
  208. var hps hostPatterns
  209. for _, p := range strings.Split(pattern, ",") {
  210. if len(p) == 0 {
  211. continue
  212. }
  213. var a addr
  214. var negate bool
  215. if p[0] == '!' {
  216. negate = true
  217. p = p[1:]
  218. }
  219. if len(p) == 0 {
  220. return nil, errors.New("knownhosts: negation without following hostname")
  221. }
  222. var err error
  223. if p[0] == '[' {
  224. a.host, a.port, err = net.SplitHostPort(p)
  225. if err != nil {
  226. return nil, err
  227. }
  228. } else {
  229. a.host, a.port, err = net.SplitHostPort(p)
  230. if err != nil {
  231. a.host = p
  232. a.port = "22"
  233. }
  234. }
  235. hps = append(hps, hostPattern{
  236. negate: negate,
  237. addr: a,
  238. })
  239. }
  240. return hps, nil
  241. }
  242. // KnownKey represents a key declared in a known_hosts file.
  243. type KnownKey struct {
  244. Key ssh.PublicKey
  245. Filename string
  246. Line int
  247. }
  248. func (k *KnownKey) String() string {
  249. return fmt.Sprintf("%s:%d: %s", k.Filename, k.Line, serialize(k.Key))
  250. }
  251. // KeyError is returned if we did not find the key in the host key
  252. // database, or there was a mismatch. Typically, in batch
  253. // applications, this should be interpreted as failure. Interactive
  254. // applications can offer an interactive prompt to the user.
  255. type KeyError struct {
  256. // Want holds the accepted host keys. For each key algorithm,
  257. // there can be one hostkey. If Want is empty, the host is
  258. // unknown. If Want is non-empty, there was a mismatch, which
  259. // can signify a MITM attack.
  260. Want []KnownKey
  261. }
  262. func (u *KeyError) Error() string {
  263. if len(u.Want) == 0 {
  264. return "knownhosts: key is unknown"
  265. }
  266. return "knownhosts: key mismatch"
  267. }
  268. // RevokedError is returned if we found a key that was revoked.
  269. type RevokedError struct {
  270. Revoked KnownKey
  271. }
  272. func (r *RevokedError) Error() string {
  273. return "knownhosts: key is revoked"
  274. }
  275. // check checks a key against the host database. This should not be
  276. // used for verifying certificates.
  277. func (db *hostKeyDB) check(address string, remote net.Addr, remoteKey ssh.PublicKey) error {
  278. if revoked := db.revoked[string(remoteKey.Marshal())]; revoked != nil {
  279. return &RevokedError{Revoked: *revoked}
  280. }
  281. host, port, err := net.SplitHostPort(remote.String())
  282. if err != nil {
  283. return fmt.Errorf("knownhosts: SplitHostPort(%s): %v", remote, err)
  284. }
  285. addrs := []addr{
  286. {host, port},
  287. }
  288. if address != "" {
  289. host, port, err := net.SplitHostPort(address)
  290. if err != nil {
  291. return fmt.Errorf("knownhosts: SplitHostPort(%s): %v", address, err)
  292. }
  293. addrs = append(addrs, addr{host, port})
  294. }
  295. return db.checkAddrs(addrs, remoteKey)
  296. }
  297. // checkAddrs checks if we can find the given public key for any of
  298. // the given addresses. If we only find an entry for the IP address,
  299. // or only the hostname, then this still succeeds.
  300. func (db *hostKeyDB) checkAddrs(addrs []addr, remoteKey ssh.PublicKey) error {
  301. // TODO(hanwen): are these the right semantics? What if there
  302. // is just a key for the IP address, but not for the
  303. // hostname?
  304. // Algorithm => key.
  305. knownKeys := map[string]KnownKey{}
  306. for _, l := range db.lines {
  307. if l.match(addrs) {
  308. typ := l.knownKey.Key.Type()
  309. if _, ok := knownKeys[typ]; !ok {
  310. knownKeys[typ] = l.knownKey
  311. }
  312. }
  313. }
  314. keyErr := &KeyError{}
  315. for _, v := range knownKeys {
  316. keyErr.Want = append(keyErr.Want, v)
  317. }
  318. // Unknown remote host.
  319. if len(knownKeys) == 0 {
  320. return keyErr
  321. }
  322. // If the remote host starts using a different, unknown key type, we
  323. // also interpret that as a mismatch.
  324. if known, ok := knownKeys[remoteKey.Type()]; !ok || !keyEq(known.Key, remoteKey) {
  325. return keyErr
  326. }
  327. return nil
  328. }
  329. // The Read function parses file contents.
  330. func (db *hostKeyDB) Read(r io.Reader, filename string) error {
  331. scanner := bufio.NewScanner(r)
  332. lineNum := 0
  333. for scanner.Scan() {
  334. lineNum++
  335. line := scanner.Bytes()
  336. line = bytes.TrimSpace(line)
  337. if len(line) == 0 || line[0] == '#' {
  338. continue
  339. }
  340. if err := db.parseLine(line, filename, lineNum); err != nil {
  341. return fmt.Errorf("knownhosts: %s:%d: %v", filename, lineNum, err)
  342. }
  343. }
  344. return scanner.Err()
  345. }
  346. // New creates a host key callback from the given OpenSSH host key
  347. // files. The returned callback is for use in
  348. // ssh.ClientConfig.HostKeyCallback. Hashed hostnames are not supported.
  349. func New(files ...string) (ssh.HostKeyCallback, error) {
  350. db := newHostKeyDB()
  351. for _, fn := range files {
  352. f, err := os.Open(fn)
  353. if err != nil {
  354. return nil, err
  355. }
  356. defer f.Close()
  357. if err := db.Read(f, fn); err != nil {
  358. return nil, err
  359. }
  360. }
  361. var certChecker ssh.CertChecker
  362. certChecker.IsHostAuthority = db.IsHostAuthority
  363. certChecker.IsRevoked = db.IsRevoked
  364. certChecker.HostKeyFallback = db.check
  365. return certChecker.CheckHostKey, nil
  366. }
  367. // Normalize normalizes an address into the form used in known_hosts
  368. func Normalize(address string) string {
  369. host, port, err := net.SplitHostPort(address)
  370. if err != nil {
  371. host = address
  372. port = "22"
  373. }
  374. entry := host
  375. if port != "22" {
  376. entry = "[" + entry + "]:" + port
  377. } else if strings.Contains(host, ":") && !strings.HasPrefix(host, "[") {
  378. entry = "[" + entry + "]"
  379. }
  380. return entry
  381. }
  382. // Line returns a line to add append to the known_hosts files.
  383. func Line(addresses []string, key ssh.PublicKey) string {
  384. var trimmed []string
  385. for _, a := range addresses {
  386. trimmed = append(trimmed, Normalize(a))
  387. }
  388. return strings.Join(trimmed, ",") + " " + serialize(key)
  389. }
  390. // HashHostname hashes the given hostname. The hostname is not
  391. // normalized before hashing.
  392. func HashHostname(hostname string) string {
  393. // TODO(hanwen): check if we can safely normalize this always.
  394. salt := make([]byte, sha1.Size)
  395. _, err := rand.Read(salt)
  396. if err != nil {
  397. panic(fmt.Sprintf("crypto/rand failure %v", err))
  398. }
  399. hash := hashHost(hostname, salt)
  400. return encodeHash(sha1HashType, salt, hash)
  401. }
  402. func decodeHash(encoded string) (hashType string, salt, hash []byte, err error) {
  403. if len(encoded) == 0 || encoded[0] != '|' {
  404. err = errors.New("knownhosts: hashed host must start with '|'")
  405. return
  406. }
  407. components := strings.Split(encoded, "|")
  408. if len(components) != 4 {
  409. err = fmt.Errorf("knownhosts: got %d components, want 3", len(components))
  410. return
  411. }
  412. hashType = components[1]
  413. if salt, err = base64.StdEncoding.DecodeString(components[2]); err != nil {
  414. return
  415. }
  416. if hash, err = base64.StdEncoding.DecodeString(components[3]); err != nil {
  417. return
  418. }
  419. return
  420. }
  421. func encodeHash(typ string, salt []byte, hash []byte) string {
  422. return strings.Join([]string{"",
  423. typ,
  424. base64.StdEncoding.EncodeToString(salt),
  425. base64.StdEncoding.EncodeToString(hash),
  426. }, "|")
  427. }
  428. // See https://android.googlesource.com/platform/external/openssh/+/ab28f5495c85297e7a597c1ba62e996416da7c7e/hostfile.c#120
  429. func hashHost(hostname string, salt []byte) []byte {
  430. mac := hmac.New(sha1.New, salt)
  431. mac.Write([]byte(hostname))
  432. return mac.Sum(nil)
  433. }
  434. type hashedHost struct {
  435. salt []byte
  436. hash []byte
  437. }
  438. const sha1HashType = "1"
  439. func newHashedHost(encoded string) (*hashedHost, error) {
  440. typ, salt, hash, err := decodeHash(encoded)
  441. if err != nil {
  442. return nil, err
  443. }
  444. // The type field seems for future algorithm agility, but it's
  445. // actually hardcoded in openssh currently, see
  446. // https://android.googlesource.com/platform/external/openssh/+/ab28f5495c85297e7a597c1ba62e996416da7c7e/hostfile.c#120
  447. if typ != sha1HashType {
  448. return nil, fmt.Errorf("knownhosts: got hash type %s, must be '1'", typ)
  449. }
  450. return &hashedHost{salt: salt, hash: hash}, nil
  451. }
  452. func (h *hashedHost) match(addrs []addr) bool {
  453. for _, a := range addrs {
  454. if bytes.Equal(hashHost(Normalize(a.String()), h.salt), h.hash) {
  455. return true
  456. }
  457. }
  458. return false
  459. }