func wrapBSDA(c io.Closer, info *MethodInfo) (io.Closer, error) { if fcc, ok := c.(bsda.FrameReadWriterCloser); ok { return fcc, nil } if cc, ok := c.(io.ReadWriteCloser); ok { return bsda.New(cc), nil } return nil, fmt.Errorf("curvecp requires ReadWriteCloser (or FrameReadWriteCloser)") }
// Utility function to dial an address and create a connection on it. // // net should almost certainly be "tcp". You must set Curvek and CurveS. func Dial(netw, addr string, cfg Config, ctx context.Context) (*Conn, error) { d := cfg.Dialer if d == nil { d = denet.DefaultDialer } conn, err := d.Dial(netw, addr, ctx) if err != nil { return nil, err } return New(bsda.New(conn), cfg, ctx) }
func wrapCurveCP(c io.Closer, info *MethodInfo) (io.Closer, error) { cc, ok := c.(io.ReadWriteCloser) fcc, ok2 := c.(bsda.FrameReadWriterCloser) if !ok && !ok2 { return nil, fmt.Errorf("curvecp requires ReadWriteCloser (or FrameReadWriteCloser)") } cfg, ok := info.Pragma["curvecp"].(*curvecp.Config) if !ok { cfg = &curvecp.Config{} } if keyIsZero(&cfg.CurveS) { if info.URL.User == nil { return nil, fmt.Errorf("no server key specified for curvecp") } username := info.URL.User.Username() skey, err := curvecp.DecodeKey(username) if err != nil { return nil, err } copy(cfg.CurveS[:], skey[:]) } if keyIsZero(&cfg.Curvek) { pk, sk, err := box.GenerateKey(rand.Reader) if err != nil { return nil, err } cfg.Curvek = *sk cfg.CurveK = pk } var bc bsda.FrameReadWriterCloser if ok2 { bc = fcc } else { bc = bsda.New(cc) } c2, err := curvecp.New(bc, *cfg) if err != nil { return nil, err } return c2, nil }
func TestA(t *testing.T) { conn1, conn2, err := getLoopbackPair() if err != nil { panic(err) } bsda1, bsda2 := bsda.New(conn1), bsda.New(conn2) curveS, curves, err := box.GenerateKey(rand.Reader) if err != nil { panic("cannot generate key") } _, curvec, err := box.GenerateKey(rand.Reader) if err != nil { panic("cannot generate key") } errChan := make(chan error, 1) go func() { // server c1, err := New(bsda1, Config{ IsServer: true, Curvek: *curves, }) errChan <- err if err != nil { return } for { f, err := c1.ReadFrame() if err != nil { return } err = c1.WriteFrame(f) if err != nil { return } } }() // client c1, err := New(bsda2, Config{ Curvek: *curvec, CurveS: *curveS, }) serr := <-errChan if err != nil { t.Logf("client instantiation failed: %v", err) } if serr != nil { t.Logf("server instantiation failed: %v", serr) } if err != nil || serr != nil { t.Fatalf("instantiation failed") } for i := 0; i < 32; i++ { randbuf := make([]byte, i) fillrand(randbuf) err = c1.WriteFrame(randbuf) if err != nil { t.Fatalf("write frame error: %v", err) } f, err := c1.ReadFrame() if err != nil { t.Fatalf("read frame error: %v", err) } if !bytes.Equal(randbuf, f) { t.Fatalf("buffers are not equal") } } }