DEV Community

Weerasak Chongnguluam
Weerasak Chongnguluam

Posted on

3 3

เชื่อมต่อ TLS Services ด้วย openssl s_client

TLS (Transport Layer Security) เป็น protocol เพื่อทำให้การเชื่อมและการส่งผ่านข้อมูลนั้นถูกเข้ารหัสเพื่อความปลอดภัย ซึ่งปกติถ้าไม่ได้ใช้ TLS เราสามารถเชื่อมต่อ TCP ได้โดยใช้ program อย่าง nc หรือ telnet ทีนี้ถ้าเกิด server ใช้ TLS เราอยากทดสอบเชื่อมต่อแบบ telnet หรือ nc เราสามารถใช้ openssl s_client แทนได้ ตัวอย่างการใช้งานเช่น

> openssl s_client -connect httpbin.org:443
CONNECTED(00000006)
depth=4 C = US, O = "Starfield Technologies, Inc.", OU = Starfield Class 2 Certification Authority
verify return:1
depth=3 C = US, ST = Arizona, L = Scottsdale, O = "Starfield Technologies, Inc.", CN = Starfield Services Root Certificate Authority - G2
verify return:1
depth=2 C = US, O = Amazon, CN = Amazon Root CA 1
verify return:1
depth=1 C = US, O = Amazon, OU = Server CA 1B, CN = Amazon
verify return:1
depth=0 CN = httpbin.org
verify return:1
---
Certificate chain
 0 s:/CN=httpbin.org
   i:/C=US/O=Amazon/OU=Server CA 1B/CN=Amazon
 1 s:/C=US/O=Amazon/OU=Server CA 1B/CN=Amazon
   i:/C=US/O=Amazon/CN=Amazon Root CA 1
 2 s:/C=US/O=Amazon/CN=Amazon Root CA 1
   i:/C=US/ST=Arizona/L=Scottsdale/O=Starfield Technologies, Inc./CN=Starfield Services Root Certificate Authority - G2
 3 s:/C=US/ST=Arizona/L=Scottsdale/O=Starfield Technologies, Inc./CN=Starfield Services Root Certificate Authority - G2
   i:/C=US/O=Starfield Technologies, Inc./OU=Starfield Class 2 Certification Authority
---
Server certificate
-----BEGIN CERTIFICATE-----
....
-----END CERTIFICATE-----
subject=/CN=httpbin.org
issuer=/C=US/O=Amazon/OU=Server CA 1B/CN=Amazon
No client certificate CA names sent
Server Temp Key: ECDH, P-256, 256 bits
---
SSL handshake has read 5494 bytes and written 322 bytes
---
New, TLSv1/SSLv3, Cipher is ECDHE-RSA-AES128-GCM-SHA256
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
....
Start Time: 1614418579
    Timeout   : 7200 (sec)
    Verify return code: 0 (ok)
---
Enter fullscreen mode Exit fullscreen mode

เราจะเห็น log การเชื่อมต่อของ TLS เกิดขึ้น เรื่องการ handshake ต่างๆ ให้เราเห็นด้วย

จากนั้นจะขึ้น --- หลังจากนี้ก็พร้อมให้เราส่ง message ตาม protocol HTTP ต่อจากนั้นได้เลยเช่น

GET /get HTTP/1.0
Content-Type: application/json
Accept: application/json

Enter fullscreen mode Exit fullscreen mode

ก็จะได้ response กลับออกมาแบบนี้

GET /get HTTP/1.0
Content-Type: application/json
Accept: application/json

HTTP/1.1 200 OK
Date: Sat, 27 Feb 2021 09:44:22 GMT
Content-Type: application/json
Content-Length: 392
Connection: close
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

{
  "args": {}, 
  "headers": {
    "Accept": "application/json", 
    "Content-Type": "application/json", 
    "Host": "a0207c42-pmhttpbin-pmhttpb-c018-592832243.us-east-1.elb.amazonaws.com", 
    "X-Amzn-Trace-Id": "Root=1-603a1476-74ca54983e04bc07559eb7dc"
  }, 
  "origin": "159.192.223.20", 
  "url": "https://a0207c42-pmhttpbin-pmhttpb-c018-592832243.us-east-1.elb.amazonaws.com/get"
}
closed
Enter fullscreen mode Exit fullscreen mode

จะเห็นว่า openssl s_client มีประโยชน์มากๆ ในการที่จะช่วยเรา debug การทำงานของ TLS และทดสอบส่ง request ไปหา server ที่ใช้ TLS ด้วยนั่นเอง

ขอฝาก Buy Me a Coffee

สำหรับท่านใดที่อ่านแล้วชอบโพสต์ต่างๆของผมที่นี่ ต้องการสนับสนุนค่ากาแฟเล็กๆน้อยๆ สามารถสนับสนุนผมได้ผ่านทาง Buy Me a Coffee คลิ๊กที่รูปด้านล่างนี้ได้เลยครับ

Buy Me A Coffee

ส่วนท่านใดไม่สะดวกใช้บัตรเครดิต หรือ Paypal สามารถสนับสนุนผมได้ผ่านทาง PromptPay โดยดู QR Code ได้จากโพสต์ที่พินเอาไว้ได้ที่ Page DevDose ครับ https://web.facebook.com/devdoseth

ขอบคุณครับ 🙏

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)