API Payload Security: Encryption, Keys & Processing Order
Resumen
Pixel shares Day 23 of a DEV Community series, explaining the fixed order of serialization, compression, and encryption in an ERP API payload pipeline. It distinguishes ApiProtectionLevel from PayloadFormat and covers access control, AES-CBC-HMAC, key sources, and why cryptographic tools are separated from security policy.
DEV Community
Day 23: The API Payload Security Pipeline: Order, Protection Levels and Keys
The previous post, The Division of Labor Between XML, JSON and MessagePack, closed by saying the other half of value comes here. Encoded and Encrypted differ by one number on the API payload, and underneath that number are three stages that run in a fixed order, while on the way back the order is more than the same run in reverse. An ERP system's API has to answer two questions that are independent of each other: what a call's content should be processed into, and who is entitled to decide it may be processed less. The first is a fixed pipeline, the second is one declaration written onto the method. The two are kept apart, and are compared against each other at one stop only, which is access control. This post covers: The order of serialization, compression and encryption, and what breaks when it is swapped That ApiProtectionLevel and PayloadFormat are two different things, and how access control compares them What actually sits under the name aes-cbc-hmac Where keys come from, and why cryptographic primitives and security policy live in separate layers 1. The order of the three stages An Encoded value goes out as a Base64 string. Getting from an object to that string takes three stages, each carried by its own interface and each replaceable as a whole: Stage What it does Current implementation Can be replaced with Serialization Turns the object into bytes messagepack json Compression Makes those bytes shorter gzip none Encryption Turns bytes into bytes carrying an authentication code aes-cbc-hmac none The latter two names are in the deployment's SystemSettings, while the first is declared by the caller on the API payload of every individual call, with no declaration meaning messagepack. That split is what the previous post's three components and two ways of deciding was about. What can be replaced is each stage's implementation, not the order they run in. The encoding side, ApiPayloadTransformer, is two lines, and the source puts the order at the end of each: byte[] bytes = serializer.Serialize(payload, type); // Serialize
return ApiServiceOptions.PayloadCompressor.Compress(bytes); // Compress
Encryption is not in those two lines. It is one further step the layer outside decides on according to the format: Encoded stops here, and Encrypted goes one stage further. Compression comes before encryption because compression does nothing at all to ciphertext. The output of good encryption is statistically indistinguishable from random data, and repetition and skew are exactly what a compression algorithm looks for. Running the case study's order FormSchema through both orders shows it: Order Result Serialize, compress, then encrypt Leaves a little over a quarter after compression, with encryption adding a few dozen bytes on top Serialize, encrypt, then compress Comes out larger after compression than it went in, and the final size is more than three times the first Swapping the order makes no call fail, and no check anywhere says a word. It just means the compression stage stops doing anything from then on. The way back is those three steps reversed: decrypt, decompress, deserialize. But what matters about the reverse direction is not that it runs backwards, it is that each stage only ever sees bytes that got past the one before it. The authentication code is verified before decryption, and decryption comes before decompression, so the gzip decompressor never at any point holds a stretch of data that has not been verified. A decompressor is a parser that accepts arbitrary bytes. Feed it ciphertext with one bit changed and it will start parsing all the same, then throw because it cannot read a structure out of it. This order means it never gets that far. 2. The level declared, and the format actually used The three values of PayloadFormat were covered in that same earlier post. What access control looks at is not that one on its own, it is the result of comparing it with another enum, and that one has four valu...

Powered by Readybot.io