MacOS下用OpenSSL解码p7m文件报错,asn1parse可正常解析
解决Mac OS X下openssl smime解码p7m文件的ASN.1解析错误
问题描述
在Mac OS X系统中尝试解码p7m格式的XML文件,执行命令:
openssl smime -decrypt -in "file.xml.p7m" -inform DER -verify -noverify -out "file.xml"
返回错误:
Error reading S/MIME message C09D9E0C01000000:error:068000A8:asn1 encoding routines:asn1_check_tlen:wrong tag:crypto/asn1/tasn_dec.c:1188: C09D9E0C01000000:error:0688010A:asn1 encoding routines:asn1_item_embed_d2i:nested asn1 error:crypto/asn1/tasn_dec.c:349:Type=PKCS7_ISSUER_AND_SERIAL C09D9E0C01000000:error:0688010A:asn1 encoding routines:asn1_template_noexp_d2i:nested asn1 error:crypto/asn1/tasn_dec.c:685:Field=issuer_and_serial, Type=PKCS7_SIGNER_INFO C09D9E0C01000000:error:0688010A:asn1 encoding routines:asn1_template_noexp_d2i:nested asn1 error:crypto/asn1/tasn_dec.c:654:Field=signer_info, Type=PKCS7_SIGNED C09D9E0C01000000:error:0688010A:asn1 encoding routines:asn1_template_noexp_d2i:nested asn1 error:crypto/asn1/tasn_dec.c:685: C09D9E0C01000000:error:0688010A:asn1 encoding routines:asn1_template_ex_d2i:nested asn1 error:crypto/asn1/tasn_dec.c:537:Field=d.sign, Type=PKCS7
但执行openssl asn1parse -inform der -in "file.xml.p7m"命令时,可正常解析该文件。
解决方法
1. 切换命令参数(针对签名型p7m文件)
报错指向PKCS7_SIGNER_INFO,说明该p7m是签名文件(SignedData),而非加密的EnvelopedData,不需要用-decrypt参数。改用验证提取命令:
openssl smime -verify -in "file.xml.p7m" -inform DER -noverify -out "file.xml"
2. 确认文件结构
用带缩进的asn1parse输出确认PKCS7类型:
openssl asn1parse -inform der -in "file.xml.p7m" -i
- 若输出中出现
:pkcs7-signedData,则为签名文件,用上述-verify命令即可。 - 若出现
:pkcs7-envelopedData,则为加密文件,需确保解密密钥正确,可补充-inkey your_private_key.pem参数重新执行解密命令。
3. 转换格式后处理
部分p7m文件为BER编码(非严格DER),先转成PEM格式再操作:
# 转PEM格式 openssl asn1parse -inform der -in "file.xml.p7m" -out "file.p7m.pem" -outform PEM # 签名文件验证提取 openssl smime -verify -in "file.p7m.pem" -noverify -out "file.xml" # 加密文件解密(需密钥时加-inkey参数) openssl smime -decrypt -in "file.p7m.pem" -noverify -out "file.xml"
4. 更新OpenSSL版本
Mac自带的OpenSSL版本可能较旧,对非标准PKCS7结构支持不足。用Homebrew安装新版:
brew install openssl
使用新版命令路径重新执行操作(例如/usr/local/opt/openssl/bin/openssl smime ...)。
内容的提问来源于stack exchange,提问作者wlj
相关产品推荐
相关产品推荐

