ENع
→Certificates
الدرس 10 · التشفير

Certificates and PKI

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة11 خطوة5 أسئلةIntermediate
عن هذا الدرس

A public key alone does not say whose it is. A certificate binds a name to a public key and is signed by a certificate authority (CA), whose own certificate is signed by a root CA your computer already trusts. Follow example.com's certificate from its signing request to a six-line Python client that checks the chain, the name, the dates and revocation, and see why switching those checks off is the classic mistake.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 10 · تفاعلي
01 / 11

Whose key is this?

Your computer wants a private connection to example.com. The server sends a public key, but a key alone carries no name. An impostor could send their own key instead. Your computer needs proof of whose key it is.

public key   lets anyone lock data for, or check
             signatures from, the key's owner
problem      the key does not say who owns it
needed       a trusted statement: this key
             belongs to example.com
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Whose key is this?

    Your computer wants a private connection to example.com. The server sends a public key, but a key alone carries no name. An impostor could send their own key instead. Your computer needs proof of whose key it is.

    public key   lets anyone lock data for, or check
                 signatures from, the key's owner
    problem      the key does not say who owns it
    needed       a trusted statement: this key
                 belongs to example.com
  2. A certificate binds name and key

    A certificate is a signed statement that ties a name to a public key. The X.509 kind used on the web lists the subject name, its public key, who issued it, valid-from and valid-to dates, and the issuer's digital signature.

    X.509 certificate (RFC 5280)
      subject         example.com
      subjectAltName  DNS:example.com, DNS:www.example.com
      public key      the server's public key
      issuer          Intermediate CA
      validity        notBefore .. notAfter
      signature       made with the issuer's private key
  3. A CA signs it

    The server makes a key pair and keeps the private key secret. It sends a certificate signing request (CSR) with its name and public key to a certificate authority (CA). The CA checks the server controls the domain, then signs.

    1  server   makes a key pair; the private key never leaves
    2  server → CA   CSR: name + public key
    3  CA       checks control of the domain
    4  CA       signs with ITS private key
    5  CA → server   the certificate
  4. The chain of trust

    Who vouches for the CA? A root CA signs the intermediate CA's certificate. The root's private key stays offline and is used rarely; the intermediate does the daily signing. Leaf, intermediate, root: a chain of trust.

    root CA          signs the intermediate's certificate
                     private key offline, used rarely
    intermediate CA  signs servers' certificates
    leaf             example.com
    PKI              the CAs, rules and systems that
                     issue and manage certificates
  5. Your trust store

    Your computer already holds a trust store: root certificates it trusts, shipped with the operating system or browser. These are trust anchors. Line 3, ssl.create_default_context(), loads them and turns on certificate and hostname checks.

    trust anchor   a root certificate trusted in advance
    trust store    the list of them on your computer
    line 3         ssl.create_default_context()
      verify_mode  = CERT_REQUIRED
      check_hostname = True
      CA certs     = the system defaults
  6. The server shows its chain

    Lines 4 and 5 connect and start TLS. In the handshake the server sends its certificate plus the intermediate's. It need not send the root: your computer must already trust that. The checks run before any data flows.

    line 4   TCP connection to port 443
    line 5   TLS handshake, server_hostname=host
    server → your computer
      1  leaf          example.com
      2  intermediate  Intermediate CA
         (the root comes from your own trust store)
  7. Check every signature

    Check the chain. The leaf's signature must verify with the intermediate's public key, and the intermediate's with the root's. That root must be in your trust store. Anyone can make a certificate; only a trusted CA's signature counts.

    leaf          signed by Intermediate CA?  ✓
    intermediate  signed by Root CA?          ✓
    Root CA       in my trust store?          ✓
    CHAIN         OK
  8. Check the name and dates

    server_hostname must match a name in the certificate's subject alternative name list: example.com does. Today must fall between its valid-from and valid-to dates. An expired certificate fails, even when every signature is good.

    asked for     example.com
    certificate   DNS:example.com, DNS:www.example.com
    NAME          OK
    notBefore  ≤  now  ≤  notAfter
    DATES         OK
  9. Not revoked

    A CA can revoke a certificate early, for example if its private key is stolen. Clients learn this from a revocation list (CRL) or an OCSP responder. Short lifetimes also limit harm: the maximum is falling from 398 to 47 days by 2029.

    revoked   cancelled by the CA before notAfter
    CRL       a signed list of revoked serial numbers
    OCSP      ask the CA: is serial N still good?
    REVOKED?  NO
    note      Python's default context does not
              check revocation; browsers do, in
              their own ways
  10. Trusted, then encrypted

    Every check passes, so the connection is trusted and encrypted. Line 6 prints the certificate's valid-to date. If any check fails, line 5 raises ssl.SSLCertVerificationError and no data is sent. That error is protection, not a nuisance.

    all checks pass  → encrypted connection
    line 6  s.getpeercert()["notAfter"]
            e.g. "Dec 25 22:56:35 2026 GMT"
    any check fails  → ssl.SSLCertVerificationError
                       raised in line 5
  11. The classic mistake

    The classic mistake is silencing that error with check_hostname = False and verify_mode = ssl.CERT_NONE. Then any certificate, even an impostor's, is accepted. The fix: keep the checks and solve the real cause, like an expired certificate.

    mistake (CWE-295, improper certificate validation)
      ctx.check_hostname = False
      ctx.verify_mode = ssl.CERT_NONE
      → every check OFF; impostors accepted
    fix: keep the defaults and fix the cause
      expired          renew it (automate renewal)
      missing chain    serve the intermediate too
      private CA       add its root: load_verify_locations()

اختبار يتاح بعد الخطوة الأخيرة.