When you buy a CD, you buy a personal copy of the music and not the right to copy or distribute it. Copying a CD and giving the copy to a friend is an infringement of copyright and is illegal.