กลับไปหน้าสูตร
#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> -help | help คำสั่งย่อย |
Generate Keys
| Command | ใช้ทำอะไร |
|---|---|
openssl genrsa -out private.key 2048 | สร้าง RSA private key |
openssl genpkey -algorithm RSA -out private.key -pkeyopt rsa_keygen_bits:4096 | RSA 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.crt | self-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 -subject | issuer/subject |
openssl x509 -in cert.crt -noout -fingerprint -sha256 | fingerprint |
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.crt | verify cert กับ CA |
openssl verify -CAfile fullchain.pem cert.crt | verify กับ 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.der | PEM -> DER |
openssl x509 -in cert.der -inform der -out cert.pem | DER -> PEM |
openssl pkcs12 -export -inkey private.key -in cert.crt -out bundle.p12 | key+cert -> P12 |
openssl pkcs12 -in bundle.p12 -nodes -out bundle.pem | P12 -> 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.txt | SHA-256 hash |
openssl dgst -sha512 file.txt | SHA-512 hash |
openssl base64 -in file.bin -out file.b64 | base64 encode |
openssl base64 -d -in file.b64 -out file.bin | base64 decode |
Common TLS Troubleshooting
| ปัญหา | วิธีเช็ก |
|---|---|
| cert หมดอายุ | openssl x509 -noout -dates |
| chain ไม่ครบ | s_client -showcerts + verify chain |
| CN/SAN ไม่ตรง domain | ตรวจ Subject Alternative Name |
| key ไม่ match cert | compare modulus hash |
| protocol mismatch | ลอง -tls1_2 / -tls1_3 |