SEO is not illegal, but things like black hat SEO practices such as filling your web pages with keywords that are visible to search engines but not visible to reader on your pages, can get your site banned or at the very least decrease your views from search engines.