π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Add support for decrypting masterKey and token
Neeraj Gupta383dd7daea8d
diff --git a/internal/crypto/crypto.go b/internal/crypto/crypto.goindex e80700ff21..79111921ee 100644--- a/internal/crypto/crypto.go+++ b/internal/crypto/crypto.go@@ -104,3 +104,20 @@ func DeriveLoginKey(keyEncKey []byte) []byte {// return the first 16 bytes of the derived keyreturn subKey[:16]}++func SecretBoxOpen(c []byte, n []byte, k []byte) ([]byte, error) {+ var cp sodium.Bytes = c+ return cp.SecretBoxOpen(sodium.SecretBoxNonce{Bytes: n}, sodium.SecretBoxKey{Bytes: k})+}++func SealedBoxOpen(cipherText []byte, publicKey, masterSecret []byte) ([]byte, error) {+ var cp sodium.Bytes = cipherText+ om, err := cp.SealedBoxOpen(sodium.BoxKP{+ PublicKey: sodium.BoxPublicKey{Bytes: publicKey},+ SecretKey: sodium.BoxSecretKey{Bytes: masterSecret},+ })+ if err != nil {+ return nil, fmt.Errorf("failed to open sealed box: %v", err)+ }+ return om, nil+}diff --git a/pkg/account.go b/pkg/account.goindex ebff57e5d5..243d482555 100644--- a/pkg/account.go+++ b/pkg/account.go@@ -4,6 +4,7 @@ import ("cli-go/internal/api""context""encoding/json"+ "fmt""log"bolt "go.etcd.io/bbolt"@@ -32,9 +33,8 @@ func (c *ClICtrl) AddAccount(cxt context.Context) {if flowErr != nil {return}- var keyEncKey []bytevar verifyEmail bool- var authResponse *api.AuthorizationResponse+srpAttr, flowErr := c.Client.GetSRPAttributes(cxt, email)if flowErr != nil {// if flowErr type is ApiError and status code is 404, then set verifyEmail to true and continue@@ -45,6 +45,8 @@ func (c *ClICtrl) AddAccount(cxt context.Context) {return}}+ var authResponse *api.AuthorizationResponse+ var keyEncKey []byteif verifyEmail || srpAttr.IsEmailMFAEnabled {authResponse, flowErr = c.validateEmail(cxt, email)} else {@@ -60,14 +62,14 @@ func (c *ClICtrl) AddAccount(cxt context.Context) {log.Fatal("no encrypted token or keyAttributes")return}- if keyEncKey == nil {- pass, flowErr := GetSensitiveField("Enter password")- if flowErr != nil {- return- } else if pass == "" {- log.Printf("do work")- }+ masterKey, token, decErr := c.decryptMasterKeyAndToken(cxt, authResponse, keyEncKey)+ if decErr != nil {+ flowErr = decErr+ return}+ // print length+ fmt.Printf("master key length: %d\n", len(masterKey))+ fmt.Printf("token length: %d\n", len(token))}func (c *ClICtrl) ListAccounts(cxt context.Context) error {diff --git a/pkg/sign_in.go b/pkg/sign_in.goindex 29b0e5f0d6..c9743381b3 100644--- a/pkg/sign_in.go+++ b/pkg/sign_in.go@@ -48,6 +48,69 @@ func (c *ClICtrl) signInViaPassword(ctx context.Context, email string, srpAttr *}}+// Parameters:+// - keyEncKey: key encryption key is derived from user's password. During SRP based login, this key is already derived.+// So, we can pass it to avoid asking for password again.+func (c *ClICtrl) decryptMasterKeyAndToken(+ _ context.Context,+ authResp *api.AuthorizationResponse,+ keyEncKey []byte,+) (masterKey, token []byte, err error) {++ var currentKeyEncKey []byte+ for {+ if keyEncKey == nil {+ // CLI prompt for password+ password, flowErr := GetSensitiveField("Enter password")+ if flowErr != nil {+ return nil, nil, flowErr+ }+ fmt.Println("\nPlease wait authenticating...")+ currentKeyEncKey, err = enteCrypto.DeriveArgonKey(password,+ authResp.KeyAttributes.KEKSalt, authResp.KeyAttributes.MemLimit, authResp.KeyAttributes.OpsLimit)+ if err != nil {+ fmt.Printf("error deriving key encryption key: %v", err)+ return nil, nil, err+ }+ } else {+ currentKeyEncKey = keyEncKey+ }++ encryptedKey := utils.Base64DecodeString(authResp.KeyAttributes.EncryptedKey)+ encryptedKeyNonce := utils.Base64DecodeString(authResp.KeyAttributes.KeyDecryptionNonce)+ key, keyErr := enteCrypto.SecretBoxOpen(encryptedKey, encryptedKeyNonce, currentKeyEncKey)+ if keyErr != nil {+ if keyEncKey != nil {+ fmt.Printf("Failed to get key from keyEncryptionKey %s", keyErr)+ return nil, nil, keyErr+ } else {+ fmt.Printf("Incorrect password, error decrypting master key: %v", keyErr)+ continue+ }+ }+ masterKey, keyErr = enteCrypto.SecretBoxOpen(+ utils.Base64DecodeString(authResp.KeyAttributes.EncryptedSecretKey),+ utils.Base64DecodeString(authResp.KeyAttributes.SecretKeyDecryptionNonce),+ key,+ )+ if keyErr != nil {+ fmt.Printf("error decrypting master key: %v", keyErr)+ return nil, nil, keyErr+ }+ token, err = enteCrypto.SealedBoxOpen(+ utils.Base64DecodeString(authResp.EncryptedToken),+ utils.Base64DecodeString(authResp.KeyAttributes.PublicKey),+ masterKey,+ )+ if err != nil {+ fmt.Printf("error decrypting token: %v", err)+ return nil, nil, err+ }+ break+ }+ return masterKey, token, nil+}+func (c *ClICtrl) validateTOTP(ctx context.Context, authResp *api.AuthorizationResponse) (*api.AuthorizationResponse, error) {if !authResp.IsMFARequired() {return authResp, nil