how can I decrypt "ENCRYPTED PRIVATE KEY" using golang

4,481 views
Skip to first unread message

terry...@gmail.com

unread,
Aug 11, 2017, 9:31:37 AM8/11/17
to golang-nuts
My private key is generated by "openssl req -new -x509 -keyout a.key -out a.crt -days 3650" with password.

And I tried to decrypted it by x509.DecryptPEMBlock(keyBlock, password), but failure.  error message: %!(EXTRA *errors.errorString=x509: no DEK-Info header in block)

my privatekey is in below, and password is '123456'

anyone else can help me ? 


-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIQgjjAaLRMbsCAggA
MBQGCCqGSIb3DQMHBAi4V74SzJ52kwSCBMiZ7gONJxBWnJsqaiyhDBvLrMQW5c/G
qdoGCChEHrKV2ndr0MKTJM5xuGUk+dtrAbezosFBb+6511HOzBReM6beQ+bEPRUe
zV7GQYozeCt7+Be/vM+PmcV9s7C6MsBD2iHV6An7TeVSbJMJWJxyXT7jI7w+N7Or
dS0SUSlCGBd0IoI+cMPyGdNLJat3ZaoNPKnM7R3/0dmSk0AkEF7Kdu83m6oByNZy
dlXEOSffeSfgN8g/CX2Sih1Bx1646WUZ3VzWWPbJxsg7Hr34TfDA/+uU/MtGi9AL
W1gfaTpj5Lsz1h6Gtw/0GVLoeloJj/I28P/aWHq26wfxbSo03gQkFaPedZh+M1Rp
75VGrx10MwM4a669P7suUMPAiPWk8fD5m8xinchcp82EuH0YINtjKXOgpvETQj3X
qDyZuxfDShJ899dRRB6NK1cdKcjXoofrYdIREBFrfOZbgEZRIozLhgp5/1B8f1Cp
sD+sqhH/7uhJQLFQ8ce9BlnsooURqe0z7sCQd+nQT93TvnRTJsiHt3bzz+UPN7pz
WHCTZ5Hq5uwswfUqszVGRX/LbVYeXSSCkzzBBKhB8jQPxokklyrH4QkDcupy7HP1
m2/S1I0H5662nv0BOFrssMQVYBtGWThVrskaJccyA4GSiMX4f1XjtXku6jzphkvO
PHnCFMfpYsV8hDWOyULZacdkKxN5BO/M45qmTxrNKnSKtI4bbjO3AG0uE/3SmhK2
RjUXbX/rXKTfcHb4F2Ur4cMb6mMBzZqA92vw2aZLr49DQERuhaCmgcIBAB27fO7X
Umg/DLyB0Qj0X02Pc/BPBMfdLl2nBxcBkKUlRqt01jkINwiMed161G3rf7HbzHiT
Y41VfWlocc5iYxKnKFUVLCPHBwHAx5oIG4Wsa9REmzvtssBOt/4bts25ZVsU/MDa
ic4cX1wL65BwxUBmD2XUifB+107vF9gbIz4U/nd5DkfeKoXxAM+kdpoS5+Th1rGn
7NHJMP658bwDI4le7Z5snrWWm8fZCVmWJKQPE5bklImuJic7PW8+nHLNhhzArGnf
1RXWS1imGNTJBEsxwc93EPc+h6N3laXusuZrNdRKNnGWvpZpGxEPAXrX+BtPzgNj
EOJyDn/VXMV0lUXKVKjlFDmmuvB5oquq/j2LGze6xbgDWOI7wHP3DWmLw/RhNrTo
DWGg4n3pnU9yb1vEhM/F8JF/HgjiDeAn6VMWNoVYlgsfkWrHC4oTHp3dywg2LDw7
4CgoD+XIU4ogYqxOCKhk5N71P29Jvrwj/eiwH+H2QSDlyeLR0CcEH2YxoGmwVdKc
S6bSvWuxCAcxOA4/k8d77aGvobGOkzFtYmKEbxgqUXgtrSfhIIijICV69vWVQjW9
7EGJVg1Vz/niGt/Mj6iPX84sBxt8Y/xiEfWas0rvEUf1osJK8lXrobIh5LfwQN7T
0CGUhA03Sbk4x5BkXHjBEaWmqf17GLlVnveV+sMLcFt6QhLcn1ZVcMFNJdsRzWne
2z0suse9iDROUVnRDIuixodjMoT5pWaFOoSCzhlVZO8OXzp6VjK5rD/3Kgagw0Gd
HgCa9oxd3Jf7qndTxbBx2uwrAkK26NXBMTj291Z9y3+KdnBGGAVBc3M3wf5PrWM4
ed4=
-----END ENCRYPTED PRIVATE KEY-----

Konstantin Khomoutov

unread,
Aug 11, 2017, 11:05:56 AM8/11/17
to terry...@gmail.com, golang-nuts
On Fri, Aug 11, 2017 at 12:30:56AM -0700, terry...@gmail.com wrote:

> My private key is generated by "openssl req -new -x509 -keyout a.key -out
> a.crt -days 3650" with password.
>
> And I tried to decrypted it by x509.DecryptPEMBlock(keyBlock, password),
> but failure. error message: %!(EXTRA *errors.errorString=x509: no DEK-Info
> header in block)
>
> my privatekey is in below, and password is '123456'
>
> anyone else can help me ?
>
> -----BEGIN ENCRYPTED PRIVATE KEY-----
> MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIQgjjAaLRMbsCAggA
> MBQGCCqGSIb3DQMHBAi4V74SzJ52kwSCBMiZ7gONJxBWnJsqaiyhDBvLrMQW5c/G
[...]

As you can see in [1], typically a PEM-encoded encrypted key indeed
contains a header block detailing how it was encoded, like this:

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,02306CD22AAC94CD
...

As you can gather from the accepted answer in [1], `openssl req`
uses DES-EDE3-CBC.

I don't know what to do but I see two ways to attack this:

* Decode the PEM (it's just a base64 encoding of a DER-encoded stream
IIRC) and then try decoding it with some API function which allows
specifying the encryption type directly.

* Try monkey-patching the PEM data by a bogus DEK-Info header.

1. https://security.stackexchange.com/q/93417

Lucio

unread,
Aug 17, 2017, 3:11:12 AM8/17/17
to golang-nuts


On Friday, 11 August 2017 15:31:37 UTC+2, Terry An wrote:
My private key is generated by "openssl req -new -x509 -keyout a.key -out a.crt -days 3650" with password.

It seems you have a PKCS8 encrypted key, which needs somewhat special treatment from the Go crypto library.

You need to pem-decode it, then decrypt it (I'm not yet sure how) to DER, then submit it to x509.parsePKCS8PrivateKey. At least, that is what it seems to me. As soon as I have familiarised myself with the decryption aspect, I'll be able to tell you more.

You may want to use OpenSSL to re-arrange things so the encryption occurs in a different place, or convert the key from PKCS8 to RSA and encrypt that.

And, just maybe, the ParsePKCS8PrivateKey function in the x509 package could be extended in the most appropriate fashion to provide for decryption.

Lucio.

PS: I found a lot of useful hints in the manner in which crypto objects are handled in the crypto/acme nd crypto/acme/autocert packages.

Lucio

unread,
Aug 17, 2017, 6:28:07 AM8/17/17
to golang-nuts


On Thursday, 17 August 2017 09:11:12 UTC+2, Lucio wrote:


On Friday, 11 August 2017 15:31:37 UTC+2, Terry An wrote:
My private key is generated by "openssl req -new -x509 -keyout a.key -out a.crt -days 3650" with password.

It seems you have a PKCS8 encrypted key, which needs somewhat special treatment from the Go crypto library.

I did a bit more investigating and found this issue: 

crypto/x509: no support for parsing encrypted PKCS8 private keys #8860


I don't quite know enough to figure out for myself whether lack of support for BER (as opposed to DER) in the "encoding/asn1" standard library is a show stopper. If it is, any attempts to use encrypted PKCS8 will be doomed as the effort involved is significant.

Maybe it is possible to add an "encoding/asn1/ber" extension, but my ASN.1 knowledge has slipped considerably since I stopped working in that environment.

Lucio.

Reply all
Reply to author
Forward
0 new messages