2018-04-22 18:54:16 +00:00
|
|
|
package client
|
2018-04-21 17:48:18 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-22 17:18:35 +00:00
|
|
|
"errors"
|
2018-04-22 18:54:16 +00:00
|
|
|
"fmt"
|
2018-04-25 06:20:57 +00:00
|
|
|
"git.xdrm.io/schastsp/internal/client/keyset"
|
2018-04-22 18:54:16 +00:00
|
|
|
"git.xdrm.io/schastsp/context"
|
2018-04-25 06:17:20 +00:00
|
|
|
"git.xdrm.io/schastsp/util/scha"
|
2018-04-21 23:25:17 +00:00
|
|
|
"io"
|
2018-04-21 17:48:18 +00:00
|
|
|
)
|
|
|
|
|
2018-04-22 23:08:18 +00:00
|
|
|
const DEBUG = false
|
2018-04-21 17:48:18 +00:00
|
|
|
|
|
|
|
/* (1) Structure
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
type T struct {
|
|
|
|
ctx *context.T // shared context
|
2018-04-22 18:54:16 +00:00
|
|
|
key *keyset.T // current key
|
|
|
|
sync *keyset.T // next bufferised key
|
2018-04-21 17:48:18 +00:00
|
|
|
fkey *config // key file management
|
|
|
|
fsync *config // sync file management
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Constructor
|
|
|
|
*
|
|
|
|
* @ctx<context.T> Shared context
|
|
|
|
* @saveDir<string> Configuration path
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
func New(ctx *context.T, saveDir string) (*T, error) {
|
|
|
|
|
2018-04-22 18:54:16 +00:00
|
|
|
var err error
|
2018-04-21 17:48:18 +00:00
|
|
|
|
2018-04-22 18:54:16 +00:00
|
|
|
inst := new(T)
|
2018-04-21 17:48:18 +00:00
|
|
|
|
|
|
|
/* (1) Store context */
|
2018-04-22 19:15:19 +00:00
|
|
|
if ctx == nil { return nil, errors.New("Context must not be nil"); }
|
2018-04-22 18:54:16 +00:00
|
|
|
inst.ctx = ctx
|
2018-04-21 17:48:18 +00:00
|
|
|
|
|
|
|
/* (2) Get file management for KEY */
|
2018-04-22 18:54:16 +00:00
|
|
|
inst.fkey, err = Config(saveDir, "key")
|
2018-04-24 16:32:00 +00:00
|
|
|
if err != nil { return nil, err }
|
2018-04-21 17:48:18 +00:00
|
|
|
|
|
|
|
/* (3) Get file management for SYNC */
|
2018-04-22 18:54:16 +00:00
|
|
|
inst.fsync, err = Config(saveDir, "sync")
|
2018-04-24 16:32:00 +00:00
|
|
|
if err != nil { return nil, err }
|
2018-04-21 17:48:18 +00:00
|
|
|
|
2018-04-21 22:59:23 +00:00
|
|
|
/* (4) Restore from config */
|
2018-04-22 18:54:16 +00:00
|
|
|
inst.updateConfig()
|
2018-04-21 17:48:18 +00:00
|
|
|
|
2018-04-22 18:54:16 +00:00
|
|
|
return inst, nil
|
2018-04-21 17:48:18 +00:00
|
|
|
|
2018-04-21 23:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Processes and sends a new request
|
|
|
|
*
|
|
|
|
* @w<io.Writer> Writer to send into
|
|
|
|
*
|
|
|
|
* @return err<error> Error
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-04-22 16:45:30 +00:00
|
|
|
func (c *T) Send(w io.Writer) error {
|
|
|
|
|
|
|
|
/* (1) Generate the request */
|
2018-04-22 22:02:55 +00:00
|
|
|
x1 := make([]byte, scha.HSIZE)
|
|
|
|
x2 := make([]byte, scha.HSIZE)
|
2018-04-22 16:45:30 +00:00
|
|
|
|
2018-04-22 18:54:16 +00:00
|
|
|
err := c.generateRequest(x1, x2)
|
2018-04-22 22:02:55 +00:00
|
|
|
if err != nil { return err }
|
2018-04-22 16:45:30 +00:00
|
|
|
|
|
|
|
/* (2) Write request into writer */
|
2018-04-22 22:02:55 +00:00
|
|
|
written, err := w.Write(x1)
|
|
|
|
if err != nil { return err }
|
|
|
|
if written != len(x1) { return errors.New("Cannot write x1") }
|
|
|
|
|
|
|
|
written, err = w.Write(x2)
|
|
|
|
if err != nil { return err }
|
|
|
|
if written != len(x1) { return errors.New("Cannot write x2") }
|
2018-04-22 16:45:30 +00:00
|
|
|
|
2018-04-22 18:54:16 +00:00
|
|
|
return nil
|
2018-04-22 16:45:30 +00:00
|
|
|
}
|
2018-04-21 23:25:17 +00:00
|
|
|
|
|
|
|
/* (4) Receives and processes a response
|
|
|
|
*
|
|
|
|
* @w<io.Reader> Reader to receive from
|
|
|
|
*
|
|
|
|
* @return err<error> Error
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-04-22 17:18:35 +00:00
|
|
|
func (c *T) Receive(r io.Reader) error {
|
|
|
|
|
|
|
|
/* (1) Read error code
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
errCode := make([]byte, 1)
|
|
|
|
read, err := r.Read(errCode)
|
2018-04-22 19:04:06 +00:00
|
|
|
if err != nil { return err }
|
2018-04-22 22:02:55 +00:00
|
|
|
if uint16(read) != 1 { return errors.New("Cannot read error code") }
|
2018-04-22 17:18:35 +00:00
|
|
|
|
2018-04-24 19:29:41 +00:00
|
|
|
if DEBUG { fmt.Printf("ERROR CODE : %d\n", errCode[0]) }
|
2018-04-24 16:32:00 +00:00
|
|
|
|
2018-04-22 17:18:35 +00:00
|
|
|
/* (2) Manage success
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if errCode[0] == 0 {
|
|
|
|
|
|
|
|
/* (1) If pending migration -> migrate */
|
|
|
|
if c.key.MigrationCode() == 2 {
|
2018-04-23 16:09:31 +00:00
|
|
|
c.migrateKey()
|
|
|
|
if DEBUG { fmt.Printf("*** VALIDATED MIGRATION\n") }
|
2018-04-22 17:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) No error anyway */
|
2018-04-22 18:54:16 +00:00
|
|
|
return nil
|
2018-04-22 17:18:35 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Manage rescue
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Read y1 */
|
2018-04-22 18:54:16 +00:00
|
|
|
y1 := make([]byte, scha.HSIZE)
|
2018-04-22 17:18:35 +00:00
|
|
|
read, err = r.Read(y1)
|
2018-04-22 19:04:06 +00:00
|
|
|
if err != nil { return err }
|
2018-04-22 22:02:55 +00:00
|
|
|
if uint16(read) != scha.HSIZE { return errors.New("Cannot read y1") }
|
2018-04-22 17:18:35 +00:00
|
|
|
|
|
|
|
/* (2) Read y2 */
|
2018-04-22 18:54:16 +00:00
|
|
|
y2 := make([]byte, scha.HSIZE)
|
2018-04-22 17:18:35 +00:00
|
|
|
read, err = r.Read(y2)
|
2018-04-22 19:04:06 +00:00
|
|
|
if err != nil { return err }
|
2018-04-22 22:02:55 +00:00
|
|
|
if uint16(read) != scha.HSIZE { return errors.New("Cannot read enough y2") }
|
2018-04-22 17:18:35 +00:00
|
|
|
|
|
|
|
/* (3) Manage rescue mode */
|
2018-04-22 18:54:16 +00:00
|
|
|
err = c.rescue(y1, y2)
|
2018-04-22 23:08:18 +00:00
|
|
|
if err != nil { return err }
|
|
|
|
|
2018-04-23 16:09:31 +00:00
|
|
|
if DEBUG { fmt.Printf("*** MIGRATION PREPARED\n") }
|
2018-04-22 17:18:35 +00:00
|
|
|
|
2018-04-22 23:08:18 +00:00
|
|
|
return nil
|
2018-04-22 17:18:35 +00:00
|
|
|
|
2018-04-22 18:54:16 +00:00
|
|
|
}
|
2018-04-22 22:02:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-24 16:43:20 +00:00
|
|
|
/* (5) Returns a synchronisation key (first server connection)
|
|
|
|
*
|
|
|
|
* @return key<[]byte> Synchronisation key
|
|
|
|
* @return err<error>
|
2018-04-22 22:02:55 +00:00
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-04-24 16:43:20 +00:00
|
|
|
func (c *T) SynchronisationKey() ([]byte, error) {
|
|
|
|
|
2018-04-24 16:48:05 +00:00
|
|
|
/* (1) Reset keys so no value can be guessed*/
|
|
|
|
c.migrateKey(); // 1: copies 'sync' into 'key'
|
|
|
|
c.migrateKey(); // 2: copies random new 'sync' into 'key' (old 'sync)
|
|
|
|
|
|
|
|
/* (2) Get current hash */
|
2018-04-22 22:02:55 +00:00
|
|
|
hash, err := c.key.CurrentHash()
|
2018-04-24 16:43:20 +00:00
|
|
|
if err != nil { return nil, err }
|
2018-04-22 22:02:55 +00:00
|
|
|
|
2018-04-24 16:48:05 +00:00
|
|
|
/* (3) Decrement key so 'hash' is valid */
|
2018-04-22 22:02:55 +00:00
|
|
|
c.key.Decrement()
|
|
|
|
|
2018-04-24 16:48:05 +00:00
|
|
|
/* (4) Return key */
|
2018-04-24 16:43:20 +00:00
|
|
|
return hash, nil;
|
2018-04-22 22:02:55 +00:00
|
|
|
}
|