Commit 4c51db0d authored by David Stainton's avatar David Stainton

Add java bindings golang code

parent 603cde05
// 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"
)
// Client is katzenpost object
type Client 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 NewClient(pkiAddress, pkiKey string, logConfig LogConfig) (*Client, 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(Client)
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
}
// 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"
"errors"
"fmt"
"github.com/katzenpost/client"
"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")
}
// Session holds the client session
type Session struct {
client *client.Client
log *logging.Logger
clientCfg *client.Config
sessionCfg *client.SessionConfig
session *client.Session
ingressMsgQueue chan string
}
// NewSession stablishes a session with provider using key
func (c *Client) NewSession(user string, provider string, linkPrivKey *ecdh.PrivateKey) (*Session, error) {
var err error
session := new(Session)
clientCfg := &client.Config{
User: user,
Provider: provider,
LinkKey: linkPrivKey,
LogBackend: c.log,
PKIClient: c.pki,
}
gClient, err := client.New(clientCfg)
if err != nil {
return session, err
}
session.client = gClient
session.ingressMsgQueue = make(chan string, 100)
session.log = c.log.GetLogger(fmt.Sprintf("session_%s@%s", user, provider))
return session, err
}
// 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 (s *Session) ReceivedMessage(senderPubKey *ecdh.PublicKey, message []byte) {
s.log.Debug("ReceivedMessage")
s.ingressMsgQueue <- string(message)
}
// GetMessage blocks until there is a message in the inbox
func (s *Session) GetMessage() string {
s.log.Debug("GetMessage")
return <-s.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 (s *Session) ReceivedACK(messageID *[block.MessageIDLength]byte, message []byte) {
s.log.Debug("ReceivedACK")
}
// 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 (s *Session) Get(identity string) (*ecdh.PublicKey, error) {
s.log.Debugf("Get identity %s", identity)
return nil, nil
}
// Connect connects the client to the Provider
func (s *Session) Connect(identityPrivKey *ecdh.PrivateKey) error {
sessionCfg := client.SessionConfig{
User: s.clientCfg.User,
Provider: s.clientCfg.Provider,
IdentityPrivKey: identityPrivKey,
LinkPrivKey: s.clientCfg.LinkKey,
MessageConsumer: s,
Storage: new(StorageStub),
UserKeyDiscovery: s,
}
s.sessionCfg = &sessionCfg
var err error
s.session, err = s.client.NewSession(&sessionCfg)
return err
}
// 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