Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 5개 있습니다.)
(시리즈 글이 10개 있습니다.)
개발 환경 구성: 555. openssl - CA로부터 인증받은 새로운 인증서를 생성하는 방법
; https://www.sysnet.pe.kr/2/0/12570

개발 환경 구성: 565. PowerShell - New-SelfSignedCertificate를 사용해 CA 인증서 생성 및 인증서 서명 방법
; https://www.sysnet.pe.kr/2/0/12588

개발 환경 구성: 654. openssl - CA로부터 인증받은 새로운 인증서를 생성하는 방법 (2)
; https://www.sysnet.pe.kr/2/0/13187

개발 환경 구성: 662. openssl - 윈도우 환경의 명령행에서 SAN 적용하는 방법
; https://www.sysnet.pe.kr/2/0/13235

개발 환경 구성: 663. openssl을 이용해 인트라넷 IIS 사이트의 SSL 인증서 생성
; https://www.sysnet.pe.kr/2/0/13236

개발 환경 구성: 681. openssl - 인증서 버전(V1 / V3)
; https://www.sysnet.pe.kr/2/0/13371

개발 환경 구성: 686. 비주얼 스튜디오로 실행한 ASP.NET Core 사이트를 WSL 2 인스턴스에서 https로 접속하는 방법
; https://www.sysnet.pe.kr/2/0/13442

개발 환경 구성: 687. OpenSSL로 생성한 사용자 인증서를 ASP.NET Core 웹 사이트에 적용하는 방법
; https://www.sysnet.pe.kr/2/0/13443

Linux: 62. 리눅스/WSL에서 CA 인증서를 저장하는 방법
; https://www.sysnet.pe.kr/2/0/13445

닷넷: 2162. ASP.NET Core 웹 사이트의 SSL 설정을 코드로 하는 방법
; https://www.sysnet.pe.kr/2/0/13447




openssl - CA로부터 인증받은 새로운 인증서를 생성하는 방법

self-signed 인증서가 있다면 그것을 인증 기관(CA: certificate authority)의 루트 인증서라고 봐도 됩니다.

How to Create Your Own SSL Certificate Authority for Local HTTPS Development
 - Becoming a (tiny) Certificate Authority
; https://deliciousbrains.com/ssl-certificate-authority-for-local-https-development/

OpenSSL Essentials: Working with SSL Certificates, Private Keys and CSRs
; https://www.digitalocean.com/community/tutorials/openssl-essentials-working-with-ssl-certificates-private-keys-and-csrs

위의 글에서는 CA 인증서 생성 후, 다른 개인키를 만들어 앞서의 CA로부터 서명을 받아내는 방법("Creating CA-Signed Certificates for Your Dev Sites")도 설명합니다.

이와 관련해서 간단하게 정리를 해볼까요? ^^

기존에 ca.crt, ca.key 파일이 있다고 가정하고, 새롭게 apiserver.key라는 개인키 파일을 하나 만들었다고 가정해보겠습니다. 그럼 해당 개인키에 대한 인증서 요청 파일을 만들 수 있습니다.

// openssl-req

c:\temp\wsl> openssl req -key apiserver.key -new -out apiserver.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:kube-apiserver
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

"Common Name"을 kube-apiserver로 지정한 것을 제외하고는 모두 기본 값을 사용하고 있습니다. 이렇게 생성된 apiserver.csr 파일은 다음과 같은 양식을 가집니다.

-----BEGIN CERTIFICATE REQUEST-----
MIICozCCAYsCAQAwXjELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUx
...[생략]...
dffpjAc4Xw==
-----END CERTIFICATE REQUEST-----

이걸로 모든 준비는 끝입니다. 이제 CA 인증 기관의 개인키 파일 및 인증서를 가지고 다음과 같이 해당 인증서 요청에 대해 서명을 해 새로운 인증서를 만들 수 있습니다.

// openssl-x509

c:\temp\wsl> openssl x509 -req -days 3650 -in apiserver.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server-cert.crt

서명 후 생성된 server-cert.crt 파일은 이런 양식을 가지며,

-----BEGIN CERTIFICATE-----
MIIC+jCCAeICFEsnOV4njaMmxkP9X+5leLdWtL0KMA0GCSqGSIb3DQEBCwUAMBUx
...[생략]...
cYvuYo6gg4XKMHsR6RILj+6/WMJ7ptLSF5M5ZQLDwuKwYjbZc57+72hrHYopFw==
-----END CERTIFICATE-----

탐색기로 보면 다음과 같이 해석 결과를 보여줍니다.

sign_from_ca_1.png




기본 과정은 저렇지만, 하나씩 사용자 정의를 추가해보겠습니다.

우선 "Common Name" (속성 창에서는 Subject) 값이 "kube-apiserver, Internet Widgits Pty Ltd"라고 나옵니다. 이것은, "openssl req ..." 명령어 실행 중 기본값으로 지정된 "Organization Name" 때문에 그런 것입니다. "Common Name"에 기재한 값만 나오게 하고 싶다면 "Organization Name" 질문에 그냥 엔터만 치지 말고 콤마(.)를 입력한 후 엔터를 치면 됩니다. 또는, 아예 명령어에서 제공하는 -subj 옵션을 활용하면 그런 귀찮은 질문 단계를 없앨 수 있습니다.

C:\temp\wsl> openssl req -key apiserver.key -new -out apiserver.csr -subj "/CN=kube-apiserver"

위와 같이 해주면, "Subject"에 해당하는 값이 "CN = kube-apisevrer"로 나오게 됩니다. 하지만, 대체로 위와 같이 명령행에 지정하기보다는 이러한 구성을 가지고 있는 cnf 파일을 만드는 것이 더 편리합니다.

How to create a CSR with OpenSSL
; https://www.switch.ch/pki/manage/request/csr-openssl/

# apiserver.cnf

FQDN = kube-apiserver

[ req ]
default_bits = 2048
default_md = sha256
prompt = no
encrypt_key = no
distinguished_name = dn

[ dn ]
CN = $FQDN

이렇게 만든 cnf 파일을 "openssl req ..." 수행 시 "-config" 옵션으로 지정하면 됩니다.

C:\temp\wsl> openssl req -key apiserver.key -new -out apiserver.csr -config apiserver.cnf

필드 설정이 원하는 대로 반영이 되었는지는 crt 파일까지 생성할 필요 없이, CSR 파일을 대상으로 "opensssl req ..." 명령어를 이용해 이렇게 해석할 수 있습니다.

C:\temp\wsl> openssl req -in apiserver.csr -noout -text -nameopt sep_multiline
Certificate Request:
    Data:
        Version: 1 (0x0)
        Subject:
            CN=kube-apiserver
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                RSA Public-Key: (2048 bit)
                Modulus:
                    00:a5:e6:1d:1b:41:15:f2:c4:b6:24:73:b4:ad:59:
                    ...[생략]...
                    1a:8f:ae:2c:de:fd:e5:7f:ac:78:b2:b2:30:ca:05:
                    17:55
                Exponent: 65537 (0x10001)
    Signature Algorithm: sha256WithRSAEncryption
         a2:d5:a6:37:56:8e:39:ec:c8:4b:03:71:5e:bd:50:b7:45:14:
         ...[생략]...
         11:12:ae:53:62:bd:4c:75:35:ec:28:d5:8c:5d:28:c6:32:38:
         ef:7c:48:33




그다음, "Subject Alternative Name"을 지정하는 방법입니다. 이를 위해 cnf 파일에 다음의 내용을 추가하고,

# apiserver.cnf

FQDN = kube-apiserver
ALTNAMES = DNS:docker-for-desktop,DNS:kubernetes,DNS:kubernetes.default,DNS:kubernetes.default.svc,DNS:kubernetes.default.svc.cluster.local,DNS:kubernetes.docker.internal,DNS:vm.docker.internal,DNS:localhost

[ req ]
default_bits = 2048
default_md = sha256
prompt = no
encrypt_key = no
distinguished_name = dn
req_extensions = req_ext

[ dn ]
CN = $FQDN

[ req_ext ]
subjectAltName = $ALTNAMES

"openssl req ..." 명령을 실행하면 되는데,

openssl req -key apiserver.key -new -out apiserver.csr -config apiserver.cnf

재미있는 것은, 이렇게 만들어진 csr 파일을 "openssl req -in apiserver.csr -noout -text -nameopt sep_multiline" 명령어로 살펴보면 분명히 다음과 같이 "X509v3 Subject Alternative Name" 항목으로 확인이 되는데,

...[생략]...
       Attributes:
        Requested Extensions:
            X509v3 Subject Alternative Name:
                DNS:docker-for-desktop, DNS:kubernetes, DNS:kubernetes.default, DNS:kubernetes.default.svc, DNS:kubernetes.default.svc.cluster.local, DNS:kubernetes.docker.internal, DNS:vm.docker.internal, DNS:localhost
...[생략]...

정작 "openssl x509 ..." 명령어를 실행해 crt 인증서를 만들었을 때는 SAN 항목의 출력이 누락됩니다. 왜냐하면, "openssl x509 ..." 명령에서도 -extfile과 -extension 옵션을 이용해 cnf(이 글에서는 apiserver.cnf)의 확장 항목을 지정해야 하기 때문입니다.

openssl x509 -extfile apiserver.cnf -extensions req_ext -req -days 3650 -in apiserver.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server-cert.crt

이렇게 만들어진 server-cert.crt를 보면,

sign_from_ca_2.png

의도했던 대로 "Subject Alternative Name" 값이 들어가 있습니다.




다음으로 "Key Usage" 항목을 설정해 보겠습니다.

# apiserver.cnf

FQDN = kube-apiserver
ALTNAMES = DNS:docker-for-desktop,DNS:kubernetes,DNS:kubernetes.default,DNS:kubernetes.default.svc,DNS:kubernetes.default.svc.cluster.local,DNS:kubernetes.docker.internal,DNS:vm.docker.internal,DNS:localhost,IP:10.96.0.1,IP:0.0.0.0,IP:192.168.65.3,IP:127.0.0.1

[ req ]
default_bits = 2048
default_md = sha256
prompt = no
encrypt_key = no
distinguished_name = dn
req_extensions = req_ext

[ dn ]
CN = $FQDN

[ req_ext ]
subjectAltName = $ALTNAMES
keyUsage = digitalSignature,keyEncipherment

이 설정으로 "openssl req ...", "openssl x509 ..."를 실행하면, 인증서에는 "Key Usage" 값이 "Digital Signature, Key Enhancement (a0)" 항목이 나옵니다.




그렇다면, Enhanced Key Usage 값도 설정해 보겠습니다.

# apiserver.cnf

FQDN = kube-apiserver
ALTNAMES = DNS:docker-for-desktop,DNS:kubernetes,DNS:kubernetes.default,DNS:kubernetes.default.svc,DNS:kubernetes.default.svc.cluster.local,DNS:kubernetes.docker.internal,DNS:vm.docker.internal,DNS:localhost,IP:10.96.0.1,IP:0.0.0.0,IP:192.168.65.3,IP:127.0.0.1

[ req ]
default_bits = 2048
default_md = sha256
prompt = no
encrypt_key = no
distinguished_name = dn
req_extensions = req_ext

[ dn ]
CN = $FQDN

[ req_ext ]
extendedKeyUsage = serverAuth
authorityKeyIdentifier = keyid, issuer
subjectAltName = $ALTNAMES
keyUsage = digitalSignature,keyEncipherment

이후 인증서를 생성하면 Enhanced Key Usage 값이 "Server Authentication (1.3.6.1.5.5.7.3.1)"로 설정이 됩니다.




하는 김에 AKID(Authority Key Identifier)까지 설정해 보겠습니다. 이 값은, CA 인증서의 "Subject Key Identifier"가 있다면 그 값을 "Authority Key Identifier"에 그대로 이어받을 수 있습니다.

그런데, 이 값의 설정은 "openssl req ..." 단계에서는 허용되지 않는 옵션이므로 이를 위해서는 openssl cnf의 설정 단계를 "openssl req"와 "openssl x509"를 위해 나누는 것이 필요합니다.

# apiserver.cnf

FQDN = kube-apiserver
ALTNAMES = DNS:docker-for-desktop,DNS:kubernetes,DNS:kubernetes.default,DNS:kubernetes.default.svc,DNS:kubernetes.default.svc.cluster.local,DNS:kubernetes.docker.internal,DNS:vm.docker.internal,DNS:localhost,IP:10.96.0.1,IP:0.0.0.0,IP:192.168.65.3,IP:127.0.0.1,IP:192.168.65.4

[ req ]
default_bits = 2048
default_md = sha256
prompt = no
encrypt_key = no
distinguished_name = dn
req_extensions = req_ext

[ dn ]
CN = $FQDN

[ req_ext ]
extendedKeyUsage = serverAuth
subjectAltName = $ALTNAMES
keyUsage = digitalSignature,keyEncipherment

[ x509_ext ]
extendedKeyUsage = serverAuth
authorityKeyIdentifier = keyid,issuer
subjectAltName = $ALTNAMES
keyUsage = digitalSignature,keyEncipherment

아울러, 이렇게 나뉜 설정을 각각 openssl req와 openssl x509 명령에서 다음과 같이 지정해 인증서를 생성할 수 있습니다.

C:\temp\wsl> openssl req -key apiserver.key -new -out apiserver.csr -config apiserver.cnf

C:\temp\wsl> openssl x509 -extfile apiserver.cnf -extensions x509_ext -req -days 3650 -in apiserver.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server-cert.crt
Signature ok
subject=CN = kube-apiserver
Getting CA Private Key

다음은 이렇게 해서 반영된 AKID 값을 보여줍니다.

sign_from_ca_3.png




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 4/7/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 31  32  33  34  35  36  37  38  39  40  [41]  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
12603정성태4/16/20218295VS.NET IDE: 162. 비주얼 스튜디오 - 상속받은 컨트롤이 디자인 창에서 지원되지 않는 문제
12602정성태4/16/20219506VS.NET IDE: 161. x64 DLL 프로젝트의 컨트롤이 Visual Studio의 Designer에서 보이지 않는 문제 [1]
12601정성태4/15/20218590.NET Framework: 1040. C# - REST API 대신 github 클라이언트 라이브러리를 통해 프로그래밍으로 접근
12600정성태4/15/20218778.NET Framework: 1039. C# - Kubeconfig의 token 설정 및 인증서 구성을 자동화하는 프로그램
12599정성태4/14/20219476.NET Framework: 1038. C# - 인증서 및 키 파일로부터 pfx/p12 파일을 생성하는 방법파일 다운로드1
12598정성태4/14/20219596.NET Framework: 1037. openssl의 PEM 개인키 파일을 .NET RSACryptoServiceProvider에서 사용하는 방법 (2)파일 다운로드1
12597정성태4/13/20219680개발 환경 구성: 569. csproj의 내용을 공통 설정할 수 있는 Directory.Build.targets / Directory.Build.props 파일
12596정성태4/12/20219413개발 환경 구성: 568. Windows의 80 포트 점유를 해제하는 방법
12595정성태4/12/20218838.NET Framework: 1036. SQL 서버 - varbinary 타입에 대한 문자열의 CAST, CONVERT 변환을 C# 코드로 구현
12594정성태4/11/20218296.NET Framework: 1035. C# - kubectl 명령어 또는 REST API 대신 Kubernetes 클라이언트 라이브러리를 통해 프로그래밍으로 접근 [1]파일 다운로드1
12593정성태4/10/20219440개발 환경 구성: 567. Docker Desktop for Windows - kubectl proxy 없이 k8s 대시보드 접근 방법
12592정성태4/10/20219245개발 환경 구성: 566. Docker Desktop for Windows - k8s dashboard의 Kubeconfig 로그인 및 Skip 방법
12591정성태4/9/202112497.NET Framework: 1034. C# - byte 배열을 Hex(16진수) 문자열로 고속 변환하는 방법 [2]파일 다운로드1
12590정성태4/9/20219007.NET Framework: 1033. C# - .NET 4.0 이하에서 Console.IsInputRedirected 구현 [1]
12589정성태4/8/202110334.NET Framework: 1032. C# - Environment.OSVersion의 문제점 및 윈도우 운영체제의 버전을 구하는 다양한 방법 [1]
12588정성태4/7/202110928개발 환경 구성: 565. PowerShell - New-SelfSignedCertificate를 사용해 CA 인증서 생성 및 인증서 서명 방법
12587정성태4/6/202111706개발 환경 구성: 564. Windows 10 - ClickOnce 배포처럼 사용할 수 있는 MSIX 설치 파일 [1]
12586정성태4/5/20219396오류 유형: 710. Windows - Restart-Computer / shutdown 명령어 수행 시 Access is denied(E_ACCESSDENIED)
12585정성태4/5/20219171개발 환경 구성: 563. 기본 생성된 kubeconfig 파일의 내용을 새롭게 생성한 인증서로 구성하는 방법
12584정성태4/1/20219861개발 환경 구성: 562. kubeconfig 파일 없이 kubectl 옵션만으로 실행하는 방법
12583정성태3/29/202111369개발 환경 구성: 561. kubectl 수행 시 다른 k8s 클러스터로 접속하는 방법
12582정성태3/29/202110043오류 유형: 709. Visual C++ - 컴파일 에러 error C2059: syntax error: '__stdcall'
12581정성태3/28/20219980.NET Framework: 1031. WinForm/WPF에서 Console 창을 띄워 출력하는 방법 (2) - Output 디버깅 출력을 AllocConsole로 우회 [2]
12580정성태3/28/20218710오류 유형: 708. SQL Server Management Studio - Execution Timeout Expired.
12579정성태3/28/20218775오류 유형: 707. 중첩 가상화(Nested Virtualization) - The virtual machine could not be started because this platform does not support nested virtualization.
12578정성태3/27/20219070개발 환경 구성: 560. Docker Desktop for Windows 기반의 Kubernetes 구성 (2) - WSL 2 인스턴스에 kind가 구성한 k8s 서비스 위치
... 31  32  33  34  35  36  37  38  39  40  [41]  42  43  44  45  ...