Sex work in the Russian Empire was legalized, and heavily regulated, in 1843 under Nicholas I; a committee headed by the mineralogist L.A. Perovski proposed the requirements to practice as a prostitute (women over the age of sixteen, regular medical checkups, and documentation) and operate a brothel (restricted to women over the age of thirty). As an aside, the local enactment of these laws was rather spotty, and as most prostitutes were lower-class women, like we see in Crime and Punishment, it would be quite difficult to do much about abuse by low officials.
These laws from the 1840s provided for prostitutes to live in their own apartments, but they must relinquish other forms of employment, such as Sonya's needlework, and are separated from their families. Petersburg prostitutes are subject to police and medical abuse, and are quite likely to catch venereal disease anyway. After the abolition of serfdom in 1861 these restrictions become more severe, as by the 1860s, the required yellow ticket had replaced other forms of identification, including the uniquely Russian internal passport: a yellow ticket was your identification as a prostitute.
More to the cultural place of prostitution in the 1860s, as you can imagine they were looked down upon; and whatever legal standing Sonya might have had was unlikely to be particularly effective (and, of course, Luzhin frames her for theft - a quite serious crime). Consider the funeral scene also to see a dramatized portrayal of how other lower-class Russians would view a prostitute. It's also worth considering more broadly the place of Petersburg in the second half of the nineteenth century, as it experienced massive population growth and industrialization (at the turn of the century it was the fifth largest city in Europe, surpassing one million inhabitants in 1890; the 1860s saw the fastest growth). Petersburg had a reputation for tenements, gambling, prostitution, and other vices - not particularly unwarranted. Gogol's Nevsky Prospekt shows the city's reputation as early as the 1830s, where a romantic artist is enthralled by a prostitute.
Prostitution was illegal in the Soviet Union - and Alexandra Kollontai's arguments are really excellent look at the debates in the early 1920s, particularly her rejection of Lombroso. Interdevochka is a late Soviet Union film about prostitution and the West that is enjoyable as well. Sonia's Daughters, by Laurie Bernstein, is a much deeper dive into prostitution, but I am not sure if much else has been published more recently on the topic - it's a fascinating question.