In the United States, Bad Person A Endorses Good Person B is perfectly legal so long as the endorsement sentiments are pointed in the correct direction. Good Company B has options to deal with the potential bad press, though.
- They can sue, because you can try to bring civil suit to anyone for almost anything, however if Bad Person A has been playing by the rules then a competent legal defense will invoke the First Amendment and either squash them in court or settle favorably outside. If A lacks the resources to withstand a lawsuit, this could convince them to stop just to avoid the trouble, but otherwise B has much better options.
- They can refuse service to A, and they can make a big deal about why they've done so. Depending on the business model they work with, this could be difficult to enforce, but as other answers have mentioned, this will at minimum keep them away from their businesses with trespassing laws (unwanted/unlawful physical presence on their property) and/or similar laws against fraudulent business (misrepresenting identity to trick B into business with a person they expressly refused to deal with).
- B can fight back with a PR effort of their own if A is trying to associate themself publicly with B: "A is not welcome at our establishments" - and in most cases, they'll win this battle. Company B is allowed to advertise what their terms of business are, and if one of those terms is not letting A buy their t-shirt cannons since A supports executing puppies and kittens, too bad for A. Keep in mind that this can be a double-edged sword, though, since even though an established company will usually have more influence on public opinion than a bad individual or interest group, this will also consequently bring the bad agent some more notoriety and attention.
- Inversely, they can ignore A's gesturing, which has the advantage of avoiding legitimizing A and giving them any attention (which in many cases will be A's actual motivation for endorsing B). However, this can backfire if the public starts to put too much stock into B's association with A.
This type of pattern is actually sometimes observed in real life as a symptom of the "all publicity is good publicity" attitude that some unsavory agents espouse. Fortunately, in order to use this principle, A has to walk a thin line.
For starters, 'Bad Person A likes Good Company B' is just A's truthful (and protected) opinion about B; however, the converse statement is defamatory since it is untrue and (measurably, with good lawyers) harmful. Without going into all the details, under U.S. law, a statement like that checks all the boxes for burden of proof in a defamation case: it's false, published, harmful, and not privileged. A could try to toe the line with regards to what exactly they say, but this is dangerous because B can threaten to take them to court if anything A says is borderline, and as mentioned above, this is usually a potent threat.
Additionally, A has a much harder time maneuvering to defend themself than B does. The public already generally dislikes B, so they're probably not gaining much favor by trying to drag some random company down with them. B also usually has a lot of truth and evidence on their side, which most of the time helps them tremendously since A has no power to actually dispute anything B says without risking a lawsuit. For example, if A says they worked at company B to try and smear B's reputation, then B can just go through their records and reveal any facts that might help distance themselves from A's unsavory behaviour, à la:
It is true that A worked here, but only for 3 weeks in the summer of 1995. A was fired for making other employees uncomfortable and trying to kill the squirrels in the parking lot.