2016-09-11 04:35:03 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2016-10-10 00:31:26 +00:00
|
|
|
"crypto/rsa"
|
2016-09-11 04:35:03 +00:00
|
|
|
"encoding/asn1"
|
|
|
|
protocol "github.com/s-rah/go-ricochet"
|
|
|
|
"log"
|
2016-10-05 21:38:18 +00:00
|
|
|
"time"
|
2016-09-11 04:35:03 +00:00
|
|
|
)
|
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
type ProtocolConnection struct {
|
|
|
|
Conn *protocol.OpenConnection
|
|
|
|
Contact *Contact
|
2016-09-11 04:35:03 +00:00
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
// Client-side authentication
|
|
|
|
MyHostname string
|
|
|
|
PrivateKey rsa.PrivateKey
|
2016-09-11 04:35:03 +00:00
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
// Service-side authentication
|
|
|
|
GetContactByHostname func(hostname string) *Contact
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
func (pc *ProtocolConnection) OnReady(oc *protocol.OpenConnection) {
|
|
|
|
if pc.Conn != nil && pc.Conn != oc {
|
|
|
|
log.Panicf("ProtocolConnection is already assigned connection %v, but OnReady called for connection %v", pc.Conn, oc)
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
pc.Conn = oc
|
2016-09-11 04:35:03 +00:00
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
if pc.Conn.Client {
|
|
|
|
log.Printf("Connected to %s", pc.Conn.OtherHostname)
|
|
|
|
pc.Conn.MyHostname = pc.MyHostname
|
|
|
|
pc.Conn.IsAuthed = true // Outbound connections are authenticated
|
|
|
|
pc.Conn.Authenticate(1)
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
func (pc *ProtocolConnection) OnDisconnect() {
|
|
|
|
log.Printf("protocol: OnDisconnect: %v", pc)
|
|
|
|
if pc.Contact != nil {
|
|
|
|
pc.Contact.OnConnectionClosed(pc.Conn)
|
2016-09-20 03:32:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-11 04:35:03 +00:00
|
|
|
// Authentication Management
|
2016-10-10 00:31:26 +00:00
|
|
|
func (pc *ProtocolConnection) OnAuthenticationRequest(channelID int32, clientCookie [16]byte) {
|
2016-09-11 04:35:03 +00:00
|
|
|
log.Printf("protocol: OnAuthenticationRequest")
|
2016-10-10 00:31:26 +00:00
|
|
|
pc.Conn.ConfirmAuthChannel(channelID, clientCookie)
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
func (pc *ProtocolConnection) OnAuthenticationChallenge(channelID int32, serverCookie [16]byte) {
|
2016-09-11 04:35:03 +00:00
|
|
|
log.Printf("protocol: OnAuthenticationChallenge")
|
2016-10-10 00:31:26 +00:00
|
|
|
publicKeyBytes, _ := asn1.Marshal(pc.PrivateKey.PublicKey)
|
|
|
|
pc.Conn.SendProof(1, serverCookie, publicKeyBytes, &pc.PrivateKey)
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
func (pc *ProtocolConnection) OnAuthenticationProof(channelID int32, publicKey []byte, signature []byte) {
|
|
|
|
result := pc.Conn.ValidateProof(channelID, publicKey, signature)
|
2016-09-16 00:32:58 +00:00
|
|
|
|
|
|
|
if result {
|
2016-10-10 00:31:26 +00:00
|
|
|
if len(pc.Conn.OtherHostname) != 16 {
|
|
|
|
log.Printf("protocol: Invalid format for hostname '%s' in authentication proof", pc.Conn.OtherHostname)
|
2016-09-16 00:32:58 +00:00
|
|
|
result = false
|
|
|
|
} else {
|
2016-10-10 00:31:26 +00:00
|
|
|
pc.Contact = pc.GetContactByHostname(pc.Conn.OtherHostname)
|
2016-09-16 00:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-10 00:31:26 +00:00
|
|
|
isKnownContact := (pc.Contact != nil)
|
2016-09-16 00:32:58 +00:00
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
pc.Conn.SendAuthenticationResult(channelID, result, isKnownContact)
|
|
|
|
pc.Conn.IsAuthed = result
|
|
|
|
pc.Conn.CloseChannel(channelID)
|
2016-09-16 00:32:58 +00:00
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
log.Printf("protocol: OnAuthenticationProof, result: %v, contact: %v", result, pc.Contact)
|
|
|
|
if result && pc.Contact != nil {
|
2016-10-28 15:50:04 +00:00
|
|
|
pc.Contact.OnConnectionAuthenticated(pc.Conn, true)
|
2016-09-16 00:32:58 +00:00
|
|
|
}
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
func (pc *ProtocolConnection) OnAuthenticationResult(channelID int32, result bool, isKnownContact bool) {
|
|
|
|
pc.Conn.IsAuthed = result
|
|
|
|
pc.Conn.CloseChannel(channelID)
|
2016-09-30 05:13:55 +00:00
|
|
|
|
|
|
|
if !result {
|
2016-10-10 00:31:26 +00:00
|
|
|
log.Printf("protocol: Outbound connection authentication to %s failed", pc.Conn.OtherHostname)
|
|
|
|
pc.Conn.Close()
|
2016-09-30 05:13:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
log.Printf("protocol: Outbound connection to %s authenticated", pc.Conn.OtherHostname)
|
|
|
|
if pc.Contact != nil {
|
2016-10-28 15:50:04 +00:00
|
|
|
pc.Contact.OnConnectionAuthenticated(pc.Conn, isKnownContact)
|
2016-09-30 05:13:55 +00:00
|
|
|
}
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Contact Management
|
2016-10-10 00:31:26 +00:00
|
|
|
func (pc *ProtocolConnection) OnContactRequest(channelID int32, nick string, message string) {
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
func (pc *ProtocolConnection) OnContactRequestAck(channelID int32, status string) {
|
2016-10-28 15:50:04 +00:00
|
|
|
if !pc.Conn.Client || pc.Contact == nil {
|
|
|
|
pc.Conn.CloseChannel(channelID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !pc.Contact.UpdateContactRequest(status) {
|
|
|
|
pc.Conn.CloseChannel(channelID)
|
|
|
|
return
|
|
|
|
}
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
// Managing Channels
|
|
|
|
func (pc *ProtocolConnection) IsChannelAllowed(channelType string) bool {
|
|
|
|
switch channelType {
|
|
|
|
case "im.ricochet.auth.hidden-service":
|
|
|
|
return !pc.Conn.IsAuthed && pc.Contact == nil
|
|
|
|
case "im.ricochet.chat":
|
|
|
|
return pc.Conn.IsAuthed && pc.Contact != nil
|
|
|
|
case "im.ricochet.contact.request":
|
|
|
|
return pc.Conn.IsAuthed && pc.Contact == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
func (pc *ProtocolConnection) OnOpenChannelRequest(channelID int32, channelType string) {
|
2016-10-05 21:38:18 +00:00
|
|
|
log.Printf("open channel request: %v %v", channelID, channelType)
|
2016-10-10 00:31:26 +00:00
|
|
|
pc.Conn.AckOpenChannel(channelID, channelType)
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
func (pc *ProtocolConnection) OnOpenChannelRequestSuccess(channelID int32) {
|
|
|
|
log.Printf("open channel request success: %v", channelID)
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
2016-10-10 00:31:26 +00:00
|
|
|
|
|
|
|
func (pc *ProtocolConnection) OnChannelClosed(channelID int32) {
|
2016-10-05 21:38:18 +00:00
|
|
|
log.Printf("channel closed: %v", channelID)
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chat Messages
|
2016-10-05 21:38:18 +00:00
|
|
|
// XXX messageID should be (at least) uint32
|
2016-10-10 00:31:26 +00:00
|
|
|
func (pc *ProtocolConnection) OnChatMessage(channelID int32, messageID int32, message string) {
|
2016-10-05 21:38:18 +00:00
|
|
|
// XXX no time delta?
|
|
|
|
// XXX sanity checks, message contents, etc
|
|
|
|
log.Printf("chat message: %d %d %s", channelID, messageID, message)
|
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
// XXX error case
|
|
|
|
if pc.Contact == nil {
|
|
|
|
pc.Conn.Close()
|
2016-10-05 21:38:18 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
// XXX cache?
|
|
|
|
conversation := pc.Contact.Conversation()
|
|
|
|
conversation.Receive(uint64(messageID), time.Now().Unix(), message)
|
|
|
|
|
|
|
|
pc.Conn.AckChatMessage(channelID, messageID)
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
2016-10-10 00:31:26 +00:00
|
|
|
|
|
|
|
func (pc *ProtocolConnection) OnChatMessageAck(channelID int32, messageID int32) {
|
2016-10-05 21:38:18 +00:00
|
|
|
// XXX no success
|
|
|
|
log.Printf("chat ack: %d %d", channelID, messageID)
|
|
|
|
|
2016-10-10 00:31:26 +00:00
|
|
|
// XXX error case
|
|
|
|
if pc.Contact == nil {
|
|
|
|
pc.Conn.Close()
|
2016-10-05 21:38:18 +00:00
|
|
|
}
|
2016-10-10 00:31:26 +00:00
|
|
|
|
|
|
|
conversation := pc.Contact.Conversation()
|
|
|
|
conversation.UpdateSentStatus(uint64(messageID), true)
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle Errors
|
2016-10-10 00:31:26 +00:00
|
|
|
func (pc *ProtocolConnection) OnFailedChannelOpen(channelID int32, errorType string) {
|
2016-10-05 21:38:18 +00:00
|
|
|
log.Printf("failed channel open: %d %s", channelID, errorType)
|
2016-10-10 00:31:26 +00:00
|
|
|
pc.Conn.UnsetChannel(channelID)
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
2016-10-10 00:31:26 +00:00
|
|
|
func (pc *ProtocolConnection) OnGenericError(channelID int32) {
|
|
|
|
pc.Conn.RejectOpenChannel(channelID, "GenericError")
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
2016-10-10 00:31:26 +00:00
|
|
|
func (pc *ProtocolConnection) OnUnknownTypeError(channelID int32) {
|
|
|
|
pc.Conn.RejectOpenChannel(channelID, "UnknownTypeError")
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
2016-10-10 00:31:26 +00:00
|
|
|
func (pc *ProtocolConnection) OnUnauthorizedError(channelID int32) {
|
|
|
|
pc.Conn.RejectOpenChannel(channelID, "UnauthorizedError")
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
2016-10-10 00:31:26 +00:00
|
|
|
func (pc *ProtocolConnection) OnBadUsageError(channelID int32) {
|
|
|
|
pc.Conn.RejectOpenChannel(channelID, "BadUsageError")
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|
2016-10-10 00:31:26 +00:00
|
|
|
func (pc *ProtocolConnection) OnFailedError(channelID int32) {
|
|
|
|
pc.Conn.RejectOpenChannel(channelID, "FailedError")
|
2016-09-11 04:35:03 +00:00
|
|
|
}
|