Ejemplo n.º 1
0
func (service *EMPService) PurgeMessage(r *http.Request, args *[]byte, reply *NilParam) error {
	if len(*args) != 16 {
		return errors.New("Invalid Txid: Bad Length")
	}

	txidHash := objects.MakeHash(*args)

	if localdb.Contains(txidHash) <= localdb.SENDBOX {
		msg, err := localdb.GetMessageDetail(txidHash)
		if err != nil {
			return errors.New(fmt.Sprintf("Problem Retrieving Message: %s", err))
		}
		msg.MetaMessage.Purged = true
		localdb.AddUpdateMessage(msg, -1)

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

		service.Config.RecvQueue <- *objects.MakeFrame(objects.PURGE, objects.BROADCAST, purge)

		return nil
	}

	return errors.New("Txid Not Found")
}
Ejemplo n.º 2
0
func (service *EMPService) GetEncrypted(r *http.Request, args *[]byte, reply *encryption.EncryptedMessage) 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
	}

	*reply = *msg.Encrypted
	return nil
}
Ejemplo n.º 3
0
// Handle Pubkey, Message, and Purge Registration
func register(config *api.ApiConfig) {
	var message objects.Message
	var txid [16]byte

	for {
		select {
		case pubHash := <-config.PubkeyRegister:

			// Check if pubkey is in database...
			pubkey := checkPubkey(config, pubHash)

			if pubkey == nil {
				break
			}

			outbox := localdb.GetBox(localdb.OUTBOX)
			for _, metamsg := range outbox {
				recvHash := objects.MakeHash([]byte(metamsg.Recipient))
				if string(pubHash.GetBytes()) == string(recvHash.GetBytes()) {
					// Send message and move to sendbox
					msg, err := localdb.GetMessageDetail(metamsg.TxidHash)
					if err != nil {
						config.Log <- err.Error()
						break
					}
					msg.Encrypted = encryption.Encrypt(config.Log, pubkey, string(msg.Decrypted.GetBytes()))
					msg.MetaMessage.Timestamp = time.Now().Round(time.Second)
					err = localdb.AddUpdateMessage(msg, localdb.SENDBOX)
					if err != nil {
						config.Log <- err.Error()
						break
					}

					sendMsg := new(objects.Message)
					sendMsg.Timestamp = msg.MetaMessage.Timestamp
					sendMsg.TxidHash = msg.MetaMessage.TxidHash
					sendMsg.AddrHash = recvHash
					sendMsg.Content = *msg.Encrypted

					config.RecvQueue <- *objects.MakeFrame(objects.MSG, objects.BROADCAST, sendMsg)
				}
			}

		case message = <-config.MessageRegister:
			// If address is registered, store message in inbox
			detail, err := localdb.GetAddressDetail(message.AddrHash)
			if err != nil {
				config.Log <- "Message address not in database..."
				break
			}
			if !detail.IsRegistered {
				config.Log <- "Message not for registered address..."
				break
			}

			config.Log <- "Registering new encrypted message..."

			msg := new(objects.FullMessage)
			msg.MetaMessage.TxidHash = message.TxidHash
			msg.MetaMessage.Timestamp = message.Timestamp
			msg.MetaMessage.Recipient = detail.String
			msg.Encrypted = &message.Content

			err = localdb.AddUpdateMessage(msg, localdb.INBOX)
			if err != nil {
				config.Log <- err.Error()
			}
		case message = <-config.PubRegister:
			// If address is registered, store message in inbox
			detail, err := localdb.GetAddressDetail(message.AddrHash)
			if err != nil {
				config.Log <- "Message address not in database..."
				break
			}
			if !detail.IsSubscribed {
				config.Log <- "Not Subscribed to Address..."
				break
			}

			config.Log <- "Registering new publication..."

			msg := new(objects.FullMessage)
			msg.MetaMessage.TxidHash = message.TxidHash
			msg.MetaMessage.Timestamp = message.Timestamp
			msg.MetaMessage.Sender = detail.String
			msg.MetaMessage.Recipient = "<Subscription Message>"
			msg.Encrypted = &message.Content

			msg.Decrypted = new(objects.DecryptedMessage)
			msg.Decrypted.FromBytes(encryption.DecryptPub(config.Log, detail.Pubkey, msg.Encrypted))

			err = localdb.AddUpdateMessage(msg, localdb.INBOX)
			if err != nil {
				config.Log <- err.Error()
			}
		case txid = <-config.PurgeRegister:
			// If Message in database, mark as purged
			detail, err := localdb.GetMessageDetail(objects.MakeHash(txid[:]))
			if err != nil {
				break
			}
			detail.MetaMessage.Purged = true
			err = localdb.AddUpdateMessage(detail, -1)
			if err != nil {
				config.Log <- fmt.Sprintf("Error registering purge: %s", err)
			}
		} // End select
	} // End for
} // End register
Ejemplo n.º 4
0
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
}