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 16KB

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