Windows Certitificate Store C++ Code samples

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Windows Certitificate Store C++ Code samples

Ron Teitelbaum
Suman,

Well I'm still fighting with it trying to get it to work, but sure I would
be happy to share with you.  

I've been researching code samples from the API documentation.  This is what
I found.  The code samples don't work as written there were header files and
libs missing.  The following setup works.

Header:
#include <stdio.h>
#include <windows.h>
#include <wincrypt.h>
#include <cryptuiapi.h>
#include <tchar.h>

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

#define MY_ENCODING_TYPE  (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)

void MyHandleError(char *s);

void main(void)
{

} // End of main.

void MyHandleError(char *s)
{
printf("An error occurred in running the program.\n");
printf("%s\n",s);
printf("Error number %x\n.",GetLastError());
printf("Program terminating.\n");
exit(1);
}


Then you can use this to:

Open Certificate store  This is the default user certificate store.

if(hCertStore = CertOpenStore(
        CERT_STORE_PROV_SYSTEM, // System store will be a
                // virtual store
        0,      // Encoding type not needed
                 // with this PROV
        NULL,  // Accept the default HCRYPTPROV
        CERT_SYSTEM_STORE_CURRENT_USER,
                // Set the system store location in the
                // registry
        L"MY"))  // Could have used other predefined
       
                // system stores
                // including Trust, CA, or Root

{
        fprintf(stderr,"The store has been opened. \n");
}

You can open select dialog by:

if(!(pCertContext = CryptUIDlgSelectCertificateFromStore(
  hCertStore,
  NULL,
  NULL,
  NULL,
  CRYPTUI_SELECT_LOCATION_COLUMN,
  0,
  NULL)))
{
    MyHandleError("Select UI failed." );
}

Then in your code you should clean up by doing the following:

Clean up
CertFreeCertificateContext(pCertContext);
CertCloseStore(hCertStore,0);
printf("The function completed successfully. \n");

Ultamately you can to access your private key by:

CryptExportKey
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/seccrypto/s
ecurity/cryptexportkey.asp

this is how a private key exported with CryptoExportKey is structured: the
numbers are encoded LSB first.
privatekey:
07 PUBLICKEYSTRUC.bType = PRIVATEKEYBLOB
02 PUBLICKEYSTRUC.bVersion
0000 PUBLICKEYSTRUC.reserved
00002400 PUBLICKEYSTRUC.aiKeyAlg = CALG_RSA_SIGN
"RSA2" RSAPUBKEY.magic
00000400 RSAPUBKEY.bitlen
00010001 RSAPUBKEY.pubexp
publicmodulus ( = prime1 * prime2 )
29 f9 be 03 f9 d6 b2 d2 62 29 dc 81 5d 0d 19 18
53 1d 35 7d 2a 6a 23 9a ec 4d 3f 08 47 8e aa a9
dd de c8 dd 87 fc 67 46 0b d8 2e d4 1a 0a d9 9e
e8 6b 16 2a 66 6e 46 60 47 bf 22 03 9a 62 d8 e5
d8 1e f7 74 f6 b3 dd 0e 70 26 20 d8 76 41 b5 70
df b5 21 79 ee 98 ee 88 43 1d af f7 ea f0 87 74
a3 e1 f5 dc c3 33 57 ed f7 6c 45 a1 e5 ff 46 bf
8d 2f 2c 1d 30 1c fe 10 6b da 12 db 07 ab a4 ca prime1
f3 53 4b c9 60 29 bb 12 e7 46 23 f0 68 7e d5 58
cf 9b 41 97 5b 08 e4 51 d4 32 31 80 93 00 ce cd
43 db f6 e5 ed 9c 48 06 d8 64 8b 17 32 2d 0e 52
c8 12 00 aa 6e d9 53 cf 84 ce 52 75 e0 ee 3b f4
prime2
73 71 3e 76 7c 47 6f aa 04 76 1e 59 8b 26 5e 70
7a fc 1b f7 30 11 eb 07 73 86 0e d1 fe 52 ab c6
60 6a b8 bb ed 66 91 16 ba 63 7a f2 e7 67 20 7c
c8 cc 02 ed 44 63 38 ac 41 35 2a 9b 97 ce 67 d4
exponent1 ( = d mod (prime1 - 1) )
97 13 50 ea 36 38 46 9e 28 3f b7 d4 54 e6 92 58
b5 dc 04 2e ba 16 da d5 b5 ac c9 6d 2c 4c 00 0e
cf 3a a7 9c 4f 8f 9d 1d 7c 03 25 ec 0e 60 69 e6
a7 99 fd 34 37 4a 16 93 b5 cb 08 57 92 a1 74 d8
exponent2 ( = d mod (prime2 - 1) )
7f 5b 63 19 48 f8 15 1a 5e 97 e5 df e4 f4 2a b7
9a 5b 22 c0 1e 77 5d c6 b5 0d 68 5d b2 5d 45 98
38 3b 4c 8c 4c 95 a1 71 54 fc ad e4 94 11 fb b9
7d a8 7e dd a8 8a be e1 79 9d 1a f6 b8 5e a5 c5
coefficient (such that : coefficient*prime2 (mod prime1)==1 )
bb ba cd 52 15 d7 3a 40 9e 02 8d ad da 6e 70 49
9b b6 4f 7f ba f7 cb e6 73 0b 06 0a db e0 e9 53
0c a0 ba 06 94 ff a2 83 7e 7e 55 a2 0f d2 fb 33
e3 eb 6a 1d 6f 05 c3 4f 8e d9 5a 12 f8 d8 48 a1
private exponent ( d )
e9 da d0 33 9c c7 54 1e 60 aa 26 44 0d 8d ad 25
ad fc 49 66 90 1c 3a cc 61 ea 46 67 67 4b 32 14
b3 04 49 1c 9d 0a 03 1d 35 0f d5 9d 57 8c 27 78
de 4c 7c 2b 0a 47 5c 68 e3 8b b3 3b b6 25 3f 2d
85 11 90 b4 a6 30 52 b2 a7 31 26 0b 13 9d 34 17
c0 93 34 53 5d c1 7d b4 99 38 1b b2 2c 2e 07 19
fd 9c 44 ac 38 2d 75 ab f9 52 b0 15 57 d9 9d 2b
b0 15 b4 e5 59 e4 15 02 38 f6 f0 ca 60 a4 31 08

I hope that helps, good luck on your project.

Ron Teitelbaum
President / Principal Software Engineer
US Medical Record Specialists
Squeak Cryptography Team Leader

> -----Original Message-----
> From: Suman Karumuri [mailto:[hidden email]]
> Sent: Tuesday, August 01, 2006 9:41 PM
> To: [hidden email]
> Subject: Re: Suggestions for C++ or DLL code
>
> Hi Ron,
>
> I am currently working on an engine for openssl, that can talk to the
> windows certificate store. So, is there any chance that you can share
> the code that you have written to access the windows certificate store
> from your program? If not the whole code base, even relevent snippets
> would ease writng my patch.
>
> Please note that this code will be opensourced with due credits.
>
> Thanks for your time,
> -Suman
>
> On 7/30/06, Ron Teitelbaum <[hidden email]> wrote:
> >
> >
> >
> >
> > Hello all,
> >
> >
> >
> > I'm currently working on a TLS / SSL implementation and on windows I am
> > planning to support the Certificate Store that is built in.  This
> requires
> > me to use the CryptoAPI dlls.  The code is complex and it uses a number
> of
> > header files and many steps.  I built some C++ code that does what I'm
> > looking for so now I would like to incorporate this into Squeak.
> >
> >
> >
> > I'd assumed that FFI was like DDL/C connect in VW but that's not what
> I'm
> > seeing.  So my question is: What is the best way to get this working?
> > Should I hack away at FFI and support the api directly, and is that even
> > possible?  Should I create DLL's to call and use them with FFI, or
> should I
> > compile some sort of plugins?  My preference would be to call the api
> > directly without having to create dll's, duplicating my C++ code in
> > Smalltalk if possible.
> >
> >
> >
> > Some direction and comparisons of different options would be very
> helpful.
> >
> >
> >
> > Thank you for your help!
> >
> >
> >
> > Ron Teitelbaum
> >
> >
> >
> >


_______________________________________________
Cryptography mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/cryptography