DocsEnterprise RunbooksTLS — PFX to CRT & Key

TLS — Convert PFX to CRT, Key & Chain

OpenSSL 3.x; Apache HTTP Server 2.4.8+ · Security · Security

Official Reference: https://docs.openssl.org/3.0/man1/openssl-pkcs12/


Executive Summary (EN): OpenSSL 3.x procedure for extracting PEM leaf certificates, CA chains, and chmod 600 private keys from PKCS#12 (.pfx) bundles, verifying SPKI public-key hashes, assembling fullchain.pem, and safely reloading Apache HTTP Server (mod_ssl).

Operational Note: Sanitized enterprise reference runbook cross-checked against OpenSSL 3.0 and Apache mod_ssl documentation — validate in a staging/lab environment before production execution.

1. วัตถุประสงค์

PFX หรือ PKCS#12 เก็บ certificate, private key และอาจมี CA certificates. CRT อาจเป็น PEM หรือ DER; ตัวอย่างนี้สร้าง PEM. Private key ต้องเป็นความลับ ห้ามส่งผ่าน chat, wiki สาธารณะ หรือ email ที่ไม่ได้รับอนุญาต

2. Pre-check

ต้องมี PFX และรหัสผ่านที่ได้รับอนุญาต, OpenSSL, private working directory, CA chain จาก issuer และ backup certificate/config เดิม. ตรวจเวอร์ชันก่อน

openssl version
umask 077
mkdir -m 700 cert-work
cd cert-work
# นำ PFX มาวางผ่านช่องทางที่อนุมัติ
openssl pkcs12 -in cert.pfx -info -noout

หาก OpenSSL 3.x อ่าน PFX เก่าที่ใช้ RC2 ไม่ได้ ให้ลอง -legacy เฉพาะขั้นตอนอ่านไฟล์ที่เชื่อถือได้ ไม่เปิด legacy provider ทั้งระบบและไม่ปิด MAC verification

3. Extract leaf, key, CA

openssl pkcs12 -in cert.pfx -clcerts -nokeys -out leaf-with-attrs.pem
openssl x509 -in leaf-with-attrs.pem -out cert.crt
openssl pkcs12 -in cert.pfx -nocerts -out cert-encrypted.key
openssl pkcs12 -in cert.pfx -cacerts -nokeys -out ca-bundle.pem

คำสั่ง key จะถาม passphrase ใหม่เพื่อเข้ารหัส output. หากแอปต้องอ่าน key แบบไม่มี passphrase และ policy อนุญาต ให้แปลงเป็นไฟล์ที่จำกัดสิทธิ์

openssl pkey -in cert-encrypted.key -out cert.key
chmod 600 cert.key cert-encrypted.key

ใช้ openssl pkey เพื่อรองรับ RSA และ EC. ตรวจว่ามี leaf และ key ที่ต้องการเพียงคู่เดียว หาก PFX มีหลาย identities ให้แยกอย่างระมัดระวัง

4. ตรวจ certificate และ key

openssl x509 -in cert.crt -noout -subject -issuer -dates -ext subjectAltName
openssl pkey -in cert-encrypted.key -check -noout
openssl x509 -in cert.crt -pubkey -noout | openssl pkey -pubin -outform DER | openssl dgst -sha256
openssl pkey -in cert-encrypted.key -pubout -outform DER | openssl dgst -sha256

hash ของ public key ทั้งสองต้องตรงกัน. ตรวจ SAN มีชื่อ service ที่ต้องการ, วันหมดอายุ และ issuer. CA bundle ที่ได้จาก PFX อาจไม่ครบหรือเรียงลำดับผิด ให้ตรวจจาก CA ทางการ ไม่คัดลอก chain เก่ามาใส่โดยไม่ยืนยัน issuer

5. ประกอบ chain

แยก intermediate certificate และ root ที่เชื่อถือได้ตาม issuer; fullchain ให้ leaf ก่อน intermediate ที่ออกให้ leaf ตามด้วย intermediate ระดับสูงขึ้น. ปกติไม่ต้องส่ง root ใน server chain

cat cert.crt intermediate.pem > fullchain.pem
openssl verify -CAfile trusted-root.pem -untrusted intermediate.pem cert.crt

expected: cert.crt: OK. ถ้าหลาย intermediates ให้รวมเรียงเป็น intermediate.pem ก่อนตรวจ. Root ต้องได้มาจาก trust store หรือ CA ที่ตรวจสอบแล้ว

6. Deploy Apache 2.4.8+ (ตัวอย่าง)

สำรอง config และ cert/key เดิม; วางไฟล์ตาม directory/ownership/SELinux policy ของ OS. ตัวอย่าง RHEL-family:

SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/fullchain.pem
SSLCertificateKeyFile /etc/pki/tls/private/cert.key

SSLCACertificateFile ไม่ใช่ตัวแทนของ server intermediate chain; มักใช้ตรวจ client cert ใน mTLS. Encrypted key ต้องมีวิธีรับ passphrase ที่รองรับการ restart ตาม policy

sudo apachectl configtest
# เมื่อ Syntax OK และได้รับอนุมัติ
sudo systemctl reload httpd
systemctl status httpd

Ubuntu/Debian ใช้ apache2 service และ path ตาม distro. ตรวจ TLS จากเครื่อง client ด้วย hostname จริง

openssl s_client -connect web.example.com:443 -servername web.example.com -verify_hostname web.example.com -verify_return_error -showcerts </dev/null
curl -I https://web.example.com/

ใช้ -CAfile ที่องค์กรอนุมัติหาก internal CA. เกณฑ์ผ่าน: hostname ถูกต้อง, chain verify สำเร็จ, certificate วันหมดอายุใหม่ และ application health ปกติ

7. Troubleshooting และ rollback

อาการแนวทาง
unsupported algorithmอ่าน PFX ด้วย -legacy เมื่อจำเป็น
MAC verify errorตรวจ password และไฟล์ต้นฉบับ ไม่ bypass
key mismatchเปรียบเทียบ public key hash
unable to get local issuerตรวจ intermediate และ trusted root
hostname mismatchตรวจ SAN และ SNI
reload failedตรวจ configtest, permissions, SELinux, service log

Rollback: คืน config และคู่ cert/key เดิมที่ยังใช้ได้, configtest, reload, ตรวจจาก client. เก็บ key ใน approved vault และกำจัดไฟล์ชั่วคราวตาม policy. ห้ามอัปโหลดไฟล์ key จริงลงระบบเอกสารหรือ repository ใดๆ

8. Lab

ใช้ self-signed test identity ที่สร้างเฉพาะ Lab สร้าง PFX, extract และเปรียบเทียบ public-key hash; ทดลอง chain mismatch ด้วย test CA. ห้ามใช้ private key จริงใน Lab ที่แชร์กับบุคคลอื่น

แหล่งอ้างอิงทางการ (Official References)