0

Follow-up to this one.

I got the reading/decoding working

1> {ok, F} = file:read_file("inaimathi.rsapub").
{ok,<<"-----BEGIN RSA PUBLIC KEY-----\nmQINBE9NBIQBEADMSzN6b0FaPP0rGiLDWKfH4ehN66Z0SAIynXm6lBHjmO69pNsm\niIe4p1X9aXhr"...>>}
2> [Entry] = public_key:pem_decode(F).
[{'RSAPublicKey',<<153,2,13,4,79,77,4,132,1,16,0,204,75,
                   51,122,111,65,90,60,253,43,26,34,195,
                   88,167,...>>,
                 not_encrypted}]

According to the docs, the last thing I have to do in order to get a working public key out of this is run public_key:pem_entry_decode/1 on that Entry. However, when I try to do that, I get an eror.

3> public_key:pem_entry_decode(Entry).
** exception error: no match of right hand side value 
                    {error,
                        {asn1,
                            {{badmatch,{error,{asn1,{wrong_tag,{131097,16}}}}},
                             [{'OTP-PUB-KEY',dec_RSAPublicKey,2,
                                  [{file,"OTP-PUB-KEY.erl"},{line,5956}]},
                              {'OTP-PUB-KEY',decode,2,
                                  [{file,"OTP-PUB-KEY.erl"},{line,493}]},
                              {public_key,der_decode,2,
                                  [{file,"public_key.erl"},{line,166}]},
                              {erl_eval,do_apply,6,
                                  [{file,"erl_eval.erl"},{line,576}]},
                              {shell,exprs,7,[{file,"shell.erl"},{line,668}]},
                              {shell,eval_exprs,7,
                                  [{file,"shell.erl"},{line,623}]},
                              {shell,eval_loop,3,
                                  [{file,"shell.erl"},{line,608}]}]}}}
     in function  public_key:der_decode/2 (public_key.erl, line 170)

What am I doing wrong here?

EDIT: Didn't think it would matter, but someone asked, so.

I'm running Debian Wheezy on a Core i3 with Erlang installed from a ~2 day old checkout of this.

erl --version says

Erlang R15B02 (erts-5.9.2) [source] [64-bit] [smp:4:4] [async-threads:0] [hipe] [kernel-poll:false]
Community
  • 1
  • 1
Inaimathi
  • 13,853
  • 9
  • 49
  • 93

1 Answers1

5

Your code works fine here:

decode() ->
    [application:start(X) || X <- [crypto, public_key, ssl]],
    RawData = ["-----BEGIN PUBLIC KEY-----\n",
               "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBm8yuHmd0P6scl48DEi+xp47w\n",
               "XVZaKWRygGKtA2XkdRuCU99f0Tq07Llcgf8XuR+Wnk+z2CdMMFMzOGhCePblVIAn\n",
               "33dcBVlDokpBF7AnTClsaLcixxZw1LIUiaPaBdN7oG8vt3G2caLHRrrkoEnccY+6\n",
               "GadfH7iuHdcVsz1mowIDAQAB\n",
               "-----END PUBLIC KEY-----"],
    D = iolist_to_binary(RawData),
    [Entry] = public_key:pem_decode(D),
    public_key:pem_entry_decode(Entry).

Generates output (shortened):

 {'RSAPublicKey',135956...,65537}
I GIVE CRAP ANSWERS
  • 18,739
  • 3
  • 42
  • 47
  • 1
    This code works on my end as well. I guess this means there's some encoding problem with the key I'm trying to decode? How did you generate the one you use as an example? – Inaimathi Jun 10 '12 at 21:08
  • I just stole it off of some random web page :) – I GIVE CRAP ANSWERS Jun 12 '12 at 15:22
  • 1
    Just thought I'd comment for the benefit of googlers; this seems to work fine with keys generated using OpenSSL or M2Crypto. It does *not* seem to work with keys generated with OpenSSH/GnuPG and converted to PEM format (though there is a `public_key:ssh_decode/2` that does work with OpenSSH public keys). – Inaimathi Jun 18 '12 at 14:18
  • Do you really have to do [application:start(X) || X <- [crypto, public_key, ssl]] first? – spc16670 Sep 12 '16 at 10:10
  • When I wrote this in '12 I think you needed that. Nowadays, I'm not too sure. You can do `application:ensure_all_started(public_key)` but preferably not in every decode call you are going to make. The best solution is to handle application dependencies as part of your release. – I GIVE CRAP ANSWERS Sep 17 '16 at 08:41