]> git.ipfire.org Git - thirdparty/openssl.git/blame - engines/e_capi.c
Add support for client cert engine setting in s_client app.
[thirdparty/openssl.git] / engines / e_capi.c
CommitLineData
7a18ecb2
DSH
1/* engines/e_capi.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project.
4 */
5/* ====================================================================
6 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 */
53
54
55#include <stdio.h>
56#include <string.h>
b3c8dd4e
DSH
57#include <windows.h>
58#include <wincrypt.h>
7a18ecb2
DSH
59#include <openssl/crypto.h>
60#include <openssl/buffer.h>
61#include <openssl/engine.h>
62#include <openssl/rsa.h>
63#include <openssl/bn.h>
64#include <openssl/pem.h>
65
66#ifdef OPENSSL_SYS_WIN32
67#ifndef OPENSSL_NO_CAPIENG
68
69#ifndef _WIN32_WINNT
70#define _WIN32_WINNT 0x400
71#endif
72
7a18ecb2
DSH
73
74#include "e_capi_err.h"
75#include "e_capi_err.c"
76
77
78static const char *engine_capi_id = "capi";
79static const char *engine_capi_name = "CryptoAPI ENGINE";
80
81typedef struct CAPI_CTX_st CAPI_CTX;
82typedef struct CAPI_KEY_st CAPI_KEY;
83
84static void capi_addlasterror(void);
85static void capi_adderror(DWORD err);
86
87static void CAPI_trace(CAPI_CTX *ctx, char *format, ...);
88
89static int capi_list_providers(CAPI_CTX *ctx, BIO *out);
90static int capi_list_containers(CAPI_CTX *ctx, BIO *out);
91int capi_list_certs(CAPI_CTX *ctx, BIO *out, char *storename);
92void capi_free_key(CAPI_KEY *key);
93
94static PCCERT_CONTEXT capi_find_cert(CAPI_CTX *ctx, const char *id, HCERTSTORE hstore);
95
96CAPI_KEY *capi_find_key(CAPI_CTX *ctx, const char *id);
97
98static EVP_PKEY *capi_load_privkey(ENGINE *eng, const char *key_id,
99 UI_METHOD *ui_method, void *callback_data);
100static int capi_rsa_sign(int dtype, const unsigned char *m, unsigned int m_len,
101 unsigned char *sigret, unsigned int *siglen, const RSA *rsa);
102static int capi_rsa_priv_enc(int flen, const unsigned char *from,
103 unsigned char *to, RSA *rsa, int padding);
104static int capi_rsa_priv_dec(int flen, const unsigned char *from,
105 unsigned char *to, RSA *rsa, int padding);
106static int capi_rsa_free(RSA *rsa);
107
108static DSA_SIG *capi_dsa_do_sign(const unsigned char *digest, int dlen,
109 DSA *dsa);
110static int capi_dsa_free(DSA *dsa);
b3c8dd4e
DSH
111
112static int capi_load_ssl_client_cert(ENGINE *e, SSL *ssl,
113 STACK_OF(X509_NAME) *ca_dn, X509 **pcert, EVP_PKEY **pkey,
114 STACK_OF(X509) **pother, UI_METHOD *ui_method, void *callback_data);
7a18ecb2
DSH
115
116/* This structure contains CAPI ENGINE specific data:
117 * it contains various global options and affects how
118 * other functions behave.
119 */
120
121#define CAPI_DBG_TRACE 2
122#define CAPI_DBG_ERROR 1
123
124struct CAPI_CTX_st {
125 int debug_level;
126 char *debug_file;
127 /* Parameters to use for container lookup */
128 DWORD keytype;
129 LPTSTR cspname;
130 DWORD csptype;
131 /* Certificate store name to use */
132 LPTSTR storename;
b3c8dd4e 133 LPTSTR ssl_client_store;
7a18ecb2
DSH
134
135/* Lookup string meanings in load_private_key */
136/* Substring of subject: uses "storename" */
137#define CAPI_LU_SUBSTR 0
138/* Friendly name: uses storename */
139#define CAPI_LU_FNAME 1
140/* Container name: uses cspname, keytype */
141#define CAPI_LU_CONTNAME 2
142 int lookup_method;
143/* Info to dump with dumpcerts option */
144/* Issuer and serial name strings */
145#define CAPI_DMP_SUMMARY 0x1
146/* Friendly name */
147#define CAPI_DMP_FNAME 0x2
148/* Full X509_print dump */
149#define CAPI_DMP_FULL 0x4
150/* Dump PEM format certificate */
151#define CAPI_DMP_PEM 0x8
152/* Dump pseudo key (if possible) */
153#define CAPI_DMP_PSKEY 0x10
154/* Dump key info (if possible) */
155#define CAPI_DMP_PKEYINFO 0x20
156
157 DWORD dump_flags;
158};
159
160
161static CAPI_CTX *capi_ctx_new();
162static void capi_ctx_free(CAPI_CTX *ctx);
163static int capi_ctx_set_provname(CAPI_CTX *ctx, LPSTR pname, DWORD type, int check);
164static int capi_ctx_set_provname_idx(CAPI_CTX *ctx, int idx);
165
b3c8dd4e 166#define CAPI_CMD_LIST_CERTS ENGINE_CMD_BASE
7a18ecb2
DSH
167#define CAPI_CMD_LOOKUP_CERT (ENGINE_CMD_BASE + 1)
168#define CAPI_CMD_DEBUG_LEVEL (ENGINE_CMD_BASE + 2)
b3c8dd4e
DSH
169#define CAPI_CMD_DEBUG_FILE (ENGINE_CMD_BASE + 3)
170#define CAPI_CMD_KEYTYPE (ENGINE_CMD_BASE + 4)
171#define CAPI_CMD_LIST_CSPS (ENGINE_CMD_BASE + 5)
7a18ecb2
DSH
172#define CAPI_CMD_SET_CSP_IDX (ENGINE_CMD_BASE + 6)
173#define CAPI_CMD_SET_CSP_NAME (ENGINE_CMD_BASE + 7)
174#define CAPI_CMD_SET_CSP_TYPE (ENGINE_CMD_BASE + 8)
175#define CAPI_CMD_LIST_CONTAINERS (ENGINE_CMD_BASE + 9)
176#define CAPI_CMD_LIST_OPTIONS (ENGINE_CMD_BASE + 10)
177#define CAPI_CMD_LOOKUP_METHOD (ENGINE_CMD_BASE + 11)
c621c7e4 178#define CAPI_CMD_STORE_NAME (ENGINE_CMD_BASE + 12)
7a18ecb2
DSH
179
180static const ENGINE_CMD_DEFN capi_cmd_defns[] = {
181 {CAPI_CMD_LIST_CERTS,
182 "list_certs",
183 "List all certificates in store",
184 ENGINE_CMD_FLAG_NO_INPUT},
185 {CAPI_CMD_LOOKUP_CERT,
186 "lookup_cert",
187 "Lookup and output certificates",
188 ENGINE_CMD_FLAG_STRING},
189 {CAPI_CMD_DEBUG_LEVEL,
190 "debug_level",
191 "debug level (1=errors, 2=trace)",
192 ENGINE_CMD_FLAG_NUMERIC},
193 {CAPI_CMD_DEBUG_FILE,
194 "debug_file",
195 "debugging filename)",
196 ENGINE_CMD_FLAG_STRING},
197 {CAPI_CMD_KEYTYPE,
198 "key_type",
199 "Key type: 1=AT_KEYEXCHANGE (default), 2=AT_SIGNATURE",
200 ENGINE_CMD_FLAG_NUMERIC},
201 {CAPI_CMD_LIST_CSPS,
202 "list_csps",
203 "List all CSPs",
204 ENGINE_CMD_FLAG_NO_INPUT},
205 {CAPI_CMD_SET_CSP_IDX,
206 "csp_idx",
207 "Set CSP by index",
208 ENGINE_CMD_FLAG_NUMERIC},
209 {CAPI_CMD_SET_CSP_NAME,
210 "csp_name",
211 "Set CSP name, (default CSP used if not specified)",
212 ENGINE_CMD_FLAG_STRING},
213 {CAPI_CMD_SET_CSP_TYPE,
214 "csp_type",
215 "Set CSP type, (default RSA_PROV_FULL)",
216 ENGINE_CMD_FLAG_NUMERIC},
217 {CAPI_CMD_LIST_CONTAINERS,
218 "list_containers",
219 "list container names",
220 ENGINE_CMD_FLAG_NO_INPUT},
221 {CAPI_CMD_LIST_OPTIONS,
222 "list_options",
223 "Set list options (1=summary,2=friendly name, 4=full printout, 8=PEM output, 16=XXX, "
224 "32=private key info)",
225 ENGINE_CMD_FLAG_NUMERIC},
226 {CAPI_CMD_LOOKUP_METHOD,
227 "lookup_method",
228 "Set key lookup method (1=substring, 2=friendlyname, 3=container name)",
229 ENGINE_CMD_FLAG_NUMERIC},
c621c7e4
DSH
230 {CAPI_CMD_STORE_NAME,
231 "store_name",
232 "certificate store name, default \"MY\"",
233 ENGINE_CMD_FLAG_STRING},
7a18ecb2
DSH
234
235 {0, NULL, NULL, 0}
236 };
237
238static int capi_idx = -1;
239static int rsa_capi_idx = -1;
240static int dsa_capi_idx = -1;
241
242static int capi_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
2aa2a577 243 {
7a18ecb2
DSH
244 int ret = 1;
245 CAPI_CTX *ctx;
246 BIO *out;
247 if (capi_idx == -1)
248 {
249 CAPIerr(CAPI_F_CAPI_CTRL, CAPI_R_ENGINE_NOT_INITIALIZED);
250 return 0;
251 }
252 ctx = ENGINE_get_ex_data(e, capi_idx);
253 out = BIO_new_fp(stdout, BIO_NOCLOSE);
254 switch (cmd)
255 {
256 case CAPI_CMD_LIST_CSPS:
257 ret = capi_list_providers(ctx, out);
258 break;
259
260 case CAPI_CMD_LIST_CERTS:
261 ret = capi_list_certs(ctx, out, NULL);
262 break;
263
264 case CAPI_CMD_LOOKUP_CERT:
265 ret = capi_list_certs(ctx, out, p);
266 break;
267
268 case CAPI_CMD_LIST_CONTAINERS:
269 ret = capi_list_containers(ctx, out);
270 break;
271
c621c7e4 272 case CAPI_CMD_STORE_NAME:
953174f4
DSH
273 if (ctx->storename)
274 OPENSSL_free(ctx->storename);
c621c7e4
DSH
275 ctx->storename = BUF_strdup(p);
276 CAPI_trace(ctx, "Setting store name to %s\n", p);
277 break;
278
7a18ecb2
DSH
279 case CAPI_CMD_DEBUG_LEVEL:
280 ctx->debug_level = (int)i;
281 CAPI_trace(ctx, "Setting debug level to %d\n", ctx->debug_level);
282 break;
283
284 case CAPI_CMD_DEBUG_FILE:
285 ctx->debug_file = BUF_strdup(p);
286 CAPI_trace(ctx, "Setting debug file to %s\n", ctx->debug_file);
287 break;
288
289 case CAPI_CMD_KEYTYPE:
290 ctx->keytype = i;
291 CAPI_trace(ctx, "Setting key type to %d\n", ctx->keytype);
292 break;
293
294 case CAPI_CMD_SET_CSP_IDX:
295 ret = capi_ctx_set_provname_idx(ctx, i);
296 break;
297
298 case CAPI_CMD_LIST_OPTIONS:
299 ctx->dump_flags = i;
300 break;
301
302 case CAPI_CMD_LOOKUP_METHOD:
303 if (i < 1 || i > 3)
304 {
305 CAPIerr(CAPI_F_CAPI_CTRL, CAPI_R_INVALID_LOOKUP_METHOD);
306 return 0;
307 }
308 ctx->lookup_method = i;
309 break;
310
311 case CAPI_CMD_SET_CSP_NAME:
312 ret = capi_ctx_set_provname(ctx, p, ctx->csptype, 1);
313 break;
314
315 case CAPI_CMD_SET_CSP_TYPE:
316 ctx->csptype = i;
317 break;
318
319 default:
320 CAPIerr(CAPI_F_CAPI_CTRL, CAPI_R_UNKNOWN_COMMAND);
321 ret = 0;
322 }
323
324 BIO_free(out);
325 return ret;
326
2aa2a577 327 }
7a18ecb2
DSH
328
329static RSA_METHOD capi_rsa_method =
330 {
331 "CryptoAPI RSA method",
332 0, /* pub_enc */
333 0, /* pub_dec */
334 capi_rsa_priv_enc, /* priv_enc */
335 capi_rsa_priv_dec, /* priv_dec */
336 0, /* rsa_mod_exp */
337 0, /* bn_mod_exp */
338 0, /* init */
339 capi_rsa_free, /* finish */
340 RSA_FLAG_SIGN_VER, /* flags */
341 NULL, /* app_data */
342 capi_rsa_sign, /* rsa_sign */
343 0 /* rsa_verify */
344 };
345
346static DSA_METHOD capi_dsa_method =
347 {
348 "CryptoAPI DSA method",
349 capi_dsa_do_sign, /* dsa_do_sign */
350 0, /* dsa_sign_setup */
351 0, /* dsa_do_verify */
352 0, /* dsa_mod_exp */
353 0, /* bn_mod_exp */
354 0, /* init */
355 capi_dsa_free, /* finish */
356 0, /* flags */
357 NULL, /* app_data */
358 0, /* dsa_paramgen */
359 0 /* dsa_keygen */
360 };
361
362static int capi_init(ENGINE *e)
363 {
364 CAPI_CTX *ctx;
365 const RSA_METHOD *ossl_rsa_meth;
366 const DSA_METHOD *ossl_dsa_meth;
367 capi_idx = ENGINE_get_ex_new_index(0, NULL, NULL, NULL, 0);
368
369 ctx = capi_ctx_new();
370 if (!ctx || (capi_idx < 0))
371 goto memerr;
372
373 ENGINE_set_ex_data(e, capi_idx, ctx);
374 /* Setup RSA_METHOD */
375 rsa_capi_idx = RSA_get_ex_new_index(0, NULL, NULL, NULL, 0);
376 ossl_rsa_meth = RSA_PKCS1_SSLeay();
377 capi_rsa_method.rsa_pub_enc = ossl_rsa_meth->rsa_pub_enc;
378 capi_rsa_method.rsa_pub_dec = ossl_rsa_meth->rsa_pub_dec;
379 capi_rsa_method.rsa_mod_exp = ossl_rsa_meth->rsa_mod_exp;
380 capi_rsa_method.bn_mod_exp = ossl_rsa_meth->bn_mod_exp;
381
382 /* Setup DSA Method */
383 dsa_capi_idx = DSA_get_ex_new_index(0, NULL, NULL, NULL, 0);
384 ossl_dsa_meth = DSA_OpenSSL();
385 capi_dsa_method.dsa_do_verify = ossl_dsa_meth->dsa_do_verify;
386 capi_dsa_method.dsa_mod_exp = ossl_dsa_meth->dsa_mod_exp;
387 capi_dsa_method.bn_mod_exp = ossl_dsa_meth->bn_mod_exp;
388
389 return 1;
390
391 memerr:
392 CAPIerr(CAPI_F_CAPI_INIT, ERR_R_MALLOC_FAILURE);
393 return 0;
394
395 return 1;
396 }
397
398static int capi_destroy(ENGINE *e)
399 {
7a18ecb2
DSH
400 ERR_unload_CAPI_strings();
401 return 1;
402 }
403
404static int capi_finish(ENGINE *e)
405 {
406 CAPI_CTX *ctx;
407 ctx = ENGINE_get_ex_data(e, capi_idx);
408 capi_ctx_free(ctx);
409 ENGINE_set_ex_data(e, capi_idx, NULL);
410 return 1;
411 }
412
413
414/* CryptoAPI key application data. This contains
415 * a handle to the private key container (for sign operations)
416 * and a handle to the key (for decrypt operations).
417 */
418
419struct CAPI_KEY_st
420 {
421 HCRYPTPROV hprov;
422 HCRYPTKEY key;
4be0a5d4 423 DWORD keyspec;
7a18ecb2
DSH
424 };
425
426static int bind_capi(ENGINE *e)
427 {
428 if (!ENGINE_set_id(e, engine_capi_id)
429 || !ENGINE_set_name(e, engine_capi_name)
430 || !ENGINE_set_init_function(e, capi_init)
431 || !ENGINE_set_finish_function(e, capi_finish)
432 || !ENGINE_set_destroy_function(e, capi_destroy)
433 || !ENGINE_set_RSA(e, &capi_rsa_method)
434 || !ENGINE_set_DSA(e, &capi_dsa_method)
435 || !ENGINE_set_load_privkey_function(e, capi_load_privkey)
b3c8dd4e
DSH
436 || !ENGINE_set_load_ssl_client_cert_function(e,
437 capi_load_ssl_client_cert)
7a18ecb2
DSH
438 || !ENGINE_set_cmd_defns(e, capi_cmd_defns)
439 || !ENGINE_set_ctrl_function(e, capi_ctrl))
440 return 0;
441 ERR_load_CAPI_strings();
442
443 return 1;
444
445 }
446
447#ifndef OPENSSL_NO_DYNAMIC_ENGINE
448static int bind_helper(ENGINE *e, const char *id)
449 {
450 if(id && (strcmp(id, engine_capi_id) != 0))
451 return 0;
452 if(!bind_capi(e))
453 return 0;
454 return 1;
455 }
456IMPLEMENT_DYNAMIC_CHECK_FN()
457IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
458#else
459static ENGINE *engine_capi(void)
460 {
461 ENGINE *ret = ENGINE_new();
462 if(!ret)
463 return NULL;
464 if(!bind_capi(ret))
465 {
466 ENGINE_free(ret);
467 return NULL;
468 }
469 return ret;
470 }
471
472void ENGINE_load_capi(void)
473 {
474 /* Copied from eng_[openssl|dyn].c */
475 ENGINE *toadd = engine_capi();
476 if(!toadd) return;
477 ENGINE_add(toadd);
478 ENGINE_free(toadd);
479 ERR_clear_error();
480 }
481#endif
482
483
484static int lend_tobn(BIGNUM *bn, unsigned char *bin, int binlen)
485 {
486 int i;
487 /* Reverse buffer in place: since this is a keyblob structure
488 * that will be freed up after conversion anyway it doesn't
489 * matter if we change it.
490 */
491 for(i = 0; i < binlen / 2; i++)
492 {
493 unsigned char c;
494 c = bin[i];
495 bin[i] = bin[binlen - i - 1];
496 bin[binlen - i - 1] = c;
497 }
498
499 if (!BN_bin2bn(bin, binlen, bn))
500 return 0;
501 return 1;
502 }
503
b3c8dd4e
DSH
504/* Given a CAPI_KEY get an EVP_PKEY structure */
505
506static EVP_PKEY *capi_get_pkey(ENGINE *eng, CAPI_KEY *key)
7a18ecb2 507 {
7a18ecb2
DSH
508 unsigned char *pubkey = NULL;
509 DWORD len;
510 BLOBHEADER *bh;
511 RSA *rkey = NULL;
512 DSA *dkey = NULL;
b3c8dd4e 513 EVP_PKEY *ret = NULL;
7a18ecb2
DSH
514 if (!CryptExportKey(key->key, 0, PUBLICKEYBLOB, 0, NULL, &len))
515 {
b3c8dd4e 516 CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_PUBKEY_EXPORT_LENGTH_ERROR);
7a18ecb2
DSH
517 capi_addlasterror();
518 return NULL;
519 }
520
521 pubkey = OPENSSL_malloc(len);
522
523 if (!pubkey)
524 goto memerr;
525
526 if (!CryptExportKey(key->key, 0, PUBLICKEYBLOB, 0, pubkey, &len))
527 {
b3c8dd4e 528 CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_PUBKEY_EXPORT_ERROR);
7a18ecb2
DSH
529 capi_addlasterror();
530 goto err;
531 }
532
533 bh = (BLOBHEADER *)pubkey;
534 if (bh->bType != PUBLICKEYBLOB)
535 {
b3c8dd4e 536 CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_INVALID_PUBLIC_KEY_BLOB);
7a18ecb2
DSH
537 goto err;
538 }
539 if (bh->aiKeyAlg == CALG_RSA_SIGN || bh->aiKeyAlg == CALG_RSA_KEYX)
540 {
541 RSAPUBKEY *rp;
542 DWORD rsa_modlen;
543 unsigned char *rsa_modulus;
544 rp = (RSAPUBKEY *)(bh + 1);
545 if (rp->magic != 0x31415352)
546 {
547 char magstr[10];
548 BIO_snprintf(magstr, 10, "%lx", rp->magic);
b3c8dd4e 549 CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_INVALID_RSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER);
7a18ecb2
DSH
550 ERR_add_error_data(2, "magic=0x", magstr);
551 goto err;
552 }
553 rsa_modulus = (unsigned char *)(rp + 1);
554 rkey = RSA_new_method(eng);
555 if (!rkey)
556 goto memerr;
557
558 rkey->e = BN_new();
559 rkey->n = BN_new();
560
561 if (!rkey->e || !rkey->n)
562 goto memerr;
563
564 if (!BN_set_word(rkey->e, rp->pubexp))
565 goto memerr;
566
567 rsa_modlen = rp->bitlen / 8;
568 if (!lend_tobn(rkey->n, rsa_modulus, rsa_modlen))
569 goto memerr;
570
571 RSA_set_ex_data(rkey, rsa_capi_idx, key);
572
573 if (!(ret = EVP_PKEY_new()))
574 goto memerr;
575
576 EVP_PKEY_assign_RSA(ret, rkey);
577 rkey = NULL;
578
579 }
580 else if (bh->aiKeyAlg == CALG_DSS_SIGN)
581 {
582 DSSPUBKEY *dp;
583 DWORD dsa_plen;
584 unsigned char *btmp;
585 dp = (DSSPUBKEY *)(bh + 1);
586 if (dp->magic != 0x31535344)
587 {
588 char magstr[10];
589 BIO_snprintf(magstr, 10, "%lx", dp->magic);
b3c8dd4e 590 CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_INVALID_DSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER);
7a18ecb2
DSH
591 ERR_add_error_data(2, "magic=0x", magstr);
592 goto err;
593 }
594 dsa_plen = dp->bitlen / 8;
595 btmp = (unsigned char *)(dp + 1);
596 dkey = DSA_new_method(eng);
597 if (!dkey)
598 goto memerr;
599 dkey->p = BN_new();
600 dkey->q = BN_new();
601 dkey->g = BN_new();
602 dkey->pub_key = BN_new();
603 if (!dkey->p || !dkey->q || !dkey->g || !dkey->pub_key)
604 goto memerr;
605 if (!lend_tobn(dkey->p, btmp, dsa_plen))
606 goto memerr;
607 btmp += dsa_plen;
608 if (!lend_tobn(dkey->q, btmp, 20))
609 goto memerr;
610 btmp += 20;
611 if (!lend_tobn(dkey->g, btmp, dsa_plen))
612 goto memerr;
613 btmp += dsa_plen;
614 if (!lend_tobn(dkey->pub_key, btmp, dsa_plen))
615 goto memerr;
616 btmp += dsa_plen;
617
618 DSA_set_ex_data(dkey, dsa_capi_idx, key);
619
620 if (!(ret = EVP_PKEY_new()))
621 goto memerr;
622
623 EVP_PKEY_assign_DSA(ret, dkey);
624 dkey = NULL;
625 }
626 else
627 {
628 char algstr[10];
629 BIO_snprintf(algstr, 10, "%lx", bh->aiKeyAlg);
b3c8dd4e 630 CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_UNSUPPORTED_PUBLIC_KEY_ALGORITHM);
7a18ecb2
DSH
631 ERR_add_error_data(2, "aiKeyAlg=0x", algstr);
632 goto err;
633 }
634
b3c8dd4e 635
7a18ecb2
DSH
636 err:
637 if (pubkey)
638 OPENSSL_free(pubkey);
639 if (!ret)
640 {
641 if (rkey)
642 RSA_free(rkey);
643 if (dkey)
644 DSA_free(dkey);
7a18ecb2
DSH
645 }
646
647 return ret;
648
649memerr:
650 CAPIerr(CAPI_F_CAPI_LOAD_PRIVKEY, ERR_R_MALLOC_FAILURE);
651 goto err;
652
653 }
654
b3c8dd4e
DSH
655static EVP_PKEY *capi_load_privkey(ENGINE *eng, const char *key_id,
656 UI_METHOD *ui_method, void *callback_data)
657 {
658 CAPI_CTX *ctx;
659 CAPI_KEY *key;
660 EVP_PKEY *ret;
661 ctx = ENGINE_get_ex_data(eng, capi_idx);
662
663 if (!ctx)
664 {
665 CAPIerr(CAPI_F_CAPI_LOAD_PRIVKEY, CAPI_R_CANT_FIND_CAPI_CONTEXT);
666 return NULL;
667 }
668
669 key = capi_find_key(ctx, key_id);
670
671 if (!key)
672 return NULL;
673
674 ret = capi_get_pkey(eng, key);
675
676 if (!ret)
677 capi_free_key(key);
678 return ret;
679
680 }
681
7a18ecb2
DSH
682/* CryptoAPI RSA operations */
683
684int capi_rsa_priv_enc(int flen, const unsigned char *from,
685 unsigned char *to, RSA *rsa, int padding)
686 {
687 CAPIerr(CAPI_F_CAPI_RSA_PRIV_ENC, CAPI_R_FUNCTION_NOT_SUPPORTED);
688 return -1;
689 }
690
691int capi_rsa_sign(int dtype, const unsigned char *m, unsigned int m_len,
692 unsigned char *sigret, unsigned int *siglen, const RSA *rsa)
693 {
694 ALG_ID alg;
695 HCRYPTHASH hash;
696 DWORD slen;
697 unsigned int i;
698 int ret = -1;
699 CAPI_KEY *capi_key;
700 CAPI_CTX *ctx;
701
702 ctx = ENGINE_get_ex_data(rsa->engine, capi_idx);
703
704 CAPI_trace(ctx, "Called CAPI_rsa_sign()\n");
705
706 capi_key = RSA_get_ex_data(rsa, rsa_capi_idx);
707 if (!capi_key)
708 {
709 CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_CANT_GET_KEY);
710 return -1;
711 }
712/* Convert the signature type to a CryptoAPI algorithm ID */
2aa2a577
DSH
713 switch(dtype)
714 {
7a18ecb2
DSH
715 case NID_sha1:
716 alg = CALG_SHA1;
717 break;
718
719 case NID_md5:
720 alg = CALG_MD5;
721 break;
722
723 case NID_md5_sha1:
724 alg = CALG_SSL3_SHAMD5;
725 break;
726 default:
727 {
728 char algstr[10];
729 BIO_snprintf(algstr, 10, "%lx", dtype);
730 CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_UNSUPPORTED_ALGORITHM_NID);
731 ERR_add_error_data(2, "NID=0x", algstr);
732 return -1;
733 }
734 }
735
736
737
738/* Create the hash object */
2aa2a577
DSH
739 if(!CryptCreateHash(capi_key->hprov, alg, 0, 0, &hash))
740 {
7a18ecb2
DSH
741 CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_CANT_CREATE_HASH_OBJECT);
742 capi_addlasterror();
743 return -1;
2aa2a577 744 }
7a18ecb2
DSH
745/* Set the hash value to the value passed */
746
2aa2a577
DSH
747 if(!CryptSetHashParam(hash, HP_HASHVAL, (unsigned char *)m, 0))
748 {
7a18ecb2
DSH
749 CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_CANT_SET_HASH_VALUE);
750 capi_addlasterror();
751 goto err;
2aa2a577 752 }
7a18ecb2
DSH
753
754
755/* Finally sign it */
756 slen = RSA_size(rsa);
2aa2a577
DSH
757 if(!CryptSignHash(hash, capi_key->keyspec, NULL, 0, sigret, &slen))
758 {
7a18ecb2
DSH
759 CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_ERROR_SIGNING_HASH);
760 capi_addlasterror();
761 goto err;
2aa2a577
DSH
762 }
763 else
764 {
7a18ecb2
DSH
765 ret = 1;
766 /* Inplace byte reversal of signature */
2aa2a577
DSH
767 for(i = 0; i < slen / 2; i++)
768 {
7a18ecb2
DSH
769 unsigned char c;
770 c = sigret[i];
771 sigret[i] = sigret[slen - i - 1];
772 sigret[slen - i - 1] = c;
2aa2a577 773 }
7a18ecb2 774 *siglen = slen;
2aa2a577 775 }
7a18ecb2
DSH
776
777
778 /* Now cleanup */
779
780err:
781 CryptDestroyHash(hash);
782
783 return ret;
2aa2a577 784 }
7a18ecb2
DSH
785
786int capi_rsa_priv_dec(int flen, const unsigned char *from,
787 unsigned char *to, RSA *rsa, int padding)
2aa2a577 788 {
7a18ecb2
DSH
789 int i;
790 unsigned char *tmpbuf;
791 CAPI_KEY *capi_key;
792 CAPI_CTX *ctx;
793 ctx = ENGINE_get_ex_data(rsa->engine, capi_idx);
794
795 CAPI_trace(ctx, "Called capi_rsa_priv_dec()\n");
796
797
798 capi_key = RSA_get_ex_data(rsa, rsa_capi_idx);
799 if (!capi_key)
800 {
801 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, CAPI_R_CANT_GET_KEY);
802 return -1;
803 }
804
805 if(padding != RSA_PKCS1_PADDING)
806 {
807 char errstr[10];
808 BIO_snprintf(errstr, 10, "%d", padding);
809 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, CAPI_R_UNSUPPORTED_PADDING);
810 ERR_add_error_data(2, "padding=", errstr);
811 return -1;
812 }
813
814 /* Create temp reverse order version of input */
815 if(!(tmpbuf = OPENSSL_malloc(flen)) )
816 {
817 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, ERR_R_MALLOC_FAILURE);
818 return -1;
819 }
820 for(i = 0; i < flen; i++)
821 tmpbuf[flen - i - 1] = from[i];
822
823 /* Finally decrypt it */
824 if(!CryptDecrypt(capi_key->key, 0, TRUE, 0, tmpbuf, &flen))
825 {
826 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, CAPI_R_DECRYPT_ERROR);
827 capi_addlasterror();
828 OPENSSL_free(tmpbuf);
829 return -1;
830 }
831 else memcpy(to, tmpbuf, flen);
832
833 OPENSSL_free(tmpbuf);
834
835 return flen;
2aa2a577 836 }
7a18ecb2
DSH
837
838static int capi_rsa_free(RSA *rsa)
839 {
840 CAPI_KEY *capi_key;
841 capi_key = RSA_get_ex_data(rsa, rsa_capi_idx);
842 capi_free_key(capi_key);
843 RSA_set_ex_data(rsa, rsa_capi_idx, 0);
844 return 1;
845 }
846
847/* CryptoAPI DSA operations */
848
849static DSA_SIG *capi_dsa_do_sign(const unsigned char *digest, int dlen,
850 DSA *dsa)
851 {
852 HCRYPTHASH hash;
853 DWORD slen;
854 DSA_SIG *ret = NULL;
855 CAPI_KEY *capi_key;
856 CAPI_CTX *ctx;
857 unsigned char csigbuf[40];
858
859 ctx = ENGINE_get_ex_data(dsa->engine, capi_idx);
860
861 CAPI_trace(ctx, "Called CAPI_dsa_do_sign()\n");
862
863 capi_key = DSA_get_ex_data(dsa, dsa_capi_idx);
864
865 if (!capi_key)
866 {
867 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_CANT_GET_KEY);
868 return NULL;
869 }
870
871 if (dlen != 20)
872 {
873 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_INVALID_DIGEST_LENGTH);
874 return NULL;
875 }
876
877 /* Create the hash object */
2aa2a577
DSH
878 if(!CryptCreateHash(capi_key->hprov, CALG_SHA1, 0, 0, &hash))
879 {
7a18ecb2
DSH
880 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_CANT_CREATE_HASH_OBJECT);
881 capi_addlasterror();
882 return NULL;
2aa2a577 883 }
7a18ecb2
DSH
884
885 /* Set the hash value to the value passed */
2aa2a577
DSH
886 if(!CryptSetHashParam(hash, HP_HASHVAL, (unsigned char *)digest, 0))
887 {
7a18ecb2
DSH
888 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_CANT_SET_HASH_VALUE);
889 capi_addlasterror();
890 goto err;
2aa2a577 891 }
7a18ecb2
DSH
892
893
894 /* Finally sign it */
895 slen = sizeof(csigbuf);
2bbe8f91 896 if(!CryptSignHash(hash, capi_key->keyspec, NULL, 0, csigbuf, &slen))
7a18ecb2
DSH
897 {
898 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_ERROR_SIGNING_HASH);
899 capi_addlasterror();
900 goto err;
901 }
902 else
903 {
904 ret = DSA_SIG_new();
905 if (!ret)
906 goto err;
907 ret->r = BN_new();
908 ret->s = BN_new();
909 if (!ret->r || !ret->s)
910 goto err;
911 if (!lend_tobn(ret->r, csigbuf, 20)
912 || !lend_tobn(ret->s, csigbuf + 20, 20))
913 {
914 DSA_SIG_free(ret);
915 ret = NULL;
916 goto err;
917 }
918 }
919
920 /* Now cleanup */
921
922err:
923 OPENSSL_cleanse(csigbuf, 40);
924 CryptDestroyHash(hash);
925 return ret;
926 }
927
928static int capi_dsa_free(DSA *dsa)
929 {
930 CAPI_KEY *capi_key;
931 capi_key = DSA_get_ex_data(dsa, dsa_capi_idx);
932 capi_free_key(capi_key);
933 DSA_set_ex_data(dsa, dsa_capi_idx, 0);
934 return 1;
935 }
936
937static void capi_vtrace(CAPI_CTX *ctx, int level, char *format, va_list argptr)
938 {
939 BIO *out;
940
941 if (!ctx || (ctx->debug_level < level) || (!ctx->debug_file))
942 return;
943 out = BIO_new_file(ctx->debug_file, "a+");
944 BIO_vprintf(out, format, argptr);
945 BIO_free(out);
946 }
947
948static void CAPI_trace(CAPI_CTX *ctx, char *format, ...)
949 {
950 va_list args;
951 va_start(args, format);
952 capi_vtrace(ctx, CAPI_DBG_TRACE, format, args);
953 va_end(args);
954 }
955
956static void capi_addlasterror(void)
957 {
958 capi_adderror(GetLastError());
959 }
960
961static void capi_adderror(DWORD err)
962 {
963 char errstr[10];
964 BIO_snprintf(errstr, 10, "%lX", err);
965 ERR_add_error_data(2, "Error code= 0x", errstr);
966 }
967
968static char *wide_to_asc(LPWSTR wstr)
969 {
970 char *str;
971 if (!wstr)
972 return NULL;
973 str = OPENSSL_malloc(wcslen(wstr) + 1);
974 if (!str)
975 {
976 CAPIerr(CAPI_F_WIDE_TO_ASC, ERR_R_MALLOC_FAILURE);
977 return NULL;
978 }
979 sprintf(str, "%S", wstr);
980 return str;
981 }
982
983static int capi_get_provname(CAPI_CTX *ctx, LPSTR *pname, DWORD *ptype, DWORD idx)
984 {
985 LPSTR name;
986 DWORD len, err;
987 CAPI_trace(ctx, "capi_get_provname, index=%d\n", idx);
988 if (!CryptEnumProviders(idx, NULL, 0, ptype, NULL, &len))
989 {
990 err = GetLastError();
991 if (err == ERROR_NO_MORE_ITEMS)
992 return 2;
993 CAPIerr(CAPI_F_CAPI_GET_PROVNAME, CAPI_R_CRYPTENUMPROVIDERS_ERROR);
994 capi_adderror(err);
995 return 0;
996 }
997 name = OPENSSL_malloc(len);
998 if (!CryptEnumProviders(idx, NULL, 0, ptype, name, &len))
999 {
1000 err = GetLastError();
1001 if (err == ERROR_NO_MORE_ITEMS)
1002 return 2;
1003 CAPIerr(CAPI_F_CAPI_GET_PROVNAME, CAPI_R_CRYPTENUMPROVIDERS_ERROR);
1004 capi_adderror(err);
1005 return 0;
1006 }
1007 *pname = name;
1008 CAPI_trace(ctx, "capi_get_provname, returned name=%s, type=%d\n", name, *ptype);
1009
1010 return 1;
1011 }
1012
1013static int capi_list_providers(CAPI_CTX *ctx, BIO *out)
1014 {
1015 DWORD idx, ptype;
1016 int ret;
1017 LPTSTR provname = NULL;
1018 CAPI_trace(ctx, "capi_list_providers\n");
1019 BIO_printf(out, "Available CSPs:\n");
1020 for(idx = 0; ; idx++)
1021 {
1022 ret = capi_get_provname(ctx, &provname, &ptype, idx);
1023 if (ret == 2)
1024 break;
1025 if (ret == 0)
1026 break;
1027 BIO_printf(out, "%d. %s, type %d\n", idx, provname, ptype);
1028 OPENSSL_free(provname);
1029 }
1030 return 1;
1031 }
1032
1033static int capi_list_containers(CAPI_CTX *ctx, BIO *out)
1034 {
1035 int ret = 1;
1036 HCRYPTPROV hprov;
1037 DWORD err, idx, flags, buflen = 0, clen;
1038 LPSTR cname;
1039 CAPI_trace(ctx, "Listing containers CSP=%s, type = %d\n", ctx->cspname, ctx->csptype);
1040 if (!CryptAcquireContext(&hprov, NULL, ctx->cspname, ctx->csptype, CRYPT_VERIFYCONTEXT))
1041 {
1042 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, CAPI_R_CRYPTACQUIRECONTEXT_ERROR);
1043 capi_addlasterror();
1044 return 0;
1045 }
1046 if (!CryptGetProvParam(hprov, PP_ENUMCONTAINERS, NULL, &buflen, CRYPT_FIRST))
1047 {
1048 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, CAPI_R_ENUMCONTAINERS_ERROR);
1049 capi_addlasterror();
1050 return 0;
1051 }
1052 CAPI_trace(ctx, "Got max container len %d\n", buflen);
1053 if (buflen == 0)
1054 buflen = 1024;
1055 cname = OPENSSL_malloc(buflen);
1056 if (!cname)
1057 {
1058 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, ERR_R_MALLOC_FAILURE);
1059 goto err;
1060 }
1061
1062 for (idx = 0;;idx++)
1063 {
2aa2a577
DSH
1064 clen = buflen;
1065 cname[0] = 0;
1066
1067 if (idx == 0)
1068 flags = CRYPT_FIRST;
1069 else
1070 flags = 0;
1071 if(!CryptGetProvParam(hprov, PP_ENUMCONTAINERS, cname, &clen, flags))
1072 {
1073 err = GetLastError();
1074 if (err == ERROR_NO_MORE_ITEMS)
7a18ecb2 1075 goto done;
2aa2a577
DSH
1076 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, CAPI_R_ENUMCONTAINERS_ERROR);
1077 capi_adderror(err);
1078 goto err;
1079 }
1080 CAPI_trace(ctx, "Container name %s, len=%d, index=%d, flags=%d\n", cname, clen, idx, flags);
1081 if (!cname[0] && (clen == buflen))
1082 {
1083 CAPI_trace(ctx, "Enumerate bug: using workaround\n");
1084 goto done;
1085 }
1086 BIO_printf(out, "%d. %s\n", idx, cname);
7a18ecb2
DSH
1087 }
1088 err:
1089
1090 ret = 0;
1091
1092 done:
1093 if (cname)
1094 OPENSSL_free(cname);
1095 CryptReleaseContext(hprov, 0);
1096
1097 return ret;
1098 }
1099
1100CRYPT_KEY_PROV_INFO *capi_get_prov_info(CAPI_CTX *ctx, PCCERT_CONTEXT cert)
1101 {
1102 DWORD len;
1103 CRYPT_KEY_PROV_INFO *pinfo;
1104
1105 if(!CertGetCertificateContextProperty(cert, CERT_KEY_PROV_INFO_PROP_ID, NULL, &len))
1106 return NULL;
1107 pinfo = OPENSSL_malloc(len);
1108 if (!pinfo)
1109 {
1110 CAPIerr(CAPI_F_CAPI_GET_PROV_INFO, ERR_R_MALLOC_FAILURE);
1111 return NULL;
1112 }
1113 if(!CertGetCertificateContextProperty(cert, CERT_KEY_PROV_INFO_PROP_ID, pinfo, &len))
1114 {
1115 CAPIerr(CAPI_F_CAPI_GET_PROV_INFO, CAPI_R_ERROR_GETTING_KEY_PROVIDER_INFO);
1116 capi_addlasterror();
1117 OPENSSL_free(pinfo);
1118 return NULL;
1119 }
1120 return pinfo;
1121 }
1122
1123static void capi_dump_prov_info(CAPI_CTX *ctx, BIO *out, CRYPT_KEY_PROV_INFO *pinfo)
1124 {
1125 char *provname = NULL, *contname = NULL;
1126 if (!pinfo)
1127 {
1128 BIO_printf(out, " No Private Key\n");
1129 return;
1130 }
1131 provname = wide_to_asc(pinfo->pwszProvName);
1132 contname = wide_to_asc(pinfo->pwszContainerName);
1133 if (!provname || !contname)
1134 goto err;
1135
1136 BIO_printf(out, " Private Key Info:\n");
1137 BIO_printf(out, " Provider Name: %s, Provider Type %d\n", provname, pinfo->dwProvType);
1138 BIO_printf(out, " Container Name: %s, Key Type %d\n", contname, pinfo->dwKeySpec);
1139 err:
1140 if (provname)
1141 OPENSSL_free(provname);
1142 if (contname)
1143 OPENSSL_free(contname);
1144 }
1145
1146char * capi_cert_get_fname(CAPI_CTX *ctx, PCCERT_CONTEXT cert)
1147 {
1148 LPWSTR wfname;
1149 DWORD dlen;
1150
1151 CAPI_trace(ctx, "capi_cert_get_fname\n");
1152 if (!CertGetCertificateContextProperty(cert, CERT_FRIENDLY_NAME_PROP_ID, NULL, &dlen))
1153 return NULL;
1154 wfname = OPENSSL_malloc(dlen);
1155 if (CertGetCertificateContextProperty(cert, CERT_FRIENDLY_NAME_PROP_ID, wfname, &dlen))
1156 {
1157 char *fname = wide_to_asc(wfname);
1158 OPENSSL_free(wfname);
1159 return fname;
1160 }
1161 CAPIerr(CAPI_F_CAPI_CERT_GET_FNAME, CAPI_R_ERROR_GETTING_FRIENDLY_NAME);
1162 capi_addlasterror();
1163
1164 OPENSSL_free(wfname);
1165 return NULL;
1166 }
1167
1168
1169void capi_dump_cert(CAPI_CTX *ctx, BIO *out, PCCERT_CONTEXT cert)
1170 {
1171 X509 *x;
1172 unsigned char *p;
1173 unsigned long flags = ctx->dump_flags;
1174 if (flags & CAPI_DMP_FNAME)
1175 {
1176 char *fname;
1177 fname = capi_cert_get_fname(ctx, cert);
1178 if (fname)
1179 {
1180 BIO_printf(out, " Friendly Name \"%s\"\n", fname);
1181 OPENSSL_free(fname);
1182 }
1183 else
1184 BIO_printf(out, " <No Friendly Name>\n");
1185 }
1186
1187 p = cert->pbCertEncoded;
1188 x = d2i_X509(NULL, &p, cert->cbCertEncoded);
1189 if (!x)
1190 BIO_printf(out, " <Can't parse certificate>\n");
1191 if (flags & CAPI_DMP_SUMMARY)
1192 {
1193 BIO_printf(out, " Subject: ");
1194 X509_NAME_print_ex(out, X509_get_subject_name(x), 0, XN_FLAG_ONELINE);
1195 BIO_printf(out, "\n Issuer: ");
1196 X509_NAME_print_ex(out, X509_get_issuer_name(x), 0, XN_FLAG_ONELINE);
1197 BIO_printf(out, "\n");
1198 }
1199 if (flags & CAPI_DMP_FULL)
1200 X509_print_ex(out, x, XN_FLAG_ONELINE,0);
1201
1202 if (flags & CAPI_DMP_PKEYINFO)
1203 {
1204 CRYPT_KEY_PROV_INFO *pinfo;
1205 pinfo = capi_get_prov_info(ctx, cert);
1206 capi_dump_prov_info(ctx, out, pinfo);
1207 if (pinfo)
1208 OPENSSL_free(pinfo);
1209 }
1210
1211 if (flags & CAPI_DMP_PEM)
1212 PEM_write_bio_X509(out, x);
1213 X509_free(x);
1214 }
1215
1216HCERTSTORE capi_open_store(CAPI_CTX *ctx, char *storename)
1217 {
1218 HCERTSTORE hstore;
1219
1220 if (!storename)
1221 storename = ctx->storename;
1222 if (!storename)
1223 storename = "MY";
1224 CAPI_trace(ctx, "Opening certificate store %s\n", storename);
1225
1226 hstore = CertOpenSystemStore(0, storename);
1227 if (!hstore)
1228 {
1229 CAPIerr(CAPI_F_CAPI_OPEN_STORE, CAPI_R_ERROR_OPENING_STORE);
1230 capi_addlasterror();
1231 }
1232 return hstore;
1233 }
1234
1235int capi_list_certs(CAPI_CTX *ctx, BIO *out, char *id)
1236 {
1237 char *storename;
1238 int idx;
1239 int ret = 1;
1240 HCERTSTORE hstore;
1241 PCCERT_CONTEXT cert = NULL;
1242
1243 storename = ctx->storename;
1244 if (!storename)
1245 storename = "MY";
1246 CAPI_trace(ctx, "Listing certs for store %s\n", storename);
1247
1248 hstore = capi_open_store(ctx, storename);
1249 if (!hstore)
1250 return 0;
1251 if (id)
1252 {
1253 cert = capi_find_cert(ctx, id, hstore);
1254 if (!cert)
1255 {
1256 ret = 0;
1257 goto err;
1258 }
1259 capi_dump_cert(ctx, out, cert);
1260 CertFreeCertificateContext(cert);
1261 }
1262 else
1263 {
1264 for(idx = 0;;idx++)
1265 {
1266 LPWSTR fname = NULL;
1267 cert = CertEnumCertificatesInStore(hstore, cert);
1268 if (!cert)
1269 break;
1270 BIO_printf(out, "Certificate %d\n", idx);
1271 capi_dump_cert(ctx, out, cert);
1272 }
1273 }
1274 err:
1275 CertCloseStore(hstore, 0);
1276 return ret;
1277 }
1278
1279static PCCERT_CONTEXT capi_find_cert(CAPI_CTX *ctx, const char *id, HCERTSTORE hstore)
1280 {
1281 PCCERT_CONTEXT cert = NULL;
1282 char *fname = NULL;
1283 int match;
1284 switch(ctx->lookup_method)
1285 {
1286 case CAPI_LU_SUBSTR:
2aa2a577
DSH
1287 return CertFindCertificateInStore(hstore,
1288 X509_ASN_ENCODING, 0,
1289 CERT_FIND_SUBJECT_STR_A, id, NULL);
7a18ecb2
DSH
1290 case CAPI_LU_FNAME:
1291 for(;;)
1292 {
1293 cert = CertEnumCertificatesInStore(hstore, cert);
1294 if (!cert)
1295 return NULL;
1296 fname = capi_cert_get_fname(ctx, cert);
1297 if (fname)
1298 {
1299 if (strcmp(fname, id))
1300 match = 0;
1301 else
1302 match = 1;
1303 OPENSSL_free(fname);
1304 if (match)
1305 return cert;
1306 }
1307 }
1308 default:
1309 return NULL;
1310 }
1311 }
1312
1313static CAPI_KEY *capi_get_key(CAPI_CTX *ctx, const char *contname, char *provname, DWORD ptype, DWORD keyspec)
1314 {
1315 CAPI_KEY *key;
1316 key = OPENSSL_malloc(sizeof(CAPI_KEY));
1317 CAPI_trace(ctx, "capi_get_key, contname=%s, provname=%s, type=%d\n",
2aa2a577 1318 contname, provname, ptype);
7a18ecb2
DSH
1319 if (!CryptAcquireContext(&key->hprov, contname, provname, ptype, 0))
1320 {
1321 CAPIerr(CAPI_F_CAPI_GET_KEY, CAPI_R_CRYPTACQUIRECONTEXT_ERROR);
1322 capi_addlasterror();
1323 goto err;
1324 }
1325 if (!CryptGetUserKey(key->hprov, keyspec, &key->key))
1326 {
1327 CAPIerr(CAPI_F_CAPI_GET_KEY, CAPI_R_GETUSERKEY_ERROR);
1328 capi_addlasterror();
1329 CryptReleaseContext(key->hprov, 0);
1330 goto err;
1331 }
4be0a5d4 1332 key->keyspec = keyspec;
7a18ecb2
DSH
1333 return key;
1334
1335 err:
1336 OPENSSL_free(key);
1337 return NULL;
1338 }
1339
1340static CAPI_KEY *capi_get_cert_key(CAPI_CTX *ctx, PCCERT_CONTEXT cert)
1341 {
1342 CAPI_KEY *key;
1343 CRYPT_KEY_PROV_INFO *pinfo = NULL;
1344 char *provname = NULL, *contname = NULL;
1345 pinfo = capi_get_prov_info(ctx, cert);
1346 if (!pinfo)
1347 goto err;
1348 provname = wide_to_asc(pinfo->pwszProvName);
1349 contname = wide_to_asc(pinfo->pwszContainerName);
1350 if (!provname || !contname)
1351 return 0;
1352
2aa2a577
DSH
1353 key = capi_get_key(ctx, contname, provname,
1354 pinfo->dwProvType, pinfo->dwKeySpec);
7a18ecb2
DSH
1355
1356 err:
1357 if (pinfo)
1358 OPENSSL_free(pinfo);
1359 if (provname)
1360 OPENSSL_free(provname);
1361 if (contname)
1362 OPENSSL_free(contname);
1363 return key;
1364 }
1365
1366CAPI_KEY *capi_find_key(CAPI_CTX *ctx, const char *id)
1367 {
1368 PCCERT_CONTEXT cert;
1369 HCERTSTORE hstore;
1370 CAPI_KEY *key = NULL;
1371 switch (ctx->lookup_method)
1372 {
1373 case CAPI_LU_SUBSTR:
1374 case CAPI_LU_FNAME:
1375 hstore = capi_open_store(ctx, NULL);
1376 if (!hstore)
1377 return NULL;
1378 cert = capi_find_cert(ctx, id, hstore);
1379 if (cert)
1380 {
1381 key = capi_get_cert_key(ctx, cert);
1382 CertFreeCertificateContext(cert);
1383 }
1384 CertCloseStore(hstore, 0);
1385 break;
1386
1387 case CAPI_LU_CONTNAME:
2aa2a577
DSH
1388 key = capi_get_key(ctx, id, ctx->cspname, ctx->csptype,
1389 ctx->keytype);
7a18ecb2
DSH
1390 break;
1391 }
1392
1393 return key;
1394 }
1395
1396void capi_free_key(CAPI_KEY *key)
1397 {
1398 if (!key)
1399 return;
1400 CryptDestroyKey(key->key);
1401 CryptReleaseContext(key->hprov, 0);
1402 OPENSSL_free(key);
1403 }
1404
1405
1406/* Initialize a CAPI_CTX structure */
1407
1408static CAPI_CTX *capi_ctx_new()
1409 {
1410 CAPI_CTX *ctx;
1411 ctx = OPENSSL_malloc(sizeof(CAPI_CTX));
1412 if (!ctx)
1413 {
1414 CAPIerr(CAPI_F_CAPI_CTX_NEW, ERR_R_MALLOC_FAILURE);
1415 return NULL;
1416 }
1417 ctx->cspname = NULL;
1418 ctx->csptype = PROV_RSA_FULL;
1419 ctx->dump_flags = CAPI_DMP_SUMMARY|CAPI_DMP_FNAME;
1420 ctx->keytype = AT_KEYEXCHANGE;
1421 ctx->storename = NULL;
b3c8dd4e 1422 ctx->ssl_client_store = NULL;
7a18ecb2
DSH
1423 ctx->lookup_method = CAPI_LU_SUBSTR;
1424 ctx->debug_level = 0;
1425 ctx->debug_file = NULL;
1426 return ctx;
1427 }
1428
1429static void capi_ctx_free(CAPI_CTX *ctx)
1430 {
1431 CAPI_trace(ctx, "Calling capi_ctx_free with %lx\n", ctx);
1432 if (!ctx)
1433 return;
1434 if (ctx->cspname)
1435 OPENSSL_free(ctx->cspname);
1436 if (ctx->debug_file)
1437 OPENSSL_free(ctx->debug_file);
1438 if (ctx->storename)
1439 OPENSSL_free(ctx->storename);
b3c8dd4e
DSH
1440 if (ctx->ssl_client_store)
1441 OPENSSL_free(ctx->ssl_client_store);
7a18ecb2
DSH
1442 OPENSSL_free(ctx);
1443 }
1444
1445static int capi_ctx_set_provname(CAPI_CTX *ctx, LPSTR pname, DWORD type, int check)
1446 {
1447 CAPI_trace(ctx, "capi_ctx_set_provname, name=%s, type=%d\n", pname, type);
1448 if (check)
1449 {
1450 HCRYPTPROV hprov;
2aa2a577
DSH
1451 if (!CryptAcquireContext(&hprov, NULL, pname, type,
1452 CRYPT_VERIFYCONTEXT))
7a18ecb2
DSH
1453 {
1454 CAPIerr(CAPI_F_CAPI_CTX_SET_PROVNAME, CAPI_R_CRYPTACQUIRECONTEXT_ERROR);
1455 capi_addlasterror();
1456 return 0;
1457 }
1458 CryptReleaseContext(hprov, 0);
1459 }
1460 ctx->cspname = BUF_strdup(pname);
1461 ctx->csptype = type;
1462 return 1;
1463 }
1464
1465static int capi_ctx_set_provname_idx(CAPI_CTX *ctx, int idx)
1466 {
1467 LPSTR pname;
1468 DWORD type;
1469 if (capi_get_provname(ctx, &pname, &type, idx) != 1)
1470 return 0;
1471 return capi_ctx_set_provname(ctx, pname, type, 0);
1472 }
1473
b3c8dd4e
DSH
1474static int cert_issuer_match(STACK_OF(X509_NAME) *ca_dn, X509 *x)
1475 {
1476 int i;
1477 X509_NAME *nm;
1478 for (i = 0; i < sk_X509_NAME_num(ca_dn); i++)
1479 {
1480 nm = sk_X509_NAME_value(ca_dn, i);
1481 if (!X509_NAME_cmp(nm, X509_get_issuer_name(x)))
1482 return 1;
1483 }
1484 return 0;
1485 }
1486
1487static int capi_load_ssl_client_cert(ENGINE *e, SSL *ssl,
1488 STACK_OF(X509_NAME) *ca_dn, X509 **pcert, EVP_PKEY **pkey,
1489 STACK_OF(X509) **pother, UI_METHOD *ui_method, void *callback_data)
1490 {
1491#if 0
1492 /* For now just one matching key/cert */
1493 STACK_OF(X509) *certs = NULL;
1494 STACK_OF(EVP_PKEY) *keys = NULL;
1495#endif
1496 X509 *x;
1497 EVP_PKEY *pk;
1498 char *storename;
1499 const char *p;
1500 int i;
1501 HCERTSTORE hstore;
1502 PCCERT_CONTEXT cert = NULL;
1503 CAPI_CTX *ctx;
1504 ctx = ENGINE_get_ex_data(e, capi_idx);
1505
1506 *pcert = NULL;
1507 *pkey = NULL;
1508
1509 storename = ctx->ssl_client_store;
1510 if (!storename)
1511 storename = "MY";
1512
1513 hstore = capi_open_store(ctx, storename);
1514 if (!hstore)
1515 return 0;
1516 /* Enumerate all certificates looking for a match */
1517 for(i = 0;!*pcert;i++)
1518 {
1519 cert = CertEnumCertificatesInStore(hstore, cert);
1520 if (!cert)
1521 break;
1522 p = cert->pbCertEncoded;
1523 x = d2i_X509(NULL, &p, cert->cbCertEncoded);
1524 if (!x)
1525 {
1526 CAPI_trace(ctx, "Can't Parse Certificate %d\n", i);
1527 continue;
1528 }
1529 if (cert_issuer_match(ca_dn, x))
1530 {
1531 CAPI_KEY *key = capi_get_cert_key(ctx, cert);
1532 if (!key)
1533 continue;
1534 pk = capi_get_pkey(e, key);
1535 if (!pk)
1536 {
1537 capi_free_key(key);
1538 continue;
1539 }
1540 *pcert = x;
1541 *pkey = pk;
1542 }
1543 else
1544 X509_free(x);
1545
1546 }
1547
1548 if (cert)
1549 CertFreeCertificateContext(cert);
1550
1551 if (*pcert)
1552 return 1;
1553 else
1554 return 0;
1555
1556 }
1557
7a18ecb2
DSH
1558#endif
1559#endif