加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell – 如何拆分PEM文件

发布时间:2020-12-15 18:44:41 所属栏目:安全 来源:网络整理
导读:注意:这不是一个真正的问题,因为我已经找到了答案,但由于我没有在这里轻易找到它,我会发布它以便它可以使其他人受益. 问题:如何读取连接的PEM文件作为apache / mod_ssl指令SSLCACertificateFile 使用的文件? 答案(原件)(source): cat $file|awk 'split_a
注意:这不是一个真正的问题,因为我已经找到了答案,但由于我没有在这里轻易找到它,我会发布它以便它可以使其他人受益.

问题:如何读取连接的PEM文件作为apache / mod_ssl指令SSLCACertificateFile 使用的文件?

答案(原件)(source):

cat $file|awk 'split_after==1{n++;split_after=0} /-----END CERTIFICATE-----/ {split_after=1} {print > "cert" n ".pem"}'

如果末尾有一个空行,这可能会留下一个空文件,例如openssl pkcs7 -outform PEM -in my-chain-file -print_certs.为防止这种情况,请在打印前检查线条的长度:

cat $file|awk 'split_after==1{n++;split_after=0}
   /-----END CERTIFICATE-----/ {split_after=1}
   {if(length($0) > 0) print > "cert" n ".pem"}'

回答29/03/2016:

在@slugchewer answer之后,csplit可能是一个更清晰的选择:

csplit -f cert- $file '/-----BEGIN CERTIFICATE-----/' '{*}'
awk片段用于提取不同的部分,但您仍需要知道哪个部分是密钥/证书/链.
我需要提取一个特定的部分,并在OpenSSL邮件列表中找到它: http://openssl.6102.n7.nabble.com/Convert-pem-to-crt-and-key-files-tp47681p47697.html
# Extract key
openssl pkey -in foo.pem -out foo-key.pem

# Extract all the certs
openssl crl2pkcs7 -nocrl -certfile foo.pem |
  openssl pkcs7 -print_certs -out foo-certs.pem

# Extract the textually first cert as DER
openssl x509 -in foo.pem -outform DER -out first-cert.der

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读