I need to verify an ECDSA signature (found in an X509 CSR).

At first glance, Crypt::OpenSSL::ECDSA::ECDSA_do_verify should do the job.

I have a digest and a signature. I have the public key as a PEM (or DER) string. I know the OID of the curve. I'm missing a couple of pieces.

It appears that do_verify wants a Crypt::OpenSSL::EC_KEY object. But I don't see a method to import the DER into an EC.

Also, it appears that EC_KEY_new_by_curve_name wants an OpenSSL nid; I don't see a method to convert an OID (1.2.3.4...) to a nid (which is an index, not a name.)

Decoding the public key (e.g. using Convert::ASN1) and creating a key from the pieces seems impractical - given that I don't know what curve is in a given public key.

The more I look, the deeper I seem to sink :-)

Someone must have been here before. Any clue on how to get there from here? I'm not fixated on Crypt::OpenSSL::ECDSA, but I DON'T want to fork a process to run the OpenSSL command line.

I've gotten this far (omitting the obvious):

my $ecdsa = Crypt::OpenSSL::ECDSA::ECDSA_SIG->new(); $ecdsa->set_r( $sig->{r} ); $ecdsa->set_s( $sig->{s} ); my $nid = ???( '1.2.3.4' ); # Extracted from CSR my $key = Crypt::OpenSSL::EC::EC_KEY::new_by_curve_name( $nid); my $asn1PublicKey = "....I have this"; ???$asn1PublicKey = asn1_to_EC_POINT( $asn1PublicKey ); # How? Crypt::OpenSSL::EC::EC_KEY::set_public_key( $key,$asn1PublicKey ) or croak( "Failed to create key" ); my $verify = Crypt::OpenSSL::ECDSA::ECDSA_do_verify( sha256( $certificationRequest ), $ecdsa, $key ); # The equivalent for RSA is trivial: my $key = Crypt::OpenSSL::RSA->new_public_key( $asn1PublicKey ); $key->use_sha256_hash; $key->use_pkcs1_padding; $verify = $key->verify( $certificationRequest, $signature ); # DSA is similar

Thanks in advance for any help!

This communication may not represent my employer's views, if any, on the matters discussed.


In reply to Verifying an ECC signature (ECDSA)? by tlhackque

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.