I try access Certificate Store "Windows-MY" using JNA. When I run using C++ exec, it' run. But in java, it's waiting... and don't finish.
__declspec(dllexport) void listCertificate(const char* storeName){
//-------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// This program lists all of the certificates in a system certificate
// store and all of the property identifier numbers of those
// certificates. It also demonstrates the use of two
// UI functions. One, CryptUIDlgSelectCertificateFromStore,
// displays the certificates in a store
// and allows the user to select one of them,
// The other, CryptUIDlgViewContext,
// displays the contents of a single certificate.
// Zero must be used on the first
// call to the function. After that,
// the last returned property identifier is passed.
//-------------------------------------------------------------------
// Begin processing and Get the name of the system certificate store
// to be enumerated. Output here is to stderr so that the program
// can be run from the command line and stdout can be redirected
// to a file.
//-------------------------------------------------------------------
// Open a system certificate store.
//storeName = "MY";
strcpy(pszStoreName,storeName);
if ( hCertStore = CertOpenSystemStore(
NULL,
pszStoreName))
{
//fprintf(stderr,"The %s store has been opened. \n", pszStoreName);
}
else
{
// If the store was not opened, exit to an error routine.
MyHandleError("The store was not opened.");
}
//-------------------------------------------------------------------
// Use CertEnumCertificatesInStore to get the certificates
// from the open store. pCertContext must be reset to
// NULL to retrieve the first certificate in the store.
while(pCertContext = CertEnumCertificatesInStore(
hCertStore,
pCertContext))
{
int i=0, length =0;
LPTSTR pszString;
LPTSTR pszName;
DWORD cbSize;
BYTE * cbOut;
DWORD * pcbOut;
CERT_BLOB blobEncodedName;
char * str;
const BYTE * teste = (const BYTE *) pCertContext->pbCertEncoded;
// -----------------------------------------------------------
// Get the fingerprint of certificate
CryptHashCertificate(0, CALG_SHA1, 0, pCertContext->pbCertEncoded,
pCertContext->cbCertEncoded, 0, &cbSize);
cbOut = (BYTE *) malloc(cbSize * sizeof(BYTE));
str = (char *) malloc(cbSize * sizeof(char));
CryptHashCertificate(0, CALG_SHA1, 0, pCertContext->pbCertEncoded,
pCertContext->cbCertEncoded, cbOut,
&cbSize);
for(i=0; i< cbSize; i++){
length += sprintf(str + length , "%02X",cbOut[i]);
}
fprintf(stderr, "\nFingerprint -> %s\n", str);
//-----------------------------------------------------------
// Get and display
// the name of subject of the certificate.
if(!(cbSize = CertGetNameString(
pCertContext,
CERT_NAME_SIMPLE_DISPLAY_TYPE,
0,
NULL,
NULL,
0)))
{
MyHandleError(TEXT("CertGetName 1 failed."));
}
if(!(pszName = (LPTSTR)malloc(cbSize * sizeof(TCHAR))))
{
MyHandleError(TEXT("Memory allocation failed."));
}
if(CertGetNameString(
pCertContext,
CERT_NAME_SIMPLE_DISPLAY_TYPE,
0,
NULL,
pszName,
cbSize))
{
fprintf(stderr, "Subject -> %s.\n", pszName);
//-------------------------------------------------------
// Free the memory allocated for the string.
free(pszName);
}
else
{
MyHandleError(TEXT("CertGetName failed."));
}
//-----------------------------------------------------------
// Get and display
// the name of Issuer of the certificate.
if(!(cbSize = CertGetNameString(
pCertContext,
CERT_NAME_SIMPLE_DISPLAY_TYPE,
CERT_NAME_ISSUER_FLAG,
NULL,
NULL,
0)))
{
MyHandleError(TEXT("CertGetName 1 failed."));
}
if(!(pszName = (LPTSTR)malloc(cbSize * sizeof(TCHAR))))
{
MyHandleError(TEXT("Memory allocation failed."));
}
if(CertGetNameString(
pCertContext,
CERT_NAME_SIMPLE_DISPLAY_TYPE,
CERT_NAME_ISSUER_FLAG,
NULL,
pszName,
cbSize))
{
fprintf(stderr, "Issuer -> %s.\n", pszName);
//-------------------------------------------------------
// Free the memory allocated for the string.
free(pszName);
}
else
{
MyHandleError(TEXT("CertGetName failed."));
}
} // End outer while.
//-------------------------------------------------------------------
// Select a new certificate by using the user interface.
if(!(pCertContext = CryptUIDlgSelectCertificateFromStore(
hCertStore,
NULL,
NULL,
NULL,
CRYPTUI_SELECT_LOCATION_COLUMN,
0,
NULL)))
{
MyHandleError("Select UI failed." );
} else {
int i=0, length =0;
LPTSTR pszString;
LPTSTR pszName;
DWORD cbSize;
BYTE * cbOut;
DWORD * pcbOut;
CERT_BLOB blobEncodedName;
char * str;
const BYTE * teste = (const BYTE *) pCertContext->pbCertEncoded;
CryptHashCertificate(0, CALG_SHA1, 0, pCertContext->pbCertEncoded,
pCertContext->cbCertEncoded, 0, &cbSize);
cbOut = (BYTE *) malloc(cbSize * sizeof(BYTE));
str = (char *) malloc(cbSize * sizeof(char));
CryptHashCertificate(0, CALG_SHA1, 0, pCertContext->pbCertEncoded,
pCertContext->cbCertEncoded, cbOut,
&cbSize);
for(i=0; i< cbSize; i++){
length += sprintf(str + length , "%02X",cbOut[i]);
}
fprintf(stderr, "\nFingerprint -> %s\n", str);
}
}