Kann
asked on
code sample: CertGetNameStringW CryptQueryObject
I need a Delphi code sample for this function:
CertGetNameStringW
CryptQueryObject
(Perhaps you can simple translate this C source:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=ut69uoT1BHA.2380%40tkmsftngp04&rnum=2 )
Thank you!
CertGetNameStringW
CryptQueryObject
(Perhaps you can simple translate this C source:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=ut69uoT1BHA.2380%40tkmsftngp04&rnum=2 )
Thank you!
Here's one example of CertGetNameStringW:
program crypttest;
{$APPTYPE CONSOLE}
uses
SysUtils,
JwaWinCrypt; // download from http://delphi-jedi.org or http://members.chello.nl/m.vanbrakel2
procedure DisplayCertContext(CertCon text: PCertContext);
var
CertName: array[0..255] of WideChar;
begin
Writeln;
if CertGetNameStringW(CertCon text, CERT_NAME_EMAIL_TYPE, 0, nil,
CertName, 256) = 0 then
RaiseLastOSError;
Writeln('Subject CERT_NAME_EMAIL_TYPE: ''', CertName, '''');
if CertGetNameStringW(CertCon text, CERT_NAME_SIMPLE_DISPLAY_T YPE, 0, nil,
CertName, 256) = 0 then
RaiseLastOSError;
Writeln('Subject CERT_NAME_SIMPLE_DISPLAY_T YPE: ''', CertName, '''');
if CertGetNameStringW(CertCon text, CERT_NAME_FRIENDLY_DISPLAY _TYPE, 0, nil,
CertName, 256) = 0 then
RaiseLastOSError;
Writeln('Subject CERT_NAME_FRIENDLY_DISPLAY _TYPE: ''', CertName, '''');
if CertGetNameStringW(CertCon text, CERT_NAME_EMAIL_TYPE, CERT_NAME_ISSUER_FLAG, nil,
CertName, 256) = 0 then
RaiseLastOSError;
Writeln('Issuer CERT_NAME_EMAIL_TYPE: ''', CertName, '''');
if CertGetNameStringW(CertCon text, CERT_NAME_SIMPLE_DISPLAY_T YPE, CERT_NAME_ISSUER_FLAG, nil,
CertName, 256) = 0 then
RaiseLastOSError;
Writeln('Issuer CERT_NAME_SIMPLE_DISPLAY_T YPE: ''', CertName, '''');
if CertGetNameStringW(CertCon text, CERT_NAME_FRIENDLY_DISPLAY _TYPE, CERT_NAME_ISSUER_FLAG, nil,
CertName, 256) = 0 then
RaiseLastOSError;
Writeln('Issuer CERT_NAME_FRIENDLY_DISPLAY _TYPE: ''', CertName, '''');
end;
var
StoreName: array[0..255] of Char;
Store: HCERTSTORE;
CertContext: PCertContext;
begin
try
Writeln('Enter certificate store name:');
Readln(StoreName);
if (StoreName <> 'CA') and (StoreName <> 'MY') and (StoreName <> 'ROOT') and (StoreName <> 'SPC') then
raise Exception.Create('Valid certificate store names are: ''CA'', ''MY'', ''ROOT'' and ''SPC''');
Store := CertOpenSystemStore(0, StoreName);
if Store = nil then
RaiseLastOSError;
try
CertContext := CertEnumCertificatesInStor e(Store, nil);
while CertContext <> nil do
begin
DisplayCertContext(CertCon text);
CertContext := CertEnumCertificatesInStor e(Store, CertContext);
end;
finally
CertCloseStore(Store, 0);
end;
except
on E: Exception do
ShowException(E, ExceptAddr);
end;
end.
program crypttest;
{$APPTYPE CONSOLE}
uses
SysUtils,
JwaWinCrypt; // download from http://delphi-jedi.org or http://members.chello.nl/m.vanbrakel2
procedure DisplayCertContext(CertCon
var
CertName: array[0..255] of WideChar;
begin
Writeln;
if CertGetNameStringW(CertCon
CertName, 256) = 0 then
RaiseLastOSError;
Writeln('Subject CERT_NAME_EMAIL_TYPE: ''', CertName, '''');
if CertGetNameStringW(CertCon
CertName, 256) = 0 then
RaiseLastOSError;
Writeln('Subject CERT_NAME_SIMPLE_DISPLAY_T
if CertGetNameStringW(CertCon
CertName, 256) = 0 then
RaiseLastOSError;
Writeln('Subject CERT_NAME_FRIENDLY_DISPLAY
if CertGetNameStringW(CertCon
CertName, 256) = 0 then
RaiseLastOSError;
Writeln('Issuer CERT_NAME_EMAIL_TYPE: ''', CertName, '''');
if CertGetNameStringW(CertCon
CertName, 256) = 0 then
RaiseLastOSError;
Writeln('Issuer CERT_NAME_SIMPLE_DISPLAY_T
if CertGetNameStringW(CertCon
CertName, 256) = 0 then
RaiseLastOSError;
Writeln('Issuer CERT_NAME_FRIENDLY_DISPLAY
end;
var
StoreName: array[0..255] of Char;
Store: HCERTSTORE;
CertContext: PCertContext;
begin
try
Writeln('Enter certificate store name:');
Readln(StoreName);
if (StoreName <> 'CA') and (StoreName <> 'MY') and (StoreName <> 'ROOT') and (StoreName <> 'SPC') then
raise Exception.Create('Valid certificate store names are: ''CA'', ''MY'', ''ROOT'' and ''SPC''');
Store := CertOpenSystemStore(0, StoreName);
if Store = nil then
RaiseLastOSError;
try
CertContext := CertEnumCertificatesInStor
while CertContext <> nil do
begin
DisplayCertContext(CertCon
CertContext := CertEnumCertificatesInStor
end;
finally
CertCloseStore(Store, 0);
end;
except
on E: Exception do
ShowException(E, ExceptAddr);
end;
end.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
TOndrej, thank you! You will get the points.
Perhaps you can help me: I get on error message in crypttest2:
CryptQueryObject() fails allways and the error "system can not find the file" (GetLastError=2147942402).
Perhaps you can help me: I get on error message in crypttest2:
CryptQueryObject() fails allways and the error "system can not find the file" (GetLastError=2147942402).
It seems the system cannot find the file you specified on the command line.
One reason for this could be that the file path contains spaces. (Command line parameters are separated by spaces, so the executable gets in fact several parameters.) If that's your case you'll have to enclose the path in quotes, e.g.
crypttest2 "C:\Program Files\MyDir\bin\MyApp.exe"
That way the OS will pass the whole string including spaces to the executable as one single parameter.
HTH
TOndrej
One reason for this could be that the file path contains spaces. (Command line parameters are separated by spaces, so the executable gets in fact several parameters.) If that's your case you'll have to enclose the path in quotes, e.g.
crypttest2 "C:\Program Files\MyDir\bin\MyApp.exe"
That way the OS will pass the whole string including spaces to the executable as one single parameter.
HTH
TOndrej
ASKER
I inserted the source line:
FileName:='C:\signed.exe';
and
FileName:='"C:\signed.exe" ';
and
FileName:='signed.exe';
but I get allways this error. (The file really exists.)
Do you get this error?
FileName:='C:\signed.exe';
and
FileName:='"C:\signed.exe"
and
FileName:='signed.exe';
but I get allways this error. (The file really exists.)
Do you get this error?
I've never tried the code because I couldn't quickly find a signed executable to check.
Looking at the documentation, it seems that FileName should be a wide string.
Change the declaration of FileName to:
var
FileName: WideString;
and see if it helps.
Looking at the documentation, it seems that FileName should be a wide string.
Change the declaration of FileName to:
var
FileName: WideString;
and see if it helps.
OK, I've finally downloaded some signed executables from Microsoft so I could try out the code. I've found two mistakes in the original posting.
Please make the following two fixes:
1. "File not found"
(As I've suggested before) Change the declaration of FileName to WideString:
var
FileName: WideString;
2. "Unexpected content type"
Change the ContentType check line (replace CERT_QUERY_CONTENT_FLAG_PK CS7_SIGNED _EMBED with CERT_QUERY_CONTENT_PKCS7_S IGNED_EMBE D):
if ContentType <> CERT_QUERY_CONTENT_PKCS7_S IGNED_EMBE D then
raise Exception.Create('Unexpect ed content type');
This should solve the problems.
HTH
TOndrej
Please make the following two fixes:
1. "File not found"
(As I've suggested before) Change the declaration of FileName to WideString:
var
FileName: WideString;
2. "Unexpected content type"
Change the ContentType check line (replace CERT_QUERY_CONTENT_FLAG_PK
if ContentType <> CERT_QUERY_CONTENT_PKCS7_S
raise Exception.Create('Unexpect
This should solve the problems.
HTH
TOndrej
ASKER
Thank you!
Glad to help.
http://codecentral.borland.com/codecentral/ccweb.exe/listing?id=17597
Regards
//raidos