ricochet-go/core/protocol.go

232 lines
6.9 KiB
Go
Raw Normal View History

package core
import (
2016-10-10 00:31:26 +00:00
"crypto/rsa"
"encoding/asn1"
protocol "github.com/s-rah/go-ricochet"
"log"
"time"
)
2016-10-10 00:31:26 +00:00
type ProtocolConnection struct {
Core *Ricochet
2016-10-10 00:31:26 +00:00
Conn *protocol.OpenConnection
Contact *Contact
2016-10-10 00:31:26 +00:00
// Client-side authentication
MyHostname string
PrivateKey rsa.PrivateKey
2016-10-10 00:31:26 +00:00
// Service-side authentication
GetContactByHostname func(hostname string) *Contact
}
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-10-10 00:31:26 +00:00
pc.Conn = oc
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-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)
}
}
// Authentication Management
2016-10-10 00:31:26 +00:00
func (pc *ProtocolConnection) OnAuthenticationRequest(channelID int32, clientCookie [16]byte) {
log.Printf("protocol: OnAuthenticationRequest")
2016-10-10 00:31:26 +00:00
pc.Conn.ConfirmAuthChannel(channelID, clientCookie)
}
2016-10-10 00:31:26 +00:00
func (pc *ProtocolConnection) OnAuthenticationChallenge(channelID int32, serverCookie [16]byte) {
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-10-10 00:31:26 +00:00
func (pc *ProtocolConnection) OnAuthenticationProof(channelID int32, publicKey []byte, signature []byte) {
result := pc.Conn.ValidateProof(channelID, publicKey, signature)
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)
result = false
} else {
2016-10-10 00:31:26 +00:00
pc.Contact = pc.GetContactByHostname(pc.Conn.OtherHostname)
}
}
2016-10-10 00:31:26 +00:00
isKnownContact := (pc.Contact != nil)
2016-10-10 00:31:26 +00:00
pc.Conn.SendAuthenticationResult(channelID, result, isKnownContact)
pc.Conn.IsAuthed = result
pc.Conn.CloseChannel(channelID)
2016-10-10 00:31:26 +00:00
log.Printf("protocol: OnAuthenticationProof, result: %v, contact: %v", result, pc.Contact)
if result && pc.Contact != nil {
pc.Contact.OnConnectionAuthenticated(pc.Conn, true)
}
}
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)
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()
return
}
2016-10-10 00:31:26 +00:00
log.Printf("protocol: Outbound connection to %s authenticated", pc.Conn.OtherHostname)
if pc.Contact != nil {
pc.Contact.OnConnectionAuthenticated(pc.Conn, isKnownContact)
}
}
// Contact Management
2016-10-10 00:31:26 +00:00
func (pc *ProtocolConnection) OnContactRequest(channelID int32, nick string, message string) {
if pc.Conn.Client || !pc.Conn.IsAuthed || pc.Contact != nil {
pc.Conn.CloseChannel(channelID)
return
}
address, ok := AddressFromPlainHost(pc.Conn.OtherHostname)
if !ok {
pc.Conn.CloseChannel(channelID)
return
}
if len(nick) > 0 && !IsNicknameAcceptable(nick) {
log.Printf("protocol: Stripping unacceptable nickname from inbound request; encoded: %x", []byte(nick))
nick = ""
}
if len(message) > 0 && !IsMessageAcceptable(message) {
log.Printf("protocol: Stripping unacceptable message from inbound request; len: %d, encoded: %x", len(message), []byte(message))
message = ""
}
contactList := pc.Core.Identity.ContactList()
request, contact := contactList.AddOrUpdateInboundContactRequest(address, nick, message)
if contact != nil {
// Accepted immediately
pc.Conn.AckContactRequestOnResponse(channelID, "Accepted")
pc.Conn.CloseChannel(channelID)
contact.OnConnectionAuthenticated(pc.Conn, true)
} else if request != nil && !request.IsRejected() {
// Pending
pc.Conn.AckContactRequestOnResponse(channelID, "Pending")
request.SetConnection(pc.Conn, channelID)
} else {
// Rejected
pc.Conn.AckContactRequestOnResponse(channelID, "Rejected")
pc.Conn.CloseChannel(channelID)
pc.Conn.Close()
if request != nil {
contactList.RemoveInboundContactRequest(request)
}
}
}
2016-10-10 00:31:26 +00:00
func (pc *ProtocolConnection) OnContactRequestAck(channelID int32, status string) {
if !pc.Conn.Client || pc.Contact == nil {
pc.Conn.CloseChannel(channelID)
return
}
if !pc.Contact.UpdateContactRequest(status) {
pc.Conn.CloseChannel(channelID)
return
}
}
func (pc *ProtocolConnection) IsKnownContact(hostname string) bool {
// All uses of this are for authenticated contacts, so it's sufficient to check pc.Contact
if pc.Contact != nil {
contactHostname, _ := PlainHostFromOnion(pc.Contact.Hostname())
if hostname != contactHostname {
log.Panicf("IsKnownContact called for unexpected hostname '%s'", hostname)
}
return true
2016-10-10 00:31:26 +00:00
}
return false
}
// Managing Channels
2016-10-10 00:31:26 +00:00
func (pc *ProtocolConnection) OnOpenChannelRequest(channelID int32, channelType string) {
log.Printf("open channel request: %v %v", channelID, channelType)
2016-10-10 00:31:26 +00:00
pc.Conn.AckOpenChannel(channelID, channelType)
}
2016-10-10 00:31:26 +00:00
func (pc *ProtocolConnection) OnOpenChannelRequestSuccess(channelID int32) {
log.Printf("open channel request success: %v", channelID)
}
2016-10-10 00:31:26 +00:00
func (pc *ProtocolConnection) OnChannelClosed(channelID int32) {
log.Printf("channel closed: %v", channelID)
}
// Chat Messages
// 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) {
// 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-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-10-10 00:31:26 +00:00
func (pc *ProtocolConnection) OnChatMessageAck(channelID int32, messageID int32) {
// 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-10 00:31:26 +00:00
conversation := pc.Contact.Conversation()
conversation.UpdateSentStatus(uint64(messageID), true)
}
// Handle Errors
2016-10-10 00:31:26 +00:00
func (pc *ProtocolConnection) OnFailedChannelOpen(channelID int32, errorType string) {
log.Printf("failed channel open: %d %s", channelID, errorType)
2016-10-10 00:31:26 +00:00
pc.Conn.UnsetChannel(channelID)
}
2016-10-10 00:31:26 +00:00
func (pc *ProtocolConnection) OnGenericError(channelID int32) {
pc.Conn.RejectOpenChannel(channelID, "GenericError")
}
2016-10-10 00:31:26 +00:00
func (pc *ProtocolConnection) OnUnknownTypeError(channelID int32) {
pc.Conn.RejectOpenChannel(channelID, "UnknownTypeError")
}
2016-10-10 00:31:26 +00:00
func (pc *ProtocolConnection) OnUnauthorizedError(channelID int32) {
pc.Conn.RejectOpenChannel(channelID, "UnauthorizedError")
}
2016-10-10 00:31:26 +00:00
func (pc *ProtocolConnection) OnBadUsageError(channelID int32) {
pc.Conn.RejectOpenChannel(channelID, "BadUsageError")
}
2016-10-10 00:31:26 +00:00
func (pc *ProtocolConnection) OnFailedError(channelID int32) {
pc.Conn.RejectOpenChannel(channelID, "FailedError")
}