I generate private rsa key and certificate files using this command in shell:
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days XXX -nodes
Now i try to convert these files to rsa public and private key. I use the code below but does not work. I think the PrivateKey
variable must be #RSAPrivateKey{}
according to http://erlang.org/doc/apps/public_key/using_public_key.html but it is #PrivateKeyInfo{}
.
How can i generate ras public and private key from key.pem and cert.pem?
erlang shell:
1> {ok, PemBin} = file:read_file("key.pem").
{ok,<<"-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDMd0dnMS9t27wo\nhloldtGYbT3C/uR"...>>}
2> [RSAEntry] = public_key:pem_decode(PemBin).
[{'PrivateKeyInfo',<<48,130,4,189,2,1,0,48,13,6,9,42,134,
72,134,247,13,1,1,1,5,0,4,130,4,167,
...>>,
not_encrypted}]
3> PrivateKey = public_key:pem_entry_decode(RSAEntry).
{'PrivateKeyInfo',v1,
{'PrivateKeyInfo_privateKeyAlgorithm',{1,2,840,113549,1,1,1},
{asn1_OPENTYPE,<<5,0>>}},
<<48,130,4,163,2,1,0,2,130,1,1,0,204,119,71,103,49,47,109,
219,188,40,134,90,37,...>>,
asn1_NOVALUE}
4> {ok, CertBin} = file:read_file("cert.pem").
{ok,<<"-----BEGIN CERTIFICATE-----\nMIIDVzCCAj+gAwIBAgIJAKBDxdUZ8v9/MA0GCSqGSIb3DQEBCwUAMEIxCzAJBgNV\nBAYTAlhYMRUwEwY"...>>}
5> [CertEntry] = public_key:pem_decode(CertBin).
[{'Certificate',<<48,130,3,87,48,130,2,63,160,3,2,1,2,2,9,
0,160,67,197,213,25,242,255,127,48,13,
...>>,
not_encrypted}]
6> Cert = public_key:pem_entry_decode(CertEntry).
{'Certificate',{'TBSCertificate',v3,11548291388562145151,
{'AlgorithmIdentifier',{1,2,840,113549,1,1,11},<<5,0>>},
{rdnSequence,[[{'AttributeTypeAndValue',{2,5,4,6},
<<19,2,88,88>>}],
[{'AttributeTypeAndValue',{2,5,4,7},<<"\f\fDefault City">>}],
[{'AttributeTypeAndValue',{2,5,4,10},
<<12,19,68,101,102,97,117,108,116,32,67,111,109,...>>}]]},
{'Validity',{utcTime,"161226221244Z"},
{utcTime,"170125221244Z"}},
{rdnSequence,[[{'AttributeTypeAndValue',{2,5,4,6},
<<19,2,88,88>>}],
[{'AttributeTypeAndValue',{2,5,4,7},<<"\f\fDefault City">>}],
[{'AttributeTypeAndValue',{2,5,4,10},
<<12,19,68,101,102,97,117,108,116,32,67,...>>}]]},
{'SubjectPublicKeyInfo',{'AlgorithmIdentifier',{1,2,840,
113549,1,1,1},
<<5,0>>},
<<48,130,1,10,2,130,1,1,0,204,119,71,103,49,47,109,...>>},
asn1_NOVALUE,asn1_NOVALUE,
[{'Extension',{2,5,29,14},
false,
<<4,20,9,99,232,184,104,132,196,200,55,...>>},
{'Extension',{2,5,29,35},
false,
<<48,22,128,20,9,99,232,184,104,132,...>>},
{'Extension',{2,5,29,19},false,<<48,3,1,1,255>>}]},
{'AlgorithmIdentifier',{1,2,840,113549,1,1,11},<<5,0>>},
<<96,39,63,51,19,154,132,69,252,134,229,148,80,40,135,23,
44,230,150,154,106,53,135,0,68,...>>}
Finally i found answer of my question. The private key and public key can be extracted from pem files using these functions: