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.
1Think of the reasoning being that the king cannot be in check, even if temporarily while castling, but the rook has no such restriction. Dec 15, 2019 at 16:27
Yes, you can, as long as the king doesn't pass through or end up on an attacked square.
From FIDE Laws of Chess:
18.104.22.168 The right to castle has been lost:
22.214.171.124.1 if the king has already moved, or
126.96.36.199.2 with a rook that has already moved.
188.8.131.52 Castling is prevented temporarily:
184.108.40.206.1 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
220.127.116.11.2 if there is any piece between the king and the rook with which castling is to be effected.
2That was interesting, your answer changed from NO to YES! May 2, 2012 at 12:59
6We should ask Viktor Korchnoi! May 5, 2012 at 13:59
6To explain Tony Ennis' joke, in 1974, Korchnoi was playing Karpov in the Candidates final for the right to challenge Fischer. Midgame, Korchnoi strolls over to the arbiter O'Kelly de Galway, and asks if he can castle while his rook is attacked. O'Kelly looks at him stunned, but answers yes. "It had never come up before," Viktor shrugged. Oct 4, 2015 at 22:38
1Does that mean that there should be no piece (of any color) between unmoved king and rook (to be castled)? Jan 4, 2016 at 21:46
I was surprised by the Korchnoi story, but according to this article from the South Florida Sun-Sentinel, the story appears in Korchnoi's memoir Chess is My Life. Mar 11, 2020 at 15:36
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.
[FEN ""] [Event "Belgian Championship"] [Site "Liege BEL"] [Date "1934"] [EventDate "?"] [Round "?"] [Result "1-0"] [White "Otto Feuer"] [Black "Alberic O'Kelly de Galway"] [ECO "C73"] [WhiteElo "?"] [BlackElo "?"] [PlyCount "25"] 1.e4 e5 2.Nf3 Nc6 3.Bb5 a6 4.Ba4 d6 5.Bxc6+ bxc6 6.d4 f6 7.Nc3 Rb8 8.Qd3 Ne7 9.Be3 Rxb2 10.dxe5 fxe5 11.Nxe5 dxe5 12.Qxd8+ Kxd8 13.O-O-O+ 1-0
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!
There is more on the condition to consider The King and Rook have to be on the same rank, otherwise Pam-Krabbe castling would be possible. Pam-Krabbe castling is basically a vertical castling, where the king would castle with a promoted pawn. This is not allowed anymore.
Here is a problem that I published in the MatPlus.net forums a few months ago. It demonstrates how the rook can move through squares that are attacked. The squares a1 and b1 are maximally guarded. If it were on a board, it would definitely frustrate a few people, which is the exact point of this little exercise!
[Title "me, Matplus.net Forum 5/24/2019, Mate In 2"] [FEN "8/8/8/8/pppbbp1r/nr2pppB/q2n2Pp/R3K2k w Q - 0 1"] 1. O-O-O+! Nf1 2. Rxf1#