กลับไปหน้าสูตร
#openssl#tls#security#cheatsheet

OpenSSL Cheatsheet

รวมคำสั่ง OpenSSL แบบจัดเต็ม: key/csr/cert, inspect TLS, verify chain, convert formats, encrypt/decrypt และ troubleshooting

11 มีนาคม 2569อ่านประมาณ 2 นาที

Version & Help

Commandใช้ทำอะไร
openssl versionดูเวอร์ชัน
openssl helpดูคำสั่งทั้งหมด
openssl <subcommand> -helphelp คำสั่งย่อย

Generate Keys

Commandใช้ทำอะไร
openssl genrsa -out private.key 2048สร้าง RSA private key
openssl genpkey -algorithm RSA -out private.key -pkeyopt rsa_keygen_bits:4096RSA 4096
openssl ecparam -name prime256v1 -genkey -noout -out ec.keyสร้าง EC key
openssl rsa -in private.key -pubout -out public.keyดึง public key

CSR / Self-signed

Commandใช้ทำอะไร
openssl req -new -key private.key -out request.csrสร้าง CSR
openssl req -new -key private.key -out request.csr -subj "/C=TH/ST=BKK/O=OLANLAB/CN=example.com"CSR แบบ non-interactive
openssl req -x509 -new -nodes -key private.key -sha256 -days 365 -out cert.crtself-signed cert

Inspect Certificates

Commandใช้ทำอะไร
openssl x509 -in cert.crt -text -nooutดูรายละเอียด cert
openssl x509 -in cert.crt -noout -datesดูวันเริ่ม/หมดอายุ
openssl x509 -in cert.crt -noout -issuer -subjectissuer/subject
openssl x509 -in cert.crt -noout -fingerprint -sha256fingerprint

Inspect CSR / Keys

Commandใช้ทำอะไร
openssl req -in request.csr -text -nooutตรวจ CSR
openssl rsa -in private.key -check -nooutตรวจ RSA private key
openssl pkey -in private.key -text -nooutดู key details

Verify Certificate Chain

Commandใช้ทำอะไร
openssl verify -CAfile ca.crt cert.crtverify cert กับ CA
openssl verify -CAfile fullchain.pem cert.crtverify กับ full chain

Match Key, CSR, Cert (ต้องตรงกัน)

openssl x509 -noout -modulus -in cert.crt | openssl md5
openssl rsa  -noout -modulus -in private.key | openssl md5
openssl req  -noout -modulus -in request.csr | openssl md5

TLS Handshake / Remote Check

Commandใช้ทำอะไร
openssl s_client -connect example.com:443 -servername example.comตรวจ TLS handshake
openssl s_client -connect example.com:443 -servername example.com -showcertsแสดง cert chain
openssl s_client -connect example.com:443 -tls1_2บังคับ TLS 1.2
openssl s_client -connect example.com:443 -tls1_3บังคับ TLS 1.3

Convert Formats

Commandใช้ทำอะไร
openssl x509 -in cert.pem -outform der -out cert.derPEM -> DER
openssl x509 -in cert.der -inform der -out cert.pemDER -> PEM
openssl pkcs12 -export -inkey private.key -in cert.crt -out bundle.p12key+cert -> P12
openssl pkcs12 -in bundle.p12 -nodes -out bundle.pemP12 -> PEM

Encrypt / Decrypt File

Commandใช้ทำอะไร
openssl enc -aes-256-cbc -salt -in secret.txt -out secret.encเข้ารหัสไฟล์
openssl enc -d -aes-256-cbc -in secret.enc -out secret.txtถอดรหัส
openssl rand -hex 32สุ่ม key/secret

Hash / Digest

Commandใช้ทำอะไร
openssl dgst -sha256 file.txtSHA-256 hash
openssl dgst -sha512 file.txtSHA-512 hash
openssl base64 -in file.bin -out file.b64base64 encode
openssl base64 -d -in file.b64 -out file.binbase64 decode

Common TLS Troubleshooting

ปัญหาวิธีเช็ก
cert หมดอายุopenssl x509 -noout -dates
chain ไม่ครบs_client -showcerts + verify chain
CN/SAN ไม่ตรง domainตรวจ Subject Alternative Name
key ไม่ match certcompare modulus hash
protocol mismatchลอง -tls1_2 / -tls1_3