randombit / botan

Cryptography Toolkit

Home Page:https://botan.randombit.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Botan CLI generates invalid self-signed certificate authority

cmazakas opened this issue · comments

Something as simple as:

botan gen_self_signed ca.key.pem localhost --ca > ca.crt.pem

will generate a certificate with:

exbigboss@exbigboss-ubuntu:~/cpp/fiona/test/tls/botan$ botan cert_info ca.crt.pem
Version: 3
Subject: CN="localhost"
Issuer: CN="localhost"
Issued: 2024/01/23 21:22:03 UTC
Expires: 2025/01/22 21:22:03 UTC
Constraints:
   Cert Sign
   CRL Sign
Signature algorithm: RSA/EMSA3(SHA-256)
Serial number: B74DE8437AD7C99263B41A56881FACF3
Authority keyid: D661FC90C7AC625677A2CB2A9A6CF5FF287B1981A882C901
Subject keyid: D661FC90C7AC625677A2CB2A9A6CF5FF287B1981A882C901
Public Key [RSA-3072]

-----BEGIN PUBLIC KEY-----
MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAyvaGF9ScGSfWV+jXz97m
1RuAUpIIVKDmiiuNBLZgaY8PQm7DhlUgxQHDMpL/yG6821vjmsgxG1O7QB7vIIr3
g0xrQDApjA7/Bkt/7QEIkOVk0pgjdS2wBgwpAATg4N1DGNi3dPFeaf+5r53dX/E6
gMakIko7K/seUiyYvi5Gp+hZm7L0hpu51oHeoBNbTFH5v8gu04cKxPVNe6bhukHc
C09bwe4a89HMcK3DYbmNj5kOcXBYm3SnGF3ZuqNmNXRt4ItZ/WrXHFEzz3qG9WcV
11fjKoTekB6NYG5ccbMWdA5Nv2ACOWI9sp5zjmoGWzomlCQoVgpidjRRzCV9t0Q9
6d5c3mHt8+cD5AJ+yy1DuzzKFS/o6Q1yQ5UOSVZySl/WSZ4LECVdRVZIqXIIga/Q
p5UEloA3TRIdoEUxGVfmKYpc30N4x8D+cr1Jeso27dIaTJeeM9fUY15632sswbeC
TZ4e15AyYGf3fV9if5VYLBRrcvCoafQUNb8IGS/zg22dAgMBAAE=
-----END PUBLIC KEY-----

But this is invalid for use in the Botan library as the following check fails:

bool certificate_allows_signing(const X509_Certificate& cert) {
const auto constraints = cert.constraints();
if(constraints.empty()) {
return true;
}
return constraints.includes_any(Key_Constraints::DigitalSignature, Key_Constraints::NonRepudiation);
}

Is there a way to generate this self-signed cert with these key usages enabled?

In my local application I see:

/home/exbigboss/cpp/fiona/test/tls_test.cpp:81: FAILED:
  {Unknown expression after the reported line}
due to unexpected exception with message:
  Certificate usage constraints do not allow signing

I can confirm that. Currently, the CLI does not offer parameters to influence the basic key constraints of the certificate.

By enabling --ca for the self-signed certificate, Botan will default the basic key constraints to KeyCertSign and CrlSign, which is incompatible with the requirements for a TLS server.

There are two possible workarounds for testing:

  1. Don't declare your self-signed cert to be a CA. In this case, the basic key constraints will remain empty, meaning "anything goes":
./botan keygen --algo=ECDSA --params=secp256r1 --output=self.key
./botan gen_self_signed self.key localhost > self.pem
  1. Create a self-signed CA as you did already and then sign a server certificate with it. The server certificate won't have any key constraints either and therefore work as a TLS server certificate.
./botan keygen --algo=ECDSA --params=secp256r1 --output=ca.key
./botan keygen --algo=ECDSA --params=secp256r1 --output=server.key
./botan gen_self_signed ca.key TestCA --ca > ca.pem
./botan gen_pkcs10 server.key localhost > server.csr
./botan sign_cert ca.pem ca.key server.csr > server.pem

By enabling --ca for the self-signed certificate, Botan will default the basic key constraints to KeyCertSign and CrlSign, which is incompatible with the requirements for a TLS server.

Ah, helps to know that.

Create a self-signed CA as you did already and then sign a server certificate with it. The server certificate won't have any key constraints either and therefore work as a TLS server certificate.

I did wind up pivoting and going exactly with this. Good to see my intuition was correct.

I'll probably close this issue because I'm not sure there's anything else to do. Hopefully this issue will be good for any other confused users down the road like myself.

I appreciate the help. Thank you, reneme.