All about programming in GNU/LINUX

Cracking the Source: UnderStanding NodeJs Source Code -1

………!!!!…………!!!!!!!!……..!!!!!! Its time for some extreme engineering 😛
Welcome to the series Cracking source ..
This is the first post in the series of posts to come about understanding the source code of Node.JS .  Its been enough time since i’ve been using Node as the webserver for all my projects and its time to understand the source and making an attempt to contribute to its repo ! This blog cannot be considered as a step by step tutorials on understand the source of NodeJs , this is the place where i share my learning , experience and views in the course of journey in understanding the source code of NodeJS.

I stumbled upon this pull request on my mobile screen while i was travelling back home few days ago . The request on the this file called tls.js  from the lib folder of the nodejs source tree . This is how the i started exploring tls.js from the source

The tls module uses OpenSSL (uses the underlying OpenSSL executable installed on your system) to provide Transport Layer Security and/or Secure Socket Layer: encrypted stream communication.

So this post is about tls.js and how it works .

Here is the documentation of the TLS api http://nodejs.org/api/tls.html  . Lets get into details of the API tls.getCiphers()

Here is the code of the API tls.getCiphers() from its source

exports.getCiphers = function() {
  var names = process.binding('crypto').getSSLCiphers();
  // Drop all-caps names in favor of their lowercase aliases,
  var ctx = {};
  names.forEach(function(name) {
    if (/^[0-9A-Z\-]+$/.test(name))
        name = name.toLowerCase();
    ctx[name] = true;
  });
  return Object.getOwnPropertyNames(ctx).sort();
};

process.binding() is a Javascript interface for the C++ core of Node . So process.binding(‘crypto’).getSSLCiphers will result in execution of corresponding function from the c++ core of Node , here is the link to source of getSSLCiphers() from the c++ core https://github.com/joyent/node/blob/master/src/node_crypto.cc. We shall get into the C++ core in the further posts to come .
The C++ core returns an array contains all Cipher names .
Later the array of names are iterated using forEach() method of node . Each names are tested against the Regular Expression /^[0-9A-Z\-] ,Any string that begins/ends and containing digits (0-9) and alphabets in caps (A-Z) and another character ‘-‘ are matched and set as keys in the ctx{} object . And at the end the keys of the object ctx containing the names of the Cipher are returned as an array in sorted order using the following line of code

return Object.getOwnPropertyNames(ctx).sort();

Lets use this API from the tls library , and here is the code

var tls = require('tls');
var cipher = tls.getCiphers();
console.log(cipher);

Lets get into the C++ core in the posts to come in future , but thats it for now .Wait for more exciting posts to come in my journey through source of Node .
Till then happy Coding 😀

Advertisements

3 responses

  1. Good one 🙂 Its interesting to see how underlying core concepts is augmented to make things simple 🙂

    November 18, 2014 at 3:32 am

  2. Pingback: Preparing to land on C++ core of NodeJS: Sharpening the weapons – Using C++ Templates | hackintoshrao

  3. Pingback: Encrypted Client-server communication using Nodejs TLS module and OpenSSL | hackintoshrao

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s