Reference: Fataawa Noor ‘alad-Darb – Question No.6282, Page 113, Volume 12
Question: If a person had amassed immense wealth made up of trading in haraam, and then repented to Allaah [from that], is it then permissible for him to perform hajj using this wealth, or donate to charity from it, or [even] get married or build a masjid using it?
Response: Whoever earns a living in a prohibited manner, then these earnings are not halaal for him; [instead] he must rid himself of this wealth, and that is by returning it to their rightful owners – if he dishonestly took it from them – without seeking any return. Otherwise, he must donate it in order to rid himself of it, or build a masjid or that which is similar in terms of noble causes.
[Importantly] however, by doing so he must not intend the worship [of Allaah], because it will not benefit him; that is because whoever worships Allaah using haraam wealth, then Allaah will not accept it from him, because Allaah (Ta’aala) is Good and does not accept anything but good. [In addition], doing so will not relinquish him of his responsibility because with this donation he has not intended to rid himself of that [haraam] wealth. So that which is required of the person who has earnt haraam earnings, is to repent to Allaah, and to use that haraam wealth in a manner which pleases Allaah (Subhaanahu wa Ta’aala) – in order to rid himself of it, and not [seeking to] worship [Allaah]; and with that he will have relinquished himself of the responsibility.