61 #ifdef OPENSSL_SYS_WIN32
62 #ifndef OPENSSL_NO_CAPIENG
69 #define _WIN32_WINNT 0x0400
86 #if defined(CERT_KEY_PROV_INFO_PROP_ID) && \
87 defined(CERT_STORE_PROV_SYSTEM_A) && \
88 defined(CERT_STORE_READONLY_FLAG)
89 # define __COMPILE_CAPIENG
94 #ifdef __COMPILE_CAPIENG
96 #undef X509_EXTENSIONS
100 #ifndef CERT_STORE_OPEN_EXISTING_FLAG
101 #define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
104 #ifndef CERT_STORE_CREATE_NEW_FLAG
105 #define CERT_STORE_CREATE_NEW_FLAG 0x00002000
108 #ifndef CERT_SYSTEM_STORE_CURRENT_USER
109 #define CERT_SYSTEM_STORE_CURRENT_USER 0x00010000
120 static const char *engine_capi_id =
"capi";
121 static const char *engine_capi_name =
"CryptoAPI ENGINE";
123 typedef struct CAPI_CTX_st CAPI_CTX;
124 typedef struct CAPI_KEY_st CAPI_KEY;
126 static void capi_addlasterror(
void);
127 static void capi_adderror(DWORD err);
129 static void CAPI_trace(CAPI_CTX *ctx,
char *
format, ...);
131 static int capi_list_providers(CAPI_CTX *ctx,
BIO *out);
132 static int capi_list_containers(CAPI_CTX *ctx,
BIO *out);
133 int capi_list_certs(CAPI_CTX *ctx,
BIO *out,
char *storename);
134 void capi_free_key(CAPI_KEY *
key);
136 static PCCERT_CONTEXT capi_find_cert(CAPI_CTX *ctx,
const char *
id, HCERTSTORE hstore);
138 CAPI_KEY *capi_find_key(CAPI_CTX *ctx,
const char *
id);
140 static EVP_PKEY *capi_load_privkey(
ENGINE *eng,
const char *key_id,
141 UI_METHOD *ui_method,
void *callback_data);
142 static int capi_rsa_sign(
int dtype,
const unsigned char *m,
unsigned int m_len,
143 unsigned char *sigret,
unsigned int *siglen,
const RSA *rsa);
144 static int capi_rsa_priv_enc(
int flen,
const unsigned char *from,
145 unsigned char *to,
RSA *rsa,
int padding);
146 static int capi_rsa_priv_dec(
int flen,
const unsigned char *from,
147 unsigned char *to,
RSA *rsa,
int padding);
148 static int capi_rsa_free(
RSA *rsa);
150 static DSA_SIG *capi_dsa_do_sign(
const unsigned char *digest,
int dlen,
152 static int capi_dsa_free(
DSA *dsa);
154 static int capi_load_ssl_client_cert(
ENGINE *
e,
SSL *ssl,
159 #ifdef OPENSSL_CAPIENG_DIALOG
163 typedef PCCERT_CONTEXT (WINAPI *CERTDLG)(HCERTSTORE, HWND, LPCWSTR,
164 LPCWSTR, DWORD, DWORD,
166 typedef HWND (WINAPI *GETCONSWIN)(
void);
173 #define CAPI_DBG_TRACE 2
174 #define CAPI_DBG_ERROR 1
185 LPSTR ssl_client_store;
191 #define CAPI_LU_SUBSTR 1
193 #define CAPI_LU_FNAME 2
195 #define CAPI_LU_CONTNAME 3
199 #define CAPI_DMP_SUMMARY 0x1
201 #define CAPI_DMP_FNAME 0x2
203 #define CAPI_DMP_FULL 0x4
205 #define CAPI_DMP_PEM 0x8
207 #define CAPI_DMP_PSKEY 0x10
209 #define CAPI_DMP_PKEYINFO 0x20
214 CERTDLG certselectdlg;
215 GETCONSWIN getconswindow;
219 static CAPI_CTX *capi_ctx_new();
220 static void capi_ctx_free(CAPI_CTX *ctx);
221 static int capi_ctx_set_provname(CAPI_CTX *ctx, LPSTR pname, DWORD
type,
int check);
222 static int capi_ctx_set_provname_idx(CAPI_CTX *ctx,
int idx);
224 #define CAPI_CMD_LIST_CERTS ENGINE_CMD_BASE
225 #define CAPI_CMD_LOOKUP_CERT (ENGINE_CMD_BASE + 1)
226 #define CAPI_CMD_DEBUG_LEVEL (ENGINE_CMD_BASE + 2)
227 #define CAPI_CMD_DEBUG_FILE (ENGINE_CMD_BASE + 3)
228 #define CAPI_CMD_KEYTYPE (ENGINE_CMD_BASE + 4)
229 #define CAPI_CMD_LIST_CSPS (ENGINE_CMD_BASE + 5)
230 #define CAPI_CMD_SET_CSP_IDX (ENGINE_CMD_BASE + 6)
231 #define CAPI_CMD_SET_CSP_NAME (ENGINE_CMD_BASE + 7)
232 #define CAPI_CMD_SET_CSP_TYPE (ENGINE_CMD_BASE + 8)
233 #define CAPI_CMD_LIST_CONTAINERS (ENGINE_CMD_BASE + 9)
234 #define CAPI_CMD_LIST_OPTIONS (ENGINE_CMD_BASE + 10)
235 #define CAPI_CMD_LOOKUP_METHOD (ENGINE_CMD_BASE + 11)
236 #define CAPI_CMD_STORE_NAME (ENGINE_CMD_BASE + 12)
237 #define CAPI_CMD_STORE_FLAGS (ENGINE_CMD_BASE + 13)
240 {CAPI_CMD_LIST_CERTS,
242 "List all certificates in store",
244 {CAPI_CMD_LOOKUP_CERT,
246 "Lookup and output certificates",
248 {CAPI_CMD_DEBUG_LEVEL,
250 "debug level (1=errors, 2=trace)",
252 {CAPI_CMD_DEBUG_FILE,
254 "debugging filename)",
258 "Key type: 1=AT_KEYEXCHANGE (default), 2=AT_SIGNATURE",
264 {CAPI_CMD_SET_CSP_IDX,
268 {CAPI_CMD_SET_CSP_NAME,
270 "Set CSP name, (default CSP used if not specified)",
272 {CAPI_CMD_SET_CSP_TYPE,
274 "Set CSP type, (default RSA_PROV_FULL)",
276 {CAPI_CMD_LIST_CONTAINERS,
278 "list container names",
280 {CAPI_CMD_LIST_OPTIONS,
282 "Set list options (1=summary,2=friendly name, 4=full printout, 8=PEM output, 16=XXX, "
283 "32=private key info)",
285 {CAPI_CMD_LOOKUP_METHOD,
287 "Set key lookup method (1=substring, 2=friendlyname, 3=container name)",
289 {CAPI_CMD_STORE_NAME,
291 "certificate store name, default \"MY\"",
293 {CAPI_CMD_STORE_FLAGS,
295 "Certificate store flags: 1 = system store",
301 static int capi_idx = -1;
302 static int rsa_capi_idx = -1;
303 static int dsa_capi_idx = -1;
304 static int cert_capi_idx = -1;
306 static int capi_ctrl(
ENGINE *e,
int cmd,
long i,
void *
p,
void (*
f)(
void))
320 case CAPI_CMD_LIST_CSPS:
321 ret = capi_list_providers(ctx, out);
324 case CAPI_CMD_LIST_CERTS:
325 ret = capi_list_certs(ctx, out, NULL);
328 case CAPI_CMD_LOOKUP_CERT:
329 ret = capi_list_certs(ctx, out, p);
332 case CAPI_CMD_LIST_CONTAINERS:
333 ret = capi_list_containers(ctx, out);
336 case CAPI_CMD_STORE_NAME:
340 CAPI_trace(ctx,
"Setting store name to %s\n", p);
343 case CAPI_CMD_STORE_FLAGS:
346 ctx->store_flags |= CERT_SYSTEM_STORE_LOCAL_MACHINE;
347 ctx->store_flags &= ~CERT_SYSTEM_STORE_CURRENT_USER;
351 ctx->store_flags |= CERT_SYSTEM_STORE_CURRENT_USER;
352 ctx->store_flags &= ~CERT_SYSTEM_STORE_LOCAL_MACHINE;
354 CAPI_trace(ctx,
"Setting flags to %d\n", i);
357 case CAPI_CMD_DEBUG_LEVEL:
358 ctx->debug_level = (int)i;
359 CAPI_trace(ctx,
"Setting debug level to %d\n", ctx->debug_level);
362 case CAPI_CMD_DEBUG_FILE:
364 CAPI_trace(ctx,
"Setting debug file to %s\n", ctx->debug_file);
367 case CAPI_CMD_KEYTYPE:
369 CAPI_trace(ctx,
"Setting key type to %d\n", ctx->keytype);
372 case CAPI_CMD_SET_CSP_IDX:
373 ret = capi_ctx_set_provname_idx(ctx, i);
376 case CAPI_CMD_LIST_OPTIONS:
380 case CAPI_CMD_LOOKUP_METHOD:
386 ctx->lookup_method = i;
389 case CAPI_CMD_SET_CSP_NAME:
390 ret = capi_ctx_set_provname(ctx, p, ctx->csptype, 1);
393 case CAPI_CMD_SET_CSP_TYPE:
409 "CryptoAPI RSA method",
426 "CryptoAPI DSA method",
440 static int capi_init(
ENGINE *e)
470 ctx = capi_ctx_new();
476 #ifdef OPENSSL_CAPIENG_DIALOG
478 HMODULE cryptui = LoadLibrary(TEXT(
"CRYPTUI.DLL"));
479 HMODULE kernel = GetModuleHandle(TEXT(
"KERNEL32.DLL"));
481 ctx->certselectdlg = (CERTDLG)GetProcAddress(cryptui,
"CryptUIDlgSelectCertificateFromStore");
483 ctx->getconswindow = (GETCONSWIN)GetProcAddress(kernel,
"GetConsoleWindow");
485 ctx->client_cert_select = cert_select_dialog;
499 static int capi_destroy(
ENGINE *e)
501 ERR_unload_CAPI_strings();
505 static int capi_finish(
ENGINE *e)
523 PCCERT_CONTEXT pcert;
529 static int bind_capi(
ENGINE *e)
541 capi_load_ssl_client_cert)
545 ERR_load_CAPI_strings();
551 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
552 static int bind_helper(
ENGINE *e,
const char *
id)
554 if(
id && (strcmp(
id, engine_capi_id) != 0))
563 static ENGINE *engine_capi(
void)
579 ENGINE *toadd = engine_capi();
588 static int lend_tobn(
BIGNUM *bn,
unsigned char *bin,
int binlen)
595 for(i = 0; i < binlen / 2; i++)
599 bin[i] = bin[binlen - i - 1];
600 bin[binlen - i - 1] = c;
612 unsigned char *pubkey = NULL;
618 if (!CryptExportKey(key->key, 0, PUBLICKEYBLOB, 0, NULL, &len))
630 if (!CryptExportKey(key->key, 0, PUBLICKEYBLOB, 0, pubkey, &len))
637 bh = (BLOBHEADER *)pubkey;
638 if (bh->bType != PUBLICKEYBLOB)
643 if (bh->aiKeyAlg == CALG_RSA_SIGN || bh->aiKeyAlg == CALG_RSA_KEYX)
647 unsigned char *rsa_modulus;
648 rp = (RSAPUBKEY *)(bh + 1);
649 if (rp->magic != 0x31415352)
657 rsa_modulus = (
unsigned char *)(rp + 1);
665 if (!rkey->
e || !rkey->
n)
671 rsa_modlen = rp->bitlen / 8;
672 if (!lend_tobn(rkey->
n, rsa_modulus, rsa_modlen))
684 else if (bh->aiKeyAlg == CALG_DSS_SIGN)
689 dp = (DSSPUBKEY *)(bh + 1);
690 if (dp->magic != 0x31535344)
698 dsa_plen = dp->bitlen / 8;
699 btmp = (
unsigned char *)(dp + 1);
707 if (!dkey->
p || !dkey->
q || !dkey->
g || !dkey->
pub_key)
709 if (!lend_tobn(dkey->
p, btmp, dsa_plen))
712 if (!lend_tobn(dkey->
q, btmp, 20))
715 if (!lend_tobn(dkey->
g, btmp, dsa_plen))
718 if (!lend_tobn(dkey->
pub_key, btmp, dsa_plen))
759 static EVP_PKEY *capi_load_privkey(
ENGINE *eng,
const char *key_id,
760 UI_METHOD *ui_method,
void *callback_data)
773 key = capi_find_key(ctx, key_id);
778 ret = capi_get_pkey(eng, key);
788 int capi_rsa_priv_enc(
int flen,
const unsigned char *from,
789 unsigned char *to,
RSA *rsa,
int padding)
795 int capi_rsa_sign(
int dtype,
const unsigned char *m,
unsigned int m_len,
796 unsigned char *sigret,
unsigned int *siglen,
const RSA *rsa)
808 CAPI_trace(ctx,
"Called CAPI_rsa_sign()\n");
828 alg = CALG_SSL3_SHAMD5;
843 if(!CryptCreateHash(capi_key->hprov, alg, 0, 0, &hash))
851 if(!CryptSetHashParam(hash, HP_HASHVAL, (
unsigned char *)m, 0))
861 if(!CryptSignHashA(hash, capi_key->keyspec, NULL, 0, sigret, &slen))
871 for(i = 0; i < slen / 2; i++)
875 sigret[i] = sigret[slen - i - 1];
876 sigret[slen - i - 1] = c;
884 CryptDestroyHash(hash);
889 int capi_rsa_priv_dec(
int flen,
const unsigned char *from,
890 unsigned char *to,
RSA *rsa,
int padding)
893 unsigned char *tmpbuf;
898 CAPI_trace(ctx,
"Called capi_rsa_priv_dec()\n");
923 for(i = 0; i < flen; i++)
924 tmpbuf[flen - i - 1] = from[i];
927 if(!CryptDecrypt(capi_key->key, 0, TRUE, 0, tmpbuf, &flen))
934 else memcpy(to, tmpbuf, flen);
941 static int capi_rsa_free(
RSA *rsa)
945 capi_free_key(capi_key);
952 static DSA_SIG *capi_dsa_do_sign(
const unsigned char *digest,
int dlen,
960 unsigned char csigbuf[40];
964 CAPI_trace(ctx,
"Called CAPI_dsa_do_sign()\n");
981 if(!CryptCreateHash(capi_key->hprov, CALG_SHA1, 0, 0, &hash))
989 if(!CryptSetHashParam(hash, HP_HASHVAL, (
unsigned char *)digest, 0))
998 slen =
sizeof(csigbuf);
999 if(!CryptSignHashA(hash, capi_key->keyspec, NULL, 0, csigbuf, &slen))
1002 capi_addlasterror();
1012 if (!ret->
r || !ret->
s)
1014 if (!lend_tobn(ret->
r, csigbuf, 20)
1015 || !lend_tobn(ret->
s, csigbuf + 20, 20))
1027 CryptDestroyHash(hash);
1031 static int capi_dsa_free(
DSA *dsa)
1035 capi_free_key(capi_key);
1040 static void capi_vtrace(CAPI_CTX *ctx,
int level,
char *
format, va_list argptr)
1044 if (!ctx || (ctx->debug_level < level) || (!ctx->debug_file))
1051 static void CAPI_trace(CAPI_CTX *ctx,
char *format, ...)
1054 va_start(args, format);
1055 capi_vtrace(ctx, CAPI_DBG_TRACE, format, args);
1059 static void capi_addlasterror(
void)
1061 capi_adderror(GetLastError());
1064 static void capi_adderror(DWORD err)
1071 static char *wide_to_asc(LPWSTR wstr)
1078 len_0 = (int)wcslen(wstr)+1;
1079 sz = WideCharToMultiByte(CP_ACP,0,wstr,len_0,NULL,0,NULL,NULL);
1091 if (!WideCharToMultiByte(CP_ACP,0,wstr,len_0,str,sz,NULL,NULL))
1100 static int capi_get_provname(CAPI_CTX *ctx, LPSTR *pname, DWORD *ptype, DWORD idx)
1104 CAPI_trace(ctx,
"capi_get_provname, index=%d\n", idx);
1105 if (!CryptEnumProvidersA(idx, NULL, 0, ptype, NULL, &len))
1107 err = GetLastError();
1108 if (err == ERROR_NO_MORE_ITEMS)
1115 if (!CryptEnumProvidersA(idx, NULL, 0, ptype, name, &len))
1117 err = GetLastError();
1118 if (err == ERROR_NO_MORE_ITEMS)
1125 CAPI_trace(ctx,
"capi_get_provname, returned name=%s, type=%d\n", name, *ptype);
1130 static int capi_list_providers(CAPI_CTX *ctx,
BIO *out)
1134 LPSTR provname = NULL;
1135 CAPI_trace(ctx,
"capi_list_providers\n");
1137 for(idx = 0; ; idx++)
1139 ret = capi_get_provname(ctx, &provname, &ptype, idx);
1144 BIO_printf(out,
"%d. %s, type %d\n", idx, provname, ptype);
1150 static int capi_list_containers(CAPI_CTX *ctx,
BIO *out)
1154 DWORD err, idx,
flags, buflen = 0, clen;
1156 CAPI_trace(ctx,
"Listing containers CSP=%s, type = %d\n", ctx->cspname, ctx->csptype);
1157 if (!CryptAcquireContextA(&hprov, NULL, ctx->cspname, ctx->csptype, CRYPT_VERIFYCONTEXT))
1160 capi_addlasterror();
1163 if (!CryptGetProvParam(hprov, PP_ENUMCONTAINERS, NULL, &buflen, CRYPT_FIRST))
1166 capi_addlasterror();
1167 CryptReleaseContext(hprov, 0);
1170 CAPI_trace(ctx,
"Got max container len %d\n", buflen);
1180 for (idx = 0;;idx++)
1186 flags = CRYPT_FIRST;
1189 if(!CryptGetProvParam(hprov, PP_ENUMCONTAINERS, cname, &clen, flags))
1191 err = GetLastError();
1192 if (err == ERROR_NO_MORE_ITEMS)
1198 CAPI_trace(ctx,
"Container name %s, len=%d, index=%d, flags=%d\n", cname, clen, idx, flags);
1199 if (!cname[0] && (clen == buflen))
1201 CAPI_trace(ctx,
"Enumerate bug: using workaround\n");
1213 CryptReleaseContext(hprov, 0);
1218 CRYPT_KEY_PROV_INFO *capi_get_prov_info(CAPI_CTX *ctx, PCCERT_CONTEXT cert)
1221 CRYPT_KEY_PROV_INFO *pinfo;
1223 if(!CertGetCertificateContextProperty(cert, CERT_KEY_PROV_INFO_PROP_ID, NULL, &len))
1231 if(!CertGetCertificateContextProperty(cert, CERT_KEY_PROV_INFO_PROP_ID, pinfo, &len))
1234 capi_addlasterror();
1241 static void capi_dump_prov_info(CAPI_CTX *ctx,
BIO *out, CRYPT_KEY_PROV_INFO *pinfo)
1243 char *provname = NULL, *contname = NULL;
1249 provname = wide_to_asc(pinfo->pwszProvName);
1250 contname = wide_to_asc(pinfo->pwszContainerName);
1251 if (!provname || !contname)
1255 BIO_printf(out,
" Provider Name: %s, Provider Type %d\n", provname, pinfo->dwProvType);
1256 BIO_printf(out,
" Container Name: %s, Key Type %d\n", contname, pinfo->dwKeySpec);
1264 char * capi_cert_get_fname(CAPI_CTX *ctx, PCCERT_CONTEXT cert)
1269 CAPI_trace(ctx,
"capi_cert_get_fname\n");
1270 if (!CertGetCertificateContextProperty(cert, CERT_FRIENDLY_NAME_PROP_ID, NULL, &dlen))
1273 if (CertGetCertificateContextProperty(cert, CERT_FRIENDLY_NAME_PROP_ID, wfname, &dlen))
1275 char *fname = wide_to_asc(wfname);
1280 capi_addlasterror();
1287 void capi_dump_cert(CAPI_CTX *ctx,
BIO *out, PCCERT_CONTEXT cert)
1291 unsigned long flags = ctx->dump_flags;
1292 if (flags & CAPI_DMP_FNAME)
1295 fname = capi_cert_get_fname(ctx, cert);
1298 BIO_printf(out,
" Friendly Name \"%s\"\n", fname);
1305 p = cert->pbCertEncoded;
1306 x = d2i_X509(NULL, &p, cert->cbCertEncoded);
1308 BIO_printf(out,
" <Can't parse certificate>\n");
1309 if (flags & CAPI_DMP_SUMMARY)
1317 if (flags & CAPI_DMP_FULL)
1320 if (flags & CAPI_DMP_PKEYINFO)
1322 CRYPT_KEY_PROV_INFO *pinfo;
1323 pinfo = capi_get_prov_info(ctx, cert);
1324 capi_dump_prov_info(ctx, out, pinfo);
1329 if (flags & CAPI_DMP_PEM)
1330 PEM_write_bio_X509(out, x);
1334 HCERTSTORE capi_open_store(CAPI_CTX *ctx,
char *storename)
1339 storename = ctx->storename;
1342 CAPI_trace(ctx,
"Opening certificate store %s\n", storename);
1344 hstore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, 0,
1345 ctx->store_flags, storename);
1349 capi_addlasterror();
1354 int capi_list_certs(CAPI_CTX *ctx,
BIO *out,
char *
id)
1360 PCCERT_CONTEXT cert = NULL;
1362 storename = ctx->storename;
1365 CAPI_trace(ctx,
"Listing certs for store %s\n", storename);
1367 hstore = capi_open_store(ctx, storename);
1372 cert = capi_find_cert(ctx,
id, hstore);
1378 capi_dump_cert(ctx, out, cert);
1379 CertFreeCertificateContext(cert);
1385 LPWSTR fname = NULL;
1386 cert = CertEnumCertificatesInStore(hstore, cert);
1390 capi_dump_cert(ctx, out, cert);
1394 CertCloseStore(hstore, 0);
1398 static PCCERT_CONTEXT capi_find_cert(CAPI_CTX *ctx,
const char *
id, HCERTSTORE hstore)
1400 PCCERT_CONTEXT cert = NULL;
1403 switch(ctx->lookup_method)
1405 case CAPI_LU_SUBSTR:
1406 return CertFindCertificateInStore(hstore,
1407 X509_ASN_ENCODING, 0,
1408 CERT_FIND_SUBJECT_STR_A,
id, NULL);
1412 cert = CertEnumCertificatesInStore(hstore, cert);
1415 fname = capi_cert_get_fname(ctx, cert);
1418 if (strcmp(fname,
id))
1432 static CAPI_KEY *capi_get_key(CAPI_CTX *ctx,
const char *contname,
char *provname, DWORD ptype, DWORD keyspec)
1436 CAPI_trace(ctx,
"capi_get_key, contname=%s, provname=%s, type=%d\n",
1437 contname, provname, ptype);
1438 if (!CryptAcquireContextA(&key->hprov, contname, provname, ptype, 0))
1441 capi_addlasterror();
1444 if (!CryptGetUserKey(key->hprov, keyspec, &key->key))
1447 capi_addlasterror();
1448 CryptReleaseContext(key->hprov, 0);
1451 key->keyspec = keyspec;
1460 static CAPI_KEY *capi_get_cert_key(CAPI_CTX *ctx, PCCERT_CONTEXT cert)
1462 CAPI_KEY *key = NULL;
1463 CRYPT_KEY_PROV_INFO *pinfo = NULL;
1464 char *provname = NULL, *contname = NULL;
1465 pinfo = capi_get_prov_info(ctx, cert);
1468 provname = wide_to_asc(pinfo->pwszProvName);
1469 contname = wide_to_asc(pinfo->pwszContainerName);
1470 if (!provname || !contname)
1472 key = capi_get_key(ctx, contname, provname,
1473 pinfo->dwProvType, pinfo->dwKeySpec);
1485 CAPI_KEY *capi_find_key(CAPI_CTX *ctx,
const char *
id)
1487 PCCERT_CONTEXT cert;
1489 CAPI_KEY *key = NULL;
1490 switch (ctx->lookup_method)
1492 case CAPI_LU_SUBSTR:
1494 hstore = capi_open_store(ctx, NULL);
1497 cert = capi_find_cert(ctx,
id, hstore);
1500 key = capi_get_cert_key(ctx, cert);
1501 CertFreeCertificateContext(cert);
1503 CertCloseStore(hstore, 0);
1506 case CAPI_LU_CONTNAME:
1507 key = capi_get_key(ctx,
id, ctx->cspname, ctx->csptype,
1515 void capi_free_key(CAPI_KEY *key)
1519 CryptDestroyKey(key->key);
1520 CryptReleaseContext(key->hprov, 0);
1522 CertFreeCertificateContext(key->pcert);
1529 static CAPI_CTX *capi_ctx_new()
1538 ctx->cspname = NULL;
1539 ctx->csptype = PROV_RSA_FULL;
1540 ctx->dump_flags = CAPI_DMP_SUMMARY|CAPI_DMP_FNAME;
1541 ctx->keytype = AT_KEYEXCHANGE;
1542 ctx->storename = NULL;
1543 ctx->ssl_client_store = NULL;
1544 ctx->store_flags = CERT_STORE_OPEN_EXISTING_FLAG |
1545 CERT_STORE_READONLY_FLAG |
1546 CERT_SYSTEM_STORE_CURRENT_USER;
1547 ctx->lookup_method = CAPI_LU_SUBSTR;
1548 ctx->debug_level = 0;
1549 ctx->debug_file = NULL;
1550 ctx->client_cert_select = cert_select_simple;
1554 static void capi_ctx_free(CAPI_CTX *ctx)
1556 CAPI_trace(ctx,
"Calling capi_ctx_free with %lx\n", ctx);
1561 if (ctx->debug_file)
1565 if (ctx->ssl_client_store)
1570 static int capi_ctx_set_provname(CAPI_CTX *ctx, LPSTR pname, DWORD
type,
int check)
1572 CAPI_trace(ctx,
"capi_ctx_set_provname, name=%s, type=%d\n", pname, type);
1576 if (!CryptAcquireContextA(&hprov, NULL, pname, type,
1577 CRYPT_VERIFYCONTEXT))
1580 capi_addlasterror();
1583 CryptReleaseContext(hprov, 0);
1588 ctx->csptype =
type;
1592 static int capi_ctx_set_provname_idx(CAPI_CTX *ctx,
int idx)
1597 if (capi_get_provname(ctx, &pname, &type, idx) != 1)
1599 res = capi_ctx_set_provname(ctx, pname, type, 0);
1622 static int capi_load_ssl_client_cert(
ENGINE *e,
SSL *ssl,
1630 int i, client_cert_idx;
1632 PCCERT_CONTEXT cert = NULL, excert = NULL;
1640 storename = ctx->ssl_client_store;
1644 hstore = capi_open_store(ctx, storename);
1650 cert = CertEnumCertificatesInStore(hstore, cert);
1653 p = cert->pbCertEncoded;
1654 x = d2i_X509(NULL, &p, cert->cbCertEncoded);
1657 CAPI_trace(ctx,
"Can't Parse Certificate %d\n", i);
1660 if (cert_issuer_match(ca_dn, x)
1663 key = capi_get_cert_key(ctx, cert);
1672 excert = CertDuplicateCertificateContext(cert);
1673 key->pcert = excert;
1687 CertFreeCertificateContext(cert);
1689 CertCloseStore(hstore, 0);
1697 client_cert_idx = ctx->client_cert_select(e, ssl, certs);
1704 if (i == client_cert_idx)
1722 *pkey = capi_get_pkey(e, key);
1737 #ifdef OPENSSL_CAPIENG_DIALOG
1747 #ifndef CRYPTUI_SELECT_LOCATION_COLUMN
1748 #define CRYPTUI_SELECT_LOCATION_COLUMN 0x000000010
1749 #define CRYPTUI_SELECT_INTENDEDUSE_COLUMN 0x000000004
1752 #define dlg_title L"OpenSSL Application SSL Client Certificate Selection"
1753 #define dlg_prompt L"Select a certificate to use for authentication"
1754 #define dlg_columns CRYPTUI_SELECT_LOCATION_COLUMN \
1755 |CRYPTUI_SELECT_INTENDEDUSE_COLUMN
1761 PCCERT_CONTEXT cert;
1770 dstore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1771 CERT_STORE_CREATE_NEW_FLAG, NULL);
1775 capi_addlasterror();
1784 if (!CertAddCertificateContextToStore(dstore, key->pcert,
1785 CERT_STORE_ADD_NEW, NULL))
1788 capi_addlasterror();
1793 hwnd = GetForegroundWindow();
1795 hwnd = GetActiveWindow();
1796 if (!hwnd && ctx->getconswindow)
1797 hwnd = ctx->getconswindow();
1799 cert = ctx->certselectdlg(dstore, hwnd, dlg_title, dlg_prompt,
1800 dlg_columns, 0, NULL);
1809 if (CertCompareCertificate(
1810 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
1812 key->pcert->pCertInfo))
1822 CertCloseStore(dstore, 0);
1830 #ifndef OPENSSL_NO_DYNAMIC_ENGINE