2017-03-16 06:57:35 +05:30
|
|
|
// 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.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-03-22 16:13:54 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-08-15 20:16:21 +05:30
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2017-03-22 16:13:54 +05:30
|
|
|
|
2017-06-14 06:13:43 +05:30
|
|
|
"github.com/keybase/go-crypto/openpgp"
|
|
|
|
"github.com/keybase/go-crypto/openpgp/packet"
|
2019-10-17 14:56:49 +05:30
|
|
|
"xorm.io/xorm"
|
2017-03-16 06:57:35 +05:30
|
|
|
)
|
|
|
|
|
2021-07-13 18:58:07 +05:30
|
|
|
// __________________ ________ ____ __.
|
|
|
|
// / _____/\______ \/ _____/ | |/ _|____ ___.__.
|
|
|
|
// / \ ___ | ___/ \ ___ | <_/ __ < | |
|
|
|
|
// \ \_\ \| | \ \_\ \ | | \ ___/\___ |
|
|
|
|
// \______ /|____| \______ / |____|__ \___ > ____|
|
|
|
|
// \/ \/ \/ \/\/
|
|
|
|
|
2017-03-16 06:57:35 +05:30
|
|
|
// GPGKey represents a GPG key.
|
|
|
|
type GPGKey struct {
|
2019-08-15 20:16:21 +05:30
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
OwnerID int64 `xorm:"INDEX NOT NULL"`
|
|
|
|
KeyID string `xorm:"INDEX CHAR(16) NOT NULL"`
|
|
|
|
PrimaryKeyID string `xorm:"CHAR(16)"`
|
|
|
|
Content string `xorm:"TEXT NOT NULL"`
|
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
|
|
|
ExpiredUnix timeutil.TimeStamp
|
|
|
|
AddedUnix timeutil.TimeStamp
|
2017-03-16 06:57:35 +05:30
|
|
|
SubsKey []*GPGKey `xorm:"-"`
|
|
|
|
Emails []*EmailAddress
|
2021-07-13 18:58:07 +05:30
|
|
|
Verified bool `xorm:"NOT NULL DEFAULT false"`
|
2017-03-16 06:57:35 +05:30
|
|
|
CanSign bool
|
|
|
|
CanEncryptComms bool
|
|
|
|
CanEncryptStorage bool
|
|
|
|
CanCertify bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// BeforeInsert will be invoked by XORM before inserting a record
|
|
|
|
func (key *GPGKey) BeforeInsert() {
|
2019-08-15 20:16:21 +05:30
|
|
|
key.AddedUnix = timeutil.TimeStampNow()
|
2017-03-16 06:57:35 +05:30
|
|
|
}
|
|
|
|
|
2017-10-01 22:22:35 +05:30
|
|
|
// 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 13:18:31 +05:30
|
|
|
log.Error("Find Sub GPGkeys[%s]: %v", key.KeyID, err)
|
2017-03-16 06:57:35 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListGPGKeys returns a list of public keys belongs to given user.
|
2020-01-25 00:30:29 +05:30
|
|
|
func ListGPGKeys(uid int64, listOptions ListOptions) ([]*GPGKey, error) {
|
2021-02-04 14:46:21 +05:30
|
|
|
return listGPGKeys(x, uid, listOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
func listGPGKeys(e Engine, uid int64, listOptions ListOptions) ([]*GPGKey, error) {
|
|
|
|
sess := e.Table(&GPGKey{}).Where("owner_id=? AND primary_key_id=''", uid)
|
2020-01-25 00:30:29 +05:30
|
|
|
if listOptions.Page != 0 {
|
|
|
|
sess = listOptions.setSessionPagination(sess)
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := make([]*GPGKey, 0, 2)
|
|
|
|
return keys, sess.Find(&keys)
|
2017-03-16 06:57:35 +05:30
|
|
|
}
|
|
|
|
|
2021-08-12 18:13:08 +05:30
|
|
|
// CountUserGPGKeys return number of gpg keys a user own
|
|
|
|
func CountUserGPGKeys(userID int64) (int64, error) {
|
|
|
|
return x.Where("owner_id=? AND primary_key_id=''", userID).Count(&GPGKey{})
|
|
|
|
}
|
|
|
|
|
2017-03-16 06:57:35 +05:30
|
|
|
// GetGPGKeyByID returns public key by given ID.
|
|
|
|
func GetGPGKeyByID(keyID int64) (*GPGKey, error) {
|
|
|
|
key := new(GPGKey)
|
2017-10-05 10:13:04 +05:30
|
|
|
has, err := x.ID(keyID).Get(key)
|
2017-03-16 06:57:35 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, ErrGPGKeyNotExist{keyID}
|
|
|
|
}
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
2019-10-16 19:12:42 +05:30
|
|
|
// GetGPGKeysByKeyID returns public key by given ID.
|
|
|
|
func GetGPGKeysByKeyID(keyID string) ([]*GPGKey, error) {
|
|
|
|
keys := make([]*GPGKey, 0, 1)
|
|
|
|
return keys, x.Where("key_id=?", keyID).Find(&keys)
|
|
|
|
}
|
|
|
|
|
2021-03-15 00:22:12 +05:30
|
|
|
// GPGKeyToEntity retrieve the imported key and the traducted entity
|
2019-04-14 22:13:56 +05:30
|
|
|
func GPGKeyToEntity(k *GPGKey) (*openpgp.Entity, error) {
|
|
|
|
impKey, err := GetGPGImportByKeyID(k.KeyID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-21 16:15:50 +05:30
|
|
|
keys, err := checkArmoredGPGKeyString(impKey.Content)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return keys[0], err
|
2019-04-14 22:13:56 +05:30
|
|
|
}
|
|
|
|
|
2021-03-15 00:22:12 +05:30
|
|
|
// parseSubGPGKey parse a sub Key
|
2017-03-16 06:57:35 +05:30
|
|
|
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 20:16:21 +05:30
|
|
|
CreatedUnix: timeutil.TimeStamp(pubkey.CreationTime.Unix()),
|
|
|
|
ExpiredUnix: timeutil.TimeStamp(expiry.Unix()),
|
2017-03-16 06:57:35 +05:30
|
|
|
CanSign: pubkey.CanSign(),
|
|
|
|
CanEncryptComms: pubkey.PubKeyAlgo.CanEncrypt(),
|
|
|
|
CanEncryptStorage: pubkey.PubKeyAlgo.CanEncrypt(),
|
|
|
|
CanCertify: pubkey.PubKeyAlgo.CanSign(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-03-15 00:22:12 +05:30
|
|
|
// parseGPGKey parse a PrimaryKey entity (primary key + subs keys + self-signature)
|
2021-07-13 18:58:07 +05:30
|
|
|
func parseGPGKey(ownerID int64, e *openpgp.Entity, verified bool) (*GPGKey, error) {
|
2019-04-16 06:02:15 +05:30
|
|
|
pubkey := e.PrimaryKey
|
|
|
|
expiry := getExpiryTime(e)
|
2017-03-16 06:57:35 +05:30
|
|
|
|
2021-03-15 00:22:12 +05:30
|
|
|
// Parse Subkeys
|
2017-03-16 06:57:35 +05:30
|
|
|
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-29 02:55:54 +05:30
|
|
|
return nil, ErrGPGKeyParsing{ParseError: err}
|
2017-03-16 06:57:35 +05:30
|
|
|
}
|
|
|
|
subkeys[i] = subs
|
|
|
|
}
|
|
|
|
|
2021-03-15 00:22:12 +05:30
|
|
|
// Check emails
|
2017-03-16 06:57:35 +05:30
|
|
|
userEmails, err := GetEmailAddresses(ownerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-05 19:15:18 +05:30
|
|
|
|
|
|
|
emails := make([]*EmailAddress, 0, len(e.Identities))
|
2017-03-16 06:57:35 +05:30
|
|
|
for _, ident := range e.Identities {
|
2020-08-16 14:14:34 +05:30
|
|
|
if ident.Revocation != nil {
|
|
|
|
continue
|
|
|
|
}
|
2017-06-17 16:26:40 +05:30
|
|
|
email := strings.ToLower(strings.TrimSpace(ident.UserId.Email))
|
2017-03-16 06:57:35 +05:30
|
|
|
for _, e := range userEmails {
|
2021-07-13 18:58:07 +05:30
|
|
|
if e.IsActivated && e.LowerEmail == email {
|
2017-09-05 19:15:18 +05:30
|
|
|
emails = append(emails, e)
|
2017-03-16 06:57:35 +05:30
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-09-05 19:15:18 +05:30
|
|
|
}
|
|
|
|
|
2021-07-13 18:58:07 +05:30
|
|
|
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-16 06:57:35 +05:30
|
|
|
}
|
|
|
|
}
|
2017-09-05 19:15:18 +05:30
|
|
|
|
2017-03-16 06:57:35 +05:30
|
|
|
content, err := base64EncPubKey(pubkey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &GPGKey{
|
|
|
|
OwnerID: ownerID,
|
|
|
|
KeyID: pubkey.KeyIdString(),
|
|
|
|
PrimaryKeyID: "",
|
|
|
|
Content: content,
|
2019-08-15 20:16:21 +05:30
|
|
|
CreatedUnix: timeutil.TimeStamp(pubkey.CreationTime.Unix()),
|
|
|
|
ExpiredUnix: timeutil.TimeStamp(expiry.Unix()),
|
2017-03-16 06:57:35 +05:30
|
|
|
Emails: emails,
|
|
|
|
SubsKey: subkeys,
|
2021-07-13 18:58:07 +05:30
|
|
|
Verified: verified,
|
2017-03-16 06:57:35 +05:30
|
|
|
CanSign: pubkey.CanSign(),
|
|
|
|
CanEncryptComms: pubkey.PubKeyAlgo.CanEncrypt(),
|
|
|
|
CanEncryptStorage: pubkey.PubKeyAlgo.CanEncrypt(),
|
|
|
|
CanCertify: pubkey.PubKeyAlgo.CanSign(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteGPGKey does the actual key deletion
|
|
|
|
func deleteGPGKey(e *xorm.Session, keyID string) (int64, error) {
|
|
|
|
if keyID == "" {
|
2021-03-15 00:22:12 +05:30
|
|
|
return 0, fmt.Errorf("empty KeyId forbidden") // Should never happen but just to be sure
|
2017-03-16 06:57:35 +05:30
|
|
|
}
|
2021-03-15 00:22:12 +05:30
|
|
|
// Delete imported key
|
2019-04-14 22:13:56 +05:30
|
|
|
n, err := e.Where("key_id=?", keyID).Delete(new(GPGKeyImport))
|
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
2017-03-16 06:57:35 +05:30
|
|
|
return e.Where("key_id=?", keyID).Or("primary_key_id=?", keyID).Delete(new(GPGKey))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteGPGKey deletes GPG key information in database.
|
|
|
|
func DeleteGPGKey(doer *User, id int64) (err error) {
|
|
|
|
key, err := GetGPGKeyByID(id)
|
|
|
|
if err != nil {
|
|
|
|
if IsErrGPGKeyNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("GetPublicKeyByID: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if user has access to delete this key.
|
|
|
|
if !doer.IsAdmin && doer.ID != key.OwnerID {
|
|
|
|
return ErrGPGKeyAccessDenied{doer.ID, key.ID}
|
|
|
|
}
|
|
|
|
|
|
|
|
sess := x.NewSession()
|
2017-06-21 06:27:05 +05:30
|
|
|
defer sess.Close()
|
2017-03-16 06:57:35 +05:30
|
|
|
if err = sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = deleteGPGKey(sess, key.KeyID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-19 13:38:30 +05:30
|
|
|
return sess.Commit()
|
2017-03-16 06:57:35 +05:30
|
|
|
}
|
2017-03-22 16:13:54 +05:30
|
|
|
|
2021-07-13 18:58:07 +05:30
|
|
|
func checkKeyEmails(email string, keys ...*GPGKey) (bool, string) {
|
|
|
|
uid := int64(0)
|
|
|
|
var userEmails []*EmailAddress
|
|
|
|
var user *User
|
2019-10-16 19:12:42 +05:30
|
|
|
for _, key := range keys {
|
2021-07-13 18:58:07 +05:30
|
|
|
for _, e := range key.Emails {
|
|
|
|
if e.IsActivated && (email == "" || strings.EqualFold(e.Email, email)) {
|
|
|
|
return true, e.Email
|
2018-03-04 08:15:01 +05:30
|
|
|
}
|
2019-10-16 19:12:42 +05:30
|
|
|
}
|
2021-07-13 18:58:07 +05:30
|
|
|
if key.Verified && key.OwnerID != 0 {
|
|
|
|
if uid != key.OwnerID {
|
|
|
|
userEmails, _ = GetEmailAddresses(key.OwnerID)
|
|
|
|
uid = key.OwnerID
|
|
|
|
user = &User{ID: uid}
|
|
|
|
_, _ = GetUser(user)
|
2017-03-22 16:13:54 +05:30
|
|
|
}
|
2021-07-13 18:58:07 +05:30
|
|
|
for _, e := range userEmails {
|
|
|
|
if e.IsActivated && (email == "" || strings.EqualFold(e.Email, email)) {
|
|
|
|
return true, e.Email
|
2017-09-05 19:15:18 +05:30
|
|
|
}
|
|
|
|
}
|
2021-07-13 18:58:07 +05:30
|
|
|
if user.KeepEmailPrivate && strings.EqualFold(email, user.GetEmail()) {
|
|
|
|
return true, user.GetEmail()
|
2017-03-22 16:13:54 +05:30
|
|
|
}
|
2019-10-16 19:12:42 +05:30
|
|
|
}
|
|
|
|
}
|
2021-07-13 18:58:07 +05:30
|
|
|
return false, email
|
2020-02-28 00:50:55 +05:30
|
|
|
}
|