Commit 6bf87237 authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Fix nil deref in Paillier key handling

parent 6346523c
......@@ -132,12 +132,15 @@ func NewCredentialManager(
if cm.attributes, err = cm.storage.LoadAttributes(); err != nil {
return nil, err
}
if cm.paillierKeyCache, err = cm.storage.LoadPaillierKeys(); err != nil {
if cm.keyshareServers, err = cm.storage.LoadKeyshareServers(); err != nil {
return nil, err
}
if cm.keyshareServers, err = cm.storage.LoadKeyshareServers(); err != nil {
if cm.paillierKeyCache, err = cm.storage.LoadPaillierKeys(); err != nil {
return nil, err
}
if cm.paillierKeyCache == nil {
cm.paillierKey(false)
}
unenrolled := cm.unenrolledKeyshareServers()
switch len(unenrolled) {
......@@ -571,19 +574,28 @@ func (cm *CredentialManager) ConstructCredentials(msg []*gabi.IssueSignatureMess
func (cm *CredentialManager) paillierKey(wait bool) *paillierPrivateKey {
cached := cm.paillierKeyCache
ch := make(chan bool)
go func() {
newkey, _ := paillier.GenerateKey(rand.Reader, 2048)
cm.paillierKeyCache = (*paillierPrivateKey)(newkey)
if wait && cached == nil {
ch <- true
}
}()
if wait && cached == nil {
// Would just write cm.paillierKeyCache instead of cached here, but the worker
// modifies cm.paillierKeyCache, and we must be sure that the boolean here and
// the if-clause below match.
go cm.paillierKeyWorker(cached == nil && wait, ch)
if cached == nil && wait {
<-ch
// generate yet another one for future calls, but no need to wait now
go cm.paillierKeyWorker(false, ch)
}
return cm.paillierKeyCache
}
func (cm *CredentialManager) paillierKeyWorker(wait bool, ch chan bool) {
newkey, _ := paillier.GenerateKey(rand.Reader, 2048)
cm.paillierKeyCache = (*paillierPrivateKey)(newkey)
cm.storage.StorePaillierKeys(cm.paillierKeyCache)
if wait {
ch <- true
}
}
func (cm *CredentialManager) unenrolledKeyshareServers() []*SchemeManager {
list := []*SchemeManager{}
for name, manager := range cm.ConfigurationStore.SchemeManagers {
......
......@@ -246,6 +246,9 @@ func (s *storage) LoadPaillierKeys() (key *paillierPrivateKey, err error) {
if err := s.load(key, paillierFile); err != nil {
return nil, err
}
if key.N == nil { // TODO this is ugly
return nil, nil
}
return key, nil
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment