|
|
@ -8,8 +8,6 @@ import ( |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
"sync" |
|
|
|
"sync" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/harmony-one/harmony/crypto/bls" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bls_core "github.com/harmony-one/bls/ffi/go/bls" |
|
|
|
bls_core "github.com/harmony-one/bls/ffi/go/bls" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
@ -36,79 +34,84 @@ func (srcType PassSrcType) isValid() bool { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type passDecrypter struct { |
|
|
|
// passDecrypterConfig is the data structure of passProviders config
|
|
|
|
pps []passProvider |
|
|
|
type passDecrypterConfig struct { |
|
|
|
} |
|
|
|
passSrcType PassSrcType |
|
|
|
|
|
|
|
passFile *string |
|
|
|
func newPassDecrypter(cfg passDecrypterConfig) *passDecrypter { |
|
|
|
persistPassphrase bool |
|
|
|
pps := cfg.makePassProviders() |
|
|
|
|
|
|
|
return &passDecrypter{pps} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (pd *passDecrypter) decrypt(keyFile string) (*bls_core.SecretKey, error) { |
|
|
|
// passDecrypter decrypt the .key bls files with passphrase from a series
|
|
|
|
for _, pp := range pd.pps { |
|
|
|
// of passProvider as passphrase source
|
|
|
|
|
|
|
|
type passDecrypter struct { |
|
|
|
|
|
|
|
config passDecrypterConfig |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
pps []passProvider |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (pd *passDecrypter) checkDecryptResult(keyFile string, got *bls_core.SecretKey) error { |
|
|
|
func newPassDecrypter(cfg passDecrypterConfig) (*passDecrypter, error) { |
|
|
|
expPubKey, err := getPubKeyFromFilePath(keyFile, passExt) |
|
|
|
pd := &passDecrypter{config: cfg} |
|
|
|
if err != nil { |
|
|
|
if err := pd.validate(); err != nil { |
|
|
|
if err == errUnableGetPubkey { |
|
|
|
return nil, err |
|
|
|
// file name not bls pub key + .pass
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
gotPubKey := *bls.FromLibBLSPublicKeyUnsafe(got.GetPublicKey()) |
|
|
|
|
|
|
|
if expPubKey != gotPubKey { |
|
|
|
|
|
|
|
return errors.New("public key unexpected") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return nil |
|
|
|
pd.makePassProviders() |
|
|
|
|
|
|
|
return pd, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// passDecrypterConfig is the data structure of passProviders config
|
|
|
|
func (pd *passDecrypter) extension() string { |
|
|
|
type passDecrypterConfig struct { |
|
|
|
return basicKeyExt |
|
|
|
passSrcType PassSrcType |
|
|
|
|
|
|
|
passFile *string |
|
|
|
|
|
|
|
passDir *string |
|
|
|
|
|
|
|
persistPassphrase bool |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (config passDecrypterConfig) validate() error { |
|
|
|
func (pd *passDecrypter) validate() error { |
|
|
|
|
|
|
|
config := pd.config |
|
|
|
if !config.passSrcType.isValid() { |
|
|
|
if !config.passSrcType.isValid() { |
|
|
|
return errors.New("unknown PassSrcType") |
|
|
|
return errors.New("unknown PassSrcType") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if stringIsSet(config.passFile) { |
|
|
|
|
|
|
|
if err := isPassFile(*config.passFile); err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("%v not a passphrase file: %v", *config.passFile, err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (config passDecrypterConfig) makePassProviders() []passProvider { |
|
|
|
func (pd *passDecrypter) makePassProviders() { |
|
|
|
switch config.passSrcType { |
|
|
|
switch pd.config.passSrcType { |
|
|
|
case PassSrcFile: |
|
|
|
case PassSrcFile: |
|
|
|
return []passProvider{config.getFilePassProvider()} |
|
|
|
pd.pps = []passProvider{pd.getFilePassProvider()} |
|
|
|
case PassSrcPrompt: |
|
|
|
case PassSrcPrompt: |
|
|
|
return []passProvider{config.getPromptPassProvider()} |
|
|
|
pd.pps = []passProvider{pd.getPromptPassProvider()} |
|
|
|
case PassSrcAuto: |
|
|
|
case PassSrcAuto: |
|
|
|
return []passProvider{ |
|
|
|
pd.pps = []passProvider{ |
|
|
|
config.getFilePassProvider(), |
|
|
|
pd.getFilePassProvider(), |
|
|
|
config.getPromptPassProvider(), |
|
|
|
pd.getPromptPassProvider(), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (config passDecrypterConfig) getFilePassProvider() passProvider { |
|
|
|
func (pd *passDecrypter) getPromptPassProvider() passProvider { |
|
|
|
|
|
|
|
return newPromptPassProvider(pd.config.persistPassphrase) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (pd *passDecrypter) getFilePassProvider() passProvider { |
|
|
|
switch { |
|
|
|
switch { |
|
|
|
case stringIsSet(config.passFile): |
|
|
|
case stringIsSet(pd.config.passFile): |
|
|
|
return newStaticPassProvider(*config.passFile) |
|
|
|
return newStaticPassProvider(*pd.config.passFile) |
|
|
|
case stringIsSet(config.passDir): |
|
|
|
|
|
|
|
return newDirPassProvider(*config.passDir) |
|
|
|
|
|
|
|
default: |
|
|
|
default: |
|
|
|
return newDynamicPassProvider() |
|
|
|
return newDynamicPassProvider() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (config passDecrypterConfig) getPromptPassProvider() passProvider { |
|
|
|
func (pd *passDecrypter) decrypt(keyFile string) (*bls_core.SecretKey, error) { |
|
|
|
return newPromptPassProvider(config.persistPassphrase) |
|
|
|
for _, pp := range pd.pps { |
|
|
|
|
|
|
|
secretKey, err := loadBasicKeyWithProvider(keyFile, pp) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
console.println(err) |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return secretKey, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return nil, fmt.Errorf("failed to load bls key %v", keyFile) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// passProvider is the interface to provide the passphrase of a bls keys.
|
|
|
|
// passProvider is the interface to provide the passphrase of a bls keys.
|
|
|
@ -200,30 +203,6 @@ func newDynamicPassProvider() passProvider { |
|
|
|
|
|
|
|
|
|
|
|
func (provider *dynamicPassProvider) getPassphrase(keyFile string) (string, error) { |
|
|
|
func (provider *dynamicPassProvider) getPassphrase(keyFile string) (string, error) { |
|
|
|
passFile := keyFileToPassFileFull(keyFile) |
|
|
|
passFile := keyFileToPassFileFull(keyFile) |
|
|
|
if !isPassFile(passFile) { |
|
|
|
|
|
|
|
return "", fmt.Errorf("pass file %v not exist", passFile) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return readPassFromFile(passFile) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// dirPassProvider provide the all bls password available in the directory.
|
|
|
|
|
|
|
|
type dirPassProvider struct { |
|
|
|
|
|
|
|
dirPath string |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (provider *dirPassProvider) toStr() string { |
|
|
|
|
|
|
|
return "directory " + provider.dirPath |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func newDirPassProvider(dirPath string) *dirPassProvider { |
|
|
|
|
|
|
|
return &dirPassProvider{dirPath: dirPath} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (provider *dirPassProvider) getPassphrase(keyFile string) (string, error) { |
|
|
|
|
|
|
|
passFile := keyFileToPassFileFull(keyFile) |
|
|
|
|
|
|
|
if !isPassFile(passFile) { |
|
|
|
|
|
|
|
return "", fmt.Errorf("pass file %v not exist", passFile) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return readPassFromFile(passFile) |
|
|
|
return readPassFromFile(passFile) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|