Skip to content

Commit

Permalink
feat(example): coap server dynamic method and better C_R parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
stoprocent committed Jul 23, 2024
1 parent 95bb521 commit 57be127
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
53 changes: 37 additions & 16 deletions example/coap-server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createServer } from 'coap'
import { EDHOC, DefaultEdhocCredentialManager, DefaultEdhocCryptoManager, EdhocMethod, EdhocSuite, X509Credentials } from '../dist/index'
import { randomBytes } from 'crypto'
import { decodeAll } from 'cbor'

const server = createServer({ type: 'udp4' });
const sessions = new Map<string, EDHOC>();
Expand All @@ -20,12 +21,26 @@ credentialManager.addTrustRoot(rootCertificate);

server.on('request', async (req, res) => {
if (req.method === 'POST' && req.url === '/.well-known/edhoc') {
const isTrue = req.payload.subarray(0, 1).equals(Buffer.from([0xf5]));
// Handle Initialization
let isTrue = false;
let method = EdhocMethod.Method0;

try {
const payload = await decodeAll(req.payload);
isTrue = (typeof payload[0] == "boolean") && payload[0] === true;
method = payload[1] as EdhocMethod;
}
catch {
// Do nothing
}

if (isTrue) {
// Responder creates a new session
const connectionID = randomBytes(4);
const responder = new EDHOC(connectionID, EdhocMethod.Method0, [ EdhocSuite.Suite2 ], credentialManager, cryptoManager);
responder.logger = (name, value) => console.log(">>>>\n", name, value.toString('hex'), '\n<<<<');
const responder = new EDHOC(connectionID, method, [ EdhocSuite.Suite2 ], credentialManager, cryptoManager);

responder.logger = (name, value) => console.log(name, value.toString('hex'), `\n`);

// Process message 1
const message1 = req.payload.subarray(1);
console.log("message1", message1.toString('hex'));
Expand All @@ -36,29 +51,35 @@ server.on('request', async (req, res) => {
const message2 = await responder.composeMessage2([{ label: 1245, value: Buffer.from('00112233445566', 'hex') }]);
console.log("message2", message2.toString('hex'));

// Store Sesssion
sessions.set(connectionID.toString('hex'), responder);

// Respond with message 2
res.end(message2);
}
else {
// Process message 3
const connectionID = req.payload.subarray(0, 4);
const responder = sessions.get(connectionID.toString('hex'));
console.log("=");
console.log("=");
console.log(req.payload.toString('hex'))
console.log("=");
console.log("=");
try {
// Decode CBOR
const payload = await decodeAll(req.payload);
const responder = sessions.get(payload[0].toString('hex'));

// Check Session
if (responder === undefined) {
throw new Error('Session does not exist')
}

if (responder) {
const message3 = req.payload.subarray(4);
console.log("message3", message3.toString('hex'));
// Message 3
const message3 = req.payload.slice(payload[0].length + 1);
const ead_3 = await responder.processMessage3(message3);
console.log("ead_3", ead_3);
res.end();
console.log("ead_3", ead_3);
console.log("OSCORE Context", await responder.exportOSCORE());

// Respond
res.end();
}
catch (error) {
res.code = '5.00';
res.end(error);
}
}

Expand Down
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"dependencies": {
"@noble/curves": "^1.4.0",
"bindings": "^1.5.0",
"cbor": "^9.0.2",
"coap": "^1.3.0"
},
"devDependencies": {
Expand Down

0 comments on commit 57be127

Please sign in to comment.