2015-10-11 04:33:07 +00:00
|
|
|
package goricochet
|
|
|
|
|
|
|
|
import (
|
2015-10-12 23:03:48 +00:00
|
|
|
"errors"
|
2015-10-11 04:33:07 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/s-rah/go-ricochet/auth"
|
2016-06-27 01:56:23 +00:00
|
|
|
"github.com/s-rah/go-ricochet/chat"
|
|
|
|
"github.com/s-rah/go-ricochet/contact"
|
|
|
|
"github.com/s-rah/go-ricochet/control"
|
|
|
|
"github.com/s-rah/go-ricochet/utils"
|
2015-10-11 04:33:07 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
2016-06-27 01:56:23 +00:00
|
|
|
"strconv"
|
2015-10-11 04:33:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Ricochet is a protocol to conducting anonymous IM.
|
|
|
|
type Ricochet struct {
|
2016-06-27 01:56:23 +00:00
|
|
|
newconns chan *OpenConnection
|
|
|
|
networkResolver utils.NetworkResolver
|
|
|
|
rni utils.RicochetNetworkInterface
|
2016-01-02 01:43:30 +00:00
|
|
|
}
|
|
|
|
|
2016-02-29 00:18:25 +00:00
|
|
|
// Init sets up the Ricochet object.
|
2016-06-27 01:56:23 +00:00
|
|
|
func (r *Ricochet) Init() {
|
|
|
|
r.newconns = make(chan *OpenConnection)
|
|
|
|
r.networkResolver = utils.NetworkResolver{}
|
|
|
|
r.rni = new(utils.RicochetNetwork)
|
2016-02-28 00:20:40 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// Connect sets up a client ricochet connection to host e.g. qn6uo4cmsrfv4kzq.onion. If this
|
2015-10-11 04:33:07 +00:00
|
|
|
// function finished successfully then the connection can be assumed to
|
|
|
|
// be open and authenticated.
|
2016-01-03 23:32:55 +00:00
|
|
|
// To specify a local port using the format "127.0.0.1:[port]|ricochet-id".
|
2016-06-27 01:56:23 +00:00
|
|
|
func (r *Ricochet) Connect(host string) (*OpenConnection, error) {
|
2016-02-28 00:20:40 +00:00
|
|
|
var err error
|
2016-06-27 01:56:23 +00:00
|
|
|
conn, host, err := r.networkResolver.Resolve(host)
|
2015-10-11 04:33:07 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2016-06-27 01:56:23 +00:00
|
|
|
return nil, err
|
2015-10-11 04:33:07 +00:00
|
|
|
}
|
|
|
|
|
2016-10-01 22:50:11 +00:00
|
|
|
return r.ConnectOpen(conn, host)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Ricochet) ConnectOpen(conn net.Conn, host string) (*OpenConnection, error) {
|
2016-06-27 01:56:23 +00:00
|
|
|
oc, err := r.negotiateVersion(conn, true)
|
2016-01-06 04:42:49 +00:00
|
|
|
if err != nil {
|
2016-06-27 01:56:23 +00:00
|
|
|
return nil, err
|
2015-10-12 23:03:48 +00:00
|
|
|
}
|
2016-06-27 01:56:23 +00:00
|
|
|
oc.OtherHostname = host
|
|
|
|
r.newconns <- oc
|
|
|
|
return oc, nil
|
2015-10-12 23:03:48 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// Server launches a new server listening on port
|
|
|
|
func (r *Ricochet) Server(service RicochetService, port int) {
|
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(port))
|
2015-10-11 04:33:07 +00:00
|
|
|
if err != nil {
|
2016-06-27 01:56:23 +00:00
|
|
|
log.Printf("Cannot Listen on Port %v", port)
|
|
|
|
return
|
2015-10-11 04:33:07 +00:00
|
|
|
}
|
|
|
|
|
2016-10-01 22:50:11 +00:00
|
|
|
r.ServeListener(service, ln)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Ricochet) ServeListener(service RicochetService, ln net.Listener) {
|
2016-06-27 01:56:23 +00:00
|
|
|
go r.ProcessMessages(service)
|
|
|
|
service.OnReady()
|
|
|
|
for {
|
|
|
|
// accept connection on port
|
|
|
|
conn, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go r.processNewConnection(conn, service)
|
2016-02-29 00:18:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// processNewConnection sets up a new connection
|
|
|
|
func (r *Ricochet) processNewConnection(conn net.Conn, service RicochetService) {
|
|
|
|
oc, err := r.negotiateVersion(conn, false)
|
|
|
|
if err == nil {
|
|
|
|
r.newconns <- oc
|
|
|
|
service.OnConnect(oc)
|
2016-02-29 00:18:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// ProcessMessages is intended to be a background thread listening for all messages
|
|
|
|
// a client will send. The given RicochetService will be used to respond to messages.
|
2015-10-12 23:03:48 +00:00
|
|
|
// Prerequisites:
|
|
|
|
// * Must have previously issued a successful Connect()
|
2016-06-27 01:56:23 +00:00
|
|
|
func (r *Ricochet) ProcessMessages(service RicochetService) {
|
|
|
|
for {
|
|
|
|
oc := <-r.newconns
|
|
|
|
go r.processConnection(oc, service)
|
2015-10-12 23:03:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// ProcessConnection starts a blocking process loop which continually waits for
|
|
|
|
// new messages to arrive from the connection and uses the given RicochetService
|
|
|
|
// to process them.
|
|
|
|
func (r *Ricochet) processConnection(oc *OpenConnection, service RicochetService) {
|
|
|
|
service.OnConnect(oc)
|
2016-10-01 22:52:35 +00:00
|
|
|
defer service.OnDisconnect(oc)
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
for {
|
|
|
|
if oc.Closed {
|
|
|
|
return
|
|
|
|
}
|
2016-01-02 01:43:30 +00:00
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
packets, err := r.rni.RecvRicochetPackets(oc.conn)
|
2016-02-29 00:18:25 +00:00
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-01-02 01:43:30 +00:00
|
|
|
|
|
|
|
for _, packet := range packets {
|
2016-02-29 00:18:25 +00:00
|
|
|
|
|
|
|
if len(packet.Data) == 0 {
|
2016-06-27 01:56:23 +00:00
|
|
|
service.OnChannelClosed(oc, packet.Channel)
|
|
|
|
continue
|
2016-02-29 00:18:25 +00:00
|
|
|
}
|
|
|
|
|
2016-01-02 01:43:30 +00:00
|
|
|
if packet.Channel == 0 {
|
2016-02-29 00:18:25 +00:00
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
res := new(Protocol_Data_Control.Packet)
|
|
|
|
err := proto.Unmarshal(packet.Data[:], res)
|
2016-01-05 03:53:10 +00:00
|
|
|
|
2016-01-02 08:47:32 +00:00
|
|
|
if err != nil {
|
2016-06-27 01:56:23 +00:00
|
|
|
service.OnGenericError(oc, packet.Channel)
|
|
|
|
continue
|
2016-01-02 08:47:32 +00:00
|
|
|
}
|
2016-01-02 01:43:30 +00:00
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
if res.GetOpenChannel() != nil {
|
|
|
|
opm := res.GetOpenChannel()
|
|
|
|
|
|
|
|
if oc.GetChannelType(opm.GetChannelIdentifier()) != "none" {
|
|
|
|
// Channel is already in use.
|
|
|
|
service.OnBadUsageError(oc, opm.GetChannelIdentifier())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If I am a Client, the server can only open even numbered channels
|
|
|
|
if oc.Client && opm.GetChannelIdentifier()%2 != 0 {
|
|
|
|
service.OnBadUsageError(oc, opm.GetChannelIdentifier())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If I am a Server, the client can only open odd numbered channels
|
|
|
|
if !oc.Client && opm.GetChannelIdentifier()%2 != 1 {
|
|
|
|
service.OnBadUsageError(oc, opm.GetChannelIdentifier())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch opm.GetChannelType() {
|
|
|
|
case "im.ricochet.auth.hidden-service":
|
|
|
|
if oc.Client {
|
|
|
|
// Servers are authed by default and can't auth with hidden-service
|
|
|
|
service.OnBadUsageError(oc, opm.GetChannelIdentifier())
|
|
|
|
} else if oc.IsAuthed {
|
|
|
|
// Can't auth if already authed
|
|
|
|
service.OnBadUsageError(oc, opm.GetChannelIdentifier())
|
|
|
|
} else if oc.HasChannel("im.ricochet.auth.hidden-service") {
|
|
|
|
// Can't open more than 1 auth channel
|
|
|
|
service.OnBadUsageError(oc, opm.GetChannelIdentifier())
|
|
|
|
} else {
|
|
|
|
clientCookie, err := proto.GetExtension(opm, Protocol_Data_AuthHiddenService.E_ClientCookie)
|
|
|
|
if err == nil {
|
|
|
|
clientCookieB := [16]byte{}
|
|
|
|
copy(clientCookieB[:], clientCookie.([]byte)[:])
|
|
|
|
service.OnAuthenticationRequest(oc, opm.GetChannelIdentifier(), clientCookieB)
|
|
|
|
} else {
|
|
|
|
// Must include Client Cookie
|
|
|
|
service.OnBadUsageError(oc, opm.GetChannelIdentifier())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "im.ricochet.chat":
|
|
|
|
if !oc.IsAuthed {
|
|
|
|
// Can't open chat channel if not authorized
|
|
|
|
service.OnUnauthorizedError(oc, opm.GetChannelIdentifier())
|
|
|
|
} else if !service.IsKnownContact(oc.OtherHostname) {
|
|
|
|
// Can't open chat channel if not a known contact
|
|
|
|
service.OnUnauthorizedError(oc, opm.GetChannelIdentifier())
|
|
|
|
} else {
|
|
|
|
service.OnOpenChannelRequest(oc, opm.GetChannelIdentifier(), "im.ricochet.chat")
|
|
|
|
}
|
|
|
|
case "im.ricochet.contact.request":
|
|
|
|
if oc.Client {
|
|
|
|
// Servers are not allowed to send contact requests
|
|
|
|
service.OnBadUsageError(oc, opm.GetChannelIdentifier())
|
|
|
|
} else if !oc.IsAuthed {
|
|
|
|
// Can't open a contact channel if not authed
|
|
|
|
service.OnUnauthorizedError(oc, opm.GetChannelIdentifier())
|
|
|
|
} else if oc.HasChannel("im.ricochet.contact.request") {
|
|
|
|
// Only 1 contact channel is allowed to be open at a time
|
|
|
|
service.OnBadUsageError(oc, opm.GetChannelIdentifier())
|
|
|
|
} else {
|
|
|
|
contactRequestI, err := proto.GetExtension(opm, Protocol_Data_ContactRequest.E_ContactRequest)
|
|
|
|
if err == nil {
|
|
|
|
contactRequest, check := contactRequestI.(*Protocol_Data_ContactRequest.ContactRequest)
|
|
|
|
if check {
|
|
|
|
service.OnContactRequest(oc, opm.GetChannelIdentifier(), contactRequest.GetNickname(), contactRequest.GetMessageText())
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
service.OnBadUsageError(oc, opm.GetChannelIdentifier())
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
service.OnUnknownTypeError(oc, opm.GetChannelIdentifier())
|
|
|
|
}
|
|
|
|
} else if res.GetChannelResult() != nil {
|
|
|
|
crm := res.GetChannelResult()
|
|
|
|
if crm.GetOpened() {
|
|
|
|
switch oc.GetChannelType(crm.GetChannelIdentifier()) {
|
|
|
|
case "im.ricochet.auth.hidden-service":
|
|
|
|
serverCookie, err := proto.GetExtension(crm, Protocol_Data_AuthHiddenService.E_ServerCookie)
|
|
|
|
if err == nil {
|
|
|
|
serverCookieB := [16]byte{}
|
|
|
|
copy(serverCookieB[:], serverCookie.([]byte)[:])
|
|
|
|
service.OnAuthenticationChallenge(oc, crm.GetChannelIdentifier(), serverCookieB)
|
|
|
|
} else {
|
|
|
|
service.OnBadUsageError(oc, crm.GetChannelIdentifier())
|
|
|
|
}
|
|
|
|
case "im.ricochet.chat":
|
|
|
|
service.OnOpenChannelRequestSuccess(oc, crm.GetChannelIdentifier())
|
|
|
|
case "im.ricochet.contact.request":
|
|
|
|
responseI, err := proto.GetExtension(res.GetChannelResult(), Protocol_Data_ContactRequest.E_Response)
|
|
|
|
if err == nil {
|
|
|
|
response, check := responseI.(*Protocol_Data_ContactRequest.Response)
|
|
|
|
if check {
|
|
|
|
service.OnContactRequestAck(oc, crm.GetChannelIdentifier(), response.GetStatus().String())
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
service.OnBadUsageError(oc, crm.GetChannelIdentifier())
|
|
|
|
default:
|
|
|
|
service.OnBadUsageError(oc, crm.GetChannelIdentifier())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if oc.GetChannelType(crm.GetChannelIdentifier()) != "none" {
|
|
|
|
service.OnFailedChannelOpen(oc, crm.GetChannelIdentifier(), crm.GetCommonError().String())
|
|
|
|
} else {
|
|
|
|
oc.CloseChannel(crm.GetChannelIdentifier())
|
|
|
|
}
|
|
|
|
}
|
2016-02-29 00:18:25 +00:00
|
|
|
} else {
|
2016-06-27 01:56:23 +00:00
|
|
|
// Unknown Message
|
|
|
|
oc.CloseChannel(packet.Channel)
|
2016-01-02 01:43:30 +00:00
|
|
|
}
|
2016-06-27 01:56:23 +00:00
|
|
|
} else if oc.GetChannelType(packet.Channel) == "im.ricochet.auth.hidden-service" {
|
|
|
|
res := new(Protocol_Data_AuthHiddenService.Packet)
|
|
|
|
err := proto.Unmarshal(packet.Data[:], res)
|
2016-01-02 01:43:30 +00:00
|
|
|
|
2016-02-29 00:18:25 +00:00
|
|
|
if err != nil {
|
2016-06-27 01:56:23 +00:00
|
|
|
oc.CloseChannel(packet.Channel)
|
|
|
|
continue
|
2016-01-02 01:43:30 +00:00
|
|
|
}
|
2016-01-05 03:53:10 +00:00
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
if res.GetProof() != nil && !oc.Client { // Only Clients Send Proofs
|
|
|
|
service.OnAuthenticationProof(oc, packet.Channel, res.GetProof().GetPublicKey(), res.GetProof().GetSignature(), service.IsKnownContact(oc.OtherHostname))
|
|
|
|
} else if res.GetResult() != nil && oc.Client { // Only Servers Send Results
|
|
|
|
service.OnAuthenticationResult(oc, packet.Channel, res.GetResult().GetAccepted(), res.GetResult().GetIsKnownContact())
|
|
|
|
} else {
|
|
|
|
// If neither of the above are satisfied we just close the connection
|
|
|
|
oc.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if oc.GetChannelType(packet.Channel) == "im.ricochet.chat" {
|
|
|
|
|
|
|
|
// NOTE: These auth checks should be redundant, however they
|
|
|
|
// are included here for defense-in-depth if for some reason
|
|
|
|
// a previously authed connection becomes untrusted / not known and
|
|
|
|
// the state is not cleaned up.
|
|
|
|
if !oc.IsAuthed {
|
|
|
|
// Can't send chat messages if not authorized
|
|
|
|
service.OnUnauthorizedError(oc, packet.Channel)
|
|
|
|
} else if !service.IsKnownContact(oc.OtherHostname) {
|
|
|
|
// Can't send chat message if not a known contact
|
|
|
|
service.OnUnauthorizedError(oc, packet.Channel)
|
|
|
|
} else {
|
|
|
|
res := new(Protocol_Data_Chat.Packet)
|
|
|
|
err := proto.Unmarshal(packet.Data[:], res)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
oc.CloseChannel(packet.Channel)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.GetChatMessage() != nil {
|
|
|
|
service.OnChatMessage(oc, packet.Channel, int32(res.GetChatMessage().GetMessageId()), res.GetChatMessage().GetMessageText())
|
|
|
|
} else if res.GetChatAcknowledge() != nil {
|
|
|
|
service.OnChatMessageAck(oc, packet.Channel, int32(res.GetChatMessage().GetMessageId()))
|
|
|
|
} else {
|
|
|
|
// If neither of the above are satisfied we just close the connection
|
|
|
|
oc.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if oc.GetChannelType(packet.Channel) == "im.ricochet.contact.request" {
|
|
|
|
|
|
|
|
// NOTE: These auth checks should be redundant, however they
|
|
|
|
// are included here for defense-in-depth if for some reason
|
|
|
|
// a previously authed connection becomes untrusted / not known and
|
|
|
|
// the state is not cleaned up.
|
|
|
|
if !oc.Client {
|
|
|
|
// Clients are not allowed to send contact request responses
|
|
|
|
service.OnBadUsageError(oc, packet.Channel)
|
|
|
|
} else if !oc.IsAuthed {
|
|
|
|
// Can't send a contact request if not authed
|
|
|
|
service.OnBadUsageError(oc, packet.Channel)
|
2016-02-29 00:18:25 +00:00
|
|
|
} else {
|
2016-06-27 01:56:23 +00:00
|
|
|
res := new(Protocol_Data_ContactRequest.Response)
|
|
|
|
err := proto.Unmarshal(packet.Data[:], res)
|
|
|
|
log.Printf("%v", res)
|
|
|
|
if err != nil {
|
|
|
|
oc.CloseChannel(packet.Channel)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
service.OnContactRequestAck(oc, packet.Channel, res.GetStatus().String())
|
2016-01-02 08:47:32 +00:00
|
|
|
}
|
2016-06-27 01:56:23 +00:00
|
|
|
} else if oc.GetChannelType(packet.Channel) == "none" {
|
|
|
|
// Invalid Channel Assignment
|
|
|
|
oc.CloseChannel(packet.Channel)
|
|
|
|
} else {
|
|
|
|
oc.Close()
|
2016-01-02 01:43:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// negotiateVersion Perform version negotiation with the connected host.
|
|
|
|
func (r *Ricochet) negotiateVersion(conn net.Conn, outbound bool) (*OpenConnection, error) {
|
|
|
|
version := make([]byte, 4)
|
|
|
|
version[0] = 0x49
|
|
|
|
version[1] = 0x4D
|
|
|
|
version[2] = 0x01
|
|
|
|
version[3] = 0x01
|
2016-01-02 01:43:30 +00:00
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// If this was initiated by us then we need to initiate the version info.
|
|
|
|
if outbound {
|
|
|
|
// Send Version String
|
2016-01-02 01:43:30 +00:00
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
conn.Write(version)
|
|
|
|
res, err := r.rni.Recv(conn)
|
2016-01-02 01:43:30 +00:00
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
if len(res) != 1 || err != nil {
|
|
|
|
return nil, errors.New("Failed Version Negotiating")
|
2016-01-02 01:43:30 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
if res[0] != 1 {
|
|
|
|
return nil, errors.New("Failed Version Negotiating - Invalid Version ")
|
2016-01-02 01:43:30 +00:00
|
|
|
}
|
2016-06-27 01:56:23 +00:00
|
|
|
} else {
|
|
|
|
// Do Version Negotiation
|
2016-01-02 08:47:32 +00:00
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
buf := make([]byte, 10)
|
|
|
|
n, err := conn.Read(buf)
|
|
|
|
if err != nil && n >= 4 {
|
|
|
|
return nil, err
|
2016-01-02 01:43:30 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
if buf[0] == version[0] && buf[1] == version[1] {
|
|
|
|
foundVersion := false
|
|
|
|
if buf[2] >= 1 {
|
|
|
|
for i := 3; i < n; i++ {
|
|
|
|
if buf[i] == 0x01 {
|
|
|
|
conn.Write([]byte{0x01})
|
|
|
|
foundVersion = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !foundVersion {
|
|
|
|
return nil, errors.New("Failed Version Negotiating - No Available Version")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("Failed Version Negotiating - Invalid Version Header")
|
|
|
|
}
|
2016-01-02 01:43:30 +00:00
|
|
|
}
|
2016-01-03 23:32:55 +00:00
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
oc := new(OpenConnection)
|
|
|
|
oc.Init(outbound, conn)
|
|
|
|
return oc, nil
|
2016-01-03 23:32:55 +00:00
|
|
|
}
|