Certificate Tools

Concept

Public Key and Private Key Pair: How it Works

source: https://comodosslstore.com/blog/public-key-and-private-key-pair-how-it-works.html

Digital Signature and Public Key as Identities

source: https://zhaohuabing.medium.com/digital-signature-and-public-key-as-identities-94f8ecac2a24

Certificates format

  • The PEM format is the most common format used for certificates. Extensions used for PEM certificates are cer, crt, and pem. They are Base64 encoded ASCII files.
  • The DER format is the binary form of the certificate. DER formatted certificates do not contain the "BEGIN CERTIFICATE/END CERTIFICATE" statements. DER formatted certificates most often use the .der extension.

Generate P12 Keystore

### (1) CREATE KEYSTORE (P12) WITH KEY PAIR
openssl pkcs12 -export -inkey int_krungsriepayment_com.key -in int_krungsriepayment_com.cer -name privatekeyalias -out ./out/bay_keystore_uat.p12 -passout "pass:epaymentPwdKey"

### CHECK
keytool -list -keystore ./out/bay_keystore_uat.p12 -storetype PKCS12 -storepass epaymentPwdKey

### (2) IMPORT CERT
keytool -import -alias certalias -file int_krungsriepayment_com.cer -keystore ./out/bay_keystore_uat.p12 -storepass epaymentPwdKey

### CHECK
keytool -list -keystore ./out/bay_keystore_uat.p12 -storetype PKCS12 -storepass epaymentPwdKey

Example

1
2
3
4
5
6
7
8
9
Keystore type: PKCS12
Keystore provider: SunJSSE

Your keystore contains 2 entries

certalias, Apr 11, 2022, trustedCertEntry, 
Certificate fingerprint (SHA-256): 6A:15:E4:E8:F3:E9:E7:D3:1F:0F:4D:8C:E3:B3:9F:D3:E9:DA:CE:E8:F6:12:72:A0:0E:8C:D8:48:83:9D:D7:5B
privatekeyalias, Apr 11, 2022, PrivateKeyEntry, 
Certificate fingerprint (SHA-256): 6A:15:E4:E8:F3:E9:E7:D3:1F:0F:4D:8C:E3:B3:9F:D3:E9:DA:CE:E8:F6:12:72:A0:0E:8C:D8:48:83:9D:D7:5B

ใช้ OpenSSL เพื่อสร้าง self-signed ECC SSL Certificate

san.conf

[ req ]
distinguished_name = req_distinguished_name
req_extensions     = req_ext
prompt = no

[ req_distinguished_name ]
C = TH
ST = Bangkok
L = Home
O = Bank
OU = IT
CN = localhost

[ req_ext ]
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[ alt_names ]
DNS.1 = localhost
IP.1  = 127.0.0.1
IP.2  = ::1
  1. สร้างคีย์ส่วนตัว (Private Key)
openssl ecparam -genkey -name prime256v1 -out key.pem
  1. สร้าง Certificate Signing Request (CSR)
openssl req -new -config san.conf -key key.pem -out csr.csr
openssl req -text -noout -verify -in ./csr.csr
  1. สร้าง Self-Signed Certificate
openssl req -x509 -config san.conf -extensions req_ext -sha256 -days 365 -key key.pem -out localhost.pem

Tools

alias portecle='java -jar /path/to/portecle.jar &'
alias kse='java -jar /path/to/kse.jar &'

View certificate chain

openssl s_client -showcerts -connect www.google.com:443
depth=2 C = US, O = Google Trust Services LLC, CN = GTS Root R1
verify return:1
depth=1 C = US, O = Google Trust Services, CN = WR2
verify return:1
depth=0 CN = www.google.com

---
Certificate chain
 0 s:CN = www.google.com
   i:C = US, O = Google Trust Services, CN = WR2
   a:PKEY: id-ecPublicKey, 256 (bit); sigalg: RSA-SHA256
   v:NotBefore: Sep 30 15:11:47 2024 GMT; NotAfter: Dec 23 15:11:46 2024 GMT

 1 s:C = US, O = Google Trust Services, CN = WR2
   i:C = US, O = Google Trust Services LLC, CN = GTS Root R1
   a:PKEY: rsaEncryption, 2048 (bit); sigalg: RSA-SHA256
   v:NotBefore: Dec 13 09:00:00 2023 GMT; NotAfter: Feb 20 14:00:00 2029 GMT

 2 s:C = US, O = Google Trust Services LLC, CN = GTS Root R1
   i:C = BE, O = GlobalSign nv-sa, OU = Root CA, CN = GlobalSign Root CA
   a:PKEY: rsaEncryption, 4096 (bit); sigalg: RSA-SHA256
   v:NotBefore: Jun 19 00:00:42 2020 GMT; NotAfter: Jan 28 00:00:42 2028 GMT

Certificate chain bundle

การ bundle (หรือรวม) CA certificate กับ intermediate certificate ไว้ในไฟล์เดียวกัน มักทำเพื่อใช้กับเว็บเซิร์ฟเวอร์ (เช่น Apache หรือ Nginx) เพื่อให้สามารถตรวจสอบ certificate chain ได้อย่างถูกต้อง กระบวนการนี้มักจะเรียกว่า "certificate chain bundle" หรือ "full chain."

ในการ bundle CA certificate กับ intermediate certificate สามารถทำได้โดยการต่อเนื้อหาของไฟล์ใบรับรองเหล่านี้เข้าด้วยกันในไฟล์เดียว โดยเริ่มจาก intermediate certificate แล้วตามด้วย CA certificate

ขั้นตอน:

  1. เตรียมไฟล์ intermediate.crt ซึ่งเป็นใบรับรอง intermediate certificate
  2. เตรียมไฟล์ ca.crt ซึ่งเป็น CA certificate
  3. ใช้คำสั่ง cat เพื่อรวมไฟล์ทั้งสองเข้าด้วยกัน

ตัวอย่างการรวมไฟล์:

cat intermediate.crt ca.crt > bundle.crt

คำอธิบาย:

  • intermediate.crt: เป็นไฟล์ที่เก็บใบรับรองของ intermediate certificate (หรือใบรับรองของผู้ที่ออกใบรับรองให้โดเมนของคุณ)
  • ca.crt: เป็นใบรับรองของ Root Certificate Authority (CA)
  • ไฟล์ bundle.crt: ไฟล์ผลลัพธ์ที่ประกอบด้วยทั้ง intermediate และ CA certificate ซึ่งสามารถใช้สำหรับการตั้งค่าเว็บเซิร์ฟเวอร์หรือ application อื่นๆ

หลังจากที่ทำการ bundle แล้ว คุณสามารถนำไฟล์ bundle.crt นี้ไปใช้ในการตั้งค่าเว็บเซิร์ฟเวอร์หรือบริการอื่นๆ ได้เพื่อให้ทำงานร่วมกับ SSL/TLS ได้อย่างถูกต้อง

References


Notes

อธิบาย concept เรื่อง trust ดังนี้ครับ
client อยากแลกเปลี่ยนข้อมูลกับ server สมมติชื่อ domainA
แต่ client ก็ไม่มีทางรู้ได้ว่าคนที่ตัวเองคุยด้วยอยู่ใช่ตัวจริงไหม หรือโดนดักปลอมระหว่างทาง
client จะเชื่อ public CA เสมอ
ดังนั้นถ้า public CA รับรองว่า server คือ domainA จริง
client ก็จึงจะคุยด้วยต่อ แต่ถ้าไม่ใช่ ก็เลิกคุย

สิ่งที่ใช้ในการรับรองคือ certificate (ใบรับรองพร้อมลายเซ็น)

บนเครื่อง client จะมี (trusted) root cert ของ public CA อยู่
root cert เอาไปตรวจได้ว่า intermediate cert ถูกออกและรับรอง(sign)โดย public CA จริง

แล้ว intermediate เอาไปตรวจต่อว่า server cert ถูก sign โดย intermediate ที่ตรวจสอบแล้วหรือไม่

สรุปแล้วฝั่ง server ต้อง provide intermediate cert & server cert ส่วน private key ไม่ได้ส่งไป แต่ต้องใส่ไว้ใน config ระบบเพื่อเอาไว้ถอดรหัสช่วงแลกkey
ฝั่ง client ไม่ต้องทำอะไร

แต่ถ้า cert ไม่ได้ออกโดย public CA ก็ต้องเอา server cert (กรณี self-signed) หรือ private CA cert(กรณีทำ private CA) ไปใส่ในส่วน trust ของทุกclientที่จะเชื่อมต่อ server

Edit

Pub: 10 Apr 2022 03:36 UTC

Edit: 17 Oct 2024 15:18 UTC

Views: 223