Documentation ¶
Overview ¶
Package e4 provides a e4 client implementation and libraries.
It aims to be quick and easy to integrate in IoT devices applications enabling to secure their communications, as well as exposing a way to manage the various keys required.
Protecting and unprotecting messages ¶
Once created, a client provide methods to protect messages before sending them to the broker:
protectedMessage, err := client.ProtectMessage([]byte("secret message"), topicKey)
or unprotecting the messages it receives.
originalMessage, err := client.Unprotect([]byte(protectedMessage, topicKey))
ReceivingTopic and client commands ¶
A special topic (called ReceivingTopic) is reserved to communicate protected commands to the client. Such commands are used to update the client state, like setting a new key for a topic, or renewing its private key. There is nothing particular to be done when receiving a command, just passing its protected form to the Unprotect() method and the client will automatically unprotect and process it (thus returning no unprotected message). See commands.go for the list of available commands and their respective parameters.
Index ¶
- Variables
- func CmdRemovePubKey(name string) ([]byte, error)
- func CmdRemoveTopic(topic string) ([]byte, error)
- func CmdResetPubKeys() ([]byte, error)
- func CmdResetTopics() ([]byte, error)
- func CmdSetIDKey(key []byte) ([]byte, error)
- func CmdSetPubKey(pubKey ed25519.PublicKey, name string) ([]byte, error)
- func CmdSetTopicKey(topicKey []byte, topic string) ([]byte, error)
- func TopicForID(id []byte) string
- type Client
- type ClientConfig
- type Command
- type PubIDAndKey
- type PubNameAndPassword
- type SymIDAndKey
- type SymNameAndPassword
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrTopicKeyNotFound occurs when a topic key is missing when encryption/decrypting ErrTopicKeyNotFound = errors.New("topic key not found") // ErrUnsupportedOperation occurs when trying to manipulate client public keys with a ClientKey not supporting it ErrUnsupportedOperation = errors.New("this operation is not supported") )
var ( // ErrInvalidCommand is returned when trying to process an unsupported command ErrInvalidCommand = errors.New("invalid command") )
Functions ¶
func CmdRemovePubKey ¶
CmdRemovePubKey creates a command to remove the public key identified by given name from the client
func CmdRemoveTopic ¶
CmdRemoveTopic creates a command to remove the key associated with the topic, from the client
func CmdResetPubKeys ¶
CmdResetPubKeys creates a command to removes all public keys from the client
func CmdResetTopics ¶
CmdResetTopics creates a command to remove all topic keys stored on the client
func CmdSetIDKey ¶
CmdSetIDKey creates a command to set the client private key to the given key
func CmdSetPubKey ¶
CmdSetPubKey creates a command to set a given public key, identified by given name on the client
func CmdSetTopicKey ¶
CmdSetTopicKey creates a command to set the given topic key and its corresponding topic, on the client
func TopicForID ¶
TopicForID generate the receiving topic that a client should subscribe to in order to receive commands
Types ¶
type Client ¶
type Client interface { // ProtectMessage will encrypt the given payload using the key associated to topic. // When the client doesn't have a key for this topic, ErrTopicKeyNotFound will be returned. // When no errors, the protected cipher bytes are returned ProtectMessage(payload []byte, topic string) ([]byte, error) // Unprotect attempts to decrypt the given cipher using the topic key. // When the client doesn't have a key for this topic, ErrTopicKeyNotFound will be returned. // When no errors, the clear payload bytes are returned, unless the protected message was a client command. // Message are client commands when received on the client receiving topic. The command will be processed // when unprotecting it, making a nil,nil response indicating a success Unprotect(protected []byte, topic string) ([]byte, error) // IsReceivingTopic returns true when the given topic is the client receiving topics. // Message received from this topics will be protected commands, meant to update the client state IsReceivingTopic(topic string) bool // GetReceivingTopic returns the receiving topic for this client, which will be used to transmit commands // allowing to update the client state, like setting a new private key or adding a new topic key. GetReceivingTopic() string // contains filtered or unexported methods }
Client defines interface for protecting and unprotecting E4 messages and commands
func LoadClient ¶
LoadClient loads a client state from the file system
func NewClient ¶
func NewClient(config ClientConfig, persistStatePath string) (Client, error)
NewClient creates a new E4 client, working either in symmetric key mode, or public key mode depending the given ClientConfig
config is a ClientConfig, either SymIDAndKey, SymNameAndPassword, PubIDAndKey or PubNameAndPassword persistStatePath is the file system path to the file to read and persist the client's state.
Example (PubIDAndKey) ¶
package main import ( "fmt" e4 "github.com/teserakt-io/e4go" e4crypto "github.com/teserakt-io/e4go/crypto" ) func main() { privateKey, err := e4crypto.Ed25519PrivateKeyFromPassword("verySecretPassword") if err != nil { panic(err) } c2PubKey, _, err := e4crypto.RandomCurve25519Keys() if err != nil { panic(err) } client, err := e4.NewClient(&e4.PubIDAndKey{ ID: []byte("clientID"), Key: privateKey, C2PubKey: c2PubKey, }, "./pubClient.json") if err != nil { panic(err) } protectedMessage, err := client.ProtectMessage([]byte("very secret message"), "topic/name") if err != nil { panic(err) } fmt.Printf("Protected message: %v", protectedMessage) }
Output:
Example (PubNameAndPassword) ¶
package main import ( "fmt" e4 "github.com/teserakt-io/e4go" e4crypto "github.com/teserakt-io/e4go/crypto" ) func main() { c2PubKey, _, err := e4crypto.RandomCurve25519Keys() if err != nil { panic(err) } config := &e4.PubNameAndPassword{ Name: "clientName", Password: "verySecretPassword", C2PubKey: c2PubKey, } client, err := e4.NewClient(config, "./pubClient.json") if err != nil { panic(err) } // We may need to get the public key derived from the password: pubKey, err := config.PubKey() if err != nil { panic(err) } fmt.Printf("Client public key: %x", pubKey) protectedMessage, err := client.ProtectMessage([]byte("very secret message"), "topic/name") if err != nil { panic(err) } fmt.Printf("Protected message: %v", protectedMessage) }
Output:
Example (SymIDAndKey) ¶
package main import ( "fmt" e4 "github.com/teserakt-io/e4go" e4crypto "github.com/teserakt-io/e4go/crypto" ) func main() { client, err := e4.NewClient(&e4.SymIDAndKey{ID: []byte("clientID"), Key: e4crypto.RandomKey()}, "./symClient.json") if err != nil { panic(err) } protectedMessage, err := client.ProtectMessage([]byte("very secret message"), "topic/name") if err != nil { panic(err) } fmt.Printf("Protected message: %v", protectedMessage) }
Output:
Example (SymNameAndPassword) ¶
package main import ( "fmt" e4 "github.com/teserakt-io/e4go" ) func main() { client, err := e4.NewClient(&e4.SymNameAndPassword{Name: "clientName", Password: "verySecretPassword"}, "./symClient.json") if err != nil { panic(err) } protectedMessage, err := client.ProtectMessage([]byte("very secret message"), "topic/name") if err != nil { panic(err) } fmt.Printf("Protected message: %v", protectedMessage) }
Output:
type ClientConfig ¶
type ClientConfig interface {
// contains filtered or unexported methods
}
ClientConfig defines an interface for client configuration
type Command ¶
type Command int
Command is a command sent by C2 to a client. This is a sequence of bytes, starting from a Command, followed by the command arguments. Such command message must then be protected using the client key, before being passed to the client's Unprotect() method. The command will then be unprotected, and processed.
const ( // RemoveTopic command allows to remove a topic key from the client. // It expects a topic hash as argument RemoveTopic Command = iota // ResetTopics allows to clear out all the topics on a client. // It doesn't have any argument ResetTopics // SetIDKey allows to set the private key of a client. // It expects a key as argument SetIDKey // SetTopicKey allows to add a topic key on the client. // It takes a key, followed by a topic hash as arguments. SetTopicKey // RemovePubKey allows to remove a public key from the client. // It takes the ID to be removed as argument RemovePubKey // ResetPubKeys removes all public keys stored on the client. // It expects no argument ResetPubKeys // SetPubKey allows to set a public key on the client. // It takes a public key, followed by an ID as arguments. SetPubKey // UnknownCommand must stay the last element. It's used to // know if a Command is out of range UnknownCommand )
List of supported commands
type PubIDAndKey ¶
type PubIDAndKey struct { ID []byte Key ed25519.PrivateKey C2PubKey e4crypto.Curve25519PublicKey }
PubIDAndKey defines a configuration to create an E4 client in public key mode from an ID, an ed25519 private key, and a curve25519 public key.
type PubNameAndPassword ¶
type PubNameAndPassword struct { Name string Password string C2PubKey e4crypto.Curve25519PublicKey }
PubNameAndPassword defines a configuration to create an E4 client in public key mode from a name, a password and a curve25519 public key. The password must contains at least 16 characters.
type SymIDAndKey ¶
SymIDAndKey defines a configuration to create an E4 client in symmetric key mode from an ID and a symmetric key
type SymNameAndPassword ¶
SymNameAndPassword defines a configuration to create an E4 client in symmetric key mode from a name and a password. The password must contains at least 16 characters.
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
Package crypto defines the cryptographic functions used in E4
|
Package crypto defines the cryptographic functions used in E4 |
Package keys holds E4 key material implementations.
|
Package keys holds E4 key material implementations. |