Server now needs client keys

It was the other way around, but this method is more secure! (if slower)
This commit is contained in:
LDDestroier 2019-03-05 12:38:29 -05:00 committed by GitHub
parent 743c96c98e
commit 219e938926
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 140 additions and 105 deletions

View File

@ -197,13 +197,14 @@ local transmit = function(msg, msgID)
}) })
end end
local encTransmit = function(msg, msgID, recipient) local encTransmit = function(msg, msgID, recipient, encID)
modem = getModem(onlyUseWiredModems) modem = getModem(onlyUseWiredModems)
if not keyList[recipient] then local key = keyList[encID or recipient]
if not key then
error("You do not possess the key of the recipient.") error("You do not possess the key of the recipient.")
else else
modem.transmit(config.channel, config.channel, { modem.transmit(config.channel, config.channel, {
msg = aeslua.encrypt(keyList[recipient], textutils.serialize(msg)), msg = aeslua.encrypt(key, textutils.serialize(msg)),
encrypted = true, encrypted = true,
msgID = msgID, msgID = msgID,
recipient = recipient recipient = recipient
@ -211,7 +212,7 @@ local encTransmit = function(msg, msgID, recipient)
end end
end end
local receive = function(msgID, specifyCommand, timer) local receive = function(msgID, specifyCommand, encID, timer)
local evt, msg, tID local evt, msg, tID
if timer then if timer then
tID = os.startTimer(timer) tID = os.startTimer(timer)
@ -222,12 +223,20 @@ local receive = function(msgID, specifyCommand, timer)
if evt[1] == "modem_message" then if evt[1] == "modem_message" then
if type(evt[5]) == "table" then if type(evt[5]) == "table" then
if evt[5].encrypted then if evt[5].encrypted then
if not keyList[yourID] then if true then
--error("keyList[yourID] was nil when decrypting!") if encID then
elseif not evt[5].msg then msg = aeslua.decrypt(keyList[encID], evt[5].msg)
--error("evt[5].msg was nil when decrypting!") else
else for id, key in pairs(keyList) do
msg = textutils.unserialize(aeslua.decrypt(keyList[yourID], evt[5].msg)) if msg then break end
if id ~= encID then
msg = aeslua.decrypt(key, evt[5].msg)
end
end
end
if msg then
msg = textutils.unserialize(msg)
end
end end
else else
msg = evt[5].msg msg = evt[5].msg
@ -260,13 +269,15 @@ local server = {} -- all server-specific commands
---- ---- ---- ----
-- if you want a super duper secure network, manually enter the server ID into this -- if you want a super duper secure network, manually enter the server ID into this
client.findServer = function(recipient) client.findServer = function(srv)
local msgID = math.random(1, 2^30) local msgID = math.random(1, 2^30)
srv = type(srv) == "number" and srv or getNameID(srv)
assert(tonumber(srv) or (not srv), "invalid server")
transmit({ transmit({
id = yourID, id = yourID,
command = "find_server" command = "find_server"
}, msgID) }, msgID)
local reply, isEncrypted = receive(msgID, "find_server_respond", 2) local reply, isEncrypted = receive(msgID, "find_server_respond", srv)
if type(reply) == "table" then if type(reply) == "table" then
if reply.server then if reply.server then
return reply.server return reply.server
@ -277,23 +288,33 @@ end
-- Registers your ID to a name. -- Registers your ID to a name.
client.register = function(srv, username) client.register = function(srv, username)
local msgID = math.random(1, 2^30) local msgID = math.random(1, 2^30)
assert(srv, "register( server, username )")
srv = type(srv) == "number" and srv or getNameID(srv)
assert(srv, "invalid server")
encTransmit({ encTransmit({
id = yourID, id = yourID,
command = "register", command = "register",
name = username name = username
}, msgID, srv) }, msgID, srv, yourID)
local reply, isEncrypted = receive(msgID, "register_respond", 2) local reply, isEncrypted = receive(msgID, "register_respond", yourID)
return reply ~= nil if reply then
return reply.result
else
return false
end
end end
-- Gets a list of all registered ID names -- Gets a list of all registered ID names
client.getNames = function(srv) client.getNames = function(srv)
local msgID = math.random(1, 2^30) local msgID = math.random(1, 2^30)
assert(srv, "getNames( server )")
srv = type(srv) == "number" and srv or getNameID(srv)
assert(srv, "invalid server")
encTransmit({ encTransmit({
id = yourID, id = yourID,
command = "get_names" command = "get_names"
}, msgID, srv) }, msgID, srv, yourID)
local reply, isEncrypted = receive(msgID, "get_names_respond", 2) local reply, isEncrypted = receive(msgID, "get_names_respond", yourID)
if type(reply) == "table" then if type(reply) == "table" then
return reply.names return reply.names
else else
@ -303,11 +324,16 @@ end
-- Sends an email to a recipient ID. -- Sends an email to a recipient ID.
client.sendMail = function(srv, recipient, subject, message, attachments) client.sendMail = function(srv, recipient, subject, message, attachments)
assert(srv, "server ID expected") assert(srv, "sendMail( server, recipient, subject, message, attachments )")
srv = type(srv) == "number" and srv or getNameID(srv)
assert(srv, "invalid server")
assert(type(subject) == "string", "invalid subject")
assert(type(message) == "string", "invalid message")
local msgID = math.random(1, 2^30) local msgID = math.random(1, 2^30)
if type(recipient) == "string" then if type(recipient) == "string" then
recipient = getNameID(recipient) recipient = getNameID(recipient)
end end
assert(recipient, "invalid recipient")
encTransmit({ encTransmit({
command = "send_mail", command = "send_mail",
id = yourID, id = yourID,
@ -315,8 +341,8 @@ client.sendMail = function(srv, recipient, subject, message, attachments)
subject = subject, subject = subject,
message = message, message = message,
attachments = attachments attachments = attachments
}, msgID, srv) }, msgID, srv, yourID)
local reply, isEncrypted = receive(msgID, "send_mail_respond", 2) local reply, isEncrypted = receive(msgID, "send_mail_respond", yourID)
if (isEncrypted and type(reply) == "table") then if (isEncrypted and type(reply) == "table") then
return reply.result return reply.result
else else
@ -326,11 +352,14 @@ end
client.getMail = function(srv) client.getMail = function(srv)
local msgID = math.random(1, 2^30) local msgID = math.random(1, 2^30)
assert(srv, "getMail( server )")
srv = type(srv) == "number" and srv or getNameID(srv)
assert(srv, "invalid server")
encTransmit({ encTransmit({
command = "get_mail", command = "get_mail",
id = yourID, id = yourID,
}, msgID, srv) }, msgID, srv, yourID)
local reply, isEncrypted = receive(msgID, "get_mail_respond", 2) local reply, isEncrypted = receive(msgID, "get_mail_respond", yourID)
if (isEncrypted and type(reply) == "table") then if (isEncrypted and type(reply) == "table") then
return reply.mail return reply.mail
else else
@ -340,12 +369,16 @@ end
client.deleteMail = function(srv, mail) client.deleteMail = function(srv, mail)
local msgID = math.random(1, 2^30) local msgID = math.random(1, 2^30)
assert(srv, "deleteMail( server, mailEntryNumber )")
srv = type(srv) == "number" and srv or getNameID(srv)
assert(srv, "invalid server")
assert(type(mail) == "number", "invalid mail entry")
encTransmit({ encTransmit({
command = "delete_mail", command = "delete_mail",
id = yourID, id = yourID,
mail = mail, mail = mail,
}, msgID, srv) }, msgID, srv, yourID)
local reply, isEncrypted = receive(msgID, "delete_mail_respond", 2) local reply, isEncrypted = receive(msgID, "delete_mail_respond", yourID)
if (isEncrypted and type(reply) == "table") then if (isEncrypted and type(reply) == "table") then
return reply.result return reply.result
else else
@ -470,100 +503,102 @@ server.makeServer = function(verbose)
end end
while true do while true do
names = names
msg, isEncrypted, msgID = receive() msg, isEncrypted, msgID = receive()
if not isEncrypted then if msg then
if msg.command == "find_server" then if not isEncrypted then
transmit({
command = msg.command .. "_respond",
server = yourID,
}, msgID, msg.id)
say("find_server")
end
elseif type(msg.id) == "number" and type(msg.command) == "string" then
if msg.command == "register" then
if (
type(msg.id) == "number" and
type(msg.name) == "string"
) then
local reply
local result, name = server.registerID(msg.id, msg.name)
if result then
reply = {
command = msg.command .. "_respond",
result = result,
name = name,
}
say("user " .. tostring(msg.id) .. " registered as " .. name)
else
reply = {
command = msg.command .. "_respond",
result = result,
}
say("user " .. tostring(msg.id) .. " failed to register as " .. tostring(msg.name) .. ": " .. name)
end
encTransmit(reply, msgID, msg.id)
end
elseif not server.checkRegister(msg.id) then
encTransmit({
command = msg.command .. "_respond",
result = false,
errorMsg = "not registered"
}, msgID, msg.id)
say("unregistered users can burn in hell")
else
-- all the real nice stuff
if msg.command == "find_server" then if msg.command == "find_server" then
encTransmit({ transmit({
command = msg.command .. "_respond", command = msg.command .. "_respond",
server = yourID, server = yourID,
result = true }, msgID, msg.id, yourID)
}, msgID, msg.id) say("find_server")
say("find_server (aes)") end
elseif msg.command == "get_names" then elseif type(msg.id) == "number" and type(msg.command) == "string" then
if msg.command == "register" then
if (
type(msg.id) == "number" and
type(msg.name) == "string"
) then
local reply
local result, name = server.registerID(msg.id, msg.name)
if result then
reply = {
command = msg.command .. "_respond",
result = result,
name = name,
}
say("user " .. tostring(msg.id) .. " registered as " .. name)
else
reply = {
command = msg.command .. "_respond",
result = result,
}
say("user " .. tostring(msg.id) .. " failed to register as " .. tostring(msg.name) .. ": " .. name)
end
encTransmit(reply, msgID, msg.id, msg.id)
end
elseif not server.checkRegister(msg.id) then
encTransmit({ encTransmit({
command = msg.command .. "_respond", command = msg.command .. "_respond",
result = true, result = false,
names = names errorMsg = "not registered"
}, msgID, msg.id) }, msgID, msg.id, msg.id)
say("get_names", msg.id) say("unregistered user attempt to use")
elseif msg.command == "send_mail" then else
if (
msg.recipient and -- all the real nice stuff
type(msg.subject) == "string" and
type(msg.message) == "string" if msg.command == "find_server" then
) then encTransmit({
command = msg.command .. "_respond",
server = yourID,
result = true
}, msgID, msg.id, msg.id)
say("find_server (aes)")
elseif msg.command == "get_names" then
encTransmit({
command = msg.command .. "_respond",
result = true,
}, msgID, msg.id, msg.id)
say("get_names", msg.id)
elseif msg.command == "send_mail" then
if (
msg.recipient and
type(msg.subject) == "string" and
type(msg.message) == "string"
) then
local reply = {
command = msg.command .. "_respond",
result = server.recordMail(msg.id, msg.recipient, msg.subject, msg.message, msg.attachments)
}
encTransmit(reply, msgID, msg.id, msg.id)
say("send_mail", msg.id)
end
elseif msg.command == "get_mail" then
local mail = server.getMail(msg.id)
local reply = { local reply = {
command = msg.command .. "_respond", command = msg.command .. "_respond",
result = server.recordMail(msg.id, msg.recipient, msg.subject, msg.message, msg.attachments) result = true,
mail = mail,
} }
encTransmit(reply, msgID, msg.id) encTransmit(reply, msgID, msg.id, msg.id)
say("send_mail", msg.id) say("get_mail", msg.id)
elseif msg.command == "delete_mail" then
local result = false
if type(msg.mail) == "number" then
result = server.deleteMail(msg.id, msg.mail, yourID)
end
encTransmit({
command = msg.command .. "_respond",
result = result,
}, msgID, msg.id, msg.id)
say("delete_mail", msg.id)
end end
elseif msg.command == "get_mail" then
local mail = server.getMail(msg.id)
local reply = {
command = msg.command .. "_respond",
result = true,
mail = mail,
}
encTransmit(reply, msgID, msg.id)
say("get_mail", msg.id)
elseif msg.command == "delete_mail" then
local result = false
if type(msg.mail) == "number" then
result = server.deleteMail(msg.id, msg.mail)
end
encTransmit({
command = msg.command .. "_respond",
result = result,
}, msgID, msg.id)
say("delete_mail", msg.id)
end
end
end end
end end