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.

gpg_key_commit_verification.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package asymkey
  4. import (
  5. "fmt"
  6. "hash"
  7. "strings"
  8. "code.gitea.io/gitea/models/db"
  9. repo_model "code.gitea.io/gitea/models/repo"
  10. user_model "code.gitea.io/gitea/models/user"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. "github.com/keybase/go-crypto/openpgp/packet"
  15. )
  16. // __________________ ________ ____ __.
  17. // / _____/\______ \/ _____/ | |/ _|____ ___.__.
  18. // / \ ___ | ___/ \ ___ | <_/ __ < | |
  19. // \ \_\ \| | \ \_\ \ | | \ ___/\___ |
  20. // \______ /|____| \______ / |____|__ \___ > ____|
  21. // \/ \/ \/ \/\/
  22. // _________ .__ __
  23. // \_ ___ \ ____ _____ _____ |__|/ |_
  24. // / \ \/ / _ \ / \ / \| \ __\
  25. // \ \___( <_> ) Y Y \ Y Y \ || |
  26. // \______ /\____/|__|_| /__|_| /__||__|
  27. // \/ \/ \/
  28. // ____ ____ .__ _____.__ __ .__
  29. // \ \ / /___________|__|/ ____\__| ____ _____ _/ |_|__| ____ ____
  30. // \ Y // __ \_ __ \ \ __\| |/ ___\\__ \\ __\ |/ _ \ / \
  31. // \ /\ ___/| | \/ || | | \ \___ / __ \| | | ( <_> ) | \
  32. // \___/ \___ >__| |__||__| |__|\___ >____ /__| |__|\____/|___| /
  33. // \/ \/ \/ \/
  34. // This file provides functions relating commit verification
  35. // CommitVerification represents a commit validation of signature
  36. type CommitVerification struct {
  37. Verified bool
  38. Warning bool
  39. Reason string
  40. SigningUser *user_model.User
  41. CommittingUser *user_model.User
  42. SigningEmail string
  43. SigningKey *GPGKey
  44. SigningSSHKey *PublicKey
  45. TrustStatus string
  46. }
  47. // SignCommit represents a commit with validation of signature.
  48. type SignCommit struct {
  49. Verification *CommitVerification
  50. *user_model.UserCommit
  51. }
  52. const (
  53. // BadSignature is used as the reason when the signature has a KeyID that is in the db
  54. // but no key that has that ID verifies the signature. This is a suspicious failure.
  55. BadSignature = "gpg.error.probable_bad_signature"
  56. // BadDefaultSignature is used as the reason when the signature has a KeyID that matches the
  57. // default Key but is not verified by the default key. This is a suspicious failure.
  58. BadDefaultSignature = "gpg.error.probable_bad_default_signature"
  59. // NoKeyFound is used as the reason when no key can be found to verify the signature.
  60. NoKeyFound = "gpg.error.no_gpg_keys_found"
  61. )
  62. // ParseCommitsWithSignature checks if signaute of commits are corresponding to users gpg keys.
  63. func ParseCommitsWithSignature(oldCommits []*user_model.UserCommit, repoTrustModel repo_model.TrustModelType, isOwnerMemberCollaborator func(*user_model.User) (bool, error)) []*SignCommit {
  64. newCommits := make([]*SignCommit, 0, len(oldCommits))
  65. keyMap := map[string]bool{}
  66. for _, c := range oldCommits {
  67. signCommit := &SignCommit{
  68. UserCommit: c,
  69. Verification: ParseCommitWithSignature(c.Commit),
  70. }
  71. _ = CalculateTrustStatus(signCommit.Verification, repoTrustModel, isOwnerMemberCollaborator, &keyMap)
  72. newCommits = append(newCommits, signCommit)
  73. }
  74. return newCommits
  75. }
  76. // ParseCommitWithSignature check if signature is good against keystore.
  77. func ParseCommitWithSignature(c *git.Commit) *CommitVerification {
  78. var committer *user_model.User
  79. if c.Committer != nil {
  80. var err error
  81. // Find Committer account
  82. committer, err = user_model.GetUserByEmail(c.Committer.Email) // This finds the user by primary email or activated email so commit will not be valid if email is not
  83. if err != nil { // Skipping not user for committer
  84. committer = &user_model.User{
  85. Name: c.Committer.Name,
  86. Email: c.Committer.Email,
  87. }
  88. // We can expect this to often be an ErrUserNotExist. in the case
  89. // it is not, however, it is important to log it.
  90. if !user_model.IsErrUserNotExist(err) {
  91. log.Error("GetUserByEmail: %v", err)
  92. return &CommitVerification{
  93. CommittingUser: committer,
  94. Verified: false,
  95. Reason: "gpg.error.no_committer_account",
  96. }
  97. }
  98. }
  99. }
  100. // If no signature just report the committer
  101. if c.Signature == nil {
  102. return &CommitVerification{
  103. CommittingUser: committer,
  104. Verified: false, // Default value
  105. Reason: "gpg.error.not_signed_commit", // Default value
  106. }
  107. }
  108. // If this a SSH signature handle it differently
  109. if strings.HasPrefix(c.Signature.Signature, "-----BEGIN SSH SIGNATURE-----") {
  110. return ParseCommitWithSSHSignature(c, committer)
  111. }
  112. // Parsing signature
  113. sig, err := extractSignature(c.Signature.Signature)
  114. if err != nil { // Skipping failed to extract sign
  115. log.Error("SignatureRead err: %v", err)
  116. return &CommitVerification{
  117. CommittingUser: committer,
  118. Verified: false,
  119. Reason: "gpg.error.extract_sign",
  120. }
  121. }
  122. keyID := ""
  123. if sig.IssuerKeyId != nil && (*sig.IssuerKeyId) != 0 {
  124. keyID = fmt.Sprintf("%X", *sig.IssuerKeyId)
  125. }
  126. if keyID == "" && sig.IssuerFingerprint != nil && len(sig.IssuerFingerprint) > 0 {
  127. keyID = fmt.Sprintf("%X", sig.IssuerFingerprint[12:20])
  128. }
  129. defaultReason := NoKeyFound
  130. // First check if the sig has a keyID and if so just look at that
  131. if commitVerification := hashAndVerifyForKeyID(
  132. sig,
  133. c.Signature.Payload,
  134. committer,
  135. keyID,
  136. setting.AppName,
  137. ""); commitVerification != nil {
  138. if commitVerification.Reason == BadSignature {
  139. defaultReason = BadSignature
  140. } else {
  141. return commitVerification
  142. }
  143. }
  144. // Now try to associate the signature with the committer, if present
  145. if committer.ID != 0 {
  146. keys, err := ListGPGKeys(db.DefaultContext, committer.ID, db.ListOptions{})
  147. if err != nil { // Skipping failed to get gpg keys of user
  148. log.Error("ListGPGKeys: %v", err)
  149. return &CommitVerification{
  150. CommittingUser: committer,
  151. Verified: false,
  152. Reason: "gpg.error.failed_retrieval_gpg_keys",
  153. }
  154. }
  155. committerEmailAddresses, _ := user_model.GetEmailAddresses(committer.ID)
  156. activated := false
  157. for _, e := range committerEmailAddresses {
  158. if e.IsActivated && strings.EqualFold(e.Email, c.Committer.Email) {
  159. activated = true
  160. break
  161. }
  162. }
  163. for _, k := range keys {
  164. // Pre-check (& optimization) that emails attached to key can be attached to the committer email and can validate
  165. canValidate := false
  166. email := ""
  167. if k.Verified && activated {
  168. canValidate = true
  169. email = c.Committer.Email
  170. }
  171. if !canValidate {
  172. for _, e := range k.Emails {
  173. if e.IsActivated && strings.EqualFold(e.Email, c.Committer.Email) {
  174. canValidate = true
  175. email = e.Email
  176. break
  177. }
  178. }
  179. }
  180. if !canValidate {
  181. continue // Skip this key
  182. }
  183. commitVerification := hashAndVerifyWithSubKeysCommitVerification(sig, c.Signature.Payload, k, committer, committer, email)
  184. if commitVerification != nil {
  185. return commitVerification
  186. }
  187. }
  188. }
  189. if setting.Repository.Signing.SigningKey != "" && setting.Repository.Signing.SigningKey != "default" && setting.Repository.Signing.SigningKey != "none" {
  190. // OK we should try the default key
  191. gpgSettings := git.GPGSettings{
  192. Sign: true,
  193. KeyID: setting.Repository.Signing.SigningKey,
  194. Name: setting.Repository.Signing.SigningName,
  195. Email: setting.Repository.Signing.SigningEmail,
  196. }
  197. if err := gpgSettings.LoadPublicKeyContent(); err != nil {
  198. log.Error("Error getting default signing key: %s %v", gpgSettings.KeyID, err)
  199. } else if commitVerification := verifyWithGPGSettings(&gpgSettings, sig, c.Signature.Payload, committer, keyID); commitVerification != nil {
  200. if commitVerification.Reason == BadSignature {
  201. defaultReason = BadSignature
  202. } else {
  203. return commitVerification
  204. }
  205. }
  206. }
  207. defaultGPGSettings, err := c.GetRepositoryDefaultPublicGPGKey(false)
  208. if err != nil {
  209. log.Error("Error getting default public gpg key: %v", err)
  210. } else if defaultGPGSettings == nil {
  211. log.Warn("Unable to get defaultGPGSettings for unattached commit: %s", c.ID.String())
  212. } else if defaultGPGSettings.Sign {
  213. if commitVerification := verifyWithGPGSettings(defaultGPGSettings, sig, c.Signature.Payload, committer, keyID); commitVerification != nil {
  214. if commitVerification.Reason == BadSignature {
  215. defaultReason = BadSignature
  216. } else {
  217. return commitVerification
  218. }
  219. }
  220. }
  221. return &CommitVerification{ // Default at this stage
  222. CommittingUser: committer,
  223. Verified: false,
  224. Warning: defaultReason != NoKeyFound,
  225. Reason: defaultReason,
  226. SigningKey: &GPGKey{
  227. KeyID: keyID,
  228. },
  229. }
  230. }
  231. func verifyWithGPGSettings(gpgSettings *git.GPGSettings, sig *packet.Signature, payload string, committer *user_model.User, keyID string) *CommitVerification {
  232. // First try to find the key in the db
  233. if commitVerification := hashAndVerifyForKeyID(sig, payload, committer, gpgSettings.KeyID, gpgSettings.Name, gpgSettings.Email); commitVerification != nil {
  234. return commitVerification
  235. }
  236. // Otherwise we have to parse the key
  237. ekeys, err := checkArmoredGPGKeyString(gpgSettings.PublicKeyContent)
  238. if err != nil {
  239. log.Error("Unable to get default signing key: %v", err)
  240. return &CommitVerification{
  241. CommittingUser: committer,
  242. Verified: false,
  243. Reason: "gpg.error.generate_hash",
  244. }
  245. }
  246. for _, ekey := range ekeys {
  247. pubkey := ekey.PrimaryKey
  248. content, err := base64EncPubKey(pubkey)
  249. if err != nil {
  250. return &CommitVerification{
  251. CommittingUser: committer,
  252. Verified: false,
  253. Reason: "gpg.error.generate_hash",
  254. }
  255. }
  256. k := &GPGKey{
  257. Content: content,
  258. CanSign: pubkey.CanSign(),
  259. KeyID: pubkey.KeyIdString(),
  260. }
  261. for _, subKey := range ekey.Subkeys {
  262. content, err := base64EncPubKey(subKey.PublicKey)
  263. if err != nil {
  264. return &CommitVerification{
  265. CommittingUser: committer,
  266. Verified: false,
  267. Reason: "gpg.error.generate_hash",
  268. }
  269. }
  270. k.SubsKey = append(k.SubsKey, &GPGKey{
  271. Content: content,
  272. CanSign: subKey.PublicKey.CanSign(),
  273. KeyID: subKey.PublicKey.KeyIdString(),
  274. })
  275. }
  276. if commitVerification := hashAndVerifyWithSubKeysCommitVerification(sig, payload, k, committer, &user_model.User{
  277. Name: gpgSettings.Name,
  278. Email: gpgSettings.Email,
  279. }, gpgSettings.Email); commitVerification != nil {
  280. return commitVerification
  281. }
  282. if keyID == k.KeyID {
  283. // This is a bad situation ... We have a key id that matches our default key but the signature doesn't match.
  284. return &CommitVerification{
  285. CommittingUser: committer,
  286. Verified: false,
  287. Warning: true,
  288. Reason: BadSignature,
  289. }
  290. }
  291. }
  292. return nil
  293. }
  294. func verifySign(s *packet.Signature, h hash.Hash, k *GPGKey) error {
  295. // Check if key can sign
  296. if !k.CanSign {
  297. return fmt.Errorf("key can not sign")
  298. }
  299. // Decode key
  300. pkey, err := base64DecPubKey(k.Content)
  301. if err != nil {
  302. return err
  303. }
  304. return pkey.VerifySignature(h, s)
  305. }
  306. func hashAndVerify(sig *packet.Signature, payload string, k *GPGKey) (*GPGKey, error) {
  307. // Generating hash of commit
  308. hash, err := populateHash(sig.Hash, []byte(payload))
  309. if err != nil { // Skipping as failed to generate hash
  310. log.Error("PopulateHash: %v", err)
  311. return nil, err
  312. }
  313. // We will ignore errors in verification as they don't need to be propagated up
  314. err = verifySign(sig, hash, k)
  315. if err != nil {
  316. return nil, nil
  317. }
  318. return k, nil
  319. }
  320. func hashAndVerifyWithSubKeys(sig *packet.Signature, payload string, k *GPGKey) (*GPGKey, error) {
  321. verified, err := hashAndVerify(sig, payload, k)
  322. if err != nil || verified != nil {
  323. return verified, err
  324. }
  325. for _, sk := range k.SubsKey {
  326. verified, err := hashAndVerify(sig, payload, sk)
  327. if err != nil || verified != nil {
  328. return verified, err
  329. }
  330. }
  331. return nil, nil
  332. }
  333. func hashAndVerifyWithSubKeysCommitVerification(sig *packet.Signature, payload string, k *GPGKey, committer, signer *user_model.User, email string) *CommitVerification {
  334. key, err := hashAndVerifyWithSubKeys(sig, payload, k)
  335. if err != nil { // Skipping failed to generate hash
  336. return &CommitVerification{
  337. CommittingUser: committer,
  338. Verified: false,
  339. Reason: "gpg.error.generate_hash",
  340. }
  341. }
  342. if key != nil {
  343. return &CommitVerification{ // Everything is ok
  344. CommittingUser: committer,
  345. Verified: true,
  346. Reason: fmt.Sprintf("%s / %s", signer.Name, key.KeyID),
  347. SigningUser: signer,
  348. SigningKey: key,
  349. SigningEmail: email,
  350. }
  351. }
  352. return nil
  353. }
  354. func hashAndVerifyForKeyID(sig *packet.Signature, payload string, committer *user_model.User, keyID, name, email string) *CommitVerification {
  355. if keyID == "" {
  356. return nil
  357. }
  358. keys, err := GetGPGKeysByKeyID(keyID)
  359. if err != nil {
  360. log.Error("GetGPGKeysByKeyID: %v", err)
  361. return &CommitVerification{
  362. CommittingUser: committer,
  363. Verified: false,
  364. Reason: "gpg.error.failed_retrieval_gpg_keys",
  365. }
  366. }
  367. if len(keys) == 0 {
  368. return nil
  369. }
  370. for _, key := range keys {
  371. var primaryKeys []*GPGKey
  372. if key.PrimaryKeyID != "" {
  373. primaryKeys, err = GetGPGKeysByKeyID(key.PrimaryKeyID)
  374. if err != nil {
  375. log.Error("GetGPGKeysByKeyID: %v", err)
  376. return &CommitVerification{
  377. CommittingUser: committer,
  378. Verified: false,
  379. Reason: "gpg.error.failed_retrieval_gpg_keys",
  380. }
  381. }
  382. }
  383. activated, email := checkKeyEmails(email, append([]*GPGKey{key}, primaryKeys...)...)
  384. if !activated {
  385. continue
  386. }
  387. signer := &user_model.User{
  388. Name: name,
  389. Email: email,
  390. }
  391. if key.OwnerID != 0 {
  392. owner, err := user_model.GetUserByID(key.OwnerID)
  393. if err == nil {
  394. signer = owner
  395. } else if !user_model.IsErrUserNotExist(err) {
  396. log.Error("Failed to user_model.GetUserByID: %d for key ID: %d (%s) %v", key.OwnerID, key.ID, key.KeyID, err)
  397. return &CommitVerification{
  398. CommittingUser: committer,
  399. Verified: false,
  400. Reason: "gpg.error.no_committer_account",
  401. }
  402. }
  403. }
  404. commitVerification := hashAndVerifyWithSubKeysCommitVerification(sig, payload, key, committer, signer, email)
  405. if commitVerification != nil {
  406. return commitVerification
  407. }
  408. }
  409. // This is a bad situation ... We have a key id that is in our database but the signature doesn't match.
  410. return &CommitVerification{
  411. CommittingUser: committer,
  412. Verified: false,
  413. Warning: true,
  414. Reason: BadSignature,
  415. }
  416. }
  417. // CalculateTrustStatus will calculate the TrustStatus for a commit verification within a repository
  418. // There are several trust models in Gitea
  419. func CalculateTrustStatus(verification *CommitVerification, repoTrustModel repo_model.TrustModelType, isOwnerMemberCollaborator func(*user_model.User) (bool, error), keyMap *map[string]bool) (err error) {
  420. if !verification.Verified {
  421. return
  422. }
  423. // In the Committer trust model a signature is trusted if it matches the committer
  424. // - it doesn't matter if they're a collaborator, the owner, Gitea or Github
  425. // NB: This model is commit verification only
  426. if repoTrustModel == repo_model.CommitterTrustModel {
  427. // default to "unmatched"
  428. verification.TrustStatus = "unmatched"
  429. // We can only verify against users in our database but the default key will match
  430. // against by email if it is not in the db.
  431. if (verification.SigningUser.ID != 0 &&
  432. verification.CommittingUser.ID == verification.SigningUser.ID) ||
  433. (verification.SigningUser.ID == 0 && verification.CommittingUser.ID == 0 &&
  434. verification.SigningUser.Email == verification.CommittingUser.Email) {
  435. verification.TrustStatus = "trusted"
  436. }
  437. return
  438. }
  439. // Now we drop to the more nuanced trust models...
  440. verification.TrustStatus = "trusted"
  441. if verification.SigningUser.ID == 0 {
  442. // This commit is signed by the default key - but this key is not assigned to a user in the DB.
  443. // However in the repo_model.CollaboratorCommitterTrustModel we cannot mark this as trusted
  444. // unless the default key matches the email of a non-user.
  445. if repoTrustModel == repo_model.CollaboratorCommitterTrustModel && (verification.CommittingUser.ID != 0 ||
  446. verification.SigningUser.Email != verification.CommittingUser.Email) {
  447. verification.TrustStatus = "untrusted"
  448. }
  449. return
  450. }
  451. // Check we actually have a GPG SigningKey
  452. if verification.SigningKey != nil {
  453. var isMember bool
  454. if keyMap != nil {
  455. var has bool
  456. isMember, has = (*keyMap)[verification.SigningKey.KeyID]
  457. if !has {
  458. isMember, err = isOwnerMemberCollaborator(verification.SigningUser)
  459. (*keyMap)[verification.SigningKey.KeyID] = isMember
  460. }
  461. } else {
  462. isMember, err = isOwnerMemberCollaborator(verification.SigningUser)
  463. }
  464. if !isMember {
  465. verification.TrustStatus = "untrusted"
  466. if verification.CommittingUser.ID != verification.SigningUser.ID {
  467. // The committing user and the signing user are not the same
  468. // This should be marked as questionable unless the signing user is a collaborator/team member etc.
  469. verification.TrustStatus = "unmatched"
  470. }
  471. } else if repoTrustModel == repo_model.CollaboratorCommitterTrustModel && verification.CommittingUser.ID != verification.SigningUser.ID {
  472. // The committing user and the signing user are not the same and our trustmodel states that they must match
  473. verification.TrustStatus = "unmatched"
  474. }
  475. }
  476. return err
  477. }