2017-03-15 21:27:35 -04:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-12-10 03:14:24 -05:00
|
|
|
package asymkey
|
2017-03-15 21:27:35 -04:00
|
|
|
|
|
|
|
import (
|
2021-12-10 03:14:24 -05:00
|
|
|
"context"
|
2017-03-15 21:27:35 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-09-19 07:49:59 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-11 02:03:30 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2017-03-22 06:43:54 -04:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-08-15 10:46:21 -04:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2017-03-22 06:43:54 -04:00
|
|
|
|
2017-06-13 20:43:43 -04:00
|
|
|
"github.com/keybase/go-crypto/openpgp"
|
|
|
|
"github.com/keybase/go-crypto/openpgp/packet"
|
2019-10-17 05:26:49 -04:00
|
|
|
"xorm.io/xorm"
|
2017-03-15 21:27:35 -04:00
|
|
|
)
|
|
|
|
|
2021-07-13 09:28:07 -04:00
|
|
|
// __________________ ________ ____ __.
|
|
|
|
// / _____/\______ \/ _____/ | |/ _|____ ___.__.
|
|
|
|
// / \ ___ | ___/ \ ___ | <_/ __ < | |
|
|
|
|
// \ \_\ \| | \ \_\ \ | | \ ___/\___ |
|
|
|
|
// \______ /|____| \______ / |____|__ \___ > ____|
|
|
|
|
// \/ \/ \/ \/\/
|
|
|
|
|
2017-03-15 21:27:35 -04:00
|
|
|
// GPGKey represents a GPG key.
|
|
|
|
type GPGKey struct {
|
2019-08-15 10:46:21 -04:00
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
OwnerID int64 `xorm:"INDEX NOT NULL"`
|
|
|
|
KeyID string `xorm:"INDEX CHAR(16) NOT NULL"`
|
|
|
|
PrimaryKeyID string `xorm:"CHAR(16)"`
|
2022-08-22 09:32:28 -04:00
|
|
|
Content string `xorm:"MEDIUMTEXT NOT NULL"`
|
2019-08-15 10:46:21 -04:00
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
|
|
|
ExpiredUnix timeutil.TimeStamp
|
|
|
|
AddedUnix timeutil.TimeStamp
|
2017-03-15 21:27:35 -04:00
|
|
|
SubsKey []*GPGKey `xorm:"-"`
|
2021-11-11 02:03:30 -05:00
|
|
|
Emails []*user_model.EmailAddress
|
2021-07-13 09:28:07 -04:00
|
|
|
Verified bool `xorm:"NOT NULL DEFAULT false"`
|
2017-03-15 21:27:35 -04:00
|
|
|
CanSign bool
|
|
|
|
CanEncryptComms bool
|
|
|
|
CanEncryptStorage bool
|
|
|
|
CanCertify bool
|
|
|
|
}
|
|
|
|
|
2021-09-19 07:49:59 -04:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(GPGKey))
|
|
|
|
}
|
|
|
|
|
2017-03-15 21:27:35 -04:00
|
|
|
// BeforeInsert will be invoked by XORM before inserting a record
|
|
|
|
func (key *GPGKey) BeforeInsert() {
|
2019-08-15 10:46:21 -04:00
|
|
|
key.AddedUnix = timeutil.TimeStampNow()
|
2017-03-15 21:27:35 -04:00
|
|
|
}
|
|
|
|
|
2017-10-01 12:52:35 -04:00
|
|
|
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
|
|
|
|
func (key *GPGKey) AfterLoad(session *xorm.Session) {
|
|
|
|
err := session.Where("primary_key_id=?", key.KeyID).Find(&key.SubsKey)
|
|
|
|
if err != nil {
|
2019-04-02 03:48:31 -04:00
|
|
|
log.Error("Find Sub GPGkeys[%s]: %v", key.KeyID, err)
|
2017-03-15 21:27:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-21 02:50:15 -04:00
|
|
|
// PaddedKeyID show KeyID padded to 16 characters
|
|
|
|
func (key *GPGKey) PaddedKeyID() string {
|
|
|
|
if len(key.KeyID) > 15 {
|
|
|
|
return key.KeyID
|
|
|
|
}
|
|
|
|
zeros := "0000000000000000"
|
|
|
|
return zeros[0:16-len(key.KeyID)] + key.KeyID
|
|
|
|
}
|
|
|
|
|
2017-03-15 21:27:35 -04:00
|
|
|
// ListGPGKeys returns a list of public keys belongs to given user.
|
2021-12-10 03:14:24 -05:00
|
|
|
func ListGPGKeys(ctx context.Context, uid int64, listOptions db.ListOptions) ([]*GPGKey, error) {
|
|
|
|
sess := db.GetEngine(ctx).Table(&GPGKey{}).Where("owner_id=? AND primary_key_id=''", uid)
|
2020-01-24 14:00:29 -05:00
|
|
|
if listOptions.Page != 0 {
|
2021-09-24 07:32:56 -04:00
|
|
|
sess = db.SetSessionPagination(sess, &listOptions)
|
2020-01-24 14:00:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
keys := make([]*GPGKey, 0, 2)
|
|
|
|
return keys, sess.Find(&keys)
|
2017-03-15 21:27:35 -04:00
|
|
|
}
|
|
|
|
|
2021-08-12 08:43:08 -04:00
|
|
|
// CountUserGPGKeys return number of gpg keys a user own
|
|
|
|
func CountUserGPGKeys(userID int64) (int64, error) {
|
2021-09-23 11:45:36 -04:00
|
|
|
return db.GetEngine(db.DefaultContext).Where("owner_id=? AND primary_key_id=''", userID).Count(&GPGKey{})
|
2021-08-12 08:43:08 -04:00
|
|
|
}
|
|
|
|
|
2017-03-15 21:27:35 -04:00
|
|
|
// GetGPGKeyByID returns public key by given ID.
|
|
|
|
func GetGPGKeyByID(keyID int64) (*GPGKey, error) {
|
|
|
|
key := new(GPGKey)
|
2021-09-23 11:45:36 -04:00
|
|
|
has, err := db.GetEngine(db.DefaultContext).ID(keyID).Get(key)
|
2017-03-15 21:27:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, ErrGPGKeyNotExist{keyID}
|
|
|
|
}
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
2019-10-16 09:42:42 -04:00
|
|
|
// GetGPGKeysByKeyID returns public key by given ID.
|
|
|
|
func GetGPGKeysByKeyID(keyID string) ([]*GPGKey, error) {
|
|
|
|
keys := make([]*GPGKey, 0, 1)
|
2021-09-23 11:45:36 -04:00
|
|
|
return keys, db.GetEngine(db.DefaultContext).Where("key_id=?", keyID).Find(&keys)
|
2019-10-16 09:42:42 -04:00
|
|
|
}
|
|
|
|
|
2021-03-14 14:52:12 -04:00
|
|
|
// GPGKeyToEntity retrieve the imported key and the traducted entity
|
2019-04-14 12:43:56 -04:00
|
|
|
func GPGKeyToEntity(k *GPGKey) (*openpgp.Entity, error) {
|
|
|
|
impKey, err := GetGPGImportByKeyID(k.KeyID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-21 06:45:50 -04:00
|
|
|
keys, err := checkArmoredGPGKeyString(impKey.Content)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return keys[0], err
|
2019-04-14 12:43:56 -04:00
|
|
|
}
|
|
|
|
|
2021-03-14 14:52:12 -04:00
|
|
|
// parseSubGPGKey parse a sub Key
|
2017-03-15 21:27:35 -04:00
|
|
|
func parseSubGPGKey(ownerID int64, primaryID string, pubkey *packet.PublicKey, expiry time.Time) (*GPGKey, error) {
|
|
|
|
content, err := base64EncPubKey(pubkey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &GPGKey{
|
|
|
|
OwnerID: ownerID,
|
|
|
|
KeyID: pubkey.KeyIdString(),
|
|
|
|
PrimaryKeyID: primaryID,
|
|
|
|
Content: content,
|
2019-08-15 10:46:21 -04:00
|
|
|
CreatedUnix: timeutil.TimeStamp(pubkey.CreationTime.Unix()),
|
|
|
|
ExpiredUnix: timeutil.TimeStamp(expiry.Unix()),
|
2017-03-15 21:27:35 -04:00
|
|
|
CanSign: pubkey.CanSign(),
|
|
|
|
CanEncryptComms: pubkey.PubKeyAlgo.CanEncrypt(),
|
|
|
|
CanEncryptStorage: pubkey.PubKeyAlgo.CanEncrypt(),
|
|
|
|
CanCertify: pubkey.PubKeyAlgo.CanSign(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-03-14 14:52:12 -04:00
|
|
|
// parseGPGKey parse a PrimaryKey entity (primary key + subs keys + self-signature)
|
2021-07-13 09:28:07 -04:00
|
|
|
func parseGPGKey(ownerID int64, e *openpgp.Entity, verified bool) (*GPGKey, error) {
|
2019-04-15 20:32:15 -04:00
|
|
|
pubkey := e.PrimaryKey
|
|
|
|
expiry := getExpiryTime(e)
|
2017-03-15 21:27:35 -04:00
|
|
|
|
2021-03-14 14:52:12 -04:00
|
|
|
// Parse Subkeys
|
2017-03-15 21:27:35 -04:00
|
|
|
subkeys := make([]*GPGKey, len(e.Subkeys))
|
|
|
|
for i, k := range e.Subkeys {
|
|
|
|
subs, err := parseSubGPGKey(ownerID, pubkey.KeyIdString(), k.PublicKey, expiry)
|
|
|
|
if err != nil {
|
2020-05-28 17:25:54 -04:00
|
|
|
return nil, ErrGPGKeyParsing{ParseError: err}
|
2017-03-15 21:27:35 -04:00
|
|
|
}
|
|
|
|
subkeys[i] = subs
|
|
|
|
}
|
|
|
|
|
2021-03-14 14:52:12 -04:00
|
|
|
// Check emails
|
2021-11-11 02:03:30 -05:00
|
|
|
userEmails, err := user_model.GetEmailAddresses(ownerID)
|
2017-03-15 21:27:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-05 09:45:18 -04:00
|
|
|
|
2021-11-11 02:03:30 -05:00
|
|
|
emails := make([]*user_model.EmailAddress, 0, len(e.Identities))
|
2017-03-15 21:27:35 -04:00
|
|
|
for _, ident := range e.Identities {
|
2020-08-16 04:44:34 -04:00
|
|
|
if ident.Revocation != nil {
|
|
|
|
continue
|
|
|
|
}
|
2017-06-17 06:56:40 -04:00
|
|
|
email := strings.ToLower(strings.TrimSpace(ident.UserId.Email))
|
2017-03-15 21:27:35 -04:00
|
|
|
for _, e := range userEmails {
|
2021-07-13 09:28:07 -04:00
|
|
|
if e.IsActivated && e.LowerEmail == email {
|
2017-09-05 09:45:18 -04:00
|
|
|
emails = append(emails, e)
|
2017-03-15 21:27:35 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-09-05 09:45:18 -04:00
|
|
|
}
|
|
|
|
|
2021-07-13 09:28:07 -04:00
|
|
|
if !verified {
|
|
|
|
// In the case no email as been found
|
|
|
|
if len(emails) == 0 {
|
|
|
|
failedEmails := make([]string, 0, len(e.Identities))
|
|
|
|
for _, ident := range e.Identities {
|
|
|
|
failedEmails = append(failedEmails, ident.UserId.Email)
|
|
|
|
}
|
|
|
|
return nil, ErrGPGNoEmailFound{failedEmails, e.PrimaryKey.KeyIdString()}
|
2017-03-15 21:27:35 -04:00
|
|
|
}
|
|
|
|
}
|
2017-09-05 09:45:18 -04:00
|
|
|
|
2017-03-15 21:27:35 -04:00
|
|
|
content, err := base64EncPubKey(pubkey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &GPGKey{
|
|
|
|
OwnerID: ownerID,
|
|
|
|
KeyID: pubkey.KeyIdString(),
|
|
|
|
PrimaryKeyID: "",
|
|
|
|
Content: content,
|
2019-08-15 10:46:21 -04:00
|
|
|
CreatedUnix: timeutil.TimeStamp(pubkey.CreationTime.Unix()),
|
|
|
|
ExpiredUnix: timeutil.TimeStamp(expiry.Unix()),
|
2017-03-15 21:27:35 -04:00
|
|
|
Emails: emails,
|
|
|
|
SubsKey: subkeys,
|
2021-07-13 09:28:07 -04:00
|
|
|
Verified: verified,
|
2017-03-15 21:27:35 -04:00
|
|
|
CanSign: pubkey.CanSign(),
|
|
|
|
CanEncryptComms: pubkey.PubKeyAlgo.CanEncrypt(),
|
|
|
|
CanEncryptStorage: pubkey.PubKeyAlgo.CanEncrypt(),
|
|
|
|
CanCertify: pubkey.PubKeyAlgo.CanSign(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteGPGKey does the actual key deletion
|
2022-05-20 10:08:52 -04:00
|
|
|
func deleteGPGKey(ctx context.Context, keyID string) (int64, error) {
|
2017-03-15 21:27:35 -04:00
|
|
|
if keyID == "" {
|
2021-03-14 14:52:12 -04:00
|
|
|
return 0, fmt.Errorf("empty KeyId forbidden") // Should never happen but just to be sure
|
2017-03-15 21:27:35 -04:00
|
|
|
}
|
2021-03-14 14:52:12 -04:00
|
|
|
// Delete imported key
|
2022-05-20 10:08:52 -04:00
|
|
|
n, err := db.GetEngine(ctx).Where("key_id=?", keyID).Delete(new(GPGKeyImport))
|
2019-04-14 12:43:56 -04:00
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
2022-05-20 10:08:52 -04:00
|
|
|
return db.GetEngine(ctx).Where("key_id=?", keyID).Or("primary_key_id=?", keyID).Delete(new(GPGKey))
|
2017-03-15 21:27:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteGPGKey deletes GPG key information in database.
|
2021-11-24 04:49:20 -05:00
|
|
|
func DeleteGPGKey(doer *user_model.User, id int64) (err error) {
|
2017-03-15 21:27:35 -04:00
|
|
|
key, err := GetGPGKeyByID(id)
|
|
|
|
if err != nil {
|
|
|
|
if IsErrGPGKeyNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("GetPublicKeyByID: %w", err)
|
2017-03-15 21:27:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if user has access to delete this key.
|
|
|
|
if !doer.IsAdmin && doer.ID != key.OwnerID {
|
|
|
|
return ErrGPGKeyAccessDenied{doer.ID, key.ID}
|
|
|
|
}
|
|
|
|
|
2021-09-19 07:49:59 -04:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2017-03-15 21:27:35 -04:00
|
|
|
return err
|
|
|
|
}
|
2021-09-19 07:49:59 -04:00
|
|
|
defer committer.Close()
|
2017-03-15 21:27:35 -04:00
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
if _, err = deleteGPGKey(ctx, key.KeyID); err != nil {
|
2017-03-15 21:27:35 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-19 07:49:59 -04:00
|
|
|
return committer.Commit()
|
2017-03-15 21:27:35 -04:00
|
|
|
}
|
2017-03-22 06:43:54 -04:00
|
|
|
|
2021-07-13 09:28:07 -04:00
|
|
|
func checkKeyEmails(email string, keys ...*GPGKey) (bool, string) {
|
|
|
|
uid := int64(0)
|
2021-11-11 02:03:30 -05:00
|
|
|
var userEmails []*user_model.EmailAddress
|
2021-11-24 04:49:20 -05:00
|
|
|
var user *user_model.User
|
2019-10-16 09:42:42 -04:00
|
|
|
for _, key := range keys {
|
2021-07-13 09:28:07 -04:00
|
|
|
for _, e := range key.Emails {
|
|
|
|
if e.IsActivated && (email == "" || strings.EqualFold(e.Email, email)) {
|
|
|
|
return true, e.Email
|
2018-03-03 21:45:01 -05:00
|
|
|
}
|
2019-10-16 09:42:42 -04:00
|
|
|
}
|
2021-07-13 09:28:07 -04:00
|
|
|
if key.Verified && key.OwnerID != 0 {
|
|
|
|
if uid != key.OwnerID {
|
2021-11-11 02:03:30 -05:00
|
|
|
userEmails, _ = user_model.GetEmailAddresses(key.OwnerID)
|
2021-07-13 09:28:07 -04:00
|
|
|
uid = key.OwnerID
|
2021-11-24 04:49:20 -05:00
|
|
|
user = &user_model.User{ID: uid}
|
|
|
|
_, _ = user_model.GetUser(user)
|
2017-03-22 06:43:54 -04:00
|
|
|
}
|
2021-07-13 09:28:07 -04:00
|
|
|
for _, e := range userEmails {
|
|
|
|
if e.IsActivated && (email == "" || strings.EqualFold(e.Email, email)) {
|
|
|
|
return true, e.Email
|
2017-09-05 09:45:18 -04:00
|
|
|
}
|
|
|
|
}
|
2021-07-13 09:28:07 -04:00
|
|
|
if user.KeepEmailPrivate && strings.EqualFold(email, user.GetEmail()) {
|
|
|
|
return true, user.GetEmail()
|
2017-03-22 06:43:54 -04:00
|
|
|
}
|
2019-10-16 09:42:42 -04:00
|
|
|
}
|
|
|
|
}
|
2021-07-13 09:28:07 -04:00
|
|
|
return false, email
|
2020-02-27 14:20:55 -05:00
|
|
|
}
|