Skip to content

Instantly share code, notes, and snippets.

@aragaer
Created September 30, 2011 04:20
Show Gist options
  • Select an option

  • Save aragaer/1252640 to your computer and use it in GitHub Desktop.

Select an option

Save aragaer/1252640 to your computer and use it in GitHub Desktop.
#define WIN32_LEAN_AND_MEAN
#define SECURITY_WIN32
#include <Windows.h>
#include <WinCrypt.h>
#include <Security.h>
#include <stdio.h>
#pragma comment(lib, "crypt32")
#define skip(...) do { printf(__VA_ARGS__); goto out; } while (0)
#define ok(cond, ...) if (!(cond)) skip(__VA_ARGS__)
static const BYTE selfSignedCert[] = {
0x30, 0x82, 0x01, 0x1f, 0x30, 0x81, 0xce, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02,
0x10, 0xeb, 0x0d, 0x57, 0x2a, 0x9c, 0x09, 0xba, 0xa4, 0x4a, 0xb7, 0x25, 0x49,
0xd9, 0x3e, 0xb5, 0x73, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1d,
0x05, 0x00, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03,
0x13, 0x0a, 0x4a, 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x30,
0x1e, 0x17, 0x0d, 0x30, 0x36, 0x30, 0x36, 0x32, 0x39, 0x30, 0x35, 0x30, 0x30,
0x34, 0x36, 0x5a, 0x17, 0x0d, 0x30, 0x37, 0x30, 0x36, 0x32, 0x39, 0x31, 0x31,
0x30, 0x30, 0x34, 0x36, 0x5a, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03,
0x55, 0x04, 0x03, 0x13, 0x0a, 0x4a, 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e,
0x67, 0x00, 0x30, 0x5c, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x4b, 0x00, 0x30, 0x48, 0x02, 0x41,
0x00, 0xe2, 0x54, 0x3a, 0xa7, 0x83, 0xb1, 0x27, 0x14, 0x3e, 0x59, 0xbb, 0xb4,
0x53, 0xe6, 0x1f, 0xe7, 0x5d, 0xf1, 0x21, 0x68, 0xad, 0x85, 0x53, 0xdb, 0x6b,
0x1e, 0xeb, 0x65, 0x97, 0x03, 0x86, 0x60, 0xde, 0xf3, 0x6c, 0x38, 0x75, 0xe0,
0x4c, 0x61, 0xbb, 0xbc, 0x62, 0x17, 0xa9, 0xcd, 0x79, 0x3f, 0x21, 0x4e, 0x96,
0xcb, 0x0e, 0xdc, 0x61, 0x94, 0x30, 0x18, 0x10, 0x6b, 0xd0, 0x1c, 0x10, 0x79,
0x02, 0x03, 0x01, 0x00, 0x01, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02,
0x1d, 0x05, 0x00, 0x03, 0x41, 0x00, 0x25, 0x90, 0x53, 0x34, 0xd9, 0x56, 0x41,
0x5e, 0xdb, 0x7e, 0x01, 0x36, 0xec, 0x27, 0x61, 0x5e, 0xb7, 0x4d, 0x90, 0x66,
0xa2, 0xe1, 0x9d, 0x58, 0x76, 0xd4, 0x9c, 0xba, 0x2c, 0x84, 0xc6, 0x83, 0x7a,
0x22, 0x0d, 0x03, 0x69, 0x32, 0x1a, 0x6d, 0xcb, 0x0c, 0x15, 0xb3, 0x6b, 0xc7,
0x0a, 0x8c, 0xb4, 0x5c, 0x34, 0x78, 0xe0, 0x3c, 0x9c, 0xe9, 0xf3, 0x30, 0x9f,
0xa8, 0x76, 0x57, 0x92, 0x36 };
int main() {
int ret;
PCCERT_CONTEXT cert, cert2 = NULL;
HCERTSTORE store, coll;
printf("Open collection store.\n");
coll = CertOpenStore(CERT_STORE_PROV_COLLECTION, X509_ASN_ENCODING, NULL, 0, NULL);
printf("Done. Collection store = %p\n\n", coll);
printf("Open memory store.\n");
store = CertOpenStore(CERT_STORE_PROV_MEMORY, X509_ASN_ENCODING, NULL, 0, NULL);
printf("Done. Memory Store = %p\n\n", store);
printf("Create certificate.\n");
cert = CertCreateCertificateContext(X509_ASN_ENCODING, selfSignedCert, sizeof(selfSignedCert));
printf("Done. Cert = %p\n\n", cert);
printf("Add cert %p to memstore %p\n", cert, store);
ret = CertAddCertificateContextToStore(store, cert, CERT_STORE_ADD_ALWAYS, &cert2);
ok(ret, "Failed to add to store: %x\n", GetLastError());
printf("Cert %p added to store %p and is now %p\n\n", cert, store, cert2);
printf("Add store %p to collection %p\n", store, coll);
ret = CertAddStoreToCollection(coll, store, 0, 0);
ok(ret, "Failed to add to collection: %x\n", GetLastError());
printf("Added store %p to collection %p\n\n", store, coll);
printf("Free cert %p\n", cert);
ret = CertFreeCertificateContext(cert);
cert = NULL;
ok(ret, "Failed to free context: %x\n", GetLastError());
printf("Done, cert freed\n\n");
printf("Free cert %p\n", cert2);
ret = CertFreeCertificateContext(cert2);
cert2 = NULL;
ok(ret, "Failed to free link context: %x\n", GetLastError());
printf("Done, cert2 freed\n\n");
do {
printf("getting cert from store %p, prev = %p\n", store, cert);
cert = CertEnumCertificatesInStore(store, cert);
if (cert)
printf("in store: cert %p, store %p\n\n", cert, cert->hCertStore);
else
printf("Done, nothing else here\n\n");
} while (cert);
do {
printf("getting cert from store %p, prev = %p\n", coll, cert);
cert = CertEnumCertificatesInStore(coll, cert);
if (cert)
printf("in store: cert %p, store %p\n\n", cert, cert->hCertStore);
else
printf("Done, nothing else here\n\n");
} while (cert);
printf("Closing store %p\n", store);
CertCloseStore(store, 0);
printf("Done, store closed\n\n");
printf("Closing collection %p\n", coll);
CertCloseStore(coll, 0);
printf("Done, collection closed\n\n");
printf("Exiting now...\n");
out:
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment