[python] M2Crypto를 사용하여 X.509 인증서를 생성하는 방법은 어떻게 되나요?
  1. 개인 키 및 CSR(Certificate Signing Request) 생성:
from M2Crypto import RSA, X509

# RSA 키 생성
rsa_key = RSA.gen_key(2048, 65537)

# RSA 키로 X.509 CSR 생성
req = X509.Request()
req.set_pubkey(rsa_key)
req.set_version(2)
req.sign(rsa_key, 'sha256')

# CSR 저장
with open('csr.pem', 'wb') as f:
    f.write(req.as_pem())
  1. 자체 서명된 X.509 인증서 생성:
# X.509 인증서 생성
cert = X509.X509()
cert.set_version(2)
cert.set_serial_number(1)
cert.set_subject(req.get_subject())
cert.gmtime_adj_notBefore(0)
cert.gmtime_adj_notAfter(31536000)  # 인증서 유효기간: 1년
cert.set_pubkey(req.get_pubkey())
cert.sign(rsa_key, 'sha256')

# 인증서 저장
with open('self_signed_cert.pem', 'wb') as f:
    f.write(cert.as_pem())

위의 코드를 실행하면 개인 키, CSR 및 자체 서명된 X.509 인증서가 생성됩니다. 이제 이러한 개체를 사용하여 SSL/TLS 연결을 설정하거나 인증된 통신에 사용할 수 있습니다.

참고 문헌:

위 예제는 M2Crypto v0.37.1에서 작동함을 확인하였습니다. M2Crypto 버전에 따라 코드가 다를 수 있습니다.