FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Acceso al almacen de certificados de Windows
Posts: 282
Joined: Mon Oct 10, 2005 08:55 AM
Acceso al almacen de certificados de Windows
Posted: Tue Dec 26, 2017 02:27 PM

Hola colegas,
Necesito acceder al almac茅n de certificados de Windows y seleccionar un certificado de entre los existentes.
驴Es posible hacer esto desde nuestro entorno de programaci贸n via [X]Harbour/FW?
Se que desde Java se puede pero desconozco el c贸digo necesario para conseguirlo.
Si alguien puede ofrecer una rutina desde este u otro lenguaje de programaci贸n ser铆a estupendo.
Gracias :shock:

Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: Acceso al almacen de certificados de Windows
Posted: Tue Dec 26, 2017 03:13 PM

Puedes ver este art铆culo a ver si nos da luz en el tema

http://www.sysadmit.com/2017/10/windows ... cados.html

Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noci贸n del tiempo

El secreto de la felicidad no est谩 en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 282
Joined: Mon Oct 10, 2005 08:55 AM
Re: Acceso al almacen de certificados de Windows
Posted: Tue Dec 26, 2017 04:21 PM
Cristobal,
de nuevo gracias por tu informaci贸n, valiosa como siempre, desde aqu铆:
Code (fw): Select all Collapse
%APPDATA%\Microsoft\SystemCertificates\My\Certificates
ya se puede seleccionar el certificado jugando con la huella digital y el alias del certificado, un pasito mas.... y ya podremos usarlo para firmar. Investigar茅 en esta direcci贸n y si consigo armar una peque帽a utilidad la pondr茅 aqu铆 por si le interesa a alguien mas. :-)
Posts: 17
Joined: Tue Oct 06, 2015 07:06 AM
Re: Acceso al almacen de certificados de Windows
Posted: Wed Dec 27, 2017 08:38 AM
Buenas, por si te sirve ayuda, te paso un c贸digo que muestra el di谩logo de selecci贸n de certificados de Windows y te devuelve el seleccionado:

Code (fw): Select all Collapse
#include "fivewin.ch"


func main()


聽 聽MsgInfo("Seleccionado certificado: "+ToTxt(SELCERT()), "CryptoApi")



return nil

//*****************************************************************************
//*****************************************************************************

function ToTxt( uVal )

聽 聽local cType := ValType( uVal )

聽 聽do case
聽 聽 聽 case cType == "C" .or. cType == "M"
聽 聽 聽 聽 聽 聽return uVal

聽 聽 聽 case cType == "D"
聽 聽 聽 聽 聽 聽return DToC( uVal )

聽 聽 聽 case cType == "L"
聽 聽 聽 聽 聽 聽return If( uVal, ".T.", ".F." )

聽 聽 聽 case cType == "N"
聽 聽 聽 聽 聽 聽return AllTrim( Str( uVal ) )

聽 聽 聽 case cType == "B"
聽 聽 聽 聽 聽 聽return "{|| ... }"

聽 聽 聽 case cType == "A"
聽 聽 聽 聽 聽 聽return "{ ... }"

聽 聽 聽 case cType == "O"
聽 聽 聽 聽 聽 聽return If( __ObjHasData( uVal, "cClassName" ), uVal:cClassName, uVal:ClassName() )

聽 聽 聽 case cType == "H"
聽 聽 聽 聽 聽 聽return "{=>}"

聽 聽 聽 otherwise
聽 聽 聽 聽 聽 聽return ""
聽 聽endcase

return nil

//*****************************************************************************
//*****************************************************************************

#pragma BEGINDUMP

#include <windows.h>
//#include <psapi.h>
//#include <hbapi.h>
//#include <hbapierr.h>
//#include <hbapiitm.h>
//#include <wincrypt.h>

#define CRYPTUI_SELECT_LOCATION_COLUMN 0x000000010

//Definir el prototipo de las funciones:
typedef HCERTSTORE (WINAPI * PTYPECERTOPEN) (HCRYPTPROV, LPTSTR);
typedef PCCERT_CONTEXT (WINAPI * PTYPECERTSELECTDLG) (HCERTSTORE, HWND, LPCWSTR, LPCWSTR, DWORD, DWORD, void*);
typedef PCCERT_CONTEXT (WINAPI * PTYPECERTENUM) (HCERTSTORE, PCCERT_CONTEXT);
typedef DWORD (WINAPI * PTYPECERTGETNAME) (PCCERT_CONTEXT, DWORD, DWORD, VOID*, LPTSTR, DWORD);
typedef DWORD (WINAPI * PTYPECERTNAMETOSTR) (DWORD, PCERT_NAME_BLOB, DWORD, LPTSTR, DWORD);
typedef BOOL (WINAPI * PTYPECERTFREECC) (PCCERT_CONTEXT);
typedef BOOL (WINAPI * PTYPECERTCLOSESTORE) (HCERTSTORE, DWORD);

HB_FUNC(SELCERT)
{

聽 聽// Hay varios ejemplos en: <!-- m --><a class="postlink" href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa382361(v=vs.85">https://msdn.microsoft.com/en-us/librar ... 61(v=vs.85</a><!-- m -->).aspx

聽 聽HCERTSTORE hStore;
聽 聽PCCERT_CONTEXT PrevContext, CurContext;
聽 聽PCHAR sNombre;
聽 聽DWORD cbSize;
聽 聽PHB_ITEM pArray;
聽 聽PHB_ITEM pItem;
聽 聽PCCERT_CONTEXT 聽 pCertContext;
聽 聽// Cargamos las librer铆as de las que queremos la direcci贸n de las funciones.
聽 聽HMODULE HCrypt = LoadLibrary("Crypt32.dll");
聽 聽HMODULE HCrypt2 = LoadLibrary("Cryptui.dll");

聽 聽// Declaramos el tipo de puntero a la funci贸n, tenemos la definici贸n arriba.
聽 聽PTYPECERTOPEN 聽 聽pCertOpen;
聽 聽PTYPECERTSELECTDLG 聽 聽pCertSelectDlg;
聽 聽PTYPECERTGETNAME pCertGetName;
聽 聽PTYPECERTNAMETOSTR pCertNameToStr;
聽 聽PTYPECERTFREECC pCertFreeCC;
聽 聽PTYPECERTCLOSESTORE pCertCloseStore;


聽 聽if (HCrypt != NULL && HCrypt2 != NULL){
聽 聽 聽 //Sacamos el puntero todas las funciones que vamos a usar mediante GetProcAddress:
聽 聽 聽 #ifdef UNICODE
聽 聽 聽 聽 聽pCertOpen 聽 聽= (PTYPECERTOPEN) GetProcAddress(HCrypt, "CertOpenSystemStoreW");
聽 聽 聽 聽 聽pCertGetName = (PTYPECERTGETNAME) GetProcAddress(HCrypt, "CertGetNameStringW");
聽 聽 聽 #else
聽 聽 聽 聽 聽pCertOpen 聽 聽= (PTYPECERTOPEN) GetProcAddress(HCrypt, "CertOpenSystemStoreA");
聽 聽 聽 聽 聽pCertGetName = (PTYPECERTGETNAME) GetProcAddress(HCrypt, "CertGetNameStringA");
聽 聽 聽 #endif
聽 聽 聽 pCertSelectDlg = (PTYPECERTSELECTDLG) GetProcAddress(HCrypt2, "CryptUIDlgSelectCertificateFromStore");
聽 聽 聽 pCertFreeCC 聽= (PTYPECERTFREECC) GetProcAddress(HCrypt, "CertFreeCertificateContext");
聽 聽 聽 pCertCloseStore 聽= (PTYPECERTCLOSESTORE) GetProcAddress(HCrypt, "CertCloseStore");
聽 聽}

聽 聽if (pCertOpen){
聽 聽 聽 // Llamada a CertOpenSystemStore:
聽 聽 聽 hStore = pCertOpen(NULL, TEXT("MY"));
聽 聽}

聽 聽if (hStore){
聽 聽 聽 // Di谩logo de selecci贸n de certificado:
聽 聽 聽 pCertContext = pCertSelectDlg(hStore, NULL, NULL, NULL, CRYPTUI_SELECT_LOCATION_COLUMN, 0, NULL);

聽 聽 聽 if (pCertContext){
聽 聽 聽 聽 聽cbSize = pCertGetName(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, NULL, 0);
聽 聽 聽 聽 聽if (cbSize>0) {
聽 聽 聽 聽 聽 聽 //Reservamos la memoria que necesitamos para el texto que recibiremos
聽 聽 聽 聽 聽 聽 sNombre = (LPTSTR)malloc(cbSize * sizeof(TCHAR));

聽 聽 聽 聽 聽 聽 pCertGetName(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, sNombre, cbSize);

聽 聽 聽 聽 聽 聽 // Llamada a CertFreeCertificateContext:
聽 聽 聽 聽 聽 聽 pCertFreeCC(pCertContext);
聽 聽 聽 聽 聽}
聽 聽 聽 }

聽 聽 聽 // Cerrar el almacen de certificados:
聽 聽 聽 // Llamada a CertCloseStore:
聽 聽 聽 pCertCloseStore(hStore, 0);
聽 聽 }

聽 聽 FreeLibrary(HCrypt);
聽 聽 FreeLibrary(HCrypt2);

聽 聽 hb_retc(sNombre);

}

#pragma ENDDUMP


Un saludo.
Posts: 282
Joined: Mon Oct 10, 2005 08:55 AM
Re: Acceso al almacen de certificados de Windows
Posted: Wed Dec 27, 2017 02:09 PM

Muchisimas gracias Dario, esto me va a ahorrar mucho trabajo, funciona perfecto. :shock:

Posts: 728
Joined: Fri Oct 07, 2005 07:38 AM
Re: Acceso al almacen de certificados de Windows
Posted: Thu Dec 28, 2017 05:21 PM

Interesante!

Angel Salom
Visionwin Software - https://www.visionwin.com
------------------------------------------------------------
fwh 19.05 - harbour 3.2 - bcc 7.4

Continue the discussion