Hi all, Did anyone have any luck with this one?
Thanks, Pratyush Parimal. ---------- Forwarded message ---------- From: pratyush parimal <pratyush.pari...@gmail.com> Date: Wed, Jun 25, 2014 at 10:43 AM Subject: Converting public part of 'EVP_PKEY' structure to 'unsigned char*' , and back. To: openssl-users@openssl.org Hi all, I was trying to use ECDH (in OpenSSL v1.0.1f) for a project, and after generating the EVP_PKEY structure, I needed to extract its public key and send it over to the other party. I was unable to find a straightforward way which worked for me. What I tried was this: EVP_PKEY* extract_peerkey_3(EVP_PKEY* EVP_PKEY_both) //'both' meaning it contains public + private { int len = 0; len = i2d_PUBKEY(EVP_PKEY_both, NULL); //find out required buffer length unsigned char *buf, *p; buf = (unsigned char*) malloc(len); //allocate p = buf; len = i2d_PUBKEY(EVP_PKEY_both, &p); const unsigned char* p2 = buf; EVP_PKEY* EVP_PKEY_public = d2i_PUBKEY(NULL, &p2, len); if (EVP_PKEY_public == NULL) { handleCryptoError("d2i failed", ERR_get_error()); } return EVP_PKEY_public; } The function doesn't throw an error, but when I pass the returned 'EVP_PKEY_public' structure to the function 'EVP_PKEY_derive_set_peer', I get an error message "error:10071065:elliptic curve routines:EC_POINT_cmp:incompatible objects". I also tried to follow the steps given at http://stackoverflow.com/questions/18155559/how-does-one-access-the-raw-ecdh-public-key-private-key-and-params-inside-opens . When i reconstruct the EVP_PKEY using the steps EC_POINT_oct2point() -> EC_KEY_set_public_key() -> EVP_PKEY_set1_EC_KEY(), the resulting EVP_PKEY does work for me. In fact I'm able to derive the same secret on both sides using this sequence, but I feel it's too roundabout. I also saw the following: http://marc.info/?l=openssl-users&m=116474297608094&w=2, which talks about using 'i2d_PUBKEY', but I haven't been able to make it work so far. Is my usage of d2i_PUBKEY or i2d_PUBKEY wrong in some way? Does anyone know how to use them properly? Any help will be appreciated. Thanks! Pratyush Parimal