It is OK for me to walk up to a friend's dog and pet her. A good way to be safe around dogs is to help them feel safe. If a strange dog comes up to me and looks like he wants to bite me, I should stare directly into his eyes. If a dog is wagging his tail, it is probably safe. I should stay away from dogs that are eating or sleeping.
Dogs also bite because they are protecting a home or a car.
It is always a good idea to ask permission before touching someone else's dog. Move slowly and talk quietly. A dog can think you are challenging it if you stare at it. Aggressive dogs may wag their tails with the tail straight up in the air. Animals need their privacy and space just like humans do.