尝试读取加密私钥时出现“块中没有DEK-Info头”。

huangapple go评论87阅读模式
英文:

"No DEK-Info header in block" when attempting to read encrypted private key

问题

我正在尝试读取一个加密的PKCS8私钥文件。我是这样生成密钥的:

openssl genrsa -out file.pem -passout pass:file -aes256 1024
openssl pkcs8 -topk8 -inform pem -in file.pem -outform pem -out filePKCS8.pem

然后我尝试用Go语言这样读取:

block, _ := pem.Decode(key)
return x509.DecryptPEMBlock(block, password)

但是我得到了一个错误:

x509: no DEK-Info header in block

然而,我无法弄清楚出了什么问题。是我生成密钥的方式有问题,还是我使用的库不对?我看到有一些专门用于读取未加密PKCS8文件的库,但没有专门用于加密PKCS8文件的库。

有人有任何想法吗?

英文:

I'm trying to read an encrypted PKCS8 private key file. I generated the keys like this:

openssl genrsa -out file.pem -passout pass:file -aes256 1024
openssl pkcs8 -topk8 -inform pem -in file.pem -outform pem -out filePKCS8.pem

And I try reading it in Go this way:

block, _ := pem.Decode(key)
return x509.DecryptPEMBlock(block, password)

But I get an error saying:

x509: no DEK-Info header in block

However, I can't figure out what's going wrong. Am I generating the key wrong or am I using the wrong library? I see libraries specifically for reading unencrypted PKCS8 files but none for encrypted PKCS8 files specifically.

Does anyone have any idea?

答案1

得分: 4

Go语言标准库中没有解密PKCS8密钥的功能。你可以使用这个包:https://github.com/youmark/pkcs8/blob/master/pkcs8.go#L103

英文:

Go don't have function to decrypt PKCS8 keys in standard library.

You can this package:
https://github.com/youmark/pkcs8/blob/master/pkcs8.go#L103

答案2

得分: 2

对于有相同问题的人,以下是更详细的解释。

有效的方法

你的第一个命令

openssl genrsa -out file.pem -passout pass:file -aes256 1024

生成了一个PKCS#1私钥文件(file.pem):

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC,1DA219DB746F88C6DDA0D852A0FD3232

AEf09rGkgGEJ79GgO4dEVsArwv4IbbODlxy95uHhfkdGYmuk6OlTpiCUE0GT68wn
KFJfBcHr8Z3VqiHGsXxM5QlKhgnfptxfbrdKErgBD5LQcrvnqmf43KeD4lGQcpiy
...
...
mAKMCwiU/GKZz8ZwQ4qGkBlVVCOFfgwmfbqguJF2l8yzM8lYI9MZ9NEwKkvEbc
-----END RSA PRIVATE KEY-----

这个私钥文件可以通过x509.DecryptPEMBlock()进行解析和解密。

无效的方法及原因

你的第二个命令

openssl pkcs8 -topk8 -inform pem -in file.pem -outform pem -out filePKCS8.pem

将该文件转换为PKCS#8格式(filePKCS8.pem)。

子命令genpkey可以直接产生类似的结果:

openssl genpkey -algorithm RSA -aes256 \
  -pkeyopt rsa_keygen_bits:1024 -out filePKCS8.pem

生成的filePKCS8.pem文件(无论哪种方式)看起来类似于:

-----BEGIN ENCRYPTED PRIVATE KEY-----
MIISrTBXBgkqhkiG9w0BBQ0wSjKpBgkqhkiG9w0BBQwwHAQIKL+ordsVfqsCAggB
MAwGCCqGSIb3DQIJCQAwHQYJYIZIWAUDBAEqBBCipOAAxWkC0/zkNLNYTSMgBIIS
...
...
zfdxjZ0XmPiwED2azsLMnRrWnRj2UqMtnv9zO/ucik9za
-----END ENCRYPTED PRIVATE KEY-----

x509.DecryptPEMBlock()不支持这种格式。正如#8860中指定的那样,Go的核心库在不久的将来没有真正支持pkcs#8的计划。

正如Gregory所提到的,如果你想使用它,你可以尝试使用第三方库,比如github.com/youmark/pkcs8文档)。

英文:

A longer explaination for anyone with the same problem.

What would work

Your first command

openssl genrsa -out file.pem -passout pass:file -aes256 1024

generates a PKCS#1 private key file (file.pem):

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC,1DA219DB746F88C6DDA0D852A0FD3232

AEf09rGkgGEJ79GgO4dEVsArwv4IbbODlxy95uHhfkdGYmuk6OlTpiCUE0GT68wn
KFJfBcHr8Z3VqiHGsXxM5QlKhgnfptxfbrdKErgBD5LQcrvnqmf43KeD4lGQcpiy
...
...
mAKMCwiU/GKZz8ZwQ4qGkBlVVCOFfgwmfbqguJF2l8yzM8lYI9MZ9NEwKkvEbc
-----END RSA PRIVATE KEY-----

This private key file can be parsed and decrypted by x509.DecryptPEMBlock() alright.

What would not work and why

Your second command

openssl pkcs8 -topk8 -inform pem -in file.pem -outform pem -out filePKCS8.pem

converts that file into PKCS#8 format (filePKCS8.pem).

The subcommmand genpkey would directly produce a similar result:

openssl genpkey -algorithm RSA -aes256 \
  -pkeyopt rsa_keygen_bits:1024 -out filePKCS8.pem

The generated filePKCS8.pem (either way) would look similar to this:

-----BEGIN ENCRYPTED PRIVATE KEY-----
MIISrTBXBgkqhkiG9w0BBQ0wSjKpBgkqhkiG9w0BBQwwHAQIKL+ordsVfqsCAggB
MAwGCCqGSIb3DQIJCQAwHQYJYIZIWAUDBAEqBBCipOAAxWkC0/zkNLNYTSMgBIIS
...
...
zfdxjZ0XmPiwED2azsLMnRrWnRj2UqMtnv9zO/ucik9za
-----END ENCRYPTED PRIVATE KEY-----

x509.DecryptPEMBlock() does not support this format. And as specified in #8860, the Go's core library has no real plan to support pkcs#8 in the near future.

As mentioned by Gregory, if you want to work with it, you'll have better luck with 3rd party library like github.com/youmark/pkcs8 (Documentation).

huangapple
  • 本文由 发表于 2015年10月7日 08:22:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/32981821.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定