Validar pin de certificado digital com wincrypt - Delphi

13/04/2022

0

Estou com dificuldade em localizar a função na biblioteca wincrypt para autenticar o pin do usuario referente ao certificado digital, tem como alguem ajudar. Segue código.

function GetCertificado: boolean;
var
  hStore: HCERTSTORE;
  ACertContext: PCCERT_CONTEXT;
  pProvKey: PCRYPT_KEY_PROV_INFO;
  dwProvKeyInfoSize, dwKeySpec, pcbSignature: DWORD;
  certString : string;
  pfCallerFreeProv: LongBool;
  ProviderOrKeyHandle: HCRYPTPROV_OR_NCRYPT_KEY_HANDLE;
  mCryptProvider: HCRYPTPROV;
  pSignatureAlgorithm: PCRYPT_ALGORITHM_IDENTIFIER;
  n : Pointer;
  pbSignature : PByte;

begin
  Result := False;
  hStore := nil;

  try
    hStore := CertOpenSystemStore(0, 'My');

    ACertContext := CryptUIDlgSelectCertificateFromStore(hStore, 0,
      'Selecione um Certificado',
      'Selecione o Certificado que deseja utilizar:',
      CRYPTUI_SELECT_LOCATION_COLUMN or CRYPTUI_SELECT_ISSUEDBY_COLUMN or
      CRYPTUI_SELECT_INTENDEDUSE_COLUMN, 0, Nil);

    if (Assigned(ACertContext)) and (ACertContext <> nil) and (GetCertIsHardware(ACertContext)) then
    begin
      certString := CertToDERBase64(ACertContext);
      MsgAdverte('CERTIFICADO DIGITAL SELECIONADO = ' + #13 + certString);

      if CryptAcquireCertificatePrivateKey( ACertContext,
                                            CRYPT_ACQUIRE_ALLOW_NCRYPT_KEY_FLAG,
                                            Nil,
                                            ProviderOrKeyHandle,
                                            dwKeySpec,
                                            pfCallerFreeProv) then
      begin
        MsgAdverte
          ('Certificado foi selecionado. Certificado tem chave privada. Drivers do certificados encontrados');

        if not GetCNGProviderIsHardware(ProviderOrKeyHandle) then
          Exit;

        MsgAdverte
          ('provedor do serviços de criptografia recuperado com sucesso ' + ProviderOrKeyHandle.ToString);

         pProvKey := nil;
         dwProvKeyInfoSize := 0;

         if not CertGetCertificateContextProperty(ACertContext, CERT_KEY_PROV_INFO_PROP_ID, pProvKey, dwProvKeyInfoSize) then
           MsgErro('CertGetCertificateContextProperty: erro')
         else
           MsgAdverte('CertGetCertificateContextProperty com sucesso ');

         GetMem(pProvKey, dwProvKeyInfoSize);

         if not CryptAcquireContext( mCryptProvider, Nil, Nil, PROV_RSA_AES, CRYPT_VERIFYCONTEXT) then
           MsgErro('CryptAcquireContext: provedor inválido')
         else
           MsgAdverte('CryptAcquireContext com sucesso ' + mCryptProvider.ToString);

         //???? como validar o pin do certtificado digital selecionado acima?
      end
      else
        RegUsuario.Mensagem := 'Drivers do certificado não encontrados';
    end
    else
    begin
      RegUsuario.Mensagem := 'Operação cancelada pelo usuário';
    end;

  finally
    CertCloseStore(hStore, 0);
  end;
end;


Marcos Barbosa

Marcos Barbosa

Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar