A necessary and sufficient condition is that $P(x)$ is a power of $x$ times a product of terms $x^2+c$ with $c$ real and positive. Hence $P(x)$ is $x^{n-2m}Q(x^2)$ where $m\ge0$ and $Q(t)$ is a unitary polynomial in $t$ of degree $m$ with integer nonnegative coefficients. Finally, a necessary condition is that $a_{k}=0$ for every odd $k$ and one should further require the underlying polynomial $Q$ to be a product of $t+c_i$.

**EDIT** It appears that the problem of locating the zeroes of a polynomial resurfaces regularly on MO, see for example this question or that one or that one. The answers to these provide the following facts.

First, there are Newton's inequalities. For any real numbers $(t_i)_{1\le i\le m}$, their elementary symmetric means $S_k$ are such that, for every $k$, $S_k^2\ge S_{k-1}S_{k+1}$. Here $S_k=\sigma_k/{m\choose k}$ where $\sigma_k$ denotes the $k$th elementary function in the real numbers $(t_i)_{1\le i\le m}$, see here.

So, write $Q$ as $Q(t)=t^m+b_1t^{m-1}+\cdots+b_m$ (and recall that $b_k=a_{2k}$). Any $Q$ under consideration is such that $b_k$ must be nonnegative for every $k$ and Newton's inequalities indicate that supplementary necessary condition are $S_k^2\ge S_{k-1}S_{k+1}$ for every $k$, where the $S_k$ are based on the $\sigma_k=b_k$. (At first sight, one should choose $\sigma_k=(-1)^kb_k$ but the $(-1)^k$ disappear.) Hence, a necessary condition is that, for every $k$,
$$
k(m-k)b_k^2\ge (k+1)(m-k+1)b_{k-1}b_{k+1}.
$$
Second, a *complete characterization* of the polynomials $Q$ with only real negative roots is based on the notion of Hermite forms, see this answer.

Recall that the Hermite form of a polynomial $Q$ of degree $m$ is a symmetric matrix, usually denoted by $H_1(Q)$, of size $m\times m$ with entries $(h_{ij}(Q))$, defined by
$$
h_{ij}(Q)=s_{i+j-2}(Q),
$$
where, for every $k$, $s_k(Q)$ is the sum of the $k$th powers of the roots of $Q$ (and $s_0(Q)=m$), see these lecture notes. Recall that the $s_k(Q)$ are well known functions of the elementary symmetric functions $\sigma_k=(-1)^kb_k$, see here. Recall also that the signature of a symmetric matrix is equal to the number of its positive eigenvalues minus the number of its negative eigenvalues. Then the signature of $H_1(Q)$ is equal to the number of real roots of $Q$.

In the context of this question, one already knows that $Q$ has no positive real root because $b_k$ is nonnegative for every $k$ and one wants $Q$ to have $m$ real roots. Hence the signature of $H_1(Q)$ must be $m$, that is, $H_1(Q)$ must be positive definite.

Finally, necessary and sufficient conditions are that the odd numbered $a_k$ are zero and that the even numbered $a_k$ are nonnegative and define a polynomial $Q$ such that the symmetric matrix $H_1(Q)$ is positive definite.