It is 20th century and all the early references to it relate to the US military. The New York Times Magazine, March 1954, had a related phrase, in a glossary of jet pilots' slang:
"Bought a plot, had a fatal crash."
That clearly refers to a burial plot. The 'bought' in that case probably doesn't suggest any actual or potential purchase, but to an earlier use of 'bought', that is, being killed. This dates back to at least the early 20th century. This example from 1943 isn't the earliest, but it does make the meaning explicit. It's from Cyril Ward-Jackson's It's a piece of cake; or, R.A.F. slang made easy:
"He's bought it, he is dead - that is, he has paid with his life."
Specific references to 'the farm' come a little later. There are reports of the phrase being in use in the US military from 1955 onward. Here's a citation from 1963, in Ed Miller's Exile to the Stars:
"The police dispatcher says a plane just bought the farm.