>_C2CZ
Serverless · Self-Hosting · Security · Independent Ops

self signed certificate in certificate chain (error 19)

By Alex Rivera · Edge & Serverless · Last verified 2026-11-18

Error: self-signed certificate in certificate chain does not mean your leaf certificate is self-signed. It means the client walked the chain the server sent, reached its last certificate, and found that this certificate is signed by itself. On a private CA that is normal and expected — a root CA is always self-signed, that is what makes it a root — and the client still refuses because that root is not in its trust store. The message names the symptom, and the symptom is the same whether the CA is your own lab root or someone else's forgotten appliance.

Node reports it as SELF_SIGNED_CERT_IN_CHAIN, curl prints curl: (60) SSL certificate problem: self-signed certificate in certificate chain, and openssl s_client prints Verify return code: 19 — the exit status of that command is still 0, so a script that checks $? will not see the failure unless it passes -verify_return_error. Three clients, one condition. Below is a lab with one root, one self-signed leaf and one CA-signed leaf, three listeners, and every client message quoted from a re-run on 2026-11-18 on OpenSSL 3.5.7 and Node v26.12.1.

  • TL;DR — the depth= line tells you which certificate is the self-issued one. depth=0 means the leaf, depth=1 means the CA you shipped with it.
  • Count the certificates on the wire: one means the leaf is the whole chain, two means an issuer travelled with it.
  • NODE_EXTRA_CA_CERTS pointing at your private root repairs error 19 and error 20 and changes nothing for error 18. Pointing it at the self-signed leaf itself repairs 18 and nothing else. Which file you name decides which shape flips.
  • The variable is read from the environment at process start; assigning it inside the program changes nothing.

What the chain in the error actually is

TLS does not send "a certificate". The server sends an ordered list: the leaf first, then the issuer of the leaf, then the issuer of that issuer, and so on. OpenSSL builds a path from that list plus its own trust store, and it starts the walk at the top of the list, not the bottom. The first depth= line you see in a failing handshake is therefore often the highest index, not zero.

That ordering explains the whole vocabulary of these errors. Each code is a statement about one position in the list:

CodeMessageWhat it says about the list
18self-signed certificatethe certificate at this depth names itself as its issuer, and it is the leaf
19self-signed certificate in certificate chainthe certificate at this depth names itself as its issuer, and it sits above the leaf
20unable to get local issuer certificatethe issuer of the certificate at this depth is missing from both the list and the trust store
21unable to verify the first certificatethe walk could not even start from the leaf

Codes 18 and 19 are the same object test applied at two depths. A certificate whose issuer field repeats its subject field is self-issued: it names itself as its issuer. That comparison is what separates the two codes, and it is worth being precise about what it does and does not prove — subject and issuer strings matching is not a signature check, so a truncated or forged certificate can also carry them. The two sentences differ by three words and describe two different infrastructures: 18 is a server that generates its own certificate and never had a CA, 19 is a server that has a CA and sends its root to strangers.

Three listeners, one lab, four clients

The lab needs three shapes, and each shape is a port. Shape one is a leaf that signs itself. Shape two is a leaf signed by a private CA, with the CA appended to the file handed to the server, so both certificates travel. Shape three is the same CA-signed leaf with the CA left off the wire — the shape that produces error 20 and that the previous post on this desk covered depth by depth (unable to get local issuer certificate: error 20 by depth).

Building the three takes four commands. Note the extensions: the root is CA:TRUE, and the leaf the CA signs is CA:FALSE. That single bit comes back later and decides whether a workaround works or silently does nothing.

mkdir -p certs
cd certs

# private root CA
openssl req -x509 -newkey rsa:2048 -sha256 -days 3650 -nodes \
  -keyout root.key -out root.pem -subj "/CN=c2cz Lab Root CA" \
  -addext "basicConstraints=critical,CA:TRUE" \
  -addext "keyUsage=critical,keyCertSign,cRLSign"

# leaf that signs itself (depth 0)
openssl req -x509 -newkey rsa:2048 -sha256 -days 825 -nodes \
  -keyout leaf1.key -out leaf1.pem -subj "/CN=localhost" \
  -addext "subjectAltName=DNS:localhost,IP:127.0.0.1" \
  -addext "basicConstraints=critical,CA:FALSE" \
  -addext "keyUsage=critical,digitalSignature,keyEncipherment" \
  -addext "extendedKeyUsage=serverAuth"

# leaf signed by the CA (depth 1)
openssl req -newkey rsa:2048 -nodes -keyout leaf2.key -out leaf2.csr -subj "/CN=localhost"
printf 'subjectAltName=DNS:localhost,IP:127.0.0.1\nbasicConstraints=critical,CA:FALSE\nkeyUsage=critical,digitalSignature,keyEncipherment\nextendedKeyUsage=serverAuth\n' > leaf2.ext
openssl x509 -req -in leaf2.csr -CA root.pem -CAkey root.key -CAcreateserial \
  -days 825 -sha256 -extfile leaf2.ext -out leaf2.pem

cat leaf2.pem root.pem > chain2.pem

A Python ThreadingHTTPServer wrapped in a server-side TLS context serves them, one process per port, each loaded with a different certificate file: leaf1.pem on 9441, chain2.pem on 9442, leaf2.pem on 9443. The key is always the leaf's key; only the certificate file changes, which is the whole point — the same key, the same hostname, three different byte counts on the wire: 1200, 2363 and 1200 bytes.

Four clients, four different sentences

Here is the complete matrix from one contiguous run. Every line below is pasted from that run, including the certificate count, which is the measurement most people skip.

### port 9441
-- curl
curl: (60) SSL certificate problem: self-signed certificate
-- node https.get
https.get 9441 DEPTH_ZERO_SELF_SIGNED_CERT self-signed certificate; if the root CA is installed locally, try running Node.js with --use-system-ca
-- node fetch
fetch 9441 TypeError: fetch failed | cause: DEPTH_ZERO_SELF_SIGNED_CERT self-signed certificate; if the root CA is installed locally, try running Node.js with --use-system-ca
-- openssl verify code
Verify return code: 18 (self-signed certificate)
-- certs sent by server
1

### port 9442
-- curl
curl: (60) SSL certificate problem: self-signed certificate in certificate chain
-- node https.get
https.get 9442 SELF_SIGNED_CERT_IN_CHAIN self-signed certificate in certificate chain
-- node fetch
fetch 9442 TypeError: fetch failed | cause: SELF_SIGNED_CERT_IN_CHAIN self-signed certificate in certificate chain
-- openssl verify code
Verify return code: 19 (self-signed certificate in certificate chain)
-- certs sent by server
2

### port 9443
-- curl
curl: (60) SSL certificate problem: unable to get local issuer certificate
-- node https.get
https.get 9443 UNABLE_TO_VERIFY_LEAF_SIGNATURE unable to verify the first certificate; if the root CA is installed locally, try running Node.js with --use-system-ca
-- node fetch
fetch 9443 TypeError: fetch failed | cause: UNABLE_TO_VERIFY_LEAF_SIGNATURE unable to verify the first certificate; if the root CA is installed locally, try running Node.js with --use-system-ca
-- openssl verify code
Verify return code: 21 (unable to verify the first certificate)
-- certs sent by server
1

The --use-system-ca hint that Node appends is not uniform: it follows the two depth-zero codes in this run and does not follow SELF_SIGNED_CERT_IN_CHAIN, so a grep for the hint is not a reliable classifier.

Two things to take from it. First, fetch never says anything useful at the top level: the promise rejects with TypeError: fetch failed and the diagnostic lives in err.cause.code. Any log line that prints only err.message throws away the only field that identifies the cause. Second, the OpenSSL code for the CA-signed-leaf-without-its-CA case is 21 here, not 20, while curl reports it with the error-20 sentence — the numbers come from different layers and do not have to agree.

Read the depth line before the sentence

Run the trace against all three ports and the structure becomes obvious. The lines below are the same three listeners, one openssl s_client per port, filtered to the depth and the code:

for p in 9441 9442 9443; do
  echo "== 127.0.0.1:$p"
  echo | openssl s_client -connect "127.0.0.1:$p" -servername localhost 2>&1 \
    | grep -E "depth=|verify error|Verify return code"
done
== 127.0.0.1:9441
depth=0 CN=localhost
verify error:num=18:self-signed certificate
depth=0 CN=localhost
Verify return code: 18 (self-signed certificate)
== 127.0.0.1:9442
depth=1 CN=c2cz Lab Root CA
verify error:num=19:self-signed certificate in certificate chain
depth=1 CN=c2cz Lab Root CA
depth=0 CN=localhost
Verify return code: 19 (self-signed certificate in certificate chain)
== 127.0.0.1:9443
depth=0 CN=localhost
verify error:num=20:unable to get local issuer certificate
depth=0 CN=localhost
verify error:num=21:unable to verify the first certificate
depth=0 CN=localhost
Verify return code: 21 (unable to verify the first certificate)

On 9442 the first line is depth=1 and it names the CA, not the leaf. OpenSSL started at the top of the two-certificate list, tested the root, found it self-signed, and stopped there. On 9441 the subject at depth=0 is localhost and the code is 18. The certificate name on the depth= line is the answer to "which one is self-signed" — read it before you read the sentence after it.

On 9443 you get two codes for one handshake, 20 and then 21, because the walk retries at the same depth with a different test. A single number is not always the whole verdict; the sequence is.

Count the certificates on the wire

The count is the cheapest independent check, and it takes one flag. In the same run, the number of PEM blocks the server sends is 1, 2, 1 for ports 9441, 9442, 9443. That single integer separates the two most confused cases in this family.

echo | openssl s_client -connect 127.0.0.1:9442 -servername localhost -showcerts 2>&1 \
  | grep -c "BEGIN CERTIFICATE"

One certificate plus error 19 is impossible: a single certificate cannot be both the leaf and its own issuer unless it is self-signed, and that case reports 18. So if the count is 1, the leaf is the problem and no amount of trust-store work will help. If the count is 2 or more and the top certificate is self-signed, you shipped your private root, and the failure is entirely client-side.

That distinction is worth being blunt about, because the two failures look identical in a ticket and cost different amounts of time. Error 18 on a staging host is a server that never had a CA, fixed by issuing a leaf from one. Error 19 is a server that has a CA and is handing its root to every caller, fixed by teaching the caller that the root exists — or by issuing the leaf from an authority the caller already trusts.

Why NODE_EXTRA_CA_CERTS repairs 19 and leaves 18 broken

Point Node at the root and re-run the same client. This is the measurement that identifies the shape without reading a single log line, because the two shapes respond differently to the same input:

### NODE_EXTRA_CA_CERTS=certs/root.pem
-- port 9441
https.get 9441 DEPTH_ZERO_SELF_SIGNED_CERT self-signed certificate; if the root CA is installed locally, try running Node.js with --use-system-ca
fetch 9441 TypeError: fetch failed | cause: DEPTH_ZERO_SELF_SIGNED_CERT self-signed certificate; if the root CA is installed locally, try running Node.js with --use-system-ca
-- port 9442
https.get 9442 OK 200
fetch 9442 OK 200 in 67ms
-- port 9443
https.get 9443 OK 200
fetch 9443 OK 200 in 67ms

9442 flips from error 19 to 200, because the certificate the walk stopped on is now present in the trust store. 9443 flips as well, because the root that was missing entirely is now available locally — the server does not have to send it. 9441 stays broken: the leaf is its own issuer and the root in root.pem has nothing to do with it. If adding your CA fixes the failure, you had 19 or 20. If it changes nothing, you had 18 and were editing the wrong file.

The same trick with the leaf file works on 9441 and nowhere else, and the reason is not the extensions. Every leaf in this lab carries basicConstraints=critical,CA:FALSE and CA:TRUE appears only on the root, yet NODE_EXTRA_CA_CERTS=certs/leaf1.pem still returns 200 on 9441: a certificate named in that variable becomes a trust anchor whether or not it is a CA. Naming certs/leaf2.pem changes nothing on 9442 or 9443, because leaf2 is the certificate being verified there, not its issuer. Which file you name decides which shape flips, not what the file says about itself.

The variable must exist before the process starts

The variable is read from the environment of the process, so where you set it matters. Here is a program that assigns it to itself and then connects to the server it is supposed to trust:

const https = require("node:https");

process.env.NODE_EXTRA_CA_CERTS = "/opt/data/c2cz/lab/2026-11-12/certs/root.pem";

function onResponse(res) {
  res.resume();
  console.log("port 9442 OK", res.statusCode);
}

function onError(err) {
  console.log("port 9442", err.code, err.message);
}

https.get({ host: "127.0.0.1", port: 9442, path: "/", servername: "localhost" }, onResponse)
  .on("error", onError);
port 9442 SELF_SIGNED_CERT_IN_CHAIN self-signed certificate in certificate chain

Assigning process.env.NODE_EXTRA_CA_CERTS inside the program does nothing, which is measured above rather than assumed. The file has to be in the environment when the interpreter starts: in the systemd unit, the container definition, the shell that launches the worker. This is the most common reason a correctly-named CA file "does not work" — the value exists in the process, and the trust store was built before it did.

A wrong path fails loudly, which is at least honest, and the warning is worth knowing because it arrives before the connection error and is easy to scroll past:

Warning: Ignoring extra certs from `/opt/lab/certs/nope.pem`, load failed: error:80000002:system library::No such file or directory

Ignoring is the operative word. Node does not exit; it continues with the default store and reports the TLS failure a moment later, so a broken mount looks exactly like a trust problem.

What curl does with the same server

curl is the second opinion, and it takes its CA file as a flag instead of an environment variable, which makes it the fastest way to test a key file without touching a Node process or a service unit. Against the same three listeners, with --cacert certs/root.pem:

9442 https://localhost:9442/ http 200
9443 https://localhost:9443/ http 200
9441 https://localhost:9441/ curl: (60) SSL certificate problem: self-signed certificate

Identical verdicts to Node, from a completely different TLS stack, which is what you want before you change a production variable: two clients agreeing on the shape means the shape is real and not a quirk of one runtime. The same binary also demonstrates the trap in this whole family — --insecure makes all three return 200 and none of them trustworthy.

Related ground on this desk, if your failure is on the key side rather than the trust side: importKey on a PEM secret: the Workers ES256 fix covers the same PEM-versus-DER confusion one layer up, and Unsupported state or unable to authenticate data covers the decryption errors that arrive after the handshake finally succeeds.

What a classifier has to get right

Reading three clients by hand works once. For the second incident a script can answer the same question in one pass: fetch the chain, test each certificate against the key that certificate carries, then ask OpenSSL for its verdict against an empty trust store. The output is the count, one line per depth, and the code — the four facts this article has been reading by eye.

Two details decide whether such a script reports the shape or a false positive, and both sit inside the verify call rather than in the code around it. The first is whether the certificate's own signature is actually checked: a certificate handed to OpenSSL as its own trust anchor is trusted by fiat, and the signature test is skipped unless it is requested explicitly. The second is whether certificate lifetime is allowed to interfere: a shape test that stops on an expired certificate reports a missing issuer where the real finding is an expired anchor.

Both are flags on one command line, and both are the difference between a classifier and a string comparison. Whoever writes the script inherits the same rule as the hand reading above: the subject and issuer lines tell you what to test, never what is true.

If the script runs in a pre-deploy check, its exit status carries more weight than its prose. A chain that could not be fetched and a chain with no self-signed certificate must not share a status, or a broker that refuses connections passes the gate that was supposed to catch it.

What each result proves

The table below is the whole article compressed, and every cell comes from the runs above rather than from documentation.

ObservationShapeWhat it rules out
1 certificate, code 18, depth=0leaf signs itselfevery trust-store fix; the server never had a CA
2 certificates, code 19, depth=1leaf plus private root on the wirea missing intermediate; the root is present and untrusted
1 certificate, code 20 then 21CA-signed leaf, issuer not senta client-side bundle problem; the server is withholding a certificate it has
root.pem as extra CA fixes it19 or 20nothing further — the client simply did not know your root
root.pem as extra CA changes nothing18the root; it is not part of this leaf's path

Row three is the one that sends people to the wrong host. A single certificate and a code around 20 reads like "the client's bundle is stale", but the server is the party that failed to send a certificate it already has on disk. The two codes differ in one sentence: 19 is a certificate that arrived on the wire and was rejected, 20 is an issuer that never arrived at all — which is why the count on the wire comes first in the triage below. The previous post on this desk walks that case down depth by depth: unable to get local issuer certificate: error 20 by depth.

Triage order that converges

Four steps, in this order, each one eliminating a shape rather than guessing at a fix.

  1. Count the certificates. openssl s_client -showcerts piped to grep -c "BEGIN CERTIFICATE". One means the leaf is the chain; two or more means a CA is on the wire.
  2. Read the depth on the error line. depth=0 is the leaf, depth=1 is the certificate after it. The CN printed there is the self-signed one.
  3. Add your root as an extra CA and retry the exact client that failed. Fixed means 19 or 20 and the work is on the client. Unchanged means 18 and the work is on the server.
  4. Confirm with a second stack. curl with --cacert agrees with Node on all three shapes here. When two stacks disagree, the disagreement itself is the finding, and usually it is a client whose trust store is not the one you edited.

The reason this converges is that none of the steps asks why. A self-signed certificate in a chain is a fact about bytes, and the four clients in this lab disagree about the wording while agreeing about the bytes. Match the bytes first; the sentence becomes trivial afterwards.

One last note for anyone running this against a live service rather than a lab: the certificate that arrives on the wire and the certificate sitting in your nginx or Caddy ssl_certificate directive are two different objects until you prove otherwise. Every measurement above reads the wire. A file on disk that contains both the leaf and the root proves nothing about what the listener sends, which is why the count comes first.

Hyper-specific engineering guides: Cloudflare Workers, Turso, self-hosting, network security, offensive security, independent operations.