It's not directly or criminally illegal. It's civilly illegal in the sense that Apple has a fiduciary responsibility to their shareholders in their business decision making. In practice, there are quite broad interpretations of what might be considered "good for your shareholders", but someone's personal interests generally wouldn't qualify as that.
If, hypothetically, Cook said "fuck this administration, we don't like their politics, we're not going to work with them", their shareholders could and probably would sue them. Those shareholders could make a case that Cook was asking of his own political interests, point to other organizations that did make exemption deals, and sue for losses in their share value. The reason for this is not entirely wacky: when you borrow someone's money to do something, you can't do your own pet projects with it.
Now that, of course, doesn't mean that Cook had to donate. But Cook is businessman himself, runs Apple to make money, and doing that is his modus operandi.