글 작성자: 근삼이

PKCS#12
PKCS#12(Public Key Cryptography Standards #12)형식은 각종 키, 인증서가 모두 포함될 수 있는 형태이다. 해당 형식을 사용하면, 바이너리 형태로 저장되며, 이 파일을 PFX 파일이라고도 한다.


  • openssl 툴이 설치되어 있어야 함
  • pfx파일에 CA인증서는 포함되어 있지 않을 수 있음
  • 인증서 파일이 암호화 되어 있을 경우, 암호키를 입력해 주어야 함
  • 사용 예시에서 확장자를 포함한 파일이름은 본인 설정에 맞게 자유롭게

추출

- 인증서 추출

openssl pkcs12 -in <filename.pfx> -clcerts -nokeys -out <clientcert.crt> --password pass:<password>

- 개인키 추출

openssl pkcs12 -in <filename.pfx> -nocerts -nodes -out <clientcert.key> --password pass:<password>

- CA인증서 추출

openssl pkcs12 -in <filename.pfx> -cacerts -nokeys -chain -out <cacerts.crt> --password pass:<password>

- 사용 예시

openssl pkcs12 -in my.pfx -nocerts -nodes -out my.key --password pass:a1234
openssl pkcs12 -in my.pfx -clcerts -nokeys -out my.crt --password pass:a1234
openssl pkcs12 -in my.pfx -cacerts -nokeys -chain -out ca_my.pem --password pass:a1234

주의사항

추출된 파일을 바로 시스템에 적용하여 사용하면 형식이 맞지 않아서 에러가 날 수 있으니, 적절하게 필요한 부분만 떼서 사용해야 한다.

- 추출 직후의 파일 포맷

아래 예시에 사용된 예시는 임의 변경한 사용 불가능한 인증서이니, 괜히 헛수고 하지 않길 바란다.

Bag Attributes
    Microsoft Local Key set: <No Values>
    localKeyID: 01 00 00 00
    friendlyName: le-e9a1231-7f1c-1234-b123-f1233da2
    Microsoft CSP Name: Microsoft RSA SChannel Cryptographic Provider
Key Attributes
    X509v3 Key Usage: 10
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDKntycKiujZNnY
5BgQ/ZEdec4uxFQ0lLK7CxHo1gHJLvqm660cuFZVJBtP6BPSHtAczqYGAiWQOESt
hnIjURqPxSFrMgUahK06+f2bCjIE844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