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