Without getting to biblically inclined it was, as zmjezhd pointed out, technically a Levirate marriage which was legal. It was indeed the spilling of the seed for his own means that was the crime.