We describe an efficient algorithm to compute all the critical points of the distance function between two Keplerian orbits (either bounded or unbounded) with a common focus.The critical values of this function are important for different purposes, for example to evaluate the risk of collisions of asteroids or comets with the Solar system planets. Our algorithm is based on the algebraic elimination theory: through the computation of the resultant of two bivariate polynomials, we find a 16th degree univariate polynomial whose real roots give us one component of the critical points. We discuss also some degenerate cases and show several examples, involving the orbits of the known asteroids and comets.