In this paper, the fractional optimal control problem for differential system is considered. The fractional time derivative is considered in Riemann--Liouville sense. Constraints on controls are imposed. Necessary and sufficient optimality conditions for the fractional Dirichlet and Neumann problems with the quadratic performance functional are derived. Some examples are analyzed in details.