First and second order characterizations of convex functions