2016-02-29 00:18:25 +00:00
|
|
|
package goricochet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/asn1"
|
|
|
|
"encoding/pem"
|
2016-06-27 01:56:23 +00:00
|
|
|
"errors"
|
|
|
|
"github.com/s-rah/go-ricochet/utils"
|
2016-02-29 00:18:25 +00:00
|
|
|
"io/ioutil"
|
2016-06-27 01:56:23 +00:00
|
|
|
"log"
|
2016-10-09 00:51:13 +00:00
|
|
|
"net"
|
|
|
|
"strconv"
|
2016-02-29 00:18:25 +00:00
|
|
|
)
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// StandardRicochetService implements all the necessary flows to implement a
|
|
|
|
// minimal, protocol compliant Ricochet Service. It can be built on by other
|
2016-10-09 00:51:13 +00:00
|
|
|
// applications to produce automated riochet applications, and is a useful
|
|
|
|
// example for other implementations.
|
2016-02-29 00:18:25 +00:00
|
|
|
type StandardRicochetService struct {
|
2016-10-09 00:51:13 +00:00
|
|
|
PrivateKey *rsa.PrivateKey
|
2016-06-27 01:56:23 +00:00
|
|
|
serverHostname string
|
2016-02-29 00:18:25 +00:00
|
|
|
}
|
|
|
|
|
2016-10-09 00:51:13 +00:00
|
|
|
// StandardRicochetConnection implements the ConnectionHandler interface
|
|
|
|
// to handle events on connections. An instance of StandardRicochetConnection
|
|
|
|
// is created for each OpenConnection by the HandleConnection method.
|
|
|
|
type StandardRicochetConnection struct {
|
|
|
|
Conn *OpenConnection
|
|
|
|
PrivateKey *rsa.PrivateKey
|
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// Init initializes a StandardRicochetService with the cryptographic key given
|
|
|
|
// by filename.
|
|
|
|
func (srs *StandardRicochetService) Init(filename string) error {
|
2016-02-29 00:18:25 +00:00
|
|
|
pemData, err := ioutil.ReadFile(filename)
|
|
|
|
|
|
|
|
if err != nil {
|
2016-06-27 01:56:23 +00:00
|
|
|
return errors.New("Could not setup ricochet service: could not read private key")
|
2016-02-29 00:18:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
block, _ := pem.Decode(pemData)
|
|
|
|
if block == nil || block.Type != "RSA PRIVATE KEY" {
|
2016-06-27 01:56:23 +00:00
|
|
|
return errors.New("Could not setup ricochet service: no valid PEM data found")
|
2016-02-29 00:18:25 +00:00
|
|
|
}
|
|
|
|
|
2016-10-09 00:51:13 +00:00
|
|
|
srs.PrivateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)
|
2016-06-27 01:56:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.New("Could not setup ricochet service: could not parse private key")
|
|
|
|
}
|
|
|
|
|
|
|
|
publicKeyBytes, _ := asn1.Marshal(rsa.PublicKey{
|
2016-10-09 00:51:13 +00:00
|
|
|
N: srs.PrivateKey.PublicKey.N,
|
|
|
|
E: srs.PrivateKey.PublicKey.E,
|
2016-06-27 01:56:23 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
srs.serverHostname = utils.GetTorHostname(publicKeyBytes)
|
|
|
|
log.Printf("Initialised ricochet service for %s", srs.serverHostname)
|
|
|
|
|
|
|
|
return nil
|
2016-02-29 00:18:25 +00:00
|
|
|
}
|
|
|
|
|
2016-10-09 00:51:13 +00:00
|
|
|
// Listen starts listening for service connections on localhost `port`.
|
|
|
|
func (srs *StandardRicochetService) Listen(handler ServiceHandler, port int) {
|
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(port))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Cannot Listen on Port %v", port)
|
|
|
|
return
|
|
|
|
}
|
2016-02-29 00:18:25 +00:00
|
|
|
|
2016-10-09 00:51:13 +00:00
|
|
|
Serve(ln, handler)
|
2016-06-27 01:56:23 +00:00
|
|
|
}
|
|
|
|
|
2016-10-09 00:51:13 +00:00
|
|
|
// Connect initiates a new client connection to `hostname`, which must be in one
|
|
|
|
// of the forms accepted by the goricochet.Connect() method.
|
|
|
|
func (srs *StandardRicochetService) Connect(hostname string) (*OpenConnection, error) {
|
2016-06-27 01:56:23 +00:00
|
|
|
log.Printf("Connecting to...%s", hostname)
|
2016-10-09 00:51:13 +00:00
|
|
|
oc, err := Connect(hostname)
|
2016-06-27 01:56:23 +00:00
|
|
|
if err != nil {
|
2016-10-09 00:51:13 +00:00
|
|
|
return nil, errors.New("Could not connect to: " + hostname + " " + err.Error())
|
2016-06-27 01:56:23 +00:00
|
|
|
}
|
|
|
|
oc.MyHostname = srs.serverHostname
|
2016-10-09 00:51:13 +00:00
|
|
|
return oc, nil
|
2016-06-27 01:56:23 +00:00
|
|
|
}
|
|
|
|
|
2016-10-09 00:51:13 +00:00
|
|
|
// OnNewConnection is called for new inbound connections to our service. This
|
|
|
|
// method implements the ServiceHandler interface.
|
|
|
|
func (srs *StandardRicochetService) OnNewConnection(oc *OpenConnection) {
|
|
|
|
oc.MyHostname = srs.serverHostname
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnFailedConnection is called for inbound connections that fail to successfully
|
|
|
|
// complete version negotiation for any reason. This method implements the
|
|
|
|
// ServiceHandler interface.
|
|
|
|
func (srs *StandardRicochetService) OnFailedConnection(err error) {
|
|
|
|
log.Printf("Inbound connection failed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------
|
|
|
|
|
|
|
|
// OnReady is called when a client or server sucessfully passes Version Negotiation.
|
|
|
|
func (src *StandardRicochetConnection) OnReady(oc *OpenConnection) {
|
|
|
|
src.Conn = oc
|
2016-06-27 01:56:23 +00:00
|
|
|
if oc.Client {
|
2016-10-09 00:51:13 +00:00
|
|
|
log.Printf("Successfully connected to %s", oc.OtherHostname)
|
2016-06-27 01:56:23 +00:00
|
|
|
oc.IsAuthed = true // Connections to Servers are Considered Authenticated by Default
|
|
|
|
oc.Authenticate(1)
|
|
|
|
} else {
|
2016-10-09 00:51:13 +00:00
|
|
|
log.Printf("Inbound connection received")
|
2016-06-27 01:56:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-29 00:18:25 +00:00
|
|
|
|
2016-10-01 22:52:35 +00:00
|
|
|
// OnDisconnect is called when a connection is closed
|
2016-10-09 00:51:13 +00:00
|
|
|
func (src *StandardRicochetConnection) OnDisconnect() {
|
|
|
|
log.Printf("Disconnected from %s", src.Conn.OtherHostname)
|
2016-10-01 22:52:35 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// OnAuthenticationRequest is called when a client requests Authentication
|
2016-10-09 00:51:13 +00:00
|
|
|
func (src *StandardRicochetConnection) OnAuthenticationRequest(channelID int32, clientCookie [16]byte) {
|
|
|
|
src.Conn.ConfirmAuthChannel(channelID, clientCookie)
|
2016-06-27 01:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnAuthenticationChallenge constructs a valid authentication challenge to the serverCookie
|
2016-10-09 00:51:13 +00:00
|
|
|
func (src *StandardRicochetConnection) OnAuthenticationChallenge(channelID int32, serverCookie [16]byte) {
|
2016-02-29 00:18:25 +00:00
|
|
|
// DER Encode the Public Key
|
|
|
|
publickeyBytes, _ := asn1.Marshal(rsa.PublicKey{
|
2016-10-09 00:51:13 +00:00
|
|
|
N: src.PrivateKey.PublicKey.N,
|
|
|
|
E: src.PrivateKey.PublicKey.E,
|
2016-02-29 00:18:25 +00:00
|
|
|
})
|
2016-10-09 00:51:13 +00:00
|
|
|
src.Conn.SendProof(1, serverCookie, publickeyBytes, src.PrivateKey)
|
2016-06-27 01:56:23 +00:00
|
|
|
}
|
2016-02-29 00:18:25 +00:00
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// OnAuthenticationProof is called when a client sends Proof for an existing authentication challenge
|
2016-10-09 00:51:13 +00:00
|
|
|
func (src *StandardRicochetConnection) OnAuthenticationProof(channelID int32, publicKey []byte, signature []byte) {
|
|
|
|
result := src.Conn.ValidateProof(channelID, publicKey, signature)
|
|
|
|
// This implementation always sends 'true', indicating that the contact is known
|
|
|
|
src.Conn.SendAuthenticationResult(channelID, result, true)
|
|
|
|
src.Conn.IsAuthed = result
|
|
|
|
src.Conn.CloseChannel(channelID)
|
2016-02-29 00:18:25 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// OnAuthenticationResult is called once a server has returned the result of the Proof Verification
|
2016-10-09 00:51:13 +00:00
|
|
|
func (src *StandardRicochetConnection) OnAuthenticationResult(channelID int32, result bool, isKnownContact bool) {
|
|
|
|
src.Conn.IsAuthed = result
|
2016-02-29 00:18:25 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// IsKnownContact allows a caller to determine if a hostname an authorized contact.
|
2016-10-09 00:51:13 +00:00
|
|
|
func (src *StandardRicochetConnection) IsKnownContact(hostname string) bool {
|
2016-06-27 01:56:23 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnContactRequest is called when a client sends a new contact request
|
2016-10-09 00:51:13 +00:00
|
|
|
func (src *StandardRicochetConnection) OnContactRequest(channelID int32, nick string, message string) {
|
2016-06-27 01:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnContactRequestAck is called when a server sends a reply to an existing contact request
|
2016-10-09 00:51:13 +00:00
|
|
|
func (src *StandardRicochetConnection) OnContactRequestAck(channelID int32, status string) {
|
2016-06-27 01:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnOpenChannelRequest is called when a client or server requests to open a new channel
|
2016-10-09 00:51:13 +00:00
|
|
|
func (src *StandardRicochetConnection) OnOpenChannelRequest(channelID int32, channelType string) {
|
|
|
|
src.Conn.AckOpenChannel(channelID, channelType)
|
2016-06-27 01:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnOpenChannelRequestSuccess is called when a client or server responds to an open channel request
|
2016-10-09 00:51:13 +00:00
|
|
|
func (src *StandardRicochetConnection) OnOpenChannelRequestSuccess(channelID int32) {
|
2016-06-27 01:56:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-08 23:04:11 +00:00
|
|
|
// OnChannelClosed is called when a client or server closes an existing channel
|
2016-10-09 00:51:13 +00:00
|
|
|
func (src *StandardRicochetConnection) OnChannelClosed(channelID int32) {
|
2016-06-27 01:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnChatMessage is called when a new chat message is received.
|
2016-10-09 00:51:13 +00:00
|
|
|
func (src *StandardRicochetConnection) OnChatMessage(channelID int32, messageID int32, message string) {
|
|
|
|
src.Conn.AckChatMessage(channelID, messageID)
|
2016-06-27 01:56:23 +00:00
|
|
|
}
|
2016-02-29 00:18:25 +00:00
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// OnChatMessageAck is called when a new chat message is ascknowledged.
|
2016-10-09 00:51:13 +00:00
|
|
|
func (src *StandardRicochetConnection) OnChatMessageAck(channelID int32, messageID int32) {
|
2016-02-29 00:18:25 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// OnFailedChannelOpen is called when a server fails to open a channel
|
2016-10-09 00:51:13 +00:00
|
|
|
func (src *StandardRicochetConnection) OnFailedChannelOpen(channelID int32, errorType string) {
|
|
|
|
src.Conn.UnsetChannel(channelID)
|
2016-02-29 00:18:25 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// OnGenericError is called when a generalized error is returned from the peer
|
2016-10-09 00:51:13 +00:00
|
|
|
func (src *StandardRicochetConnection) OnGenericError(channelID int32) {
|
|
|
|
src.Conn.RejectOpenChannel(channelID, "GenericError")
|
2016-02-29 00:18:25 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
//OnUnknownTypeError is called when an unknown type error is returned from the peer
|
2016-10-09 00:51:13 +00:00
|
|
|
func (src *StandardRicochetConnection) OnUnknownTypeError(channelID int32) {
|
|
|
|
src.Conn.RejectOpenChannel(channelID, "UnknownTypeError")
|
2016-02-29 00:18:25 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// OnUnauthorizedError is called when an unathorized error is returned from the peer
|
2016-10-09 00:51:13 +00:00
|
|
|
func (src *StandardRicochetConnection) OnUnauthorizedError(channelID int32) {
|
|
|
|
src.Conn.RejectOpenChannel(channelID, "UnauthorizedError")
|
2016-02-29 00:18:25 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// OnBadUsageError is called when a bad usage error is returned from the peer
|
2016-10-09 00:51:13 +00:00
|
|
|
func (src *StandardRicochetConnection) OnBadUsageError(channelID int32) {
|
|
|
|
src.Conn.RejectOpenChannel(channelID, "BadUsageError")
|
2016-02-29 00:18:25 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// OnFailedError is called when a failed error is returned from the peer
|
2016-10-09 00:51:13 +00:00
|
|
|
func (src *StandardRicochetConnection) OnFailedError(channelID int32) {
|
|
|
|
src.Conn.RejectOpenChannel(channelID, "FailedError")
|
2016-02-29 00:18:25 +00:00
|
|
|
}
|