title: How does one access the raw ECDH public key, private key and params inside OpenSSL's EVP_PKEY structure?
body: <p>I'm using OpenSSL's c library to generate an elliptic curve Diffie-Hellman (ECDH) key pair, following the first code sample <a href="http://wiki.openssl.org/index.php/Elliptic_Curve_Diffie_Hellman">here</a>. It glosses over the actual exchange of public keys with this line:</p>
  
  <pre><code>peerkey = get_peerkey(pkey);
  </code></pre>
  
  <p>The <code>pkey</code> variable and the return value are both of type <code>EVP *</code>. <code>pkey</code> contains the public key, private key, and params generated earlier, and the return value only contains the peer's public key. So this raises three questions:</p>
  
  <ol>
  <li>How would <code>get_peerkey()</code> actually extract just the public key from <code>pkey</code> for sending to the peer?</li>
  <li>How would the code extract the private key and params from <code>pKey</code> to store them for later use after the key exchange?</li>
  <li>How would <code>get_peerkey()</code> generate a new <code>EVP_PKEY</code> structure from the peer's raw public key?</li>
  </ol>
  
  <p>I've seen the OpenSSL functions <code>EVP_PKEY_print_public()</code>, <code>EVP_PKEY_print_private()</code>, and <code>EVP_PKEY_print_params()</code> but these are for generating human-readable output. And I haven't found any equivalent for converting a human-readable public key back into an <code>EVP_PKEY</code> structure.</p>