immediatextradeai.com → cloud → certificate major investing → curl self signed certificate
Posted on by Patrick Wade

curl self signed certificate

Terminal window showing curl command with error message about untrusted self-signed certificate

When curl connects to a server using HTTPS, it verifies the server's SSL certificate. A self-signed certificate lacks validation from a trusted Certificate Authority, causing curl to reject the connection unless explicitly instructed otherwise.

Curl enforces TLS security by default. If a server presents a self-signed certificate-common in internal systems, development environments, or test servers-curl refuses to proceed. This behavior prevents man-in-the-middle attacks but can block access during testing or API integration.

To accept a self-signed certificate, use the --insecure flag to disable certificate verification. Alternatively, use --cacert to specify a custom CA file containing the self-signed certificate's public key. Both methods override curl's default security checks.

How It Works

Curl uses OpenSSL or another TLS library to establish encrypted connections. During handshake, the server sends its certificate chain. Curl checks each certificate against a built-in list of trusted root CAs. If the certificate isn't issued by a known authority, curl fails with an error like "unable to verify the first certificate" or "SSL certificate problem: self-signed certificate."

Self-signed certificates are generated by the server itself, not a third-party CA. They lack external validation, making them inherently untrusted by default. This design helps users don't accidentally connect to spoofed servers.

For example, a local API server running on https://localhost:8443 may use a self-signed certificate. Running curl https://localhost:8443 returns:

curl: (60) SSL certificate problem: self-signed certificate
More details here: https://curl.se/docs/sslcerts.html

Types and Common Use Cases

Self-signed certificates fall into two categories: temporary test certs and long-lived internal PKI setups. Temporary ones are often created with tools like openssl req or mkcert for quick testing. Internal PKIs use private CAs to issue certificates across an organization.

Common scenarios include:

Using curl --insecure is acceptable in controlled environments but risky in production. It disables all certificate validation, exposing the client to interception.

Method Use Case Security Risk
--insecure Quick testing, dev environments High - no certificate verification
--cacert with custom CA Internal services, CI/CD pipelines Low - only trusts specified cert
--capath Multiple trusted certs in directory Low - validates against a trust store

Correcting Common Mistakes

Many users assume --insecure is safe because it works. But it removes all trust checks. A malicious proxy could intercept traffic without detection.

A better alternative is to extract the self-signed certificate and add it to a custom CA bundle. For example:

  1. Export the certificate from the server using a browser or openssl s_client:
openssl s_client -connect localhost:8443 -servername localhost < /dev/null | \
  openssl x509 -outform PEM -out mycert.pem
  1. Create a CA bundle:
cat mycert.pem >> /tmp/trusted-ca-bundle.crt
  1. Use it with curl:
curl --cacert /tmp/trusted-ca-bundle.crt https://localhost:8443

This method maintains encryption while trusting only the intended server.

Some developers mistakenly believe --insecure is required for all self-signed certs. That's false. With proper configuration, curl can validate self-signed certificates securely.

Real-World Example

I once debugged a CI pipeline that failed when calling a staging API hosted on a self-signed certificate. The error was:

curl: (60) SSL certificate problem: self-signed certificate

Instead of adding --insecure globally, I extracted the certificate from the staging server using openssl s_client, saved it as staging-ca.crt, and passed it via --cacert in the script. The connection succeeded, and the pipeline ran without compromising security.

This approach is recommended for automation. It avoids hardcoding insecure flags and aligns with DevOps principles of least privilege.

What It's Not

curl self signed certificate is not a configuration file, nor is it a standalone tool. It describes a specific interaction between curl and a server using a certificate not issued by a trusted CA.

It's also not equivalent to disabling TLS entirely. Disabling TLS would expose data in plaintext. Using --insecure keeps encryption but skips validation. Using --cacert preserves both encryption and trust.

Some confuse this with setting environment variables like CURL_CA_BUNDLE. While that affects the default CA path, it doesn't replace the need to specify a certificate file when dealing with self-signed certs.

Authoritative Sources

The cURL project documentation states: "By default, curl verifies the server's certificate against a set of trusted CAs. If the certificate is self-signed or not issued by a recognized CA, the connection fails." curl.se/docs/sslcerts.html.

OpenSSL's official guide confirms that self-signed certificates are valid for testing but must be explicitly trusted in applications. "Self-signed certificates are not inherently insecure, but they require manual trust setup." openssl.org/docs/man1.1.1/man1/openssl-x509.html.

Using --insecure in production environments violates PCI DSS and NIST guidelines for secure communications. Always prefer --cacert when possible.


Related Posts