Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to enable ocsp in web socket #46

Open
LMJayasundara opened this issue Jul 21, 2022 · 0 comments
Open

How to enable ocsp in web socket #46

LMJayasundara opened this issue Jul 21, 2022 · 0 comments

Comments

@LMJayasundara
Copy link

LMJayasundara commented Jul 21, 2022

I used wb with https and express. Still I get ocsp respond from the ocsp server even I enable ocsp stapling. How to enable it. I coud not figure out it.
This is my code.

var WebSocket = require('ws');
var https = require('https');
var ocsp = require('ocsp');

var bodyparser = require('body-parser');
var express = require('express');
var app = express();

app.use(bodyparser.json());
var PORT = 8080;

var ocspCache = new ocsp.Cache();

var server = new https.createServer({
    cert: fs.readFileSync(`${__dirname}/pki/server/certs/server.cert.pem`),
    key: fs.readFileSync(`${__dirname}/pki/server/private/server.key.pem`),
    ca: [
        fs.readFileSync(`${__dirname}/pki/intermediate/certs/ca-chain.cert.pem`)
    ],
    requestCert: true,
    rejectUnauthorized: true
}, app);

var wss = new WebSocket.Server({
    server
});

wss.on('connection', function connection(ws, req) {

    var cert = req.socket.getPeerCertificate(true);
    var rawCert = cert.raw;
    var rawIssuer = cert.issuerCertificate.raw;

    ocsp.getOCSPURI(rawCert, function(err, uri) {
        if (err) console.log(err);
        var req = ocsp.request.generate(rawCert, rawIssuer);
        var options = {
            url: uri,
            ocsp: req.data
        };
        ocspCache.request(req.id, options, null);
    });

    ws.on('message', function incoming(message) {
        console.log(ocspCache.cache);
        ocsp.check({cert: rawCert, issuer: rawIssuer}, function(err, res) {
            if(err) {
                console.log(err.message);
                ws.send('Failed to obtain OCSP response!');
            } else {
                console.log(res.type);
                var status = res.type;
                if(status == 'good'){
                    console.log('Received: %s', message);
                    ws.send('Hello from server!');
                }else{
                    ws.send('Certificate is revoked!');
                }
            }                              
        });
    });
});

server.listen(PORT, ()=>{
    console.log( (new Date()) + " Server is listening on port " + PORT);
});

@indutny

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant