Is castling possible if any of the squares involved in the castling are under attack, or is this a problem only if the squares moved through by the king are under attack and not the ones moved through by the rook?
Yes, if the rook is threatened, you may still castle. The threatened squares rule only applies to squares where the king passes (starting and final position included).
For example, in the case of white castling queenside, for instance, a threat to
b1 does not prevent the castle from taking place.
Yes, you can, as long as the king doesn't pass through or end up on an attacked square.
From FIDE Laws of Chess:
- The right to castle has been lost:
- if the king has already moved, or
- with a rook that has already moved.
- Castling is prevented temporarily:
- if the square on which the king stands, or the square which it must cross, or the square which it is to occupy, is attacked by one or more of the opponent's pieces, or
- if there is any piece between the king and the rook with which castling is to be effected.
One nice example of this is in this game, where a player named Feuer takes advantage of the ability to castle queenside while b1 is attacked to play a beautiful combination.
The rules are that the king can't castle into check, through check, or when in check. This applies to the king's square, plus the two squares to the right or left.
Castling is permitted when the rook is under attack (on the rook's square). On the queen side, that would also include the knight's square. But not on the kingside, because the king would be castling into check. He also can't castle if the bishop's or queen's square is under attack.
You can castle when all of these conditions hold true:
The King and the Rook did not make any moves so far.
The King is not in check.
The King will not pass a threatened square during castling.
The King will not land on a threatened square.
Condition 3 might need clarification. For example, you have the white pieces and you want to castle kingside. Then, you are using your Ke1 and Rh1 for castling. Let's say that conditions 1 and 2 are fulfilled. Next, the g1 square should not be threatened in order to fulfill condition 4. Finally, the King will pass the square f1 on its way to g1. Thus, the square f1 should not be threatened for condition 3 to hold true!