Castling may only be done if:

  1. the king has never moved,
  2. the rook involved has never moved,
  3. the squares between the king and the rook involved are unoccupied,
  4. the king is not in check, and
    • In other words, you cannot castle out of check
  5. the king does not cross over or end on a square in which it would be in check.
    • In other words, you cannot castle into check