In section 12 of his 2d Treatise on Government: On Civil Government," John Locke makes two key points. The first is that a punishment should be just severe enough "as will suffice to make it an ill bargain to the offender, give him cause to repent, and terrify others from doing the like":
By the same reason may a man in the state of nature punish the lesser breaches of that law. It will perhaps be demanded, with death? I answer, each transgression may be punished to that degree, and with so much severity, as will suffice to make it an ill bargain to the offender, give him cause to repent, and terrify others from doing the like.
I wrote about how this principle of a punishment just severe enough to deter applies could apply to civil law in "Reparation and Deterrence."
The second key point is that to be legitimate, laws must be founded on the underlying morality of the law of nature.
Every offence, that can be committed in the state of nature, may in the state of nature be also punished equally, and as far forth as it may, in a commonwealth: for though it would be besides my present purpose, to enter here into the particulars of the law of nature, or its measures of punishment; yet, it is certain there is such a law, and that too, as intelligible and plain to a rational creature, and a studier of that law, as the positive laws of commonwealths; nay, possibly plainer; as much as reason is easier to be understood, than the fancies and intricate contrivances of men, following contrary and hidden interests put into words; for so truly are a great part of the municipal laws of countries, which are only so far right, as they are founded on the law of nature, by which they are to be regulated and interpreted.
I marvel at the number of people I run across who think there is some magic in democracy that makes everything that 51% of people vote for good and right—or in any case legitimate to impose on everyone else. Some people are more circumspect and argue that the large supermajorities needed to pass a constitutional amendment are necessary to make something good and right—or at least legitimate to impose on everyone else. But it is not so. Something can be wrong to impose on people even if a large supermajority wants to.
I do not mean to claim that it is always clear what the law of nature allows and what it doesn't. Where some freedom is compromised to raise overall social welfare or where overall social welfare is compromised for the sake of freedom, I don't always know what is legitimate and what isn't, let alone what is the right thing to do. But there is one case where I know something is illegitimate and contrary to Locke's law of nature: when it both compromises freedom and lowers overall social welfare in order to benefit some at the expense of others.
There are many such laws that both compromise freedom and lower overall social welfare in order to benefit some people at the expense of others. I mention a few in 'Keep the Riffraff Out!'" but attentive readers will notice how this is a theme throughout this entire blog.
On the underlying principle that democracy doesn't necessarily make things right, see "Democracy is Not Freedom" and "John Locke: The Right to Enforce the Law of Nature Does Not Depend on Any Social Contract." Also, I remember being inspired by the Milton Friedman video below on "The Proper Role of Government" to write the gloss "Legally formalizing ethical rules is different from other, more arbitrary legislation" in my post "Milton Friedman: Celebrating His 100th Birthday with Videos of Milton."