A gravitational keyhole is a tiny region of space where a planet's gravity would alter the orbit of a passing asteroid such that the asteroid would collide with that planet on a given future orbital pass. The word "keyhole" contrasts the large uncertainty of trajectory calculations (between the time of the observations of the asteroid and the first encounter with the planet) with the relatively narrow bundle(s) of critical trajectories. The term was coined by P. W. Chodas in 1999. It gained some public interest when it became clear, in January 2005, that the Asteroid (99942) Apophis would miss the Earth in 2029 but may go through one or another keyhole leading to impacts in 2036 or 2037. This has been ruled out in 2012.
Keyholes for the nearer or farther future are named by the numbers of orbital periods of the planet and the asteroid, respectively, between the two encounters (for example “7:6 resonance keyhole”). There are even more but smaller secondary keyholes, with trajectories including a less close intermediate encounter (bank shots). Secondary gravitational keyholes are searched for by importance sampling: Virtual asteroid trajectories (or rather their ‘initial’ values at the time of the first encounter) are sampled according to their likelihood given the observations of the asteroid. Very few of these virtual asteroids are virtual impactors.
Due to observational inaccuracies, bias in the frame of reference stars, and largely unknown non-gravitational forces on the asteroid, mainly the Yarkovsky effect, its position at the predicted time of encounter is uncertain in three dimensions. Typically, the region of probable positions is formed like a hair, thin and elongated, because visual observations yield 2-dimensional positions at the sky but no distances. If the region is not too extended, less than about one percent of the orbital radius, it may be represented as a 3-dimensional uncertainty ellipsoid and the orbits (ignoring the interaction) approximated as straight lines.
Now imagine a plane comoving with the planet and perpendicular to the incoming velocity of the asteroid (unperturbed by the interaction). This target plane is named b-plane after the collision parameter b, which is the distance of a point in the plane to the planet at its coordinate origin. Depending on a trajectory's position in the b-plane its post-encounter direction and kinetic energy is affected. The orbital energy is directly related to the length of the semi-major axis and also to the orbital period. If the post-encounter orbital period of the asteroid is a fractional multiple of the orbital period of the planet, there will be a close encounter at the same orbital position after the given numbers of orbits.
According to Ernst Öpik's theory of close encounters, the set of points in the b-plane leading to a given resonance ratio forms a circle. Lying on this circle are the planet and two gravitational keyholes, which are images of the planet in the b-plane of the future encounter (or rather of the slightly larger catchment area due to gravitational focusing). The form of the keyholes is a small circle elongated and bent along the circle for the given resonance ratio. The keyhole closest to the planet is smaller than the other, because the variation of deflection becomes steeper with decreasing collision parameter b.
Accuracy may matter 
Relevant keyholes are those close to the uncertainty ellipsoid projected onto the b-plane, where it becomes an elongated ellipse. The ellipse shrinks and jitters as new observations of the asteroid are added to the evaluation. If the probable path of the asteroid keeps close to a keyhole, the precise position of the keyhole itself would matter. It varies with the incoming direction and velocitiy of the asteroid and with the non-gravitational forces acting on it between the two encounters. Thus, “a miss is as good as a mile,” does not apply to a keyhole of several hundred meter width. However, changing the path of an asteroid by a mile can be done with a relatively small velocity change if the first encounter is still years away. Deflecting the asteroid after the fly-by would need a much stronger kick.
For a rapidly rotating planet as the Earth, calculation of trajectories passing close to it, less than a dozen radii, shall include the oblateness of the planet—its gravitational field is not spherically symmetric. For even closer trajectories, gravity anomalies may be important.
For a large asteroid (or comet) passing close to the Roche limit, its size, which is inferred from its magnitude, affects not only the Roche limit but also the trajectory because the center of gravitational force on the body deviates from its center of mass resulting in a higher-order tidal force shifting the keyhole.
See also 
- P. W. Chodas: Orbit uncertainties, keyholes, and collision probabilities, Bulletin of the Astronomical Society, Vol. 31, 1999, p 1117, BibCode 1999BAAS...31R1117C, (online).
- Andrea Milani et al.: Asteroid Close Approaches: Analysis and Potential Impact Detection, S. 55-69 in: William F. Bottke et al. (Ed.): Asteroids III, University of Arizona Press, 2002, ISBN 0-8165-2281-2, (online).
- Giovanni B. Valsecchi et al.: Resonant returns to close approaches: Analytical theory, A&A 408, 2003, p 1179-1196, doi:10.1051/0004-6361:20031039.
- Giovanni B. Valsecchi: Resonant returns, keyholes and all that..., Tumbling Stone Number 20, 24/05/2003.