العودة إلى التحديثات
New releaseSep 8, 2026

strongswan v6.1.0

strongSwan - VPN قائم على IPsec

مشاركة

إعداد strongSwan

نظرة عامة

strongSwan هو حل VPN مفتوح المصدر قائم على IPsec.

هذه الوثيقة ليست سوى مقدمة قصيرة لأمر strongSwan swanctl الذي يستخدم vici الحديث واجهة إعداد IKE متعددة الاستخدامات. أما الأمر ipsec المُهمل الذي يستخدم واجهة الإعداد القديمة stroke فموصوف هنا. للحصول على معلومات أكثر تفصيلاً، راجع صفحات الدليل (man pages)، وموقع التوثيق الجديد، والويكي القديم.

بدء سريع

تم إصدار الشهادات للمستخدمين والمضيفين والبوابات من قِبل مرجع شهادات (CA) خيالي لـ strongSwan. في سيناريوهات الأمثلة لدينا، يجب أن تكون شهادة CA strongswanCert.pem حاضرة على جميع نقاط نهاية VPN حتى يمكن المصادقة على الأقران. بالنسبة لتطبيق VPN الخاص بك، يمكنك إما استخدام شهادات من أي مرجع شهادات تابع لجهة خارجية أو توليد المفاتيح الخاصة والشهادات اللازمة بنفسك باستخدام أداة pki الخاصة بـ strongSwan، وسيتم شرح استخدامها في أحد الأقسام أدناه.

حالة موقع-إلى-موقع

في هذا السيناريو، تقوم بوابتا أمان moon و_sun_ بربط الشبكتين الفرعيتين moon-net و_sun-net_ ببعضهما عبر نفق VPN يُقام بين البوابتين:

10.1.0.0/16 -- | 192.168.0.1 | === | 192.168.0.2 | -- 10.2.0.0/16
  moon-net          moon                 sun           sun-net

الإعداد على البوابة moon:

/etc/swanctl/x509ca/strongswanCert.pem
/etc/swanctl/x509/moonCert.pem
/etc/swanctl/private/moonKey.pem

/etc/swanctl/swanctl.conf:

    connections {
        net-net {
            remote_addrs = 192.168.0.2

            local {
                auth = pubkey
                certs = moonCert.pem
            }
            remote {
                auth = pubkey
                id = "C=CH, O=strongSwan, CN=sun.strongswan.org"
            }
            children {
                net-net {
                    local_ts  = 10.1.0.0/16
                    remote_ts = 10.2.0.0/16
                    start_action = trap
                }
            }
        }
    }

الإعداد على البوابة sun:

/etc/swanctl/x509ca/strongswanCert.pem
/etc/swanctl/x509/sunCert.pem
/etc/swanctl/private/sunKey.pem

/etc/swanctl/swanctl.conf:

    connections {
        net-net {
            remote_addrs = 192.168.0.1

            local {
                auth = pubkey
                certs = sunCert.pem
            }
            remote {
                auth = pubkey
                id = "C=CH, O=strongSwan, CN=moon.strongswan.org"
            }
            children {
                net-net {
                    local_ts  = 10.2.0.0/16
                    remote_ts = 10.1.0.0/16
                    start_action = trap
                }
            }
        }
    }

الهويتان المحلية والبعيدة المستخدمتان في هذا السيناريو هما subjectDistinguishedNames الموجودان في شهادات الكيان النهائي. يتم تحميل الشهادات والمفاتيح الخاصة في الخفي charon بالأمر

swanctl --load-creds

بينما

swanctl --load-conns

يقوم بتحميل الاتصالات المعرّفة في swanctl.conf. مع start_action = trap، يتم إعداد اتصال IPsec تلقائيًا عند أول حزمة IP ذات حمولة نصية صريحة تريد المرور عبر النفق.

حالة مضيف-إلى-مضيف

هذا إعداد بين مضيفين منفردين لا توجد خلفهما شبكة فرعية. على الرغم من أن وضع النقل لـ IPsec سيكون كافيًا لاتصالات المضيف-إلى-مضيف، إلا أننا سنستخدم وضع النفق الافتراضي لـ IPsec.

| 192.168.0.1 | === | 192.168.0.2 |
     moon                sun

الإعداد على المضيف moon:

/etc/swanctl/x509ca/strongswanCert.pem
/etc/swanctl/x509/moonCert.pem
/etc/swanctl/private/moonKey.pem

/etc/swanctl/swanctl.conf:

    connections {
        host-host {
            remote_addrs = 192.168.0.2

            local {
                auth=pubkey
                certs = moonCert.pem
            }
            remote {
                auth = pubkey
                id = "C=CH, O=strongSwan, CN=sun.strongswan.org"
            }
            children {
                net-net {
                    start_action = trap
                }
            }
        }
    }

الإعداد على المضيف sun:

/etc/swanctl/x509ca/strongswanCert.pem
/etc/swanctl/x509/sunCert.pem
/etc/swanctl/private/sunKey.pem

/etc/swanctl/swanctl.conf:

    connections {
        host-host {
            remote_addrs = 192.168.0.1

            local {
                auth = pubkey
                certs = sunCert.pem
            }
            remote {
                auth = pubkey
                id = "C=CH, O=strongSwan, CN=moon.strongswan.org"
            }
            children {
                host-host {
                    start_action = trap
                }
            }
        }
    }

حالة Roadwarrior

هذه حالة شائعة جدًا حيث تخدم بوابة strongSwan عددًا عشوائيًا من عملاء VPN البعيدين الذين عادةً ما تكون عناوين IP الخاصة بهم ديناميكية.

10.1.0.0/16 -- | 192.168.0.1 | === | x.x.x.x |
  moon-net          moon              carol

الإعداد على البوابة moon:

/etc/swanctl/x509ca/strongswanCert.pem
/etc/swanctl/x509/moonCert.pem
/etc/swanctl/private/moonKey.pem

/etc/swanctl/swanctl.conf:

    connections {
        rw {
            local {
                auth = pubkey
                certs = moonCert.pem
                id = moon.strongswan.org
            }
            remote {
                auth = pubkey
            }
            children {
                net-net {
                    local_ts  = 10.1.0.0/16
                }
            }
        }
    }

الإعداد على Roadwarrior carol:

/etc/swanctl/x509ca/strongswanCert.pem
/etc/swanctl/x509/carolCert.pem
/etc/swanctl/private/carolKey.pem

/etc/swanctl/swanctl.conf:

    connections {
        home {
            remote_addrs = moon.strongswan.org

            local {
                auth = pubkey
                certs = carolCert.pem
                id = [email protected]
            }
            remote {
                auth = pubkey
                id = moon.strongswan.org
            }
            children {
                home {
                    local_ts  = 10.1.0.0/16
                    start_action = start
                }
            }
        }
    }

بالنسبة إلى remote_addrs، تم اختيار اسم المضيف moon.strongswan.org والذي سيتم حله بواسطة DNS وقت التشغيل إلى عنوان IP الوجهة المقابل. في هذا السيناريو، هوية Roadwarrior carol هي عنوان البريد الإلكتروني [email protected] والذي يجب تضمينه كـ subjectAlternativeName في شهادة Roadwarrior carolCert.pem.

حالة Roadwarrior مع IP افتراضي

عادةً ما يكون لدى عملاء Roadwarrior عناوين IP ديناميكية يعينها مزود خدمة الإنترنت المتصلين به حاليًا. من أجل تبسيط التوجيه من moon-net عائدًا إلى عميل الوصول البعيد carol، سيكون من المرغوب فيه أن يكون لدى Roadwarrior عنوان IP داخلي مُختار من مجموعة عناوين محددة مسبقًا.

10.1.0.0/16 -- | 192.168.0.1 | === | x.x.x.x | -- 10.3.0.1
  moon-net          moon              carol       virtual IP

في مثالنا، يتم اختيار عنوان IP الافتراضي من مجموعة العناوين 10.3.0.0/16 والتي يمكن إعدادها بإضافة القسم

pools {
    rw_pool {
        addrs = 10.3.0.0/16
    }
}

إلى swanctl.conf الخاص بالبوابة، حيث يتم تحميلها في الخفي charon باستخدام الأمر

swanctl --load-pools

لطلب عنوان IP من هذه المجموعة، يمكن لـ Roadwarrior استخدام وضع إعداد IKEv1 (mode config) أو حمولات إعداد IKEv2. الإعداد لكليهما هو نفسه

vips = 0.0.0.0

الإعداد على البوابة moon:

/etc/swanctl/x509ca/strongswanCert.pem
/etc/swanctl/x509/moonCert.pem
/etc/swanctl/private/moonKey.pem

/etc/swanctl/swanctl.conf:

    connections {
        rw {
            pools = rw_pool

            local {
                auth = pubkey
                certs = moonCert.pem
                id = moon.strongswan.org
            }
            remote {
                auth = pubkey
            }
            children {
                net-net {
                    local_ts  = 10.1.0.0/16
                }
            }
        }
    }

    pools {
        rw_pool {
            addrs = 10.30.0.0/16
        }
    }

الإعداد على Roadwarrior carol:

/etc/swanctl/x509ca/strongswanCert.pem
/etc/swanctl/x509/carolCert.pem
/etc/swanctl/private/carolKey.pem

/etc/swanctl/swanctl.conf:

    connections {
        home {
            remote_addrs = moon.strongswan.org
            vips = 0.0.0.0

            local {
                auth = pubkey
                certs = carolCert.pem
                id = [email protected]
            }
            remote {
                auth = pubkey
                id = moon.strongswan.org
            }
            children {
                home {
                    local_ts  = 10.1.0.0/16
                    start_action = start
                }
            }
        }
    }

حالة Roadwarrior مع مصادقة EAP

هذه حالة شائعة جدًا حيث تخدم بوابة strongSwan عددًا عشوائيًا من عملاء VPN البعيدين الذين يصادقون على أنفسهم عبر بروتوكول المصادقة الموسع القائم على كلمة المرور مثل EAP-MD5 أو EAP-MSCHAPv2.

10.1.0.0/16 -- | 192.168.0.1 | === | x.x.x.x |
  moon-net          moon              carol

الإعداد على البوابة moon:

/etc/swanctl/x509ca/strongswanCert.pem
/etc/swanctl/x509/moonCert.pem
/etc/swanctl/private/moonKey.pem

/etc/swanctl/swanctl.conf:

    connections {
        rw {
            local {
                auth = pubkey
                certs = moonCert.pem
                id = moon.strongswan.org
            }
            remote {
                auth = eap-md5
            }
            children {
                net-net {
                    local_ts  = 10.1.0.0/16
                }
            }
            send_certreq = no
        }
    }

يحتوي ملف swanctl.conf أيضًا على قسم secrets يحدد جميع بيانات اعتماد العملاء

    secrets {
        eap-carol {
            id = [email protected]
            secret = Ar3etTnp
        }
        eap-dave {
            id = [email protected]
            secret = W7R0g3do
        }
    }

الإعداد على Roadwarrior carol:

/etc/swanctl/x509ca/strongswanCert.pem

/etc/swanctl/swanctl.conf:

    connections {
        home {
            remote_addrs = moon.strongswan.org

            local {
                auth = eap
                id = [email protected]
            }
            remote {
                auth = pubkey
                id = moon.strongswan.org
            }
            children {
                home {
                    local_ts  = 10.1.0.0/16
                    start_action = start
                }
            }
        }
    }

    secrets {
        eap-carol {
            id = [email protected]
            secret = Ar3etTnp
        }
    }

حالة Roadwarrior مع هوية EAP

غالبًا ما يتم تبادل هوية EAP الخاصة بالعميل عبر EAP وتختلف عن هوية IKEv2 الخارجية. في هذا المثال، تكون هوية IKEv2 افتراضيًا عنوان IPv4 الخاص بالعميل.

10.1.0.0/16 -- | 192.168.0.1 | === | x.x.x.x |
  moon-net          moon              carol

الإعداد على البوابة moon:

/etc/swanctl/x509ca/strongswanCert.pem
/etc/swanctl/x509/moonCert.pem
/etc/swanctl/private/moonKey.pem

/etc/swanctl/swanctl.conf:

    connections {
        rw {
            local {
                auth = pubkey
                certs = moonCert.pem
                id = moon.strongswan.org
            }
            remote {
                auth = eap-md5
                eap_id = %any
            }
            children {
                net-net {
                    local_ts  = 10.1.0.0/16
                }
            }
            send_certreq = no
        }
    }

    secrets {
        eap-carol {
            id = carol
            secret = Ar3etTnp
        }
        eap-dave {
            id = dave
            secret = W7R0g3do
        }
    }

الإعداد على Roadwarrior carol:

/etc/swanctl/x509ca/strongswanCert.pem

/etc/swanctl/swanctl.conf:

    connections {
        home {
            remote_addrs = moon.strongswan.org

            local {
                auth = eap
                eap_id = carol
            }
            remote {
                auth = pubkey
                id = moon.strongswan.org
            }
            children {
                home {
                    local_ts  = 10.1.0.0/16
                    start_action = start
                }
            }
        }
    }

    secrets {
        eap-carol {
            id = carol
            secret = Ar3etTnp
        }
    }

توليد الشهادات وقوائم إلغاء الشهادات (CRLs)

هذا القسم ليس برنامجًا تعليميًا كاملاً حول كيفية استخدام أداة strongSwan pki. إنه يسرد فقط بضع نقاط ذات صلة إذا كنت تريد توليد الشهادات وقوائم CRL الخاصة بك لاستخدامها مع strongSwan.

توليد شهادة CA

عبارة pki

pki --gen --type ed25519 --outform pem > strongswanKey.pem

تولّد مفتاحًا إهليلجيًا من نوع Edwards-Curve بقوة تشفير تبلغ 128 بتًا. يتم تضمين المفتاح العام المقابل في شهادة CA موقعة ذاتيًا بعمر يبلغ 10 سنوات (3652 يومًا)

pki --self --ca --lifetime 3652 --in strongswanKey.pem \
           --dn "C=CH, O=strongSwan, CN=strongSwan Root CA" \
           --outform pem > strongswanCert.pem

والتي يمكن عرضها بالأمر

pki --print --in strongswanCert.pem

subject:  "C=CH, O=strongSwan, CN=strongSwan Root CA"
issuer:   "C=CH, O=strongSwan, CN=strongSwan Root CA"
validity:  not before May 18 08:32:06 2017, ok
           not after  May 18 08:32:06 2027, ok (expires in 3651 days)
serial:    57:e0:6b:3a:9a:eb:c6:e0
flags:     CA CRLSign self-signed
subjkeyId: 2b:95:14:5b:c3:22:87:de:d1:42:91:88:63:b3:d5:c1:92:7a:0f:5d
pubkey:    ED25519 256 bits
keyid:     a7:e1:6a:3f:e7:6f:08:9d:89:ec:23:92:a9:a1:14:3c:78:a8:7a:f7
subjkey:   2b:95:14:5b:c3:22:87:de:d1:42:91:88:63:b3:d5:c1:92:7a:0f:5d

إذا كنت تفضل أن يكون مفتاح CA الخاص وشهادة X.509 بصيغة DER الثنائية، فما عليك سوى حذف خيار --outform pem. يحتوي الدليل /etc/swanctl/x509ca على جميع شهادات CA المطلوبة إما بصيغة DER الثنائية أو بصيغة PEM Base64. وبغض النظر عن لاحقة الملف، سيتم تحديد الصيغة الصحيحة تلقائيًا بواسطة strongSwan.

توليد شهادة كيان طرفي لمضيف أو مستخدم

مرة أخرى نستخدم الأمر

pki --gen --type ed25519 --outform pem > moonKey.pem

لتوليد مفتاح خاص Ed25519 للمضيف moon. بدلاً من ذلك، يمكنك كتابة

pki --gen --type rsa --size 3072 > moonKey.der

لتوليد مفتاح RSA تقليدي بحجم 3072 بت وتخزينه بصيغة DER الثنائية. وكبديل، يمكن استخدام TPM 2.0 وحدة النظام الأساسي الموثوقة المتوفرة على كل منصة Intel حديثة كبطاقة ذكية افتراضية لتخزين مفتاح خاص RSA أو ECDSA بشكل آمن. للتفاصيل، راجع HOWTO الخاص بـ TPM 2.0.

في الخطوة التالية، يقوم الأمر

pki --req --type priv --in moonKey.pem \
          --dn "C=CH, O=strongswan, CN=moon.strongswan.org" \
          --san moon.strongswan.org --outform pem > moonReq.pem

بإنشاء طلب شهادة PKCS#10 يجب توقيعه بواسطة CA. من خلال الاستخدام [المتعدد] لمعامل --san، يمكن إضافة أي عدد من subjectAlternativeNames المطلوبة إلى الطلب. ويمكن أن تكون بالصيغة

--san sun.strongswan.org     # fully qualified host name
--san [email protected]   # RFC822 user email address
--san 192.168.0.1            # IPv4 address
--san fec0::1                # IPv6 address

بناءً على طلب الشهادة، تصدر CA شهادة كيان طرفي موقعة بالأمر التالي

pki --issue --cacert strongswanCert.pem --cakey strongswanKey.pem \
            --type pkcs10 --in moonReq.pem --serial 01 --lifetime 1826 \
            --outform pem > moonCert.pem

إذا تم حذف معامل --serial مع وسيطة سداسية عشرية، فسيتم توليد رقم تسلسلي عشوائي. تتطلب بعض عملاء VPN من جهات خارجية أن تحتوي شهادة بوابة VPN على علم استخدام المفتاح الموسع (EKU) الخاص بـ TLS Server Authentication والذي يمكن تضمينه بالخيار التالي

--flag serverAuth

إذا كنت تريد استخدام ميزة الجلب الديناميكي لقوائم CRL الموضحة في أحد الأقسام التالية، فيمكنك تضمين نقطة توزيع واحدة أو عدة crlDistributionPoints في شهادات الكيان الطرفي الخاصة بك باستخدام معامل --crl

--crl  http://crl.strongswan.org/strongswan.crl
--crl "ldap://ldap.strongswan.org/cn=strongSwan Root CA, o=strongSwan,c=CH?certificateRevocationList"

يمكن عرض شهادة المضيف الصادرة باستخدام

pki --print --in moonCert.pem

subject:  "C=CH, O=strongSwan, CN=moon.strongswan.org"
issuer:   "C=CH, O=strongSwan, CN=strongSwan Root CA"
validity:  not before May 19 10:28:19 2017, ok
           not after  May 19 10:28:19 2022, ok (expires in 1825 days)
serial:    01
altNames:  moon.strongswan.org
flags:     serverAuth
CRL URIs:  http://crl.strongswan.org/strongswan.crl
authkeyId: 2b:95:14:5b:c3:22:87:de:d1:42:91:88:63:b3:d5:c1:92:7a:0f:5d
subjkeyId: 60:9d:de:30:a6:ca:b9:8e:87:bb:33:23:61:19:18:b8:c4:7e:23:8f
pubkey:    ED25519 256 bits
keyid:     39:1b:b3:c2:34:72:1a:01:08:40:ce:97:75:b8:be:ce:24:30:26:29
subjkey:   60:9d:de:30:a6:ca:b9:8e:87:bb:33:23:61:19:18:b8:c4:7e:23:8f

عادةً، يحتاج عميل VPN القائم على Windows أو OSX أو Android أو iOS إلى مفتاحه الخاص، وشهادة المضيف أو المستخدم الخاصة به، وشهادة CA. الطريقة الأكثر ملاءمة لتحميل هذه المعلومات هي وضع كل شيء في حاوية PKCS#12:

openssl pkcs12 -export -inkey carolKey.pem \
               -in carolCert.pem -name "carol" \
               -certfile strongswanCert.pem -caname "strongSwan Root CA" \
               -out carolCert.p12

أداة strongSwan pki غير قادرة حاليًا على إنشاء حاويات PKCS#12 لذلك يجب استخدام openssl.

توليد قائمة CRL

يمكن توليد قائمة CRL فارغة موقعة بواسطة CA باستخدام الأمر

pki --signcrl --cacert strongswanCert.pem --cakey strongswanKey.pem \
              --lifetime 30 > strongswan.crl

إذا حذفت خيار --lifetime، فسيتم استخدام القيمة الافتراضية البالغة 15 يومًا. يمكن تحميل قوائم CRL إما إلى خادم HTTP أو LDAP أو وضعها بصيغة DER الثنائية أو صيغة PEM Base64 في الدليل /etc/swanctl/x509crl حيث يتم تحميلها في الخفي charon باستخدام الأمر

swanctl --load-creds### إبطال شهادة ###

يتم إبطال شهادة كيان نهائي محددة باستخدام الأمر

pki --signcrl --cacert strongswanCert.pem --cakey strongswanKey.pem \
              --lifetime 30 --lastcrl strongswan.crl \
              --reason key-compromise --cert moonCert.pem > new.crl

بدلاً من ملف الشهادة (في مثالنا moonCert.pem)، يمكن تحديد الرقم التسلسلي لشهادة المطلوب إبطالها باستخدام المعامل --serial. يوثّق الأمر pki --signcrl --help جميع أسباب الإبطال الممكنة، ولكن يمكن أيضًا حذف المعامل --reason. يمكن عرض محتوى ملف CRL الجديد باستخدام الأمر

pki --print --type crl --in new.crl

issuer:   "C=CH, O=strongSwan, CN=strongSwan Root CA"
update:    this on May 19 11:13:01 2017, ok
           next on Jun 18 11:13:01 2017, ok (expires in 29 days)
serial:    02
authKeyId: 2b:95:14:5b:c3:22:87:de:d1:42:91:88:63:b3:d5:c1:92:7a:0f:5d
1 revoked certificate:
  01: May 19 11:13:01 2017, key compromise

التخزين المؤقت المحلي لقوائم CRL

خيار strongswan.conf

charon {
    cache_crls = yes
}

يعمل على تفعيل التخزين المؤقت المحلي لقوائم CRL التي تم جلبها ديناميكيًا من خادم HTTP أو LDAP. تُخزَّن النسخ المؤقتة في /etc/swanctl/x509crl باستخدام اسم ملف فريد يتكون من subjectKeyIdentifier الخاص بالمُصدِر واللاحقة .crl.

مع النسخة المؤقتة، يكون CRL متاحًا فورًا بعد بدء التشغيل. وعندما تصبح النسخة المحلية قديمة، يتم جلب CRL محدّث تلقائيًا من أحد نقاط توزيع CRL المعرّفة أثناء مصادقة IKEv2 التالية.

الفئات