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.

ssh_key_fingerprint.go 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "errors"
  7. "fmt"
  8. "strings"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/process"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/util"
  13. "golang.org/x/crypto/ssh"
  14. )
  15. // ___________.__ .__ __
  16. // \_ _____/|__| ____ ____ ________________________|__| _____/ |_
  17. // | __) | |/ \ / ___\_/ __ \_ __ \____ \_ __ \ |/ \ __\
  18. // | \ | | | \/ /_/ > ___/| | \/ |_> > | \/ | | \ |
  19. // \___ / |__|___| /\___ / \___ >__| | __/|__| |__|___| /__|
  20. // \/ \//_____/ \/ |__| \/
  21. //
  22. // This file contains functions for fingerprinting SSH keys
  23. //
  24. // The database is used in checkKeyFingerprint however most of these functions probably belong in a module
  25. // checkKeyFingerprint only checks if key fingerprint has been used as public key,
  26. // it is OK to use same key as deploy key for multiple repositories/users.
  27. func checkKeyFingerprint(e Engine, fingerprint string) error {
  28. has, err := e.Get(&PublicKey{
  29. Fingerprint: fingerprint,
  30. })
  31. if err != nil {
  32. return err
  33. } else if has {
  34. return ErrKeyAlreadyExist{0, fingerprint, ""}
  35. }
  36. return nil
  37. }
  38. func calcFingerprintSSHKeygen(publicKeyContent string) (string, error) {
  39. // Calculate fingerprint.
  40. tmpPath, err := writeTmpKeyFile(publicKeyContent)
  41. if err != nil {
  42. return "", err
  43. }
  44. defer func() {
  45. if err := util.Remove(tmpPath); err != nil {
  46. log.Warn("Unable to remove temporary key file: %s: Error: %v", tmpPath, err)
  47. }
  48. }()
  49. stdout, stderr, err := process.GetManager().Exec("AddPublicKey", "ssh-keygen", "-lf", tmpPath)
  50. if err != nil {
  51. if strings.Contains(stderr, "is not a public key file") {
  52. return "", ErrKeyUnableVerify{stderr}
  53. }
  54. return "", fmt.Errorf("'ssh-keygen -lf %s' failed with error '%s': %s", tmpPath, err, stderr)
  55. } else if len(stdout) < 2 {
  56. return "", errors.New("not enough output for calculating fingerprint: " + stdout)
  57. }
  58. return strings.Split(stdout, " ")[1], nil
  59. }
  60. func calcFingerprintNative(publicKeyContent string) (string, error) {
  61. // Calculate fingerprint.
  62. pk, _, _, _, err := ssh.ParseAuthorizedKey([]byte(publicKeyContent))
  63. if err != nil {
  64. return "", err
  65. }
  66. return ssh.FingerprintSHA256(pk), nil
  67. }
  68. func calcFingerprint(publicKeyContent string) (string, error) {
  69. // Call the method based on configuration
  70. var (
  71. fnName, fp string
  72. err error
  73. )
  74. if setting.SSH.StartBuiltinServer {
  75. fnName = "calcFingerprintNative"
  76. fp, err = calcFingerprintNative(publicKeyContent)
  77. } else {
  78. fnName = "calcFingerprintSSHKeygen"
  79. fp, err = calcFingerprintSSHKeygen(publicKeyContent)
  80. }
  81. if err != nil {
  82. if IsErrKeyUnableVerify(err) {
  83. log.Info("%s", publicKeyContent)
  84. return "", err
  85. }
  86. return "", fmt.Errorf("%s: %v", fnName, err)
  87. }
  88. return fp, nil
  89. }