Castling may only be done if:
- the king has never moved,
- the rook involved has never moved,
- the squares between the king and the rook involved are unoccupied,
- the king is not in check, and
- In other words, you cannot castle out of check
- 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