将证书和私钥导入到ukey中

将证书和私钥导入到ukey中

DWORD  cbPrivateKeyBlob = 0;
BYTE*  pbPrivateKeyBlob = NULL;
PCCERT_CONTEXT pCertCtx=NULL;
DWORD dwBufferLen = 0;
LPBYTE pbBuffer = NULL;
HCRYPTPROV  hCryptProv = 0;
HCRYPTKEY hImpKey = 0;
//get cert
HCERTSTORE hCertStore = CertOpenStore(
            CERT_STORE_PROV_FILENAME,
            PKCS_7_ASN_ENCODING | X509_ASN_ENCODING,
            NULL,
            (CERT_STORE_OPEN_EXISTING_FLAG | CERT_STORE_READONLY_FLAG),
            L"test.cert.cer");
if ((pCertCtx = CertEnumCertificatesInStore(hCertStore, pCertCtx)) == NULL)
    return -1;
//get priv
char szPemPrivKey[1024];
FILE *fp=fopen("test.key.pem","rb");
if(fp==NULL)
    return -1;
fseek(fp, 0, SEEK_SET);
fread(szPemPrivKey, 1024, 1, fp);
if(! CryptStringToBinaryA (szPemPrivKey,0,CRYPT_STRING_BASE64HEADER,NULL,&dwBufferLen,NULL,NULL))
{
    qDebug()<<("Failed to convert BASE64 private key. Error 0x%.8X\n"+GetLastError());
    return -1;
}

pbBuffer = (LPBYTE) LocalAlloc(0, dwBufferLen);
if (! CryptStringToBinaryA (szPemPrivKey, 0, CRYPT_STRING_BASE64HEADER, pbBuffer, &dwBufferLen, NULL, NULL))
{
    qDebug()<<("Failed to convert BASE64 private key. Error 0x%.8X\n"+GetLastError());
    return -1;
}

if (!CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, PKCS_RSA_PRIVATE_KEY, pbBuffer, dwBufferLen, 0, NULL, NULL, &cbPrivateKeyBlob))
{
    qDebug()<<("Failed to parse private key. Error 0x%.8X\n"+ GetLastError());
    return -1;
}

pbPrivateKeyBlob = (LPBYTE) LocalAlloc(0, cbPrivateKeyBlob);
if (!CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, PKCS_RSA_PRIVATE_KEY, pbBuffer, dwBufferLen, 0, NULL, pbPrivateKeyBlob, &cbPrivateKeyBlob))
{
    qDebug()<<("Failed to parse private key. Error 0x%.8X\n"+ GetLastError());
    return -1;
}
//import
// To initialize the ePass2000 CSP
if(CryptAcquireContextA(
            &hCryptProv,
            KEY_CA_CONTAINER,
            CSP_NAME,
            PROV_RSA_FULL,
            CRYPT_NEWKEYSET))
{
    qDebug()<<("EnterSafe ePass2001 CSP v1.0 has been acquired. \n");
}
else if (GetLastError() == NTE_EXISTS)
{
    if(CryptAcquireContextA(
                &hCryptProv,
                KEY_CA_CONTAINER,
                CSP_NAME,
                PROV_RSA_FULL,
                CRYPT_DELETEKEYSET))//删除容器
    {
        qDebug()<<("EnterSafe ePass2001 CSP v1.0 has been del. \n");
        if(CryptAcquireContextA(
                    &hCryptProv,
                    KEY_CA_CONTAINER,
                    CSP_NAME,
                    PROV_RSA_FULL,
                    CRYPT_NEWKEYSET))
        {
            qDebug()<<("EnterSafe ePass2001 CSP v1.0 has been acquired. \n");
        }
    }
    else
    {
        qDebug()<<("CryptAcquireContext failed.");
        return -1;
    }
}
else
{
    qDebug()<<("CryptAcquireContext failed.");
    return -1;
}

// To import private key
if(CryptImportKey(
            hCryptProv,
            pbPrivateKeyBlob,
            cbPrivateKeyBlob,
            NULL,
            0,
            &hImpKey))
{
    LocalFree( pbPrivateKeyBlob);
    qDebug()<<("The private key has been imported !\n");
}
else
{
    LocalFree( pbPrivateKeyBlob);
    qDebug()<<("Failed to import private key!");
    return -1;
}


// To import certificate
if(CryptSetKeyParam(
            hImpKey,
            KP_CERTIFICATE,
            pCertCtx->pbCertEncoded,
            0
            ))
{
    qDebug()<<("The certificate has been imported!\n");
}
else
{
    qDebug()<<("Failed to import certificate!\n");
    return -1;
}

// Release ePass2000 CSP
CryptReleaseContext(hCryptProv, 0);
qDebug()<<("The private key and certificate has been imported!\n");
return 0;

你可能感兴趣的:(将证书和私钥导入到ukey中)