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.

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