Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sensor.live-things-registry

Package Overview
Dependencies
Maintainers
2
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sensor.live-things-registry - npm Package Compare versions

Comparing version 1.0.3 to 1.0.4

15

example/index.js

@@ -1,3 +0,3 @@

const aws_iot = require('aws-iot-device-sdk');
const thing_registry = require('sensor.live-things-registry');
const aws_iot = require('aws-iot-device-sdk')
const ThingRegistry = require('sensor.live-things-registry')
const config = {

@@ -10,8 +10,9 @@ aws_iot: {

}
let thing_registry.setCertsPath('./certs'); // you can change the default certificates folder
const thing_registry = new ThingRegistry()
let thing_registry.setCertsPath('./certs') // you can change the default certificates folder
if (!thing_registry.hasDeviceCertificate()) {
thing_registry.generateDeviceCertificate();
thing_registry.generateDeviceCertificate()
}
let thing_name = thing_registry.getThingName();
let keys_path = thing_registry.getKeysPath();
let thing_name = thing_registry.getThingName()
let keys_path = thing_registry.getKeysPath()
let client_id = `device-${thing_name}`

@@ -24,2 +25,2 @@ let thing_shadow = aws_iot.thingShadow({

clientId: client_id
});
})
{
"name": "sensor.live-things-registry",
"version": "1.0.3",
"version": "1.0.4",
"description": "",

@@ -5,0 +5,0 @@ "main": "src/index.js",

@@ -1,3 +0,1 @@

let ThingRegistry = require('./ThingRegistry');
module.exports = new ThingRegistry;
module.exports = require('./ThingRegistry')

@@ -1,16 +0,16 @@

const forge = require('node-forge');
const fs = require('fs');
const forge = require('node-forge')
const fs = require('fs')
class KeyGenerator {
generateDeviceKeyPair() {
let keys = forge.pki.rsa.generateKeyPair(2048);
let keys = forge.pki.rsa.generateKeyPair(2048)
let key_pair = {
private_key: forge.pki.privateKeyToPem(keys.privateKey),
public_key: forge.pki.publicKeyToPem(keys.publicKey),
};
return key_pair;
}
return key_pair
}
generateDeviceCsr(public_key, private_key, thing_name, country_name, state_name, locality_name, organization_name, organization_unit_name) {
let csr = forge.pki.createCertificationRequest();
csr.publicKey = forge.pki.publicKeyFromPem(public_key);
let csr = forge.pki.createCertificationRequest()
csr.publicKey = forge.pki.publicKeyFromPem(public_key)
csr.setSubject([{

@@ -34,3 +34,3 @@ name: 'commonName',

value: organization_unit_name
}]);
}])
// add optional attributes

@@ -40,21 +40,21 @@ csr.setAttributes([{

value: 'SoftChef'
}]);
}])
// sign certification request
csr.sign(forge.pki.privateKeyFromPem(private_key), forge.md.sha256.create());
csr.sign(forge.pki.privateKeyFromPem(private_key), forge.md.sha256.create())
// PEM-format keys and csr
return forge.pki.certificationRequestToPem(csr);
return forge.pki.certificationRequestToPem(csr)
}
generateDeviceCertificate(ca_certificate_path, ca_key_path, device_csr_pem) {
let ca_certificate_pem = fs.readFileSync(ca_certificate_path, 'utf8');
let ca_key_pem = fs.readFileSync(ca_key_path, 'utf8');
let ca_certificate = forge.pki.certificateFromPem(ca_certificate_pem);
let ca_key = forge.pki.privateKeyFromPem(ca_key_pem);
let device_csr = forge.pki.certificationRequestFromPem(device_csr_pem);
let certificate = forge.pki.createCertificate();
certificate.validity.notBefore = new Date();
certificate.validity.notAfter = new Date();
certificate.validity.notAfter.setFullYear(certificate.validity.notBefore.getFullYear() + 25);
certificate.setSubject(device_csr.subject.attributes);
certificate.setIssuer(ca_certificate.subject.attributes);
certificate.publicKey = device_csr.publicKey;
let ca_certificate_pem = fs.readFileSync(ca_certificate_path, 'utf8')
let ca_key_pem = fs.readFileSync(ca_key_path, 'utf8')
let ca_certificate = forge.pki.certificateFromPem(ca_certificate_pem)
let ca_key = forge.pki.privateKeyFromPem(ca_key_pem)
let device_csr = forge.pki.certificationRequestFromPem(device_csr_pem)
let certificate = forge.pki.createCertificate()
certificate.validity.notBefore = new Date()
certificate.validity.notAfter = new Date()
certificate.validity.notAfter.setFullYear(certificate.validity.notBefore.getFullYear() + 25)
certificate.setSubject(device_csr.subject.attributes)
certificate.setIssuer(ca_certificate.subject.attributes)
certificate.publicKey = device_csr.publicKey
certificate.setExtensions([{

@@ -68,14 +68,14 @@ name: 'basicConstraints',

keyIdentifier: true
}]);
certificate.sign(ca_key, forge.md.sha256.create());
return forge.pki.certificateToPem(certificate) + forge.pki.certificateToPem(ca_certificate);
}])
certificate.sign(ca_key, forge.md.sha256.create())
return forge.pki.certificateToPem(certificate) + forge.pki.certificateToPem(ca_certificate)
}
getCommonName(device_certificate_pem) {
let device_certificate = forge.pki.certificateFromPem(device_certificate_pem);
let field = device_certificate.subject.getField('CN') || {};
let common_name = field.value || null;
let device_certificate = forge.pki.certificateFromPem(device_certificate_pem)
let field = device_certificate.subject.getField('CN') || {}
let common_name = field.value || null
if (common_name && common_name !== 'sensor.live') {
return common_name;
return common_name
} else {
return null;
return null
}

@@ -85,2 +85,2 @@ }

module.exports = new KeyGenerator;
module.exports = new KeyGenerator

@@ -1,39 +0,39 @@

const fs = require('fs');
const Buffer = require('buffer').Buffer;
const key_generator = require('./KeyGenerator');
const fs = require('fs')
const Buffer = require('buffer').Buffer
const key_generator = require('./KeyGenerator')
class ThingRegistry {
constructor() {
this.certs_path = './certs';
this.root_ca_certificate = 'root_ca.cert.pem';
this.ca_certificate = 'ca.cert.pem';
this.ca_private_key = 'ca.private_key.pem';
this.device_csr = 'device.csr';
this.device_certificate = 'device.cert.pem';
this.device_public_key = 'device.public_key.pem';
this.device_private_key = 'device.private_key.pem';
this.certs_path = './certs'
this.root_ca_certificate = 'root_ca.cert.pem'
this.ca_certificate = 'ca.cert.pem'
this.ca_private_key = 'ca.private_key.pem'
this.device_csr = 'device.csr'
this.device_certificate = 'device.cert.pem'
this.device_public_key = 'device.public_key.pem'
this.device_private_key = 'device.private_key.pem'
}
setCertsPath(path) {
this.certs_path = path;
this.certs_path = path
}
setCACertificateName(name) {
this.ca_certificate = name;
this.ca_certificate = name
}
setCAPrivateKeyName(name) {
this.ca_private_key = name;
this.ca_private_key = name
}
setRootCACertificateName(name) {
this.root_ca_certificate = name;
this.root_ca_certificate = name
}
setDeviceCsrName(name) {
this.device_csr = name;
this.device_csr = name
}
setDeviceCertificateName(name) {
this.device_certificate = name;
this.device_certificate = name
}
setDevicePublicKeyName(name) {
this.device_public_key = name;
this.device_public_key = name
}
setDevicePrivateKeyName(name) {
this.device_private_key = name;
this.device_private_key = name
}

@@ -43,3 +43,3 @@ checkRootCACertificateFile() {

this.getRootCACertificatePath()
);
)
}

@@ -49,3 +49,3 @@ checkCACertificateFile() {

this.getCACertificatePath()
);
)
}

@@ -55,3 +55,3 @@ checkCAPrivateKeyFile() {

this.getCAPrivateKeyPath()
);
)
}

@@ -61,13 +61,13 @@ hasDeviceCertificate() {

this.getDeviceCertificatePath()
);
)
}
generateDeviceCertificate({ thing_name = null, country_name = 'TW', state_name = 'Taipei', locality_name = 'Nangang', organization_name = 'SoftChef', organization_unit_name = 'IT'}) {
if (!this.checkCACertificateFile() || !this.checkCAPrivateKeyFile()) {
throw `${this.ca_certificate} or ${this.ca_private_key} file not founded.`;
throw `${this.ca_certificate} or ${this.ca_private_key} file not founded.`
}
let ca_certificate_path = `${this.certs_path}/${this.ca_certificate}`
let ca_private_key_path = `${this.certs_path}/${this.ca_private_key}`
let device_public_key = null;
let device_private_key = null;
let device_csr = null;
let device_public_key = null
let device_private_key = null
let device_csr = null
if (fs.existsSync(this.getDevicePublicKeyPath()) && fs.existsSync(this.getDevicePrivateKeyPath())) {

@@ -77,6 +77,6 @@ if (!fs.existsSync(this.getDeviceCsrPath())) {

this.getDevicePublicKeyPath()
);
)
let device_private_key = fs.readFileSync(
this.getDevicePrivateKeyPath()
);
)
device_csr = key_generator.generateDeviceCsr(device_public_key, device_private_key, thing_name, country_name, state_name, locality_name, organization_name, organization_unit_name)

@@ -87,13 +87,13 @@ fs.writeFileSync(this.getDeviceCsrPath(), device_csr)

this.getDeviceCsrPath()
);
)
}
} else {
let key_pair = key_generator.generateDeviceKeyPair();
device_csr = key_generator.generateDeviceCsr(key_pair.public_key, key_pair.private_key, thing_name, country_name, state_name, locality_name, organization_name, organization_unit_name);
fs.writeFileSync(this.getDevicePublicKeyPath(), key_pair.public_key);
fs.writeFileSync(this.getDevicePrivateKeyPath(), key_pair.private_key);
let key_pair = key_generator.generateDeviceKeyPair()
device_csr = key_generator.generateDeviceCsr(key_pair.public_key, key_pair.private_key, thing_name, country_name, state_name, locality_name, organization_name, organization_unit_name)
fs.writeFileSync(this.getDevicePublicKeyPath(), key_pair.public_key)
fs.writeFileSync(this.getDevicePrivateKeyPath(), key_pair.private_key)
fs.writeFileSync(this.getDeviceCsrPath(), device_csr)
}
let device_certificate = key_generator.generateDeviceCertificate(ca_certificate_path, ca_private_key_path, device_csr);
fs.writeFileSync(this.getDeviceCertificatePath(), device_certificate);
let device_certificate = key_generator.generateDeviceCertificate(ca_certificate_path, ca_private_key_path, device_csr)
fs.writeFileSync(this.getDeviceCertificatePath(), device_certificate)
}

@@ -103,13 +103,13 @@ getThingName() {

this.getDeviceCertificatePath()
).toString();
let common_name = key_generator.getCommonName(device_certificate_pem);
).toString()
let common_name = key_generator.getCommonName(device_certificate_pem)
if (common_name) {
return common_name;
return common_name
}
let lines = device_certificate_pem.split('\n');
let lines = device_certificate_pem.split('\n')
let base64_pem = new Buffer(
lines.slice(1, lines.indexOf('-----END CERTIFICATE-----\r') - 1).toString()
, 'base64').toString('hex');
let prefix = '301d0603551d0e04160414';
return base64_pem.substr(base64_pem.indexOf(prefix) + prefix.length, 40);
, 'base64').toString('hex')
let prefix = '301d0603551d0e04160414'
return base64_pem.substr(base64_pem.indexOf(prefix) + prefix.length, 40)
}

@@ -121,30 +121,30 @@ getKeysPath() {

caPath: this.getRootCACertificatePath()
};
}
}
getRootCACertificatePath() {
return `${this.certs_path}/${this.root_ca_certificate}`;
return `${this.certs_path}/${this.root_ca_certificate}`
}
getCACertificatePath() {
return `${this.certs_path}/${this.ca_certificate}`;
return `${this.certs_path}/${this.ca_certificate}`
}
getCAPrivateKeyPath() {
return `${this.certs_path}/${this.ca_private_key}`;
return `${this.certs_path}/${this.ca_private_key}`
}
getDeviceCsrPath() {
return `${this.certs_path}/${this.device_csr}`;
return `${this.certs_path}/${this.device_csr}`
}
getDeviceCertificatePath() {
return `${this.certs_path}/${this.device_certificate}`;
return `${this.certs_path}/${this.device_certificate}`
}
getDeviceCsrPath() {
return `${this.certs_path}/${this.device_csr}`;
return `${this.certs_path}/${this.device_csr}`
}
getDevicePublicKeyPath() {
return `${this.certs_path}/${this.device_public_key}`;
return `${this.certs_path}/${this.device_public_key}`
}
getDevicePrivateKeyPath() {
return `${this.certs_path}/${this.device_private_key}`;
return `${this.certs_path}/${this.device_private_key}`
}
}
module.exports = ThingRegistry;
module.exports = ThingRegistry
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc