]>
Commit | Line | Data |
---|---|---|
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> | |
57 | #include <openssl/crypto.h> | |
58 | #include <openssl/buffer.h> | |
7a18ecb2 | 59 | #include <openssl/bn.h> |
7a18ecb2 DSH |
60 | |
61 | #ifdef OPENSSL_SYS_WIN32 | |
62 | #ifndef OPENSSL_NO_CAPIENG | |
63 | ||
d4f0339c | 64 | #include <openssl/rsa.h> |
a0f3679b | 65 | |
f79262e9 | 66 | #include <windows.h> |
a0f3679b DSH |
67 | |
68 | #ifndef _WIN32_WINNT | |
69 | #define _WIN32_WINNT 0x0400 | |
70 | #endif | |
71 | ||
f79262e9 | 72 | #include <wincrypt.h> |
7a18ecb2 | 73 | |
f87e3078 AP |
74 | /* |
75 | * This module uses several "new" interfaces, among which is | |
76 | * CertGetCertificateContextProperty. CERT_KEY_PROV_INFO_PROP_ID is | |
77 | * one of possible values you can pass to function in question. By | |
78 | * checking if it's defined we can see if wincrypt.h and accompanying | |
6b02f9fa DSH |
79 | * crypt32.lib are in shape. The native MingW32 headers up to and |
80 | * including __W32API_VERSION 3.14 lack of struct DSSPUBKEY and the | |
81 | * defines CERT_STORE_PROV_SYSTEM_A and CERT_STORE_READONLY_FLAG, | |
82 | * so we check for these too and avoid compiling. | |
83 | * Yes, it's rather "weak" test and if compilation fails, | |
84 | * then re-configure with -DOPENSSL_NO_CAPIENG. | |
f87e3078 | 85 | */ |
6b02f9fa DSH |
86 | #if defined(CERT_KEY_PROV_INFO_PROP_ID) && \ |
87 | defined(CERT_STORE_PROV_SYSTEM_A) && \ | |
88 | defined(CERT_STORE_READONLY_FLAG) | |
f87e3078 AP |
89 | # define __COMPILE_CAPIENG |
90 | #endif /* CERT_KEY_PROV_INFO_PROP_ID */ | |
91 | #endif /* OPENSSL_NO_CAPIENG */ | |
92 | #endif /* OPENSSL_SYS_WIN32 */ | |
93 | ||
94 | #ifdef __COMPILE_CAPIENG | |
95 | ||
11f3cee9 DSH |
96 | #undef X509_EXTENSIONS |
97 | #undef X509_CERT_PAIR | |
98 | ||
a0f3679b DSH |
99 | /* Definitions which may be missing from earlier version of headers */ |
100 | #ifndef CERT_STORE_OPEN_EXISTING_FLAG | |
101 | #define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000 | |
102 | #endif | |
103 | ||
104 | #ifndef CERT_STORE_CREATE_NEW_FLAG | |
105 | #define CERT_STORE_CREATE_NEW_FLAG 0x00002000 | |
106 | #endif | |
107 | ||
6c6bdd54 DSH |
108 | #ifndef CERT_SYSTEM_STORE_CURRENT_USER |
109 | #define CERT_SYSTEM_STORE_CURRENT_USER 0x00010000 | |
82f385d7 | 110 | #endif |
6c6bdd54 | 111 | |
11f3cee9 DSH |
112 | #include <openssl/engine.h> |
113 | #include <openssl/pem.h> | |
2836cb38 | 114 | #include <openssl/x509v3.h> |
37cf49a3 | 115 | |
7a18ecb2 DSH |
116 | #include "e_capi_err.h" |
117 | #include "e_capi_err.c" | |
118 | ||
119 | ||
120 | static const char *engine_capi_id = "capi"; | |
121 | static const char *engine_capi_name = "CryptoAPI ENGINE"; | |
122 | ||
123 | typedef struct CAPI_CTX_st CAPI_CTX; | |
124 | typedef struct CAPI_KEY_st CAPI_KEY; | |
125 | ||
126 | static void capi_addlasterror(void); | |
127 | static void capi_adderror(DWORD err); | |
128 | ||
129 | static void CAPI_trace(CAPI_CTX *ctx, char *format, ...); | |
130 | ||
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); | |
135 | ||
136 | static PCCERT_CONTEXT capi_find_cert(CAPI_CTX *ctx, const char *id, HCERTSTORE hstore); | |
137 | ||
138 | CAPI_KEY *capi_find_key(CAPI_CTX *ctx, const char *id); | |
139 | ||
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); | |
149 | ||
150 | static DSA_SIG *capi_dsa_do_sign(const unsigned char *digest, int dlen, | |
151 | DSA *dsa); | |
152 | static int capi_dsa_free(DSA *dsa); | |
b3c8dd4e DSH |
153 | |
154 | static int capi_load_ssl_client_cert(ENGINE *e, SSL *ssl, | |
155 | STACK_OF(X509_NAME) *ca_dn, X509 **pcert, EVP_PKEY **pkey, | |
156 | STACK_OF(X509) **pother, UI_METHOD *ui_method, void *callback_data); | |
1cd504e7 DSH |
157 | |
158 | static int cert_select_simple(ENGINE *e, SSL *ssl, STACK_OF(X509) *certs); | |
159 | #ifdef OPENSSL_CAPIENG_DIALOG | |
160 | static int cert_select_dialog(ENGINE *e, SSL *ssl, STACK_OF(X509) *certs); | |
161 | #endif | |
162 | ||
163 | typedef PCCERT_CONTEXT (WINAPI *CERTDLG)(HCERTSTORE, HWND, LPCWSTR, | |
164 | LPCWSTR, DWORD, DWORD, | |
165 | void *); | |
166 | typedef HWND (WINAPI *GETCONSWIN)(void); | |
167 | ||
7a18ecb2 DSH |
168 | /* This structure contains CAPI ENGINE specific data: |
169 | * it contains various global options and affects how | |
170 | * other functions behave. | |
171 | */ | |
172 | ||
173 | #define CAPI_DBG_TRACE 2 | |
174 | #define CAPI_DBG_ERROR 1 | |
175 | ||
176 | struct CAPI_CTX_st { | |
177 | int debug_level; | |
178 | char *debug_file; | |
179 | /* Parameters to use for container lookup */ | |
180 | DWORD keytype; | |
01483c26 | 181 | LPSTR cspname; |
7a18ecb2 DSH |
182 | DWORD csptype; |
183 | /* Certificate store name to use */ | |
01483c26 AP |
184 | LPSTR storename; |
185 | LPSTR ssl_client_store; | |
46d47828 DSH |
186 | /* System store flags */ |
187 | DWORD store_flags; | |
7a18ecb2 DSH |
188 | |
189 | /* Lookup string meanings in load_private_key */ | |
190 | /* Substring of subject: uses "storename" */ | |
0b368578 | 191 | #define CAPI_LU_SUBSTR 1 |
7a18ecb2 | 192 | /* Friendly name: uses storename */ |
0b368578 | 193 | #define CAPI_LU_FNAME 2 |
7a18ecb2 | 194 | /* Container name: uses cspname, keytype */ |
0b368578 | 195 | #define CAPI_LU_CONTNAME 3 |
7a18ecb2 DSH |
196 | int lookup_method; |
197 | /* Info to dump with dumpcerts option */ | |
198 | /* Issuer and serial name strings */ | |
199 | #define CAPI_DMP_SUMMARY 0x1 | |
200 | /* Friendly name */ | |
201 | #define CAPI_DMP_FNAME 0x2 | |
202 | /* Full X509_print dump */ | |
203 | #define CAPI_DMP_FULL 0x4 | |
204 | /* Dump PEM format certificate */ | |
205 | #define CAPI_DMP_PEM 0x8 | |
206 | /* Dump pseudo key (if possible) */ | |
207 | #define CAPI_DMP_PSKEY 0x10 | |
208 | /* Dump key info (if possible) */ | |
209 | #define CAPI_DMP_PKEYINFO 0x20 | |
210 | ||
211 | DWORD dump_flags; | |
1cd504e7 DSH |
212 | int (*client_cert_select)(ENGINE *e, SSL *ssl, STACK_OF(X509) *certs); |
213 | ||
214 | CERTDLG certselectdlg; | |
215 | GETCONSWIN getconswindow; | |
7a18ecb2 DSH |
216 | }; |
217 | ||
218 | ||
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); | |
223 | ||
b3c8dd4e | 224 | #define CAPI_CMD_LIST_CERTS ENGINE_CMD_BASE |
7a18ecb2 DSH |
225 | #define CAPI_CMD_LOOKUP_CERT (ENGINE_CMD_BASE + 1) |
226 | #define CAPI_CMD_DEBUG_LEVEL (ENGINE_CMD_BASE + 2) | |
b3c8dd4e DSH |
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) | |
7a18ecb2 DSH |
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) | |
c621c7e4 | 236 | #define CAPI_CMD_STORE_NAME (ENGINE_CMD_BASE + 12) |
46d47828 | 237 | #define CAPI_CMD_STORE_FLAGS (ENGINE_CMD_BASE + 13) |
7a18ecb2 DSH |
238 | |
239 | static const ENGINE_CMD_DEFN capi_cmd_defns[] = { | |
240 | {CAPI_CMD_LIST_CERTS, | |
241 | "list_certs", | |
242 | "List all certificates in store", | |
243 | ENGINE_CMD_FLAG_NO_INPUT}, | |
244 | {CAPI_CMD_LOOKUP_CERT, | |
245 | "lookup_cert", | |
246 | "Lookup and output certificates", | |
247 | ENGINE_CMD_FLAG_STRING}, | |
248 | {CAPI_CMD_DEBUG_LEVEL, | |
249 | "debug_level", | |
250 | "debug level (1=errors, 2=trace)", | |
251 | ENGINE_CMD_FLAG_NUMERIC}, | |
252 | {CAPI_CMD_DEBUG_FILE, | |
253 | "debug_file", | |
254 | "debugging filename)", | |
255 | ENGINE_CMD_FLAG_STRING}, | |
256 | {CAPI_CMD_KEYTYPE, | |
257 | "key_type", | |
258 | "Key type: 1=AT_KEYEXCHANGE (default), 2=AT_SIGNATURE", | |
259 | ENGINE_CMD_FLAG_NUMERIC}, | |
260 | {CAPI_CMD_LIST_CSPS, | |
261 | "list_csps", | |
262 | "List all CSPs", | |
263 | ENGINE_CMD_FLAG_NO_INPUT}, | |
264 | {CAPI_CMD_SET_CSP_IDX, | |
265 | "csp_idx", | |
266 | "Set CSP by index", | |
267 | ENGINE_CMD_FLAG_NUMERIC}, | |
268 | {CAPI_CMD_SET_CSP_NAME, | |
269 | "csp_name", | |
270 | "Set CSP name, (default CSP used if not specified)", | |
271 | ENGINE_CMD_FLAG_STRING}, | |
272 | {CAPI_CMD_SET_CSP_TYPE, | |
273 | "csp_type", | |
274 | "Set CSP type, (default RSA_PROV_FULL)", | |
275 | ENGINE_CMD_FLAG_NUMERIC}, | |
276 | {CAPI_CMD_LIST_CONTAINERS, | |
277 | "list_containers", | |
278 | "list container names", | |
279 | ENGINE_CMD_FLAG_NO_INPUT}, | |
280 | {CAPI_CMD_LIST_OPTIONS, | |
281 | "list_options", | |
282 | "Set list options (1=summary,2=friendly name, 4=full printout, 8=PEM output, 16=XXX, " | |
283 | "32=private key info)", | |
284 | ENGINE_CMD_FLAG_NUMERIC}, | |
285 | {CAPI_CMD_LOOKUP_METHOD, | |
286 | "lookup_method", | |
287 | "Set key lookup method (1=substring, 2=friendlyname, 3=container name)", | |
288 | ENGINE_CMD_FLAG_NUMERIC}, | |
c621c7e4 DSH |
289 | {CAPI_CMD_STORE_NAME, |
290 | "store_name", | |
291 | "certificate store name, default \"MY\"", | |
292 | ENGINE_CMD_FLAG_STRING}, | |
46d47828 DSH |
293 | {CAPI_CMD_STORE_FLAGS, |
294 | "store_flags", | |
295 | "Certificate store flags: 1 = system store", | |
296 | ENGINE_CMD_FLAG_NUMERIC}, | |
7a18ecb2 DSH |
297 | |
298 | {0, NULL, NULL, 0} | |
299 | }; | |
300 | ||
301 | static int capi_idx = -1; | |
302 | static int rsa_capi_idx = -1; | |
303 | static int dsa_capi_idx = -1; | |
7d537d4f | 304 | static int cert_capi_idx = -1; |
7a18ecb2 DSH |
305 | |
306 | static int capi_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void)) | |
2aa2a577 | 307 | { |
7a18ecb2 DSH |
308 | int ret = 1; |
309 | CAPI_CTX *ctx; | |
310 | BIO *out; | |
311 | if (capi_idx == -1) | |
312 | { | |
313 | CAPIerr(CAPI_F_CAPI_CTRL, CAPI_R_ENGINE_NOT_INITIALIZED); | |
314 | return 0; | |
315 | } | |
316 | ctx = ENGINE_get_ex_data(e, capi_idx); | |
317 | out = BIO_new_fp(stdout, BIO_NOCLOSE); | |
318 | switch (cmd) | |
319 | { | |
320 | case CAPI_CMD_LIST_CSPS: | |
321 | ret = capi_list_providers(ctx, out); | |
322 | break; | |
323 | ||
324 | case CAPI_CMD_LIST_CERTS: | |
325 | ret = capi_list_certs(ctx, out, NULL); | |
326 | break; | |
327 | ||
328 | case CAPI_CMD_LOOKUP_CERT: | |
329 | ret = capi_list_certs(ctx, out, p); | |
330 | break; | |
331 | ||
332 | case CAPI_CMD_LIST_CONTAINERS: | |
333 | ret = capi_list_containers(ctx, out); | |
334 | break; | |
335 | ||
c621c7e4 | 336 | case CAPI_CMD_STORE_NAME: |
953174f4 DSH |
337 | if (ctx->storename) |
338 | OPENSSL_free(ctx->storename); | |
c621c7e4 DSH |
339 | ctx->storename = BUF_strdup(p); |
340 | CAPI_trace(ctx, "Setting store name to %s\n", p); | |
341 | break; | |
342 | ||
46d47828 DSH |
343 | case CAPI_CMD_STORE_FLAGS: |
344 | if (i & 1) | |
345 | { | |
346 | ctx->store_flags |= CERT_SYSTEM_STORE_LOCAL_MACHINE; | |
347 | ctx->store_flags &= ~CERT_SYSTEM_STORE_CURRENT_USER; | |
348 | } | |
349 | else | |
350 | { | |
351 | ctx->store_flags |= CERT_SYSTEM_STORE_CURRENT_USER; | |
352 | ctx->store_flags &= ~CERT_SYSTEM_STORE_LOCAL_MACHINE; | |
353 | } | |
354 | CAPI_trace(ctx, "Setting flags to %d\n", i); | |
355 | break; | |
356 | ||
7a18ecb2 DSH |
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); | |
360 | break; | |
361 | ||
362 | case CAPI_CMD_DEBUG_FILE: | |
363 | ctx->debug_file = BUF_strdup(p); | |
364 | CAPI_trace(ctx, "Setting debug file to %s\n", ctx->debug_file); | |
365 | break; | |
366 | ||
367 | case CAPI_CMD_KEYTYPE: | |
368 | ctx->keytype = i; | |
369 | CAPI_trace(ctx, "Setting key type to %d\n", ctx->keytype); | |
370 | break; | |
371 | ||
372 | case CAPI_CMD_SET_CSP_IDX: | |
373 | ret = capi_ctx_set_provname_idx(ctx, i); | |
374 | break; | |
375 | ||
376 | case CAPI_CMD_LIST_OPTIONS: | |
377 | ctx->dump_flags = i; | |
378 | break; | |
379 | ||
380 | case CAPI_CMD_LOOKUP_METHOD: | |
381 | if (i < 1 || i > 3) | |
382 | { | |
383 | CAPIerr(CAPI_F_CAPI_CTRL, CAPI_R_INVALID_LOOKUP_METHOD); | |
384 | return 0; | |
385 | } | |
386 | ctx->lookup_method = i; | |
387 | break; | |
388 | ||
389 | case CAPI_CMD_SET_CSP_NAME: | |
390 | ret = capi_ctx_set_provname(ctx, p, ctx->csptype, 1); | |
391 | break; | |
392 | ||
393 | case CAPI_CMD_SET_CSP_TYPE: | |
394 | ctx->csptype = i; | |
395 | break; | |
396 | ||
397 | default: | |
398 | CAPIerr(CAPI_F_CAPI_CTRL, CAPI_R_UNKNOWN_COMMAND); | |
399 | ret = 0; | |
400 | } | |
401 | ||
402 | BIO_free(out); | |
403 | return ret; | |
404 | ||
2aa2a577 | 405 | } |
7a18ecb2 DSH |
406 | |
407 | static RSA_METHOD capi_rsa_method = | |
408 | { | |
409 | "CryptoAPI RSA method", | |
410 | 0, /* pub_enc */ | |
411 | 0, /* pub_dec */ | |
412 | capi_rsa_priv_enc, /* priv_enc */ | |
413 | capi_rsa_priv_dec, /* priv_dec */ | |
414 | 0, /* rsa_mod_exp */ | |
415 | 0, /* bn_mod_exp */ | |
416 | 0, /* init */ | |
417 | capi_rsa_free, /* finish */ | |
418 | RSA_FLAG_SIGN_VER, /* flags */ | |
419 | NULL, /* app_data */ | |
420 | capi_rsa_sign, /* rsa_sign */ | |
421 | 0 /* rsa_verify */ | |
422 | }; | |
423 | ||
424 | static DSA_METHOD capi_dsa_method = | |
425 | { | |
426 | "CryptoAPI DSA method", | |
427 | capi_dsa_do_sign, /* dsa_do_sign */ | |
428 | 0, /* dsa_sign_setup */ | |
429 | 0, /* dsa_do_verify */ | |
430 | 0, /* dsa_mod_exp */ | |
431 | 0, /* bn_mod_exp */ | |
432 | 0, /* init */ | |
433 | capi_dsa_free, /* finish */ | |
434 | 0, /* flags */ | |
435 | NULL, /* app_data */ | |
436 | 0, /* dsa_paramgen */ | |
437 | 0 /* dsa_keygen */ | |
438 | }; | |
439 | ||
440 | static int capi_init(ENGINE *e) | |
441 | { | |
442 | CAPI_CTX *ctx; | |
443 | const RSA_METHOD *ossl_rsa_meth; | |
444 | const DSA_METHOD *ossl_dsa_meth; | |
445 | capi_idx = ENGINE_get_ex_new_index(0, NULL, NULL, NULL, 0); | |
7d537d4f | 446 | cert_capi_idx = X509_get_ex_new_index(0, NULL, NULL, NULL, 0); |
7a18ecb2 DSH |
447 | |
448 | ctx = capi_ctx_new(); | |
449 | if (!ctx || (capi_idx < 0)) | |
450 | goto memerr; | |
451 | ||
452 | ENGINE_set_ex_data(e, capi_idx, ctx); | |
453 | /* Setup RSA_METHOD */ | |
454 | rsa_capi_idx = RSA_get_ex_new_index(0, NULL, NULL, NULL, 0); | |
455 | ossl_rsa_meth = RSA_PKCS1_SSLeay(); | |
456 | capi_rsa_method.rsa_pub_enc = ossl_rsa_meth->rsa_pub_enc; | |
457 | capi_rsa_method.rsa_pub_dec = ossl_rsa_meth->rsa_pub_dec; | |
458 | capi_rsa_method.rsa_mod_exp = ossl_rsa_meth->rsa_mod_exp; | |
459 | capi_rsa_method.bn_mod_exp = ossl_rsa_meth->bn_mod_exp; | |
460 | ||
461 | /* Setup DSA Method */ | |
462 | dsa_capi_idx = DSA_get_ex_new_index(0, NULL, NULL, NULL, 0); | |
463 | ossl_dsa_meth = DSA_OpenSSL(); | |
464 | capi_dsa_method.dsa_do_verify = ossl_dsa_meth->dsa_do_verify; | |
465 | capi_dsa_method.dsa_mod_exp = ossl_dsa_meth->dsa_mod_exp; | |
466 | capi_dsa_method.bn_mod_exp = ossl_dsa_meth->bn_mod_exp; | |
467 | ||
1cd504e7 DSH |
468 | #ifdef OPENSSL_CAPIENG_DIALOG |
469 | { | |
470 | HMODULE cryptui = LoadLibrary(TEXT("CRYPTUI.DLL")); | |
01483c26 | 471 | HMODULE kernel = GetModuleHandle(TEXT("KERNEL32.DLL")); |
1cd504e7 DSH |
472 | if (cryptui) |
473 | ctx->certselectdlg = (CERTDLG)GetProcAddress(cryptui, "CryptUIDlgSelectCertificateFromStore"); | |
474 | if (kernel) | |
475 | ctx->getconswindow = (GETCONSWIN)GetProcAddress(kernel, "GetConsoleWindow"); | |
46d47828 | 476 | if (cryptui && !OPENSSL_isservice()) |
1cd504e7 DSH |
477 | ctx->client_cert_select = cert_select_dialog; |
478 | } | |
479 | #endif | |
480 | ||
481 | ||
7a18ecb2 DSH |
482 | return 1; |
483 | ||
484 | memerr: | |
485 | CAPIerr(CAPI_F_CAPI_INIT, ERR_R_MALLOC_FAILURE); | |
486 | return 0; | |
487 | ||
488 | return 1; | |
489 | } | |
490 | ||
491 | static int capi_destroy(ENGINE *e) | |
492 | { | |
7a18ecb2 DSH |
493 | ERR_unload_CAPI_strings(); |
494 | return 1; | |
495 | } | |
496 | ||
497 | static int capi_finish(ENGINE *e) | |
498 | { | |
499 | CAPI_CTX *ctx; | |
500 | ctx = ENGINE_get_ex_data(e, capi_idx); | |
501 | capi_ctx_free(ctx); | |
502 | ENGINE_set_ex_data(e, capi_idx, NULL); | |
503 | return 1; | |
504 | } | |
505 | ||
506 | ||
507 | /* CryptoAPI key application data. This contains | |
508 | * a handle to the private key container (for sign operations) | |
509 | * and a handle to the key (for decrypt operations). | |
510 | */ | |
511 | ||
512 | struct CAPI_KEY_st | |
513 | { | |
7d537d4f | 514 | /* Associated certificate context (if any) */ |
e0f7b872 | 515 | PCCERT_CONTEXT pcert; |
7a18ecb2 DSH |
516 | HCRYPTPROV hprov; |
517 | HCRYPTKEY key; | |
4be0a5d4 | 518 | DWORD keyspec; |
7a18ecb2 DSH |
519 | }; |
520 | ||
521 | static int bind_capi(ENGINE *e) | |
522 | { | |
523 | if (!ENGINE_set_id(e, engine_capi_id) | |
524 | || !ENGINE_set_name(e, engine_capi_name) | |
525 | || !ENGINE_set_init_function(e, capi_init) | |
526 | || !ENGINE_set_finish_function(e, capi_finish) | |
527 | || !ENGINE_set_destroy_function(e, capi_destroy) | |
528 | || !ENGINE_set_RSA(e, &capi_rsa_method) | |
529 | || !ENGINE_set_DSA(e, &capi_dsa_method) | |
530 | || !ENGINE_set_load_privkey_function(e, capi_load_privkey) | |
b3c8dd4e DSH |
531 | || !ENGINE_set_load_ssl_client_cert_function(e, |
532 | capi_load_ssl_client_cert) | |
7a18ecb2 DSH |
533 | || !ENGINE_set_cmd_defns(e, capi_cmd_defns) |
534 | || !ENGINE_set_ctrl_function(e, capi_ctrl)) | |
535 | return 0; | |
536 | ERR_load_CAPI_strings(); | |
537 | ||
538 | return 1; | |
539 | ||
540 | } | |
541 | ||
542 | #ifndef OPENSSL_NO_DYNAMIC_ENGINE | |
543 | static int bind_helper(ENGINE *e, const char *id) | |
544 | { | |
545 | if(id && (strcmp(id, engine_capi_id) != 0)) | |
546 | return 0; | |
547 | if(!bind_capi(e)) | |
548 | return 0; | |
549 | return 1; | |
550 | } | |
551 | IMPLEMENT_DYNAMIC_CHECK_FN() | |
552 | IMPLEMENT_DYNAMIC_BIND_FN(bind_helper) | |
553 | #else | |
554 | static ENGINE *engine_capi(void) | |
555 | { | |
556 | ENGINE *ret = ENGINE_new(); | |
557 | if(!ret) | |
558 | return NULL; | |
559 | if(!bind_capi(ret)) | |
560 | { | |
561 | ENGINE_free(ret); | |
562 | return NULL; | |
563 | } | |
564 | return ret; | |
565 | } | |
566 | ||
567 | void ENGINE_load_capi(void) | |
568 | { | |
569 | /* Copied from eng_[openssl|dyn].c */ | |
570 | ENGINE *toadd = engine_capi(); | |
571 | if(!toadd) return; | |
572 | ENGINE_add(toadd); | |
573 | ENGINE_free(toadd); | |
574 | ERR_clear_error(); | |
575 | } | |
576 | #endif | |
577 | ||
578 | ||
579 | static int lend_tobn(BIGNUM *bn, unsigned char *bin, int binlen) | |
580 | { | |
581 | int i; | |
582 | /* Reverse buffer in place: since this is a keyblob structure | |
583 | * that will be freed up after conversion anyway it doesn't | |
584 | * matter if we change it. | |
585 | */ | |
586 | for(i = 0; i < binlen / 2; i++) | |
587 | { | |
588 | unsigned char c; | |
589 | c = bin[i]; | |
590 | bin[i] = bin[binlen - i - 1]; | |
591 | bin[binlen - i - 1] = c; | |
592 | } | |
593 | ||
594 | if (!BN_bin2bn(bin, binlen, bn)) | |
595 | return 0; | |
596 | return 1; | |
597 | } | |
598 | ||
b3c8dd4e DSH |
599 | /* Given a CAPI_KEY get an EVP_PKEY structure */ |
600 | ||
601 | static EVP_PKEY *capi_get_pkey(ENGINE *eng, CAPI_KEY *key) | |
7a18ecb2 | 602 | { |
7a18ecb2 DSH |
603 | unsigned char *pubkey = NULL; |
604 | DWORD len; | |
605 | BLOBHEADER *bh; | |
606 | RSA *rkey = NULL; | |
607 | DSA *dkey = NULL; | |
b3c8dd4e | 608 | EVP_PKEY *ret = NULL; |
7a18ecb2 DSH |
609 | if (!CryptExportKey(key->key, 0, PUBLICKEYBLOB, 0, NULL, &len)) |
610 | { | |
b3c8dd4e | 611 | CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_PUBKEY_EXPORT_LENGTH_ERROR); |
7a18ecb2 DSH |
612 | capi_addlasterror(); |
613 | return NULL; | |
614 | } | |
615 | ||
616 | pubkey = OPENSSL_malloc(len); | |
617 | ||
618 | if (!pubkey) | |
619 | goto memerr; | |
620 | ||
621 | if (!CryptExportKey(key->key, 0, PUBLICKEYBLOB, 0, pubkey, &len)) | |
622 | { | |
b3c8dd4e | 623 | CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_PUBKEY_EXPORT_ERROR); |
7a18ecb2 DSH |
624 | capi_addlasterror(); |
625 | goto err; | |
626 | } | |
627 | ||
628 | bh = (BLOBHEADER *)pubkey; | |
629 | if (bh->bType != PUBLICKEYBLOB) | |
630 | { | |
b3c8dd4e | 631 | CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_INVALID_PUBLIC_KEY_BLOB); |
7a18ecb2 DSH |
632 | goto err; |
633 | } | |
634 | if (bh->aiKeyAlg == CALG_RSA_SIGN || bh->aiKeyAlg == CALG_RSA_KEYX) | |
635 | { | |
636 | RSAPUBKEY *rp; | |
637 | DWORD rsa_modlen; | |
638 | unsigned char *rsa_modulus; | |
639 | rp = (RSAPUBKEY *)(bh + 1); | |
640 | if (rp->magic != 0x31415352) | |
641 | { | |
642 | char magstr[10]; | |
643 | BIO_snprintf(magstr, 10, "%lx", rp->magic); | |
b3c8dd4e | 644 | CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_INVALID_RSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER); |
7a18ecb2 DSH |
645 | ERR_add_error_data(2, "magic=0x", magstr); |
646 | goto err; | |
647 | } | |
648 | rsa_modulus = (unsigned char *)(rp + 1); | |
649 | rkey = RSA_new_method(eng); | |
650 | if (!rkey) | |
651 | goto memerr; | |
652 | ||
653 | rkey->e = BN_new(); | |
654 | rkey->n = BN_new(); | |
655 | ||
656 | if (!rkey->e || !rkey->n) | |
657 | goto memerr; | |
658 | ||
659 | if (!BN_set_word(rkey->e, rp->pubexp)) | |
660 | goto memerr; | |
661 | ||
662 | rsa_modlen = rp->bitlen / 8; | |
663 | if (!lend_tobn(rkey->n, rsa_modulus, rsa_modlen)) | |
664 | goto memerr; | |
665 | ||
666 | RSA_set_ex_data(rkey, rsa_capi_idx, key); | |
667 | ||
668 | if (!(ret = EVP_PKEY_new())) | |
669 | goto memerr; | |
670 | ||
671 | EVP_PKEY_assign_RSA(ret, rkey); | |
672 | rkey = NULL; | |
673 | ||
674 | } | |
675 | else if (bh->aiKeyAlg == CALG_DSS_SIGN) | |
676 | { | |
677 | DSSPUBKEY *dp; | |
678 | DWORD dsa_plen; | |
679 | unsigned char *btmp; | |
680 | dp = (DSSPUBKEY *)(bh + 1); | |
681 | if (dp->magic != 0x31535344) | |
682 | { | |
683 | char magstr[10]; | |
684 | BIO_snprintf(magstr, 10, "%lx", dp->magic); | |
b3c8dd4e | 685 | CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_INVALID_DSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER); |
7a18ecb2 DSH |
686 | ERR_add_error_data(2, "magic=0x", magstr); |
687 | goto err; | |
688 | } | |
689 | dsa_plen = dp->bitlen / 8; | |
690 | btmp = (unsigned char *)(dp + 1); | |
691 | dkey = DSA_new_method(eng); | |
692 | if (!dkey) | |
693 | goto memerr; | |
694 | dkey->p = BN_new(); | |
695 | dkey->q = BN_new(); | |
696 | dkey->g = BN_new(); | |
697 | dkey->pub_key = BN_new(); | |
698 | if (!dkey->p || !dkey->q || !dkey->g || !dkey->pub_key) | |
699 | goto memerr; | |
700 | if (!lend_tobn(dkey->p, btmp, dsa_plen)) | |
701 | goto memerr; | |
702 | btmp += dsa_plen; | |
703 | if (!lend_tobn(dkey->q, btmp, 20)) | |
704 | goto memerr; | |
705 | btmp += 20; | |
706 | if (!lend_tobn(dkey->g, btmp, dsa_plen)) | |
707 | goto memerr; | |
708 | btmp += dsa_plen; | |
709 | if (!lend_tobn(dkey->pub_key, btmp, dsa_plen)) | |
710 | goto memerr; | |
711 | btmp += dsa_plen; | |
712 | ||
713 | DSA_set_ex_data(dkey, dsa_capi_idx, key); | |
714 | ||
715 | if (!(ret = EVP_PKEY_new())) | |
716 | goto memerr; | |
717 | ||
718 | EVP_PKEY_assign_DSA(ret, dkey); | |
719 | dkey = NULL; | |
720 | } | |
721 | else | |
722 | { | |
723 | char algstr[10]; | |
724 | BIO_snprintf(algstr, 10, "%lx", bh->aiKeyAlg); | |
b3c8dd4e | 725 | CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_UNSUPPORTED_PUBLIC_KEY_ALGORITHM); |
7a18ecb2 DSH |
726 | ERR_add_error_data(2, "aiKeyAlg=0x", algstr); |
727 | goto err; | |
728 | } | |
729 | ||
b3c8dd4e | 730 | |
7a18ecb2 DSH |
731 | err: |
732 | if (pubkey) | |
733 | OPENSSL_free(pubkey); | |
734 | if (!ret) | |
735 | { | |
736 | if (rkey) | |
737 | RSA_free(rkey); | |
738 | if (dkey) | |
739 | DSA_free(dkey); | |
7a18ecb2 DSH |
740 | } |
741 | ||
742 | return ret; | |
743 | ||
744 | memerr: | |
9be8035b | 745 | CAPIerr(CAPI_F_CAPI_GET_PKEY, ERR_R_MALLOC_FAILURE); |
7a18ecb2 DSH |
746 | goto err; |
747 | ||
748 | } | |
749 | ||
b3c8dd4e DSH |
750 | static EVP_PKEY *capi_load_privkey(ENGINE *eng, const char *key_id, |
751 | UI_METHOD *ui_method, void *callback_data) | |
752 | { | |
753 | CAPI_CTX *ctx; | |
754 | CAPI_KEY *key; | |
755 | EVP_PKEY *ret; | |
756 | ctx = ENGINE_get_ex_data(eng, capi_idx); | |
757 | ||
758 | if (!ctx) | |
759 | { | |
760 | CAPIerr(CAPI_F_CAPI_LOAD_PRIVKEY, CAPI_R_CANT_FIND_CAPI_CONTEXT); | |
761 | return NULL; | |
762 | } | |
763 | ||
764 | key = capi_find_key(ctx, key_id); | |
765 | ||
766 | if (!key) | |
767 | return NULL; | |
768 | ||
769 | ret = capi_get_pkey(eng, key); | |
770 | ||
771 | if (!ret) | |
772 | capi_free_key(key); | |
773 | return ret; | |
774 | ||
775 | } | |
776 | ||
7a18ecb2 DSH |
777 | /* CryptoAPI RSA operations */ |
778 | ||
779 | int capi_rsa_priv_enc(int flen, const unsigned char *from, | |
780 | unsigned char *to, RSA *rsa, int padding) | |
781 | { | |
782 | CAPIerr(CAPI_F_CAPI_RSA_PRIV_ENC, CAPI_R_FUNCTION_NOT_SUPPORTED); | |
783 | return -1; | |
784 | } | |
785 | ||
786 | int capi_rsa_sign(int dtype, const unsigned char *m, unsigned int m_len, | |
787 | unsigned char *sigret, unsigned int *siglen, const RSA *rsa) | |
788 | { | |
789 | ALG_ID alg; | |
790 | HCRYPTHASH hash; | |
791 | DWORD slen; | |
792 | unsigned int i; | |
793 | int ret = -1; | |
794 | CAPI_KEY *capi_key; | |
795 | CAPI_CTX *ctx; | |
796 | ||
797 | ctx = ENGINE_get_ex_data(rsa->engine, capi_idx); | |
798 | ||
799 | CAPI_trace(ctx, "Called CAPI_rsa_sign()\n"); | |
800 | ||
801 | capi_key = RSA_get_ex_data(rsa, rsa_capi_idx); | |
802 | if (!capi_key) | |
803 | { | |
804 | CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_CANT_GET_KEY); | |
805 | return -1; | |
806 | } | |
807 | /* Convert the signature type to a CryptoAPI algorithm ID */ | |
2aa2a577 DSH |
808 | switch(dtype) |
809 | { | |
7a18ecb2 DSH |
810 | case NID_sha1: |
811 | alg = CALG_SHA1; | |
812 | break; | |
813 | ||
814 | case NID_md5: | |
815 | alg = CALG_MD5; | |
816 | break; | |
817 | ||
818 | case NID_md5_sha1: | |
819 | alg = CALG_SSL3_SHAMD5; | |
820 | break; | |
821 | default: | |
822 | { | |
823 | char algstr[10]; | |
824 | BIO_snprintf(algstr, 10, "%lx", dtype); | |
825 | CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_UNSUPPORTED_ALGORITHM_NID); | |
826 | ERR_add_error_data(2, "NID=0x", algstr); | |
827 | return -1; | |
828 | } | |
829 | } | |
830 | ||
831 | ||
832 | ||
833 | /* Create the hash object */ | |
2aa2a577 DSH |
834 | if(!CryptCreateHash(capi_key->hprov, alg, 0, 0, &hash)) |
835 | { | |
7a18ecb2 DSH |
836 | CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_CANT_CREATE_HASH_OBJECT); |
837 | capi_addlasterror(); | |
838 | return -1; | |
2aa2a577 | 839 | } |
7a18ecb2 DSH |
840 | /* Set the hash value to the value passed */ |
841 | ||
2aa2a577 DSH |
842 | if(!CryptSetHashParam(hash, HP_HASHVAL, (unsigned char *)m, 0)) |
843 | { | |
7a18ecb2 DSH |
844 | CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_CANT_SET_HASH_VALUE); |
845 | capi_addlasterror(); | |
846 | goto err; | |
2aa2a577 | 847 | } |
7a18ecb2 DSH |
848 | |
849 | ||
850 | /* Finally sign it */ | |
851 | slen = RSA_size(rsa); | |
01483c26 | 852 | if(!CryptSignHashA(hash, capi_key->keyspec, NULL, 0, sigret, &slen)) |
2aa2a577 | 853 | { |
7a18ecb2 DSH |
854 | CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_ERROR_SIGNING_HASH); |
855 | capi_addlasterror(); | |
856 | goto err; | |
2aa2a577 DSH |
857 | } |
858 | else | |
859 | { | |
7a18ecb2 DSH |
860 | ret = 1; |
861 | /* Inplace byte reversal of signature */ | |
2aa2a577 DSH |
862 | for(i = 0; i < slen / 2; i++) |
863 | { | |
7a18ecb2 DSH |
864 | unsigned char c; |
865 | c = sigret[i]; | |
866 | sigret[i] = sigret[slen - i - 1]; | |
867 | sigret[slen - i - 1] = c; | |
2aa2a577 | 868 | } |
7a18ecb2 | 869 | *siglen = slen; |
2aa2a577 | 870 | } |
7a18ecb2 | 871 | |
7a18ecb2 DSH |
872 | /* Now cleanup */ |
873 | ||
874 | err: | |
875 | CryptDestroyHash(hash); | |
876 | ||
877 | return ret; | |
2aa2a577 | 878 | } |
7a18ecb2 DSH |
879 | |
880 | int capi_rsa_priv_dec(int flen, const unsigned char *from, | |
881 | unsigned char *to, RSA *rsa, int padding) | |
2aa2a577 | 882 | { |
7a18ecb2 DSH |
883 | int i; |
884 | unsigned char *tmpbuf; | |
885 | CAPI_KEY *capi_key; | |
886 | CAPI_CTX *ctx; | |
887 | ctx = ENGINE_get_ex_data(rsa->engine, capi_idx); | |
888 | ||
889 | CAPI_trace(ctx, "Called capi_rsa_priv_dec()\n"); | |
890 | ||
891 | ||
892 | capi_key = RSA_get_ex_data(rsa, rsa_capi_idx); | |
893 | if (!capi_key) | |
894 | { | |
895 | CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, CAPI_R_CANT_GET_KEY); | |
896 | return -1; | |
897 | } | |
898 | ||
899 | if(padding != RSA_PKCS1_PADDING) | |
900 | { | |
901 | char errstr[10]; | |
902 | BIO_snprintf(errstr, 10, "%d", padding); | |
903 | CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, CAPI_R_UNSUPPORTED_PADDING); | |
904 | ERR_add_error_data(2, "padding=", errstr); | |
905 | return -1; | |
906 | } | |
907 | ||
908 | /* Create temp reverse order version of input */ | |
909 | if(!(tmpbuf = OPENSSL_malloc(flen)) ) | |
910 | { | |
911 | CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, ERR_R_MALLOC_FAILURE); | |
912 | return -1; | |
913 | } | |
914 | for(i = 0; i < flen; i++) | |
915 | tmpbuf[flen - i - 1] = from[i]; | |
916 | ||
917 | /* Finally decrypt it */ | |
918 | if(!CryptDecrypt(capi_key->key, 0, TRUE, 0, tmpbuf, &flen)) | |
919 | { | |
920 | CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, CAPI_R_DECRYPT_ERROR); | |
921 | capi_addlasterror(); | |
922 | OPENSSL_free(tmpbuf); | |
923 | return -1; | |
924 | } | |
925 | else memcpy(to, tmpbuf, flen); | |
926 | ||
927 | OPENSSL_free(tmpbuf); | |
928 | ||
929 | return flen; | |
2aa2a577 | 930 | } |
7a18ecb2 DSH |
931 | |
932 | static int capi_rsa_free(RSA *rsa) | |
933 | { | |
934 | CAPI_KEY *capi_key; | |
935 | capi_key = RSA_get_ex_data(rsa, rsa_capi_idx); | |
936 | capi_free_key(capi_key); | |
937 | RSA_set_ex_data(rsa, rsa_capi_idx, 0); | |
938 | return 1; | |
939 | } | |
940 | ||
941 | /* CryptoAPI DSA operations */ | |
942 | ||
943 | static DSA_SIG *capi_dsa_do_sign(const unsigned char *digest, int dlen, | |
944 | DSA *dsa) | |
945 | { | |
946 | HCRYPTHASH hash; | |
947 | DWORD slen; | |
948 | DSA_SIG *ret = NULL; | |
949 | CAPI_KEY *capi_key; | |
950 | CAPI_CTX *ctx; | |
951 | unsigned char csigbuf[40]; | |
952 | ||
953 | ctx = ENGINE_get_ex_data(dsa->engine, capi_idx); | |
954 | ||
955 | CAPI_trace(ctx, "Called CAPI_dsa_do_sign()\n"); | |
956 | ||
957 | capi_key = DSA_get_ex_data(dsa, dsa_capi_idx); | |
958 | ||
959 | if (!capi_key) | |
960 | { | |
961 | CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_CANT_GET_KEY); | |
962 | return NULL; | |
963 | } | |
964 | ||
965 | if (dlen != 20) | |
966 | { | |
967 | CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_INVALID_DIGEST_LENGTH); | |
968 | return NULL; | |
969 | } | |
970 | ||
971 | /* Create the hash object */ | |
2aa2a577 DSH |
972 | if(!CryptCreateHash(capi_key->hprov, CALG_SHA1, 0, 0, &hash)) |
973 | { | |
7a18ecb2 DSH |
974 | CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_CANT_CREATE_HASH_OBJECT); |
975 | capi_addlasterror(); | |
976 | return NULL; | |
2aa2a577 | 977 | } |
7a18ecb2 DSH |
978 | |
979 | /* Set the hash value to the value passed */ | |
2aa2a577 DSH |
980 | if(!CryptSetHashParam(hash, HP_HASHVAL, (unsigned char *)digest, 0)) |
981 | { | |
7a18ecb2 DSH |
982 | CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_CANT_SET_HASH_VALUE); |
983 | capi_addlasterror(); | |
984 | goto err; | |
2aa2a577 | 985 | } |
7a18ecb2 DSH |
986 | |
987 | ||
988 | /* Finally sign it */ | |
989 | slen = sizeof(csigbuf); | |
01483c26 | 990 | if(!CryptSignHashA(hash, capi_key->keyspec, NULL, 0, csigbuf, &slen)) |
7a18ecb2 DSH |
991 | { |
992 | CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_ERROR_SIGNING_HASH); | |
993 | capi_addlasterror(); | |
994 | goto err; | |
995 | } | |
996 | else | |
997 | { | |
998 | ret = DSA_SIG_new(); | |
999 | if (!ret) | |
1000 | goto err; | |
1001 | ret->r = BN_new(); | |
1002 | ret->s = BN_new(); | |
1003 | if (!ret->r || !ret->s) | |
1004 | goto err; | |
1005 | if (!lend_tobn(ret->r, csigbuf, 20) | |
1006 | || !lend_tobn(ret->s, csigbuf + 20, 20)) | |
1007 | { | |
1008 | DSA_SIG_free(ret); | |
1009 | ret = NULL; | |
1010 | goto err; | |
1011 | } | |
1012 | } | |
1013 | ||
1014 | /* Now cleanup */ | |
1015 | ||
1016 | err: | |
1017 | OPENSSL_cleanse(csigbuf, 40); | |
1018 | CryptDestroyHash(hash); | |
1019 | return ret; | |
1020 | } | |
1021 | ||
1022 | static int capi_dsa_free(DSA *dsa) | |
1023 | { | |
1024 | CAPI_KEY *capi_key; | |
1025 | capi_key = DSA_get_ex_data(dsa, dsa_capi_idx); | |
1026 | capi_free_key(capi_key); | |
1027 | DSA_set_ex_data(dsa, dsa_capi_idx, 0); | |
1028 | return 1; | |
1029 | } | |
1030 | ||
1031 | static void capi_vtrace(CAPI_CTX *ctx, int level, char *format, va_list argptr) | |
1032 | { | |
1033 | BIO *out; | |
1034 | ||
1035 | if (!ctx || (ctx->debug_level < level) || (!ctx->debug_file)) | |
1036 | return; | |
1037 | out = BIO_new_file(ctx->debug_file, "a+"); | |
1038 | BIO_vprintf(out, format, argptr); | |
1039 | BIO_free(out); | |
1040 | } | |
1041 | ||
1042 | static void CAPI_trace(CAPI_CTX *ctx, char *format, ...) | |
1043 | { | |
1044 | va_list args; | |
1045 | va_start(args, format); | |
1046 | capi_vtrace(ctx, CAPI_DBG_TRACE, format, args); | |
1047 | va_end(args); | |
1048 | } | |
1049 | ||
1050 | static void capi_addlasterror(void) | |
1051 | { | |
1052 | capi_adderror(GetLastError()); | |
1053 | } | |
1054 | ||
1055 | static void capi_adderror(DWORD err) | |
1056 | { | |
1057 | char errstr[10]; | |
1058 | BIO_snprintf(errstr, 10, "%lX", err); | |
1059 | ERR_add_error_data(2, "Error code= 0x", errstr); | |
1060 | } | |
1061 | ||
1062 | static char *wide_to_asc(LPWSTR wstr) | |
1063 | { | |
1064 | char *str; | |
01483c26 AP |
1065 | int len_0,sz; |
1066 | ||
7a18ecb2 DSH |
1067 | if (!wstr) |
1068 | return NULL; | |
01483c26 AP |
1069 | len_0 = (int)wcslen(wstr)+1; /* WideCharToMultiByte expects int */ |
1070 | sz = WideCharToMultiByte(CP_ACP,0,wstr,len_0,NULL,0,NULL,NULL); | |
1071 | if (!sz) | |
1072 | { | |
1073 | CAPIerr(CAPI_F_WIDE_TO_ASC, CAPI_R_WIN32_ERROR); | |
1074 | return NULL; | |
1075 | } | |
1076 | str = OPENSSL_malloc(sz); | |
7a18ecb2 DSH |
1077 | if (!str) |
1078 | { | |
1079 | CAPIerr(CAPI_F_WIDE_TO_ASC, ERR_R_MALLOC_FAILURE); | |
1080 | return NULL; | |
1081 | } | |
01483c26 AP |
1082 | if (!WideCharToMultiByte(CP_ACP,0,wstr,len_0,str,sz,NULL,NULL)) |
1083 | { | |
1084 | OPENSSL_free(str); | |
1085 | CAPIerr(CAPI_F_WIDE_TO_ASC, CAPI_R_WIN32_ERROR); | |
1086 | return NULL; | |
1087 | } | |
7a18ecb2 DSH |
1088 | return str; |
1089 | } | |
1090 | ||
1091 | static int capi_get_provname(CAPI_CTX *ctx, LPSTR *pname, DWORD *ptype, DWORD idx) | |
1092 | { | |
1093 | LPSTR name; | |
1094 | DWORD len, err; | |
1095 | CAPI_trace(ctx, "capi_get_provname, index=%d\n", idx); | |
01483c26 | 1096 | if (!CryptEnumProvidersA(idx, NULL, 0, ptype, NULL, &len)) |
7a18ecb2 DSH |
1097 | { |
1098 | err = GetLastError(); | |
1099 | if (err == ERROR_NO_MORE_ITEMS) | |
1100 | return 2; | |
1101 | CAPIerr(CAPI_F_CAPI_GET_PROVNAME, CAPI_R_CRYPTENUMPROVIDERS_ERROR); | |
1102 | capi_adderror(err); | |
1103 | return 0; | |
1104 | } | |
1105 | name = OPENSSL_malloc(len); | |
01483c26 | 1106 | if (!CryptEnumProvidersA(idx, NULL, 0, ptype, name, &len)) |
7a18ecb2 DSH |
1107 | { |
1108 | err = GetLastError(); | |
1109 | if (err == ERROR_NO_MORE_ITEMS) | |
1110 | return 2; | |
1111 | CAPIerr(CAPI_F_CAPI_GET_PROVNAME, CAPI_R_CRYPTENUMPROVIDERS_ERROR); | |
1112 | capi_adderror(err); | |
1113 | return 0; | |
1114 | } | |
1115 | *pname = name; | |
1116 | CAPI_trace(ctx, "capi_get_provname, returned name=%s, type=%d\n", name, *ptype); | |
1117 | ||
1118 | return 1; | |
1119 | } | |
1120 | ||
1121 | static int capi_list_providers(CAPI_CTX *ctx, BIO *out) | |
1122 | { | |
1123 | DWORD idx, ptype; | |
1124 | int ret; | |
01483c26 | 1125 | LPSTR provname = NULL; |
7a18ecb2 DSH |
1126 | CAPI_trace(ctx, "capi_list_providers\n"); |
1127 | BIO_printf(out, "Available CSPs:\n"); | |
1128 | for(idx = 0; ; idx++) | |
1129 | { | |
1130 | ret = capi_get_provname(ctx, &provname, &ptype, idx); | |
1131 | if (ret == 2) | |
1132 | break; | |
1133 | if (ret == 0) | |
1134 | break; | |
1135 | BIO_printf(out, "%d. %s, type %d\n", idx, provname, ptype); | |
1136 | OPENSSL_free(provname); | |
1137 | } | |
1138 | return 1; | |
1139 | } | |
1140 | ||
1141 | static int capi_list_containers(CAPI_CTX *ctx, BIO *out) | |
1142 | { | |
1143 | int ret = 1; | |
1144 | HCRYPTPROV hprov; | |
1145 | DWORD err, idx, flags, buflen = 0, clen; | |
1146 | LPSTR cname; | |
1147 | CAPI_trace(ctx, "Listing containers CSP=%s, type = %d\n", ctx->cspname, ctx->csptype); | |
01483c26 | 1148 | if (!CryptAcquireContextA(&hprov, NULL, ctx->cspname, ctx->csptype, CRYPT_VERIFYCONTEXT)) |
7a18ecb2 DSH |
1149 | { |
1150 | CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, CAPI_R_CRYPTACQUIRECONTEXT_ERROR); | |
1151 | capi_addlasterror(); | |
1152 | return 0; | |
1153 | } | |
1154 | if (!CryptGetProvParam(hprov, PP_ENUMCONTAINERS, NULL, &buflen, CRYPT_FIRST)) | |
1155 | { | |
1156 | CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, CAPI_R_ENUMCONTAINERS_ERROR); | |
1157 | capi_addlasterror(); | |
1158 | return 0; | |
1159 | } | |
1160 | CAPI_trace(ctx, "Got max container len %d\n", buflen); | |
1161 | if (buflen == 0) | |
1162 | buflen = 1024; | |
1163 | cname = OPENSSL_malloc(buflen); | |
1164 | if (!cname) | |
1165 | { | |
1166 | CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, ERR_R_MALLOC_FAILURE); | |
1167 | goto err; | |
1168 | } | |
1169 | ||
1170 | for (idx = 0;;idx++) | |
1171 | { | |
2aa2a577 DSH |
1172 | clen = buflen; |
1173 | cname[0] = 0; | |
1174 | ||
1175 | if (idx == 0) | |
1176 | flags = CRYPT_FIRST; | |
1177 | else | |
1178 | flags = 0; | |
1179 | if(!CryptGetProvParam(hprov, PP_ENUMCONTAINERS, cname, &clen, flags)) | |
1180 | { | |
1181 | err = GetLastError(); | |
1182 | if (err == ERROR_NO_MORE_ITEMS) | |
7a18ecb2 | 1183 | goto done; |
2aa2a577 DSH |
1184 | CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, CAPI_R_ENUMCONTAINERS_ERROR); |
1185 | capi_adderror(err); | |
1186 | goto err; | |
1187 | } | |
1188 | CAPI_trace(ctx, "Container name %s, len=%d, index=%d, flags=%d\n", cname, clen, idx, flags); | |
1189 | if (!cname[0] && (clen == buflen)) | |
1190 | { | |
1191 | CAPI_trace(ctx, "Enumerate bug: using workaround\n"); | |
1192 | goto done; | |
1193 | } | |
1194 | BIO_printf(out, "%d. %s\n", idx, cname); | |
7a18ecb2 DSH |
1195 | } |
1196 | err: | |
1197 | ||
1198 | ret = 0; | |
1199 | ||
1200 | done: | |
1201 | if (cname) | |
1202 | OPENSSL_free(cname); | |
1203 | CryptReleaseContext(hprov, 0); | |
1204 | ||
1205 | return ret; | |
1206 | } | |
1207 | ||
1208 | CRYPT_KEY_PROV_INFO *capi_get_prov_info(CAPI_CTX *ctx, PCCERT_CONTEXT cert) | |
1209 | { | |
1210 | DWORD len; | |
1211 | CRYPT_KEY_PROV_INFO *pinfo; | |
1212 | ||
1213 | if(!CertGetCertificateContextProperty(cert, CERT_KEY_PROV_INFO_PROP_ID, NULL, &len)) | |
1214 | return NULL; | |
1215 | pinfo = OPENSSL_malloc(len); | |
1216 | if (!pinfo) | |
1217 | { | |
1218 | CAPIerr(CAPI_F_CAPI_GET_PROV_INFO, ERR_R_MALLOC_FAILURE); | |
1219 | return NULL; | |
1220 | } | |
1221 | if(!CertGetCertificateContextProperty(cert, CERT_KEY_PROV_INFO_PROP_ID, pinfo, &len)) | |
1222 | { | |
1223 | CAPIerr(CAPI_F_CAPI_GET_PROV_INFO, CAPI_R_ERROR_GETTING_KEY_PROVIDER_INFO); | |
1224 | capi_addlasterror(); | |
1225 | OPENSSL_free(pinfo); | |
1226 | return NULL; | |
1227 | } | |
1228 | return pinfo; | |
1229 | } | |
1230 | ||
1231 | static void capi_dump_prov_info(CAPI_CTX *ctx, BIO *out, CRYPT_KEY_PROV_INFO *pinfo) | |
1232 | { | |
1233 | char *provname = NULL, *contname = NULL; | |
1234 | if (!pinfo) | |
1235 | { | |
1236 | BIO_printf(out, " No Private Key\n"); | |
1237 | return; | |
1238 | } | |
1239 | provname = wide_to_asc(pinfo->pwszProvName); | |
1240 | contname = wide_to_asc(pinfo->pwszContainerName); | |
1241 | if (!provname || !contname) | |
1242 | goto err; | |
1243 | ||
1244 | BIO_printf(out, " Private Key Info:\n"); | |
1245 | BIO_printf(out, " Provider Name: %s, Provider Type %d\n", provname, pinfo->dwProvType); | |
1246 | BIO_printf(out, " Container Name: %s, Key Type %d\n", contname, pinfo->dwKeySpec); | |
1247 | err: | |
1248 | if (provname) | |
1249 | OPENSSL_free(provname); | |
1250 | if (contname) | |
1251 | OPENSSL_free(contname); | |
1252 | } | |
1253 | ||
1254 | char * capi_cert_get_fname(CAPI_CTX *ctx, PCCERT_CONTEXT cert) | |
1255 | { | |
1256 | LPWSTR wfname; | |
1257 | DWORD dlen; | |
1258 | ||
1259 | CAPI_trace(ctx, "capi_cert_get_fname\n"); | |
1260 | if (!CertGetCertificateContextProperty(cert, CERT_FRIENDLY_NAME_PROP_ID, NULL, &dlen)) | |
1261 | return NULL; | |
1262 | wfname = OPENSSL_malloc(dlen); | |
1263 | if (CertGetCertificateContextProperty(cert, CERT_FRIENDLY_NAME_PROP_ID, wfname, &dlen)) | |
1264 | { | |
1265 | char *fname = wide_to_asc(wfname); | |
1266 | OPENSSL_free(wfname); | |
1267 | return fname; | |
1268 | } | |
1269 | CAPIerr(CAPI_F_CAPI_CERT_GET_FNAME, CAPI_R_ERROR_GETTING_FRIENDLY_NAME); | |
1270 | capi_addlasterror(); | |
1271 | ||
1272 | OPENSSL_free(wfname); | |
1273 | return NULL; | |
1274 | } | |
1275 | ||
1276 | ||
1277 | void capi_dump_cert(CAPI_CTX *ctx, BIO *out, PCCERT_CONTEXT cert) | |
1278 | { | |
1279 | X509 *x; | |
1280 | unsigned char *p; | |
1281 | unsigned long flags = ctx->dump_flags; | |
1282 | if (flags & CAPI_DMP_FNAME) | |
1283 | { | |
1284 | char *fname; | |
1285 | fname = capi_cert_get_fname(ctx, cert); | |
1286 | if (fname) | |
1287 | { | |
1288 | BIO_printf(out, " Friendly Name \"%s\"\n", fname); | |
1289 | OPENSSL_free(fname); | |
1290 | } | |
1291 | else | |
1292 | BIO_printf(out, " <No Friendly Name>\n"); | |
1293 | } | |
1294 | ||
1295 | p = cert->pbCertEncoded; | |
1296 | x = d2i_X509(NULL, &p, cert->cbCertEncoded); | |
1297 | if (!x) | |
1298 | BIO_printf(out, " <Can't parse certificate>\n"); | |
1299 | if (flags & CAPI_DMP_SUMMARY) | |
1300 | { | |
1301 | BIO_printf(out, " Subject: "); | |
1302 | X509_NAME_print_ex(out, X509_get_subject_name(x), 0, XN_FLAG_ONELINE); | |
1303 | BIO_printf(out, "\n Issuer: "); | |
1304 | X509_NAME_print_ex(out, X509_get_issuer_name(x), 0, XN_FLAG_ONELINE); | |
1305 | BIO_printf(out, "\n"); | |
1306 | } | |
1307 | if (flags & CAPI_DMP_FULL) | |
1308 | X509_print_ex(out, x, XN_FLAG_ONELINE,0); | |
1309 | ||
1310 | if (flags & CAPI_DMP_PKEYINFO) | |
1311 | { | |
1312 | CRYPT_KEY_PROV_INFO *pinfo; | |
1313 | pinfo = capi_get_prov_info(ctx, cert); | |
1314 | capi_dump_prov_info(ctx, out, pinfo); | |
1315 | if (pinfo) | |
1316 | OPENSSL_free(pinfo); | |
1317 | } | |
1318 | ||
1319 | if (flags & CAPI_DMP_PEM) | |
1320 | PEM_write_bio_X509(out, x); | |
1321 | X509_free(x); | |
1322 | } | |
1323 | ||
1324 | HCERTSTORE capi_open_store(CAPI_CTX *ctx, char *storename) | |
1325 | { | |
1326 | HCERTSTORE hstore; | |
1327 | ||
1328 | if (!storename) | |
1329 | storename = ctx->storename; | |
1330 | if (!storename) | |
1331 | storename = "MY"; | |
1332 | CAPI_trace(ctx, "Opening certificate store %s\n", storename); | |
1333 | ||
46d47828 DSH |
1334 | hstore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, 0, |
1335 | ctx->store_flags, storename); | |
7a18ecb2 DSH |
1336 | if (!hstore) |
1337 | { | |
1338 | CAPIerr(CAPI_F_CAPI_OPEN_STORE, CAPI_R_ERROR_OPENING_STORE); | |
1339 | capi_addlasterror(); | |
1340 | } | |
1341 | return hstore; | |
1342 | } | |
1343 | ||
1344 | int capi_list_certs(CAPI_CTX *ctx, BIO *out, char *id) | |
1345 | { | |
1346 | char *storename; | |
1347 | int idx; | |
1348 | int ret = 1; | |
1349 | HCERTSTORE hstore; | |
1350 | PCCERT_CONTEXT cert = NULL; | |
1351 | ||
1352 | storename = ctx->storename; | |
1353 | if (!storename) | |
1354 | storename = "MY"; | |
1355 | CAPI_trace(ctx, "Listing certs for store %s\n", storename); | |
1356 | ||
1357 | hstore = capi_open_store(ctx, storename); | |
1358 | if (!hstore) | |
1359 | return 0; | |
1360 | if (id) | |
1361 | { | |
1362 | cert = capi_find_cert(ctx, id, hstore); | |
1363 | if (!cert) | |
1364 | { | |
1365 | ret = 0; | |
1366 | goto err; | |
1367 | } | |
1368 | capi_dump_cert(ctx, out, cert); | |
1369 | CertFreeCertificateContext(cert); | |
1370 | } | |
1371 | else | |
1372 | { | |
1373 | for(idx = 0;;idx++) | |
1374 | { | |
1375 | LPWSTR fname = NULL; | |
1376 | cert = CertEnumCertificatesInStore(hstore, cert); | |
1377 | if (!cert) | |
1378 | break; | |
1379 | BIO_printf(out, "Certificate %d\n", idx); | |
1380 | capi_dump_cert(ctx, out, cert); | |
1381 | } | |
1382 | } | |
1383 | err: | |
1384 | CertCloseStore(hstore, 0); | |
1385 | return ret; | |
1386 | } | |
1387 | ||
1388 | static PCCERT_CONTEXT capi_find_cert(CAPI_CTX *ctx, const char *id, HCERTSTORE hstore) | |
1389 | { | |
1390 | PCCERT_CONTEXT cert = NULL; | |
1391 | char *fname = NULL; | |
1392 | int match; | |
1393 | switch(ctx->lookup_method) | |
1394 | { | |
1395 | case CAPI_LU_SUBSTR: | |
2aa2a577 DSH |
1396 | return CertFindCertificateInStore(hstore, |
1397 | X509_ASN_ENCODING, 0, | |
1398 | CERT_FIND_SUBJECT_STR_A, id, NULL); | |
7a18ecb2 DSH |
1399 | case CAPI_LU_FNAME: |
1400 | for(;;) | |
1401 | { | |
1402 | cert = CertEnumCertificatesInStore(hstore, cert); | |
1403 | if (!cert) | |
1404 | return NULL; | |
1405 | fname = capi_cert_get_fname(ctx, cert); | |
1406 | if (fname) | |
1407 | { | |
1408 | if (strcmp(fname, id)) | |
1409 | match = 0; | |
1410 | else | |
1411 | match = 1; | |
1412 | OPENSSL_free(fname); | |
1413 | if (match) | |
1414 | return cert; | |
1415 | } | |
1416 | } | |
1417 | default: | |
1418 | return NULL; | |
1419 | } | |
1420 | } | |
1421 | ||
1422 | static CAPI_KEY *capi_get_key(CAPI_CTX *ctx, const char *contname, char *provname, DWORD ptype, DWORD keyspec) | |
1423 | { | |
1424 | CAPI_KEY *key; | |
1425 | key = OPENSSL_malloc(sizeof(CAPI_KEY)); | |
1426 | CAPI_trace(ctx, "capi_get_key, contname=%s, provname=%s, type=%d\n", | |
2aa2a577 | 1427 | contname, provname, ptype); |
01483c26 | 1428 | if (!CryptAcquireContextA(&key->hprov, contname, provname, ptype, 0)) |
7a18ecb2 DSH |
1429 | { |
1430 | CAPIerr(CAPI_F_CAPI_GET_KEY, CAPI_R_CRYPTACQUIRECONTEXT_ERROR); | |
1431 | capi_addlasterror(); | |
1432 | goto err; | |
1433 | } | |
1434 | if (!CryptGetUserKey(key->hprov, keyspec, &key->key)) | |
1435 | { | |
1436 | CAPIerr(CAPI_F_CAPI_GET_KEY, CAPI_R_GETUSERKEY_ERROR); | |
1437 | capi_addlasterror(); | |
1438 | CryptReleaseContext(key->hprov, 0); | |
1439 | goto err; | |
1440 | } | |
4be0a5d4 | 1441 | key->keyspec = keyspec; |
7d537d4f | 1442 | key->pcert = NULL; |
7a18ecb2 DSH |
1443 | return key; |
1444 | ||
1445 | err: | |
1446 | OPENSSL_free(key); | |
1447 | return NULL; | |
1448 | } | |
1449 | ||
1450 | static CAPI_KEY *capi_get_cert_key(CAPI_CTX *ctx, PCCERT_CONTEXT cert) | |
1451 | { | |
46d47828 | 1452 | CAPI_KEY *key = NULL; |
7a18ecb2 DSH |
1453 | CRYPT_KEY_PROV_INFO *pinfo = NULL; |
1454 | char *provname = NULL, *contname = NULL; | |
1455 | pinfo = capi_get_prov_info(ctx, cert); | |
1456 | if (!pinfo) | |
1457 | goto err; | |
1458 | provname = wide_to_asc(pinfo->pwszProvName); | |
1459 | contname = wide_to_asc(pinfo->pwszContainerName); | |
1460 | if (!provname || !contname) | |
46d47828 | 1461 | goto err; |
2aa2a577 DSH |
1462 | key = capi_get_key(ctx, contname, provname, |
1463 | pinfo->dwProvType, pinfo->dwKeySpec); | |
7a18ecb2 DSH |
1464 | |
1465 | err: | |
1466 | if (pinfo) | |
1467 | OPENSSL_free(pinfo); | |
1468 | if (provname) | |
1469 | OPENSSL_free(provname); | |
1470 | if (contname) | |
1471 | OPENSSL_free(contname); | |
1472 | return key; | |
1473 | } | |
1474 | ||
1475 | CAPI_KEY *capi_find_key(CAPI_CTX *ctx, const char *id) | |
1476 | { | |
1477 | PCCERT_CONTEXT cert; | |
1478 | HCERTSTORE hstore; | |
1479 | CAPI_KEY *key = NULL; | |
1480 | switch (ctx->lookup_method) | |
1481 | { | |
1482 | case CAPI_LU_SUBSTR: | |
1483 | case CAPI_LU_FNAME: | |
1484 | hstore = capi_open_store(ctx, NULL); | |
1485 | if (!hstore) | |
1486 | return NULL; | |
1487 | cert = capi_find_cert(ctx, id, hstore); | |
1488 | if (cert) | |
1489 | { | |
1490 | key = capi_get_cert_key(ctx, cert); | |
1491 | CertFreeCertificateContext(cert); | |
1492 | } | |
1493 | CertCloseStore(hstore, 0); | |
1494 | break; | |
1495 | ||
1496 | case CAPI_LU_CONTNAME: | |
2aa2a577 DSH |
1497 | key = capi_get_key(ctx, id, ctx->cspname, ctx->csptype, |
1498 | ctx->keytype); | |
7a18ecb2 DSH |
1499 | break; |
1500 | } | |
1501 | ||
1502 | return key; | |
1503 | } | |
1504 | ||
1505 | void capi_free_key(CAPI_KEY *key) | |
1506 | { | |
1507 | if (!key) | |
1508 | return; | |
1509 | CryptDestroyKey(key->key); | |
1510 | CryptReleaseContext(key->hprov, 0); | |
7d537d4f DSH |
1511 | if (key->pcert) |
1512 | CertFreeCertificateContext(key->pcert); | |
7a18ecb2 DSH |
1513 | OPENSSL_free(key); |
1514 | } | |
1515 | ||
1516 | ||
1517 | /* Initialize a CAPI_CTX structure */ | |
1518 | ||
1519 | static CAPI_CTX *capi_ctx_new() | |
1520 | { | |
1521 | CAPI_CTX *ctx; | |
1522 | ctx = OPENSSL_malloc(sizeof(CAPI_CTX)); | |
1523 | if (!ctx) | |
1524 | { | |
1525 | CAPIerr(CAPI_F_CAPI_CTX_NEW, ERR_R_MALLOC_FAILURE); | |
1526 | return NULL; | |
1527 | } | |
1528 | ctx->cspname = NULL; | |
1529 | ctx->csptype = PROV_RSA_FULL; | |
1530 | ctx->dump_flags = CAPI_DMP_SUMMARY|CAPI_DMP_FNAME; | |
1531 | ctx->keytype = AT_KEYEXCHANGE; | |
1532 | ctx->storename = NULL; | |
b3c8dd4e | 1533 | ctx->ssl_client_store = NULL; |
46d47828 DSH |
1534 | ctx->store_flags = CERT_STORE_OPEN_EXISTING_FLAG | |
1535 | CERT_STORE_READONLY_FLAG | | |
1536 | CERT_SYSTEM_STORE_CURRENT_USER; | |
7a18ecb2 DSH |
1537 | ctx->lookup_method = CAPI_LU_SUBSTR; |
1538 | ctx->debug_level = 0; | |
1539 | ctx->debug_file = NULL; | |
1cd504e7 | 1540 | ctx->client_cert_select = cert_select_simple; |
7a18ecb2 DSH |
1541 | return ctx; |
1542 | } | |
1543 | ||
1544 | static void capi_ctx_free(CAPI_CTX *ctx) | |
1545 | { | |
1546 | CAPI_trace(ctx, "Calling capi_ctx_free with %lx\n", ctx); | |
1547 | if (!ctx) | |
1548 | return; | |
1549 | if (ctx->cspname) | |
1550 | OPENSSL_free(ctx->cspname); | |
1551 | if (ctx->debug_file) | |
1552 | OPENSSL_free(ctx->debug_file); | |
1553 | if (ctx->storename) | |
1554 | OPENSSL_free(ctx->storename); | |
b3c8dd4e DSH |
1555 | if (ctx->ssl_client_store) |
1556 | OPENSSL_free(ctx->ssl_client_store); | |
7a18ecb2 DSH |
1557 | OPENSSL_free(ctx); |
1558 | } | |
1559 | ||
1560 | static int capi_ctx_set_provname(CAPI_CTX *ctx, LPSTR pname, DWORD type, int check) | |
1561 | { | |
1562 | CAPI_trace(ctx, "capi_ctx_set_provname, name=%s, type=%d\n", pname, type); | |
1563 | if (check) | |
1564 | { | |
1565 | HCRYPTPROV hprov; | |
01483c26 | 1566 | if (!CryptAcquireContextA(&hprov, NULL, pname, type, |
2aa2a577 | 1567 | CRYPT_VERIFYCONTEXT)) |
7a18ecb2 DSH |
1568 | { |
1569 | CAPIerr(CAPI_F_CAPI_CTX_SET_PROVNAME, CAPI_R_CRYPTACQUIRECONTEXT_ERROR); | |
1570 | capi_addlasterror(); | |
1571 | return 0; | |
1572 | } | |
1573 | CryptReleaseContext(hprov, 0); | |
1574 | } | |
1575 | ctx->cspname = BUF_strdup(pname); | |
1576 | ctx->csptype = type; | |
1577 | return 1; | |
1578 | } | |
1579 | ||
1580 | static int capi_ctx_set_provname_idx(CAPI_CTX *ctx, int idx) | |
1581 | { | |
1582 | LPSTR pname; | |
1583 | DWORD type; | |
1584 | if (capi_get_provname(ctx, &pname, &type, idx) != 1) | |
1585 | return 0; | |
1586 | return capi_ctx_set_provname(ctx, pname, type, 0); | |
1587 | } | |
1588 | ||
b3c8dd4e DSH |
1589 | static int cert_issuer_match(STACK_OF(X509_NAME) *ca_dn, X509 *x) |
1590 | { | |
1591 | int i; | |
1592 | X509_NAME *nm; | |
ca89fc1f DSH |
1593 | /* Special case: empty list: match anything */ |
1594 | if (sk_X509_NAME_num(ca_dn) <= 0) | |
1595 | return 1; | |
b3c8dd4e DSH |
1596 | for (i = 0; i < sk_X509_NAME_num(ca_dn); i++) |
1597 | { | |
1598 | nm = sk_X509_NAME_value(ca_dn, i); | |
1599 | if (!X509_NAME_cmp(nm, X509_get_issuer_name(x))) | |
1600 | return 1; | |
1601 | } | |
1602 | return 0; | |
1603 | } | |
1604 | ||
e0f7b872 | 1605 | |
7d537d4f | 1606 | |
b3c8dd4e DSH |
1607 | static int capi_load_ssl_client_cert(ENGINE *e, SSL *ssl, |
1608 | STACK_OF(X509_NAME) *ca_dn, X509 **pcert, EVP_PKEY **pkey, | |
1609 | STACK_OF(X509) **pother, UI_METHOD *ui_method, void *callback_data) | |
1610 | { | |
b3c8dd4e | 1611 | STACK_OF(X509) *certs = NULL; |
b3c8dd4e | 1612 | X509 *x; |
b3c8dd4e DSH |
1613 | char *storename; |
1614 | const char *p; | |
7d537d4f | 1615 | int i, client_cert_idx; |
b3c8dd4e | 1616 | HCERTSTORE hstore; |
7d537d4f | 1617 | PCCERT_CONTEXT cert = NULL, excert = NULL; |
b3c8dd4e | 1618 | CAPI_CTX *ctx; |
7d537d4f | 1619 | CAPI_KEY *key; |
b3c8dd4e DSH |
1620 | ctx = ENGINE_get_ex_data(e, capi_idx); |
1621 | ||
1622 | *pcert = NULL; | |
1623 | *pkey = NULL; | |
1624 | ||
1625 | storename = ctx->ssl_client_store; | |
1626 | if (!storename) | |
1627 | storename = "MY"; | |
1628 | ||
1629 | hstore = capi_open_store(ctx, storename); | |
1630 | if (!hstore) | |
1631 | return 0; | |
b814c01a | 1632 | /* Enumerate all certificates collect any matches */ |
7d537d4f | 1633 | for(i = 0;;i++) |
b3c8dd4e DSH |
1634 | { |
1635 | cert = CertEnumCertificatesInStore(hstore, cert); | |
1636 | if (!cert) | |
1637 | break; | |
1638 | p = cert->pbCertEncoded; | |
1639 | x = d2i_X509(NULL, &p, cert->cbCertEncoded); | |
1640 | if (!x) | |
1641 | { | |
1642 | CAPI_trace(ctx, "Can't Parse Certificate %d\n", i); | |
1643 | continue; | |
1644 | } | |
2836cb38 DSH |
1645 | if (cert_issuer_match(ca_dn, x) |
1646 | && X509_check_purpose(x, X509_PURPOSE_SSL_CLIENT, 0)) | |
b3c8dd4e | 1647 | { |
7d537d4f | 1648 | key = capi_get_cert_key(ctx, cert); |
b3c8dd4e | 1649 | if (!key) |
2836cb38 DSH |
1650 | { |
1651 | X509_free(x); | |
b3c8dd4e | 1652 | continue; |
2836cb38 | 1653 | } |
b814c01a DSH |
1654 | /* Match found: attach extra data to it so |
1655 | * we can retrieve the key later. | |
1656 | */ | |
7d537d4f | 1657 | excert = CertDuplicateCertificateContext(cert); |
e0f7b872 | 1658 | key->pcert = excert; |
7d537d4f DSH |
1659 | X509_set_ex_data(x, cert_capi_idx, key); |
1660 | ||
1661 | if (!certs) | |
1662 | certs = sk_X509_new_null(); | |
1663 | ||
1664 | sk_X509_push(certs, x); | |
b3c8dd4e DSH |
1665 | } |
1666 | else | |
1667 | X509_free(x); | |
1668 | ||
1669 | } | |
1670 | ||
1671 | if (cert) | |
1672 | CertFreeCertificateContext(cert); | |
e0f7b872 DSH |
1673 | if (hstore) |
1674 | CertCloseStore(hstore, 0); | |
b3c8dd4e | 1675 | |
7d537d4f | 1676 | if (!certs) |
b3c8dd4e DSH |
1677 | return 0; |
1678 | ||
b814c01a DSH |
1679 | |
1680 | /* Select the appropriate certificate */ | |
1681 | ||
1cd504e7 | 1682 | client_cert_idx = ctx->client_cert_select(e, ssl, certs); |
7d537d4f | 1683 | |
b814c01a DSH |
1684 | /* Set the selected certificate and free the rest */ |
1685 | ||
7d537d4f DSH |
1686 | for(i = 0; i < sk_X509_num(certs); i++) |
1687 | { | |
1688 | x = sk_X509_value(certs, i); | |
1689 | if (i == client_cert_idx) | |
1690 | *pcert = x; | |
1691 | else | |
1692 | { | |
1693 | key = X509_get_ex_data(x, cert_capi_idx); | |
1694 | capi_free_key(key); | |
1695 | X509_free(x); | |
1696 | } | |
1697 | } | |
1698 | ||
1699 | sk_X509_free(certs); | |
1700 | ||
1701 | if (!*pcert) | |
1702 | return 0; | |
1703 | ||
b814c01a DSH |
1704 | /* Setup key for selected certificate */ |
1705 | ||
7d537d4f DSH |
1706 | key = X509_get_ex_data(*pcert, cert_capi_idx); |
1707 | *pkey = capi_get_pkey(e, key); | |
1708 | X509_set_ex_data(*pcert, cert_capi_idx, NULL); | |
1709 | ||
1710 | return 1; | |
1711 | ||
b3c8dd4e DSH |
1712 | } |
1713 | ||
e0f7b872 DSH |
1714 | |
1715 | /* Simple client cert selection function: always select first */ | |
1716 | ||
1cd504e7 | 1717 | static int cert_select_simple(ENGINE *e, SSL *ssl, STACK_OF(X509) *certs) |
e0f7b872 DSH |
1718 | { |
1719 | return 0; | |
1720 | } | |
1721 | ||
1cd504e7 | 1722 | #ifdef OPENSSL_CAPIENG_DIALOG |
e0f7b872 DSH |
1723 | |
1724 | /* More complex cert selection function, using standard function | |
1725 | * CryptUIDlgSelectCertificateFromStore() to produce a dialog box. | |
1726 | */ | |
1727 | ||
a0f3679b DSH |
1728 | /* Definitions which are in cryptuiapi.h but this is not present in older |
1729 | * versions of headers. | |
1730 | */ | |
1731 | ||
1732 | #ifndef CRYPTUI_SELECT_LOCATION_COLUMN | |
1733 | #define CRYPTUI_SELECT_LOCATION_COLUMN 0x000000010 | |
1734 | #define CRYPTUI_SELECT_INTENDEDUSE_COLUMN 0x000000004 | |
1735 | #endif | |
e0f7b872 | 1736 | |
1381bf90 DSH |
1737 | #define dlg_title L"OpenSSL Application SSL Client Certificate Selection" |
1738 | #define dlg_prompt L"Select a certificate to use for authentication" | |
e1451bb5 DSH |
1739 | #define dlg_columns CRYPTUI_SELECT_LOCATION_COLUMN \ |
1740 | |CRYPTUI_SELECT_INTENDEDUSE_COLUMN | |
1381bf90 | 1741 | |
1cd504e7 | 1742 | static int cert_select_dialog(ENGINE *e, SSL *ssl, STACK_OF(X509) *certs) |
e0f7b872 DSH |
1743 | { |
1744 | X509 *x; | |
1745 | HCERTSTORE dstore; | |
1746 | PCCERT_CONTEXT cert; | |
1747 | CAPI_CTX *ctx; | |
1748 | CAPI_KEY *key; | |
1381bf90 | 1749 | HWND hwnd; |
e0f7b872 | 1750 | int i, idx = -1; |
ffc2b3e9 DSH |
1751 | if (sk_X509_num(certs) == 1) |
1752 | return 0; | |
e0f7b872 DSH |
1753 | ctx = ENGINE_get_ex_data(e, capi_idx); |
1754 | /* Create an in memory store of certificates */ | |
1755 | dstore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0, | |
1756 | CERT_STORE_CREATE_NEW_FLAG, NULL); | |
1757 | if (!dstore) | |
1758 | { | |
9be8035b | 1759 | CAPIerr(CAPI_F_CERT_SELECT_DIALOG, CAPI_R_ERROR_CREATING_STORE); |
e0f7b872 DSH |
1760 | capi_addlasterror(); |
1761 | goto err; | |
1762 | } | |
1763 | /* Add all certificates to store */ | |
1764 | for(i = 0; i < sk_X509_num(certs); i++) | |
1765 | { | |
1766 | x = sk_X509_value(certs, i); | |
1767 | key = X509_get_ex_data(x, cert_capi_idx); | |
1768 | ||
1769 | if (!CertAddCertificateContextToStore(dstore, key->pcert, | |
1770 | CERT_STORE_ADD_NEW, NULL)) | |
1771 | { | |
9be8035b | 1772 | CAPIerr(CAPI_F_CERT_SELECT_DIALOG, CAPI_R_ERROR_ADDING_CERT); |
e0f7b872 DSH |
1773 | capi_addlasterror(); |
1774 | goto err; | |
1775 | } | |
1776 | ||
1777 | } | |
a0f3679b DSH |
1778 | hwnd = GetForegroundWindow(); |
1779 | if (!hwnd) | |
1780 | hwnd = GetActiveWindow(); | |
1cd504e7 DSH |
1781 | if (!hwnd && ctx->getconswindow) |
1782 | hwnd = ctx->getconswindow(); | |
e0f7b872 | 1783 | /* Call dialog to select one */ |
1cd504e7 DSH |
1784 | cert = ctx->certselectdlg(dstore, hwnd, dlg_title, dlg_prompt, |
1785 | dlg_columns, 0, NULL); | |
e0f7b872 DSH |
1786 | |
1787 | /* Find matching cert from list */ | |
1788 | if (cert) | |
1789 | { | |
1790 | for(i = 0; i < sk_X509_num(certs); i++) | |
1791 | { | |
1792 | x = sk_X509_value(certs, i); | |
1793 | key = X509_get_ex_data(x, cert_capi_idx); | |
1794 | if (CertCompareCertificate( | |
1795 | X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, | |
1796 | cert->pCertInfo, | |
1797 | key->pcert->pCertInfo)) | |
1798 | { | |
1799 | idx = i; | |
1800 | break; | |
1801 | } | |
1802 | } | |
1803 | } | |
1804 | ||
1805 | err: | |
1806 | if (dstore) | |
1807 | CertCloseStore(dstore, 0); | |
1808 | return idx; | |
1809 | ||
1810 | } | |
1811 | #endif | |
1812 | ||
f87e3078 | 1813 | #else /* !__COMPILE_CAPIENG */ |
492279f6 AP |
1814 | #include <openssl/engine.h> |
1815 | #ifndef OPENSSL_NO_DYNAMIC_ENGINE | |
1816 | OPENSSL_EXPORT | |
1817 | int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns) { return 0; } | |
1818 | IMPLEMENT_DYNAMIC_CHECK_FN() | |
f87e3078 AP |
1819 | #else |
1820 | void ENGINE_load_capi(void){} | |
7a18ecb2 DSH |
1821 | #endif |
1822 | #endif |