2016-07-01 03:18:55 +00:00
|
|
|
package core
|
|
|
|
|
2016-08-30 01:49:30 +00:00
|
|
|
import (
|
|
|
|
"crypto/rsa"
|
|
|
|
"errors"
|
2016-10-17 04:26:35 +00:00
|
|
|
"github.com/ricochet-im/ricochet-go/core/utils"
|
2017-09-24 20:15:53 +00:00
|
|
|
"github.com/ricochet-im/ricochet-go/rpc"
|
2016-10-27 19:57:04 +00:00
|
|
|
protocol "github.com/s-rah/go-ricochet"
|
2017-08-14 02:04:35 +00:00
|
|
|
connection "github.com/s-rah/go-ricochet/connection"
|
2016-08-30 01:49:30 +00:00
|
|
|
"github.com/yawning/bulb/utils/pkcs1"
|
|
|
|
"log"
|
2017-08-14 02:04:35 +00:00
|
|
|
"net"
|
2016-09-11 04:35:03 +00:00
|
|
|
"sync"
|
2016-08-30 01:49:30 +00:00
|
|
|
)
|
2016-07-01 03:18:55 +00:00
|
|
|
|
2016-09-11 04:35:03 +00:00
|
|
|
// Identity represents the local user, including their contact address,
|
|
|
|
// and contains the contacts list.
|
2016-08-30 01:49:30 +00:00
|
|
|
type Identity struct {
|
2016-09-11 04:57:54 +00:00
|
|
|
core *Ricochet
|
2016-08-30 02:46:41 +00:00
|
|
|
|
2016-09-11 04:35:03 +00:00
|
|
|
mutex sync.Mutex
|
2016-07-01 03:18:55 +00:00
|
|
|
|
2016-09-11 04:35:03 +00:00
|
|
|
address string
|
|
|
|
privateKey *rsa.PrivateKey
|
2016-07-01 03:18:55 +00:00
|
|
|
contactList *ContactList
|
2016-10-16 00:04:19 +00:00
|
|
|
|
|
|
|
ConversationStream *utils.Publisher
|
2016-07-01 03:18:55 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 04:57:54 +00:00
|
|
|
func CreateIdentity(core *Ricochet) (*Identity, error) {
|
2016-08-30 02:46:41 +00:00
|
|
|
me := &Identity{
|
2016-10-16 00:04:19 +00:00
|
|
|
core: core,
|
|
|
|
ConversationStream: utils.CreatePublisher(),
|
2016-08-30 02:46:41 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 04:35:03 +00:00
|
|
|
if err := me.loadIdentity(); err != nil {
|
|
|
|
log.Printf("Failed loading identity: %v", err)
|
2016-08-30 02:46:41 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
contactList, err := LoadContactList(core)
|
|
|
|
if err != nil {
|
2016-09-11 04:35:03 +00:00
|
|
|
log.Printf("Failed loading contact list: %v", err)
|
2016-08-30 02:46:41 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
me.contactList = contactList
|
2016-08-30 01:49:30 +00:00
|
|
|
|
2016-10-28 22:08:16 +00:00
|
|
|
contactList.StartConnections()
|
2016-09-11 04:35:03 +00:00
|
|
|
go me.publishService(me.privateKey)
|
2016-08-30 02:46:41 +00:00
|
|
|
return me, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *Identity) loadIdentity() error {
|
2017-09-24 20:15:53 +00:00
|
|
|
config := me.core.Config.Read()
|
2016-08-30 01:49:30 +00:00
|
|
|
|
2017-09-24 20:15:53 +00:00
|
|
|
if keyData := config.Secrets.GetServicePrivateKey(); keyData != nil {
|
|
|
|
var err error
|
2016-08-30 01:49:30 +00:00
|
|
|
me.privateKey, _, err = pkcs1.DecodePrivateKeyDER(keyData)
|
|
|
|
if err != nil {
|
2016-08-30 02:46:41 +00:00
|
|
|
return err
|
2016-08-30 01:49:30 +00:00
|
|
|
}
|
2016-11-06 03:27:13 +00:00
|
|
|
me.address, err = AddressFromKey(&me.privateKey.PublicKey)
|
2016-08-30 01:49:30 +00:00
|
|
|
if err != nil {
|
2016-08-30 02:46:41 +00:00
|
|
|
return err
|
2016-08-30 01:49:30 +00:00
|
|
|
}
|
2016-09-11 04:35:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2017-09-24 20:15:53 +00:00
|
|
|
config := me.core.Config.Lock()
|
|
|
|
if config.Secrets == nil {
|
|
|
|
config.Secrets = &ricochet.Secrets{}
|
|
|
|
}
|
|
|
|
config.Secrets.ServicePrivateKey = keyData
|
|
|
|
me.core.Config.Unlock()
|
2016-09-11 04:35:03 +00:00
|
|
|
|
|
|
|
// Update Identity
|
2016-11-06 03:27:13 +00:00
|
|
|
me.address, err = AddressFromKey(&key.PublicKey)
|
2016-09-11 04:35:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-07-01 03:18:55 +00:00
|
|
|
}
|
2016-09-11 04:35:03 +00:00
|
|
|
me.privateKey = key
|
2016-07-01 03:18:55 +00:00
|
|
|
|
2016-09-11 04:35:03 +00:00
|
|
|
log.Printf("Created new identity %s", me.address)
|
2016-08-30 02:46:41 +00:00
|
|
|
return nil
|
2016-07-01 03:18:55 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 04:35:03 +00:00
|
|
|
// BUG(special): No error handling for failures under publishService
|
|
|
|
func (me *Identity) publishService(key *rsa.PrivateKey) {
|
2016-09-16 00:32:58 +00:00
|
|
|
// 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.
|
2016-09-11 04:35:03 +00:00
|
|
|
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 {
|
2016-09-16 00:32:58 +00:00
|
|
|
if service.PrivateKey == nil {
|
|
|
|
log.Printf("Setting private key failed: no key returned")
|
|
|
|
// XXX handle
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-11 04:35:03 +00:00
|
|
|
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")
|
2017-08-14 02:04:35 +00:00
|
|
|
for {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Identity listener failed: %v", err)
|
|
|
|
// XXX handle
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle connection in a separate goroutine and continue listening
|
|
|
|
go me.handleInboundConnection(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *Identity) handleInboundConnection(conn net.Conn) error {
|
|
|
|
defer func() {
|
|
|
|
// Close conn on return unless explicitly cleared
|
|
|
|
if conn != nil {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
contactByHostname := func(hostname string) (*Contact, error) {
|
|
|
|
address, ok := AddressFromPlainHost(hostname)
|
|
|
|
if !ok {
|
|
|
|
// This would be a bug
|
|
|
|
return nil, errors.New("invalid authenticated hostname")
|
|
|
|
}
|
|
|
|
return me.contactList.ContactByAddress(address), nil
|
|
|
|
}
|
|
|
|
lookupContactAuth := func(hostname string, publicKey rsa.PublicKey) (bool, bool) {
|
|
|
|
contact, err := contactByHostname(hostname)
|
|
|
|
if err != nil {
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
// allowed, known
|
|
|
|
return true, contact != nil
|
2016-10-10 00:31:26 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 02:04:35 +00:00
|
|
|
rc, err := protocol.NegotiateVersionInbound(conn)
|
2016-10-10 00:31:26 +00:00
|
|
|
if err != nil {
|
2017-08-14 02:04:35 +00:00
|
|
|
log.Printf("Inbound connection failed: %v", err)
|
|
|
|
return err
|
2016-10-10 00:31:26 +00:00
|
|
|
}
|
2017-08-14 02:04:35 +00:00
|
|
|
|
|
|
|
authHandler := connection.HandleInboundConnection(rc)
|
|
|
|
err = authHandler.ProcessAuthAsServer(me.privateKey, lookupContactAuth)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Inbound connection auth failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
contact, err := contactByHostname(rc.RemoteHostname)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Inbound connection lookup failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if contact != nil {
|
|
|
|
// Known contact, pass the new connection to Contact
|
|
|
|
contact.AssignConnection(rc)
|
|
|
|
conn = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-23 21:56:29 +00:00
|
|
|
err = HandleInboundRequestConnection(rc, me.contactList)
|
|
|
|
if err == nil {
|
|
|
|
// Connection now belongs to an accepted contact, don't close it
|
|
|
|
conn = nil
|
|
|
|
}
|
|
|
|
return err
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-07-01 03:18:55 +00:00
|
|
|
func (me *Identity) Address() string {
|
|
|
|
return me.address
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *Identity) ContactList() *ContactList {
|
|
|
|
return me.contactList
|
|
|
|
}
|
2016-09-11 04:35:03 +00:00
|
|
|
|
|
|
|
func (me *Identity) PrivateKey() rsa.PrivateKey {
|
|
|
|
return *me.privateKey
|
|
|
|
}
|