[erlang-questions] public_key:pem_decode/1 and public_key:verify/4

Joakim G. <>
Mon Jan 24 09:26:25 CET 2011


Thanks. A nice addition to the public_key module.

/Jocke

On 2011-01-24 06:14, Seth Falcon wrote:
> Hi,
> 
> On Fri, Jan 21, 2011 at 12:48 AM, Ingela Andin <> wrote:
>> Well PEM-files that only BEGIN PUBLIC KEY and not BEGIN RSA PUBLIC
>> KEY are using another ASN1 spec and can contain both RSA and DSA
>> keys. It seems that it is the ASN1-spec SubjectPublicKeyInfo from
>> PKIXExplicit88.asn1 also part of public_key.
> 
> Thanks, that was a very useful hint :-)
> 
> I can now read RSA public key PEM files as generated by openssl.
> Here's the recipe (assuming stock public_key module):
> 
>     read_rsa_public_key(Key) ->
>         Bin = erlang:iolist_to_binary(public_key_lines(re:split(Key,
> "\n"), [])),
>         Spki = public_key:der_decode('SubjectPublicKeyInfo',
> base64:mime_decode(Bin)),
>         {_, _, {0, KeyDer}} = Spki,
>         public_key:der_decode('RSAPublicKey', KeyDer).
> 
>     public_key_lines([<<"-----BEGIN PUBLIC KEY-----">>|Rest], Acc) ->
>         public_key_lines(Rest, Acc);
>     public_key_lines([<<"-----END PUBLIC KEY-----">>|Rest], Acc) ->
>         lists:reverse(Acc);
>     public_key_lines([Line|Rest], Acc) ->
>         public_key_lines(Rest, [Line|Acc]).

<trunc/>


More information about the erlang-questions mailing list