关注Exporting PFX file from windows certificates store

我试图从Windows密钥库导出一个包含证书和私钥的pfx文件,并将其转储到PEM格式文件,该文件应由openssl读取 .

在将文件导入WCS期间,私钥被标记为可导出

我深入研究了大多数问题,这些问题涉及提取证书,但它们都没有涉及包含私钥的pfx文件,或者它们使用工具,例如mmc等我必须将它编码为C语言中的应用程序的一部分 .

从WCS导出证书之后的主要问题是在将证书和私钥部分从二进制格式转换为base64编码(应添加部分 Headers 和预告片)之前将证书和私钥部分分开,并且可能还从pfx转换为PEM .

在MS示例中,我的代码基于证书是序列化的,但在其他示例中,它不是 .

我还被建议将导出的证书导入新的“虚拟”商店,但我不明白为什么 .

对我来说重要的是从WCS导出证书和私钥以格式化并放置哪个openssl可以从中导入它们 .

如果有人有其他办法,我将感谢任何信息 . 提前感谢所有评论/答案 .

我的代码:

//-----------------------------------------

#pragma comment(lib, "crypt32.lib")

#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
#define MY_ENCODING_TYPE  (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
void MyHandleError(char *s);

char *base64_encode(const unsigned char *data,
                    size_t input_length,
                    size_t *output_length);

void main(void)
{
//-------------------------------------------------------------------
// Declare and initialize variables.
HCERTSTORE         hSystemStore;
PCCERT_CONTEXT     pCertContext = NULL;
char pszStoreName[256] = "root";
char               pszNameString[256] = "CARootTest"; 
char*              pbElement;
size_t              cbElement;
//-------------------------------------------------------------------
// Open a system certificate store.
if(hSystemStore = CertOpenSystemStore(
    0,
    pszStoreName))
{
  printf("The %s system store is open. Continue.\n", pszStoreName );
}
else
{
  MyHandleError("The first system store did not open.");
}
//-------------------------------------------------------------------
// Get a certificate that has the desired friendly name. 
if(pCertContext=CertFindCertificateInStore(
      hSystemStore,
      MY_ENCODING_TYPE,             // Use X509_ASN_ENCODING
      0,                            // No dwFlags needed 
      CERT_NAME_FRIENDLY_DISPLAY_TYPE,        // Find a certificate
      pszNameString, // The Unicode string to be found
                                    // in a certificate's subject
      NULL))                        // NULL for the first call 
{
  printf("The %s certificate was found. \n", pszNameString);
}
else
{
   MyHandleError("Could not find the %s certificate.");
}

pbElement = base64_encode(pCertContext->pbCertEncoded,
                    sizeof (pCertContext->pbCertEncoded),
                    &cbElement);
/*
//-------------------------------------------------------------------
//  pbElement could be written to a file ??

FILE *fp;
errno_t err;
if ((err = fopen_s(&fp, "cert.p12", "w")) != 0)
    printf("File was not opened\n");
else
    fprintf(fp, "%s", pbElement);
fclose(fp);

//-------------------------------------------------------------------
// Free memory.

free(pbElement);
CertCloseStore(hSystemStore,0);
printf("The program ran without error to the end.\n");
} // End of main

//-------------------------------------------------------------------
void MyHandleError(char *s)
{
    fprintf(stderr,"An error occurred in running the program. \n");
    fprintf(stderr,"%s\n",s);
    fprintf(stderr, "Error number %x.\n", GetLastError());
    fprintf(stderr, "Program terminating. \n");
    exit(1);
} // End of MyHandleError