有没有办法用python修改PEM编码的x509证书的签名?
我曾尝试使用加密python模块,但似乎无法设置x509签名。我只能得到它的价值。是否还有另一个python模块可能对此更好用?
加密python模块文档在这里:https://cryptography.io/en/latest/x509/reference/#x-509-certificate-object
from cryptography import x509
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.serialization import Encoding
import os
#change the signature of the cert
def change_cert_sig(pem_data):
cert = x509.load_pem_x509_certificate(pem_data, default_backend())
#the line below works
print(cert.signature)
#the line below causes an exception "AttributeError: can't set attribute"
cert.set_signature(os.urandom(len(cert.signature))) #set signature to random bytes
return cert.public_bytes(Encoding.PEM)
有没有办法用python修改PEM编码的x509证书的签名?
是
我不是Python专家,所以我只能描述该怎么做...将证书读入内存。这是来自RFC 5280, Appendix A, p. 116 (and friends)的证书的ASN.1结构:
Certificate ::= SEQUENCE {
tbsCertificate TBSCertificate,
signatureAlgorithm AlgorithmIdentifier,
signature BIT STRING }
tbsCertificate
是颁发者(或证书颁发机构)签署的内容。 “ TBS”表示“待签名。” signature
是发行人在tbsCertificate
上的签名。 signatureAlgorithm
描述了使用的签名算法,例如sha256WithRSAEncryption
。
跳到第三个属性,即第一个signature BIT STRING
。前4个八位位组是BIT_STRING
的ASN.1编码的一部分。八位字节5-37是签名字节。拿一个签名字节并篡改它。类似Byte b = data[6]
,b ^= 0x01
,然后是data[6] = b
。
由于signature
是证书中的最后一项,因此您应该可以篡改文件中的最后32个字节。最后的32个字节是签名。 (32字节假定使用SHA-256进行签名)。