Duality and Optimality Conditions in Stochastic Optimization and Mathematical Finance