ricochet-go/core/identity.go

181 lines
4.1 KiB
Go
Raw Normal View History

2016-07-01 05:18:55 +02:00
package core
import (
"crypto/rsa"
"encoding/base64"
"errors"
2016-10-17 06:26:35 +02:00
"github.com/ricochet-im/ricochet-go/core/utils"
protocol "github.com/s-rah/go-ricochet"
"github.com/yawning/bulb/utils/pkcs1"
"log"
"sync"
)
2016-07-01 05:18:55 +02:00
// Identity represents the local user, including their contact address,
// and contains the contacts list.
type Identity struct {
2016-09-11 06:57:54 +02:00
core *Ricochet
2016-08-30 04:46:41 +02:00
mutex sync.Mutex
2016-07-01 05:18:55 +02:00
address string
privateKey *rsa.PrivateKey
2016-07-01 05:18:55 +02:00
contactList *ContactList
ConversationStream *utils.Publisher
2016-07-01 05:18:55 +02:00
}
2016-09-11 06:57:54 +02:00
func CreateIdentity(core *Ricochet) (*Identity, error) {
2016-08-30 04:46:41 +02:00
me := &Identity{
core: core,
ConversationStream: utils.CreatePublisher(),
2016-08-30 04:46:41 +02:00
}
if err := me.loadIdentity(); err != nil {
log.Printf("Failed loading identity: %v", err)
2016-08-30 04:46:41 +02:00
return nil, err
}
contactList, err := LoadContactList(core)
if err != nil {
log.Printf("Failed loading contact list: %v", err)
2016-08-30 04:46:41 +02:00
return nil, err
}
me.contactList = contactList
go me.publishService(me.privateKey)
2016-08-30 04:46:41 +02:00
return me, nil
}
func (me *Identity) loadIdentity() error {
2016-09-11 06:57:54 +02:00
config := me.core.Config.OpenRead()
defer config.Close()
2016-08-30 04:46:41 +02:00
if config.Identity.ServiceKey != "" {
keyData, err := base64.StdEncoding.DecodeString(config.Identity.ServiceKey)
if err != nil {
2016-08-30 04:46:41 +02:00
return err
}
me.privateKey, _, err = pkcs1.DecodePrivateKeyDER(keyData)
if err != nil {
2016-08-30 04:46:41 +02:00
return err
}
me.address, err = utils.RicochetAddressFromKey(&me.privateKey.PublicKey)
if err != nil {
2016-08-30 04:46:41 +02:00
return err
}
log.Printf("Loaded identity %s", me.address)
} else {
log.Printf("Initializing new identity")
}
return nil
}
func (me *Identity) setPrivateKey(key *rsa.PrivateKey) error {
me.mutex.Lock()
defer me.mutex.Unlock()
if me.privateKey != nil || me.address != "" {
return errors.New("Cannot change private key on identity")
}
// Save key to config
keyData, err := pkcs1.EncodePrivateKeyDER(key)
if err != nil {
return err
}
config := me.core.Config.OpenWrite()
config.Identity.ServiceKey = base64.StdEncoding.EncodeToString(keyData)
config.Save()
// Update Identity
me.address, err = utils.RicochetAddressFromKey(&key.PublicKey)
if err != nil {
return err
2016-07-01 05:18:55 +02:00
}
me.privateKey = key
2016-07-01 05:18:55 +02:00
log.Printf("Created new identity %s", me.address)
2016-08-30 04:46:41 +02:00
return nil
2016-07-01 05:18:55 +02:00
}
2016-10-10 02:31:26 +02:00
type identityService struct {
Identity *Identity
MyHostname string
}
func (is *identityService) OnNewConnection(oc *protocol.OpenConnection) {
log.Printf("Inbound connection accepted")
oc.MyHostname = is.MyHostname
// XXX Should have pre-auth handling, timeouts
identity := is.Identity
handler := &ProtocolConnection{
Conn: oc,
GetContactByHostname: func(hostname string) *Contact {
return identity.ContactList().ContactByAddress("ricochet:" + hostname)
},
}
go oc.Process(handler)
}
func (is *identityService) OnFailedConnection(err error) {
log.Printf("Inbound connection failed: %v", err)
}
// BUG(special): No error handling for failures under publishService
func (me *Identity) publishService(key *rsa.PrivateKey) {
// This call will block until a control connection is available and the
// ADD_ONION command has returned. After creating the listener, it will
// be automatically re-published if the control connection is lost and
// later reconnected.
service, listener, err := me.core.Network.NewOnionListener(9878, key)
if err != nil {
log.Printf("Identity listener failed: %v", err)
// XXX handle
return
}
if key == nil {
if service.PrivateKey == nil {
log.Printf("Setting private key failed: no key returned")
// XXX handle
return
}
err := me.setPrivateKey(service.PrivateKey.(*rsa.PrivateKey))
if err != nil {
log.Printf("Setting private key failed: %v", err)
// XXX handle
return
}
}
log.Printf("Identity service published, accepting connections")
2016-10-10 02:31:26 +02:00
is := &identityService{
Identity: me,
MyHostname: me.Address()[9:],
}
err = protocol.Serve(listener, is)
if err != nil {
log.Printf("Identity listener failed: %v", err)
// XXX handle
return
}
}
2016-07-01 05:18:55 +02:00
func (me *Identity) Address() string {
return me.address
}
func (me *Identity) ContactList() *ContactList {
return me.contactList
}
func (me *Identity) PrivateKey() rsa.PrivateKey {
return *me.privateKey
}