2

I'm a bit confused by this command for generating a self-signed certificate from an otherwise clear tutorial on OpenSSL:

openssl req \
  -x509 -nodes -days 365 -sha256 \
  -newkey rsa:2048 -keyout mycert.pem -out mycert.pem

I understand this generates a request for a self-signed x509 certificate, and not a certificate request to be signed by a CA, but the private key is in the generated certificate file and sounds dodgy to me since the private key shouldn't be sent with the certificate.

Is this an error or how this is supposed to work?

8
  • 1
    Dismissing the fact self-signed certs aren't secure (they lack Chain of Trust), there's no use case for this, as doing so is a massive security risk. The closest use-case would be for a web server, however, even then, while the private key wouldn't be encrypted, it does require different permissions than the certificate. Those types of tutorials are bare-minimum ones, choosing the bare minimum over what's recommended for basic security, similar to the vast majority of OpenVPN & OpenSSH tutorials (I cover how to securely create certs in this answer)
    – JW0914
    Commented May 30, 2021 at 12:00
  • my question is this: Does it make sense to have the private key and certificate on the same file or they have to be on separated files? Also, chain of trust does not improve security afaik, just the browsers will trust it. @JW0914
    – user1136218
    Commented May 30, 2021 at 12:23
  • From ssl.com/faqs/what-is-an-x-509-certificate it seems only the public key should be there
    – user1136218
    Commented May 30, 2021 at 12:29
  • @Minksy As stated above, there is no use case for this as it's a massive security risk. Chain of Trust has nothing to do with browsers in and of themselves and everything to do with the CoT of a certificate (browsers are only one use case for certs, others are authentication [VPNs], encryption of files, identity verification [CAC, email], etc.). PKI keys are never public, as doing so makes PKI pointless (the "public" reference in the link is referring to PKI).
    – JW0914
    Commented May 30, 2021 at 12:40
  • I understand you think this is risky and appreciate your comments @JW0914, but if we don't go by steps there is no point in commenting. I'm creating certificates just for understanding how this works. I do that by successive approximations, and this means there will be errors. So this won't be a wan site, it's a switch with 2 pcs. Now, the first question is: what is the risk of having a private key on the certificate (see the command -keyout mycert.pem -out mycert.pem)? Next, I will go deeper.
    – user1136218
    Commented May 30, 2021 at 12:40

1 Answer 1

1

I understand this generates a request for a self-signed x509 certificate, and not a certificate request to be signed by a CA, but the private key is in the generated certificate file and sounds dodgy to me since the private key shouldn't be sent with the certificate.

It will not be sent with the certificate.

Certificates and files are not the same thing. The TLS software never sends the actual file anywhere as-is – it reads the file, interprets its contents, and loads the data that it wants to load. When you have a PEM-formatted file with multiple items, the program easily knows when one item ends and another begins.

In this case, the program using your .pem file will load one "certificate" object and one "private key" object from it, after which point the original source is no longer relevant – it could've been a single .pem file, two separate .pem files, a DER .crt file, a PKCS#12 .pfx file, or not even a file in the first place. The program only cares about the actual certificates that are now in memory – if it needs to send a certificate it'll send only that.

3
  • and how is this self signed?
    – user1136218
    Commented May 30, 2021 at 14:58
  • 1
    It has nothing to do whatsoever with the certificate being self-signed or not. The signature and the issuer field are part of the certificate object, not of the surrounding file format. Commented May 30, 2021 at 15:11
  • yes, corroborated googling. thank you
    – user1136218
    Commented May 30, 2021 at 16:27

You must log in to answer this question.