Pessoal,
O CNPJ está salvo como extensão do certificado.
E está codificado na forma ASN.1.
O OID do CNPJ é 2.16.76.1.3.3.
Segue o exemplo de como recuperar o CNPJ do certificado digital:
Código:
TfmrOID = class(TForm)
edt1: TEdit;
edt2: TEdit;
edt3: TEdit;
edt4: TEdit;
lbl1: TLabel;
lbl2: TLabel;
lbl3: TLabel;
lbl4: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
...
const
CAPICOM_CURRENT_USER_STORE = $00000002;
CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED = $00000002;
CLASS_Store: TGUID = '{91D221C4-0CD4-461C-A728-01D509321556}';
//Converte Cada 2 bytes para Char
function ASN1Decode(Value: String): String;
begin
Result := '';
Delete(Value, 1, 4);
repeat
Result := Result + Char(StrToIntDef('$'+Value[1]+Value[2], 0));
Delete(Value, 1, 2);
until Value = '';
end;
procedure TfmrOID.Button1Click(Sender: TObject);
var
Store : OleVariant; //IStore3;
Certs : OleVariant; //ICertificates2;
Certs2 : OleVariant; //ICertificates2;
Cert : OleVariant; //ICertificate2;
Extensions : OleVariant; //IExtensions
Extension : OleVariant;
OID : String;
SubjectName : TStrings;
otherName : TStrings;
I : Integer;
begin
Try
SubjectName := TStringList.Create;
otherName := TStringList.Create;
Store := CreateComObject(CLASS_Store) as IDispatch;
if VarIsEmpty(Store) then
Exit;
Store.Open(CAPICOM_CURRENT_USER_STORE, 'My', CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED);
Certs := Store.Certificates;
Certs2 := Certs.Select('Certificado(s) Digital(is) disponível(is)', 'Selecione o Certificado Digital para uso no aplicativo', false);
if not(Certs2.Count = 0) then
begin
Cert := Certs2.Item[1];
//Serial do Certificado
edt1.Text := Cert.SerialNumber;
//Data de Validade
edt4.Text := Cert.ValidToDate;
//Razão Social
SubjectName.Text := StringReplace(Cert.SubjectName, ', ', #$D#$A, [rfReplaceAll]);
edt3.Text := SubjectName.Values['CN'];
//CNPJ
edt2.Text := '';
Extensions := Cert.Extensions;
For i := 1 to Extensions.Count do
begin
Extension := Extensions.Item[i];
OID := Extension.OID.Value;
if (OID = '2.5.29.17') or (OID = '2.5.29.7') then
begin
otherName.Text := StringReplace(Extension.EncodedData.Format(true), ' ', '', [rfReplaceAll]);
edt2.Text := ASN1Decode(otherName.Values['2.16.76.1.3.3']);
end;
end;
end;
finally
FreeAndNil(otherName);
FreeAndNil(SubjectName);
Cert := null;
Certs2:= null;
Certs := null;
Store := null;
end;
end;
Para fazer download do exemplo completo, acesse o link:
http://www.mediafire.com/file/imzmv1zmnoz/OID.rar
Att,
Luiz Vaz