The answer to this question depends upon whether the marriage is invalid or valid.
If the marriage is valid (either certainly valid or putatively valid), then the children are legitimate, even if the marriage is subsequently annuled:
Canon 1137 The children conceived or born of a valid or putative[ly valid] marriage are legitimate.
Expressed negatively: If "children [are not] conceived or born of a valid or putative[ly valid] marriage," then they are illegitimate.
The canonist Charles Augustine, O.S.B., D.D.—commentating on 1917 Code canon 1174, which corresponds to the 1983 Code canon 1137—writes in his A Commentary on the New Code of Canon Law book 3, vol. 5, p. 332:
But legitimacy always requires a marriage, whether certainly or putatively valid. A marriage is certainly valid if contracted without an invalidating impediment and according to the form prescribed by the Church. A putatively valid marriage is one contracted with due observance of the prescribed form, but with an invalidating impediment, the existence of which is unknown to one of the parties.
The New Commentary on the Code of Canon Law p. 1259, published in 2000, also describes how "putative marriage" in Canon 1137 specifically means a "putatively valid marriage":
For a marriage to be considered putative, it must have been celebrated according to the required form and, therefore, have the appearance of a marriage. Thus, when a Catholic enters marriage without observing the canonical form and without being dispensed from its observance, the attempted marriage is not considered putative but simply invalid. If, however, the canonical form was observed but was substantially defective (e.g., the official minister was not properly delegated), the marriage would be considered putative.
The most significant effect of a putative marriage is that any children born of it are considered legitimate, even if it is subsequently declared invalid (c. 1137). A putative marriage also enjoys the favor of the law. It remains putative until both spouses become certain of its nullity. Thus, the spouses' doubts, even grave doubts, about the validity of their marriage are not sufficient to deprive it of its status as putative. the spouses on their own may become certain of the invalidity of their marriage because of an impediment. However, most couples become certain that their marriage was invalid because of a defect of consent only through the decision of a church tribunal.