+int ecdsa_public_key_from_pk(mbedtls_pk_context *pk, uint8_t *key, size_t keylen) {
+ int res = 0;
+ size_t realkeylen = 0;
+ if (keylen < 65)
+ return 1;
+
+ mbedtls_ecdsa_context ctx;
+ mbedtls_ecdsa_init(&ctx);
+
+ res = mbedtls_ecp_group_load(&ctx.grp, MBEDTLS_ECP_DP_SECP256R1); // secp256r1
+ if (res)
+ goto exit;
+
+ res = mbedtls_ecdsa_from_keypair(&ctx, mbedtls_pk_ec(*pk) );
+ if (res)
+ goto exit;
+
+ res = mbedtls_ecp_point_write_binary(&ctx.grp, &ctx.Q, MBEDTLS_ECP_PF_UNCOMPRESSED, &realkeylen, key, keylen);
+ if (realkeylen != 65)
+ res = 2;
+exit:
+ mbedtls_ecdsa_free(&ctx);
+ return res;
+}
+