Open In App

Node.js tlsSocket.exportKeyingMaterial() Method

The tlsSocket.exportKeyingMaterial() is an inbuilt application programming interface of class TLSSocket within tls module which is used to return the requested bytes of the keying material to prevent different kind of attacks in network protocols.

Syntax:



const tlsSocket.exportKeyingMaterial(length, label[, context])

Parameters: This method takes the following argument as parameter:

Return Value: This method returns the buffer containing the keying material.



How to generate Private key and Public certificate?

Example 1: Filename: index.js




// Node.js program to demonstrate the
// tlsSocket.exportKeyingMaterial() method
var tls = require('tls'),
    fs = require('fs'),
  
// Port and host address for server    
PORT = 1337,
HOST = '127.0.0.1',
value = null;
   
// Private key and public certificate for access
var options = {
  key: fs.readFileSync('private-key.pem'),
  cert: fs.readFileSync('public-cert.pem'),
  rejectUnauthorized: false
};
  
// Creating and initializing server
var server = tls.createServer(options, function(socket) {
  
  // Print the data that we received
  socket.on('data', function(data) {
    console.log('\nReceived: %s ',
    data.toString().replace(/(\n)/gm, ""));
  });
  
  // Stopping the server
  // by using the close() method
  server.close(() => {
     console.log("Server closed successfully");
  });
});
  
// Start listening on a specific port and address
// by using listen() method
server.listen(PORT, HOST, function() {
  console.log("I'm listening at %s, on port %s", HOST, PORT);
});
  
// Creating and initializing client
var client = tls.connect(PORT, HOST, options, function() {
    
  // Getting keying material
  // by using tlsSocket.exportKeyingMaterial() method
  value = client.exportKeyingMaterial(
  128, 'client finished');
  
  client.write("keying material : " + value.toJSON().data);
    
  client.end(() => {
    console.log("Client closed successfully");
  });
});

Run the index.js file using the following command:

node index.js

Output:

I'm listening at 127.0.0.1, on port 1337
Client closed successfully

Received: keying material : 10, 183, 131,....... 52, 121, 159
Server closed successfully

Example 2: Filename: index.js




// Node.js program to demonstrate the
// tlsSocket.exportKeyingMaterial() method
var tls = require('tls'),
    fs = require('fs'),
  
// Port and host address for server    
PORT = 1337,
HOST = '127.0.0.1';
   
// Private key and public certificate for access
var options = {
  key: fs.readFileSync('private-key.pem'),
  cert: fs.readFileSync('public-cert.pem'),
  rejectUnauthorized: false
};
  
// Creating and initializing server
var server = tls.createServer(options, function(socket) {
  
  // Getting keying material
  // by using tlsSocket.exportKeyingMaterial() method
  value = socket.exportKeyingMaterial(
  128, 'server finished');;
  
  socket.write("keying material : " + value.toJSON().data);
  
  // Stopping the server
  // by using the close() method
  server.close(() => {
     console.log("Server closed successfully")
  });
});
  
// Start listening on a specific port and address
// by using listen() method
server.listen(PORT, HOST, function() {
  console.log("I'm listening at %s, on port %s", HOST, PORT);
});
  
// Creating and initializing client
var client = tls.connect(PORT, HOST, options, function() {
   console.log("client is connected");
});
  
client.on("data", function(data) {
  
  console.log('Received: %s',
  data.toString().replace(/(\n)/gm, ""));
  
  // Close the connection after receiving the message
  client.end(() => {
     console.log("client closed successfully")
  });
});

Run the index.js file using the following command:

node index.js

Output:

I'm listening at 127.0.0.1, on port 1337
client is connected
Received: keying material : 239, 29, 74, 173 .... 209, 242, 131
client closed successfully
Server closed successfully

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/tls.html#tls_tlssocket_exportkeyingmaterial_length_label_context


Article Tags :