I'm with Pace on this one. Even if the PIC is flying illegally, he/she can log P1. It could be used in court as an admission of guilt though, so whether it's wise to put it in the logbook is a different matter.
Even in the ultimate case. A non-pilot (no license whatsoever, not even a student pilot) hops in an aircraft, starts it and flies it solo. Of course you can argue whether someone like that is "in command" for practical purposes, but for legal purposes that person is "in command" (otherwise, who else is?), and hence should be entitled to log P1. In fact, that person could actually be legally required to log P1, even though such a person is not very likely to have a logbook.
(And now that I'm thinking about it, is there any explicit provision in the ANO or EASA-FCL that says that the experience hours required for issue or revalidation of a license/rating/whatever, need to be flown legally?)
In case of the OP I'm assuming we're either talking about a check-out scenario, or the scenario where somebody lost a medical or something like that. In that case it's best if the PIC, and thus the person who logs P1, is the person who is in every aspect legal and insured to fly the aircraft. If the other person is doing the actual handling then that's fine, but that person is not the PIC and hence logs nothing.
(Note that in a single pilot aircraft only one person can act as, and log P1, and everybody else on board logs nothing. Except when the PIC is an instructor or examiner.)