[OpenSSL] 사설 인증서(SELF-SIGNED CERTIFICATE) 만들기 (MacOS)

gweowe·2023년 6월 29일

[OpenSSL] 사설 인증서(SELF-SIGNED CERTIFICATE) 만들기 (MacOS)

자체 인증서가 있다면, 개발 환경에서 테스트하거나, 실제 내부망에 적용하여 서버, 클라이언트, 애플리케이션 간에 안전하고 신뢰할 수 있는 통신을 구현할 수 있습니다. OpenSSL을 사용하여 상위 인증서(Root CA)와 하위 인증서(Server Certificate)를 만들어서 계층 구조의 인증서를 만드는 방법을 간단하게 정리해 보았습니다.

Root CA 생성하기

1. Root CA 개인키 생성

Root CA를 생성하기 위해서 처음 해야하는 작업은 바로 Root CA의 개인키를 생성하는 것입니다. 이 개인키는 Root CA의 신원을 보장함과 동시에 인증서를 생성하는 데 사용됩니다.

openssl genrsa -out rootca.key 2048

-aes256 플래그를 추가하면 AES 256bit로 암호화할 수 있습니다.

openssl rsa -in rootca.key -pubout -out rootca_public.key

만약 Root CA에 대한 공개키를 따로 추출하고 싶다면 위와 같은 명령어를 사용하시면 됩니다.

2. Root CA 개인키 생성 확인

openssl rsa -text -in rootca.key -noout

rootca.key가 제대로 생성되었는지 확인합니다.

3. Root CA 인증서 서명 요청서(CSR) 생성

CSR은 인증서에 포함될 정보와 공캐키를 담고 있는 파일입니다. CSR은 조직 정보, 도메인 이름 등의 정보가 포함되어 있으며, CSR을 기반으로 인증서를 생성하게 됩니다.

rootca.conf
[ req ]
default_bits           = 2048
default_md             = sha1
default_keyfile        = rootca.key
distinguished_name     = req_distinguished_name
extensions             = v3_ca
req_extensions = v3_ca

[ v3_ca ]
basicConstraints       = critical, CA:TRUE, pathlen:0
subjectKeyIdentifier   = hash
keyUsage               = keyCertSign, cRLSign
nsCertType             = sslCA, emailCA, objCA

[ req_distinguished_name ]
countryName                     = Country Name
countryName_default             = KR
countryName_min                 = 2
countryName_max                 = 2
organizationName                = Organization Name
organizationName_default        = gweowe Company
organizationalUnitName          = Organizational Unit Name
organizationalUnitName_default  = SE Team
commonName                      = Common Name
commonName_default              = gweowe
commonName_max                  = 64

CSR을 생성하기 위한 설정 파일을 먼저 만들어야 합니다.

openssl req -new -key rootca.key -out rootca.csr -config rootca.conf

명령어를 입력합니다. 설정 파일을 참조하여 CSR을 생성하게 됩니다.

Output :
Country Name [KR]:
Organization Name [gweowe Company]:
Organizational Unit Name [SE Team]:
Common Name [gweowe]:

공란으로 Enter를 입력하면 default 값이 지정되며, 이 Output에서 각 옵션에 대해 직접 입력할 수도 있습니다.

4. Root CA 인증서 생성

개인키, 설정 파일, CSR을 이용하여 인증서를 생성합니다. 이 인증서는 Root CA의 공개키와 서명 등을 포함하고 있으며, Root CA의 개인키로 서명된 인증서는 신뢰할 수 있는 인증 기관의 역할을 수행합니다.

openssl x509 -req \
-days 365 \
-extensions v3_ca \
-set_serial 1 \
-in rootca.csr \
-signkey rootca.key \
-out rootca.crt \
-extfile rootca.conf

명령어를 입력합니다. 개인키, CSR, 설정 파일을 모두 사용하여 인증서를 생성합니다.

5. Root CA 인증서 생성 확인

openssl x509 -text -in rootca.crt -noout
Output :
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number: 1 (0x1)
    Signature Algorithm: sha256WithRSAEncryption
        Issuer: C=KR, O=gweowe Company, OU=SE Team, CN=gweowe
        Validity
            Not Before: Jun 29 01:10:39 2023 GMT
            Not After : Jun 28 01:10:39 2024 GMT
        Subject: C=KR, O=gweowe Company, OU=SE Team, CN=gweowe

# ..............................생략..............................

Output을 살펴보면, 설정 파일에 입력되어 있는 정보들이 출력되는 것을 확인하실 수 있습니다.

Server Certificate 생성하기

1. Server Certificate 개인키 생성

인증서를 생성하는 방법은 Root CA와 매우 유사합니다. 우선 개인키를 생성해야 합니다.

openssl genrsa -out server_cert.key 2048

-aes256 플래그를 추가하면 AES 256bit로 암호화할 수 있습니다.

openssl rsa -in server_cert.key -pubout -out server_cert_public.key

만약 Server Certificate에 대한 공개키를 따로 추출하고 싶다면 위와 같은 명령어를 사용하시면 됩니다.

2. Server Certificate 개인키 생성 확인

openssl rsa -text -in server_cert.key -noout

server_cert.key가 제대로 생성되었는지 확인합니다.

3. Server Certificate 인증서 서명 요청서(CSR) 생성

server_cert.conf
[ req ]
default_bits            = 2048
default_md              = sha1
default_keyfile         = rootca.key
distinguished_name      = req_distinguished_name
extensions             = v3_user

[ v3_user ]
basicConstraints = CA:FALSE
authorityKeyIdentifier = keyid,issuer
subjectKeyIdentifier = hash
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth,clientAuth
subjectAltName          = @alt_names
[alt_names]
DNS.1   = gweowe.test-domain.com
DNS.2   = example.com

[ req_distinguished_name ]
countryName                     = Country Name
countryName_default             = KR
countryName_min                 = 2
countryName_max                 = 2
organizationName                = Organization Name
organizationName_default        = gweowe Server Company
organizationalUnitName          = Organizational Unit Name
organizationalUnitName_default  = DevOps Team
commonName                      = Common Name
commonName_default              = Server gweowe
commonName_max                  = 64

설정 파일의 [ alt_names ]를 보면, gweowe.test-domain.comDNS.1에, example.comDNS.2에 지정되었습니다. 이러한 설정은 CSR 파일을 통해 다양한 Domain을 포함하는 단일 인증서를 생성할 수 있도록 합니다.

openssl req -new -key server_cert.key -out server_cert.csr -config server_cert.conf

명령어를 입력합니다. 설정 파일을 참조하여 CSR을 생성하게 됩니다.

Output :
Country Name [KR]:
Organization Name [gweowe Company]:
Organizational Unit Name [SE Team]:
Common Name [gweowe]:

공란으로 Enter를 입력하면 default 값이 지정되며, 이 Output에서 각 옵션에 대해 직접 입력할 수도 있습니다.

4. Server Certificate 인증서 생성

openssl x509 -req \
-days 365 \
-extensions v3_user \
-CA rootca.crt \
-CAcreateserial \
-CAkey rootca.key \
-in server_cert.csr \
-out server_cert.crt \
-extfile server_cert.conf

Root CA와 같은 방법으로 인증서를 생성합니다.

5. Server Certificate 인증서 생성 확인

openssl x509 -text -in server_cert.crt -noout
Output :
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number: 1 (0x1)
    Signature Algorithm: sha256WithRSAEncryption
        Issuer: C=KR, O=gweowe Company, OU=SE Team, CN=gweowe
        Validity
            Not Before: Jun 29 04:14:12 2023 GMT
            Not After : Jun 28 04:14:12 2024 GMT
        Subject: C=KR, O=gweowe Server Company, OU=DevOps Team, CN=Server gweowe

# ..............................생략..............................

Output을 살펴보면, Issuer에는 Root CA의 정보가 출력되고, Subject에는 Server Certificate의 정보가 출력되는 것을 보실 수 있습니다.

profile
정리하는 공간

0개의 댓글