Example #1
0
func (cah *connectionAwaitHandshake) start() (bool, error) {

	helloSeg := cah.makeHello()
	if err := cah.send(server.SegToBytes(helloSeg)); err != nil {
		return cah.maybeRestartConnection(err)
	}

	if seg, err := cah.readOne(); err == nil {
		hello := cmsgs.ReadRootHello(seg)
		if cah.verifyHello(&hello) {
			if hello.IsClient() {
				cah.isClient = true
				cah.nextState(&cah.connectionAwaitClientHandshake)

			} else {
				cah.isServer = true
				cah.nextState(&cah.connectionAwaitServerHandshake)
			}
			return false, nil

		} else {
			return cah.maybeRestartConnection(fmt.Errorf("Received erroneous hello from peer"))
		}
	} else {
		return cah.maybeRestartConnection(err)
	}
}
Example #2
0
func (cah *connectionAwaitHandshake) start() (bool, error) {
	helloSeg, err := cah.makeHello()
	if err != nil {
		return false, err
	}
	buf := new(bytes.Buffer)
	if _, err := helloSeg.WriteTo(buf); err != nil {
		return false, err
	}
	if err := cah.send(buf.Bytes()); err != nil {
		return false, err
	}

	if seg, err := capn.ReadFromStream(cah.socket, nil); err == nil {
		if hello := msgs.ReadRootHello(seg); cah.verifyHello(&hello) {
			sessionKey := [32]byte{}
			remotePublicKey := [32]byte{}
			copy(remotePublicKey[:], hello.PublicKey())
			box.Precompute(&sessionKey, &remotePublicKey, cah.privateKey)
			cah.sessionKey = &sessionKey
			cah.nonceAryOut[0] = 128
			cah.nonce = 0
			cah.nextState()
		} else {
			return false, fmt.Errorf("Received erroneous hello from server")
		}
	} else {
		return false, err
	}

	return false, nil
}
Example #3
0
func (cah *connectionAwaitHandshake) start() (bool, error) {
	helloSeg, err := cah.makeHello()
	if err != nil {
		return cah.maybeRestartConnection(err)
	}
	if err := cah.send(server.SegToBytes(helloSeg)); err != nil {
		return cah.maybeRestartConnection(err)
	}
	cah.nonce = 0

	if seg, err := capn.ReadFromStream(cah.socket, nil); err == nil {
		hello := msgs.ReadRootHello(seg)
		if cah.verifyHello(&hello) {
			sessionKey := [32]byte{}
			remotePublicKey := [32]byte{}
			copy(remotePublicKey[:], hello.PublicKey())
			box.Precompute(&sessionKey, &remotePublicKey, cah.privateKey)

			if hello.IsClient() {
				cah.Lock()
				cah.isClient = true
				cah.sessionKey = &sessionKey
				cah.Unlock()
				cah.nonceAryIn[0] = 128
				cah.nextState(&cah.connectionAwaitClientHandshake)

			} else {
				extendedKey := make([]byte, 64)
				copy(extendedKey[:32], sessionKey[:])
				copy(extendedKey[32:], cah.connectionManager.passwordHash[:])
				sessionKey = sha256.Sum256(extendedKey)
				cah.Lock()
				cah.isServer = true
				cah.sessionKey = &sessionKey
				cah.Unlock()
				if cah.remoteHost == "" {
					cah.nonceAryIn[0] = 128
				} else {
					cah.nonceAryOut[0] = 128
				}
				cah.nextState(&cah.connectionAwaitServerHandshake)
			}
			return false, nil
		} else {
			return cah.maybeRestartConnection(fmt.Errorf("Received erroneous hello from peer"))
		}
	} else {
		return cah.maybeRestartConnection(err)
	}
}