Alien-ngtcp2
view release on metacpan or search on metacpan
vendor/picotls/lib/openssl.c view on Meta::CPAN
ret = 0;
goto Exit;
}
if ((peer_point = x9_62_decode_point(group, peerkey, ctx->bn_ctx)) == NULL) {
ret = PTLS_ALERT_DECODE_ERROR;
goto Exit;
}
if ((ret = ecdh_calc_secret(secret, group, ctx->privkey, peer_point)) != 0)
goto Exit;
Exit:
if (peer_point != NULL)
EC_POINT_free(peer_point);
if (release) {
x9_62_free_context(ctx);
*_ctx = NULL;
}
return ret;
}
static int x9_62_create_context(ptls_key_exchange_algorithm_t *algo, struct st_x9_62_keyex_context_t **ctx)
{
int ret;
if ((*ctx = (struct st_x9_62_keyex_context_t *)malloc(sizeof(**ctx))) == NULL) {
ret = PTLS_ERROR_NO_MEMORY;
goto Exit;
}
**ctx = (struct st_x9_62_keyex_context_t){{algo, {NULL}, x9_62_on_exchange}};
if (((*ctx)->bn_ctx = BN_CTX_new()) == NULL) {
ret = PTLS_ERROR_NO_MEMORY;
goto Exit;
}
ret = 0;
Exit:
if (ret != 0 && *ctx != NULL) {
x9_62_free_context(*ctx);
*ctx = NULL;
}
return ret;
}
static int x9_62_setup_pubkey(struct st_x9_62_keyex_context_t *ctx)
{
const EC_GROUP *group = EC_KEY_get0_group(ctx->privkey);
const EC_POINT *pubkey = EC_KEY_get0_public_key(ctx->privkey);
if ((ctx->super.pubkey = x9_62_encode_point(group, pubkey, ctx->bn_ctx)).base == NULL)
return PTLS_ERROR_NO_MEMORY;
return 0;
}
static int x9_62_create_key_exchange(ptls_key_exchange_algorithm_t *algo, ptls_key_exchange_context_t **_ctx)
{
EC_GROUP *group = NULL;
struct st_x9_62_keyex_context_t *ctx = NULL;
int ret;
/* FIXME use a global? */
if ((group = EC_GROUP_new_by_curve_name((int)algo->data)) == NULL) {
ret = PTLS_ERROR_LIBRARY;
goto Exit;
}
if ((ret = x9_62_create_context(algo, &ctx)) != 0)
goto Exit;
if ((ctx->privkey = ecdh_generate_key(group)) == NULL) {
ret = PTLS_ERROR_LIBRARY;
goto Exit;
}
if ((ret = x9_62_setup_pubkey(ctx)) != 0)
goto Exit;
ret = 0;
Exit:
if (group != NULL)
EC_GROUP_free(group);
if (ret == 0) {
*_ctx = &ctx->super;
} else {
if (ctx != NULL)
x9_62_free_context(ctx);
*_ctx = NULL;
}
return ret;
}
static int x9_62_init_key(ptls_key_exchange_algorithm_t *algo, ptls_key_exchange_context_t **_ctx, EC_KEY *eckey)
{
struct st_x9_62_keyex_context_t *ctx = NULL;
int ret;
if ((ret = x9_62_create_context(algo, &ctx)) != 0)
goto Exit;
ctx->privkey = eckey;
if ((ret = x9_62_setup_pubkey(ctx)) != 0)
goto Exit;
ret = 0;
Exit:
if (ret == 0) {
*_ctx = &ctx->super;
} else {
if (ctx != NULL)
x9_62_free_context(ctx);
*_ctx = NULL;
}
return ret;
}
static int x9_62_key_exchange(EC_GROUP *group, ptls_iovec_t *pubkey, ptls_iovec_t *secret, ptls_iovec_t peerkey, BN_CTX *bn_ctx)
{
EC_POINT *peer_point = NULL;
EC_KEY *privkey = NULL;
int ret;
*pubkey = (ptls_iovec_t){NULL};
*secret = (ptls_iovec_t){NULL};
( run in 1.507 second using v1.01-cache-2.11-cpan-1236ad508bc )