| 1 |
package proton |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"runtime" |
| 6 |
|
| 7 |
"github.com/ProtonMail/gluon/async" |
| 8 |
"github.com/ProtonMail/gopenpgp/v2/crypto" |
| 9 |
"github.com/bradenaw/juniper/parallel" |
| 10 |
) |
| 11 |
|
| 12 |
func Unlock(user User, addresses []Address, saltedKeyPass []byte, panicHandler async.PanicHandler) (*crypto.KeyRing, map[string]*crypto.KeyRing, error) { |
| 13 |
userKR, err := user.Keys.Unlock(saltedKeyPass, nil) |
| 14 |
if err != nil { |
| 15 |
return nil, nil, fmt.Errorf("failed to unlock user keys: %w", err) |
| 16 |
} else if userKR.CountDecryptionEntities() == 0 { |
| 17 |
return nil, nil, fmt.Errorf("failed to unlock any user keys") |
| 18 |
} |
| 19 |
|
| 20 |
addrKRs := make(map[string]*crypto.KeyRing) |
| 21 |
|
| 22 |
for idx, addrKR := range parallel.Map(runtime.NumCPU(), addresses, func(addr Address) *crypto.KeyRing { |
| 23 |
defer async.HandlePanic(panicHandler) |
| 24 |
|
| 25 |
return addr.Keys.TryUnlock(saltedKeyPass, userKR) |
| 26 |
}) { |
| 27 |
if addrKR == nil { |
| 28 |
continue |
| 29 |
} else if addrKR.CountDecryptionEntities() == 0 { |
| 30 |
continue |
| 31 |
} |
| 32 |
|
| 33 |
addrKRs[addresses[idx].ID] = addrKR |
| 34 |
} |
| 35 |
|
| 36 |
if len(addrKRs) == 0 { |
| 37 |
return nil, nil, fmt.Errorf("failed to unlock any address keys") |
| 38 |
} |
| 39 |
|
| 40 |
return userKR, addrKRs, nil |
| 41 |
} |