Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

gpg_key_commit_verification.go 16KB

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