int crypto_cert_get_public_key(CryptoCert cert, DATABLOB * public_key) { int length; int success = 1; EVP_PKEY *pkey = NULL; unsigned char *p; pkey = X509_get_pubkey(cert->px509); if (!pkey) { printf("crypto_cert_get_public_key: X509_get_pubkey() failed\n"); success = 0; goto exit; } length = i2d_PublicKey(pkey, NULL); if (length < 1) { printf("crypto_cert_get_public_key: i2d_PublicKey() failed\n"); success = 0; goto exit; } datablob_alloc(public_key, length); p = (unsigned char*) public_key->data; i2d_PublicKey(pkey, &p); exit: if (pkey) EVP_PKEY_free(pkey); return success; }
int tls_get_public_key(SSL *connection, DATABLOB *public_key) { int length; int success = 1; X509 *cert = NULL; EVP_PKEY *pkey = NULL; unsigned char *p; cert = SSL_get_peer_certificate(connection); if (!cert) { printf("tls_get_public_key: SSL_get_peer_certificate() failed\n"); success = 0; goto exit; } pkey = X509_get_pubkey(cert); if (!cert) { printf("tls_get_public_key: X509_get_pubkey() failed\n"); success = 0; goto exit; } length = i2d_PublicKey(pkey, NULL); if (length < 1) { printf("tls_get_public_key: i2d_PublicKey() failed\n"); success = 0; goto exit; } datablob_alloc(public_key, length); p = (unsigned char*) public_key->data; i2d_PublicKey(pkey, &p); exit: if (cert) X509_free(cert); if (pkey) EVP_PKEY_free(pkey); return success; }