Node JS TLS server and client aren’t working correctly












0















Recently I’ve been trying to make a basic TLC server and client to maybe put on a Rasperry PI. I found an example on github and i modified it so its up to date. My issue is that Node JS keeps saying unable to verify the first certificate Even though i generated a new one.




note the certificates and keys are self signed.




Here is my code, I have not the slightest clue as to why its failing because when I set ServerOptions As the client options, it just logs Unsecure connection.



Server.js



'use strict';

const tls = require('tls');
const fs = require('fs');
const util = require('util');
const events = require('events');
const port = 4170;

const ServerOptions = {
key: fs.readFileSync('agent1-key.pem'),
cert: fs.readFileSync('agent1-cert.pem'),
ca: fs.readFileSync('root-cert.pem'), // authority chain for the clients
requestCert: true,
rejectUnauthorized: false
};

var server = tls.createServer(ServerOptions, (socket) => {
socket.write('welcome!n');
socket.setEncoding('utf8');
socket.pipe(socket);
})

.on('connection', function(c) {
console.log('Unsecure connection');
})

.on('secureConnect', function (c) {
// c.authorized will be true if the client cert presented validates with our CA
console.log('secure connectionn client authorization Status:', c);
})

.listen(port, function() {
console.log('server listening on port' + port + 'n');
});`


Client.js



const tls = require('tls');
const fs = require('fs');
const util = require('util');
const events = require('events');
const port = 4170;

const ClientOptions = {
key: fs.readFileSync('agent2-key.pem'),
cert: fs.readFileSync('agent2-cert.pem'),
ca: fs.readFileSync('root-cert.pem')
};

const socket = tls.connect(port, ClientOptions, () => {
console.log('client connected', socket.authorized ? 'authorized' : 'unauthorized');
process.stdin.pipe(socket);
process.stdin.resume();
});
socket.setEncoding('utf8');
socket.on('data', (data) => {
console.log(data);
});
socket.on('end', () => {
server.close();
});


So as you probably saw, there is the ca1-cert.pem, agent1-cert.pem, agent2-cert.pem, agent1-key.pem, and agent2-key.pem. Those are the certificates.



Each of the cert files have the beginning and ending



-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----


And each of the key files have the beginning and ending



-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----


In addition to that, I’m wondering how secure this is because my initial plan is to use this as a login/database server or something that cant be easily hacked into.



Thank you.



Edit 1



In the comments, someone said that its possible that the certificate isn’t being verified because of the wrong CA file. I put the CA file in the same place as the “...-cert.pem” files. I’m assuming that’s not the problem though because I talked to my friend, he went over the certificate and key files and said that they’re all good.










share|improve this question

























  • "unable to verify the first certificate" probably means that the system does not have the proper CA in its trust store hence it can not verify the validity of the certificate it sees. So the problem is not directly with the certificate, but with installation of its CA locally.

    – Patrick Mevzek
    Nov 26 '18 at 23:35











  • @PatrickMevzek I have the CA file in the same folder/area as the certificate. Because this is only a test program, I could post a link to the files on github.

    – Ridley Nelson
    Nov 27 '18 at 13:24













  • You do not just need them on disk, you need to configure your client to use them. I can not tell you how precisely as I know nothing in NodeJS. But look for some option speaking about "ca certificate" and a path to provide. Then provide the path to your CA files.

    – Patrick Mevzek
    Nov 27 '18 at 14:49
















0















Recently I’ve been trying to make a basic TLC server and client to maybe put on a Rasperry PI. I found an example on github and i modified it so its up to date. My issue is that Node JS keeps saying unable to verify the first certificate Even though i generated a new one.




note the certificates and keys are self signed.




Here is my code, I have not the slightest clue as to why its failing because when I set ServerOptions As the client options, it just logs Unsecure connection.



Server.js



'use strict';

const tls = require('tls');
const fs = require('fs');
const util = require('util');
const events = require('events');
const port = 4170;

const ServerOptions = {
key: fs.readFileSync('agent1-key.pem'),
cert: fs.readFileSync('agent1-cert.pem'),
ca: fs.readFileSync('root-cert.pem'), // authority chain for the clients
requestCert: true,
rejectUnauthorized: false
};

var server = tls.createServer(ServerOptions, (socket) => {
socket.write('welcome!n');
socket.setEncoding('utf8');
socket.pipe(socket);
})

.on('connection', function(c) {
console.log('Unsecure connection');
})

.on('secureConnect', function (c) {
// c.authorized will be true if the client cert presented validates with our CA
console.log('secure connectionn client authorization Status:', c);
})

.listen(port, function() {
console.log('server listening on port' + port + 'n');
});`


Client.js



const tls = require('tls');
const fs = require('fs');
const util = require('util');
const events = require('events');
const port = 4170;

const ClientOptions = {
key: fs.readFileSync('agent2-key.pem'),
cert: fs.readFileSync('agent2-cert.pem'),
ca: fs.readFileSync('root-cert.pem')
};

const socket = tls.connect(port, ClientOptions, () => {
console.log('client connected', socket.authorized ? 'authorized' : 'unauthorized');
process.stdin.pipe(socket);
process.stdin.resume();
});
socket.setEncoding('utf8');
socket.on('data', (data) => {
console.log(data);
});
socket.on('end', () => {
server.close();
});


So as you probably saw, there is the ca1-cert.pem, agent1-cert.pem, agent2-cert.pem, agent1-key.pem, and agent2-key.pem. Those are the certificates.



Each of the cert files have the beginning and ending



-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----


And each of the key files have the beginning and ending



-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----


In addition to that, I’m wondering how secure this is because my initial plan is to use this as a login/database server or something that cant be easily hacked into.



Thank you.



Edit 1



In the comments, someone said that its possible that the certificate isn’t being verified because of the wrong CA file. I put the CA file in the same place as the “...-cert.pem” files. I’m assuming that’s not the problem though because I talked to my friend, he went over the certificate and key files and said that they’re all good.










share|improve this question

























  • "unable to verify the first certificate" probably means that the system does not have the proper CA in its trust store hence it can not verify the validity of the certificate it sees. So the problem is not directly with the certificate, but with installation of its CA locally.

    – Patrick Mevzek
    Nov 26 '18 at 23:35











  • @PatrickMevzek I have the CA file in the same folder/area as the certificate. Because this is only a test program, I could post a link to the files on github.

    – Ridley Nelson
    Nov 27 '18 at 13:24













  • You do not just need them on disk, you need to configure your client to use them. I can not tell you how precisely as I know nothing in NodeJS. But look for some option speaking about "ca certificate" and a path to provide. Then provide the path to your CA files.

    – Patrick Mevzek
    Nov 27 '18 at 14:49














0












0








0


1






Recently I’ve been trying to make a basic TLC server and client to maybe put on a Rasperry PI. I found an example on github and i modified it so its up to date. My issue is that Node JS keeps saying unable to verify the first certificate Even though i generated a new one.




note the certificates and keys are self signed.




Here is my code, I have not the slightest clue as to why its failing because when I set ServerOptions As the client options, it just logs Unsecure connection.



Server.js



'use strict';

const tls = require('tls');
const fs = require('fs');
const util = require('util');
const events = require('events');
const port = 4170;

const ServerOptions = {
key: fs.readFileSync('agent1-key.pem'),
cert: fs.readFileSync('agent1-cert.pem'),
ca: fs.readFileSync('root-cert.pem'), // authority chain for the clients
requestCert: true,
rejectUnauthorized: false
};

var server = tls.createServer(ServerOptions, (socket) => {
socket.write('welcome!n');
socket.setEncoding('utf8');
socket.pipe(socket);
})

.on('connection', function(c) {
console.log('Unsecure connection');
})

.on('secureConnect', function (c) {
// c.authorized will be true if the client cert presented validates with our CA
console.log('secure connectionn client authorization Status:', c);
})

.listen(port, function() {
console.log('server listening on port' + port + 'n');
});`


Client.js



const tls = require('tls');
const fs = require('fs');
const util = require('util');
const events = require('events');
const port = 4170;

const ClientOptions = {
key: fs.readFileSync('agent2-key.pem'),
cert: fs.readFileSync('agent2-cert.pem'),
ca: fs.readFileSync('root-cert.pem')
};

const socket = tls.connect(port, ClientOptions, () => {
console.log('client connected', socket.authorized ? 'authorized' : 'unauthorized');
process.stdin.pipe(socket);
process.stdin.resume();
});
socket.setEncoding('utf8');
socket.on('data', (data) => {
console.log(data);
});
socket.on('end', () => {
server.close();
});


So as you probably saw, there is the ca1-cert.pem, agent1-cert.pem, agent2-cert.pem, agent1-key.pem, and agent2-key.pem. Those are the certificates.



Each of the cert files have the beginning and ending



-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----


And each of the key files have the beginning and ending



-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----


In addition to that, I’m wondering how secure this is because my initial plan is to use this as a login/database server or something that cant be easily hacked into.



Thank you.



Edit 1



In the comments, someone said that its possible that the certificate isn’t being verified because of the wrong CA file. I put the CA file in the same place as the “...-cert.pem” files. I’m assuming that’s not the problem though because I talked to my friend, he went over the certificate and key files and said that they’re all good.










share|improve this question
















Recently I’ve been trying to make a basic TLC server and client to maybe put on a Rasperry PI. I found an example on github and i modified it so its up to date. My issue is that Node JS keeps saying unable to verify the first certificate Even though i generated a new one.




note the certificates and keys are self signed.




Here is my code, I have not the slightest clue as to why its failing because when I set ServerOptions As the client options, it just logs Unsecure connection.



Server.js



'use strict';

const tls = require('tls');
const fs = require('fs');
const util = require('util');
const events = require('events');
const port = 4170;

const ServerOptions = {
key: fs.readFileSync('agent1-key.pem'),
cert: fs.readFileSync('agent1-cert.pem'),
ca: fs.readFileSync('root-cert.pem'), // authority chain for the clients
requestCert: true,
rejectUnauthorized: false
};

var server = tls.createServer(ServerOptions, (socket) => {
socket.write('welcome!n');
socket.setEncoding('utf8');
socket.pipe(socket);
})

.on('connection', function(c) {
console.log('Unsecure connection');
})

.on('secureConnect', function (c) {
// c.authorized will be true if the client cert presented validates with our CA
console.log('secure connectionn client authorization Status:', c);
})

.listen(port, function() {
console.log('server listening on port' + port + 'n');
});`


Client.js



const tls = require('tls');
const fs = require('fs');
const util = require('util');
const events = require('events');
const port = 4170;

const ClientOptions = {
key: fs.readFileSync('agent2-key.pem'),
cert: fs.readFileSync('agent2-cert.pem'),
ca: fs.readFileSync('root-cert.pem')
};

const socket = tls.connect(port, ClientOptions, () => {
console.log('client connected', socket.authorized ? 'authorized' : 'unauthorized');
process.stdin.pipe(socket);
process.stdin.resume();
});
socket.setEncoding('utf8');
socket.on('data', (data) => {
console.log(data);
});
socket.on('end', () => {
server.close();
});


So as you probably saw, there is the ca1-cert.pem, agent1-cert.pem, agent2-cert.pem, agent1-key.pem, and agent2-key.pem. Those are the certificates.



Each of the cert files have the beginning and ending



-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----


And each of the key files have the beginning and ending



-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----


In addition to that, I’m wondering how secure this is because my initial plan is to use this as a login/database server or something that cant be easily hacked into.



Thank you.



Edit 1



In the comments, someone said that its possible that the certificate isn’t being verified because of the wrong CA file. I put the CA file in the same place as the “...-cert.pem” files. I’m assuming that’s not the problem though because I talked to my friend, he went over the certificate and key files and said that they’re all good.







node.js ssl web tcp insecure-connection






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 13:31







Ridley Nelson

















asked Nov 26 '18 at 22:45









Ridley NelsonRidley Nelson

177




177













  • "unable to verify the first certificate" probably means that the system does not have the proper CA in its trust store hence it can not verify the validity of the certificate it sees. So the problem is not directly with the certificate, but with installation of its CA locally.

    – Patrick Mevzek
    Nov 26 '18 at 23:35











  • @PatrickMevzek I have the CA file in the same folder/area as the certificate. Because this is only a test program, I could post a link to the files on github.

    – Ridley Nelson
    Nov 27 '18 at 13:24













  • You do not just need them on disk, you need to configure your client to use them. I can not tell you how precisely as I know nothing in NodeJS. But look for some option speaking about "ca certificate" and a path to provide. Then provide the path to your CA files.

    – Patrick Mevzek
    Nov 27 '18 at 14:49



















  • "unable to verify the first certificate" probably means that the system does not have the proper CA in its trust store hence it can not verify the validity of the certificate it sees. So the problem is not directly with the certificate, but with installation of its CA locally.

    – Patrick Mevzek
    Nov 26 '18 at 23:35











  • @PatrickMevzek I have the CA file in the same folder/area as the certificate. Because this is only a test program, I could post a link to the files on github.

    – Ridley Nelson
    Nov 27 '18 at 13:24













  • You do not just need them on disk, you need to configure your client to use them. I can not tell you how precisely as I know nothing in NodeJS. But look for some option speaking about "ca certificate" and a path to provide. Then provide the path to your CA files.

    – Patrick Mevzek
    Nov 27 '18 at 14:49

















"unable to verify the first certificate" probably means that the system does not have the proper CA in its trust store hence it can not verify the validity of the certificate it sees. So the problem is not directly with the certificate, but with installation of its CA locally.

– Patrick Mevzek
Nov 26 '18 at 23:35





"unable to verify the first certificate" probably means that the system does not have the proper CA in its trust store hence it can not verify the validity of the certificate it sees. So the problem is not directly with the certificate, but with installation of its CA locally.

– Patrick Mevzek
Nov 26 '18 at 23:35













@PatrickMevzek I have the CA file in the same folder/area as the certificate. Because this is only a test program, I could post a link to the files on github.

– Ridley Nelson
Nov 27 '18 at 13:24







@PatrickMevzek I have the CA file in the same folder/area as the certificate. Because this is only a test program, I could post a link to the files on github.

– Ridley Nelson
Nov 27 '18 at 13:24















You do not just need them on disk, you need to configure your client to use them. I can not tell you how precisely as I know nothing in NodeJS. But look for some option speaking about "ca certificate" and a path to provide. Then provide the path to your CA files.

– Patrick Mevzek
Nov 27 '18 at 14:49





You do not just need them on disk, you need to configure your client to use them. I can not tell you how precisely as I know nothing in NodeJS. But look for some option speaking about "ca certificate" and a path to provide. Then provide the path to your CA files.

– Patrick Mevzek
Nov 27 '18 at 14:49












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490255%2fnode-js-tls-server-and-client-aren-t-working-correctly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490255%2fnode-js-tls-server-and-client-aren-t-working-correctly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)