Commit 21ea946c authored by Vincent Breitmoser's avatar Vincent Breitmoser

copy new bindings over to java

parent 72a35731
// 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"
"github.com/katzenpost/core/crypto/ecdh"
"github.com/katzenpost/mailproxy"
"github.com/katzenpost/mailproxy/config"
"github.com/katzenpost/mailproxy/event"
)
// KatzenClient is katzenpost object
type KatzenClient struct {
log *log.Backend
pki cpki.Client
}
const (
pkiName = "default"
)
// LogConfig keeps the configuration of the loger
type LogConfig struct {
File string
Level string
Enabled bool
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
}
// NewClient configures the pki to be used
func NewKatzenClient(pkiAddress, pkiKey string, logConfig *LogConfig) (*KatzenClient, error) {
var pubKey eddsa.PublicKey
err := pubKey.FromString(pkiKey)
func New(cfg Config) (Client, error) {
eventSink := make(chan event.Event)
dataDir, err := cfg.getDataDir()
if err != nil {
return nil, err
return Client{}, err
}
logLevel := "NOTICE"
if logConfig.Level != "" {
logLevel = logConfig.Level
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{},
}
client := new(KatzenClient)
client.log, err = log.New(logConfig.File, logLevel, !logConfig.Enabled)
err = proxyCfg.FixupAndValidate()
if err != nil {
return nil, err
return Client{}, err
}
pkiCfg := npki.Config{
LogBackend: client.log,
Address: pkiAddress,
PublicKey: &pubKey,
}
client.pki, err = npki.New(&pkiCfg)
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)
}
......@@ -18,7 +18,7 @@
package client
import (
"encoding/base64"
"encoding/hex"
"github.com/katzenpost/core/crypto/ecdh"
"github.com/katzenpost/core/crypto/rand"
......@@ -26,35 +26,41 @@ import (
// Key keeps the key public and private data
type Key struct {
priv *ecdh.PrivateKey
Private string
Public string
priv *ecdh.PrivateKey
}
// GenKey creates a new ecdh key
func GenKey() (*Key, error) {
mKey := new(Key)
var err error
mKey.priv, err = ecdh.NewKeypair(rand.Reader)
func GenKey() (Key, error) {
key, err := ecdh.NewKeypair(rand.Reader)
if err != nil {
return mKey, err
return Key{}, err
}
return mKey, err
return buildKey(key), nil
}
// StringToKey builds a Key from a string
func KeyFromBase64(keyStr string) (*Key, error) {
func StringToKey(keyStr string) (Key, error) {
var key ecdh.PrivateKey
keyBytes, err := base64.StdEncoding.DecodeString(keyStr)
keyBytes, err := hex.DecodeString(keyStr)
if err != nil {
return &Key{}, err
return Key{}, err
}
err = key.FromBytes(keyBytes)
if err != nil {
return &Key{}, err
return Key{}, err
}
k := Key{
priv: &key,
return buildKey(&key), nil
}
func buildKey(key *ecdh.PrivateKey) Key {
return Key{
Private: hex.EncodeToString(key.Bytes()),
Public: key.PublicKey().String(),
priv: key,
}
return &k, nil
}
// 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"
)
// KatzenClient is katzenpost object
type KatzenClient struct {
log *log.Backend
pki cpki.Client
}
// LogConfig keeps the configuration of the loger
type LogConfig struct {
File string
Level string
Enabled bool
}
// NewClient configures the pki to be used
func NewKatzenClient(pkiAddress, pkiKey string, logConfig *LogConfig) (*KatzenClient, error) {
var pubKey eddsa.PublicKey
err := pubKey.FromString(pkiKey)
if err != nil {
return nil, err
}
logLevel := "NOTICE"
if logConfig.Level != "" {
logLevel = logConfig.Level
}
client := new(KatzenClient)
client.log, err = log.New(logConfig.File, logLevel, !logConfig.Enabled)
if err != nil {
return nil, err
}
pkiCfg := npki.Config{
LogBackend: client.log,
Address: pkiAddress,
PublicKey: &pubKey,
}
client.pki, err = npki.New(&pkiCfg)
return client, err
}
// 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/base64"
"github.com/katzenpost/core/crypto/ecdh"
"github.com/katzenpost/core/crypto/rand"
)
// Key keeps the key public and private data
type Key struct {
priv *ecdh.PrivateKey
}
// GenKey creates a new ecdh key
func GenKey() (*Key, error) {
mKey := new(Key)
var err error
mKey.priv, err = ecdh.NewKeypair(rand.Reader)
if err != nil {
return mKey, err
}
return mKey, err
}
// StringToKey builds a Key from a string
func KeyFromBase64(keyStr string) (*Key, error) {
var key ecdh.PrivateKey
keyBytes, err := base64.StdEncoding.DecodeString(keyStr)
if err != nil {
return &Key{}, err
}
err = key.FromBytes(keyBytes)
if err != nil {
return &Key{}, err
}
k := Key{
priv: &key,
}
return &k, nil
}
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