Setting up HTTPS on Deno , with self signed certificate?

Viewed 2192

I was setting up a Deno server to handle HTTPS request, I used self signed certificates to do the job. Used below code for this:

import { serveTLS } from "https://deno.land/std/http/server.ts";

const body = new TextEncoder().encode("Hello HTTPS");
const options = {
  hostname: "localhost",
  port: 443,
  certFile: "./path/to/localhost.crt",
  keyFile: "./path/to/localhost.key",
};
// Top-level await supported
for await (const req of serveTLS(options)) {
  req.respond({ body });
}

I ran this code as: deno --allow-net --allow-read app.ts I get following error:

ERROR RS - rustls::session:571 - TLS alert received: Message {
    typ: Alert,
    version: TLSv1_3,
    payload: Alert(
        AlertMessagePayload {
            level: Fatal,
            description: BadCertificate,
        },
    ),
}
error: Uncaught InvalidData: received fatal alert: BadCertificate
► $deno$/errors.ts:57:13
    at InvalidData ($deno$/errors.ts:135:5)
    at constructError ($deno$/errors.ts:57:13)
    at unwrapResponse ($deno$/dispatch_json.ts:41:12)
    at sendAsync ($deno$/dispatch_json.ts:96:10)

Is it possible to use self signed certificates with Deno ? What went wrong and how to fix it ?

2 Answers

It was a problem with my certificate files, strangly the same certificates were working with nodejs. I created local certificates and key than using this mkcert, then it worked!

If you're on MacOS, the best way to fix this is to instruct Deno to look at the system as the CA store.

Run this, then try again to see if you still get a warning/error:

export DENO_TLS_CA_STORE=system

You probably also want this in your .bash_profile as well.

Related