Can any CA sign any cert for any domain?
If the answer is yes, what prevents having two different CAs creating a valid cert for the same domain?
Does that mean that the whole TLS security has the same level of security of the least secure CA?
Can any CA sign any cert for any domain?
If the answer is yes, what prevents having two different CAs creating a valid cert for the same domain?
Does that mean that the whole TLS security has the same level of security of the least secure CA?
Mostly yes, any CA in your trusted root, (or subordinates) can issue a cert for any DNS name.
Name constraints and Enhanced Key Usage can be used to mitigate this, but they aren't enforced everywhere.
DANE, Certificate Pinning, and Certificate transparency are a few projects that help protect from this risk.
Can any CA sign any cert for any domain?
In general, yes. Trusted root certs are trusted for anything under the root.
If the answer is yes, what prevents having two different CAs creating a valid cert for the same domain?
Nothing - it's completely legitimate for you, the owner of example.com, to go get a certificate for www.example.com issued by a collection of CAs: Comodo, Entrust, Thawte, whoever. (You usually wouldn't, because it's additional expense with no gain to you, but there's nothing that stops you. This situation usually arises when an organization wants to transition from using one CA to another; during the cutover, they'll usually have valid certs from both CAs for the same set of names.)
Your true concern here is "what prevents a fraudulent certificate from being issued by CA X when a valid certificate is already issued by CA Y". And in this case, the problem is not that a cert can be issued by multiple CAs, it's that a cert can be issued fraudulently. The purpose of "trusted CA" is that you trust the CAs to be diligent in issuing certs to valid and not to unauthorized purchasers.
Does that mean that the whole TLS security has the same level of security of the least secure CA?
Yup! (Unless you're pinning). It's a legitimate weakness of the CA system. (And the weakness here, to be clear, is that any one CA can be tricked or evil and issue certs that don't belong - not that they can do so if a cert in that name already exists).
It's prevented through legal (contractual) and not technical means.
What happens if a CA creates an certificate which is not duly authorized by the legitimate domain owner:
This has happened before:
Others have mentioned mitigation techniques, here are some examples:
Antivirus manufacturers like Kaspersky frequently install a CA in order to "protect" you by eavesdropping on all your connections, including SSL links.
In February 2015, media covered the SuperFish adware / malware deliberately installed by Lenovo on its computers*.
It comes with a CA certificate and key to dynamically issue host certificates.
One major issue here is that the CA is not only installed on your computer but it is part of the software and thus the same for all computers.
EFF found thousands of certificate counterfeits via HTTPS Everywhere / SSL Observatory (see below).
Want to gift SuperFish users with signed software or valid SSL sites?
Here is the key and cert to do it, along with a story how it was "open-sourced". Hurry up, Microsoft etc. are sending out updates to clean this mess up.
Fiddler is an HTTP proxy with SSL interception capability. It will install a CA certificate to tap into encrypted links.
Some companies or their firewalls intercept encrypted traffic to detect intrusions and data leaks, or to spy on their employees. Usually corporate computers and mobile devices will come with the company's CAs, so you will not get any warning unless you visit a certificate-pinned site.
Other than those, fake certificates for big sites like Google, Facebook or iTunes have been found in the wild in recent years.
Also, EV (extended validation) certificates - those that display a green lock or a green address bar in major browsers - were invented to profit from and exploit that flaw in the certification system and the related FUD.
EV CAs are not user-editable, in contrast to regular certs, and issuers and browser vendors promise to apply higher validation and security standards for those.
However, this is still just convention, little more.