예제 #1
0
파일: dbAdmin.go 프로젝트: msecret/emp
func GetByRecipient(recipient string) []objects.MetaMessage {
	localMutex.Lock()
	defer localMutex.Unlock()

	ret := make([]objects.MetaMessage, 0, 0)

	for s, err := LocalDB.Query("SELECT txid_hash, timestamp, purged, sender, recipient FROM msg WHERE recipient=?", recipient); err == nil; err = s.Next() {
		mm := new(objects.MetaMessage)
		sendBytes := make([]byte, 0, 0)
		recvBytes := make([]byte, 0, 0)
		txidHash := make([]byte, 0, 0)
		var timestamp int64

		s.Scan(&txidHash, &timestamp, &mm.Purged, &sendBytes, &recvBytes)
		mm.Sender = encryption.AddressToString(sendBytes)
		mm.Recipient = encryption.AddressToString(recvBytes)

		mm.TxidHash.FromBytes(txidHash)
		mm.Timestamp = time.Unix(timestamp, 0)

		ret = append(ret, *mm)
	}

	return ret
}
예제 #2
0
파일: dbAdmin.go 프로젝트: msecret/emp
func GetMessageDetail(txidHash objects.Hash) (*objects.FullMessage, error) {
	localMutex.Lock()
	defer localMutex.Unlock()

	if Contains(txidHash) > SENDBOX {
		return nil, errors.New("Message not found!")
	}

	ret := new(objects.FullMessage)
	ret.Encrypted = new(encryption.EncryptedMessage)
	ret.Decrypted = new(objects.DecryptedMessage)

	s, err := LocalDB.Query("SELECT * FROM msg WHERE txid_hash=?", txidHash.GetBytes())
	if err == nil {
		recipient := make([]byte, 0, 0)
		sender := make([]byte, 0, 0)
		encrypted := make([]byte, 0, 0)
		decrypted := make([]byte, 0, 0)
		txidHash := make([]byte, 0, 0)
		var timestamp int64
		var purged bool
		var box int

		s.Scan(&txidHash, &recipient, &timestamp, &box, &encrypted, &decrypted, &purged, &sender)
		ret.MetaMessage.TxidHash.FromBytes(txidHash)
		ret.MetaMessage.Recipient = encryption.AddressToString(recipient)
		ret.MetaMessage.Sender = encryption.AddressToString(sender)
		ret.MetaMessage.Timestamp = time.Unix(timestamp, 0)
		ret.MetaMessage.Purged = purged
		ret.Encrypted.FromBytes(encrypted)
		if len(decrypted) > 0 {
			ret.Decrypted.FromBytes(decrypted)
		} else {
			ret.Decrypted = nil
		}
		return ret, nil
	}

	return nil, err

}
예제 #3
0
파일: dbAdmin.go 프로젝트: msecret/emp
func ListAddresses(registered bool) [][2]string {
	ret := make([][2]string, 0, 0)

	for s, err := LocalDB.Query("SELECT address, label FROM addressbook WHERE registered=?", registered); err == nil; err = s.Next() {
		var addr []byte
		var label string
		s.Scan(&addr, &label)
		ret = append(ret, [2]string{encryption.AddressToString(addr), label})
	}

	return ret
}
예제 #4
0
파일: rpcfunc.go 프로젝트: msecret/emp
func (service *EMPService) CreateAddress(r *http.Request, args *NilParam, reply *objects.AddressDetail) error {
	if !basicAuth(service.Config, r) {
		service.Config.Log <- fmt.Sprintf("Unauthorized RPC Request from: %s", r.RemoteAddr)
		return errors.New("Unauthorized")
	}

	// Create Address

	priv, x, y := encryption.CreateKey(service.Config.Log)
	reply.Privkey = priv
	if x == nil {
		return errors.New("Key Pair Generation Error")
	}

	reply.Pubkey = encryption.MarshalPubkey(x, y)

	reply.IsRegistered = true

	reply.Address = encryption.GetAddress(service.Config.Log, x, y)

	if reply.Address == nil {
		return errors.New("Could not create address, function returned nil.")
	}

	reply.String = encryption.AddressToString(reply.Address)

	// Add Address to Database
	err := localdb.AddUpdateAddress(reply)
	if err != nil {
		service.Config.Log <- fmt.Sprintf("Error Adding Address: ", err)
		return err
	}

	// Send Pubkey to Network
	encPub := new(objects.EncryptedPubkey)

	encPub.AddrHash = objects.MakeHash(reply.Address)

	encPub.IV, encPub.Payload, err = encryption.SymmetricEncrypt(reply.Address, string(reply.Pubkey))
	if err != nil {
		service.Config.Log <- fmt.Sprintf("Error Encrypting Pubkey: ", err)
		return nil
	}

	// Record Pubkey for Network
	service.Config.RecvQueue <- *objects.MakeFrame(objects.PUBKEY, objects.BROADCAST, encPub)
	return nil
}
예제 #5
0
파일: dbAdmin.go 프로젝트: msecret/emp
func GetAddressDetail(addrHash objects.Hash) (*objects.AddressDetail, error) {
	localMutex.Lock()
	defer localMutex.Unlock()

	if Contains(addrHash) != ADDRESS {
		return nil, errors.New("Address not found!")
	}

	ret := new(objects.AddressDetail)

	s, err := LocalDB.Query("SELECT address, registered, pubkey, privkey, label, subscribed, encprivkey FROM addressbook WHERE hash=?", addrHash.GetBytes())
	if err == nil {
		s.Scan(&ret.Address, &ret.IsRegistered, &ret.Pubkey, &ret.Privkey, &ret.Label, &ret.IsSubscribed, &ret.EncPrivkey)
		ret.String = encryption.AddressToString(ret.Address)
		return ret, nil
	}

	return nil, err
}
예제 #6
0
파일: rpcmsg.go 프로젝트: msecret/emp
func (service *EMPService) OpenMessage(r *http.Request, args *[]byte, reply *objects.FullMessage) error {
	if !basicAuth(service.Config, r) {
		service.Config.Log <- fmt.Sprintf("Unauthorized RPC Request from: %s", r.RemoteAddr)
		return errors.New("Unauthorized")
	}

	var txidHash objects.Hash
	txidHash.FromBytes(*args)

	// Get Message from Database
	msg, err := localdb.GetMessageDetail(txidHash)
	if err != nil {
		return err
	}

	if msg.Encrypted == nil {
		*reply = *msg
		return nil
	}

	// If not decrypted, decrypt message and purge
	if msg.Decrypted == nil {
		recipient, err := localdb.GetAddressDetail(objects.MakeHash(encryption.StringToAddress(msg.MetaMessage.Recipient)))
		if err != nil {
			return err
		}

		if recipient.Privkey == nil {
			*reply = *msg
			return nil
		}

		// Decrypt Message
		decrypted := encryption.Decrypt(service.Config.Log, recipient.Privkey, msg.Encrypted)
		if len(decrypted) == 0 {
			*reply = *msg
			return nil
		}
		msg.Decrypted = new(objects.DecryptedMessage)
		msg.Decrypted.FromBytes(decrypted)

		// Update Sender

		x, y := encryption.UnmarshalPubkey(msg.Decrypted.Pubkey[:])
		address := encryption.GetAddress(service.Config.Log, x, y)
		addrStr := encryption.AddressToString(address)
		addrHash := objects.MakeHash(address)

		detail, _ := localdb.GetAddressDetail(addrHash)
		if detail == nil {
			detail = new(objects.AddressDetail)
		}
		detail.Address = address
		detail.String = addrStr
		detail.Pubkey = msg.Decrypted.Pubkey[:]

		localdb.AddUpdateAddress(detail)
		msg.MetaMessage.Sender = detail.String

		// Send Purge Request
		purge := new(objects.Purge)
		purge.Txid = msg.Decrypted.Txid

		service.Config.RecvQueue <- *objects.MakeFrame(objects.PURGE, objects.BROADCAST, purge)
		msg.MetaMessage.Purged = true

		localdb.AddUpdateMessage(msg, localdb.Contains(msg.MetaMessage.TxidHash))
	} else {
		if msg.MetaMessage.Purged == false && localdb.Contains(txidHash) == localdb.INBOX {
			msg.MetaMessage.Purged = true
			localdb.AddUpdateMessage(msg, localdb.Contains(msg.MetaMessage.TxidHash))
		}
	}

	*reply = *msg
	return nil
}