@@ -106,12 +106,12 @@ func GetGPGImportByKeyID(keyID string) (*GPGKeyImport, error) {
106
106
107
107
// checkArmoredGPGKeyString checks if the given key string is a valid GPG armored key.
108
108
// The function returns the actual public key on success
109
- func checkArmoredGPGKeyString (content string ) (* openpgp.Entity , error ) {
109
+ func checkArmoredGPGKeyString (content string ) (openpgp.EntityList , error ) {
110
110
list , err := openpgp .ReadArmoredKeyRing (strings .NewReader (content ))
111
111
if err != nil {
112
112
return nil , ErrGPGKeyParsing {err }
113
113
}
114
- return list [ 0 ] , nil
114
+ return list , nil
115
115
}
116
116
117
117
//addGPGKey add key, import and subkeys to database
@@ -152,38 +152,40 @@ func addGPGSubKey(e Engine, key *GPGKey) (err error) {
152
152
}
153
153
154
154
// AddGPGKey adds new public key to database.
155
- func AddGPGKey (ownerID int64 , content string ) (* GPGKey , error ) {
156
- ekey , err := checkArmoredGPGKeyString (content )
155
+ func AddGPGKey (ownerID int64 , content string ) ([] * GPGKey , error ) {
156
+ ekeys , err := checkArmoredGPGKeyString (content )
157
157
if err != nil {
158
158
return nil , err
159
159
}
160
-
161
- // Key ID cannot be duplicated.
162
- has , err := x .Where ("key_id=?" , ekey .PrimaryKey .KeyIdString ()).
163
- Get (new (GPGKey ))
164
- if err != nil {
165
- return nil , err
166
- } else if has {
167
- return nil , ErrGPGKeyIDAlreadyUsed {ekey .PrimaryKey .KeyIdString ()}
168
- }
169
-
170
- //Get DB session
171
160
sess := x .NewSession ()
172
161
defer sess .Close ()
173
162
if err = sess .Begin (); err != nil {
174
163
return nil , err
175
164
}
165
+ keys := make ([]* GPGKey , 0 , len (ekeys ))
166
+ for _ , ekey := range ekeys {
167
+ // Key ID cannot be duplicated.
168
+ has , err := sess .Where ("key_id=?" , ekey .PrimaryKey .KeyIdString ()).
169
+ Get (new (GPGKey ))
170
+ if err != nil {
171
+ return nil , err
172
+ } else if has {
173
+ return nil , ErrGPGKeyIDAlreadyUsed {ekey .PrimaryKey .KeyIdString ()}
174
+ }
176
175
177
- key , err := parseGPGKey (ownerID , ekey )
178
- if err != nil {
179
- return nil , err
180
- }
176
+ //Get DB session
181
177
182
- if err = addGPGKey (sess , key , content ); err != nil {
183
- return nil , err
184
- }
178
+ key , err := parseGPGKey (ownerID , ekey )
179
+ if err != nil {
180
+ return nil , err
181
+ }
185
182
186
- return key , sess .Commit ()
183
+ if err = addGPGKey (sess , key , content ); err != nil {
184
+ return nil , err
185
+ }
186
+ keys = append (keys , key )
187
+ }
188
+ return keys , sess .Commit ()
187
189
}
188
190
189
191
//base64EncPubKey encode public key content to base 64
@@ -221,7 +223,11 @@ func GPGKeyToEntity(k *GPGKey) (*openpgp.Entity, error) {
221
223
if err != nil {
222
224
return nil , err
223
225
}
224
- return checkArmoredGPGKeyString (impKey .Content )
226
+ keys , err := checkArmoredGPGKeyString (impKey .Content )
227
+ if err != nil {
228
+ return nil , err
229
+ }
230
+ return keys [0 ], err
225
231
}
226
232
227
233
//parseSubGPGKey parse a sub Key
@@ -758,7 +764,7 @@ func verifyWithGPGSettings(gpgSettings *git.GPGSettings, sig *packet.Signature,
758
764
}
759
765
760
766
// Otherwise we have to parse the key
761
- ekey , err := checkArmoredGPGKeyString (gpgSettings .PublicKeyContent )
767
+ ekeys , err := checkArmoredGPGKeyString (gpgSettings .PublicKeyContent )
762
768
if err != nil {
763
769
log .Error ("Unable to get default signing key: %v" , err )
764
770
return & CommitVerification {
@@ -767,48 +773,50 @@ func verifyWithGPGSettings(gpgSettings *git.GPGSettings, sig *packet.Signature,
767
773
Reason : "gpg.error.generate_hash" ,
768
774
}
769
775
}
770
- pubkey := ekey .PrimaryKey
771
- content , err := base64EncPubKey (pubkey )
772
- if err != nil {
773
- return & CommitVerification {
774
- CommittingUser : committer ,
775
- Verified : false ,
776
- Reason : "gpg.error.generate_hash" ,
777
- }
778
- }
779
- k := & GPGKey {
780
- Content : content ,
781
- CanSign : pubkey .CanSign (),
782
- KeyID : pubkey .KeyIdString (),
783
- }
784
- for _ , subKey := range ekey .Subkeys {
785
- content , err := base64EncPubKey (subKey .PublicKey )
776
+ for _ , ekey := range ekeys {
777
+ pubkey := ekey .PrimaryKey
778
+ content , err := base64EncPubKey (pubkey )
786
779
if err != nil {
787
780
return & CommitVerification {
788
781
CommittingUser : committer ,
789
782
Verified : false ,
790
783
Reason : "gpg.error.generate_hash" ,
791
784
}
792
785
}
793
- k . SubsKey = append ( k . SubsKey , & GPGKey {
786
+ k := & GPGKey {
794
787
Content : content ,
795
- CanSign : subKey .PublicKey .CanSign (),
796
- KeyID : subKey .PublicKey .KeyIdString (),
797
- })
798
- }
799
- if commitVerification := hashAndVerifyWithSubKeys (sig , payload , k , committer , & User {
800
- Name : gpgSettings .Name ,
801
- Email : gpgSettings .Email ,
802
- }, gpgSettings .Email ); commitVerification != nil {
803
- return commitVerification
804
- }
805
- if keyID == k .KeyID {
806
- // This is a bad situation ... We have a key id that matches our default key but the signature doesn't match.
807
- return & CommitVerification {
808
- CommittingUser : committer ,
809
- Verified : false ,
810
- Warning : true ,
811
- Reason : BadSignature ,
788
+ CanSign : pubkey .CanSign (),
789
+ KeyID : pubkey .KeyIdString (),
790
+ }
791
+ for _ , subKey := range ekey .Subkeys {
792
+ content , err := base64EncPubKey (subKey .PublicKey )
793
+ if err != nil {
794
+ return & CommitVerification {
795
+ CommittingUser : committer ,
796
+ Verified : false ,
797
+ Reason : "gpg.error.generate_hash" ,
798
+ }
799
+ }
800
+ k .SubsKey = append (k .SubsKey , & GPGKey {
801
+ Content : content ,
802
+ CanSign : subKey .PublicKey .CanSign (),
803
+ KeyID : subKey .PublicKey .KeyIdString (),
804
+ })
805
+ }
806
+ if commitVerification := hashAndVerifyWithSubKeys (sig , payload , k , committer , & User {
807
+ Name : gpgSettings .Name ,
808
+ Email : gpgSettings .Email ,
809
+ }, gpgSettings .Email ); commitVerification != nil {
810
+ return commitVerification
811
+ }
812
+ if keyID == k .KeyID {
813
+ // This is a bad situation ... We have a key id that matches our default key but the signature doesn't match.
814
+ return & CommitVerification {
815
+ CommittingUser : committer ,
816
+ Verified : false ,
817
+ Warning : true ,
818
+ Reason : BadSignature ,
819
+ }
812
820
}
813
821
}
814
822
return nil
0 commit comments