메모리에있는 개체로 X509 인증서를 가지고 pem 파일로 저장 한 다음 다시로드하면 동일한 것으로 끝날 것입니다. 나는 처음부터 시작했다. 그러나 이것은 사실이 아닌 것처럼 보입니다. A.as_text()는 B.as_text()와 동일하지만 A.as_pem()은 B.as_pem()과 다릅니다. 최소한을 말하면, 나는 이것으로 혼란스러워합니다. 부수적으로 A가 다른 엔티티 C가 서명 한 경우 A는 C의 인증서와 대조하지만 B는 그렇지 않습니다.M2Crypto를 사용하여 X509 인증서를 pem 파일로 저장 및로드
나는 내가 본 것을 보여주기 위해 작은 샘플 프로그램을 만들었습니다. 이걸 실행하면 두 번째 RuntimeError가 발생합니다.
감사합니다,
브록
#!/usr/bin/python2.6
import M2Crypto as m2
import time
cur_time = m2.ASN1.ASN1_UTCTIME()
cur_time.set_time(int(time.time()) - 60*60*24)
expire_time = m2.ASN1.ASN1_UTCTIME()
# Expire certs in 1 hour.
expire_time.set_time(int(time.time()) + 60 * 60 * 24)
cs_rsa = m2.RSA.gen_key(1024, 65537, lambda: None)
cs_pk = m2.EVP.PKey()
cs_pk.assign_rsa(cs_rsa)
cs_cert = m2.X509.X509()
# These two seem the minimum necessary to make the as_text function call work
# at all
cs_cert.set_not_before(cur_time)
cs_cert.set_not_after(expire_time)
# This seems necessary to fill out the complete cert without errors.
cs_cert.set_pubkey(cs_pk)
# I've tried with the following set lines commented out and not commented.
cs_name = m2.X509.X509_Name()
cs_name.C = "US"
cs_name.ST = "CA"
cs_name.OU = "Fake Org CA 1"
cs_name.CN = "www.fakeorg.dex"
cs_name.Email = "[email protected]"
cs_cert.set_subject(cs_name)
cs_cert.set_issuer_name(cs_name)
cs_cert.sign(cs_pk, md="sha256")
orig_text = cs_cert.as_text()
orig_pem = cs_cert.as_pem()
print "orig_text:\n%s" % orig_text
cs_cert.save_pem("/tmp/foo")
tcs = m2.X509.load_cert("/tmp/foo")
tcs_text = tcs.as_text()
tcs_pem = tcs.as_pem()
if orig_text != tcs_text:
raise RuntimeError(
"Texts were different.\nOrig:\n%s\nAfter load:\n%s" %
(orig_text, tcs_text))
if orig_pem != tcs_pem:
raise RuntimeError(
"Pems were different.\nOrig:\n%s\nAfter load:\n%s" %
(orig_pem, tcs_pem))
파일에 tcs를 저장하려고 했습니까? cs_cert와 정확히 같은 파일이 있습니까? 첫 번째 save_pem에서 동일한 파일을 가지고 있으며 일종의 데이터 변환이 이루어졌습니다. 두 개의 파일이 다르면 OpenSSL 유틸리티 또는 CertUtil.exe -dump를 사용하여 파일 포함을 덤프하고 동일한 인증서의 두 버전에서 다른 내용을 볼 수 있습니다. – Oleg
나는 파일에 tcs를 저장하려고 애썼지 않았다. tcs는 이미 cs에 서명 한 인증서를 검증하지 않았으므로 (이 예제가 아니라 원래이 문제가 발생했음을 알 수있는 원본 코드에 있음), tcs를 다시 파일로 저장하면 실제로 도움이되지 않습니다. 어떤 경우 든, pem 파일과 x509 certs에 관해 읽은 모든 것에서, 다른 pem 표현을 가진 certs는 다른 텍스트 표현을 가져야합니다. 또한이 예에서 certs의 TEXT가 동일하다는 것을주의하십시오 (m2 및 openssl에 따라). 그러나 pem 파일은 그렇지 않습니다. 그러므로 내 혼란. – bpytlik