First steps on the mailproxy implementation

parent d09a1ac8
package client
import (
"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))
}
package client
import (
"fmt"
"os"
"path"
"github.com/katzenpost/core/crypto/ecdh"
"github.com/katzenpost/core/crypto/eddsa"
"github.com/katzenpost/mailproxy/config"
)
type Config struct {
PkiAddress string
PkiKey string
User string
Provider string
LinkKey Key
Log LogConfig
DataDir string
}
// LogConfig keeps the configuration of the loger
type LogConfig struct {
File string
Level string
Enabled bool
}
func (c Config) getAuthority() *config.NonvotingAuthority {
var pkiPublicKey eddsa.PublicKey
pkiPublicKey.FromString(c.PkiKey)
return &config.NonvotingAuthority{
Address: c.PkiAddress,
PublicKey: &pkiPublicKey,
}
}
func (c Config) getAccount() *config.Account {
var identityKey ecdh.PrivateKey
identityKey.FromBytes(identityKeyBytes)
return &config.Account{
User: c.User,
Provider: c.Provider,
Authority: pkiName,
LinkKey: c.LinkKey.priv,
IdentityKey: &identityKey,
}
}
func (c Config) getDataDir() (string, error) {
if c.DataDir != "" {
return c.DataDir, nil
}
workingDir, err := os.Getwd()
if err != nil {
return "", err
}
return path.Join(workingDir, "data"), nil
}
func (c Config) getLogging() *config.Logging {
if c.Log.Level != "" {
return &config.Logging{
File: c.Log.File,
Level: c.Log.Level,
Disable: !c.Log.Enabled,
}
}
return nil
}
func (c Config) getAddress() string {
return fmt.Sprintf("%s@%s", c.User, c.Provider)
}
// 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,
}
}
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