So, yes, the marriage needs to be legal in order for the Church to proceed. However there are cases of necessity (i.e. immoral marriage laws) that would permit the Church to witness a non-legal wedding but those are very rare.
DISCLAIMER: Catholic Answers has turned over the archive to Catholic-Questions.org and no longer owns, manages, or moderates the forums. For additional apologetics resources please visit www.catholic.com.