Open Source How-To

You are here: Home > 3. Ubuntu 16.04 Server > ติดตั้ง ssl-cert บน ubuntu 16.04 server

ติดตั้ง ssl-cert บน ubuntu 16.04 server

 
 

บันทึกนี้ปรับปรุงล่าสุดเมื่อวันที่ 17-10-2560
ดูแลโดย WIBOON

 

  • โดยทั่วไปแล้ว เรามักไม่ได้เป็นผู้ติดตั้งโปรแกรมนี้ แต่โปรแกรมนี้มักจะถูกติดตั้งไปพร้อม ๆ กับการติดตั้งโปรแกรม web server เช่น apache2 web server เป็นต้น
  • แต่เพื่อความเข้าใจ เราควรจะรู้ว่า นอกจากใช้กับ web server แล้ว บางทีโปรแกรมที่ต้องการใช้งานเรื่องความปลอดภัยแบบ TLS ก็ต้องใช้ แต่หากว่ายังไม่ได้ติดตั้งไว้ ก็จะทำให้ติดตั้งโปรแกรมดังกล่าวไม่ผ่าน เช่น LDAPS เป็นต้น
  • ssl-cert ถูกนำไปใช้เป็นค่า default โดยเรียกว่า Self-signed Certificate
  • หลังจากติดตั้งจะได้ไดเรกทอรี /etc/ssl และเกิด group ssl-cert และมีชื่อเรียกว่า ssl-cert-snakeoil

 

1.ติดตั้งโปรแกรม

mama@ubuntu:~$ sudo apt install ssl-cert

 

2.ตรวจสอบดู file permission และ file owner ในไดเรกทอรี /etc/ssl

mama@ubuntu:~$ ls -l /etc/ssl/
total 36
drwxr-xr-x 2 root root     20480 Aug 18 16:12 certs
-rw-r--r-- 1 root root     10835 Apr 15 12:28 openssl.cnf
drwx--x--- 2 root ssl-cert  4096 Aug 18 16:12 private


และไดเรกทอรี /etc/ssl/private/ ต้องใช้สิทธิ root จึงจะดูรายชื่อไฟล์ที่เก็บอยู่ได้ อย่างนี้แสดงว่าติดตั้งถูกต้อง

mama@sysadmin:~$ ls -l /etc/ssl/private/
ls: cannot open directory '/etc/ssl/private/': Permission denied
mama@ubuntu:~$ sudo ls -l /etc/ssl/private/
total 4
-rw-r----- 1 root ssl-cert 1704 Aug 18 16:12 ssl-cert-snakeoil.key

ตรวจสอบดูว่าในไดเรกทอรี /etc/ssl/certs มี cert ของบริษัทต่าง ๆ ซึ่งไดเรกทอรีนี้เปิดดูรายการไฟล์ได้

mama@ubuntu:~$ ls -l /etc/ssl/certs/
.
.
.
lrwxrwxrwx 1 root root     63 Apr 22 16:32 Comodo_AAA_Services_root.pem -> /usr/share/ca-certificates/mozilla/
Comodo_AAA_Services_root.crt lrwxrwxrwx 1 root root 69 Apr 22 16:32 COMODO_Certification_Authority.pem -> /usr/share/ca-certificates/mozilla/
COMODO_Certification_Authority.crt lrwxrwxrwx 1 root root 73 Apr 22 16:32 COMODO_ECC_Certification_Authority.pem -> /usr/share/ca-certificates/mozilla/
COMODO_ECC_Certification_Authority.crt lrwxrwxrwx 1 root root 73 Apr 22 16:32 COMODO_RSA_Certification_Authority.pem -> /usr/share/ca-certificates/mozilla/
COMODO_RSA_Certification_Authority.crt lrwxrwxrwx 1 root root 66 Apr 22 16:32 Comodo_Secure_Services_root.pem -> /usr/share/ca-certificates/mozilla/
Comodo_Secure_Services_root.crt lrwxrwxrwx 1 root root 67 Apr 22 16:32 Comodo_Trusted_Services_root.pem -> /usr/share/ca-certificates/mozilla/
Comodo_Trusted_Services_root.crt . . .

 

3.ตัวอย่าง ssl-cert กับ apache2 web server
จะมีตัวอย่างบรรทัดการใช้งาน SSL อยู่ที่ไฟล์ /etc/apache2/sites-available/default-ssl.conf

                SSLEngine on
                #   A self-signed (snakeoil) certificate can be created by installing
                #   the ssl-cert package. See
                #   /usr/share/doc/apache2/README.Debian.gz for more info.
                #   If both key and certificate are stored in the same file, only the
                #   SSLCertificateFile directive is needed.
                SSLCertificateFile      /etc/ssl/certs/ssl-cert-snakeoil.pem
                SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key


หากเราไม่ต้องการใช้ค่า default ที่ให้มา เราก็ต้องซื้อ ssl certificate มาเอง แล้วนำไฟล์มาวางไว้ที่ใดก็ได้ หรือ จะวางไว้ที่เดิมที่ /etc/ssl/private/ ก็ได้ เพื่อความสะดวกเรามักจะสร้างไฟล์คอนฟิกใหม่ เช่น mysite-ssl.conf ให้แทนที่คำว่า mysite ด้วยชื่อเว็บไซต์เพื่อให้เข้าใจได้ง่าย
ในตัวอย่างข้างล่างนี้ สมมติว่าบริษัทที่ขาย cert ส่งไฟล์มาให้มี 3 ไฟล์ คือ yourcert.crt, yourcert.key และ yourcert.ca-bundle

mama@ubuntu:~$ sudo more /etc/apache2/sites-enabled/mysite-ssl.conf
<IfModule mod_ssl.c>
   <VirtualHost *:443>
        DocumentRoot "/var/www/wordpress"
        ServerName xxx.xxx.xxxx.xx
        <Directory /var/www/wordpress/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride all
                Order allow,deny
                allow from all
        </Directory>
        SSLEngine on
        SSLCertificateFile /etc/ssl/private/yourcert.crt
        SSLCertificateKeyFile /etc/ssl/private/yourcert.key
        SSLCertificateChainFile /etc/ssl/private/yourcert.ca-bundle
   </VirtualHost>
</IfModule>


4.ตัวอย่าง ssl-cert กับ haproxy
การทำ haproxy ssl terminate
หากไม่มี cert ที่ซื้อมาก็ทดสอบได้ด้วย ssl-cert-snakeoil.pem
ขั้นตอน
ติดตั้ง ssl-cert-snakeoil.pem ดังนี้

sudo su -
apt install ssl-cert
cd /etc/ssl/private/
cat ../certs/ssl-cert-snakeoil.pem ssl-cert-snakeoil.key > my.pem
chgrp ssl-cert my.pem
exit

แล้วนำ my.pem ไปใช้ใน haproxy.cfg ดังนี้

frontend www-https
   bind *:443 ssl crt /etc/ssl/private/my.pem


5.กรณีที่ LDAP server ติดตั้งเป็น LDAPS แบบใช้ self-signed certificate ของ ssl-cert บน ubuntu
การใช้คำสั่งจาก LDAP client เช่น
$ ldapadd -H ldaps://ldap.example.com -f ./src/create-students.ldif -x -D "cn=admin,dc=example,dc=com" -w 123456
จะพบข้อความว่า

ldap_sasl_bind(SIMPLE): Can't contact LDAP server (-1)<br/>

ให้ทำคำสั่ง 2 บรรทัดข้างล่างนี้ เพื่อเพิ่ม certificate จาก LDAP server แล้วลองคำสั่งอีกครั้งจะทำได้

$ openssl s_client -connect ldap.example.com:636 -showcerts </dev/null 2>/dev/null | \
openssl x509 -outform PEM | \
sudo tee /usr/local/share/ca-certificates/ldap.example.com.crt $ sudo update-ca-certificates


6.กรณีที่ connect https ที่เป็นแบบใช้ self-signed certificate ของ ssl-cert บน ubuntu
เช่น การใช้ docker login เข้าไปยัง Harbor Registry ที่ command line prompt

$ docker login docker-registry.in.psu.ac.th
Username: lulu
Password:

จะพบข้อความแบบนี้

Error response from daemon: Get https://docker-registry.in.psu.ac.th/v2/: x509: certificate signed by unknown authority

เนื่องจากไม่ได้ใช้ certificates จริง เราต้องทำที่ client ทุกเครื่อง ด้วยคำสั่งเหล่านี้ ก่อนที่จะใช้งานกับ Harbor Registry ได้ ทำดังนี้

$ export DOMAIN_NAME=docker-registry.in.psu.ac.th
$ openssl s_client -connect $DOMAIN_NAME:443 -showcerts </dev/null 2>/dev/null | \
openssl x509 -outform PEM | sudo tee /usr/local/share/ca-certificates/$DOMAIN_NAME.crt $ sudo update-ca-certificates
nach oben