A South African judge ruled on Wednesday that the detention of Mozambique’s former finance minister Manuel Chang over $2 billion of fraudulent loans was legal.