示例#1
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
}
示例#2
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
}