Place the new bindings in the python folder

parent ccc51727
package client
import (
"errors"
"time"
"github.com/katzenpost/core/crypto/ecdh"
"github.com/katzenpost/mailproxy"
"github.com/katzenpost/mailproxy/config"
"github.com/katzenpost/mailproxy/event"
)
const (
pkiName = "default"
)
var identityKeyBytes = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
// Client is katzenpost object
type Client struct {
address string
proxy *mailproxy.Proxy
eventSink chan event.Event
}
func New(cfg Config) (Client, error) {
eventSink := make(chan event.Event)
dataDir, err := cfg.getDataDir()
if err != nil {
return Client{}, err
}
proxyCfg := config.Config{
Proxy: &config.Proxy{
NoLaunchListeners: true,
DataDir: dataDir,
EventSink: eventSink,
},
Logging: cfg.getLogging(),
UpstreamProxy: &config.UpstreamProxy{
Type: "none",
},
NonvotingAuthority: map[string]*config.NonvotingAuthority{
pkiName: cfg.getAuthority(),
},
Account: []*config.Account{cfg.getAccount()},
Recipients: map[string]string{},
}
err = proxyCfg.FixupAndValidate()
if err != nil {
return Client{}, err
}
proxy, err := mailproxy.New(&proxyCfg)
return Client{cfg.getAddress(), proxy, eventSink}, err
}
func (c Client) Shutdown() {
c.proxy.Shutdown()
c.proxy.Wait()
}
func (c Client) Send(recipient, msg string) error {
var identityKey ecdh.PrivateKey
identityKey.FromBytes(identityKeyBytes)
c.proxy.SetRecipient(recipient, identityKey.PublicKey())
return c.proxy.SendMessage(c.address, recipient, []byte(msg))
}
type Message struct {
Sender string
Payload string
}
func (c Client) GetMessage(timeout int64) (Message, error) {
if timeout == 0 {
ev := <-c.eventSink
return c.handleEvent(ev)
}
select {
case ev := <-c.eventSink:
return c.handleEvent(ev)
case <-time.After(time.Second * time.Duration(timeout)):
return Message{}, errors.New("Timeout")
}
}
func (c Client) handleEvent(ev event.Event) (Message, error) {
switch ev.(type) {
case *event.MessageReceivedEvent:
msg, err := c.proxy.ReceivePop(c.address)
return Message{msg.SenderID, string(msg.Payload)}, err
default:
return Message{}, errors.New("Another event arrived")
}
}
// key.go - mixnet user key
// Copyright (C) 2017 Yawning Angel.
// Copyright (C) 2017 Ruben Pollan.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package client
import (
"encoding/hex"
"github.com/katzenpost/core/crypto/ecdh"
"github.com/katzenpost/core/crypto/rand"
)
// Key keeps the key public and private data
type Key struct {
Private string
Public string
priv *ecdh.PrivateKey
}
// GenKey creates a new ecdh key
func GenKey() (Key, error) {
key, err := ecdh.NewKeypair(rand.Reader)
if err != nil {
return Key{}, err
}
return buildKey(key), nil
}
// StringToKey builds a Key from a string
func StringToKey(keyStr string) (Key, error) {
var key ecdh.PrivateKey
keyBytes, err := hex.DecodeString(keyStr)
if err != nil {
return Key{}, err
}
err = key.FromBytes(keyBytes)
if err != nil {
return Key{}, err
}
return buildKey(&key), nil
}
func buildKey(key *ecdh.PrivateKey) Key {
return Key{
Private: hex.EncodeToString(key.Bytes()),
Public: key.PublicKey().String(),
priv: key,
}
}
// minclient.go - mixnet client
// Copyright (C) 2017 Yawning Angel.
// Copyright (C) 2017 Ruben Pollan.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package client provides a mixnet client library
package client
import (
npki "github.com/katzenpost/authority/nonvoting/client"
"github.com/katzenpost/core/crypto/eddsa"
"github.com/katzenpost/core/log"
cpki "github.com/katzenpost/core/pki"
"errors"
"time"
"github.com/katzenpost/core/crypto/ecdh"
"github.com/katzenpost/mailproxy"
"github.com/katzenpost/mailproxy/config"
"github.com/katzenpost/mailproxy/event"
)
const (
pkiName = "default"
)
var identityKeyBytes = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
// Client is katzenpost object
type Client struct {
log *log.Backend
pki cpki.Client
address string
proxy *mailproxy.Proxy
eventSink chan event.Event
}
// LogConfig keeps the configuration of the loger
type LogConfig struct {
File string
Level string
Enabled bool
}
func New(cfg Config) (Client, error) {
eventSink := make(chan event.Event)
dataDir, err := cfg.getDataDir()
if err != nil {
return Client{}, err
}
// NewClient configures the pki to be used
func NewClient(pkiAddress, pkiKey string, logConfig LogConfig) (Client, error) {
var client Client
proxyCfg := config.Config{
Proxy: &config.Proxy{
NoLaunchListeners: true,
DataDir: dataDir,
EventSink: eventSink,
},
Logging: cfg.getLogging(),
UpstreamProxy: &config.UpstreamProxy{
Type: "none",
},
var pubKey eddsa.PublicKey
err := pubKey.FromString(pkiKey)
NonvotingAuthority: map[string]*config.NonvotingAuthority{
pkiName: cfg.getAuthority(),
},
Account: []*config.Account{cfg.getAccount()},
Recipients: map[string]string{},
}
err = proxyCfg.FixupAndValidate()
if err != nil {
return client, err
return Client{}, err
}
logLevel := "NOTICE"
if logConfig.Level != "" {
logLevel = logConfig.Level
proxy, err := mailproxy.New(&proxyCfg)
return Client{cfg.getAddress(), proxy, eventSink}, err
}
func (c Client) Shutdown() {
c.proxy.Shutdown()
c.proxy.Wait()
}
func (c Client) Send(recipient, msg string) error {
var identityKey ecdh.PrivateKey
identityKey.FromBytes(identityKeyBytes)
c.proxy.SetRecipient(recipient, identityKey.PublicKey())
return c.proxy.SendMessage(c.address, recipient, []byte(msg))
}
type Message struct {
Sender string
Payload string
}
func (c Client) GetMessage(timeout int64) (Message, error) {
if timeout == 0 {
ev := <-c.eventSink
return c.handleEvent(ev)
}
client.log, err = log.New(logConfig.File, logLevel, !logConfig.Enabled)
if err != nil {
return client, err
select {
case ev := <-c.eventSink:
return c.handleEvent(ev)
case <-time.After(time.Second * time.Duration(timeout)):
return Message{}, errors.New("Timeout")
}
}
pkiCfg := npki.Config{
LogBackend: client.log,
Address: pkiAddress,
PublicKey: &pubKey,
func (c Client) handleEvent(ev event.Event) (Message, error) {
switch ev.(type) {
case *event.MessageReceivedEvent:
msg, err := c.proxy.ReceivePop(c.address)
return Message{msg.SenderID, string(msg.Payload)}, err
default:
return Message{}, errors.New("Another event arrived")
}
client.pki, err = npki.New(&pkiCfg)
return client, err
}
// session.go - mixnet session client
// Copyright (C) 2017 Yawning Angel, Ruben Pollan, David Stainton
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package fenced
import (
"errors"
"github.com/katzenpost/core/crypto/ecdh"
"github.com/katzenpost/minclient/block"
"github.com/op/go-logging"
)
// StorageStub implements the Storage interface
// as defined in the client library.
// XXX This should be replaced by something useful.
type StorageStub struct {
}
// GetBlocks returns a slice of blocks
func (s StorageStub) GetBlocks(*[block.MessageIDLength]byte) ([][]byte, error) {
return nil, errors.New("failure: StorageStub GetBlocks not yet implemented")
}
// PutBlock puts a block into storage
func (s StorageStub) PutBlock(*[block.MessageIDLength]byte, []byte) error {
return errors.New("failure: StorageStub PutBlock not yet implemented")
}
type MessageConsumer struct {
log *logging.Logger
ingressMsgQueue chan string
}
// ReceivedMessage is used to receive a message.
// This is a method on the MessageConsumer interface
// which is defined in the client library.
// XXX fix me
func (c MessageConsumer) ReceivedMessage(senderPubKey *ecdh.PublicKey, message []byte) {
c.log.Debug("ReceivedMessage")
c.ingressMsgQueue <- string(message)
}
// GetMessage blocks until there is a message in the inbox
func (c MessageConsumer) GetMessage() string {
c.log.Debug("GetMessage")
return <-c.ingressMsgQueue
}
// ReceivedACK is used to receive a signal that a message was received by
// the recipient Provider. This is a method on the MessageConsumer interface
// which is defined in the client library.
// XXX fix me
func (c MessageConsumer) ReceivedACK(messageID *[block.MessageIDLength]byte, message []byte) {
c.log.Debug("ReceivedACK")
}
func NewMessageConsumer(log *logging.Logger) MessageConsumer {
c := MessageConsumer{
log: log,
ingressMsgQueue: make(chan string, 100),
}
return c
}
type UserKeyDiscoveryStub struct{}
// Get returns the identity public key for a given identity.
// This is part of the UserKeyDiscovery interface defined
// in the client library.
// XXX fix me
func (u UserKeyDiscoveryStub) Get(identity string) (*ecdh.PublicKey, error) {
//u.log.Debugf("Get identity %s", identity)
return nil, nil
}
// session.go - mixnet session client
// Copyright (C) 2017 Yawning Angel, Ruben Pollan, David Stainton
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package client
import (
"encoding/hex"
"fmt"
"github.com/katzenpost/bindings/python/fenced"
"github.com/katzenpost/client"
"github.com/op/go-logging"
)
// Session holds the client session
type Session struct {
client *client.Client
log *logging.Logger
clientCfg *client.Config
sessionCfg *client.SessionConfig
session *client.Session
}
// NewSession stablishes a session with provider using key
func (c Client) NewSession(user string, provider string, key Key) (Session, error) {
var err error
var session Session
clientCfg := &client.Config{
User: user,
Provider: provider,
LinkKey: key.priv,
LogBackend: c.log,
PKIClient: c.pki,
}
gClient, err := client.New(clientCfg)
if err != nil {
return session, err
}
session.client = gClient
session.log = c.log.GetLogger(fmt.Sprintf("session_%s@%s", user, provider))
return session, err
}
// Connect connects the client to the Provider
func (s Session) Connect(identityKey Key) error {
consumer := fenced.NewMessageConsumer(s.log)
userKeyDiscoveryStub := fenced.UserKeyDiscoveryStub{}
sessionCfg := client.SessionConfig{
User: s.clientCfg.User,
Provider: s.clientCfg.Provider,
IdentityPrivKey: identityKey.priv,
LinkPrivKey: s.clientCfg.LinkKey,
MessageConsumer: consumer,
Storage: new(fenced.StorageStub),
UserKeyDiscovery: userKeyDiscoveryStub,
}
s.sessionCfg = &sessionCfg
var err error
s.session, err = s.client.NewSession(&sessionCfg)
return err
}
func (s Session) GetMessage() string {
return s.sessionCfg.MessageConsumer.(fenced.MessageConsumer).GetMessage()
}
// Shutdown the session
func (s Session) Shutdown() {
s.Shutdown()
}
// Send into the mix network
func (s Session) Send(recipient, provider, msg string) error {
raw, err := hex.DecodeString(msg)
if err != nil {
return err
}
messageID, err := s.session.Send(recipient, provider, raw)
if err != nil {
return err
}
s.log.Debugf("sent message with messageID %x", messageID)
return nil
}
// SendUnreliable into the mix network
func (s Session) SendUnreliable(recipient, provider, msg string) error {
raw, err := hex.DecodeString(msg)
if err != nil {
return err
}
return s.session.SendUnreliable(recipient, provider, raw)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment