Solved it!! Thought of sharing the solution here, it might be helpful to someone.

Qt Code:
  1. // Open the certificate store to be searched.
  2. HCERTSTORE hSystemStore = CertOpenStore((LPCSTR)(CERT_STORE_PROV_SYSTEM), 0, NULL,
  3. CERT_SYSTEM_STORE_LOCAL_MACHINE, L"MY");
  4.  
  5. CRYPT_DATA_BLOB dataBlob = {0};
  6. QString password("password"); // your password for the cretificate and private key goes here
  7.  
  8. if(PFXExportCertStoreEx(hSystemStore, &dataBlob, password.toStdWString().c_str(), NULL,
  9. EXPORT_PRIVATE_KEYS | REPORT_NOT_ABLE_TO_EXPORT_PRIVATE_KEY | REPORT_NO_PRIVATE_KEY))
  10. {
  11. if (dataBlob.cbData > 0)
  12. {
  13. dataBlob.pbData = (BYTE*)malloc(dataBlob.cbData);
  14. if (PFXExportCertStoreEx(hSystemStore, &dataBlob, password.toStdWString().c_str(), NULL,
  15. EXPORT_PRIVATE_KEYS | REPORT_NOT_ABLE_TO_EXPORT_PRIVATE_KEY | REPORT_NO_PRIVATE_KEY))
  16. {
  17. EVP_PKEY *pkey;
  18. X509 *cert;
  19. STACK_OF(X509) *ca = NULL;
  20. PKCS12 *p12;
  21. int i;
  22. CRYPTO_malloc_init();
  23. OpenSSL_add_all_algorithms();
  24. SSLeay_add_all_algorithms();
  25. ERR_load_crypto_strings();
  26.  
  27. BIO* input = BIO_new_mem_buf((void*)dataBlob.pbData, dataBlob.cbData);
  28. p12 = d2i_PKCS12_bio(input, NULL);
  29.  
  30. PKCS12_parse(p12, password.toStdString().c_str(), &pkey, &cert, &ca);
  31. PKCS12_free(p12);
  32.  
  33. if (cert)
  34. {
  35. BIO *boCert = BIO_new( BIO_s_mem() );
  36.  
  37. PEM_write_bio_X509(boCert, cert);
  38. if (ca && sk_X509_num(ca))
  39. {
  40. for (i = 0; i < sk_X509_num(ca); i++)
  41. {
  42. PEM_write_bio_X509(boCert, sk_X509_value(ca, i));
  43. }
  44. }
  45. char *certStr;
  46. long len = BIO_get_mem_data(boCert, &certStr);
  47.  
  48. QSslCertificate localCertificate(QByteArray::fromRawData(certStr, len));
  49. mySslSocket->setLocalCertificate(localCertificate);
  50.  
  51. BIO_free_all(boCert);
  52. }
  53.  
  54. if (pkey)
  55. {
  56. BIO *bo = BIO_new( BIO_s_mem() );
  57. PEM_write_bio_PrivateKey(bo, pkey, NULL, (unsigned char*)(password.toStdString().c_str()), password.length(), NULL, (char*)(password.toStdString().c_str()));
  58.  
  59. char *p;
  60. long len = BIO_get_mem_data(bo, &p);
  61.  
  62. QSslKey key(QByteArray::fromRawData(p, len), QSsl::Rsa);
  63. mySslSocket->setPrivateKey(key);
  64. BIO_free_all(bo);
  65. }
  66. free(dataBlob.pbData);
  67. }
  68. }
  69. }
  70.  
  71. if(hSystemStore)
  72. CertCloseStore(hSystemStore, 0);
To copy to clipboard, switch view to plain text mode 

Thanks, new to the forum so didn't knew how to do that