the IRS doesn't look at who has legal custody .
here are the IRS rules
the child must
1) be the taxpayer's child, stepchild, eligible foster child or descendant of any of them such as a grandchild or brother, sister half-brother, half-sister, stepbrother, stepsister or descendant of any of them such as a niece or nephew, legally adopted child or place with the taxpayer for legal adoption
2) be any of the following a) under age 19 at the end of the year b) a full-time student and under age 24 at end of the year c) ot any age if totally and permanently disabled
3) have the same principal abode as taxpayer for more than ½ the tax year.
4) Hasn’t provided over ½ his support
5) No joint return with the exception for a joint return filed only to claim a refund of tax withheld when neither spouse is required to file and no tax liability would exist for either spouse if separate returns were filed
6) Not be a qualifying child of another taxpayer with a higher priority under the tie-breaker rules
if he isn't a qualifying child of either taxpayer then he could be a qualifying relative if these tests are met
1) meets relationship test (same as for qualifying child) or lived in the taxpayers household for the entire calendar year
2) gross income for 2019 less than $4,200
3) taxpayer provides over ½ his support