Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 2개 있습니다.)
(시리즈 글이 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




PowerShell - New-SelfSignedCertificate를 사용해 CA 인증서 생성 및 인증서 서명 방법

예전에 openssl을 이용해 CA로부터 서명한 인증서를 생성하는 방법에 대해 알아봤는데요,

openssl - CA로부터 인증받은 새로운 인증서를 생성하는 방법
; https://www.sysnet.pe.kr/2/0/12570

이번에는 PowerShell을 통해 알아보겠습니다. ^^




"openssl - CA로부터 인증받은 새로운 인증서를 생성하는 방법" 글에서는 기존에 있었던 CA 인증서를 이용했는데, 이번에는 CA 인증서도 새롭게 생성하는 방법도 알아보겠습니다.

간단하게 다음과 같이 실행하면 CA 인증서가 생성됩니다.

New-SelfSignedCertificate -Type Custom -Subject "CATest" -KeyUsage CertSign,DigitalSignature,KeyEncipherment -CertStoreLocation "Cert:\LocalMachine\My" -TextExtension @("2.5.29.19={critical}{text}ca=TRUE") -SerialNumber 00 -NotAfter (Get-Date).AddYears(10)


참고로 위의 인증서 옵션은 k8s에서 생성한 ca.crt와 동일한 출력을 갖습니다. 자, 그럼 이 CA 인증서로 서명한 새로운 인증서를 생성할 텐데요, 이를 위해 PowerShell에서는 CA 인증서의 참조 값을 가져야 하므로 다음과 같이 Get-ChildItem으로 미리 변수에 저장해 둡니다.

$rootCA = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object { $_.Subject -eq "CN=CATest" }

// 혹은 -Path 경로에 해당 인증서의 Thumbprint 값을 알고 있다면, (예를 들어 "be28fa76285084cf6c88d4eb432b517f8add6f14")
// $rootCA = Get-ChildItem -Path Cert:\LocalMachine\My\be28fa76285084cf6c88d4eb432b517f8add6f14
// $rootCA = Get-ChildItem -Path (Join-Path Cert:\LocalMachine\My $rootCA.Thumbprint)

// 혹은 CA 인증서 생성 당시 New-SelfSignedCertificate의 반환 값으로 받거나,
// $rootCA = New-SelfSignedCertificate ...

이후 해당 참조를 -Signer 인자로 넘겨 주면 새롭게 인증서를 생성할 수 있습니다.

New-SelfSignedCertificate -Signer $rootCA -NotAfter (Get-Date).AddYears(10) -Subject "CASigned" -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.3","2.5.29.19={text}") -CertStoreLocation "Cert:\CurrentUser\My" -KeyUsage DigitalSignature

// 혹은 CA 인증서를 생성하지 않았다면 "-TestRoot" 옵션을 사용하면 "Intermediate Certification Authorities" 경로에 있는 "CertReq Test Root"를 사용

참고로, 이전 글에서는 실제 CA 인증 기관이 동작하는 것처럼 CSR까지 생성하는 등의 과정을 거쳤던 반면 이번 글에서는 그냥 개발자가 사용할 목적의 인증서를 빠르게 생성한 것이므로 그 CSR 요청 과정은 생략하였습니다.




openssl의 경우 명령행 인자와 cnf 파일을 이용해 다양한 옵션 설정을 했는데요, New-SelfSignedCertificate의 경우 이에 대한 옵션을 대략 다음과 같은 설정으로 할 수 있습니다.

New-SelfSignedCertificate
; https://learn.microsoft.com/en-us/powershell/module/pkiclient/new-selfsignedcertificate

Serial Number
    00
        -SerialNumber 00

Valid to
    10년
        -NotAfter (Get-Date).AddYears(10)

Enhanced Key Usage: -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.3", "2.5.29.19={text}")
    Secure Email
        -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.4")
    Client Authentication (default)
        -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.2")
    Server Authentication (default)
        -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.1")
    Code Sigining
        -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.3")
    Timestamp Sigining
        -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.8")
    Any Purpose
        -TextExtension @("2.5.29.37={text}2.5.29.37.0")
    (empty)
        -Type Custom

Basic Constraints:
    Subject Type=End Entity
    Path Length Constraint=None
        -TextExtension @("2.5.29.19={text}")
    Subject Type=CA
    Path Length Constraint=None
        -TextExtension @("2.5.29.19={critical}{text}ca=TRUE")

Subject Alternative Name
    Other Name:
      Principal Name=pattifuller@contoso.com
        -TextExtension @("2.5.29.17={text}upn=pattifuller@contoso.com")
    DNS Name=localhost, IP Address=127.0.0.1, IP Address=0000:0000:0000:0000:0000:0000:0000:0001
        -TextExtension @("2.5.29.17={text}DNS=localhost&IPAddress=127.0.0.1&IPAddress=::1")

Key Usage
    Digital Signature (80)
        -KeyUsage DigitalSignature
    Digital Signature, Key Encipherment (a0) (default)
        -KeyUsage DigitalSignature,KeyEncipherment
    Certificate Signing
        -KeyUsage CertSign
    CRL Signing
        -KeyUsage CRLSign
    Data Encipherment
        -KeyUsage DataEncipherment

Subject
    CN = Patti Fuller
    OU = UserAccounts
    DC = corp
    DC = contoso
    DC = com
        -Subject "CN=Patti Fuller,OU=UserAccounts,DC=corp,DC=contoso,DC=com"

Public key
    RSA (2048 Bits) (default)
        -KeyAlgorithm RSA -KeyLength 2048
    ECC (256 Bits)
        -KeyAlgorithm ECDSA_nistP256 -CurveExport CurveName

Signature algorithm
    sha256RSA (default)
        -KeyAlgorithm RSA
    sha256ECDSA
        -KeyAlgorithm ECDSA_nistP256 -CurveExport CurveName

Public key parameters
    05 00 (default)
        -KeyAlgorithm RSA
    ECDSA_P256
        -KeyAlgorithm ECDSA_nistP256 -CurveExport CurveName

SMIME Capabilities
    [1]SMIME Capability
         Object ID=2.16.840.1.101.3.4.1.42
    [2]SMIME Capability
         Object ID=2.16.840.1.101.3.4.1.45
    [3]SMIME Capability
         Object ID=2.16.840.1.101.3.4.1.22
    [4]SMIME Capability
         Object ID=2.16.840.1.101.3.4.1.25
    [5]SMIME Capability
         Object ID=2.16.840.1.101.3.4.1.2
    [6]SMIME Capability
         Object ID=2.16.840.1.101.3.4.1.5
    [7]SMIME Capability
         Object ID=1.2.840.113549.3.7
    [8]SMIME Capability
         Object ID=1.3.14.3.2.7
    [9]SMIME Capability
         Object ID=1.2.840.113549.3.2
         Parameters=02 02 00 80
    [10]SMIME Capability
         Object ID=1.2.840.113549.3.4
         Parameters=02 02 02 00
        -SmimeCapabilities

인증서 등록 위치
    Local Machine/My
        -CertStoreLocation "Cert:\LocalMachine\My"
    Local Machine/Trusted Root Certification Authorities
        -CertStoreLocation "Cert:\LocalMachine\Root"
    Current User/My
        -CertStoreLocation "Cert:\CurrentUser\My"
    
개인키 exportable 여부 지정
    Exportable (default)
        -KeyExportPolicy Exportable
    NonExportable
        -KeyExportPolicy NonExportable




새로 생성한 인증서가 등록될 위치를 My 이외의 경로를 지정하면 다음과 같은 오류가 발생합니다.

PS C:\temp> New-SelfSignedCertificate -Type Custom -Subject "CATest" -CertStoreLocation "Cert:\LocalMachine\Root" ...[생략]...
New-SelfSignedCertificate : A new certificate can only be installed into MY store.
At line:1 char:1
+ New-SelfSignedCertificate -Type Custom -Subject "CATest" -CertStoreLocation "Ce ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-SelfSignedCertificate], ParameterBindingException
    + FullyQualifiedErrorId : RuntimeException,Microsoft.CertificateServices.Commands.NewSelfSignedCertificateCommand"

그러니까, 안 되는 것은 안 되는 겁니다. ^^ 이를 우회하기 위해 해당 인증서를 일단 My에 생성한 다음 Root로 이동하는 작업을 별도로 해야 합니다.

$newCertPath = New-SelfSignedCertificate -Type Custom -Subject "CATest" -CertStoreLocation "Cert:\LocalMachine\My" 

Move-Item $newCertPath -Destination Cert:\LocalMachine\Root

참고로 예전에 C# 코드를 이용해 인증서를 등록하는 방법도 있고. ^^

C# - 인증서를 윈도우에 설치하는 방법
; https://www.sysnet.pe.kr/2/0/11719




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

[연관 글]






[최초 등록일: ]
[최종 수정일: 6/13/2023]

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

비밀번호

댓글 작성자
 




1  2  3  4  5  6  [7]  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13447정성태11/16/20232481닷넷: 2162. ASP.NET Core 웹 사이트의 SSL 설정을 코드로 하는 방법
13446정성태11/16/20232418닷넷: 2161. .NET Conf 2023 - Day 1 Blazor 개요 정리
13445정성태11/15/20232695Linux: 62. 리눅스/WSL에서 CA 인증서를 저장하는 방법
13444정성태11/15/20232452닷넷: 2160. C# 12 - Experimental 특성 지원
13443정성태11/14/20232489개발 환경 구성: 687. OpenSSL로 생성한 사용자 인증서를 ASP.NET Core 웹 사이트에 적용하는 방법
13442정성태11/13/20232317개발 환경 구성: 686. 비주얼 스튜디오로 실행한 ASP.NET Core 사이트를 WSL 2 인스턴스에서 https로 접속하는 방법
13441정성태11/12/20232644닷넷: 2159. C# - ASP.NET Core 프로젝트에서 서버 Socket을 직접 생성하는 방법파일 다운로드1
13440정성태11/11/20232350Windows: 253. 소켓 Listen 시 방화벽의 Public/Private 제어 기능이 비활성화된 경우
13439정성태11/10/20232840닷넷: 2158. C# - 소켓 포트를 미리 시스템에 등록/예약해 사용하는 방법(Port Exclusion Ranges)파일 다운로드1
13438정성태11/9/20232459닷넷: 2157. C# - WinRT 기능을 이용해 윈도우에서 실행 중인 Media App 제어
13437정성태11/8/20232653닷넷: 2156. .NET 7 이상의 콘솔 프로그램을 (dockerfile 없이) 로컬 docker에 배포하는 방법
13436정성태11/7/20232889닷넷: 2155. C# - .NET 8 런타임부터 (Reflection 없이) 특성을 이용해 public이 아닌 멤버 호출 가능
13435정성태11/6/20232825닷넷: 2154. C# - 네이티브 자원을 포함한 관리 개체(예: 스레드)의 GC 정리
13434정성태11/1/20232617스크립트: 62. 파이썬 - class의 정적 함수를 동적으로 교체
13433정성태11/1/20232341스크립트: 61. 파이썬 - 함수 오버로딩 미지원
13432정성태10/31/20232373오류 유형: 878. 탐색기의 WSL 디렉터리 접근 시 "Attempt to access invalid address." 오류 발생
13431정성태10/31/20232699스크립트: 60. 파이썬 - 비동기 FastAPI 앱을 gunicorn으로 호스팅
13430정성태10/30/20232596닷넷: 2153. C# - 사용자가 빌드한 ICU dll 파일을 사용하는 방법
13429정성태10/27/20232849닷넷: 2152. Win32 Interop - C/C++ DLL로부터 이중 포인터 버퍼를 C#으로 받는 예제파일 다운로드1
13428정성태10/25/20232897닷넷: 2151. C# 12 - ref readonly 매개변수
13427정성태10/18/20233079닷넷: 2150. C# 12 - 정적 문맥에서 인스턴스 멤버에 대한 nameof 접근 허용(Allow nameof to always access instance members from static context)
13426정성태10/13/20233260스크립트: 59. 파이썬 - 비동기 호출 함수(run_until_complete, run_in_executor, create_task, run_in_threadpool)
13425정성태10/11/20233084닷넷: 2149. C# - PLinq의 Partitioner<T>를 이용한 사용자 정의 분할파일 다운로드1
13423정성태10/6/20233059스크립트: 58. 파이썬 - async/await 기본 사용법
13422정성태10/5/20233199닷넷: 2148. C# - async 유무에 따른 awaitable 메서드의 병렬 및 예외 처리
13421정성태10/4/20233238닷넷: 2147. C# - 비동기 메서드의 async 예약어 유무에 따른 차이
1  2  3  4  5  6  [7]  8  9  10  11  12  13  14  15  ...