Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

jotnode-register: more work

	modified:   jotnode_register_cli.js
	modified:   jotnode_register_srv.js
	new file:   x509_certtool_t.js
  • Loading branch information...
commit 90e9123802a2b63c03d1fec4c3692360a3800029 1 parent 7dda12a
@jeromeetienne authored
View
97 contrib/jotnode-register/jotnode_register_cli.js
@@ -11,90 +11,16 @@
* communication is done via post url encoded
*/
-/**
- * base64 support
- * - nodejs specific
-*/
-var base64 = {
- encode : function(data){
- return new Buffer(data,"binary").toString("base64");
- },
- decode : function(data){
- return new Buffer(data, "base64").toString("binary");
- }
-}
-
-console.log(base64.decode(base64.encode("bonjour")));
+// require local dependancies
+var base64 = require('../../lib/base64');
+var x509 = require('./x509_certtool_t').create();
-process.exit(-1);
-
-/**
- * bunch of x509 generation on top of certtool from gnutls
-*/
-var x509_certtool = function(){
- //////////////////////////////////////////////////////////////////////////
- // internal //
- //////////////////////////////////////////////////////////////////////////
- var cmdline_run = function(cmdline, success_cb, faillure_cb){
- if(!faillure_cb) faillure_cb = function(error){
- console.log('exec ('+cmdline+') error: ' + error);
- process.exit(-1);
- }
- console.log(cmdline);
- require('child_process').exec(cmdline, function (error, stdout, stderr) {
- if (error !== null) faillure_cb(error)
- else success_cb();
- })
- }
-
- //////////////////////////////////////////////////////////////////////////
- // public functions //
- //////////////////////////////////////////////////////////////////////////
- var priv_generate = function(nbits, fname_priv, success_cb, faillure_cb){
- var cmdline = "certtool --generate-privkey";
- cmdline += " --outder --outfile '"+fname_priv+"'";
- cmdline += " --bits "+nbits;
- cmdline_run(cmdline, success_cb, faillure_cb);
- }
-
- var ssig_generate = function(fname_priv, fname_cert, fname_temp, success_cb, faillure_cb){
- var cmdline = "certtool --generate-self-signed";
- cmdline += " --inder --load-privkey '"+fname_priv+"'";
- cmdline += " --outder --outfile '"+fname_cert+"'";
- cmdline += " --template '"+fname_temp+"'";
- cmdline_run(cmdline, success_cb, faillure_cb);
- }
-
- var creq_generate = function(fname_priv, fname_creq, fname_temp, success_cb, faillure_cb){
- var cmdline = "certtool --generate-request";
- cmdline += " --inder --infile '"+fname_priv+"'";
- cmdline += " --outder --outfile '"+fname_creq+"'";
- cmdline += " --template '"+fname_temp+"'";
- cmdline_run(cmdline, success_cb, faillure_cb);
- }
-
- var cert_generate = function(fname_capriv, fname_cacert, fname_catemp, fname_creq, fname_cert, success_cb, faillure_cb){
- var cmdline = "certtool --generate-request";
- cmdline += " --inder --load-request '"+fname_creq+"'";
- cmdline += " --outder --outfile '"+fname_cert+"'";
- cmdline += " --load-ca-privkey '"+fname_capriv+"'";
- cmdline += " --load-ca-certificate '"+fname_cacert+"'";
- cmdline += " --template '"+fname_temp+"'";
- cmdline_run(cmdline, success_cb, faillure_cb);
- }
-
- //////////////////////////////////////////////////////////////////////////
- // run initialisation //
- //////////////////////////////////////////////////////////////////////////
- // return the public properties
- return {
- priv_generate : priv_generate,
- ssig_generate : ssig_generate,
- creq_generate : creq_generate,
- cert_generate : cert_generate
- }
+if( false ){
+ console.log(base64.decode(base64.encode("bonjour")));
+ process.exit(-1);
}
+
if(false){
var fname_priv = "/tmp/user.priv.der";
var fname_creq = "/tmp/user.creq.der";
@@ -103,7 +29,6 @@ if(false){
var fname_capriv= "/tmp/user.capriv.der";
var fname_cacert= "/tmp/user.cacert.der";
var fname_catemp= "./certtool.template";
- var x509 = x509_certtool();
x509.priv_generate(512, fname_capriv, function(){
x509.ssig_generate(fname_capriv, fname_cacert, fname_catemp, function(){
x509.priv_generate(512, fname_priv, function(){
@@ -118,15 +43,15 @@ if(false){
-if( false ){
+if( true ){
var http = require('http');
- var url = "http://127.0.0.1:8124/?User=bob&GetChallenge=1";
+ var url = "http://127.0.0.1:8124/register?creq="+base64.encode("supertruc");
var parsed_url = require('url').parse(url);
- var pqh_str = parsed_url.pathname;
+ var pqh_str = parsed_url.pathname;
if( parsed_url.query ) pqh_str += "?"+parsed_url.query;
if( parsed_url.hash ) pqh_str += parsed_url.hash;
var client = http.createClient((parsed_url.port||80), parsed_url.hostname);
- var request = client.request('GET', pqh_str, {
+ var request = client.request('POST', pqh_str, {
'host' : parsed_url.host
});
request.end();
View
46 contrib/jotnode-register/jotnode_register_srv.js
@@ -1,6 +1,20 @@
-(new Buffer("xyz","utf8")).toString("base64")
-var http = require('http');
+
+// require system dependancies
+var http = require('http');
+var fs = require('fs');
+// require local dependancies
+var base64 = require('../../lib/base64');
+var x509 = require('./x509_certtool_t').create();
+
+
+var fname_priv = "/tmp/user.priv.der";
+var fname_creq = "/tmp/user.creq.der";
+var fname_cert = "/tmp/user.cert.der";
+var fname_temp = "./certtool.template";
+var fname_capriv= "/tmp/user.capriv.der";
+var fname_cacert= "/tmp/user.cacert.der";
+var fname_catemp= "./certtool.template";
http.createServer(function(req, res) {
console.dir(req);
@@ -10,7 +24,7 @@ http.createServer(function(req, res) {
var url = require('url').parse(req.url);
console.dir(url);
- if( req.url == "/register" && req.method == "POST" ){
+ if( url.pathname == "/register" && req.method == "POST" ){
var query_vars = {};
// parse the url query
var url = require('url').parse(req.url);
@@ -18,7 +32,31 @@ http.createServer(function(req, res) {
var keyval = item.split('=');
query_vars[keyval[0]] = keyval[1];
}));
-
+ console.dir(query_vars);
+ // extract creq_data
+ console.assert(query_vars.creq);
+ if(true){
+ // send the cert_base64 back to client
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.write(base64.encode('prout'));
+ res.end();
+ }
+ if(false){
+ var creq_base64 = query_vars.creq;
+ var creq_data = base64.decode_safe(creq_base64);
+ // write the cert request in a file
+ fs.writeFileSync(fname_creq, creq_data, 'binary');
+ // generate the x509 certificate
+ x509.cert_generate(fname_capriv, fname_cacert, fname_catemp, fname_creq, fname_cert, function(){
+ // read cert file
+ var cert_data = fs.readFileSync(fname_cert);
+ var cert_base64 = base.encode(cert_data);
+ // send the cert_base64 back to client
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.write(cert_base64);
+ res.end();
+ })
+ }
}else{
res.writeHead(404, {'Content-Type': 'text/plain'});
res.write('hello World\n')
View
80 contrib/jotnode-register/x509_certtool_t.js
@@ -0,0 +1,80 @@
+/**
+ * bunch of x509 generation on top of certtool from gnutls
+*/
+var x509_certtool_t = function(){
+ //////////////////////////////////////////////////////////////////////////
+ // internal //
+ //////////////////////////////////////////////////////////////////////////
+ var cmdline_run = function(cmdline, success_cb, faillure_cb){
+ if(!faillure_cb) faillure_cb = function(error){
+ console.log('exec ('+cmdline+') error: ' + error);
+ process.exit(-1);
+ }
+ console.log(cmdline);
+ require('child_process').exec(cmdline, function (error, stdout, stderr) {
+ if (error !== null) faillure_cb(error)
+ else success_cb();
+ })
+ }
+
+ //////////////////////////////////////////////////////////////////////////
+ // public functions //
+ //////////////////////////////////////////////////////////////////////////
+ var priv_generate = function(nbits, fname_priv, success_cb, faillure_cb){
+ var cmdline = "certtool --generate-privkey";
+ cmdline += " --outder --outfile '"+fname_priv+"'";
+ cmdline += " --bits "+nbits;
+ cmdline_run(cmdline, success_cb, faillure_cb);
+ }
+
+ var ssig_generate = function(fname_priv, fname_cert, fname_temp, success_cb, faillure_cb){
+ var cmdline = "certtool --generate-self-signed";
+ cmdline += " --inder --load-privkey '"+fname_priv+"'";
+ cmdline += " --outder --outfile '"+fname_cert+"'";
+ cmdline += " --template '"+fname_temp+"'";
+ cmdline_run(cmdline, success_cb, faillure_cb);
+ }
+
+ var creq_generate = function(fname_priv, fname_creq, fname_temp, success_cb, faillure_cb){
+ var cmdline = "certtool --generate-request";
+ cmdline += " --inder --infile '"+fname_priv+"'";
+ cmdline += " --outder --outfile '"+fname_creq+"'";
+ cmdline += " --template '"+fname_temp+"'";
+ cmdline_run(cmdline, success_cb, faillure_cb);
+ }
+
+ var cert_generate = function(fname_capriv, fname_cacert, fname_catemp, fname_creq, fname_cert, success_cb, faillure_cb){
+ var cmdline = "certtool --generate-request";
+ cmdline += " --inder --load-request '"+fname_creq+"'";
+ cmdline += " --outder --outfile '"+fname_cert+"'";
+ cmdline += " --load-ca-privkey '"+fname_capriv+"'";
+ cmdline += " --load-ca-certificate '"+fname_cacert+"'";
+ cmdline += " --template '"+fname_temp+"'";
+ cmdline_run(cmdline, success_cb, faillure_cb);
+ }
+
+ //////////////////////////////////////////////////////////////////////////
+ // run initialisation //
+ //////////////////////////////////////////////////////////////////////////
+ // return the public properties
+ return {
+ priv_generate : priv_generate,
+ ssig_generate : ssig_generate,
+ creq_generate : creq_generate,
+ cert_generate : cert_generate
+ }
+}
+
+/**
+ * Class method to create an object
+ * - thus avoid new operator
+*/
+x509_certtool_t.create = function(){
+ return new x509_certtool_t();
+}
+
+// export it via commonjs
+exports.create = x509_certtool_t.create;
+
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.