For establishing the paternity of an illegitimate child, there will be different cases. In permissible cases, the wife and the husband will be considered the mother and father of the child. If the sperm of a stranger is put into somebody else’s wife and this pregnant lady is married to somebody else, then this is adultery. In this case, the husband of that lady will be considered the child’s father and not the stranger whose sperm was used as this comes in the category of adultery and about adultery, the Holy Prophet (Sallallahu alaihi wa sallam) said:
Translation: The child is of the master of the bed i.e. the husband and for the adulterer, there is the stone. (Sahih Bukhari, Hadith No. 6749; Sahih Muslim Hadith No. 3686)
Even if the pregnant lady is not married to anybody else, even then the stranger whose sperm was used will not be considered the father of the child, but will be considered the child of the mother alone, as given in Sunan Abu Dawood about a child born of adultery:
Translation: The child will not be considered the progeny of the adulterer and neither will he inherit from him. (Sunan Abu Dawood, Hadith No. 2267)
There is a Hadith in Sunan Ibn Majah:
Translation: He is a child born of adultery who will be considered to belong to the mother’s family whoever they are. (Sunan Ibn Majah, Hadith No. 2851)