Peak Estimation aims to find the maximum value of a state function achieved by a dynamical system. This problem is non-convex when considering standard Barrier and Density methods for invariant sets, and has been treated heuristically by using auxiliary functions. A convex formulation based on occupation measures is proposed in this paper to solve peak estimation. This method is dual to the auxiliary function approach. Our method will converge to the optimal solution and can recover trajectories even from approximate solutions. This framework is extended to safety analysis by maximizing the minimum of a set of costs along trajectories.