mirror of
https://github.com/aziascreations/BakaShrot.git
synced 2026-11-23 20:58:46 +01:00
Initial commit
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
; ============================================================================
|
||||
; BakaShrot Cryptography
|
||||
;
|
||||
; Authenticates packets using Ed25519 signatures (via libsodium - this does
|
||||
; NOT reimplement any cryptographic primitive).
|
||||
;
|
||||
; EncodeData signs a buffer with the sending node's own private key.
|
||||
; DecodeData checks that signature against the claimed sender's public key.
|
||||
;
|
||||
; This buys integrity + authenticity, NOT confidentiality: anyone who can see
|
||||
; the packet can still read it, but they cannot forge one or tamper with one
|
||||
; without the signature check failing. That matches BakaShrot's
|
||||
; replication/broadcast model, where a message is meant to be read by every
|
||||
; peer in the group - encrypting it for a single recipient would defeat that.
|
||||
;
|
||||
; Key material:
|
||||
; - Public key: #BakaShrot_Ed25519_PublicKeySize (32) raw bytes.
|
||||
; - Private key: #BakaShrot_Ed25519_PrivateKeySize (64) raw bytes.
|
||||
; (libsodium's Ed25519 secret key already embeds the seed and the public
|
||||
; key, which is why it is twice the size of the public key.)
|
||||
; A node's private key lives in BakaShrot_CoreConfig\PrivateKey (Config.pbi);
|
||||
; the public key it hands out to peers lives in each
|
||||
; BakaShrot_PeerConfig\PublicKey. Both are stored as Base64 text in the ini
|
||||
; file - see EncodeKeyToString()/DecodeKeyFromString() below.
|
||||
;
|
||||
; Setup required to build/run this:
|
||||
; - Windows: the official libsodium release's "libsodium.lib" (import
|
||||
; library) and "libsodium.dll" alongside the executable.
|
||||
; - Linux: libsodium-dev (Debian/Ubuntu) or libsodium-devel (Fedora)
|
||||
; installed, so "-lsodium" resolves at link time and
|
||||
; libsodium.so.* is available at runtime.
|
||||
; libsodium itself is the same standard, audited library on both platforms,
|
||||
; so signatures made on one are verifiable on the other without change.
|
||||
; ============================================================================
|
||||
|
||||
|
||||
#BakaShrot_Ed25519_PublicKeySize = 32
|
||||
#BakaShrot_Ed25519_PrivateKeySize = 64
|
||||
#BakaShrot_Ed25519_SignatureSize = 64
|
||||
|
||||
|
||||
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
|
||||
ImportC "libsodium.lib"
|
||||
CompilerElse
|
||||
ImportC "-lsodium"
|
||||
CompilerEndIf
|
||||
|
||||
; Must be called once (any number of extra calls are harmless) before any
|
||||
; other function below is used. Returns 0 on success, 1 if it was already
|
||||
; initialized, -1 if libsodium failed to self-check (e.g. no RNG available).
|
||||
sodium_init.l()
|
||||
|
||||
; Fills *PublicKey (32 bytes) and *PrivateKey (64 bytes) with a fresh,
|
||||
; cryptographically random Ed25519 keypair.
|
||||
crypto_sign_keypair.l(*PublicKey, *PrivateKey)
|
||||
|
||||
; Writes a #BakaShrot_Ed25519_SignatureSize-byte signature of *Message
|
||||
; (MessageLength bytes) to *Signature, made with *PrivateKey.
|
||||
crypto_sign_detached.l(*Signature, *SignatureLength, *Message, MessageLength.q, *PrivateKey)
|
||||
|
||||
; Returns 0 if *Signature is a valid signature of *Message (MessageLength
|
||||
; bytes) made by the holder of the private key matching *PublicKey;
|
||||
; non-zero otherwise (forged, corrupted, or signed different data).
|
||||
crypto_sign_verify_detached.l(*Signature, *Message, MessageLength.q, *PublicKey)
|
||||
|
||||
EndImport
|
||||
|
||||
|
||||
; Must be called once at startup, before GenerateKeyPair/EncodeData/DecodeData.
|
||||
; Returns #False if libsodium could not initialize - treat that as fatal,
|
||||
; there is no safe fallback to running without it.
|
||||
Procedure.i InitCryptography()
|
||||
ProcedureReturn Bool(sodium_init() >= 0)
|
||||
EndProcedure
|
||||
|
||||
|
||||
; Generates a new Ed25519 keypair for provisioning a node.
|
||||
; *PublicKey must point to #BakaShrot_Ed25519_PublicKeySize bytes,
|
||||
; *PrivateKey to #BakaShrot_Ed25519_PrivateKeySize bytes.
|
||||
Procedure GenerateKeyPair(*PublicKey, *PrivateKey)
|
||||
crypto_sign_keypair(*PublicKey, *PrivateKey)
|
||||
EndProcedure
|
||||
|
||||
|
||||
; Signs *Data (DataLength bytes) with *PrivateKey (this node's own private
|
||||
; key, #BakaShrot_Ed25519_PrivateKeySize bytes).
|
||||
; Writes the signature to *Signature, which must point to
|
||||
; #BakaShrot_Ed25519_SignatureSize bytes. *Data itself is left untouched.
|
||||
Procedure.i EncodeData(*Data, DataLength.i, *PrivateKey, *Signature)
|
||||
Protected SignatureLength.q
|
||||
ProcedureReturn Bool(crypto_sign_detached(*Signature, @SignatureLength, *Data, DataLength, *PrivateKey) = 0)
|
||||
EndProcedure
|
||||
|
||||
|
||||
; Verifies that *Signature (#BakaShrot_Ed25519_SignatureSize bytes) is a
|
||||
; valid signature of *Data (DataLength bytes), made by the holder of the
|
||||
; private key matching *PublicKey (the claimed sender's public key, from
|
||||
; BakaShrot_PeerConfig\PublicKey).
|
||||
; Returns #True only if the signature checks out. On #False, reject the
|
||||
; packet outright - do not process it, do not fall back to trusting it.
|
||||
Procedure.i DecodeData(*Data, DataLength.i, *PublicKey, *Signature)
|
||||
ProcedureReturn Bool(crypto_sign_verify_detached(*Signature, *Data, DataLength, *PublicKey) = 0)
|
||||
EndProcedure
|
||||
|
||||
|
||||
; Encodes KeySize raw bytes at *Key as a Base64 string, for storing a key in
|
||||
; the ini file (PrivateKey / PublicKey fields in Config.pbi).
|
||||
Procedure.s EncodeKeyToString(*Key, KeySize.i)
|
||||
ProcedureReturn Base64Encoder(*Key, KeySize)
|
||||
EndProcedure
|
||||
|
||||
|
||||
; Decodes KeyString back into KeySize raw bytes at *Key.
|
||||
; Returns #False (and leaves *Key untouched) if KeyString is not valid
|
||||
; Base64 or does not decode to exactly KeySize bytes - a malformed or
|
||||
; truncated key in the config file must never be silently accepted as one
|
||||
; that's merely different.
|
||||
Procedure.i DecodeKeyFromString(KeyString.s, *Key, KeySize.i)
|
||||
; PureBasic's own docs warn the output buffer must have some slack over
|
||||
; the exact decoded size or the decoder can overflow it - at least 25%
|
||||
; extra and never under 64 bytes, hence the margin below.
|
||||
Protected *Buffer, BufferSize.i, DecodedSize.i
|
||||
|
||||
BufferSize = (KeySize * 5) / 4 + 64
|
||||
*Buffer = AllocateMemory(BufferSize)
|
||||
DecodedSize = Base64Decoder(KeyString, *Buffer, BufferSize)
|
||||
If DecodedSize = KeySize
|
||||
CopyMemory(*Buffer, *Key, KeySize)
|
||||
EndIf
|
||||
FreeMemory(*Buffer)
|
||||
|
||||
ProcedureReturn Bool(DecodedSize = KeySize)
|
||||
EndProcedure
|
||||
|
||||
|
||||
; Usage example. Only compiled when this file is run directly (F5 in the
|
||||
; IDE), not when it's included as a module by the rest of the project.
|
||||
CompilerIf #PB_Compiler_IsMainFile
|
||||
|
||||
If Not InitCryptography()
|
||||
Debug "Failed to initialize libsodium."
|
||||
Else
|
||||
Define *PublicKey = AllocateMemory(#BakaShrot_Ed25519_PublicKeySize)
|
||||
Define *PrivateKey = AllocateMemory(#BakaShrot_Ed25519_PrivateKeySize)
|
||||
Define *Signature = AllocateMemory(#BakaShrot_Ed25519_SignatureSize)
|
||||
Define Message.s = ~"{\"messageData\": {\"MsgType\": \"hello\"}}"
|
||||
Define MessageLength = StringByteLength(Message, #PB_Ascii)
|
||||
Define *MessageBytes = AllocateMemory(MessageLength)
|
||||
|
||||
PokeS(*MessageBytes, Message, -1, #PB_Ascii)
|
||||
|
||||
GenerateKeyPair(*PublicKey, *PrivateKey)
|
||||
Debug "PublicKey: " + EncodeKeyToString(*PublicKey, #BakaShrot_Ed25519_PublicKeySize)
|
||||
Debug "PrivateKey: " + EncodeKeyToString(*PrivateKey, #BakaShrot_Ed25519_PrivateKeySize)
|
||||
|
||||
If EncodeData(*MessageBytes, MessageLength, *PrivateKey, *Signature)
|
||||
Debug "Signed ok. Signature: " + EncodeKeyToString(*Signature, #BakaShrot_Ed25519_SignatureSize)
|
||||
|
||||
If DecodeData(*MessageBytes, MessageLength, *PublicKey, *Signature)
|
||||
Debug "Verified ok, as expected."
|
||||
Else
|
||||
Debug "FAILED to verify a genuine signature - something is broken."
|
||||
EndIf
|
||||
|
||||
; Tamper with one byte and make sure verification then rejects it.
|
||||
PokeB(*MessageBytes, PeekB(*MessageBytes) ! 1)
|
||||
If DecodeData(*MessageBytes, MessageLength, *PublicKey, *Signature)
|
||||
Debug "FAILED: tampered data was accepted as genuine."
|
||||
Else
|
||||
Debug "Tampered data correctly rejected."
|
||||
EndIf
|
||||
Else
|
||||
Debug "Failed to sign data."
|
||||
EndIf
|
||||
|
||||
FreeMemory(*PublicKey)
|
||||
FreeMemory(*PrivateKey)
|
||||
FreeMemory(*Signature)
|
||||
FreeMemory(*MessageBytes)
|
||||
EndIf
|
||||
|
||||
CompilerEndIf
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
|
||||
|
||||
|
||||
Enumeration BakaShrot_ConnectionStatuses
|
||||
#BakaShrot_ConnectionStatus_New
|
||||
#BakaShrot_ConnectionStatus_Authenticated
|
||||
#BakaShrot_ConnectionStatus_Idk
|
||||
#BakaShrot_ConnectionStatus_Disconnected
|
||||
#BakaShrot_ConnectionStatus_Aborted
|
||||
EndEnumeration
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
|
||||
|
||||
; Represents the root of all BakaShrot packets.
|
||||
; Any packets below this size MUST be thrown out.
|
||||
Structure BakaShrot_PacketBase
|
||||
Magic.l
|
||||
Version.l
|
||||
PacketType.l
|
||||
EndStructure
|
||||
|
||||
; Fixed values every BakaShrot_PacketBase must carry. Checked on decode so a
|
||||
; packet from a different protocol/version is thrown out instead of being
|
||||
; misparsed.
|
||||
#BakaShrot_ProtocolMagic = $42534854 ; "BSHT"
|
||||
#BakaShrot_ProtocolVersion = 1
|
||||
|
||||
Enumeration BakaShrot_PacketTypes
|
||||
; Safety fallback value, should be dropped when encountered
|
||||
#BakaShrot_PacketType_None = 0
|
||||
|
||||
#BakaShrot_PacketType_AuthenticationRequest = 1
|
||||
#BakaShrot_PacketType_AuthenticationChallenge = 2
|
||||
#BakaShrot_PacketType_AuthenticationResult = 3
|
||||
EndEnumeration
|
||||
|
||||
|
||||
@@ -0,0 +1,303 @@
|
||||
|
||||
|
||||
; ============================================================================
|
||||
; BakaShrot Authentication Packets
|
||||
;
|
||||
; The three packets exchanged during the auth handshake:
|
||||
; C2S AuthRequest - client introduces itself and its public key.
|
||||
; S2C AuthChallenge - server sends back something for the client to sign.
|
||||
; C2S AuthResult - client replies with the signed challenge.
|
||||
;
|
||||
; Wire format: BakaShrot_PacketBase (Magic, Version, PacketType - see
|
||||
; Packets.pbi) followed by each struct's own fields, in declaration order.
|
||||
; Every string field is packed as {len}{str}: a 4-byte (Long) UTF-8 byte
|
||||
; length, followed by exactly that many UTF-8 bytes - no null terminator.
|
||||
; ============================================================================
|
||||
|
||||
|
||||
XIncludeFile "../Packets.pbi"
|
||||
|
||||
; C2S auth request
|
||||
Structure BakaShrot_AuthRequest Extends BakaShrot_PacketBase
|
||||
ClientGroupId.l
|
||||
ClientNodeId.l
|
||||
ClientPubKeyB64.s
|
||||
EndStructure
|
||||
|
||||
; S2C Chalenge the client must sign it with their private key
|
||||
Structure BakaShrot_AuthChallenge Extends BakaShrot_PacketBase
|
||||
ClientPubKeyB64.s
|
||||
EndStructure
|
||||
|
||||
; C2S Response with the chalenge signed by their private key
|
||||
Structure BakaShrot_AuthResult Extends BakaShrot_PacketBase
|
||||
ChallengeResponseB64.s
|
||||
EndStructure
|
||||
|
||||
|
||||
DeclareModule BakaShrot_AuthenticationPacket
|
||||
|
||||
; Encodes *Request/*Challenge/*Result into a freshly allocated buffer.
|
||||
; *OutSize receives its size in bytes. Returns 0 (and leaves *OutSize
|
||||
; untouched) if the input is invalid (null) or the buffer could not be
|
||||
; allocated. Caller owns the returned buffer - free it with FreeMemory.
|
||||
Declare.i EncodeRequest(*Request.BakaShrot_AuthRequest, *OutSize.l)
|
||||
Declare.i EncodeChallenge(*Challenge.BakaShrot_AuthChallenge, *OutSize.l)
|
||||
Declare.i EncodeResult(*Result.BakaShrot_AuthResult, *OutSize.l)
|
||||
|
||||
; Decodes *Buffer (BufferSize bytes) into a freshly allocated struct.
|
||||
; Returns 0 if the packet is too small, malformed, truncated, or not of
|
||||
; the expected kind. Caller owns the returned struct - free it with
|
||||
; FreeStructure.
|
||||
Declare.i DecodeRequest(*Buffer, BufferSize.i)
|
||||
Declare.i DecodeChallenge(*Buffer, BufferSize.i)
|
||||
Declare.i DecodeResult(*Buffer, BufferSize.i)
|
||||
|
||||
EndDeclareModule
|
||||
|
||||
|
||||
Module BakaShrot_AuthenticationPacket
|
||||
|
||||
; ------------------------------------------------------------------------
|
||||
; Wire helpers. Every string on the wire is {len}{str}: a 4-byte UTF-8
|
||||
; byte length followed by exactly that many UTF-8 bytes.
|
||||
; ------------------------------------------------------------------------
|
||||
|
||||
; Size, in bytes, Str will take once written as {len}{str}.
|
||||
Procedure.i PackedStringSize(Str.s)
|
||||
ProcedureReturn 4 + StringByteLength(Str, #PB_UTF8)
|
||||
EndProcedure
|
||||
|
||||
; Writes Str at *Buffer + Offset as {len}{str}. *Buffer must have room
|
||||
; for at least PackedStringSize(Str) bytes from Offset onward.
|
||||
Procedure WritePackedString(*Buffer, Offset.i, Str.s)
|
||||
PokeL(*Buffer + Offset, StringByteLength(Str, #PB_UTF8))
|
||||
PokeS(*Buffer + Offset + 4, Str, -1, #PB_UTF8)
|
||||
EndProcedure
|
||||
|
||||
; #True if a {len}{str} value starting at Offset is fully contained
|
||||
; within the first BufferSize bytes of *Buffer. Always check this before
|
||||
; calling ReadPackedString() - it does no bounds checking of its own.
|
||||
Procedure.i PackedStringFits(*Buffer, BufferSize.i, Offset.i)
|
||||
Protected ByteLen.l
|
||||
|
||||
If Offset + 4 > BufferSize
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
|
||||
ByteLen = PeekL(*Buffer + Offset)
|
||||
If ByteLen < 0 Or Offset + 4 + ByteLen > BufferSize
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
|
||||
ProcedureReturn #True
|
||||
EndProcedure
|
||||
|
||||
; Reads the {len}{str} value at *Buffer + Offset. Caller MUST have
|
||||
; checked PackedStringFits() first.
|
||||
Procedure.s ReadPackedString(*Buffer, Offset.i)
|
||||
ProcedureReturn PeekS(*Buffer + Offset + 4, PeekL(*Buffer + Offset), #PB_UTF8 | #PB_ByteLength)
|
||||
EndProcedure
|
||||
|
||||
|
||||
; ------------------------------------------------------------------------
|
||||
; Header. All three packets share the same BakaShrot_PacketBase layout:
|
||||
; Magic.l, Version.l, PacketType.l.
|
||||
; ------------------------------------------------------------------------
|
||||
|
||||
; Writes the packet header at the start of *Buffer. Returns the offset of
|
||||
; the first byte after the header.
|
||||
Procedure.i WriteHeader(*Buffer, PacketType.l)
|
||||
PokeL(*Buffer, #BakaShrot_ProtocolMagic)
|
||||
PokeL(*Buffer + 4, #BakaShrot_ProtocolVersion)
|
||||
PokeL(*Buffer + 8, PacketType)
|
||||
|
||||
ProcedureReturn SizeOf(BakaShrot_PacketBase)
|
||||
EndProcedure
|
||||
|
||||
; Validates the header at the start of *Buffer: big enough to hold one,
|
||||
; correct Magic/Version, and PacketType matching ExpectedPacketType.
|
||||
; Returns the offset of the first byte after the header on success, or
|
||||
; -1 if the packet must be thrown out.
|
||||
Procedure.i ReadHeader(*Buffer, BufferSize.i, ExpectedPacketType.l)
|
||||
If BufferSize < SizeOf(BakaShrot_PacketBase)
|
||||
ProcedureReturn -1
|
||||
EndIf
|
||||
|
||||
If PeekL(*Buffer) <> #BakaShrot_ProtocolMagic
|
||||
ProcedureReturn -1
|
||||
EndIf
|
||||
|
||||
If PeekL(*Buffer + 4) <> #BakaShrot_ProtocolVersion
|
||||
ProcedureReturn -1
|
||||
EndIf
|
||||
|
||||
If PeekL(*Buffer + 8) <> ExpectedPacketType
|
||||
ProcedureReturn -1
|
||||
EndIf
|
||||
|
||||
ProcedureReturn SizeOf(BakaShrot_PacketBase)
|
||||
EndProcedure
|
||||
|
||||
|
||||
; ------------------------------------------------------------------------
|
||||
; AuthRequest (C2S)
|
||||
; ------------------------------------------------------------------------
|
||||
|
||||
Procedure.i EncodeRequest(*Request.BakaShrot_AuthRequest, *OutSize.l)
|
||||
Protected Size.i, *Buffer, Offset.i
|
||||
|
||||
If Not *Request
|
||||
ProcedureReturn 0
|
||||
EndIf
|
||||
|
||||
Size = SizeOf(BakaShrot_PacketBase) + 4 + 4 + PackedStringSize(*Request\ClientPubKeyB64)
|
||||
|
||||
*Buffer = AllocateMemory(Size)
|
||||
If Not *Buffer
|
||||
ProcedureReturn 0
|
||||
EndIf
|
||||
|
||||
Offset = WriteHeader(*Buffer, #BakaShrot_PacketType_AuthenticationRequest)
|
||||
|
||||
PokeL(*Buffer + Offset, *Request\ClientGroupId)
|
||||
Offset = Offset + 4
|
||||
PokeL(*Buffer + Offset, *Request\ClientNodeId)
|
||||
Offset = Offset + 4
|
||||
|
||||
WritePackedString(*Buffer, Offset, *Request\ClientPubKeyB64)
|
||||
|
||||
*OutSize\l = Size
|
||||
ProcedureReturn *Buffer
|
||||
EndProcedure
|
||||
|
||||
Procedure.i DecodeRequest(*Buffer, BufferSize.i)
|
||||
Protected Offset.i, *Request.BakaShrot_AuthRequest
|
||||
|
||||
Offset = ReadHeader(*Buffer, BufferSize, #BakaShrot_PacketType_AuthenticationRequest)
|
||||
If Offset = -1
|
||||
ProcedureReturn 0
|
||||
EndIf
|
||||
|
||||
If Offset + 4 + 4 > BufferSize
|
||||
ProcedureReturn 0
|
||||
EndIf
|
||||
|
||||
*Request = AllocateStructure(BakaShrot_AuthRequest)
|
||||
|
||||
*Request\Magic = #BakaShrot_ProtocolMagic
|
||||
*Request\Version = #BakaShrot_ProtocolVersion
|
||||
*Request\PacketType = #BakaShrot_PacketType_AuthenticationRequest
|
||||
|
||||
*Request\ClientGroupId = PeekL(*Buffer + Offset)
|
||||
Offset = Offset + 4
|
||||
*Request\ClientNodeId = PeekL(*Buffer + Offset)
|
||||
Offset = Offset + 4
|
||||
|
||||
If Not PackedStringFits(*Buffer, BufferSize, Offset)
|
||||
FreeStructure(*Request)
|
||||
ProcedureReturn 0
|
||||
EndIf
|
||||
*Request\ClientPubKeyB64 = ReadPackedString(*Buffer, Offset)
|
||||
|
||||
ProcedureReturn *Request
|
||||
EndProcedure
|
||||
|
||||
|
||||
; ------------------------------------------------------------------------
|
||||
; AuthChallenge (S2C)
|
||||
; ------------------------------------------------------------------------
|
||||
|
||||
Procedure.i EncodeChallenge(*Challenge.BakaShrot_AuthChallenge, *OutSize.l)
|
||||
Protected Size.i, *Buffer, Offset.i
|
||||
|
||||
If Not *Challenge
|
||||
ProcedureReturn 0
|
||||
EndIf
|
||||
|
||||
Size = SizeOf(BakaShrot_PacketBase) + PackedStringSize(*Challenge\ClientPubKeyB64)
|
||||
|
||||
*Buffer = AllocateMemory(Size)
|
||||
If Not *Buffer
|
||||
ProcedureReturn 0
|
||||
EndIf
|
||||
|
||||
Offset = WriteHeader(*Buffer, #BakaShrot_PacketType_AuthenticationChallenge)
|
||||
WritePackedString(*Buffer, Offset, *Challenge\ClientPubKeyB64)
|
||||
|
||||
*OutSize\l = Size
|
||||
ProcedureReturn *Buffer
|
||||
EndProcedure
|
||||
|
||||
Procedure.i DecodeChallenge(*Buffer, BufferSize.i)
|
||||
Protected Offset.i, *Challenge.BakaShrot_AuthChallenge
|
||||
|
||||
Offset = ReadHeader(*Buffer, BufferSize, #BakaShrot_PacketType_AuthenticationChallenge)
|
||||
If Offset = -1
|
||||
ProcedureReturn 0
|
||||
EndIf
|
||||
|
||||
If Not PackedStringFits(*Buffer, BufferSize, Offset)
|
||||
ProcedureReturn 0
|
||||
EndIf
|
||||
|
||||
*Challenge = AllocateStructure(BakaShrot_AuthChallenge)
|
||||
|
||||
*Challenge\Magic = #BakaShrot_ProtocolMagic
|
||||
*Challenge\Version = #BakaShrot_ProtocolVersion
|
||||
*Challenge\PacketType = #BakaShrot_PacketType_AuthenticationChallenge
|
||||
*Challenge\ClientPubKeyB64 = ReadPackedString(*Buffer, Offset)
|
||||
|
||||
ProcedureReturn *Challenge
|
||||
EndProcedure
|
||||
|
||||
|
||||
; ------------------------------------------------------------------------
|
||||
; AuthResult (C2S)
|
||||
; ------------------------------------------------------------------------
|
||||
|
||||
Procedure.i EncodeResult(*Result.BakaShrot_AuthResult, *OutSize.l)
|
||||
Protected Size.i, *Buffer, Offset.i
|
||||
|
||||
If Not *Result
|
||||
ProcedureReturn 0
|
||||
EndIf
|
||||
|
||||
Size = SizeOf(BakaShrot_PacketBase) + PackedStringSize(*Result\ChallengeResponseB64)
|
||||
|
||||
*Buffer = AllocateMemory(Size)
|
||||
If Not *Buffer
|
||||
ProcedureReturn 0
|
||||
EndIf
|
||||
|
||||
Offset = WriteHeader(*Buffer, #BakaShrot_PacketType_AuthenticationResult)
|
||||
WritePackedString(*Buffer, Offset, *Result\ChallengeResponseB64)
|
||||
|
||||
*OutSize\l = Size
|
||||
ProcedureReturn *Buffer
|
||||
EndProcedure
|
||||
|
||||
Procedure.i DecodeResult(*Buffer, BufferSize.i)
|
||||
Protected Offset.i, *Result.BakaShrot_AuthResult
|
||||
|
||||
Offset = ReadHeader(*Buffer, BufferSize, #BakaShrot_PacketType_AuthenticationResult)
|
||||
If Offset = -1
|
||||
ProcedureReturn 0
|
||||
EndIf
|
||||
|
||||
If Not PackedStringFits(*Buffer, BufferSize, Offset)
|
||||
ProcedureReturn 0
|
||||
EndIf
|
||||
|
||||
*Result = AllocateStructure(BakaShrot_AuthResult)
|
||||
|
||||
*Result\Magic = #BakaShrot_ProtocolMagic
|
||||
*Result\Version = #BakaShrot_ProtocolVersion
|
||||
*Result\PacketType = #BakaShrot_PacketType_AuthenticationResult
|
||||
*Result\ChallengeResponseB64 = ReadPackedString(*Buffer, Offset)
|
||||
|
||||
ProcedureReturn *Result
|
||||
EndProcedure
|
||||
|
||||
EndModule
|
||||
|
||||
Reference in New Issue
Block a user