pem 证书中的私钥如何加密? [英] How is a private key encrypted in a pem certificate?

查看:136
本文介绍了pem 证书中的私钥如何加密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为尝试调试问题的一部分,我试图了解如何在 pem 证书中加密私钥,因为我想知道 curl 是否没有设法解密私钥.

As part of trying to debug an issue, I am trying to understand how a private key is encrypted in a pem certificate, because I am wondering whether curl does not manage to decrypt the private key.

我的 pem 中有一个 -----BEGIN ENCRYPTED PRIVATE KEY----- 部分.

I have a -----BEGIN ENCRYPTED PRIVATE KEY----- section in my pem.

是否使用密码加密?是否涉及其他类型的加密方案?

Is it encrypted with the passphrase? Is there some other kind of encryption scheme involved?

更准确

一位同事建议即使没有密码短语也可以在 pem 中加密私钥.这是正确的吗?

A colleague suggests a private key could be encrypted in a pem even without a passphrase. Is this correct?

推荐答案

私钥可以有几种不同的记录形式,但最常见的形式是 PKCS#8,定义在 RFC 5208.

Private keys can have a couple of different forms of being written down, but the most common form is PKCS#8, defined in RFC 5208.

RFC 定义了两种形式的结构.

The RFC defines two forms of structure.

PrivateKeyInfo ::= SEQUENCE {
  version                   Version,
  privateKeyAlgorithm       PrivateKeyAlgorithmIdentifier,
  privateKey                PrivateKey,
  attributes           [0]  IMPLICIT Attributes OPTIONAL }

这个结构的版本号,以防它演变",如何确定使用什么解析器来读取私钥",一些东西,希望你能读懂",一些关于这个结构的数据东西,也许".

"The version number for this structure, in case it evolves", "how to identify what parser to use to read privateKey", "some stuff, hope you can read it", "some data about the stuff, maybe".

当 PEM 编码 PrivateKeyInfo 时,会获得标签PRIVATE KEY",如BEGIN PRIVATE KEY".您没有其中之一.

When PEM encoded a PrivateKeyInfo gets the tag "PRIVATE KEY", as in "BEGIN PRIVATE KEY". You don't have one of these.

另一种形式是

EncryptedPrivateKeyInfo ::= SEQUENCE {
  encryptionAlgorithm  EncryptionAlgorithmIdentifier,
  encryptedData        EncryptedData }

我如何加密东西",加密的东西".

"How I encrypted stuff", "the encrypted stuff".

EncryptedPrivateKeyInfo 带有 PEM 标签ENCRYPTED PRIVATE KEY",这就是您所拥有的.

EncryptedPrivateKeyInfo comes with the PEM tag "ENCRYPTED PRIVATE KEY", which is what you have.

研究其中之一:

$ openssl asn1parse -i -dump < rsa.enc.p8
    0:d=0  hl=4 l= 710 cons: SEQUENCE
    4:d=1  hl=2 l=  64 cons:  SEQUENCE
    6:d=2  hl=2 l=   9 prim:   OBJECT            :PBES2
   17:d=2  hl=2 l=  51 cons:   SEQUENCE
   19:d=3  hl=2 l=  27 cons:    SEQUENCE
   21:d=4  hl=2 l=   9 prim:     OBJECT            :PBKDF2
   32:d=4  hl=2 l=  14 cons:     SEQUENCE
   34:d=5  hl=2 l=   8 prim:      OCTET STRING
      0000 - e9 37 68 99 cb 9c 4f 10-                          .7h...O.
   44:d=5  hl=2 l=   2 prim:      INTEGER           :0800
   48:d=3  hl=2 l=  20 cons:    SEQUENCE
   50:d=4  hl=2 l=   8 prim:     OBJECT            :des-ede3-cbc
   60:d=4  hl=2 l=   8 prim:     OCTET STRING
      0000 - 16 ad ce 41 47 e8 ba 85-                          ...AG...
   70:d=1  hl=4 l= 640 prim:  OCTET STRING
     <data_omitted />

数据是在基于密码的加密方案 2 (PBES2).输入的密码/密码通过基于密码的密钥派生函数 2 转换为密钥材料(PBKDF2) 和盐(在加密时随机选择,但解密时必须相同)e9 37 68 99 cb 9c 4f 10 并使用 2048 (0x800) 的迭代次数.该密钥用于 CBC 模式下的 TripleDES 加密,其 IV(在加密时随机选择,但必须与解密相同)为 16 ad ce 41 47 e8 ba 85.加密内容为640字节,解析为PrivateKeyInfo结构.

The data was encrypted under Password-Based Encryption Scheme 2 (PBES2). The input password/passphrase is transformed into key material by Password-Based Key Derivation Function 2 (PBKDF2) with the salt (randomly chosen at encryption time, but must be the same to decrypt) e9 37 68 99 cb 9c 4f 10 and using an iteration count of 2048 (0x800). The key was used for TripleDES encryption in CBC mode with an IV (randomly chosen at encryption time, but must be the same to decrypt) of 16 ad ce 41 47 e8 ba 85. The encrypted content is 640 bytes, and will then be parsed as a PrivateKeyInfo structure.

除了 PKCS#8 之外,传输私钥的唯一真正的其他选择是 PKCS#12/PFX,但该数据结构没有标准的 PEM 表示.最新版本的 PKCS#12 允许以 EnvelopedCMS/PKCS#7 的样式将数据加密为证书.

Aside from PKCS#8 the only real other choice for transmitting private keys is PKCS#12/PFX, but that data structure doesn't have a standard PEM representation. The newest version of PKCS#12 allows for encrypting the data to a certificate, in the style of EnvelopedCMS/PKCS#7.

所以,对于一些简洁的答案:

So, for some succinct answers:

  • ENCRYPTED PRIVATE KEY 表单已加密.
    • 99.999% 的可能性是使用密码加密.
    • 但有人可能已经使用密码以外的其他东西向 OpenSSL 教授了 PBES2 KDF :).
    • RSA PRIVATE KEY 表单也未加密,但它是 PKCS#1 RSAPrivateKey,而不是 PKCS#8 PrivateKeyInfo.
    • The RSA PRIVATE KEY form is also not encrypted, but that's a PKCS#1 RSAPrivateKey, not a PKCS#8 PrivateKeyInfo.

    这篇关于pem 证书中的私钥如何加密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆