i found the function that creates the CSRs on this thread: Generating a CSR in Python
However it has no mention to how i could add SAN (SubjectAltName) to the CSR which is important for the browsers to consider the certificate generated using this CSR to be secure.
I then found a separate function for adding SAN to the CSR in this thread: Is it possible to set subjectAltName using pyOpenSSL?
The problem is i am not able to combine both for whatever reason. whenever i add it, it seems like the Microsoft Active Directory Certificate Services doesn't like it and gives me this error: Certificate Services Error Message
What am i doing wrong? thank you for all the help in advance
here is how my code is looking for reference:
def create_csr(common_name, country=None, state=None, city=None,
organization=None, organizational_unit=None,
email_address=None):
"""
Args:
common_name (str).
country (str).
state (str).
city (str).
organization (str).
organizational_unit (str).
email_address (str).
Returns:
(str, str). Tuple containing private key and certificate
signing request (PEM).
"""
key = OpenSSL.crypto.PKey()
key.generate_key(OpenSSL.crypto.TYPE_RSA, 2048)
req = OpenSSL.crypto.X509Req()
req.get_subject().CN = common_name
if country:
req.get_subject().C = country
if state:
req.get_subject().ST = state
if city:
req.get_subject().L = city
if organization:
req.get_subject().O = organization
if organizational_unit:
req.get_subject().OU = organizational_unit
if email_address:
req.get_subject().emailAddress = email_address
req.set_pubkey(key)
req.sign(key, 'sha256')
san_list = ["IP:" + common_name, "DNS:" + common_name]
req.add_extensions([
OpenSSL.crypto.X509Extension(
"subjectAltName".encode("utf-8"), False, (", ".join(san_list)).encode("utf-8")
)
])
private_key = OpenSSL.crypto.dump_privatekey(
OpenSSL.crypto.FILETYPE_PEM, key)
csr = OpenSSL.crypto.dump_certificate_request(
OpenSSL.crypto.FILETYPE_PEM, req)
return private_key, csr