> From: [email protected] On Behalf Of MatejTomcik
> Sent: Friday, 27 April, 2012 04:28

> my application uses openssl to protect the communication, to 
> allow acces
> only for the users who have a certificate signed by my CA but 
> I also need to

To be exact: you have a server that demands client authentication 
(SSL not e.g. HTTP auth) using a cert issued by your CA?

> know the public key and the sha1 hash of the key. I was able 
> to make an
> application which meets first two points. But I'm stuck on finding the
> solution for the last point. After I call 
> SSL_get_verify_result I try to
> obtain the public key like this:
> X509 * cert = SSL_get_peer_certificate(ssl);
> EVP_PKEY * key = X509_get_pubkey(cert);
> printf_s("RSA-%d\n", BN_num_bits(key->pkey.rsa->n));
> EVP_PKEY_free(key);
> X509_free(cert);
> The problem is that key struct seems to be empty, or at least the pkey
> field. This is how it looks:
> http://old.nabble.com/file/p33757674/x509pkey.png 
> What is the right way to obtain the public key and its sha1 
> hash from a
> client once the communication has been established?

That is the right way to get the pubkey from the cert, 
and it works for me in a simple test program.

Something must be wrong with either the code you're running, 
or the way your debugger looks at it, because many fields 
in key->* are visibly wrong not just key->pkey.rsa .
Make sure the code you are debugging is exactly the code 
you intend, and the debugger has the right info (e.g. 
symbols) that exactly match the code. In most environments 
the usual way to do that is clean and then build with no 
or low optimization, although there may be other ways.

Aside: to be robust you should verify k.type == EVP_PKEY_RSA 
before using k.pkey.rsa, although if you only trust one CA 
and it only certifies RSA keys, and you verify the cert before 
using the key, in effect the key is guaranteed to be RSA.

Doing the hash is up to you. OpenSSL doesn't automatically 
hash publickeys except in creating a SubjKeyId extension.

Also, be sure if your requirement is to check/hash just n.
An RSA public key actually consists of (n,e), and security 
measures like "hashing the key" tend to be defined in terms 
that allow algorithm independence by using the formal definition 
of the key, often as a DER or similarly canonical encoding.


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       [email protected]
Automated List Manager                           [email protected]

Reply via email to