Beispiel #1
0
// Serve starts a secure echo server on the given listener.
func Serve(l net.Listener) error {
	// Generate new private/public key pair using a crypto secure random key
	pub, priv, err := crypto.GenerateKeyPair()
	if err != nil {
		return err
	}

	// Start a messenger server with our custom handshaker
	hs := &Handshake{
		LocalPublicKey: *pub,
	}

	for {
		conn, err := l.Accept()
		if err != nil {
			return nil
		}

		// Serve the request with messenger
		go messenger.Serve(
			conn,
			hs,
			NewSecureWriter(conn, priv, &hs.RemotePublicKey),
			NewSecureReader(conn, priv, &hs.RemotePublicKey),
		)
	}
}
Beispiel #2
0
// Dial generates a private/public key pair, connects to the server (using
// the messenger package) and returns a ReadWriteCloser per test specs.
func Dial(addr string) (io.ReadWriteCloser, error) {
	// Generate new private/public key pair using a crypto secure random key
	pub, priv, err := crypto.GenerateKeyPair()
	if err != nil {
		return nil, err
	}

	hs := &Handshake{
		LocalPublicKey: *pub,
	}

	// Dial a messenger with our custom handshaker. if it fails, send a blank message over
	// the connection (to satisfy test case TestSecureDial
	conn, err := messenger.Dial(addr, hs)
	if err != nil {
		return nil, err
	}

	// Create new type satisfying the ReadWriteCloser interface but with the crypto Reader/Writer
	return &cryptoReadWriteCloser{
		reader: NewSecureReader(conn, priv, &hs.RemotePublicKey),
		writer: NewSecureWriter(conn, priv, &hs.RemotePublicKey),
	}, nil
}