failed do access certificate store "Windows-MY"

199 views
Skip to first unread message

Franklin Roque

unread,
Sep 3, 2014, 4:26:36 PM9/3/14
to jna-...@googlegroups.com
Hi all,

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.

My Assinador.java:

import com.sun.jna.Library;
import com.sun.jna.Native;

public class Assinador  {
   
    public interface Add extends Library
    {     
        void listCertificate(String storeName);
    }

    public static void main(String[] args) {
        Add lib = (Add) Native.loadLibrary("libCppDynamicLibrary_1", Add.class);
        lib.listCertificate("MY");
    }
}

My signAll.h

#ifndef SIGNALL_H
#define SIGNALL_H

#include <stdio.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <windows.h>
#include <wincrypt.h>
#include <cryptuiapi.h>
#include <openssl/sha.h>
#include <string>
#include <wchar.h>

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



extern "C" __declspec(dllexport) void listCertificate(const char* storeName);
void CheckError(BOOL condition, wchar_t * message);

#endif /* SIGNALL_H */


My signAll.cpp


#include "signAll.h"

using namespace std;
....
__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);
     
}

}

Timothy Wall

unread,
Sep 4, 2014, 7:33:27 AM9/4/14
to jna-...@googlegroups.com
Maybe you should make your native code return data instead of emitting it to stderr? The native output streams could wind up in a variety of places depending on your terminal and native program settings. stdout/stderr are not in any way linked to the Java System.out/System.err streams, and it’d be something of a pain to tie them together.

BTW, you should prefer stdout for normal program output and stderr for program errors/warnings.
> --
> You received this message because you are subscribed to the Google Groups "Java Native Access" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages