A Lagrangian approach to shape inverse electromagnetic problems