It became legal in 1789, when the office of President was created. Article II section 1 of the constitution specifies the requirements to be president: No Person except a natural born Citizen, or a Citizen of the United States, at the time of the Adoption of this Constitution, shall be eligible to the Office of President; neither shall any person be eligible to that Office who shall not have attained to the Age of thirty five Years, and been fourteen Years a Resident within the United States. This does not specify a requirement to be either male or to be an eligible voter. In fact, before women won the right to vote in 1920 with the ratification of the 19th amendment, at least 3,985 women had run for public office.
Victoria Claflin Woodhull was the first woman to run for the office of the Presidency in the election of 1872 with the support of the Equal Rights Party, naming Frederick Douglass as her Vice Presidential candidate, and promising a new constitution within a year. Douglass never acknowledged his own nomination. She campaigned extensively through her own feminist newspaper, the Woodhull and Claflin Weekly, which she founded herself in 1870 for the purpose of supporting her campaign. Her party was on the ballot in 22 states but her name for the most part was absent due to her ineligibility for the office, not because she was a woman, but because she was only 34 years old. One day before the election she was arrested for publishing an 'obscene' newspaper, and on election day she won few votes and no electoral votes, despite many women's rights activists attempting to (illegally) vote for her. Susan B Anthony was arrested and tried for attempting to vote for Woodhull, where she was found guilty and fined $100. She refused to pay but the judge declined to enforce her punishment, thus denying her standing to appeal the verdict. The trial was covered extensively by newspapers, giving the issue of women's suffrage national attention for the first time.
Sources
http://www.herhatwasinthering.org/index.php
https://www.theattic.space/home-page-blogs/woodhull
https://www.fjc.gov/sites/default/files/trials/susanbanthony.pdf#