There is pretty much never a right to retaliate against harm to oneself, even blatantly unlawful harm.
There is a right to defend oneself and others. One can use force to stop someone from inflicting unlawful or unjustified harm, or to prevent someone from inflicting such harm when the harm is imminent. One is not permitted to use more force than is "reasonably required" under the actual circumstances. This is true in pretty much every jurisdiction that i know of. The details on how much force will be considered "reasonable" will vary.
In some jurisdictions there is, under some circumstances, a s"duty to retreat". This generally means that if a person attacked can avoid the harm by fleeing with reasonable safety, that person must do so rather than using force in self-defense. In some jurisdictions this "duty to retreat" applies id the victim is attacked in public, but not in the victim's own home.
The right to self defense does not apply when the "attacker" is an agent of the state acting lawfully. For example, a prison guard taking a condemned prisoner to a death sentence cannot be attacked on the grounds that the prisoner is engaging in self defense.
In theory a police officer engaging an excessive force, particularly unjustified deadly force, may be resisted in self-defense. But courts are quite reluctant to find such resistance justified in practice. There generally must be very clear evidence of egregious misconduct for the court to rule for the non-police person in such a case.
Note that "self" defense can equally be defense of another person. Pretty much all the same rules apply.
Self defense applies no matter who the attacker is, but that force is reasonable may vary depending on the attacker. Only such force as is reasonably required to stop or prevent the harm may be used with a justification of self-defense.