01TLS ยังต้องมี แต่อาจยังไม่พอ
HTTPS ปกป้องข้อมูลในแต่ละ TLS connection แต่ request บนเส้นทางจริงอาจผ่าน CDN, WAF, API gateway และ load balancer ที่ terminate TLS หลายครั้ง RFC 9421 เพิ่ม detached signature ระดับ HTTP เพื่อผูกตัวตนของ signer กับส่วนของ message ที่ระบบตกลงกันว่าสำคัญ
- Authenticity: request ถูก sign ด้วย key ที่เชื่อถือ
- Integrity: covered components ไม่ถูกเปลี่ยน
- Freshness: จำกัดอายุและตรวจ nonce
- Auditability: ระบุ signer และ key version
- TLS สำหรับ encryption และ server authentication
- Authorization ว่า signer ทำอะไรกับ resource ใดได้
- Input validation และ business rules
- Rate limiting, WAF และ monitoring
02สองระบบต้องกำหนด Signature Profile
RFC 9421 เป็น toolkit ไม่ได้กำหนด policy ของแต่ละ API ให้ System A และ System B จึงต้องตกลง profile เดียวกัน และ version เอกสารนี้ควบคู่กับ API contract
`@method`, `@authority`, `@path`, `content-type`, `content-digest`
ผูก action, destination, route และ body
เพิ่ม `@query` หรือ `@query-param` ถ้ามีผลต่อธุรกรรม
กันผู้โจมตีเปลี่ยนพารามิเตอร์ที่มีความหมาย
`created`, `expires`, `nonce`, `keyid`; `alg` ตาม policy
จำกัดอายุ ป้องกัน replay และระบุ key
Ed25519 หรือค่าที่ทั้งสองฝ่าย allowlist
ใช้ asymmetric key เพื่อแยก signing กับ verification
30–120 วินาที บวก clock skew ที่กำหนดชัด
ลดหน้าต่างที่ request เก่ายังใช้ได้
ทะเบียน `keyid` → public key แบบ out-of-band
ไม่ fetch URL จาก `keyid` โดยไม่มี trust policy
03Anatomy ของ Signed Request
`Signature-Input` ระบุรายการ component และ metadata ส่วน `Signature` เก็บผลลัพธ์การ sign ทั้งคู่ใช้ label เดียวกัน เช่น `sig1` ส่วน body ถูกผูกเข้ากับลายเซ็นผ่าน `Content-Digest` ตาม RFC 9530
POST /v1/payments HTTP/1.1
Host: api.system-b.example
Content-Type: application/json
Content-Digest: sha-256=:<base64-sha256-of-body>:
Signature-Input: sig1=("@method" "@authority" "@path" "content-type" "content-digest");created=1790150400;expires=1790150460;nonce="01K5W7K8QF4X8MZQ";keyid="system-a-2026-09";alg="ed25519"
Signature: sig1=:<base64-signature>:
{"paymentId":"pay_123","amount":1500,"currency":"THB"}Signature base ที่ทั้งสองฝ่ายต้องสร้างได้ตรงกัน
"@method": POST
"@authority": api.system-b.example
"@path": /v1/payments
"content-type": application/json
"content-digest": sha-256=:<base64-sha256-of-body>:
"@signature-params": ("@method" "@authority" "@path" "content-type" "content-digest");created=1790150400;expires=1790150460;nonce="01K5W7K8QF4X8MZQ";keyid="system-a-2026-09";alg="ed25519"อย่าต่อ string หรือ canonicalize header เองจากความเข้าใจ ควรใช้ library ที่รองรับ RFC 9421 และ HTTP Structured Fields พร้อมทดสอบด้วย test vectors จาก RFC
04Verification Pipeline ที่ปลอดภัย
1. Parse Signature-Input และ Signature แบบ Structured Fields
2. ตรวจ label, covered components และ algorithm กับ policy
3. ตรวจ created/expires กับ clock-skew ที่ยอมรับ
4. Resolve keyid ไปยัง public key ที่ผูกกับ System A
5. Rebuild signature base ตาม RFC 9421 และ verify signature
6. Hash body bytes จริง แล้วเทียบ Content-Digest
7. Consume nonce แบบ atomic ก่อนเริ่ม business transaction
8. Authorize signer กับ action/resource ที่ร้องขอการตรวจ `Content-Digest` มีสองชั้น: ตรวจว่า field ถูกครอบด้วย signature และคำนวณ digest จาก body bytes ที่รับจริงเพื่อเทียบค่า หากข้ามขั้นหลัง ผู้โจมตีอาจ เปลี่ยน body แต่คง header และ signature เดิมไว้ได้
05แบ่งหน้าที่ระหว่างสองระบบ
ถือ private key ใน KMS/HSM หรือ secret store
HTTPS ผ่าน internet; อาจ allowlist egress/IP
ถือ public key และ trust mapping ของ System A
Serialize body ครั้งเดียว แล้ว hash/sign bytes ที่จะส่ง
Gateway ต้องไม่ rewrite covered components
Verify ก่อน parse body เข้า domain model
สร้าง nonce ใหม่ต่อ transport attempt
NTP ของทั้งสองฝ่ายต้องเที่ยงตรง
Replay cache ต้อง shared ระหว่าง instances
Rotate key โดยออก `keyid` ใหม่
ช่วง overlap รองรับ old/new public key
Revoke key และ alert ได้ทันที
06Failure, Rotation และ Observability
Malformed Signature
Structured Fields parse ไม่ได้, label ไม่ครบ หรือขาด required component
Invalid Signature
ไม่รู้จัก key, signature ไม่ผ่าน, digest ไม่ตรง, หมดอายุ หรือ replay
Not Authorized
ยืนยัน signer ได้แต่ไม่มีสิทธิ์ใช้ method, route หรือ resource นั้น
Verifier Unavailable
Key registry หรือ replay store ล้ม ให้ fail closed สำหรับ state-changing API
Log เฉพาะ `keyid`, signer, signature label, covered-component profile, time skew, verification result และ request correlation ID ห้าม log private key, raw signature base, credential หรือ body ที่มีข้อมูลอ่อนไหว ควรมี metrics แยกตาม failure reason และ alert เมื่อ unknown key/replay/time skew เพิ่มผิดปกติ
07Production Checklist
- ระบุ required covered components และ parameters
- Allowlist algorithm และผูกกับ key type
- กำหนด validity window, clock skew และ nonce TTL
- ระบุ error contract โดยไม่รั่วรายละเอียดเกินจำเป็น
- ใช้ RFC 9421/Structured Fields library ที่ทดสอบแล้ว
- Verify digest จาก raw body bytes ก่อน transform
- Replay check แบบ atomic และ shared ทุก instance
- Private key ไม่อยู่ใน source code หรือ container image
- Golden vectors ระหว่างภาษาที่ทั้งสองระบบใช้
- Tamper method, path, query, body และ header ที่ sign
- Replay, expired, future created และ unknown key
- Proxy rewrite, duplicate headers และ key rotation overlap
- มี key inventory, owner, expiry และ revoke procedure
- Monitor verification failures และ NTP drift
- Runbook เมื่อ private key รั่วหรือ replay store ล้ม
- ทดลอง rotate/revoke โดยไม่หยุดบริการ