Roger Federer was given a fright before overcoming compatriot Stanislas Wawrinka at the BNP Paribas Open in Indian Wells.