2021-07-13 09:28:07 -04:00
|
|
|
// Copyright 2021 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 (
|
|
|
|
"strings"
|
|
|
|
|
2021-09-19 07:49:59 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-07-13 09:28:07 -04:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
|
|
|
|
"github.com/keybase/go-crypto/openpgp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// __________________ ________ ____ __.
|
|
|
|
// / _____/\______ \/ _____/ | |/ _|____ ___.__.
|
|
|
|
// / \ ___ | ___/ \ ___ | <_/ __ < | |
|
|
|
|
// \ \_\ \| | \ \_\ \ | | \ ___/\___ |
|
|
|
|
// \______ /|____| \______ / |____|__ \___ > ____|
|
|
|
|
// \/ \/ \/ \/\/
|
|
|
|
// _____ .___ .___
|
|
|
|
// / _ \ __| _/__| _/
|
|
|
|
// / /_\ \ / __ |/ __ |
|
|
|
|
// / | \/ /_/ / /_/ |
|
|
|
|
// \____|__ /\____ \____ |
|
|
|
|
// \/ \/ \/
|
|
|
|
|
|
|
|
// This file contains functions relating to adding GPG Keys
|
|
|
|
|
|
|
|
// addGPGKey add key, import and subkeys to database
|
2021-09-19 07:49:59 -04:00
|
|
|
func addGPGKey(e db.Engine, key *GPGKey, content string) (err error) {
|
2021-07-13 09:28:07 -04:00
|
|
|
// Add GPGKeyImport
|
|
|
|
if _, err = e.Insert(GPGKeyImport{
|
|
|
|
KeyID: key.KeyID,
|
|
|
|
Content: content,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Save GPG primary key.
|
|
|
|
if _, err = e.Insert(key); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Save GPG subs key.
|
|
|
|
for _, subkey := range key.SubsKey {
|
|
|
|
if err := addGPGSubKey(e, subkey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// addGPGSubKey add subkeys to database
|
2021-09-19 07:49:59 -04:00
|
|
|
func addGPGSubKey(e db.Engine, key *GPGKey) (err error) {
|
2021-07-13 09:28:07 -04:00
|
|
|
// Save GPG primary key.
|
|
|
|
if _, err = e.Insert(key); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Save GPG subs key.
|
|
|
|
for _, subkey := range key.SubsKey {
|
|
|
|
if err := addGPGSubKey(e, subkey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddGPGKey adds new public key to database.
|
|
|
|
func AddGPGKey(ownerID int64, content, token, signature string) ([]*GPGKey, error) {
|
|
|
|
ekeys, err := checkArmoredGPGKeyString(content)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-19 07:49:59 -04:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2021-07-13 09:28:07 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-19 07:49:59 -04:00
|
|
|
defer committer.Close()
|
|
|
|
|
2021-07-13 09:28:07 -04:00
|
|
|
keys := make([]*GPGKey, 0, len(ekeys))
|
|
|
|
|
|
|
|
verified := false
|
|
|
|
// Handle provided signature
|
|
|
|
if signature != "" {
|
|
|
|
signer, err := openpgp.CheckArmoredDetachedSignature(ekeys, strings.NewReader(token), strings.NewReader(signature))
|
|
|
|
if err != nil {
|
|
|
|
signer, err = openpgp.CheckArmoredDetachedSignature(ekeys, strings.NewReader(token+"\n"), strings.NewReader(signature))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
signer, err = openpgp.CheckArmoredDetachedSignature(ekeys, strings.NewReader(token+"\r\n"), strings.NewReader(signature))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to validate token signature. Error: %v", err)
|
|
|
|
return nil, ErrGPGInvalidTokenSignature{
|
|
|
|
ID: ekeys[0].PrimaryKey.KeyIdString(),
|
|
|
|
Wrapped: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ekeys = []*openpgp.Entity{signer}
|
|
|
|
verified = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ekey := range ekeys {
|
|
|
|
// Key ID cannot be duplicated.
|
2021-09-19 07:49:59 -04:00
|
|
|
has, err := ctx.Engine().Where("key_id=?", ekey.PrimaryKey.KeyIdString()).
|
2021-07-13 09:28:07 -04:00
|
|
|
Get(new(GPGKey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if has {
|
|
|
|
return nil, ErrGPGKeyIDAlreadyUsed{ekey.PrimaryKey.KeyIdString()}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get DB session
|
|
|
|
|
|
|
|
key, err := parseGPGKey(ownerID, ekey, verified)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-19 07:49:59 -04:00
|
|
|
if err = addGPGKey(ctx.Engine(), key, content); err != nil {
|
2021-07-13 09:28:07 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
2021-09-19 07:49:59 -04:00
|
|
|
return keys, committer.Commit()
|
2021-07-13 09:28:07 -04:00
|
|
|
}
|