A TLS 1.3 full handshake always sends its messages in the same order. A message out of place means something is wrong.
What to build
Check whether a list of handshake messages is in the right order.
Requirements
- The correct order is: client ClientHello, server ServerHello, server EncryptedExtensions, server Certificate, server CertificateVerify, server Finished, client Finished.
- Each input line is
sender message. - If the list is exactly that sequence, print
valid. - Otherwise print
wrong at line K: expected SENDER MESSAGE, for the first line that differs (counting from 1), ormissing: SENDER MESSAGEif the list stops early. If there are extra lines after a complete handshake, printextra at line K.
Input
One message per line.
Output
One line.
Constraints
1 to 20 lines.
Example
Input:
client ClientHello
server ServerHello
server EncryptedExtensions
server Certificate
server CertificateVerify
server Finished
client Finished
Output:
valid
Why it works
Comparing position by position with the one correct sequence finds the first difference, which is where a broken or tampered handshake goes wrong.
Examples
Example 1 · Input
client ClientHello server ServerHello server EncryptedExtensions server Certificate server CertificateVerify server Finished client Finished
Expected output
valid
Example 2 · Input
client ClientHello server ServerHello server Certificate
Expected output
wrong at line 3: expected server EncryptedExtensions
Hints
Hint 1
Keep the correct sequence in an array of strings like "client ClientHello".
Hint 2
Compare up to the shorter length first, then check which list is longer.
Solution
Show one correct solution (try it yourself first)
const expected = ["client ClientHello", "server ServerHello", "server EncryptedExtensions", "server Certificate", "server CertificateVerify", "server Finished", "client Finished"];
const got = [];
let line = prompt();
while (line !== null) {
if (line.trim()) got.push(line.trim().split(/\s+/).join(" "));
line = prompt();
}
let answer = "valid";
for (let k = 0; k < Math.min(got.length, expected.length); k++) {
if (got[k] !== expected[k]) {
answer = `wrong at line ${k + 1}: expected ${expected[k]}`;
break;
}
}
if (answer === "valid") {
if (got.length < expected.length) answer = `missing: ${expected[got.length]}`;
else if (got.length > expected.length) answer = `extra at line ${expected.length + 1}`;
}
console.log(answer);
▶ Open this solution in the compiler, then press Visualize to watch it run step by step