/* * Get parameters from a keypair */ int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key, mbedtls_ecdh_side side ) { int ret; if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ) return( ret ); /* If it's not our key, just import the public part as Qp */ if( side == MBEDTLS_ECDH_THEIRS ) return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) ); /* Our key: import public (as Q) and private parts */ if( side != MBEDTLS_ECDH_OURS ) return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 || ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 ) return( ret ); return( 0 ); }
/* * Set context from an mbedtls_ecp_keypair */ int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key ) { int ret; if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 || ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 || ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ) { mbedtls_ecdsa_free( ctx ); } return( ret ); }
result_t PKey::copy(const mbedtls_pk_context &key) { mbedtls_pk_type_t type = mbedtls_pk_get_type(&key); int32_t ret; if (type == MBEDTLS_PK_RSA) { mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key); ret = mbedtls_pk_setup(&m_key, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)); if (ret != 0) return CHECK_ERROR(_ssl::setError(ret)); mbedtls_rsa_context *rsa1 = mbedtls_pk_rsa(m_key); ret = mbedtls_rsa_copy(rsa1, rsa); if (ret != 0) return CHECK_ERROR(_ssl::setError(ret)); return 0; } if (type == MBEDTLS_PK_ECKEY) { mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(key); ret = mbedtls_pk_setup(&m_key, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY)); if (ret != 0) return CHECK_ERROR(_ssl::setError(ret)); mbedtls_ecp_keypair *ecp1 = mbedtls_pk_ec(m_key); ret = mbedtls_ecp_group_copy(&ecp1->grp, &ecp->grp); if (ret != 0) return CHECK_ERROR(_ssl::setError(ret)); ret = mbedtls_mpi_copy(&ecp1->d, &ecp->d); if (ret != 0) return CHECK_ERROR(_ssl::setError(ret)); ret = mbedtls_ecp_copy(&ecp1->Q, &ecp->Q); if (ret != 0) return CHECK_ERROR(_ssl::setError(ret)); return 0; } return CHECK_ERROR(CALL_E_INVALID_CALL); }
result_t PKey::get_publicKey(obj_ptr<PKey_base> &retVal) { result_t hr; bool priv; hr = isPrivate(priv); if (hr < 0) return hr; if (!priv) return CALL_RETURN_NULL; mbedtls_pk_type_t type = mbedtls_pk_get_type(&m_key); int32_t ret; if (type == MBEDTLS_PK_RSA) { mbedtls_rsa_context *rsa = mbedtls_pk_rsa(m_key); obj_ptr<PKey> pk1 = new PKey(); ret = mbedtls_pk_setup(&pk1->m_key, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)); if (ret != 0) return CHECK_ERROR(_ssl::setError(ret)); mbedtls_rsa_context *rsa1 = mbedtls_pk_rsa(pk1->m_key); rsa1->len = rsa->len; rsa1->padding = rsa->padding; rsa1->hash_id = rsa->hash_id; ret = mbedtls_mpi_copy(&rsa1->N, &rsa->N); if (ret != 0) return CHECK_ERROR(_ssl::setError(ret)); ret = mbedtls_mpi_copy(&rsa1->E, &rsa->E); if (ret != 0) return CHECK_ERROR(_ssl::setError(ret)); retVal = pk1; return 0; } if (type == MBEDTLS_PK_ECKEY) { mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(m_key); obj_ptr<PKey> pk1 = new PKey(); ret = mbedtls_pk_setup(&pk1->m_key, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY)); if (ret != 0) return CHECK_ERROR(_ssl::setError(ret)); mbedtls_ecp_keypair *ecp1 = mbedtls_pk_ec(pk1->m_key); ret = mbedtls_ecp_group_copy(&ecp1->grp, &ecp->grp); if (ret != 0) return CHECK_ERROR(_ssl::setError(ret)); ret = mbedtls_ecp_copy(&ecp1->Q, &ecp->Q); if (ret != 0) return CHECK_ERROR(_ssl::setError(ret)); retVal = pk1; return 0; } return CHECK_ERROR(CALL_E_INVALID_CALL); }