|
@@ -5,8 +5,10 @@ import (
|
|
|
"crypto/rsa"
|
|
|
"crypto/sha256"
|
|
|
"encoding/base64"
|
|
|
+ "os"
|
|
|
"path"
|
|
|
"strconv"
|
|
|
+ "strings"
|
|
|
|
|
|
"github.com/langgenius/dify-plugin-daemon/internal/core/license/public_key"
|
|
|
"github.com/langgenius/dify-plugin-daemon/internal/utils/encryption"
|
|
@@ -15,19 +17,51 @@ import (
|
|
|
// VerifyPlugin is a function that verifies the signature of a plugin
|
|
|
// It takes a plugin decoder and verifies the signature with a bundled public key
|
|
|
func VerifyPlugin(decoder PluginDecoder) error {
|
|
|
- // load public key
|
|
|
- publicKey, err := encryption.LoadPublicKey(public_key.PUBLIC_KEY)
|
|
|
+ var publicKeys []*rsa.PublicKey
|
|
|
+
|
|
|
+ // load official public key
|
|
|
+ officialPublicKey, err := encryption.LoadPublicKey(public_key.PUBLIC_KEY)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
+ publicKeys = append(publicKeys, officialPublicKey)
|
|
|
|
|
|
// verify the plugin
|
|
|
- return VerifyPluginWithPublicKey(decoder, publicKey)
|
|
|
+ return VerifyPluginWithPublicKeys(decoder, publicKeys)
|
|
|
+}
|
|
|
+
|
|
|
+// VerifyPluginWithPublicKeyPaths is a function that verifies the signature of a plugin
|
|
|
+// It takes a plugin decoder and a list of public key paths to verify the signature
|
|
|
+func VerifyPluginWithPublicKeyPaths(decoder PluginDecoder, publicKeyPaths []string) error {
|
|
|
+ var publicKeys []*rsa.PublicKey
|
|
|
+
|
|
|
+ // load official public key
|
|
|
+ officialPublicKey, err := encryption.LoadPublicKey(public_key.PUBLIC_KEY)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ publicKeys = append(publicKeys, officialPublicKey)
|
|
|
+
|
|
|
+ // load keys provided in the arguments
|
|
|
+ for _, publicKeyPath := range publicKeyPaths {
|
|
|
+ // open file by trimming the spaces in path
|
|
|
+ keyBytes, err := os.ReadFile(strings.TrimSpace(publicKeyPath))
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ publicKey, err := encryption.LoadPublicKey(keyBytes)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ publicKeys = append(publicKeys, publicKey)
|
|
|
+ }
|
|
|
+
|
|
|
+ return VerifyPluginWithPublicKeys(decoder, publicKeys)
|
|
|
}
|
|
|
|
|
|
-// VerifyPluginWithPublicKey is a function that verifies the signature of a plugin
|
|
|
-// It takes a plugin decoder and a public key to verify the signature
|
|
|
-func VerifyPluginWithPublicKey(decoder PluginDecoder, publicKey *rsa.PublicKey) error {
|
|
|
+// VerifyPluginWithPublicKeys is a function that verifies the signature of a plugin
|
|
|
+// It takes a plugin decoder and a list of public keys to verify the signature
|
|
|
+func VerifyPluginWithPublicKeys(decoder PluginDecoder, publicKeys []*rsa.PublicKey) error {
|
|
|
data := new(bytes.Buffer)
|
|
|
// read one by one
|
|
|
err := decoder.Walk(func(filename, dir string) error {
|
|
@@ -70,6 +104,12 @@ func VerifyPluginWithPublicKey(decoder PluginDecoder, publicKey *rsa.PublicKey)
|
|
|
}
|
|
|
|
|
|
// verify signature
|
|
|
- err = encryption.VerifySign(publicKey, data.Bytes(), sigBytes)
|
|
|
- return err
|
|
|
+ var lastErr error
|
|
|
+ for _, publicKey := range publicKeys {
|
|
|
+ lastErr = encryption.VerifySign(publicKey, data.Bytes(), sigBytes)
|
|
|
+ if lastErr == nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return lastErr
|
|
|
}
|