aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
Diffstat (limited to 'models')
-rw-r--r--models/error.go15
-rw-r--r--models/fixtures/gpg_key_import.yml1
-rw-r--r--models/gpg_key.go97
-rw-r--r--models/migrations/migrations.go2
-rw-r--r--models/migrations/v84.go18
-rw-r--r--models/models.go1
-rw-r--r--models/user.go2
7 files changed, 117 insertions, 19 deletions
diff --git a/models/error.go b/models/error.go
index 6a135bda1a..f079af4e1f 100644
--- a/models/error.go
+++ b/models/error.go
@@ -379,6 +379,21 @@ func (err ErrGPGKeyNotExist) Error() string {
return fmt.Sprintf("public gpg key does not exist [id: %d]", err.ID)
}
+// ErrGPGKeyImportNotExist represents a "GPGKeyImportNotExist" kind of error.
+type ErrGPGKeyImportNotExist struct {
+ ID string
+}
+
+// IsErrGPGKeyImportNotExist checks if an error is a ErrGPGKeyImportNotExist.
+func IsErrGPGKeyImportNotExist(err error) bool {
+ _, ok := err.(ErrGPGKeyImportNotExist)
+ return ok
+}
+
+func (err ErrGPGKeyImportNotExist) Error() string {
+ return fmt.Sprintf("public gpg key import does not exist [id: %s]", err.ID)
+}
+
// ErrGPGKeyIDAlreadyUsed represents a "GPGKeyIDAlreadyUsed" kind of error.
type ErrGPGKeyIDAlreadyUsed struct {
KeyID string
diff --git a/models/fixtures/gpg_key_import.yml b/models/fixtures/gpg_key_import.yml
new file mode 100644
index 0000000000..ca780a73aa
--- /dev/null
+++ b/models/fixtures/gpg_key_import.yml
@@ -0,0 +1 @@
+[] # empty
diff --git a/models/gpg_key.go b/models/gpg_key.go
index 0352456e54..2e10fd782a 100644
--- a/models/gpg_key.go
+++ b/models/gpg_key.go
@@ -43,6 +43,12 @@ type GPGKey struct {
CanCertify bool
}
+//GPGKeyImport the original import of key
+type GPGKeyImport struct {
+ KeyID string `xorm:"pk CHAR(16) NOT NULL"`
+ Content string `xorm:"TEXT NOT NULL"`
+}
+
// BeforeInsert will be invoked by XORM before inserting a record
func (key *GPGKey) BeforeInsert() {
key.AddedUnix = util.TimeStampNow()
@@ -74,6 +80,18 @@ func GetGPGKeyByID(keyID int64) (*GPGKey, error) {
return key, nil
}
+// GetGPGImportByKeyID returns the import public armored key by given KeyID.
+func GetGPGImportByKeyID(keyID string) (*GPGKeyImport, error) {
+ key := new(GPGKeyImport)
+ has, err := x.ID(keyID).Get(key)
+ if err != nil {
+ return nil, err
+ } else if !has {
+ return nil, ErrGPGKeyImportNotExist{keyID}
+ }
+ return key, nil
+}
+
// checkArmoredGPGKeyString checks if the given key string is a valid GPG armored key.
// The function returns the actual public key on success
func checkArmoredGPGKeyString(content string) (*openpgp.Entity, error) {
@@ -84,15 +102,37 @@ func checkArmoredGPGKeyString(content string) (*openpgp.Entity, error) {
return list[0], nil
}
-//addGPGKey add key and subkeys to database
-func addGPGKey(e Engine, key *GPGKey) (err error) {
+//addGPGKey add key, import and subkeys to database
+func addGPGKey(e Engine, key *GPGKey, content string) (err error) {
+ //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
+func addGPGSubKey(e Engine, key *GPGKey) (err error) {
// Save GPG primary key.
if _, err = e.Insert(key); err != nil {
return err
}
// Save GPG subs key.
for _, subkey := range key.SubsKey {
- if err := addGPGKey(e, subkey); err != nil {
+ if err := addGPGSubKey(e, subkey); err != nil {
return err
}
}
@@ -127,14 +167,14 @@ func AddGPGKey(ownerID int64, content string) (*GPGKey, error) {
return nil, err
}
- if err = addGPGKey(sess, key); err != nil {
+ if err = addGPGKey(sess, key, content); err != nil {
return nil, err
}
return key, sess.Commit()
}
-//base64EncPubKey encode public kay content to base 64
+//base64EncPubKey encode public key content to base 64
func base64EncPubKey(pubkey *packet.PublicKey) (string, error) {
var w bytes.Buffer
err := pubkey.Serialize(&w)
@@ -144,6 +184,34 @@ func base64EncPubKey(pubkey *packet.PublicKey) (string, error) {
return base64.StdEncoding.EncodeToString(w.Bytes()), nil
}
+//base64DecPubKey decode public key content from base 64
+func base64DecPubKey(content string) (*packet.PublicKey, error) {
+ b, err := readerFromBase64(content)
+ if err != nil {
+ return nil, err
+ }
+ //Read key
+ p, err := packet.Read(b)
+ if err != nil {
+ return nil, err
+ }
+ //Check type
+ pkey, ok := p.(*packet.PublicKey)
+ if !ok {
+ return nil, fmt.Errorf("key is not a public key")
+ }
+ return pkey, nil
+}
+
+//GPGKeyToEntity retrieve the imported key and the traducted entity
+func GPGKeyToEntity(k *GPGKey) (*openpgp.Entity, error) {
+ impKey, err := GetGPGImportByKeyID(k.KeyID)
+ if err != nil {
+ return nil, err
+ }
+ return checkArmoredGPGKeyString(impKey.Content)
+}
+
//parseSubGPGKey parse a sub Key
func parseSubGPGKey(ownerID int64, primaryID string, pubkey *packet.PublicKey, expiry time.Time) (*GPGKey, error) {
content, err := base64EncPubKey(pubkey)
@@ -244,6 +312,11 @@ func deleteGPGKey(e *xorm.Session, keyID string) (int64, error) {
if keyID == "" {
return 0, fmt.Errorf("empty KeyId forbidden") //Should never happen but just to be sure
}
+ //Delete imported key
+ n, err := e.Where("key_id=?", keyID).Delete(new(GPGKeyImport))
+ if err != nil {
+ return n, err
+ }
return e.Where("key_id=?", keyID).Or("primary_key_id=?", keyID).Delete(new(GPGKey))
}
@@ -339,22 +412,10 @@ func verifySign(s *packet.Signature, h hash.Hash, k *GPGKey) error {
return fmt.Errorf("key can not sign")
}
//Decode key
- b, err := readerFromBase64(k.Content)
- if err != nil {
- return err
- }
- //Read key
- p, err := packet.Read(b)
+ pkey, err := base64DecPubKey(k.Content)
if err != nil {
return err
}
-
- //Check type
- pkey, ok := p.(*packet.PublicKey)
- if !ok {
- return fmt.Errorf("key is not a public key")
- }
-
return pkey.VerifySignature(h, s)
}
diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go
index baedcbb715..62c41fb906 100644
--- a/models/migrations/migrations.go
+++ b/models/migrations/migrations.go
@@ -221,6 +221,8 @@ var migrations = []Migration{
NewMigration("hot fix for wrong release sha1 on release table", fixReleaseSha1OnReleaseTable),
// v83 -> v84
NewMigration("add uploader id for table attachment", addUploaderIDForAttachment),
+ // v84 -> v85
+ NewMigration("add table to store original imported gpg keys", addGPGKeyImport),
}
// Migrate database to current version
diff --git a/models/migrations/v84.go b/models/migrations/v84.go
new file mode 100644
index 0000000000..4acb94b9ce
--- /dev/null
+++ b/models/migrations/v84.go
@@ -0,0 +1,18 @@
+// Copyright 2019 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 migrations
+
+import (
+ "github.com/go-xorm/xorm"
+)
+
+func addGPGKeyImport(x *xorm.Engine) error {
+ type GPGKeyImport struct {
+ KeyID string `xorm:"pk CHAR(16) NOT NULL"`
+ Content string `xorm:"TEXT NOT NULL"`
+ }
+
+ return x.Sync2(new(GPGKeyImport))
+}
diff --git a/models/models.go b/models/models.go
index e7ecc67fc5..352c07e0c3 100644
--- a/models/models.go
+++ b/models/models.go
@@ -108,6 +108,7 @@ func init() {
new(LFSMetaObject),
new(TwoFactor),
new(GPGKey),
+ new(GPGKeyImport),
new(RepoUnit),
new(RepoRedirect),
new(ExternalLoginUser),
diff --git a/models/user.go b/models/user.go
index 723892b0b6..93fdc6f4af 100644
--- a/models/user.go
+++ b/models/user.go
@@ -747,7 +747,7 @@ var (
".",
"..",
}
- reservedUserPatterns = []string{"*.keys"}
+ reservedUserPatterns = []string{"*.keys", "*.gpg"}
)
// isUsableName checks if name is reserved or pattern of name is not allowed