We compute the algebras of self-extensions of the vacuum module and the Verma modules over an affine Kac–Moody algebra $\hat{\mathfrak g}$ in suitable categories of Harish-Chandra modules. We show that at the critical level these algebras are isomorphic to the algebras of differential forms on various spaces of opers associated to the Langlands dual Lie algebra of ${\mathfrak g}$, whereas away from the critical level they become trivial. These results rely on and generalize the description of the corresponding algebras of endomorphisms obtained by Feigin and Frenkel and the description of the corresponding graded versions due to Fishel, Grojnowski and Teleman.