Microsoft MVP성태의 닷넷 이야기
.NET Framework: 647. 닷넷(C#) 코드로 인증서 요청 코드 만드는 방법 [링크 복사], [링크+제목 복사],
조회: 14403
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

닷넷(C#) 코드로 인증서 요청 코드 만드는 방법

인터넷 떠돌다가 본 코드인데,

How to create a certificate request with CertEnroll and .NET (C#)
; https://learn.microsoft.com/en-us/archive/blogs/alejacma/how-to-create-a-certificate-request-with-certenroll-and-net-c

Cipher selection for sslStream in .NET 4.5
; http://stackoverflow.com/questions/22825663/cipher-selection-for-sslstream-in-net-4-5

저도 실습해봤습니다. ^^ C# 프로젝트를 하나 만들고 "CertEnroll 1.0 Type Library" 참조 추가를 한 다음 위의 코드를 거의 그대로 베끼면 됩니다.

using System;
using CERTENROLLLib;
using System.Security.Cryptography.X509Certificates;

namespace ConsoleApp1
{
    class Program
    {
        public static string GenerateRequest(string Subject, StoreLocation Location, string providerName, int KeyLength)
        {
            //code originally came from: http://blogs.msdn.com/b/alejacma/archive/2008/09/05/how-to-create-a-certificate-request-with-certenroll-and-net-c.aspx
            //modified version of it is here: http://stackoverflow.com/questions/16755634/issue-generating-a-csr-in-windows-vista-cx509certificaterequestpkcs10
            //here is the standard for certificates: http://www.ietf.org/rfc/rfc3280.txt


            //the PKCS#10 certificate request (https://learn.microsoft.com/en-us/windows/win32/api/certenroll/nn-certenroll-ix509certificaterequestpkcs10)
            CX509CertificateRequestPkcs10 objPkcs10 = new CX509CertificateRequestPkcs10();

            //assymetric private key that can be used for encryption (https://learn.microsoft.com/en-us/windows/win32/api/certenroll/nn-certenroll-ix509privatekey)
            CX509PrivateKey objPrivateKey = new CX509PrivateKey();

            //access to the general information about a cryptographic provider (https://learn.microsoft.com/en-us/windows/win32/api/certenroll/nn-certenroll-icspinformation)
            CCspInformation objCSP = new CCspInformation();

            //collection on cryptographic providers available: https://learn.microsoft.com/en-us/windows/win32/api/certenroll/nn-certenroll-icspinformation
            CCspInformations objCSPs = new CCspInformations();

            CX500DistinguishedName objDN = new CX500DistinguishedName();

            //top level object that enables installing a certificate response https://learn.microsoft.com/en-us/windows/win32/api/certenroll/nn-certenroll-ix509enrollment
            CX509Enrollment objEnroll = new CX509Enrollment();
            CObjectIds objObjectIds = new CObjectIds();
            CObjectId objObjectId = new CObjectId();
            CObjectId objObjectId2 = new CObjectId();
            CX509ExtensionKeyUsage objExtensionKeyUsage = new CX509ExtensionKeyUsage();
            CX509ExtensionEnhancedKeyUsage objX509ExtensionEnhancedKeyUsage = new CX509ExtensionEnhancedKeyUsage();

            string csr_pem = null;

            //  Initialize the csp object using the desired Cryptograhic Service Provider (CSP)
            objCSPs.AddAvailableCsps();

            //Provide key container name, key length and key spec to the private key object
            objPrivateKey.ProviderName = providerName;
            objPrivateKey.Length = KeyLength;
            objPrivateKey.KeySpec = X509KeySpec.XCN_AT_KEYEXCHANGE; //Must flag as XCN_AT_KEYEXCHANGE to use this certificate for exchanging symmetric keys (needed for most SSL cipher suites)
            objPrivateKey.KeyUsage = X509PrivateKeyUsageFlags.XCN_NCRYPT_ALLOW_ALL_USAGES;
            if (Location == StoreLocation.LocalMachine)
                objPrivateKey.MachineContext = true;
            else
                objPrivateKey.MachineContext = false; //must set this to true if installing to the local machine certificate store

            objPrivateKey.ExportPolicy = X509PrivateKeyExportFlags.XCN_NCRYPT_ALLOW_EXPORT_FLAG;    //must set this if we want to be able to export it later. 
            objPrivateKey.CspInformations = objCSPs;

            //  Create the actual key pair
            objPrivateKey.Create();

            //  Initialize the PKCS#10 certificate request object based on the private key.
            //  Using the context, indicate that this is a user certificate request and don't
            //  provide a template name
            if (Location == StoreLocation.LocalMachine)
                objPkcs10.InitializeFromPrivateKey(X509CertificateEnrollmentContext.ContextMachine, objPrivateKey, "");
            else
                objPkcs10.InitializeFromPrivateKey(X509CertificateEnrollmentContext.ContextUser, objPrivateKey, "");

            //Set hash to sha256
            CObjectId hashobj = new CObjectId();
            hashobj.InitializeFromAlgorithmName(ObjectIdGroupId.XCN_CRYPT_HASH_ALG_OID_GROUP_ID, ObjectIdPublicKeyFlags.XCN_CRYPT_OID_INFO_PUBKEY_ANY, AlgorithmFlags.AlgorithmFlagsNone, "SHA256");
            objPkcs10.HashAlgorithm = hashobj;

            // Key Usage Extension -- we only need digital signature and key encipherment for TLS:
            //  NOTE: in openSSL, I didn't used to request any specific extensions. Instead, I let the CA add them
            objExtensionKeyUsage.InitializeEncode(
                CERTENROLLLib.X509KeyUsageFlags.XCN_CERT_DIGITAL_SIGNATURE_KEY_USAGE |
                CERTENROLLLib.X509KeyUsageFlags.XCN_CERT_KEY_ENCIPHERMENT_KEY_USAGE
            );
            objPkcs10.X509Extensions.Add((CX509Extension)objExtensionKeyUsage);

            // Enhanced Key Usage Extension
            objObjectId.InitializeFromValue("1.3.6.1.5.5.7.3.1"); // OID for Server Authentication usage (see this: http://stackoverflow.com/questions/17477279/client-authentication-1-3-6-1-5-5-7-3-2-oid-in-server-certificates)
            objObjectId2.InitializeFromValue("1.3.6.1.5.5.7.3.2"); // OID for Client Authentication usage (see this: http://stackoverflow.com/questions/17477279/client-authentication-1-3-6-1-5-5-7-3-2-oid-in-server-certificates)
            objObjectIds.Add(objObjectId);
            objObjectIds.Add(objObjectId2);
            objX509ExtensionEnhancedKeyUsage.InitializeEncode(objObjectIds);
            objPkcs10.X509Extensions.Add((CX509Extension)objX509ExtensionEnhancedKeyUsage);

            //  Encode the name in using the Distinguished Name object
            // see here: http://https://learn.microsoft.com/en-us/windows/win32/api/certenroll/ne-certenroll-x500nameflags
            objDN.Encode(Subject, X500NameFlags.XCN_CERT_NAME_STR_SEMICOLON_FLAG);

            // Assign the subject name by using the Distinguished Name object initialized above
            objPkcs10.Subject = objDN;

            //suppress extra attributes:
            objPkcs10.SuppressDefaults = true;

            // Create enrollment request
            // objEnroll.InitializeFromTemplateName(X509CertificateEnrollmentContext.ContextMachine, "WebServer");
            objEnroll.InitializeFromRequest(objPkcs10);
            csr_pem = objEnroll.CreateRequest(EncodingType.XCN_CRYPT_STRING_BASE64);
            csr_pem = "-----BEGIN CERTIFICATE REQUEST-----\r\n" + csr_pem + "-----END CERTIFICATE REQUEST-----";

            return csr_pem;
        }
    }
}

사용을 위한 호출은 이렇게 간단합니다. ^^

static void Main(string[] args)
{
    string txt = GenerateRequest("CN=the10", StoreLocation.CurrentUser, "Microsoft RSA SChannel Cryptographic Provider", 1024);
    Console.WriteLine(txt);
}

출력된 txt 내용은 이렇게 나옵니다.

-----BEGIN CERTIFICATE REQUEST-----
MIIBjzCB+QIBADAQMQ4wDAYDVQQDDAV0aGUxMDCBnzANBgkqhkiG9w0BAQEFAAOB
jQAwgYkCgYEAtotimc9N0i6VaWxbgktp28KxbX7xlr3QXFhosqGh2Rqs5fP032HZ
CZ920PciQSijof8GbLL/sVMImPD8ACfoWzfeP8HDoC6YaXD2CtibxvdoXAEkFrfE
RtfG/gxCyEc6O7zDDIyPygPG+R8ih3I2HflzxQmNg1kJ/bzKgLz+b10CAwEAAaBA
MD4GCSqGSIb3DQEJDjExMC8wDgYDVR0PAQH/BAQDAgWgMB0GA1UdJQQWMBQGCCsG
AQUFBwMBBggrBgEFBQcDAjANBgkqhkiG9w0BAQsFAAOBgQAMOHR9g9rpDeDt8a+Y
gNGsJ0G3JGs9bEFwY6m8mMHiRDBlIueIBnn7PF9+CIeV2xVGgPMtyDL3wYnrjgjM
WEO2OXpuNNl2m1B14oBYBT+5Bqo06S5rckzMGmrPArUOw94zdnvcp22h3RH86rTA
MmhAarv9DL3tVIiP/Ofh9xOv1Q==
-----END CERTIFICATE REQUEST-----

그러니까 위의 코드는 결국 다음의 글에서 설명한,

2. 웹 사이트에 SSL 을 적용
; https://www.sysnet.pe.kr/2/0/372

1번 단계인 "IIS 관리자에서 인증서 서비스로 보낼 '요청 파일' 준비"를 한 것입니다.




실제로 위의 코드가 생산한 CSR 내용으로 인증서를 발급받아 볼까요? ^^ 정식 인증서 업체로는 곤란하니, Windows 서버에 인증서 서비스를 설치한 다음 CA 관리 콘솔을 띄웁니다.

다음 그림에서 보는 것처럼,

create_csr_1.png

CA 서버 명 노드를 마우스 우 클릭해 "All Tasks" / "Submit new request..." 메뉴를 선택하면 파일 선택 대화창이 뜹니다. 그럼, 위의 "-----BEGIN CERTIFICATE REQUEST----- ... -----END CERTIFICATE REQUEST-----" 내용을 파일로 저장한 것을 선택해 줍니다.

선택하자마자, CA 서비스는 해당 인증서 요청을 "Pending Requests"에 보관합니다.

create_csr_2.png

그럼, 위의 인증서에 마우스 우 클릭하고 "All Tasks" / "Issue" 메뉴를 눌러 명시적인 발급을 하면 곧바로 "Pending Requests"에는 사라지고 "Issued Certificates" 영역으로 이동합니다.

결국 다음과 같은 인증서를 확인할 수 있습니다. ^^

create_csr_3.png

정말 잘 되는군요. ^^

(첨부 파일은 이 글의 예제 코드를 포함합니다.)




참고로, Active Directory가 설치된 Enterprise CA 서비스에 이 글의 CSR 내용을 전달하면 다음과 같은 오류를 만나게 됩니다.

The request contains no certificate template information. 0x80094801 (-2146875391 CERTSRV_E_NO_CERT_TYPE)
Denied by Policy Module 0x80094801, The request does not contain a certificate template extension or the CertificateTemplate request attribute.


원인은 이렇고,

You may receive a "The request contains no certificate template information" error message when you submit a CSR to an enterprise CA by using the Certification Authority Microsoft Management Console (MMC) snap-in in Windows Server 2003
; https://support.microsoft.com/en-us/help/910249/you-may-receive-a-the-request-contains-no-certificate-template-information-error-message-when-you-submit-a-csr-to-an-enterprise-ca-by-using-the-certification-authority-microsoft-management-console-mmc-snap-in-in-windows-server-2003

따라서, 인증서 템플릿을 반드시 지정해야 합니다.

방법을 찾아보니, CX509Enrollment 객체에 InitializeFromTemplateName 메서드가 제공되는데요. 아마도 "objEnroll.InitializeFromRequest(objPkcs10);" 호출 대신 "objEnroll.InitializeFromTemplateName(X509CertificateEnrollmentContext.ContextMachine, "WebServer");"라는 식으로 호출해야 할 듯!




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







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

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

비밀번호

댓글 작성자
 




... 16  17  18  19  20  21  22  23  24  25  26  [27]  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
12962정성태2/9/20227872오류 유형: 793. 마이크로소프트 스토어 - 제품이 존재하지 않습니다. 재고가 없는 것일 수 있습니다.
12961정성태2/8/20228001.NET Framework: 1151. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 비디오 프레임의 크기 및 포맷 변경 예제(scaling_video.c) [7]파일 다운로드1
12960정성태2/8/20227409개발 환경 구성: 637. ffmpeg(FFmpeg.AutoGen)를 이용한 비디오 디코딩 예제(decode_video.c) - 세 번째 이야기
12959정성태2/7/20228128.NET Framework: 1150. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 비디오 디코딩 예제(decode_video.c) - 두 번째 이야기 [2]파일 다운로드1
12958정성태2/6/20228193.NET Framework: 1149. C# - ffmpeg(FFmpeg.AutoGen) - 비디오 프레임 디코딩 [2]파일 다운로드1
12957정성태2/6/20227779개발 환경 구성: 636. ffmpeg.exe를 이용해 planar 포맷의 데이터를 packed 형식으로 변환하는 방법? [2]
12956정성태2/4/20227004.NET Framework: 1148. C# - ffmpeg(FFmpeg.AutoGen) - decoding 과정 [2]파일 다운로드1
12955정성태2/4/20226469개발 환경 구성: 635. 비주얼 스튜디오에서 실행하던 ASP.NET Core (.NET Framework) 응용 프로그램을 명령행에서 실행하는 방법 (2)
12954정성태2/4/20226323VS.NET IDE: 173. 비주얼 스튜디오 - Output 창에 색상이 지정된 출력 결과가 "[39m[22m" 식의 문자로 나오는 문제
12953정성태2/2/20226508Linux: 48. Windows 11 + WSL 우분투 GUI 환경에서 한글 출력
12952정성태2/2/20227003.NET Framework: 1148. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 오디오 필터 예제(filter_audio.c)파일 다운로드1
12951정성태2/2/20226979.NET Framework: 1147. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 오디오 필터링 예제(filtering_audio.c)파일 다운로드1
12950정성태2/1/20226565.NET Framework: 1146. .NET 6에 추가되지 않은 Generic Math (예: INumber<T>)
12949정성태2/1/20226424.NET Framework: 1145. C# - ffmpeg(FFmpeg.AutoGen) - Codec 정보 열람 및 사용 준비파일 다운로드1
12948정성태1/30/20226545.NET Framework: 1144. C# - ffmpeg(FFmpeg.AutoGen) AVFormatContext를 이용해 ffprobe처럼 정보 출력파일 다운로드1
12947정성태1/30/20227702개발 환경 구성: 634. ffmpeg.exe - 기존 동영상 컨테이너에 다중 스트림을 추가하는 방법
12946정성태1/28/20226169오류 유형: 792. .NET Core - 로컬 개발 중에 docker 호스팅으로 바꾸는 경우 SQL 서버 접근 방법
12945정성태1/28/20226446오류 유형: 791. SQL 서버 로그인 시 localhost는 되고, 127.0.0.1로는 안 되는 문제
12944정성태1/28/20228876.NET Framework: 1143. C# - Entity Framework Core 6 개요
12943정성태1/27/20227768.NET Framework: 1142. .NET 5+로 포팅 시 플랫폼 호환성 경고 메시지(SYSLIB0006, SYSLIB0011, CA1416)파일 다운로드1
12942정성태1/27/20228041.NET Framework: 1141. XmlSerializer와 Dictionary 타입파일 다운로드1
12941정성태1/26/20229442오류 유형: 790. AKS/k8s - pod 상태가 Pending으로 지속되는 경우
12940정성태1/26/20226809오류 유형: 789. AKS에서 hpa에 따른 autoscale 기능이 동작하지 않는다면?
12939정성태1/25/20227527.NET Framework: 1140. C# - ffmpeg(FFmpeg.AutoGen)를 이용해 MP3 오디오 파일 인코딩/디코딩하는 예제파일 다운로드1
12938정성태1/24/20229879개발 환경 구성: 633. Docker Desktop + k8s 환경에서 local 이미지를 사용하는 방법
12937정성태1/24/20227688.NET Framework: 1139. C# - ffmpeg(FFmpeg.AutoGen)를 이용해 오디오(mp2) 인코딩하는 예제(encode_audio.c) [2]파일 다운로드1
... 16  17  18  19  20  21  22  23  24  25  26  [27]  28  29  30  ...