Skip to content

Commit

Permalink
Webinos PZP moved to a separate repository
Browse files Browse the repository at this point in the history
-includes wrt folder to include webinos and webinos.session
-includes new code to create certificates
-webinos_pzp to start pzp
-this can currently start a PZP but not stable enough
  • Loading branch information
habibvirji committed Apr 5, 2013
1 parent 078b21b commit 6f32baf
Show file tree
Hide file tree
Showing 109 changed files with 8,499 additions and 2,544 deletions.
24 changes: 24 additions & 0 deletions config.json
@@ -0,0 +1,24 @@
{
"ports": {
"provider": 80,
"provider_webServer": 443,
"pzp_webSocket": 8080,
"pzp_tlsServer": 8040,
"pzp_zeroConf": 4321,
"iot": 3000
},
"pzpDefaultServices": [
{
"name": "test",
"params": {
"num": "21"
}
}
],
"friendlyName": "",
"webinos_version": {
"tag": "v0.8.0",
"num_commit": "134",
"commit_id": "gd62f210"
}
}
File renamed without changes.
44 changes: 44 additions & 0 deletions lib/pzp.js
@@ -0,0 +1,44 @@
var mandatoryModule = ["webinos-utilities",
"webinos-certificateHandler",
"webinos-messaging",
"webinos-jsonrpc2",
"webinos-api-serviceDiscovery",
"webinos-api-test",
"webinos-messaging"
];
var optionalModule = ["webinos-synchronization",
"webinos-policy"
];
mandatoryModule.forEach(function(name) {
if (!require.resolve(name)) {
throw new Error("Webinos PZP mandatory module "+ name + " is missing, these modules are compulsory for PZP to run. " +
"Please run npm install "+name+" at command prompt to proceed");
}
});
optionalModule.forEach(function(name) {
if (!require.resolve(name)) {
console.log("Webinos PZP optional module "+ name + " is missing, PZP will run but functionality provided by "+
name + " will be missing");
}
});
module.exports = {
"wUtil" : require("webinos-utilities"),
"wCertificate": require("webinos-certificateHandler"),
"discovery" : require("webinos-api-serviceDiscovery").Service,
"messageHandler": require("webinos-messaging").MessageHandler,
"rpc" : require("webinos-jsonrpc2"),
"session" : require("./pzp_sessionHandling"),
"PzpDiscovery": require ("./pzp_peerDiscovery"),
"PzpSib" : require("./pzp_SIB_auth"),
"certExchange": require("./pzp_peerCertificateExchange.js"),
"os" : require("os"),
"https" : require('https'),
"http" : require ("http"),
"path" : require("path"),
"fs" : require("fs"),
"tls" : require("tls"),
"url" : require("url"),
"WebSocketServer":require ("websocket").server,
"mandatoryModule":mandatoryModule

};
96 changes: 96 additions & 0 deletions lib/pzp_SIB_auth.js
@@ -0,0 +1,96 @@
/*******************************************************************************
* Code contributed to the webinos project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2012 - 2013 Samsung Electronics (UK) Ltd
* AUTHORS: Ziran Sun (ziran.sun@samsung.com)
*******************************************************************************/
function PzpSIBAuth(PzpInstance) {
"use strict";
var PzpCommon = require("./pzp.js");
var logger = PzpCommon.wUtil.webinosLogging(__filename) || console;
/**
* Create QR image using public key Hash
* @param infile. Path for the public key certificate
* @param outfile. Path for the generate QR image file (Android only)
* @param width. Width of QR image to be generated (Android only)
* @param height. Height of QR image to be generated (Android only)
* @param cb. Callback when QR image generated
*/
this.createQRHash = function(infile, outfile, width, height, cb) {
PzpInstance.getKeyHash(infile, function(status, value){
if(status) {
logger.log("get hash: " + value);
if(PzpCommon.os.platform().toLowerCase() == "android") {
try {
var bridge = require('bridge');
var QRencode = bridge.load('org.webinos.impl.QRImpl', this);
QRencode.enCode(value, width, height, outfile, function(outfile){
cb(outfile);
});
}
catch(e) {
logger.error("Android QRencode - error: "+e.message);
}
}
else
{
try {
var QRCode = require("qrcode");
QRCode.toDataURL(value, function(err, value) {
logger.log("created url: " + value);
cb(err, value);
});
} catch (err) {
logger.log("create QR failed: " + err);
}
}
}
else
logger.log("get hash err: " + value);
});
};

/**
* Compare hash from the scanned QR code with Hash stored locally
* @param filepath. path for other party's public key certificate
* @param hash. Locally stored hash code
* @param cb. Callback when comprison is done
*/
this.checkQRHash = function(filename, hash, cb) {
PzpInstance.getKeyHash(filename, function(status, value){
if(status)
{
logger.log("hash passed over is: " + hash);
logger.log("get hash of the other party: " + value)
if(hash == value){
logger.log("correct hash key");
cb(true);
}
else
{
logger.log("Wrong Hash Key");
cb(false);
}
}
else
{
logger.log("get hash err: " + value);
}
});
};
};

module.exports = PzpSIBAuth;

36 changes: 17 additions & 19 deletions webinos/core/pzp/lib/pzp_connectPeer.js → lib/pzp_connectPeer.js
Expand Up @@ -2,68 +2,66 @@
var PzpClient = function () {
"use strict";
var PzpCommon = require("./pzp.js");
var PzpObject = this;
var logger = PzpCommon.wUtil.webinosLogging (__filename) || console;
/**
*
*/
function pzpClient_PeerCleanup() {
var path = require("path");
var fs = require("fs");
var existsSync = fs.existsSync || path.existsSync;
var existsSync = PzpCommon.fs.existsSync || PzpCommon.path.existsSync;
logger.log("Clean up SiB leftovers");
var own = path.join(parent.config.metaData.webinosRoot, "keys", "conn.pem");
var other = path.join(parent.config.metaData.webinosRoot, "keys", "otherconn.pem");
var exlist = path.join(parent.config.metaData.webinosRoot, "exCertList.json");
var own = path.join(PzpObject.getWebinosPath(), "keys", "conn.pem");
var other = path.join(PzpObject.getWebinosPath(), "keys", "otherconn.pem");
var exlist = path.join(PzpObject.getWebinosPath(), "exCertList.json");
if(existsSync(own)) {
fs.unlink(own, function(err){
PzpCommon.fs.unlink(own, function(err){
if(err) throw err;
logger.log("removed" + own);
});
}
if(existsSync(other)) {
fs.unlink(other, function(err){
PzpCommon.fs.unlink(other, function(err){
if(err) throw err;
logger.log("removed" + other);
});
}
if(existsSync(exlist)) {
fs.unlink(exlist, function(err){
PzpCommon.fs.unlink(exlist, function(err){
if(err) throw err;
logger.log("removed - " + exlist);
});
}
parent.pzp_state.connectingPeerAddr = "";
PzpObject.setConnectingPeerAddr("");
}
/**
* Connect Peer PZPs. This is either triggered by PZH sending PZPUpdate message or else from PZP local discovery
* @param msg - msg is an object containing port, address and name of PZP to be connected
*/
this.connectPeer = function (msg) {
parent.setConnectionParameters(function (options) {
PzpObject.setConnectionParameters(function (status, options) {
var name = msg.name, n, client;
if(name && (n = name.indexOf("/"))) options.servername = name.substring(0, n);
client = require("tls").connect(parent.getPorts().pzp_tlsServer, msg.address, options, function () {
client = PzpCommon.tls.connect(PzpObject.getPorts().pzp_tlsServer, msg.address, options, function () {
if (client.authorized) {
parent.handlePeerAuthorization(msg.name, client);
PzpObject.handlePeerAuthorization(msg.name, client);
pzpClient_PeerCleanup();
} else {
logger.error("pzp client - connection failed, " + client.authorizationError);
PzpObject.unAuthentication(client);
}
});

client.on("data", function (buffer) {
parent.handleMsg(client, buffer);
PzpObject.handleMsg(client, buffer);
});

client.on("end", function () {
parent.cleanUp(client.id);
PzpObject.cleanUp(client.id);
});

client.on("error", function (err) {
logger.error(err.message);
PzpObject.handleError(err);
});
});
}
};
};

module.exports = PzpClient;
31 changes: 15 additions & 16 deletions webinos/core/pzp/lib/pzp_connectPzh.js → lib/pzp_connectPzh.js
Expand Up @@ -3,48 +3,47 @@
* Connects with PZH and handle respective events
*/
var PzpOtherManager = require("./pzp_otherManager.js");
var PzpCommon = require("./pzp.js");

var PzpConnectHub = function () {
"use strict";
PzpOtherManager.call(this);
var self = this;
var logger = PzpCommon.wutil.webinosLogging (__filename) || console;
var PzpCommon = require("./pzp.js");
var PzpObject = this;
var logger = PzpCommon.wUtil.webinosLogging(__filename) || console;

/**
* If PZP fails to connect to PZH, this tries to connect back to PZH
*/
function retryConnecting () {
if (self.getEnrolledStatus()) {
if (PzpObject.getEnrolledStatus()) {
setTimeout (function () {
self.connect (function (status) {
logger.log ("retrying to connect back to the PZH " + (status ? "successful" : "failed"));
});
logger.log ("retrying to connect back to the PZH ");
PzpObject.connectHub();
}, 60000);//increase time limit to suggest when it should retry connecting back to the PZH
}
}
/**
*
* @param callback
*/
this.connectHub = function (callback) {
this.connectHub = function () {
var pzpClient;
try {
self.setConnectionParameters(function (certificateConfiguration) {
pzpClient = require("tls").connect(parent.getPorts().provider,
self.getServerAddress(),
PzpObject.setConnectionParameters(function (status, certificateConfiguration) {
pzpClient = PzpCommon.tls.connect(PzpObject.getPorts().provider,
PzpObject.getServerAddress(),
certificateConfiguration, function() {
logger.log ("connection to pzh status: " + pzpClient.authorized);
if (pzpClient.authorized) {
authenticated (pzpClient, callback);
PzpObject.handlePzhAuthentication(pzpClient);
} else {
unauthenticated (pzpClient, callback);
PzpObject.unAuthentication(pzpClient);
}
});
pzpClient.setTimeout(100);

pzpClient.on ("data", function (buffer) {
handleMsg(pzpClient, buffer);
PzpObject.handleMsg(pzpClient, buffer);
});

pzpClient.on ("close", function(had_error) {
Expand All @@ -53,12 +52,12 @@ var PzpConnectHub = function () {
}
});
pzpClient.on ("end", function() {
if (pzpClient.id) cleanUp(pzpClient.id);
if (pzpClient.id) PzpObject.cleanUp(pzpClient.id);
retryConnecting();
});

pzpClient.on ("error", function(err) {
handlePzpError(err);
PzpObject.emit("CONNECTION_FAILED", err);
});
});
} catch (err) {
Expand Down

0 comments on commit 6f32baf

Please sign in to comment.