Borrowed from Latin lēgitimus (compare Catalan legítim). Doublet of lídimo, and probably lindo.
Practice speaking and memorizing "legítimo" and many other words and sentences in Portuguese.