gitea源码

commit.go 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // Copyright 2025 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package asymkey
  4. import (
  5. "context"
  6. "fmt"
  7. "slices"
  8. "strings"
  9. asymkey_model "code.gitea.io/gitea/models/asymkey"
  10. "code.gitea.io/gitea/models/db"
  11. user_model "code.gitea.io/gitea/models/user"
  12. "code.gitea.io/gitea/modules/cache"
  13. "code.gitea.io/gitea/modules/cachegroup"
  14. "code.gitea.io/gitea/modules/git"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/setting"
  17. "github.com/42wim/sshsig"
  18. "github.com/ProtonMail/go-crypto/openpgp/packet"
  19. )
  20. // ParseCommitWithSignature check if signature is good against keystore.
  21. func ParseCommitWithSignature(ctx context.Context, c *git.Commit) *asymkey_model.CommitVerification {
  22. committer, err := user_model.GetUserByEmail(ctx, c.Committer.Email)
  23. if err != nil && !user_model.IsErrUserNotExist(err) {
  24. log.Error("GetUserByEmail: %v", err)
  25. return &asymkey_model.CommitVerification{
  26. Verified: false,
  27. Reason: "gpg.error.no_committer_account", // this error is not right, but such error should seldom happen
  28. }
  29. }
  30. return ParseCommitWithSignatureCommitter(ctx, c, committer)
  31. }
  32. // ParseCommitWithSignatureCommitter parses a commit's GPG or SSH signature.
  33. // The caller guarantees that the committer user is related to the commit by checking its activated email addresses or no-reply address.
  34. // If the commit is singed by an instance key, then committer can be nil.
  35. // If the signature exists, even if committer is nil, the returned CommittingUser will be a non-nil fake user (e.g.: instance key)
  36. func ParseCommitWithSignatureCommitter(ctx context.Context, c *git.Commit, committer *user_model.User) *asymkey_model.CommitVerification {
  37. // If no signature, just report the committer
  38. if c.Signature == nil {
  39. return &asymkey_model.CommitVerification{
  40. CommittingUser: committer,
  41. Verified: false,
  42. Reason: "gpg.error.not_signed_commit",
  43. }
  44. }
  45. // to support instance key, we need a fake committer user (not really needed, but legacy code accesses the committer without nil-check)
  46. if committer == nil {
  47. committer = &user_model.User{
  48. Name: c.Committer.Name,
  49. Email: c.Committer.Email,
  50. }
  51. }
  52. if strings.HasPrefix(c.Signature.Signature, "-----BEGIN SSH SIGNATURE-----") {
  53. return parseCommitWithSSHSignature(ctx, c, committer)
  54. }
  55. return parseCommitWithGPGSignature(ctx, c, committer)
  56. }
  57. func parseCommitWithGPGSignature(ctx context.Context, c *git.Commit, committer *user_model.User) *asymkey_model.CommitVerification {
  58. // Parsing signature
  59. sig, err := asymkey_model.ExtractSignature(c.Signature.Signature)
  60. if err != nil { // Skipping failed to extract sign
  61. log.Error("SignatureRead err: %v", err)
  62. return &asymkey_model.CommitVerification{
  63. CommittingUser: committer,
  64. Verified: false,
  65. Reason: "gpg.error.extract_sign",
  66. }
  67. }
  68. keyID := asymkey_model.TryGetKeyIDFromSignature(sig)
  69. defaultReason := asymkey_model.NoKeyFound
  70. // First check if the sig has a keyID and if so just look at that
  71. if commitVerification := HashAndVerifyForKeyID(
  72. ctx,
  73. sig,
  74. c.Signature.Payload,
  75. committer,
  76. keyID,
  77. setting.AppName,
  78. ""); commitVerification != nil {
  79. if commitVerification.Reason == asymkey_model.BadSignature {
  80. defaultReason = asymkey_model.BadSignature
  81. } else {
  82. return commitVerification
  83. }
  84. }
  85. // Now try to associate the signature with the committer, if present
  86. if committer.ID != 0 {
  87. keys, err := db.Find[asymkey_model.GPGKey](ctx, asymkey_model.FindGPGKeyOptions{
  88. OwnerID: committer.ID,
  89. })
  90. if err != nil { // Skipping failed to get gpg keys of user
  91. log.Error("ListGPGKeys: %v", err)
  92. return &asymkey_model.CommitVerification{
  93. CommittingUser: committer,
  94. Verified: false,
  95. Reason: "gpg.error.failed_retrieval_gpg_keys",
  96. }
  97. }
  98. if err := asymkey_model.GPGKeyList(keys).LoadSubKeys(ctx); err != nil {
  99. log.Error("LoadSubKeys: %v", err)
  100. return &asymkey_model.CommitVerification{
  101. CommittingUser: committer,
  102. Verified: false,
  103. Reason: "gpg.error.failed_retrieval_gpg_keys",
  104. }
  105. }
  106. for _, k := range keys {
  107. // Pre-check (& optimization) that emails attached to key can be attached to the committer email and can validate
  108. canValidate := false
  109. email := ""
  110. if k.Verified {
  111. canValidate = true
  112. email = c.Committer.Email
  113. }
  114. if !canValidate {
  115. for _, e := range k.Emails {
  116. if e.IsActivated && strings.EqualFold(e.Email, c.Committer.Email) {
  117. canValidate = true
  118. email = e.Email
  119. break
  120. }
  121. }
  122. }
  123. if !canValidate {
  124. continue // Skip this key
  125. }
  126. commitVerification := asymkey_model.HashAndVerifyWithSubKeysCommitVerification(sig, c.Signature.Payload, k, committer, committer, email)
  127. if commitVerification != nil {
  128. return commitVerification
  129. }
  130. }
  131. }
  132. if setting.Repository.Signing.SigningKey != "" && setting.Repository.Signing.SigningKey != "default" && setting.Repository.Signing.SigningKey != "none" {
  133. // OK we should try the default key
  134. gpgSettings := git.GPGSettings{
  135. Sign: true,
  136. KeyID: setting.Repository.Signing.SigningKey,
  137. Name: setting.Repository.Signing.SigningName,
  138. Email: setting.Repository.Signing.SigningEmail,
  139. }
  140. if err := gpgSettings.LoadPublicKeyContent(); err != nil {
  141. log.Error("Error getting default signing key: %s %v", gpgSettings.KeyID, err)
  142. } else if commitVerification := verifyWithGPGSettings(ctx, &gpgSettings, sig, c.Signature.Payload, committer, keyID); commitVerification != nil {
  143. if commitVerification.Reason == asymkey_model.BadSignature {
  144. defaultReason = asymkey_model.BadSignature
  145. } else {
  146. return commitVerification
  147. }
  148. }
  149. }
  150. defaultGPGSettings, err := c.GetRepositoryDefaultPublicGPGKey(false)
  151. if err != nil {
  152. log.Error("Error getting default public gpg key: %v", err)
  153. } else if defaultGPGSettings == nil {
  154. log.Warn("Unable to get defaultGPGSettings for unattached commit: %s", c.ID.String())
  155. } else if defaultGPGSettings.Sign {
  156. if commitVerification := verifyWithGPGSettings(ctx, defaultGPGSettings, sig, c.Signature.Payload, committer, keyID); commitVerification != nil {
  157. if commitVerification.Reason == asymkey_model.BadSignature {
  158. defaultReason = asymkey_model.BadSignature
  159. } else {
  160. return commitVerification
  161. }
  162. }
  163. }
  164. return &asymkey_model.CommitVerification{ // Default at this stage
  165. CommittingUser: committer,
  166. Verified: false,
  167. Warning: defaultReason != asymkey_model.NoKeyFound,
  168. Reason: defaultReason,
  169. SigningKey: &asymkey_model.GPGKey{
  170. KeyID: keyID,
  171. },
  172. }
  173. }
  174. func checkKeyEmails(ctx context.Context, email string, keys ...*asymkey_model.GPGKey) (bool, string) {
  175. uid := int64(0)
  176. var userEmails []*user_model.EmailAddress
  177. var user *user_model.User
  178. for _, key := range keys {
  179. for _, e := range key.Emails {
  180. if e.IsActivated && (email == "" || strings.EqualFold(e.Email, email)) {
  181. return true, e.Email
  182. }
  183. }
  184. if key.Verified && key.OwnerID != 0 {
  185. if uid != key.OwnerID {
  186. userEmails, _ = cache.GetWithContextCache(ctx, cachegroup.UserEmailAddresses, key.OwnerID, user_model.GetEmailAddresses)
  187. uid = key.OwnerID
  188. user, _ = cache.GetWithContextCache(ctx, cachegroup.User, uid, user_model.GetUserByID)
  189. }
  190. for _, e := range userEmails {
  191. if e.IsActivated && (email == "" || strings.EqualFold(e.Email, email)) {
  192. return true, e.Email
  193. }
  194. }
  195. if user != nil && strings.EqualFold(email, user.GetPlaceholderEmail()) {
  196. return true, user.GetPlaceholderEmail()
  197. }
  198. }
  199. }
  200. return false, email
  201. }
  202. func HashAndVerifyForKeyID(ctx context.Context, sig *packet.Signature, payload string, committer *user_model.User, keyID, name, email string) *asymkey_model.CommitVerification {
  203. if keyID == "" {
  204. return nil
  205. }
  206. keys, err := cache.GetWithContextCache(ctx, cachegroup.GPGKeyWithSubKeys, keyID, asymkey_model.FindGPGKeyWithSubKeys)
  207. if err != nil {
  208. log.Error("GetGPGKeysByKeyID: %v", err)
  209. return &asymkey_model.CommitVerification{
  210. CommittingUser: committer,
  211. Verified: false,
  212. Reason: "gpg.error.failed_retrieval_gpg_keys",
  213. }
  214. }
  215. if len(keys) == 0 {
  216. return nil
  217. }
  218. for _, key := range keys {
  219. var primaryKeys []*asymkey_model.GPGKey
  220. if key.PrimaryKeyID != "" {
  221. primaryKeys, err = cache.GetWithContextCache(ctx, cachegroup.GPGKeyWithSubKeys, key.PrimaryKeyID, asymkey_model.FindGPGKeyWithSubKeys)
  222. if err != nil {
  223. log.Error("GetGPGKeysByKeyID: %v", err)
  224. return &asymkey_model.CommitVerification{
  225. CommittingUser: committer,
  226. Verified: false,
  227. Reason: "gpg.error.failed_retrieval_gpg_keys",
  228. }
  229. }
  230. }
  231. activated, email := checkKeyEmails(ctx, email, append([]*asymkey_model.GPGKey{key}, primaryKeys...)...)
  232. if !activated {
  233. continue
  234. }
  235. signer := &user_model.User{
  236. Name: name,
  237. Email: email,
  238. }
  239. if key.OwnerID > 0 {
  240. owner, err := cache.GetWithContextCache(ctx, cachegroup.User, key.OwnerID, user_model.GetUserByID)
  241. if err == nil {
  242. signer = owner
  243. } else if !user_model.IsErrUserNotExist(err) {
  244. log.Error("Failed to user_model.GetUserByID: %d for key ID: %d (%s) %v", key.OwnerID, key.ID, key.KeyID, err)
  245. return &asymkey_model.CommitVerification{
  246. CommittingUser: committer,
  247. Verified: false,
  248. Reason: "gpg.error.no_committer_account",
  249. }
  250. }
  251. }
  252. commitVerification := asymkey_model.HashAndVerifyWithSubKeysCommitVerification(sig, payload, key, committer, signer, email)
  253. if commitVerification != nil {
  254. return commitVerification
  255. }
  256. }
  257. // This is a bad situation ... We have a key id that is in our database but the signature doesn't match.
  258. return &asymkey_model.CommitVerification{
  259. CommittingUser: committer,
  260. Verified: false,
  261. Warning: true,
  262. Reason: asymkey_model.BadSignature,
  263. }
  264. }
  265. func verifyWithGPGSettings(ctx context.Context, gpgSettings *git.GPGSettings, sig *packet.Signature, payload string, committer *user_model.User, keyID string) *asymkey_model.CommitVerification {
  266. // First try to find the key in the db
  267. if commitVerification := HashAndVerifyForKeyID(ctx, sig, payload, committer, gpgSettings.KeyID, gpgSettings.Name, gpgSettings.Email); commitVerification != nil {
  268. return commitVerification
  269. }
  270. // Otherwise we have to parse the key
  271. ekeys, err := asymkey_model.CheckArmoredGPGKeyString(gpgSettings.PublicKeyContent)
  272. if err != nil {
  273. log.Error("Unable to get default signing key: %v", err)
  274. return &asymkey_model.CommitVerification{
  275. CommittingUser: committer,
  276. Verified: false,
  277. Reason: "gpg.error.generate_hash",
  278. }
  279. }
  280. for _, ekey := range ekeys {
  281. pubkey := ekey.PrimaryKey
  282. content, err := asymkey_model.Base64EncPubKey(pubkey)
  283. if err != nil {
  284. return &asymkey_model.CommitVerification{
  285. CommittingUser: committer,
  286. Verified: false,
  287. Reason: "gpg.error.generate_hash",
  288. }
  289. }
  290. k := &asymkey_model.GPGKey{
  291. Content: content,
  292. CanSign: pubkey.CanSign(),
  293. KeyID: pubkey.KeyIdString(),
  294. }
  295. for _, subKey := range ekey.Subkeys {
  296. content, err := asymkey_model.Base64EncPubKey(subKey.PublicKey)
  297. if err != nil {
  298. return &asymkey_model.CommitVerification{
  299. CommittingUser: committer,
  300. Verified: false,
  301. Reason: "gpg.error.generate_hash",
  302. }
  303. }
  304. k.SubsKey = append(k.SubsKey, &asymkey_model.GPGKey{
  305. Content: content,
  306. CanSign: subKey.PublicKey.CanSign(),
  307. KeyID: subKey.PublicKey.KeyIdString(),
  308. })
  309. }
  310. if commitVerification := asymkey_model.HashAndVerifyWithSubKeysCommitVerification(sig, payload, k, committer, &user_model.User{
  311. Name: gpgSettings.Name,
  312. Email: gpgSettings.Email,
  313. }, gpgSettings.Email); commitVerification != nil {
  314. return commitVerification
  315. }
  316. if keyID == k.KeyID {
  317. // This is a bad situation ... We have a key id that matches our default key but the signature doesn't match.
  318. return &asymkey_model.CommitVerification{
  319. CommittingUser: committer,
  320. Verified: false,
  321. Warning: true,
  322. Reason: asymkey_model.BadSignature,
  323. }
  324. }
  325. }
  326. return nil
  327. }
  328. func verifySSHCommitVerificationByInstanceKey(c *git.Commit, committerUser, signerUser *user_model.User, committerGitEmail, publicKeyContent string) *asymkey_model.CommitVerification {
  329. fingerprint, err := asymkey_model.CalcFingerprint(publicKeyContent)
  330. if err != nil {
  331. log.Error("Error calculating the fingerprint public key %q, err: %v", publicKeyContent, err)
  332. return nil
  333. }
  334. sshPubKey := &asymkey_model.PublicKey{
  335. Verified: true,
  336. Content: publicKeyContent,
  337. Fingerprint: fingerprint,
  338. HasUsed: true,
  339. }
  340. return verifySSHCommitVerification(c.Signature.Signature, c.Signature.Payload, sshPubKey, committerUser, signerUser, committerGitEmail)
  341. }
  342. // parseCommitWithSSHSignature check if signature is good against keystore.
  343. func parseCommitWithSSHSignature(ctx context.Context, c *git.Commit, committerUser *user_model.User) *asymkey_model.CommitVerification {
  344. // Now try to associate the signature with the committer, if present
  345. if committerUser.ID != 0 {
  346. keys, err := db.Find[asymkey_model.PublicKey](ctx, asymkey_model.FindPublicKeyOptions{
  347. OwnerID: committerUser.ID,
  348. NotKeytype: asymkey_model.KeyTypePrincipal,
  349. })
  350. if err != nil { // Skipping failed to get ssh keys of user
  351. log.Error("ListPublicKeys: %v", err)
  352. return &asymkey_model.CommitVerification{
  353. CommittingUser: committerUser,
  354. Verified: false,
  355. Reason: "gpg.error.failed_retrieval_gpg_keys",
  356. }
  357. }
  358. for _, k := range keys {
  359. if k.Verified {
  360. commitVerification := verifySSHCommitVerification(c.Signature.Signature, c.Signature.Payload, k, committerUser, committerUser, c.Committer.Email)
  361. if commitVerification != nil {
  362. return commitVerification
  363. }
  364. }
  365. }
  366. }
  367. // Try the pre-set trusted keys (for key-rotation purpose)
  368. // At the moment, we still use the SigningName&SigningEmail for the rotated keys.
  369. // Maybe in the future we can extend the key format to "ssh-xxx .... old-user@example.com" to support different signer emails.
  370. for _, k := range setting.Repository.Signing.TrustedSSHKeys {
  371. signerUser := &user_model.User{
  372. Name: setting.Repository.Signing.SigningName,
  373. Email: setting.Repository.Signing.SigningEmail,
  374. }
  375. commitVerification := verifySSHCommitVerificationByInstanceKey(c, committerUser, signerUser, c.Committer.Email, k)
  376. if commitVerification != nil && commitVerification.Verified {
  377. return commitVerification
  378. }
  379. }
  380. // Try the configured instance-wide SSH public key
  381. if setting.Repository.Signing.SigningFormat == git.SigningKeyFormatSSH && !slices.Contains([]string{"", "default", "none"}, setting.Repository.Signing.SigningKey) {
  382. gpgSettings := git.GPGSettings{
  383. Sign: true,
  384. KeyID: setting.Repository.Signing.SigningKey,
  385. Name: setting.Repository.Signing.SigningName,
  386. Email: setting.Repository.Signing.SigningEmail,
  387. Format: setting.Repository.Signing.SigningFormat,
  388. }
  389. signerUser := &user_model.User{
  390. Name: gpgSettings.Name,
  391. Email: gpgSettings.Email,
  392. }
  393. if err := gpgSettings.LoadPublicKeyContent(); err != nil {
  394. log.Error("Error getting instance-wide SSH signing key %q, err: %v", gpgSettings.KeyID, err)
  395. } else {
  396. commitVerification := verifySSHCommitVerificationByInstanceKey(c, committerUser, signerUser, gpgSettings.Email, gpgSettings.PublicKeyContent)
  397. if commitVerification != nil && commitVerification.Verified {
  398. return commitVerification
  399. }
  400. }
  401. }
  402. return &asymkey_model.CommitVerification{
  403. CommittingUser: committerUser,
  404. Verified: false,
  405. Reason: asymkey_model.NoKeyFound,
  406. }
  407. }
  408. func verifySSHCommitVerification(sig, payload string, k *asymkey_model.PublicKey, committer, signer *user_model.User, email string) *asymkey_model.CommitVerification {
  409. if err := sshsig.Verify(strings.NewReader(payload), []byte(sig), []byte(k.Content), "git"); err != nil {
  410. return nil
  411. }
  412. return &asymkey_model.CommitVerification{ // Everything is ok
  413. CommittingUser: committer,
  414. Verified: true,
  415. Reason: fmt.Sprintf("%s / %s", signer.Name, k.Fingerprint),
  416. SigningUser: signer,
  417. SigningSSHKey: k,
  418. SigningEmail: email,
  419. }
  420. }