A credit default swap (CDS) is an exchange of premium payments for a compensation for the occurrence of a credit event. Counterparty risks refer to defaults of parties holding CDS contracts. In this paper we develop a valuation/pricing model for a CDS subject to counterparty risks. Using the Cox–Ingersoll–Ross (CIR) model for interest rate and first arrival times of Poisson processes with variable intensities for the occurrences of credit default and counterparty defaults, we derive a mathematical formulation and make a full theoretical investigation. In addition, we develop a full theory for the corresponding infinite horizon problem and establish its connection with the asymptotic long expiry behaviour of finite horizon problem. Furthermore, we establish a connection between two major frameworks for default times: the structure model approach and the intensity model approach. We show that a solution of the structure model can be obtained as the limit of a sequence of solutions of intensity models. Regarded as an important theoretical development, we remove a constraint typically imposed on the parameters of the CIR model; that is, the well-posedness (existence, uniqueness and continuous dependence of parameters) of the mathematical model holds for any empirically calibrated parameters for the CIR model.