Exemplo n.º 1
0
func CreateTestHomeServerConn(dename string, denameClient *client.Client, secretConfig *proto.LocalAccountConfig, t testing.TB) *transport.Conn {
	profile, err := denameClient.Lookup(dename)
	if err != nil {
		t.Fatal(err)
	}

	chatProfileBytes, err := client.GetProfileField(profile, PROFILE_FIELD_ID)
	if err != nil {
		t.Fatal(err)
	}

	chatProfile := new(proto.Profile)
	if err := chatProfile.Unmarshal(chatProfileBytes); err != nil {
		t.Fatal(err)
	}

	addr := chatProfile.ServerAddressTCP
	port := chatProfile.ServerPortTCP
	pkTransport := ([32]byte)(chatProfile.ServerTransportPK)
	pkp := (*[32]byte)(&chatProfile.UserIDAtServer)

	oldConn, err := net.Dial("tcp", net.JoinHostPort(addr, fmt.Sprint(port)))
	if err != nil {
		t.Fatal(err)
	}

	skp := (*[32]byte)(&secretConfig.TransportSecretKeyForServer)

	conn, _, err := transport.Handshake(oldConn, pkp, skp, &pkTransport, proto.SERVER_MESSAGE_SIZE)
	if err != nil {
		t.Fatal(err)
	}

	return conn
}
Exemplo n.º 2
0
func (d *Daemon) sendMessage(msg []byte, theirDename string, msgRatch *ratchet.Ratchet) error {
	profile := new(dename.Profile)
	err := persistence.UnmarshalFromFile(d.profilePath(theirDename), profile)
	if err != nil {
		return err
	}

	chatProfileBytes, err := client.GetProfileField(profile, util.PROFILE_FIELD_ID)
	if err != nil {
		return err
	}

	chatProfile := new(proto.Profile)
	if err := chatProfile.Unmarshal(chatProfileBytes); err != nil {
		return err
	}

	addr := chatProfile.ServerAddressTCP
	port := (int)(chatProfile.ServerPortTCP)
	pkTransport := (*[32]byte)(&chatProfile.ServerTransportPK)
	theirPk := (*[32]byte)(&chatProfile.UserIDAtServer)

	if err != nil {
		return err
	}

	theirInBuf := make([]byte, proto.SERVER_MESSAGE_SIZE)

	encMsg, ratch, err := util.EncryptAuth(msg, msgRatch)
	if err != nil {
		return err
	}

	theirConn, err := d.cc.DialServer(theirDename, addr, port, pkTransport, nil, nil)
	if err != nil {
		return err
	}
	if err := StoreRatchet(d, theirDename, ratch); err != nil {
		theirConn.Close()
		d.cc.PutClose(theirDename)
		return err
	}
	err = util.UploadMessageToUser(theirConn, theirInBuf, theirPk, encMsg)
	if err != nil {
		theirConn.Close()
		d.cc.PutClose(theirDename)
		return err
	}
	d.cc.Put(theirDename, theirConn)
	return nil
}
Exemplo n.º 3
0
func GenerateLongTermKeys(secretConfig *proto.LocalAccountConfig, publicProfile *proto.Profile, rand io.Reader) error {
	if pk, sk, err := box.GenerateKey(rand); err != nil {
		return err
	} else {
		secretConfig.TransportSecretKeyForServer = (proto.Byte32)(*sk)
		publicProfile.UserIDAtServer = (proto.Byte32)(*pk)
	}
	if pk, sk, err := box.GenerateKey(rand); err != nil {
		return err
	} else {
		secretConfig.MessageAuthSecretKey = (proto.Byte32)(*sk)
		publicProfile.MessageAuthKey = (proto.Byte32)(*pk)
	}

	if pk, sk, err := ed25519.GenerateKey(rand); err != nil {
		return err
	} else {
		secretConfig.KeySigningSecretKey = sk[:]
		publicProfile.KeySigningKey = (proto.Byte32)(*pk)
	}
	return nil
}
Exemplo n.º 4
0
func CheckAuthWith(prt ProfileRatchet) func([]byte, []byte, []byte, *[32]byte) error {
	return func(tag, data, msg []byte, ourAuthPrivate *[32]byte) error {
		var sharedAuthKey [32]byte
		message := new(proto.Message)
		unpadMsg := proto.Unpad(msg)
		err := message.Unmarshal(unpadMsg)
		if err != nil {
			return err
		}

		profile, err := prt(message.Dename, message.DenameLookup)
		if err != nil {
			return err
		}

		chatProfileBytes, err := client.GetProfileField(profile, PROFILE_FIELD_ID)
		if err != nil {
			return err
		}

		chatProfile := new(proto.Profile)
		if err := chatProfile.Unmarshal(chatProfileBytes); err != nil {
			return err
		}

		theirAuthPublic := (*[32]byte)(&chatProfile.MessageAuthKey)

		curve25519.ScalarMult(&sharedAuthKey, ourAuthPrivate, theirAuthPublic)
		h := hmac.New(sha256.New, sharedAuthKey[:])

		h.Write(data)
		if subtle.ConstantTimeCompare(tag, h.Sum(nil)[:len(tag)]) == 0 {
			return errors.New("Authentication failed: failed to reproduce envelope auth tag using the current auth pubkey from dename")
		}
		return nil
	}
}
Exemplo n.º 5
0
func (d *Daemon) sendFirstMessage(msg []byte, theirDename string) error {
	profile, err := d.foreignDenameClient.Lookup(theirDename)
	if err != nil {
		return err
	}
	if profile == nil {
		fmt.Errorf("unkown dename on to line: " + theirDename)
	}
	if err := d.MarshalToFile(d.profilePath(theirDename), profile); err != nil {
		return err
	}

	chatProfileBytes, err := client.GetProfileField(profile, util.PROFILE_FIELD_ID)
	if err != nil {
		return err
	}

	chatProfile := new(proto.Profile)
	if err := chatProfile.Unmarshal(chatProfileBytes); err != nil {
		return err
	}

	addr := chatProfile.ServerAddressTCP
	pkSig := (*[32]byte)(&chatProfile.KeySigningKey)
	port := (int)(chatProfile.ServerPortTCP)
	pkTransport := (*[32]byte)(&chatProfile.ServerTransportPK)
	theirPk := (*[32]byte)(&chatProfile.UserIDAtServer)

	ourSkAuth := (*[32]byte)(&d.MessageAuthSecretKey)

	theirConn, err := d.cc.DialServer(theirDename, addr, port, pkTransport, nil, nil)
	if err != nil {
		return err
	}

	theirInBuf := make([]byte, proto.SERVER_MESSAGE_SIZE)
	theirKey, err := util.GetKey(theirConn, theirInBuf, theirPk, theirDename, pkSig)
	if err != nil {
		theirConn.Close()
		d.cc.PutClose(theirDename)
		return err
	}
	encMsg, ratch, err := util.EncryptAuthFirst(msg, ourSkAuth, theirKey, d.ProfileRatchet)
	if err != nil {
		theirConn.Close()
		d.cc.PutClose(theirDename)
		return err
	}
	if err := StoreRatchet(d, theirDename, ratch); err != nil {
		theirConn.Close()
		d.cc.PutClose(theirDename)
		return err
	}
	err = util.UploadMessageToUser(theirConn, theirInBuf, theirPk, encMsg)
	if err != nil {
		theirConn.Close()
		d.cc.PutClose(theirDename)
		return err
	}
	d.cc.Put(theirDename, theirConn)
	return nil
}