2016-04-27 13:31:54 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2016-04-27 13:31:54 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var fs = require('fs');
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
function TLSConfig(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.valid = true;
|
2017-02-27 22:04:19 +01:00
|
|
|
this.verifyservercert = n.verifyservercert;
|
2016-04-27 13:31:54 +02:00
|
|
|
var certPath = n.cert.trim();
|
|
|
|
var keyPath = n.key.trim();
|
|
|
|
var caPath = n.ca.trim();
|
|
|
|
|
2017-02-27 22:04:19 +01:00
|
|
|
if ((certPath.length > 0) || (keyPath.length > 0)) {
|
2016-04-27 13:31:54 +02:00
|
|
|
|
2017-02-27 22:04:19 +01:00
|
|
|
if ( (certPath.length > 0) !== (keyPath.length > 0)) {
|
|
|
|
this.valid = false;
|
|
|
|
this.error(RED._("tls.error.missing-file"));
|
|
|
|
return;
|
2016-04-27 13:31:54 +02:00
|
|
|
}
|
2017-02-27 22:04:19 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (certPath) {
|
|
|
|
this.cert = fs.readFileSync(certPath);
|
|
|
|
}
|
|
|
|
if (keyPath) {
|
|
|
|
this.key = fs.readFileSync(keyPath);
|
|
|
|
}
|
|
|
|
if (caPath) {
|
|
|
|
this.ca = fs.readFileSync(caPath);
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
this.valid = false;
|
|
|
|
this.error(err.toString());
|
|
|
|
return;
|
2016-04-27 13:31:54 +02:00
|
|
|
}
|
2017-02-27 22:04:19 +01:00
|
|
|
} else {
|
|
|
|
if (this.credentials) {
|
|
|
|
var certData = this.credentials.certdata || "";
|
|
|
|
var keyData = this.credentials.keydata || "";
|
|
|
|
var caData = this.credentials.cadata || "";
|
2017-03-09 21:00:54 +01:00
|
|
|
|
2017-02-27 22:04:19 +01:00
|
|
|
if ((certData.length > 0) !== (keyData.length > 0)) {
|
|
|
|
this.valid = false;
|
|
|
|
this.error(RED._("tls.error.missing-file"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (certData) {
|
|
|
|
this.cert = certData;
|
|
|
|
}
|
|
|
|
if (keyData) {
|
|
|
|
this.key = keyData;
|
|
|
|
}
|
|
|
|
if (caData) {
|
|
|
|
this.ca = caData;
|
|
|
|
}
|
2016-04-27 13:31:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-27 22:04:19 +01:00
|
|
|
RED.nodes.registerType("tls-config", TLSConfig, {
|
|
|
|
credentials: {
|
|
|
|
certdata: {type:"text"},
|
|
|
|
keydata: {type:"text"},
|
2018-01-13 21:27:25 +01:00
|
|
|
cadata: {type:"text"},
|
|
|
|
passphrase: {type:"password"}
|
2017-03-09 21:00:54 +01:00
|
|
|
},
|
2017-03-09 20:58:34 +01:00
|
|
|
settings: {
|
|
|
|
tlsConfigDisableLocalFiles: {
|
2017-03-09 21:01:44 +01:00
|
|
|
value: false,
|
2017-03-09 22:06:49 +01:00
|
|
|
exportable: true
|
2017-03-09 20:58:34 +01:00
|
|
|
}
|
2017-02-27 22:04:19 +01:00
|
|
|
}
|
|
|
|
});
|
2016-04-27 13:31:54 +02:00
|
|
|
|
|
|
|
TLSConfig.prototype.addTLSOptions = function(opts) {
|
|
|
|
if (this.valid) {
|
|
|
|
if (this.key) {
|
|
|
|
opts.key = this.key;
|
|
|
|
}
|
|
|
|
if (this.cert) {
|
|
|
|
opts.cert = this.cert;
|
|
|
|
}
|
|
|
|
if (this.ca) {
|
|
|
|
opts.ca = this.ca;
|
|
|
|
}
|
2018-01-13 21:27:25 +01:00
|
|
|
if (this.credentials && this.credentials.passphrase) {
|
|
|
|
opts.passphrase = this.credentials.passphrase;
|
|
|
|
}
|
2016-04-27 13:31:54 +02:00
|
|
|
opts.rejectUnauthorized = this.verifyservercert;
|
|
|
|
}
|
|
|
|
return opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|