In this paper, we describe a numerical framework for achieving passive thermal cloaking of arbitrary shapes in both static and transient regimes. The design strategy is cast as the solution of an optimal control problem (OCP) for the heat equation where the coefficients of the thermal diffusivity matrix take the role of control functions, and the distance between the uncloaked and the cloaked field is minimized in a suitable observation domain. The control actions enter bilinearly in the heat equation, thus making the resulting OCP nonlinear, and its analysis non-trivial. We show that optimal diffusivity coefficients exist both for the static and the transient case; we derive a system of first-order necessary optimality conditions; finally, we carry out their numerical approximation using the finite-element method. A series of numerical test cases assess the capability of our strategy to tackle passive thermal cloaking of arbitrarily complex two-dimensional objects.