Ok I found my answer and I believe it will help others !
So, there is an important difference between a "req" certificate and a x509 certificate!
In the x509, the Private Key is also included in the file !
So, in the following, I'll show how to generate a certificate and read it in Python, using REQ and x509 type of certificate.
Using a "req" certificate :
To generate it :
openssl req -new -newkey rsa:2048 -nodes -keyout domain.key -out domain.csr
And to read it :
import OpenSSL
# cert contains the content of domain.csr
OpenSSL.crypto.load_certificate_request(OpenSSL.crypto.FILETYPE_PEM, cert).get_subject().get_components()
Using a "x509" certificate :
To generate it :
openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem
And to read it :
import OpenSSL
# cert contains the content of cert.pem
OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert).get_subject().get_components()
That's it !
The difference is located in how you load the certificate in OpenSSL.crypto
:
- For "req" :
load_certificate_request()
- For "x509" :
load_certificate()
It was that simple ;)